3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
21 #ifndef HAVE_STMTRANGE
22 /* StmtRange was replaced by iterator_range in more recent versions of clang.
23 * Implement a StmtRange in terms of this iterator_range if StmtRange
26 struct StmtRange
: std::pair
<StmtIterator
,StmtIterator
> {
27 StmtRange(const StmtIterator
&begin
, const StmtIterator
&end
) :
28 std::pair
<StmtIterator
,StmtIterator
>(begin
, end
) {}
29 StmtRange(Stmt::child_range range
) :
30 std::pair
<StmtIterator
,StmtIterator
>(range
.begin(),
37 /* The location of the scop, as delimited by scop and endscop
38 * pragmas by the user.
39 * "start_line" is the line number of the start position.
49 /* The information extracted from a pragma pencil independent.
50 * We currently only keep track of the line number where
54 Independent(unsigned line
) : line(line
) {}
59 /* Compare two TypeDecl pointers based on their names.
62 bool operator()(const clang::TypeDecl
*x
,
63 const clang::TypeDecl
*y
) {
64 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
68 /* The PetTypes structure collects a set of RecordDecl and
69 * TypedefNameDecl pointers.
70 * The pointers are sorted using a fixed order. The actual order
71 * is not important, only that it is consistent across platforms.
74 std::set
<clang::RecordDecl
*, less_name
> records
;
75 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
77 void insert(clang::RecordDecl
*decl
) {
80 void insert(clang::TypedefNameDecl
*decl
) {
81 typedefs
.insert(decl
);
86 clang::Preprocessor
&PP
;
87 clang::ASTContext
&ast_context
;
88 /* The DeclContext of the function containing the scop.
90 clang::DeclContext
*decl_context
;
91 /* If autodetect is false, then loc contains the location
92 * of the scop to be extracted.
97 /* Set if the pet_scop returned by an extract method only
98 * represents part of the input tree.
102 /* A cache of size expressions for array types as computed
103 * by PetScan::get_array_size.
105 std::map
<const clang::Type
*, pet_expr
*> type_size
;
107 /* A cache of funtion summaries for function declarations
108 * as extracted by PetScan::get_summary.
110 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
112 /* A union of mappings of the form
113 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
115 isl_union_map
*value_bounds
;
117 /* The line number of the previously considered Stmt. */
119 /* The line number of the Stmt currently being considered. */
120 unsigned current_line
;
121 /* Information about the independent pragmas in the source code. */
122 std::vector
<Independent
> &independent
;
124 /* All variables that have already been declared
125 * in the current compound statement.
127 std::vector
<clang::VarDecl
*> declarations
;
128 /* Sequence number of the next rename. */
130 /* Have the declared names been collected? */
131 bool declared_names_collected
;
132 /* The names of the variables declared in decl_context,
133 * if declared_names_collected is set.
135 std::set
<std::string
> declared_names
;
136 /* A set of names known to be in use. */
137 std::set
<std::string
> used_names
;
139 /* Sequence number of the next temporary inlined argument variable. */
142 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
143 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
144 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
145 std::vector
<Independent
> &independent
) :
146 ctx(isl_union_map_get_ctx(value_bounds
)), PP(PP
),
147 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
148 options(options
), value_bounds(value_bounds
),
149 partial(false), last_line(0), current_line(0),
150 independent(independent
), n_rename(0),
151 declared_names_collected(false), n_arg(0) { }
155 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
157 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
158 clang::IntegerLiteral
*expr
);
159 __isl_give pet_expr
*get_array_size(const clang::Type
*type
);
160 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
161 PetTypes
*types
, __isl_keep pet_context
*pc
);
163 void set_current_stmt(clang::Stmt
*stmt
);
164 bool is_current_stmt_marked_independent();
166 void collect_declared_names();
167 void add_new_used_names(const std::set
<std::string
> &used_names
);
168 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
169 std::string
generate_new_name(const std::string
&name
);
171 __isl_give pet_tree
*add_kills(__isl_take pet_tree
*tree
,
172 std::set
<clang::ValueDecl
*> locals
);
174 struct pet_scop
*scan(clang::Stmt
*stmt
);
176 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
177 __isl_keep pet_context
*pc
);
178 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
179 PetTypes
*types
, __isl_keep pet_context
*pc
);
180 struct pet_array
*extract_array(__isl_keep isl_id_list
*delcs
,
181 PetTypes
*types
, __isl_keep pet_context
*pc
);
182 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
183 const clang::Type
*type
, int pos
);
184 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
185 const clang::Type
*type
, __isl_keep pet_context
*pc
);
187 __isl_give pet_tree
*insert_initial_declarations(
188 __isl_take pet_tree
*tree
, int n_decl
,
189 clang::StmtRange stmt_range
);
190 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
191 bool skip_declarations
= false);
192 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
193 bool skip_declarations
);
194 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
195 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
196 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
197 bool skip_declarations
= false);
198 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
199 __isl_give pet_tree
*extract(clang::Decl
*decl
);
200 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
202 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
204 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
205 clang::SourceRange range
, bool skip_semi
);
206 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
209 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
211 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
212 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
213 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
214 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
216 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
217 clang::ValueDecl
*iv
);
218 __isl_give pet_expr
*extract_binary_increment(
219 clang::BinaryOperator
*op
,
220 clang::ValueDecl
*iv
);
221 __isl_give pet_expr
*extract_compound_increment(
222 clang::CompoundAssignOperator
*op
,
223 clang::ValueDecl
*iv
);
224 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
225 clang::ValueDecl
*iv
);
226 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
227 __isl_give pet_tree
*extract_expr_stmt(clang::Stmt
*stmt
);
228 __isl_give pet_tree
*extract_inlined_call(clang::CallExpr
*call
,
229 clang::FunctionDecl
*fd
);
230 int set_inliner_arguments(pet_inliner
&inliner
, clang::CallExpr
*call
,
231 clang::FunctionDecl
*fd
);
233 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
234 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
235 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
236 clang::FunctionDecl
*fd
);
237 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
238 clang::Expr
*expr
, bool detect_writes
);
239 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
240 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
241 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
242 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
243 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
244 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
245 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
246 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
247 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
248 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
249 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
250 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
252 __isl_give pet_expr
*extract_access_expr(clang::QualType qt
,
253 __isl_take pet_expr
*index
);
254 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
255 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
257 __isl_give pet_expr
*extract_index_expr(
258 clang::ArraySubscriptExpr
*expr
);
259 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
260 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
261 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
262 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
263 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
265 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
266 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
268 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
270 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
272 void report(clang::Stmt
*stmt
, unsigned id
);
273 void unsupported(clang::Stmt
*stmt
);
274 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
275 void report_unsupported_statement_type(clang::Stmt
*stmt
);
276 void report_prototype_required(clang::Stmt
*stmt
);
277 void report_missing_increment(clang::Stmt
*stmt
);
278 void report_missing_summary_function(clang::Stmt
*stmt
);
279 void report_missing_summary_function_body(clang::Stmt
*stmt
);
280 void report_unsupported_inline_function_argument(clang::Stmt
*stmt
);