7 #include <clang/Basic/SourceManager.h>
8 #include <clang/AST/Decl.h>
9 #include <clang/AST/Stmt.h>
10 #include <clang/Lex/Preprocessor.h>
18 #include "isl_id_to_pet_expr.h"
26 #ifndef HAVE_STMTRANGE
27 /* StmtRange was replaced by iterator_range in more recent versions of clang.
28 * Implement a StmtRange in terms of this iterator_range if StmtRange
31 struct StmtRange
: std::pair
<StmtIterator
,StmtIterator
> {
32 StmtRange(const StmtIterator
&begin
, const StmtIterator
&end
) :
33 std::pair
<StmtIterator
,StmtIterator
>(begin
, end
) {}
34 StmtRange(Stmt::child_range range
) :
35 std::pair
<StmtIterator
,StmtIterator
>(range
.begin(),
42 /* The location of the scop, as delimited by scop and endscop
43 * pragmas by the user.
44 * "scop" and "endscop" are the source locations of the scop and
46 * "start_line" is the line number of the start position.
51 clang::SourceLocation scop
;
52 clang::SourceLocation endscop
;
58 /* The information extracted from a pragma pencil independent.
59 * We currently only keep track of the line number where
63 Independent(unsigned line
) : line(line
) {}
68 /* Compare two TypeDecl pointers based on their names.
71 bool operator()(const clang::TypeDecl
*x
,
72 const clang::TypeDecl
*y
) {
73 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
77 /* The PetTypes structure collects a set of RecordDecl and
78 * TypedefNameDecl pointers.
79 * The pointers are sorted using a fixed order. The actual order
80 * is not important, only that it is consistent across platforms.
83 std::set
<clang::RecordDecl
*, less_name
> records
;
84 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
86 void insert(clang::RecordDecl
*decl
) {
89 void insert(clang::TypedefNameDecl
*decl
) {
90 typedefs
.insert(decl
);
95 clang::Preprocessor
&PP
;
96 clang::ASTContext
&ast_context
;
97 /* The DeclContext of the function containing the scop.
99 clang::DeclContext
*decl_context
;
100 /* If autodetect is false, then loc contains the location
101 * of the scop to be extracted.
105 pet_options
*options
;
106 /* If not NULL, then return_root represents the compound statement
107 * in which a return statement is allowed as the final child.
108 * If return_root is NULL, then no return statements are allowed.
110 clang::Stmt
*return_root
;
111 /* Set if the pet_scop returned by an extract method only
112 * represents part of the input tree.
116 /* A cache of size expressions for array identifiers as computed
117 * by PetScan::get_array_size, or set by PetScan::set_array_size.
119 isl_id_to_pet_expr
*id_size
;
120 /* A cache of size expressions for array types as computed
121 * by PetScan::get_array_size.
123 std::map
<const clang::Type
*, pet_expr
*> type_size
;
125 /* A cache of funtion summaries for function declarations
126 * as extracted by PetScan::get_summary.
128 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
130 /* A union of mappings of the form
131 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
133 isl_union_map
*value_bounds
;
135 /* The line number of the previously considered Stmt. */
137 /* The line number of the Stmt currently being considered. */
138 unsigned current_line
;
139 /* Information about the independent pragmas in the source code. */
140 std::vector
<Independent
> &independent
;
142 /* All variables that have already been declared
143 * in the current compound statement.
145 std::vector
<clang::VarDecl
*> declarations
;
146 /* Sequence number of the next rename. */
148 /* Have the declared names been collected? */
149 bool declared_names_collected
;
150 /* The names of the variables declared in decl_context,
151 * if declared_names_collected is set.
153 std::set
<std::string
> declared_names
;
154 /* A set of names known to be in use. */
155 std::set
<std::string
> used_names
;
157 /* If not NULL, then "call2id" maps inlined call expressions
158 * that return a value to the corresponding variables.
160 std::map
<clang::Stmt
*, isl_id
*> *call2id
;
162 /* Sequence number of the next temporary inlined argument variable. */
164 /* Sequence number of the next temporary inlined return variable. */
167 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
168 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
169 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
170 std::vector
<Independent
> &independent
) :
172 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
173 ctx(isl_union_map_get_ctx(value_bounds
)),
174 options(options
), return_root(NULL
), partial(false),
175 value_bounds(value_bounds
), last_line(0), current_line(0),
176 independent(independent
), n_rename(0),
177 declared_names_collected(false), call2id(NULL
),
179 id_size
= isl_id_to_pet_expr_alloc(ctx
, 0);
184 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
186 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
187 clang::IntegerLiteral
*expr
);
188 __isl_give pet_expr
*get_array_size(__isl_keep isl_id
*id
);
189 void set_array_size(__isl_take isl_id
*id
, __isl_take pet_expr
*size
);
190 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
191 PetTypes
*types
, __isl_keep pet_context
*pc
);
192 __isl_give pet_tree
*extract_inlined_call(clang::CallExpr
*call
,
193 clang::FunctionDecl
*fd
, __isl_keep isl_id
*return_id
);
195 void set_current_stmt(clang::Stmt
*stmt
);
196 bool is_current_stmt_marked_independent();
198 void collect_declared_names();
199 void add_new_used_names(const std::set
<std::string
> &used_names
);
200 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
201 std::string
generate_new_name(const std::string
&name
);
203 __isl_give pet_tree
*add_kills(__isl_take pet_tree
*tree
,
204 std::set
<clang::ValueDecl
*> locals
);
206 struct pet_scop
*scan(clang::Stmt
*stmt
);
208 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
209 __isl_keep pet_context
*pc
);
210 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
211 PetTypes
*types
, __isl_keep pet_context
*pc
);
212 struct pet_array
*extract_array(__isl_keep isl_id_list
*decls
,
213 PetTypes
*types
, __isl_keep pet_context
*pc
);
214 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
215 clang::QualType qt
, int pos
);
216 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
217 __isl_keep pet_context
*pc
);
218 int substitute_array_sizes(__isl_keep pet_tree
*tree
,
219 pet_substituter
*substituter
);
221 __isl_give pet_tree
*insert_initial_declarations(
222 __isl_take pet_tree
*tree
, int n_decl
,
223 clang::StmtRange stmt_range
);
224 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
225 bool skip_declarations
= false);
226 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
227 bool skip_declarations
, clang::Stmt
*parent
);
228 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
229 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
230 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
231 bool skip_declarations
= false);
232 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
233 __isl_give pet_tree
*extract(clang::Decl
*decl
);
234 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
235 __isl_give pet_tree
*extract(clang::ReturnStmt
*stmt
);
237 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
239 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
240 clang::SourceRange range
, bool skip_semi
);
241 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
244 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
246 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
247 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
248 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
249 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
251 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
252 clang::ValueDecl
*iv
);
253 __isl_give pet_expr
*extract_binary_increment(
254 clang::BinaryOperator
*op
,
255 clang::ValueDecl
*iv
);
256 __isl_give pet_expr
*extract_compound_increment(
257 clang::CompoundAssignOperator
*op
,
258 clang::ValueDecl
*iv
);
259 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
260 clang::ValueDecl
*iv
);
261 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
262 __isl_give pet_tree
*extract_expr_stmt(clang::Stmt
*stmt
);
263 int set_inliner_arguments(pet_inliner
&inliner
, clang::CallExpr
*call
,
264 clang::FunctionDecl
*fd
);
266 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
267 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
268 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
269 clang::FunctionDecl
*fd
);
270 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
271 clang::Expr
*expr
, bool detect_writes
);
272 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
273 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
274 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
275 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
276 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
277 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
278 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
279 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
280 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
281 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
282 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
283 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
285 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
286 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
288 __isl_give pet_expr
*extract_index_expr(
289 clang::ArraySubscriptExpr
*expr
);
290 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
291 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
292 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
293 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
294 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
296 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
297 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
299 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
301 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
303 void report(clang::SourceRange range
, unsigned id
);
304 void report(clang::Stmt
*stmt
, unsigned id
);
305 void report(clang::Decl
*decl
, unsigned id
);
306 void unsupported(clang::Stmt
*stmt
);
307 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
308 void report_unsupported_binary_operator(clang::Stmt
*stmt
);
309 void report_unsupported_statement_type(clang::Stmt
*stmt
);
310 void report_prototype_required(clang::Stmt
*stmt
);
311 void report_missing_increment(clang::Stmt
*stmt
);
312 void report_missing_summary_function(clang::Stmt
*stmt
);
313 void report_missing_summary_function_body(clang::Stmt
*stmt
);
314 void report_unsupported_inline_function_argument(clang::Stmt
*stmt
);
315 void report_unsupported_declaration(clang::Decl
*decl
);
316 void report_unbalanced_pragmas(clang::SourceLocation scop
,
317 clang::SourceLocation endscop
);
318 void report_unsupported_return(clang::Stmt
*stmt
);
319 void report_return_not_at_end_of_function(clang::Stmt
*stmt
);