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 * "start_line" is the line number of the start position.
50 /* The information extracted from a pragma pencil independent.
51 * We currently only keep track of the line number where
55 Independent(unsigned line
) : line(line
) {}
60 /* Compare two TypeDecl pointers based on their names.
63 bool operator()(const clang::TypeDecl
*x
,
64 const clang::TypeDecl
*y
) {
65 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
69 /* The PetTypes structure collects a set of RecordDecl and
70 * TypedefNameDecl pointers.
71 * The pointers are sorted using a fixed order. The actual order
72 * is not important, only that it is consistent across platforms.
75 std::set
<clang::RecordDecl
*, less_name
> records
;
76 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
78 void insert(clang::RecordDecl
*decl
) {
81 void insert(clang::TypedefNameDecl
*decl
) {
82 typedefs
.insert(decl
);
87 clang::Preprocessor
&PP
;
88 clang::ASTContext
&ast_context
;
89 /* The DeclContext of the function containing the scop.
91 clang::DeclContext
*decl_context
;
92 /* If autodetect is false, then loc contains the location
93 * of the scop to be extracted.
98 /* Set if the pet_scop returned by an extract method only
99 * represents part of the input tree.
103 /* A cache of size expressions for array identifiers as computed
104 * by PetScan::get_array_size, or set by PetScan::set_array_size.
106 isl_id_to_pet_expr
*id_size
;
107 /* A cache of size expressions for array types as computed
108 * by PetScan::get_array_size.
110 std::map
<const clang::Type
*, pet_expr
*> type_size
;
112 /* A cache of funtion summaries for function declarations
113 * as extracted by PetScan::get_summary.
115 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
117 /* A union of mappings of the form
118 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
120 isl_union_map
*value_bounds
;
122 /* The line number of the previously considered Stmt. */
124 /* The line number of the Stmt currently being considered. */
125 unsigned current_line
;
126 /* Information about the independent pragmas in the source code. */
127 std::vector
<Independent
> &independent
;
129 /* All variables that have already been declared
130 * in the current compound statement.
132 std::vector
<clang::VarDecl
*> declarations
;
133 /* Sequence number of the next rename. */
135 /* Have the declared names been collected? */
136 bool declared_names_collected
;
137 /* The names of the variables declared in decl_context,
138 * if declared_names_collected is set.
140 std::set
<std::string
> declared_names
;
141 /* A set of names known to be in use. */
142 std::set
<std::string
> used_names
;
144 /* Sequence number of the next temporary inlined argument variable. */
147 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
148 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
149 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
150 std::vector
<Independent
> &independent
) :
152 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
153 ctx(isl_union_map_get_ctx(value_bounds
)),
154 options(options
), partial(false), value_bounds(value_bounds
),
155 last_line(0), current_line(0),
156 independent(independent
), n_rename(0),
157 declared_names_collected(false), n_arg(0) {
158 id_size
= isl_id_to_pet_expr_alloc(ctx
, 0);
163 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
165 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
166 clang::IntegerLiteral
*expr
);
167 __isl_give pet_expr
*get_array_size(__isl_keep isl_id
*id
);
168 void set_array_size(__isl_take isl_id
*id
, __isl_take pet_expr
*size
);
169 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
170 PetTypes
*types
, __isl_keep pet_context
*pc
);
172 void set_current_stmt(clang::Stmt
*stmt
);
173 bool is_current_stmt_marked_independent();
175 void collect_declared_names();
176 void add_new_used_names(const std::set
<std::string
> &used_names
);
177 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
178 std::string
generate_new_name(const std::string
&name
);
180 __isl_give pet_tree
*add_kills(__isl_take pet_tree
*tree
,
181 std::set
<clang::ValueDecl
*> locals
);
183 struct pet_scop
*scan(clang::Stmt
*stmt
);
185 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
186 __isl_keep pet_context
*pc
);
187 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
188 PetTypes
*types
, __isl_keep pet_context
*pc
);
189 struct pet_array
*extract_array(__isl_keep isl_id_list
*decls
,
190 PetTypes
*types
, __isl_keep pet_context
*pc
);
191 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
192 clang::QualType qt
, int pos
);
193 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
194 __isl_keep pet_context
*pc
);
195 int substitute_array_sizes(__isl_keep pet_tree
*tree
,
196 pet_substituter
*substituter
);
198 __isl_give pet_tree
*insert_initial_declarations(
199 __isl_take pet_tree
*tree
, int n_decl
,
200 clang::StmtRange stmt_range
);
201 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
202 bool skip_declarations
= false);
203 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
204 bool skip_declarations
, clang::Stmt
*parent
);
205 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
206 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
207 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
208 bool skip_declarations
= false);
209 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
210 __isl_give pet_tree
*extract(clang::Decl
*decl
);
211 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
213 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
215 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
216 clang::SourceRange range
, bool skip_semi
);
217 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
220 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
222 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
223 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
224 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
225 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
227 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
228 clang::ValueDecl
*iv
);
229 __isl_give pet_expr
*extract_binary_increment(
230 clang::BinaryOperator
*op
,
231 clang::ValueDecl
*iv
);
232 __isl_give pet_expr
*extract_compound_increment(
233 clang::CompoundAssignOperator
*op
,
234 clang::ValueDecl
*iv
);
235 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
236 clang::ValueDecl
*iv
);
237 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
238 __isl_give pet_tree
*extract_expr_stmt(clang::Stmt
*stmt
);
239 __isl_give pet_tree
*extract_inlined_call(clang::CallExpr
*call
,
240 clang::FunctionDecl
*fd
);
241 int set_inliner_arguments(pet_inliner
&inliner
, clang::CallExpr
*call
,
242 clang::FunctionDecl
*fd
);
244 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
245 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
246 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
247 clang::FunctionDecl
*fd
);
248 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
249 clang::Expr
*expr
, bool detect_writes
);
250 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
251 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
252 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
253 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
254 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
255 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
256 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
257 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
258 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
259 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
260 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
261 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
263 __isl_give pet_expr
*extract_access_expr(clang::QualType qt
,
264 __isl_take pet_expr
*index
);
265 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
266 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
268 __isl_give pet_expr
*extract_index_expr(
269 clang::ArraySubscriptExpr
*expr
);
270 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
271 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
272 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
273 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
274 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
276 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
277 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
279 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
281 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
283 void report(clang::SourceRange range
, unsigned id
);
284 void report(clang::Stmt
*stmt
, unsigned id
);
285 void report(clang::Decl
*decl
, unsigned id
);
286 void unsupported(clang::Stmt
*stmt
);
287 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
288 void report_unsupported_statement_type(clang::Stmt
*stmt
);
289 void report_prototype_required(clang::Stmt
*stmt
);
290 void report_missing_increment(clang::Stmt
*stmt
);
291 void report_missing_summary_function(clang::Stmt
*stmt
);
292 void report_missing_summary_function_body(clang::Stmt
*stmt
);
293 void report_unsupported_inline_function_argument(clang::Stmt
*stmt
);
294 void report_unsupported_declaration(clang::Decl
*decl
);