scan.h: protect against multiple inclusions
[pet.git] / scan.h
blob5bdc0e011b56397c7bd36759442f059d3ae4e3e2
1 #ifndef PET_SCAN_H
2 #define PET_SCAN_H
4 #include <map>
6 #include <clang/Basic/SourceManager.h>
7 #include <clang/AST/Decl.h>
8 #include <clang/AST/Stmt.h>
9 #include <clang/Lex/Preprocessor.h>
11 #include <isl/ctx.h>
12 #include <isl/map.h>
13 #include <isl/val.h>
15 #include "context.h"
16 #include "inliner.h"
17 #include "isl_id_to_pet_expr.h"
18 #include "loc.h"
19 #include "scop.h"
20 #include "summary.h"
21 #include "tree.h"
23 namespace clang {
25 #ifndef HAVE_STMTRANGE
26 /* StmtRange was replaced by iterator_range in more recent versions of clang.
27 * Implement a StmtRange in terms of this iterator_range if StmtRange
28 * is not available.
30 struct StmtRange : std::pair<StmtIterator,StmtIterator> {
31 StmtRange(const StmtIterator &begin, const StmtIterator &end) :
32 std::pair<StmtIterator,StmtIterator>(begin, end) {}
33 StmtRange(Stmt::child_range range) :
34 std::pair<StmtIterator,StmtIterator>(range.begin(),
35 range.end()) {}
37 #endif
41 /* The location of the scop, as delimited by scop and endscop
42 * pragmas by the user.
43 * "scop" and "endscop" are the source locations of the scop and
44 * endscop pragmas.
45 * "start_line" is the line number of the start position.
47 struct ScopLoc {
48 ScopLoc() : end(0) {}
50 clang::SourceLocation scop;
51 clang::SourceLocation endscop;
52 unsigned start_line;
53 unsigned start;
54 unsigned end;
57 /* The information extracted from a pragma pencil independent.
58 * We currently only keep track of the line number where
59 * the pragma appears.
61 struct Independent {
62 Independent(unsigned line) : line(line) {}
64 unsigned line;
67 /* Compare two TypeDecl pointers based on their names.
69 struct less_name {
70 bool operator()(const clang::TypeDecl *x,
71 const clang::TypeDecl *y) {
72 return x->getNameAsString().compare(y->getNameAsString()) < 0;
76 /* The PetTypes structure collects a set of RecordDecl and
77 * TypedefNameDecl pointers.
78 * The pointers are sorted using a fixed order. The actual order
79 * is not important, only that it is consistent across platforms.
81 struct PetTypes {
82 std::set<clang::RecordDecl *, less_name> records;
83 std::set<clang::TypedefNameDecl *, less_name> typedefs;
85 void insert(clang::RecordDecl *decl) {
86 records.insert(decl);
88 void insert(clang::TypedefNameDecl *decl) {
89 typedefs.insert(decl);
93 struct PetScan {
94 clang::Preprocessor &PP;
95 clang::ASTContext &ast_context;
96 /* The DeclContext of the function containing the scop.
98 clang::DeclContext *decl_context;
99 /* If autodetect is false, then loc contains the location
100 * of the scop to be extracted.
102 ScopLoc &loc;
103 isl_ctx *ctx;
104 pet_options *options;
105 /* Set if the pet_scop returned by an extract method only
106 * represents part of the input tree.
108 bool partial;
110 /* A cache of size expressions for array identifiers as computed
111 * by PetScan::get_array_size, or set by PetScan::set_array_size.
113 isl_id_to_pet_expr *id_size;
114 /* A cache of size expressions for array types as computed
115 * by PetScan::get_array_size.
117 std::map<const clang::Type *, pet_expr *> type_size;
119 /* A cache of funtion summaries for function declarations
120 * as extracted by PetScan::get_summary.
122 std::map<clang::FunctionDecl *, pet_function_summary *> summary_cache;
124 /* A union of mappings of the form
125 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
127 isl_union_map *value_bounds;
129 /* The line number of the previously considered Stmt. */
130 unsigned last_line;
131 /* The line number of the Stmt currently being considered. */
132 unsigned current_line;
133 /* Information about the independent pragmas in the source code. */
134 std::vector<Independent> &independent;
136 /* All variables that have already been declared
137 * in the current compound statement.
139 std::vector<clang::VarDecl *> declarations;
140 /* Sequence number of the next rename. */
141 int n_rename;
142 /* Have the declared names been collected? */
143 bool declared_names_collected;
144 /* The names of the variables declared in decl_context,
145 * if declared_names_collected is set.
147 std::set<std::string> declared_names;
148 /* A set of names known to be in use. */
149 std::set<std::string> used_names;
151 /* Sequence number of the next temporary inlined argument variable. */
152 int n_arg;
154 PetScan(clang::Preprocessor &PP, clang::ASTContext &ast_context,
155 clang::DeclContext *decl_context, ScopLoc &loc,
156 pet_options *options, __isl_take isl_union_map *value_bounds,
157 std::vector<Independent> &independent) :
158 PP(PP),
159 ast_context(ast_context), decl_context(decl_context), loc(loc),
160 ctx(isl_union_map_get_ctx(value_bounds)),
161 options(options), partial(false), value_bounds(value_bounds),
162 last_line(0), current_line(0),
163 independent(independent), n_rename(0),
164 declared_names_collected(false), n_arg(0) {
165 id_size = isl_id_to_pet_expr_alloc(ctx, 0);
168 ~PetScan();
170 struct pet_scop *scan(clang::FunctionDecl *fd);
172 static __isl_give isl_val *extract_int(isl_ctx *ctx,
173 clang::IntegerLiteral *expr);
174 __isl_give pet_expr *get_array_size(__isl_keep isl_id *id);
175 void set_array_size(__isl_take isl_id *id, __isl_take pet_expr *size);
176 struct pet_array *extract_array(__isl_keep isl_id *id,
177 PetTypes *types, __isl_keep pet_context *pc);
178 private:
179 void set_current_stmt(clang::Stmt *stmt);
180 bool is_current_stmt_marked_independent();
182 void collect_declared_names();
183 void add_new_used_names(const std::set<std::string> &used_names);
184 bool name_in_use(const std::string &name, clang::Decl *decl);
185 std::string generate_new_name(const std::string &name);
187 __isl_give pet_tree *add_kills(__isl_take pet_tree *tree,
188 std::set<clang::ValueDecl *> locals);
190 struct pet_scop *scan(clang::Stmt *stmt);
192 struct pet_scop *scan_arrays(struct pet_scop *scop,
193 __isl_keep pet_context *pc);
194 struct pet_array *extract_array(clang::ValueDecl *decl,
195 PetTypes *types, __isl_keep pet_context *pc);
196 struct pet_array *extract_array(__isl_keep isl_id_list *decls,
197 PetTypes *types, __isl_keep pet_context *pc);
198 __isl_give pet_expr *set_upper_bounds(__isl_take pet_expr *expr,
199 clang::QualType qt, int pos);
200 struct pet_array *set_upper_bounds(struct pet_array *array,
201 __isl_keep pet_context *pc);
202 int substitute_array_sizes(__isl_keep pet_tree *tree,
203 pet_substituter *substituter);
205 __isl_give pet_tree *insert_initial_declarations(
206 __isl_take pet_tree *tree, int n_decl,
207 clang::StmtRange stmt_range);
208 __isl_give pet_tree *extract(clang::Stmt *stmt,
209 bool skip_declarations = false);
210 __isl_give pet_tree *extract(clang::StmtRange stmt_range, bool block,
211 bool skip_declarations, clang::Stmt *parent);
212 __isl_give pet_tree *extract(clang::IfStmt *stmt);
213 __isl_give pet_tree *extract(clang::WhileStmt *stmt);
214 __isl_give pet_tree *extract(clang::CompoundStmt *stmt,
215 bool skip_declarations = false);
216 __isl_give pet_tree *extract(clang::LabelStmt *stmt);
217 __isl_give pet_tree *extract(clang::Decl *decl);
218 __isl_give pet_tree *extract(clang::DeclStmt *expr);
220 __isl_give pet_loc *construct_pet_loc(clang::SourceRange range,
221 bool skip_semi);
222 __isl_give pet_tree *extract(__isl_take pet_expr *expr,
223 clang::SourceRange range, bool skip_semi);
224 __isl_give pet_tree *update_loc(__isl_take pet_tree *tree,
225 clang::Stmt *stmt);
227 struct pet_scop *extract_scop(__isl_take pet_tree *tree);
229 clang::BinaryOperator *initialization_assignment(clang::Stmt *init);
230 clang::Decl *initialization_declaration(clang::Stmt *init);
231 clang::ValueDecl *extract_induction_variable(clang::BinaryOperator *stmt);
232 clang::VarDecl *extract_induction_variable(clang::Stmt *init,
233 clang::Decl *stmt);
234 __isl_give pet_expr *extract_unary_increment(clang::UnaryOperator *op,
235 clang::ValueDecl *iv);
236 __isl_give pet_expr *extract_binary_increment(
237 clang::BinaryOperator *op,
238 clang::ValueDecl *iv);
239 __isl_give pet_expr *extract_compound_increment(
240 clang::CompoundAssignOperator *op,
241 clang::ValueDecl *iv);
242 __isl_give pet_expr *extract_increment(clang::ForStmt *stmt,
243 clang::ValueDecl *iv);
244 __isl_give pet_tree *extract_for(clang::ForStmt *stmt);
245 __isl_give pet_tree *extract_expr_stmt(clang::Stmt *stmt);
246 __isl_give pet_tree *extract_inlined_call(clang::CallExpr *call,
247 clang::FunctionDecl *fd);
248 int set_inliner_arguments(pet_inliner &inliner, clang::CallExpr *call,
249 clang::FunctionDecl *fd);
251 __isl_give pet_expr *extract_assume(clang::Expr *expr);
252 __isl_give pet_function_summary *get_summary(clang::FunctionDecl *fd);
253 __isl_give pet_expr *set_summary(__isl_take pet_expr *expr,
254 clang::FunctionDecl *fd);
255 __isl_give pet_expr *extract_argument(clang::FunctionDecl *fd, int pos,
256 clang::Expr *expr, bool detect_writes);
257 __isl_give pet_expr *extract_expr(const llvm::APInt &val);
258 __isl_give pet_expr *extract_expr(clang::Expr *expr);
259 __isl_give pet_expr *extract_expr(clang::UnaryOperator *expr);
260 __isl_give pet_expr *extract_expr(clang::BinaryOperator *expr);
261 __isl_give pet_expr *extract_expr(clang::ImplicitCastExpr *expr);
262 __isl_give pet_expr *extract_expr(clang::IntegerLiteral *expr);
263 __isl_give pet_expr *extract_expr(clang::EnumConstantDecl *expr);
264 __isl_give pet_expr *extract_expr(clang::FloatingLiteral *expr);
265 __isl_give pet_expr *extract_expr(clang::ParenExpr *expr);
266 __isl_give pet_expr *extract_expr(clang::ConditionalOperator *expr);
267 __isl_give pet_expr *extract_expr(clang::CallExpr *expr);
268 __isl_give pet_expr *extract_expr(clang::CStyleCastExpr *expr);
270 __isl_give pet_expr *extract_access_expr(clang::QualType qt,
271 __isl_take pet_expr *index);
272 __isl_give pet_expr *extract_access_expr(clang::Expr *expr);
273 __isl_give pet_expr *extract_access_expr(clang::ValueDecl *decl);
275 __isl_give pet_expr *extract_index_expr(
276 clang::ArraySubscriptExpr *expr);
277 __isl_give pet_expr *extract_index_expr(clang::Expr *expr);
278 __isl_give pet_expr *extract_index_expr(clang::ImplicitCastExpr *expr);
279 __isl_give pet_expr *extract_index_expr(clang::DeclRefExpr *expr);
280 __isl_give pet_expr *extract_index_expr(clang::ValueDecl *decl);
281 __isl_give pet_expr *extract_index_expr(clang::MemberExpr *expr);
283 __isl_give isl_val *extract_int(clang::Expr *expr);
284 __isl_give isl_val *extract_int(clang::ParenExpr *expr);
286 clang::FunctionDecl *find_decl_from_name(clang::CallExpr *call,
287 std::string name);
288 clang::FunctionDecl *get_summary_function(clang::CallExpr *call);
290 void report(clang::SourceRange range, unsigned id);
291 void report(clang::Stmt *stmt, unsigned id);
292 void report(clang::Decl *decl, unsigned id);
293 void unsupported(clang::Stmt *stmt);
294 void report_unsupported_unary_operator(clang::Stmt *stmt);
295 void report_unsupported_statement_type(clang::Stmt *stmt);
296 void report_prototype_required(clang::Stmt *stmt);
297 void report_missing_increment(clang::Stmt *stmt);
298 void report_missing_summary_function(clang::Stmt *stmt);
299 void report_missing_summary_function_body(clang::Stmt *stmt);
300 void report_unsupported_inline_function_argument(clang::Stmt *stmt);
301 void report_unsupported_declaration(clang::Decl *decl);
302 void report_unbalanced_pragmas(clang::SourceLocation scop,
303 clang::SourceLocation endscop);
306 #endif