scan.cc: fix typo in comment
[pet.git] / scan.h
blobbb91b609caf33367b6656c8bdd05dad9c634335c
1 #include <map>
3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
8 #include <isl/ctx.h>
9 #include <isl/map.h>
10 #include <isl/val.h>
12 #include "context.h"
13 #include "inliner.h"
14 #include "isl_id_to_pet_expr.h"
15 #include "loc.h"
16 #include "scop.h"
17 #include "summary.h"
18 #include "tree.h"
20 namespace clang {
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
25 * is not available.
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(),
32 range.end()) {}
34 #endif
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.
42 struct ScopLoc {
43 ScopLoc() : end(0) {}
45 unsigned start_line;
46 unsigned start;
47 unsigned end;
50 /* The information extracted from a pragma pencil independent.
51 * We currently only keep track of the line number where
52 * the pragma appears.
54 struct Independent {
55 Independent(unsigned line) : line(line) {}
57 unsigned line;
60 /* Compare two TypeDecl pointers based on their names.
62 struct less_name {
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.
74 struct PetTypes {
75 std::set<clang::RecordDecl *, less_name> records;
76 std::set<clang::TypedefNameDecl *, less_name> typedefs;
78 void insert(clang::RecordDecl *decl) {
79 records.insert(decl);
81 void insert(clang::TypedefNameDecl *decl) {
82 typedefs.insert(decl);
86 struct PetScan {
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.
95 ScopLoc &loc;
96 isl_ctx *ctx;
97 pet_options *options;
98 /* Set if the pet_scop returned by an extract method only
99 * represents part of the input tree.
101 bool partial;
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. */
123 unsigned last_line;
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. */
134 int n_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. */
145 int n_arg;
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) :
151 PP(PP),
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);
161 ~PetScan();
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);
171 private:
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);
196 __isl_give pet_tree *insert_initial_declarations(
197 __isl_take pet_tree *tree, int n_decl,
198 clang::StmtRange stmt_range);
199 __isl_give pet_tree *extract(clang::Stmt *stmt,
200 bool skip_declarations = false);
201 __isl_give pet_tree *extract(clang::StmtRange stmt_range, bool block,
202 bool skip_declarations, clang::Stmt *parent);
203 __isl_give pet_tree *extract(clang::IfStmt *stmt);
204 __isl_give pet_tree *extract(clang::WhileStmt *stmt);
205 __isl_give pet_tree *extract(clang::CompoundStmt *stmt,
206 bool skip_declarations = false);
207 __isl_give pet_tree *extract(clang::LabelStmt *stmt);
208 __isl_give pet_tree *extract(clang::Decl *decl);
209 __isl_give pet_tree *extract(clang::DeclStmt *expr);
211 __isl_give pet_loc *construct_pet_loc(clang::SourceRange range,
212 bool skip_semi);
213 __isl_give pet_tree *extract(__isl_take pet_expr *expr,
214 clang::SourceRange range, bool skip_semi);
215 __isl_give pet_tree *update_loc(__isl_take pet_tree *tree,
216 clang::Stmt *stmt);
218 struct pet_scop *extract_scop(__isl_take pet_tree *tree);
220 clang::BinaryOperator *initialization_assignment(clang::Stmt *init);
221 clang::Decl *initialization_declaration(clang::Stmt *init);
222 clang::ValueDecl *extract_induction_variable(clang::BinaryOperator *stmt);
223 clang::VarDecl *extract_induction_variable(clang::Stmt *init,
224 clang::Decl *stmt);
225 __isl_give pet_expr *extract_unary_increment(clang::UnaryOperator *op,
226 clang::ValueDecl *iv);
227 __isl_give pet_expr *extract_binary_increment(
228 clang::BinaryOperator *op,
229 clang::ValueDecl *iv);
230 __isl_give pet_expr *extract_compound_increment(
231 clang::CompoundAssignOperator *op,
232 clang::ValueDecl *iv);
233 __isl_give pet_expr *extract_increment(clang::ForStmt *stmt,
234 clang::ValueDecl *iv);
235 __isl_give pet_tree *extract_for(clang::ForStmt *stmt);
236 __isl_give pet_tree *extract_expr_stmt(clang::Stmt *stmt);
237 __isl_give pet_tree *extract_inlined_call(clang::CallExpr *call,
238 clang::FunctionDecl *fd);
239 int set_inliner_arguments(pet_inliner &inliner, clang::CallExpr *call,
240 clang::FunctionDecl *fd);
242 __isl_give pet_expr *extract_assume(clang::Expr *expr);
243 __isl_give pet_function_summary *get_summary(clang::FunctionDecl *fd);
244 __isl_give pet_expr *set_summary(__isl_take pet_expr *expr,
245 clang::FunctionDecl *fd);
246 __isl_give pet_expr *extract_argument(clang::FunctionDecl *fd, int pos,
247 clang::Expr *expr, bool detect_writes);
248 __isl_give pet_expr *extract_expr(const llvm::APInt &val);
249 __isl_give pet_expr *extract_expr(clang::Expr *expr);
250 __isl_give pet_expr *extract_expr(clang::UnaryOperator *expr);
251 __isl_give pet_expr *extract_expr(clang::BinaryOperator *expr);
252 __isl_give pet_expr *extract_expr(clang::ImplicitCastExpr *expr);
253 __isl_give pet_expr *extract_expr(clang::IntegerLiteral *expr);
254 __isl_give pet_expr *extract_expr(clang::EnumConstantDecl *expr);
255 __isl_give pet_expr *extract_expr(clang::FloatingLiteral *expr);
256 __isl_give pet_expr *extract_expr(clang::ParenExpr *expr);
257 __isl_give pet_expr *extract_expr(clang::ConditionalOperator *expr);
258 __isl_give pet_expr *extract_expr(clang::CallExpr *expr);
259 __isl_give pet_expr *extract_expr(clang::CStyleCastExpr *expr);
261 __isl_give pet_expr *extract_access_expr(clang::QualType qt,
262 __isl_take pet_expr *index);
263 __isl_give pet_expr *extract_access_expr(clang::Expr *expr);
264 __isl_give pet_expr *extract_access_expr(clang::ValueDecl *decl);
266 __isl_give pet_expr *extract_index_expr(
267 clang::ArraySubscriptExpr *expr);
268 __isl_give pet_expr *extract_index_expr(clang::Expr *expr);
269 __isl_give pet_expr *extract_index_expr(clang::ImplicitCastExpr *expr);
270 __isl_give pet_expr *extract_index_expr(clang::DeclRefExpr *expr);
271 __isl_give pet_expr *extract_index_expr(clang::ValueDecl *decl);
272 __isl_give pet_expr *extract_index_expr(clang::MemberExpr *expr);
274 __isl_give isl_val *extract_int(clang::Expr *expr);
275 __isl_give isl_val *extract_int(clang::ParenExpr *expr);
277 clang::FunctionDecl *find_decl_from_name(clang::CallExpr *call,
278 std::string name);
279 clang::FunctionDecl *get_summary_function(clang::CallExpr *call);
281 void report(clang::SourceRange range, unsigned id);
282 void report(clang::Stmt *stmt, unsigned id);
283 void report(clang::Decl *decl, unsigned id);
284 void unsupported(clang::Stmt *stmt);
285 void report_unsupported_unary_operator(clang::Stmt *stmt);
286 void report_unsupported_statement_type(clang::Stmt *stmt);
287 void report_prototype_required(clang::Stmt *stmt);
288 void report_missing_increment(clang::Stmt *stmt);
289 void report_missing_summary_function(clang::Stmt *stmt);
290 void report_missing_summary_function_body(clang::Stmt *stmt);
291 void report_unsupported_inline_function_argument(clang::Stmt *stmt);
292 void report_unsupported_declaration(clang::Decl *decl);