3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
14 #include "isl_id_to_pet_expr.h"
22 #ifndef HAVE_STMTRANGE
23 /* StmtRange was replaced by iterator_range in more recent versions of clang.
24 * Implement a StmtRange in terms of this iterator_range if StmtRange
27 struct StmtRange
: std::pair
<StmtIterator
,StmtIterator
> {
28 StmtRange(const StmtIterator
&begin
, const StmtIterator
&end
) :
29 std::pair
<StmtIterator
,StmtIterator
>(begin
, end
) {}
30 StmtRange(Stmt::child_range range
) :
31 std::pair
<StmtIterator
,StmtIterator
>(range
.begin(),
38 /* The location of the scop, as delimited by scop and endscop
39 * pragmas by the user.
40 * "scop" and "endscop" are the source locations of the scop and
42 * "start_line" is the line number of the start position.
47 clang::SourceLocation scop
;
48 clang::SourceLocation endscop
;
54 /* The information extracted from a pragma pencil independent.
55 * We currently only keep track of the line number where
59 Independent(unsigned line
) : line(line
) {}
64 /* Compare two TypeDecl pointers based on their names.
67 bool operator()(const clang::TypeDecl
*x
,
68 const clang::TypeDecl
*y
) {
69 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
73 /* The PetTypes structure collects a set of RecordDecl and
74 * TypedefNameDecl pointers.
75 * The pointers are sorted using a fixed order. The actual order
76 * is not important, only that it is consistent across platforms.
79 std::set
<clang::RecordDecl
*, less_name
> records
;
80 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
82 void insert(clang::RecordDecl
*decl
) {
85 void insert(clang::TypedefNameDecl
*decl
) {
86 typedefs
.insert(decl
);
91 clang::Preprocessor
&PP
;
92 clang::ASTContext
&ast_context
;
93 /* The DeclContext of the function containing the scop.
95 clang::DeclContext
*decl_context
;
96 /* If autodetect is false, then loc contains the location
97 * of the scop to be extracted.
101 pet_options
*options
;
102 /* Set if the pet_scop returned by an extract method only
103 * represents part of the input tree.
107 /* A cache of size expressions for array identifiers as computed
108 * by PetScan::get_array_size, or set by PetScan::set_array_size.
110 isl_id_to_pet_expr
*id_size
;
111 /* A cache of size expressions for array types as computed
112 * by PetScan::get_array_size.
114 std::map
<const clang::Type
*, pet_expr
*> type_size
;
116 /* A cache of funtion summaries for function declarations
117 * as extracted by PetScan::get_summary.
119 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
121 /* A union of mappings of the form
122 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
124 isl_union_map
*value_bounds
;
126 /* The line number of the previously considered Stmt. */
128 /* The line number of the Stmt currently being considered. */
129 unsigned current_line
;
130 /* Information about the independent pragmas in the source code. */
131 std::vector
<Independent
> &independent
;
133 /* All variables that have already been declared
134 * in the current compound statement.
136 std::vector
<clang::VarDecl
*> declarations
;
137 /* Sequence number of the next rename. */
139 /* Have the declared names been collected? */
140 bool declared_names_collected
;
141 /* The names of the variables declared in decl_context,
142 * if declared_names_collected is set.
144 std::set
<std::string
> declared_names
;
145 /* A set of names known to be in use. */
146 std::set
<std::string
> used_names
;
148 /* Sequence number of the next temporary inlined argument variable. */
151 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
152 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
153 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
154 std::vector
<Independent
> &independent
) :
156 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
157 ctx(isl_union_map_get_ctx(value_bounds
)),
158 options(options
), partial(false), value_bounds(value_bounds
),
159 last_line(0), current_line(0),
160 independent(independent
), n_rename(0),
161 declared_names_collected(false), n_arg(0) {
162 id_size
= isl_id_to_pet_expr_alloc(ctx
, 0);
167 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
169 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
170 clang::IntegerLiteral
*expr
);
171 __isl_give pet_expr
*get_array_size(__isl_keep isl_id
*id
);
172 void set_array_size(__isl_take isl_id
*id
, __isl_take pet_expr
*size
);
173 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
174 PetTypes
*types
, __isl_keep pet_context
*pc
);
176 void set_current_stmt(clang::Stmt
*stmt
);
177 bool is_current_stmt_marked_independent();
179 void collect_declared_names();
180 void add_new_used_names(const std::set
<std::string
> &used_names
);
181 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
182 std::string
generate_new_name(const std::string
&name
);
184 __isl_give pet_tree
*add_kills(__isl_take pet_tree
*tree
,
185 std::set
<clang::ValueDecl
*> locals
);
187 struct pet_scop
*scan(clang::Stmt
*stmt
);
189 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
190 __isl_keep pet_context
*pc
);
191 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
192 PetTypes
*types
, __isl_keep pet_context
*pc
);
193 struct pet_array
*extract_array(__isl_keep isl_id_list
*decls
,
194 PetTypes
*types
, __isl_keep pet_context
*pc
);
195 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
196 clang::QualType qt
, int pos
);
197 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
198 __isl_keep pet_context
*pc
);
199 int substitute_array_sizes(__isl_keep pet_tree
*tree
,
200 pet_substituter
*substituter
);
202 __isl_give pet_tree
*insert_initial_declarations(
203 __isl_take pet_tree
*tree
, int n_decl
,
204 clang::StmtRange stmt_range
);
205 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
206 bool skip_declarations
= false);
207 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
208 bool skip_declarations
, clang::Stmt
*parent
);
209 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
210 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
211 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
212 bool skip_declarations
= false);
213 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
214 __isl_give pet_tree
*extract(clang::Decl
*decl
);
215 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
217 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
219 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
220 clang::SourceRange range
, bool skip_semi
);
221 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
224 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
226 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
227 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
228 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
229 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
231 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
232 clang::ValueDecl
*iv
);
233 __isl_give pet_expr
*extract_binary_increment(
234 clang::BinaryOperator
*op
,
235 clang::ValueDecl
*iv
);
236 __isl_give pet_expr
*extract_compound_increment(
237 clang::CompoundAssignOperator
*op
,
238 clang::ValueDecl
*iv
);
239 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
240 clang::ValueDecl
*iv
);
241 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
242 __isl_give pet_tree
*extract_expr_stmt(clang::Stmt
*stmt
);
243 __isl_give pet_tree
*extract_inlined_call(clang::CallExpr
*call
,
244 clang::FunctionDecl
*fd
);
245 int set_inliner_arguments(pet_inliner
&inliner
, clang::CallExpr
*call
,
246 clang::FunctionDecl
*fd
);
248 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
249 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
250 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
251 clang::FunctionDecl
*fd
);
252 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
253 clang::Expr
*expr
, bool detect_writes
);
254 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
255 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
256 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
257 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
258 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
259 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
260 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
261 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
262 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
263 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
264 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
265 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
267 __isl_give pet_expr
*extract_access_expr(clang::QualType qt
,
268 __isl_take pet_expr
*index
);
269 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
270 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
272 __isl_give pet_expr
*extract_index_expr(
273 clang::ArraySubscriptExpr
*expr
);
274 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
275 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
276 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
277 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
278 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
280 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
281 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
283 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
285 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
287 void report(clang::SourceRange range
, unsigned id
);
288 void report(clang::Stmt
*stmt
, unsigned id
);
289 void report(clang::Decl
*decl
, unsigned id
);
290 void unsupported(clang::Stmt
*stmt
);
291 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
292 void report_unsupported_statement_type(clang::Stmt
*stmt
);
293 void report_prototype_required(clang::Stmt
*stmt
);
294 void report_missing_increment(clang::Stmt
*stmt
);
295 void report_missing_summary_function(clang::Stmt
*stmt
);
296 void report_missing_summary_function_body(clang::Stmt
*stmt
);
297 void report_unsupported_inline_function_argument(clang::Stmt
*stmt
);
298 void report_unsupported_declaration(clang::Decl
*decl
);
299 void report_unbalanced_pragmas(clang::SourceLocation scop
,
300 clang::SourceLocation endscop
);