3 #include <clang/Basic/SourceManager.h>
4 #include <clang/AST/Decl.h>
5 #include <clang/AST/Stmt.h>
6 #include <clang/Lex/Preprocessor.h>
19 /* The location of the scop, as delimited by scop and endscop
20 * pragmas by the user.
21 * "start_line" is the line number of the start position.
31 /* The information extracted from a pragma pencil independent.
32 * We currently only keep track of the line number where
36 Independent(unsigned line
) : line(line
) {}
41 /* Compare two TypeDecl pointers based on their names.
44 bool operator()(const clang::TypeDecl
*x
,
45 const clang::TypeDecl
*y
) {
46 return x
->getNameAsString().compare(y
->getNameAsString()) < 0;
50 /* The PetTypes structure collects a set of RecordDecl and
51 * TypedefNameDecl pointers.
52 * The pointers are sorted using a fixed order. The actual order
53 * is not important, only that it is consistent across platforms.
56 std::set
<clang::RecordDecl
*, less_name
> records
;
57 std::set
<clang::TypedefNameDecl
*, less_name
> typedefs
;
59 void insert(clang::RecordDecl
*decl
) {
62 void insert(clang::TypedefNameDecl
*decl
) {
63 typedefs
.insert(decl
);
68 clang::Preprocessor
&PP
;
69 clang::ASTContext
&ast_context
;
70 /* The DeclContext of the function containing the scop.
72 clang::DeclContext
*decl_context
;
73 /* If autodetect is false, then loc contains the location
74 * of the scop to be extracted.
79 /* Set if the pet_scop returned by an extract method only
80 * represents part of the input tree.
84 /* A cache of size expressions for array types as computed
85 * by PetScan::get_array_size.
87 std::map
<const clang::Type
*, pet_expr
*> type_size
;
89 /* A cache of funtion summaries for function declarations
90 * as extracted by PetScan::get_summary.
92 std::map
<clang::FunctionDecl
*, pet_function_summary
*> summary_cache
;
94 /* A union of mappings of the form
95 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
97 isl_union_map
*value_bounds
;
99 /* The line number of the previously considered Stmt. */
101 /* The line number of the Stmt currently being considered. */
102 unsigned current_line
;
103 /* Information about the independent pragmas in the source code. */
104 std::vector
<Independent
> &independent
;
106 /* All variables that have already been declared
107 * in the current compound statement.
109 std::vector
<clang::VarDecl
*> declarations
;
110 /* Sequence number of the next rename. */
112 /* Have the declared names been collected? */
113 bool declared_names_collected
;
114 /* The names of the variables declared in decl_context,
115 * if declared_names_collected is set.
117 std::set
<std::string
> declared_names
;
118 /* A set of names known to be in use. */
119 std::set
<std::string
> used_names
;
121 /* Sequence number of the next temporary inlined argument variable. */
124 PetScan(clang::Preprocessor
&PP
, clang::ASTContext
&ast_context
,
125 clang::DeclContext
*decl_context
, ScopLoc
&loc
,
126 pet_options
*options
, __isl_take isl_union_map
*value_bounds
,
127 std::vector
<Independent
> &independent
) :
128 ctx(isl_union_map_get_ctx(value_bounds
)), PP(PP
),
129 ast_context(ast_context
), decl_context(decl_context
), loc(loc
),
130 options(options
), value_bounds(value_bounds
),
131 partial(false), last_line(0), current_line(0),
132 independent(independent
), n_rename(0),
133 declared_names_collected(false), n_arg(0) { }
137 struct pet_scop
*scan(clang::FunctionDecl
*fd
);
139 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
,
140 clang::IntegerLiteral
*expr
);
141 __isl_give pet_expr
*get_array_size(const clang::Type
*type
);
142 struct pet_array
*extract_array(__isl_keep isl_id
*id
,
143 PetTypes
*types
, __isl_keep pet_context
*pc
);
145 void set_current_stmt(clang::Stmt
*stmt
);
146 bool is_current_stmt_marked_independent();
148 void collect_declared_names();
149 void add_new_used_names(const std::set
<std::string
> &used_names
);
150 bool name_in_use(const std::string
&name
, clang::Decl
*decl
);
151 std::string
generate_new_name(const std::string
&name
);
153 struct pet_scop
*scan(clang::Stmt
*stmt
);
155 struct pet_scop
*scan_arrays(struct pet_scop
*scop
,
156 __isl_keep pet_context
*pc
);
157 struct pet_array
*extract_array(clang::ValueDecl
*decl
,
158 PetTypes
*types
, __isl_keep pet_context
*pc
);
159 struct pet_array
*extract_array(isl_ctx
*ctx
,
160 std::vector
<clang::ValueDecl
*> decls
,
161 PetTypes
*types
, __isl_keep pet_context
*pc
);
162 __isl_give pet_expr
*set_upper_bounds(__isl_take pet_expr
*expr
,
163 const clang::Type
*type
, int pos
);
164 struct pet_array
*set_upper_bounds(struct pet_array
*array
,
165 const clang::Type
*type
, __isl_keep pet_context
*pc
);
167 __isl_give pet_tree
*insert_initial_declarations(
168 __isl_take pet_tree
*tree
, int n_decl
,
169 clang::StmtRange stmt_range
);
170 __isl_give pet_tree
*extract(clang::Stmt
*stmt
,
171 bool skip_declarations
= false);
172 __isl_give pet_tree
*extract(clang::StmtRange stmt_range
, bool block
,
173 bool skip_declarations
);
174 __isl_give pet_tree
*extract(clang::IfStmt
*stmt
);
175 __isl_give pet_tree
*extract(clang::WhileStmt
*stmt
);
176 __isl_give pet_tree
*extract(clang::CompoundStmt
*stmt
,
177 bool skip_declarations
= false);
178 __isl_give pet_tree
*extract(clang::LabelStmt
*stmt
);
179 __isl_give pet_tree
*extract(clang::Decl
*decl
);
180 __isl_give pet_tree
*extract(clang::DeclStmt
*expr
);
182 __isl_give pet_loc
*construct_pet_loc(clang::SourceRange range
,
184 __isl_give pet_tree
*extract(__isl_take pet_expr
*expr
,
185 clang::SourceRange range
, bool skip_semi
);
186 __isl_give pet_tree
*update_loc(__isl_take pet_tree
*tree
,
189 struct pet_scop
*extract_scop(__isl_take pet_tree
*tree
);
191 clang::BinaryOperator
*initialization_assignment(clang::Stmt
*init
);
192 clang::Decl
*initialization_declaration(clang::Stmt
*init
);
193 clang::ValueDecl
*extract_induction_variable(clang::BinaryOperator
*stmt
);
194 clang::VarDecl
*extract_induction_variable(clang::Stmt
*init
,
196 __isl_give pet_expr
*extract_unary_increment(clang::UnaryOperator
*op
,
197 clang::ValueDecl
*iv
);
198 __isl_give pet_expr
*extract_binary_increment(
199 clang::BinaryOperator
*op
,
200 clang::ValueDecl
*iv
);
201 __isl_give pet_expr
*extract_compound_increment(
202 clang::CompoundAssignOperator
*op
,
203 clang::ValueDecl
*iv
);
204 __isl_give pet_expr
*extract_increment(clang::ForStmt
*stmt
,
205 clang::ValueDecl
*iv
);
206 __isl_give pet_tree
*extract_for(clang::ForStmt
*stmt
);
207 __isl_give pet_tree
*extract_expr_stmt(clang::Stmt
*stmt
);
208 __isl_give pet_tree
*extract_inlined_call(clang::CallExpr
*call
,
209 clang::FunctionDecl
*fd
);
210 int set_inliner_arguments(pet_inliner
&inliner
, clang::CallExpr
*call
,
211 clang::FunctionDecl
*fd
);
213 __isl_give pet_expr
*extract_assume(clang::Expr
*expr
);
214 __isl_give pet_function_summary
*get_summary(clang::FunctionDecl
*fd
);
215 __isl_give pet_expr
*set_summary(__isl_take pet_expr
*expr
,
216 clang::FunctionDecl
*fd
);
217 __isl_give pet_expr
*extract_argument(clang::FunctionDecl
*fd
, int pos
,
218 clang::Expr
*expr
, bool detect_writes
);
219 __isl_give pet_expr
*extract_expr(const llvm::APInt
&val
);
220 __isl_give pet_expr
*extract_expr(clang::Expr
*expr
);
221 __isl_give pet_expr
*extract_expr(clang::UnaryOperator
*expr
);
222 __isl_give pet_expr
*extract_expr(clang::BinaryOperator
*expr
);
223 __isl_give pet_expr
*extract_expr(clang::ImplicitCastExpr
*expr
);
224 __isl_give pet_expr
*extract_expr(clang::IntegerLiteral
*expr
);
225 __isl_give pet_expr
*extract_expr(clang::EnumConstantDecl
*expr
);
226 __isl_give pet_expr
*extract_expr(clang::FloatingLiteral
*expr
);
227 __isl_give pet_expr
*extract_expr(clang::ParenExpr
*expr
);
228 __isl_give pet_expr
*extract_expr(clang::ConditionalOperator
*expr
);
229 __isl_give pet_expr
*extract_expr(clang::CallExpr
*expr
);
230 __isl_give pet_expr
*extract_expr(clang::CStyleCastExpr
*expr
);
232 __isl_give pet_expr
*extract_access_expr(clang::QualType qt
,
233 __isl_take pet_expr
*index
);
234 __isl_give pet_expr
*extract_access_expr(clang::Expr
*expr
);
235 __isl_give pet_expr
*extract_access_expr(clang::ValueDecl
*decl
);
237 __isl_give pet_expr
*extract_index_expr(
238 clang::ArraySubscriptExpr
*expr
);
239 __isl_give pet_expr
*extract_index_expr(clang::Expr
*expr
);
240 __isl_give pet_expr
*extract_index_expr(clang::ImplicitCastExpr
*expr
);
241 __isl_give pet_expr
*extract_index_expr(clang::DeclRefExpr
*expr
);
242 __isl_give pet_expr
*extract_index_expr(clang::ValueDecl
*decl
);
243 __isl_give pet_expr
*extract_index_expr(clang::MemberExpr
*expr
);
245 __isl_give isl_val
*extract_int(clang::Expr
*expr
);
246 __isl_give isl_val
*extract_int(clang::ParenExpr
*expr
);
248 clang::FunctionDecl
*find_decl_from_name(clang::CallExpr
*call
,
250 clang::FunctionDecl
*get_summary_function(clang::CallExpr
*call
);
252 void report(clang::Stmt
*stmt
, unsigned id
);
253 void unsupported(clang::Stmt
*stmt
);
254 void report_unsupported_unary_operator(clang::Stmt
*stmt
);
255 void report_unsupported_statement_type(clang::Stmt
*stmt
);
256 void report_prototype_required(clang::Stmt
*stmt
);
257 void report_missing_increment(clang::Stmt
*stmt
);
258 void report_missing_summary_function(clang::Stmt
*stmt
);
259 void report_missing_summary_function_body(clang::Stmt
*stmt
);
260 void report_unsupported_inline_function_argument(clang::Stmt
*stmt
);