support declaration statements declaring more than one variable
[pet.git] / scan.h
blobd067480b07acb9b70ba15b1fdf966bab0c8ee948
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 "loc.h"
14 #include "scop.h"
15 #include "summary.h"
16 #include "tree.h"
18 /* The location of the scop, as delimited by scop and endscop
19 * pragmas by the user.
20 * "start_line" is the line number of the start position.
22 struct ScopLoc {
23 ScopLoc() : end(0) {}
25 unsigned start_line;
26 unsigned start;
27 unsigned end;
30 /* The information extracted from a pragma pencil independent.
31 * We currently only keep track of the line number where
32 * the pragma appears.
34 struct Independent {
35 Independent(unsigned line) : line(line) {}
37 unsigned line;
40 /* Compare two TypeDecl pointers based on their names.
42 struct less_name {
43 bool operator()(const clang::TypeDecl *x,
44 const clang::TypeDecl *y) {
45 return x->getNameAsString().compare(y->getNameAsString()) < 0;
49 /* The PetTypes structure collects a set of RecordDecl and
50 * TypedefNameDecl pointers.
51 * The pointers are sorted using a fixed order. The actual order
52 * is not important, only that it is consistent across platforms.
54 struct PetTypes {
55 std::set<clang::RecordDecl *, less_name> records;
56 std::set<clang::TypedefNameDecl *, less_name> typedefs;
58 void insert(clang::RecordDecl *decl) {
59 records.insert(decl);
61 void insert(clang::TypedefNameDecl *decl) {
62 typedefs.insert(decl);
66 struct PetScan {
67 clang::Preprocessor &PP;
68 clang::ASTContext &ast_context;
69 /* If autodetect is false, then loc contains the location
70 * of the scop to be extracted.
72 ScopLoc &loc;
73 isl_ctx *ctx;
74 pet_options *options;
75 /* Set if the pet_scop returned by an extract method only
76 * represents part of the input tree.
78 bool partial;
80 /* A cache of size expressions for array types as computed
81 * by PetScan::get_array_size.
83 std::map<const clang::Type *, pet_expr *> type_size;
85 /* A cache of funtion summaries for function declarations
86 * as extracted by PetScan::get_summary.
88 std::map<clang::FunctionDecl *, pet_function_summary *> summary_cache;
90 /* A union of mappings of the form
91 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
93 isl_union_map *value_bounds;
95 /* The line number of the previously considered Stmt. */
96 unsigned last_line;
97 /* The line number of the Stmt currently being considered. */
98 unsigned current_line;
99 /* Information about the independent pragmas in the source code. */
100 std::vector<Independent> &independent;
102 PetScan(clang::Preprocessor &PP,
103 clang::ASTContext &ast_context, ScopLoc &loc,
104 pet_options *options, __isl_take isl_union_map *value_bounds,
105 std::vector<Independent> &independent) :
106 ctx(isl_union_map_get_ctx(value_bounds)), PP(PP),
107 ast_context(ast_context), loc(loc),
108 options(options), value_bounds(value_bounds),
109 partial(false), last_line(0), current_line(0),
110 independent(independent) { }
112 ~PetScan();
114 struct pet_scop *scan(clang::FunctionDecl *fd);
116 static __isl_give isl_val *extract_int(isl_ctx *ctx,
117 clang::IntegerLiteral *expr);
118 __isl_give pet_expr *get_array_size(const clang::Type *type);
119 struct pet_array *extract_array(isl_ctx *ctx, clang::ValueDecl *decl,
120 PetTypes *types, __isl_keep pet_context *pc);
121 private:
122 void set_current_stmt(clang::Stmt *stmt);
123 bool is_current_stmt_marked_independent();
125 struct pet_scop *scan(clang::Stmt *stmt);
127 struct pet_scop *scan_arrays(struct pet_scop *scop,
128 __isl_keep pet_context *pc);
129 struct pet_array *extract_array(isl_ctx *ctx,
130 std::vector<clang::ValueDecl *> decls,
131 PetTypes *types, __isl_keep pet_context *pc);
132 __isl_give pet_expr *set_upper_bounds(__isl_take pet_expr *expr,
133 const clang::Type *type, int pos);
134 struct pet_array *set_upper_bounds(struct pet_array *array,
135 const clang::Type *type, __isl_keep pet_context *pc);
137 __isl_give pet_tree *insert_initial_declarations(
138 __isl_take pet_tree *tree, int n_decl,
139 clang::StmtRange stmt_range);
140 __isl_give pet_tree *extract(clang::Stmt *stmt,
141 bool skip_declarations = false);
142 __isl_give pet_tree *extract(clang::StmtRange stmt_range, bool block,
143 bool skip_declarations);
144 __isl_give pet_tree *extract(clang::IfStmt *stmt);
145 __isl_give pet_tree *extract(clang::WhileStmt *stmt);
146 __isl_give pet_tree *extract(clang::CompoundStmt *stmt,
147 bool skip_declarations = false);
148 __isl_give pet_tree *extract(clang::LabelStmt *stmt);
149 __isl_give pet_tree *extract(clang::Decl *decl);
150 __isl_give pet_tree *extract(clang::DeclStmt *expr);
152 __isl_give pet_loc *construct_pet_loc(clang::SourceRange range,
153 bool skip_semi);
154 __isl_give pet_tree *extract(__isl_take pet_expr *expr,
155 clang::SourceRange range, bool skip_semi);
156 __isl_give pet_tree *update_loc(__isl_take pet_tree *tree,
157 clang::Stmt *stmt);
159 struct pet_scop *extract_scop(__isl_take pet_tree *tree);
161 clang::BinaryOperator *initialization_assignment(clang::Stmt *init);
162 clang::Decl *initialization_declaration(clang::Stmt *init);
163 clang::ValueDecl *extract_induction_variable(clang::BinaryOperator *stmt);
164 clang::VarDecl *extract_induction_variable(clang::Stmt *init,
165 clang::Decl *stmt);
166 __isl_give pet_expr *extract_unary_increment(clang::UnaryOperator *op,
167 clang::ValueDecl *iv);
168 __isl_give pet_expr *extract_binary_increment(
169 clang::BinaryOperator *op,
170 clang::ValueDecl *iv);
171 __isl_give pet_expr *extract_compound_increment(
172 clang::CompoundAssignOperator *op,
173 clang::ValueDecl *iv);
174 __isl_give pet_expr *extract_increment(clang::ForStmt *stmt,
175 clang::ValueDecl *iv);
176 __isl_give pet_tree *extract_for(clang::ForStmt *stmt);
178 __isl_give pet_expr *extract_assume(clang::Expr *expr);
179 __isl_give pet_function_summary *get_summary(clang::FunctionDecl *fd);
180 __isl_give pet_expr *set_summary(__isl_take pet_expr *expr,
181 clang::FunctionDecl *fd);
182 __isl_give pet_expr *extract_argument(clang::FunctionDecl *fd, int pos,
183 clang::Expr *expr, bool detect_writes);
184 __isl_give pet_expr *extract_expr(const llvm::APInt &val);
185 __isl_give pet_expr *extract_expr(clang::Expr *expr);
186 __isl_give pet_expr *extract_expr(clang::UnaryOperator *expr);
187 __isl_give pet_expr *extract_expr(clang::BinaryOperator *expr);
188 __isl_give pet_expr *extract_expr(clang::ImplicitCastExpr *expr);
189 __isl_give pet_expr *extract_expr(clang::IntegerLiteral *expr);
190 __isl_give pet_expr *extract_expr(clang::EnumConstantDecl *expr);
191 __isl_give pet_expr *extract_expr(clang::FloatingLiteral *expr);
192 __isl_give pet_expr *extract_expr(clang::ParenExpr *expr);
193 __isl_give pet_expr *extract_expr(clang::ConditionalOperator *expr);
194 __isl_give pet_expr *extract_expr(clang::CallExpr *expr);
195 __isl_give pet_expr *extract_expr(clang::CStyleCastExpr *expr);
197 __isl_give pet_expr *extract_access_expr(clang::QualType qt,
198 __isl_take pet_expr *index);
199 __isl_give pet_expr *extract_access_expr(clang::Expr *expr);
200 __isl_give pet_expr *extract_access_expr(clang::ValueDecl *decl);
202 __isl_give pet_expr *extract_index_expr(
203 clang::ArraySubscriptExpr *expr);
204 __isl_give pet_expr *extract_index_expr(clang::Expr *expr);
205 __isl_give pet_expr *extract_index_expr(clang::ImplicitCastExpr *expr);
206 __isl_give pet_expr *extract_index_expr(clang::DeclRefExpr *expr);
207 __isl_give pet_expr *extract_index_expr(clang::ValueDecl *decl);
208 __isl_give pet_expr *extract_index_expr(clang::MemberExpr *expr);
210 __isl_give isl_val *extract_int(clang::Expr *expr);
211 __isl_give isl_val *extract_int(clang::ParenExpr *expr);
213 clang::FunctionDecl *find_decl_from_name(clang::CallExpr *call,
214 std::string name);
215 clang::FunctionDecl *get_summary_function(clang::CallExpr *call);
217 void report(clang::Stmt *stmt, unsigned id);
218 void unsupported(clang::Stmt *stmt);
219 void report_unsupported_statement_type(clang::Stmt *stmt);
220 void report_prototype_required(clang::Stmt *stmt);
221 void report_missing_increment(clang::Stmt *stmt);
222 void report_missing_summary_function(clang::Stmt *stmt);
223 void report_missing_summary_function_body(clang::Stmt *stmt);