3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
18 /* The location of the scop, as delimited by scop and endscop
19 * pragmas by the user.
20 * "start_line" is the line number of the start position.
30 /* The information extracted from a pragma pencil independent.
31 * We currently only keep track of the line number where
35 Independent(unsigned line
) : line(line
) {}
40 /* Compare two TypeDecl pointers based on their names.
43 bool operator()(const clang::TypeDecl
*x
,
44 const clang::TypeDecl
*y
) {
45 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
49 /* The PetTypes structure collects a set of RecordDecl and
50 * TypedefNameDecl pointers.
51 * The pointers are sorted using a fixed order. The actual order
52 * is not important, only that it is consistent across platforms.
55 std::set
<clang::RecordDecl
*, less_name
> records
;
56 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
58 void insert(clang::RecordDecl
*decl
) {
61 void insert(clang::TypedefNameDecl
*decl
) {
62 typedefs
.insert(decl
);
67 clang::Preprocessor
&PP
;
68 clang::ASTContext
&ast_context
;
69 /* The DeclContext of the function containing the scop.
71 clang::DeclContext
*decl_context
;
72 /* If autodetect is false, then loc contains the location
73 * of the scop to be extracted.
78 /* Set if the pet_scop returned by an extract method only
79 * represents part of the input tree.
83 /* A cache of size expressions for array types as computed
84 * by PetScan::get_array_size.
86 std::map
<const clang::Type
*, pet_expr
*> type_size
;
88 /* A cache of funtion summaries for function declarations
89 * as extracted by PetScan::get_summary.
91 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
93 /* A union of mappings of the form
94 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
96 isl_union_map
*value_bounds
;
98 /* The line number of the previously considered Stmt. */
100 /* The line number of the Stmt currently being considered. */
101 unsigned current_line
;
102 /* Information about the independent pragmas in the source code. */
103 std::vector
<Independent
> &independent
;
105 /* All variables that have already been declared
106 * in the current compound statement.
108 std::vector
<clang::VarDecl
*> declarations
;
109 /* Sequence number of the next rename. */
111 /* A set of names known to be in use. */
112 std::set
<std::string
> used_names
;
114 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
115 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
116 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
117 std::vector
<Independent
> &independent
) :
118 ctx(isl_union_map_get_ctx(value_bounds
)), PP(PP
),
119 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
120 options(options
), value_bounds(value_bounds
),
121 partial(false), last_line(0), current_line(0),
122 independent(independent
), n_rename(0) { }
126 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
128 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
129 clang::IntegerLiteral
*expr
);
130 __isl_give pet_expr
*get_array_size(const clang::Type
*type
);
131 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
132 PetTypes
*types
, __isl_keep pet_context
*pc
);
134 void set_current_stmt(clang::Stmt
*stmt
);
135 bool is_current_stmt_marked_independent();
137 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
138 std::string
generate_new_name(const std::string
&name
);
140 struct pet_scop
*scan(clang::Stmt
*stmt
);
142 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
143 __isl_keep pet_context
*pc
);
144 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
145 PetTypes
*types
, __isl_keep pet_context
*pc
);
146 struct pet_array
*extract_array(isl_ctx
*ctx
,
147 std::vector
<clang::ValueDecl
*> decls
,
148 PetTypes
*types
, __isl_keep pet_context
*pc
);
149 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
150 const clang::Type
*type
, int pos
);
151 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
152 const clang::Type
*type
, __isl_keep pet_context
*pc
);
154 __isl_give pet_tree
*insert_initial_declarations(
155 __isl_take pet_tree
*tree
, int n_decl
,
156 clang::StmtRange stmt_range
);
157 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
158 bool skip_declarations
= false);
159 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
160 bool skip_declarations
);
161 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
162 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
163 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
164 bool skip_declarations
= false);
165 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
166 __isl_give pet_tree
*extract(clang::Decl
*decl
);
167 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
169 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
171 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
172 clang::SourceRange range
, bool skip_semi
);
173 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
176 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
178 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
179 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
180 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
181 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
183 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
184 clang::ValueDecl
*iv
);
185 __isl_give pet_expr
*extract_binary_increment(
186 clang::BinaryOperator
*op
,
187 clang::ValueDecl
*iv
);
188 __isl_give pet_expr
*extract_compound_increment(
189 clang::CompoundAssignOperator
*op
,
190 clang::ValueDecl
*iv
);
191 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
192 clang::ValueDecl
*iv
);
193 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
195 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
196 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
197 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
198 clang::FunctionDecl
*fd
);
199 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
200 clang::Expr
*expr
, bool detect_writes
);
201 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
202 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
203 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
204 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
205 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
206 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
207 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
208 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
209 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
210 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
211 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
212 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
214 __isl_give pet_expr
*extract_access_expr(clang::QualType qt
,
215 __isl_take pet_expr
*index
);
216 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
217 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
219 __isl_give pet_expr
*extract_index_expr(
220 clang::ArraySubscriptExpr
*expr
);
221 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
222 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
223 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
224 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
225 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
227 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
228 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
230 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
232 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
234 void report(clang::Stmt
*stmt
, unsigned id
);
235 void unsupported(clang::Stmt
*stmt
);
236 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
237 void report_unsupported_statement_type(clang::Stmt
*stmt
);
238 void report_prototype_required(clang::Stmt
*stmt
);
239 void report_missing_increment(clang::Stmt
*stmt
);
240 void report_missing_summary_function(clang::Stmt
*stmt
);
241 void report_missing_summary_function_body(clang::Stmt
*stmt
);