1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // InitTweak.cc -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Fri May 13 11:26:36 2016 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Mon Jun 10 13:30:00 2019 |
---|
13 | // Update Count : 5 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> // for find, all_of |
---|
17 | #include <cassert> // for assertf, assert, strict_dynamic_cast |
---|
18 | #include <iostream> // for ostream, cerr, endl |
---|
19 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
20 | #include <memory> // for __shared_ptr |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | #include "AST/Expr.hpp" |
---|
24 | #include "AST/Node.hpp" |
---|
25 | #include "AST/Stmt.hpp" |
---|
26 | #include "AST/Type.hpp" |
---|
27 | #include "Common/PassVisitor.h" |
---|
28 | #include "Common/SemanticError.h" // for SemanticError |
---|
29 | #include "Common/UniqueName.h" // for UniqueName |
---|
30 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
31 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
32 | #include "InitTweak.h" |
---|
33 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
34 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
35 | #include "SymTab/Autogen.h" |
---|
36 | #include "SymTab/Indexer.h" // for Indexer |
---|
37 | #include "SynTree/Attribute.h" // for Attribute |
---|
38 | #include "SynTree/Constant.h" // for Constant |
---|
39 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
40 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
41 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
42 | #include "SynTree/Label.h" // for Label |
---|
43 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
44 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
45 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
46 | #include "Tuples/Tuples.h" // for Tuples::isTtype |
---|
47 | |
---|
48 | namespace InitTweak { |
---|
49 | namespace { |
---|
50 | struct HasDesignations : public WithShortCircuiting { |
---|
51 | bool hasDesignations = false; |
---|
52 | |
---|
53 | void previsit( BaseSyntaxNode * ) { |
---|
54 | // short circuit if we already know there are designations |
---|
55 | if ( hasDesignations ) visit_children = false; |
---|
56 | } |
---|
57 | |
---|
58 | void previsit( Designation * des ) { |
---|
59 | // short circuit if we already know there are designations |
---|
60 | if ( hasDesignations ) visit_children = false; |
---|
61 | else if ( ! des->get_designators().empty() ) { |
---|
62 | hasDesignations = true; |
---|
63 | visit_children = false; |
---|
64 | } |
---|
65 | } |
---|
66 | }; |
---|
67 | |
---|
68 | struct InitDepthChecker : public WithGuards { |
---|
69 | bool depthOkay = true; |
---|
70 | Type * type; |
---|
71 | int curDepth = 0, maxDepth = 0; |
---|
72 | InitDepthChecker( Type * type ) : type( type ) { |
---|
73 | Type * t = type; |
---|
74 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { |
---|
75 | maxDepth++; |
---|
76 | t = at->get_base(); |
---|
77 | } |
---|
78 | maxDepth++; |
---|
79 | } |
---|
80 | void previsit( ListInit * ) { |
---|
81 | curDepth++; |
---|
82 | GuardAction( [this]() { curDepth--; } ); |
---|
83 | if ( curDepth > maxDepth ) depthOkay = false; |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | struct InitFlattener : public WithShortCircuiting { |
---|
88 | void previsit( SingleInit * singleInit ) { |
---|
89 | visit_children = false; |
---|
90 | argList.push_back( singleInit->value->clone() ); |
---|
91 | } |
---|
92 | std::list< Expression * > argList; |
---|
93 | }; |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
98 | PassVisitor<InitFlattener> flattener; |
---|
99 | maybeAccept( init, flattener ); |
---|
100 | return flattener.pass.argList; |
---|
101 | } |
---|
102 | |
---|
103 | bool isDesignated( Initializer * init ) { |
---|
104 | PassVisitor<HasDesignations> finder; |
---|
105 | maybeAccept( init, finder ); |
---|
106 | return finder.pass.hasDesignations; |
---|
107 | } |
---|
108 | |
---|
109 | bool checkInitDepth( ObjectDecl * objDecl ) { |
---|
110 | PassVisitor<InitDepthChecker> checker( objDecl->type ); |
---|
111 | maybeAccept( objDecl->init, checker ); |
---|
112 | return checker.pass.depthOkay; |
---|
113 | } |
---|
114 | |
---|
115 | std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init ) { |
---|
116 | #warning unimplmented |
---|
117 | (void)init; |
---|
118 | assert(false); |
---|
119 | return {}; |
---|
120 | } |
---|
121 | |
---|
122 | class InitExpander_old::ExpanderImpl { |
---|
123 | public: |
---|
124 | virtual ~ExpanderImpl() = default; |
---|
125 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
126 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
127 | }; |
---|
128 | |
---|
129 | class InitImpl_old : public InitExpander_old::ExpanderImpl { |
---|
130 | public: |
---|
131 | InitImpl_old( Initializer * init ) : init( init ) {} |
---|
132 | virtual ~InitImpl_old() = default; |
---|
133 | |
---|
134 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { |
---|
135 | // this is wrong, but just a placeholder for now |
---|
136 | // if ( ! flattened ) flatten( indices ); |
---|
137 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
138 | return makeInitList( init ); |
---|
139 | } |
---|
140 | |
---|
141 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
142 | private: |
---|
143 | Initializer * init; |
---|
144 | }; |
---|
145 | |
---|
146 | class ExprImpl_old : public InitExpander_old::ExpanderImpl { |
---|
147 | public: |
---|
148 | ExprImpl_old( Expression * expr ) : arg( expr ) {} |
---|
149 | virtual ~ExprImpl_old() { delete arg; } |
---|
150 | |
---|
151 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
152 | std::list< Expression * > ret; |
---|
153 | Expression * expr = maybeClone( arg ); |
---|
154 | if ( expr ) { |
---|
155 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
156 | // go through indices and layer on subscript exprs ?[?] |
---|
157 | ++it; |
---|
158 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
159 | subscriptExpr->get_args().push_back( expr ); |
---|
160 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
161 | expr = subscriptExpr; |
---|
162 | } |
---|
163 | ret.push_back( expr ); |
---|
164 | } |
---|
165 | return ret; |
---|
166 | } |
---|
167 | |
---|
168 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
169 | private: |
---|
170 | Expression * arg; |
---|
171 | }; |
---|
172 | |
---|
173 | InitExpander_old::InitExpander_old( Initializer * init ) : expander( new InitImpl_old( init ) ) {} |
---|
174 | |
---|
175 | InitExpander_old::InitExpander_old( Expression * expr ) : expander( new ExprImpl_old( expr ) ) {} |
---|
176 | |
---|
177 | std::list< Expression * > InitExpander_old::operator*() { |
---|
178 | return cur; |
---|
179 | } |
---|
180 | |
---|
181 | InitExpander_old & InitExpander_old::operator++() { |
---|
182 | cur = expander->next( indices ); |
---|
183 | return *this; |
---|
184 | } |
---|
185 | |
---|
186 | // use array indices list to build switch statement |
---|
187 | void InitExpander_old::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
188 | indices.push_back( index ); |
---|
189 | indices.push_back( dimension ); |
---|
190 | } |
---|
191 | |
---|
192 | void InitExpander_old::clearArrayIndices() { |
---|
193 | deleteAll( indices ); |
---|
194 | indices.clear(); |
---|
195 | } |
---|
196 | |
---|
197 | bool InitExpander_old::addReference() { |
---|
198 | bool added = false; |
---|
199 | for ( Expression *& expr : cur ) { |
---|
200 | expr = new AddressExpr( expr ); |
---|
201 | added = true; |
---|
202 | } |
---|
203 | return added; |
---|
204 | } |
---|
205 | |
---|
206 | namespace { |
---|
207 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
208 | /// if (i < d) f(..., init) |
---|
209 | /// ++i; |
---|
210 | /// so that only elements within the range of the array are constructed |
---|
211 | template< typename OutIterator > |
---|
212 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
213 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
214 | cond->get_args().push_back( index->clone() ); |
---|
215 | cond->get_args().push_back( dimension->clone() ); |
---|
216 | |
---|
217 | std::list< Expression * > args = makeInitList( init ); |
---|
218 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
219 | |
---|
220 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr ); |
---|
221 | |
---|
222 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
223 | increment->get_args().push_back( index->clone() ); |
---|
224 | *out++ = new ExprStmt( increment ); |
---|
225 | } |
---|
226 | |
---|
227 | template< typename OutIterator > |
---|
228 | void build( UntypedExpr * callExpr, InitExpander_old::IndexList::iterator idx, InitExpander_old::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
229 | if ( idx == idxEnd ) return; |
---|
230 | Expression * index = *idx++; |
---|
231 | assert( idx != idxEnd ); |
---|
232 | Expression * dimension = *idx++; |
---|
233 | |
---|
234 | // xxx - may want to eventually issue a warning here if we can detect |
---|
235 | // that the number of elements exceeds to dimension of the array |
---|
236 | if ( idx == idxEnd ) { |
---|
237 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
238 | for ( Initializer * init : *listInit ) { |
---|
239 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
240 | } |
---|
241 | } else { |
---|
242 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
243 | } |
---|
244 | } else { |
---|
245 | std::list< Statement * > branches; |
---|
246 | |
---|
247 | unsigned long cond = 0; |
---|
248 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
249 | if ( ! listInit ) { |
---|
250 | // xxx - this shouldn't be an error, but need a way to |
---|
251 | // terminate without creating output, so should catch this error |
---|
252 | SemanticError( init->location, "unbalanced list initializers" ); |
---|
253 | } |
---|
254 | |
---|
255 | static UniqueName targetLabel( "L__autogen__" ); |
---|
256 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
257 | for ( Initializer * init : *listInit ) { |
---|
258 | Expression * condition; |
---|
259 | // check for designations |
---|
260 | // if ( init-> ) { |
---|
261 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
262 | ++cond; |
---|
263 | // } else { |
---|
264 | // condition = // ... take designation |
---|
265 | // cond = // ... take designation+1 |
---|
266 | // } |
---|
267 | std::list< Statement * > stmts; |
---|
268 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
269 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) ); |
---|
270 | CaseStmt * caseStmt = new CaseStmt( condition, stmts ); |
---|
271 | branches.push_back( caseStmt ); |
---|
272 | } |
---|
273 | *out++ = new SwitchStmt( index->clone(), branches ); |
---|
274 | *out++ = new NullStmt( { switchLabel } ); |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | // if array came with an initializer list: initialize each element |
---|
280 | // may have more initializers than elements in the array - need to check at each index that |
---|
281 | // we haven't exceeded size. |
---|
282 | // may have fewer initializers than elements in the array - need to default construct |
---|
283 | // remaining elements. |
---|
284 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
285 | Statement * InitImpl_old::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
286 | if ( ! init ) return nullptr; |
---|
287 | CompoundStmt * block = new CompoundStmt(); |
---|
288 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
289 | if ( block->get_kids().empty() ) { |
---|
290 | delete block; |
---|
291 | return nullptr; |
---|
292 | } else { |
---|
293 | init = nullptr; // init was consumed in creating the list init |
---|
294 | return block; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | Statement * ExprImpl_old::buildListInit( UntypedExpr *, std::list< Expression * > & ) { |
---|
299 | return nullptr; |
---|
300 | } |
---|
301 | |
---|
302 | Statement * InitExpander_old::buildListInit( UntypedExpr * dst ) { |
---|
303 | return expander->buildListInit( dst, indices ); |
---|
304 | } |
---|
305 | |
---|
306 | class InitExpander_new::ExpanderImpl { |
---|
307 | public: |
---|
308 | virtual ~ExpanderImpl() = default; |
---|
309 | virtual std::vector< ast::ptr< ast::Expr > > next( IndexList & indices ) = 0; |
---|
310 | virtual ast::ptr< ast::Stmt > buildListInit( |
---|
311 | const ast::UntypedExpr * callExpr, IndexList & indices ) = 0; |
---|
312 | }; |
---|
313 | |
---|
314 | namespace { |
---|
315 | class InitImpl_new final : public InitExpander_new::ExpanderImpl { |
---|
316 | ast::ptr< ast::Init > init; |
---|
317 | public: |
---|
318 | InitImpl_new( const ast::Init * i ) : init( i ) {} |
---|
319 | |
---|
320 | std::vector< ast::ptr< ast::Expr > > next( InitExpander_new::IndexList & ) override { |
---|
321 | return makeInitList( init ); |
---|
322 | } |
---|
323 | |
---|
324 | ast::ptr< ast::Stmt > buildListInit( |
---|
325 | const ast::UntypedExpr * callExpr, InitExpander_new::IndexList & indices |
---|
326 | ) override { |
---|
327 | #warning unimplemented |
---|
328 | (void)callExpr; (void)indices; |
---|
329 | assert(false); |
---|
330 | return {}; |
---|
331 | } |
---|
332 | }; |
---|
333 | |
---|
334 | class ExprImpl_new final : public InitExpander_new::ExpanderImpl { |
---|
335 | ast::ptr< ast::Expr > arg; |
---|
336 | public: |
---|
337 | ExprImpl_new( const ast::Expr * a ) : arg( a ) {} |
---|
338 | |
---|
339 | std::vector< ast::ptr< ast::Expr > > next( |
---|
340 | InitExpander_new::IndexList & indices |
---|
341 | ) override { |
---|
342 | #warning unimplemented |
---|
343 | (void)indices; |
---|
344 | assert(false); |
---|
345 | return {}; |
---|
346 | } |
---|
347 | |
---|
348 | ast::ptr< ast::Stmt > buildListInit( |
---|
349 | const ast::UntypedExpr *, InitExpander_new::IndexList & |
---|
350 | ) override { |
---|
351 | return {}; |
---|
352 | } |
---|
353 | }; |
---|
354 | } // anonymous namespace |
---|
355 | |
---|
356 | InitExpander_new::InitExpander_new( const ast::Init * init ) |
---|
357 | : expander( new InitImpl_new{ init } ), crnt(), indices() {} |
---|
358 | |
---|
359 | InitExpander_new::InitExpander_new( const ast::Expr * expr ) |
---|
360 | : expander( new ExprImpl_new{ expr } ), crnt(), indices() {} |
---|
361 | |
---|
362 | std::vector< ast::ptr< ast::Expr > > InitExpander_new::operator* () { return crnt; } |
---|
363 | |
---|
364 | InitExpander_new & InitExpander_new::operator++ () { |
---|
365 | crnt = expander->next( indices ); |
---|
366 | return *this; |
---|
367 | } |
---|
368 | |
---|
369 | /// builds statement which has the same semantics as a C-style list initializer (for array |
---|
370 | /// initializers) using callExpr as the base expression to perform initialization |
---|
371 | ast::ptr< ast::Stmt > InitExpander_new::buildListInit( const ast::UntypedExpr * callExpr ) { |
---|
372 | return expander->buildListInit( callExpr, indices ); |
---|
373 | } |
---|
374 | |
---|
375 | void InitExpander_new::addArrayIndex( const ast::Expr * index, const ast::Expr * dimension ) { |
---|
376 | indices.emplace_back( index ); |
---|
377 | indices.emplace_back( dimension ); |
---|
378 | } |
---|
379 | |
---|
380 | void InitExpander_new::clearArrayIndices() { indices.clear(); } |
---|
381 | |
---|
382 | bool InitExpander_new::addReference() { |
---|
383 | for ( ast::ptr< ast::Expr > & expr : crnt ) { |
---|
384 | expr = new ast::AddressExpr{ expr }; |
---|
385 | } |
---|
386 | return ! crnt.empty(); |
---|
387 | } |
---|
388 | |
---|
389 | Type * getTypeofThis( FunctionType * ftype ) { |
---|
390 | assertf( ftype, "getTypeofThis: nullptr ftype" ); |
---|
391 | ObjectDecl * thisParam = getParamThis( ftype ); |
---|
392 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type ); |
---|
393 | return refType->base; |
---|
394 | } |
---|
395 | |
---|
396 | ObjectDecl * getParamThis( FunctionType * ftype ) { |
---|
397 | assertf( ftype, "getParamThis: nullptr ftype" ); |
---|
398 | auto & params = ftype->parameters; |
---|
399 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() ); |
---|
400 | return strict_dynamic_cast< ObjectDecl * >( params.front() ); |
---|
401 | } |
---|
402 | |
---|
403 | bool tryConstruct( DeclarationWithType * dwt ) { |
---|
404 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); |
---|
405 | if ( ! objDecl ) return false; |
---|
406 | return (objDecl->get_init() == nullptr || |
---|
407 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) |
---|
408 | && ! objDecl->get_storageClasses().is_extern |
---|
409 | && isConstructable( objDecl->type ); |
---|
410 | } |
---|
411 | |
---|
412 | bool isConstructable( Type * type ) { |
---|
413 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
414 | } |
---|
415 | |
---|
416 | struct CallFinder_old { |
---|
417 | CallFinder_old( const std::list< std::string > & names ) : names( names ) {} |
---|
418 | |
---|
419 | void postvisit( ApplicationExpr * appExpr ) { |
---|
420 | handleCallExpr( appExpr ); |
---|
421 | } |
---|
422 | |
---|
423 | void postvisit( UntypedExpr * untypedExpr ) { |
---|
424 | handleCallExpr( untypedExpr ); |
---|
425 | } |
---|
426 | |
---|
427 | std::list< Expression * > * matches; |
---|
428 | private: |
---|
429 | const std::list< std::string > names; |
---|
430 | |
---|
431 | template< typename CallExpr > |
---|
432 | void handleCallExpr( CallExpr * expr ) { |
---|
433 | std::string fname = getFunctionName( expr ); |
---|
434 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
435 | matches->push_back( expr ); |
---|
436 | } |
---|
437 | } |
---|
438 | }; |
---|
439 | |
---|
440 | struct CallFinder_new final { |
---|
441 | std::vector< ast::ptr< ast::Expr > > matches; |
---|
442 | const std::vector< std::string > names; |
---|
443 | |
---|
444 | CallFinder_new( std::vector< std::string > && ns ) : matches(), names( std::move(ns) ) {} |
---|
445 | |
---|
446 | void handleCallExpr( const ast::Expr * expr ) { |
---|
447 | std::string fname = getFunctionName( expr ); |
---|
448 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
449 | matches.emplace_back( expr ); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | void postvisit( const ast::ApplicationExpr * expr ) { handleCallExpr( expr ); } |
---|
454 | void postvisit( const ast::UntypedExpr * expr ) { handleCallExpr( expr ); } |
---|
455 | }; |
---|
456 | |
---|
457 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
458 | static PassVisitor<CallFinder_old> finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
459 | finder.pass.matches = &matches; |
---|
460 | maybeAccept( stmt, finder ); |
---|
461 | } |
---|
462 | |
---|
463 | std::vector< ast::ptr< ast::Expr > > collectCtorDtorCalls( const ast::Stmt * stmt ) { |
---|
464 | ast::Pass< CallFinder_new > finder{ std::vector< std::string >{ "?{}", "^?{}" } }; |
---|
465 | maybe_accept( stmt, finder ); |
---|
466 | return std::move( finder.pass.matches ); |
---|
467 | } |
---|
468 | |
---|
469 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
470 | std::list< Expression * > matches; |
---|
471 | collectCtorDtorCalls( stmt, matches ); |
---|
472 | assertf( matches.size() <= 1, "%zd constructor/destructors found in %s", matches.size(), toString( stmt ).c_str() ); |
---|
473 | return matches.size() == 1 ? matches.front() : nullptr; |
---|
474 | } |
---|
475 | |
---|
476 | namespace { |
---|
477 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
478 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ); |
---|
479 | |
---|
480 | template<typename CallExpr> |
---|
481 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
482 | // (*f)(x) => should get "f" |
---|
483 | std::string name = getFunctionName( expr ); |
---|
484 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
485 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
486 | return getCalledFunction( expr->get_args().front() ); |
---|
487 | } |
---|
488 | |
---|
489 | template<typename CallExpr> |
---|
490 | const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) { |
---|
491 | // (*f)(x) => should get "f" |
---|
492 | std::string name = getFunctionName( expr ); |
---|
493 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
494 | assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" ); |
---|
495 | return getCalledFunction( expr->args.front() ); |
---|
496 | } |
---|
497 | |
---|
498 | |
---|
499 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
500 | assert( expr ); |
---|
501 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
502 | return varExpr->var; |
---|
503 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
504 | return memberExpr->member; |
---|
505 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
506 | return getCalledFunction( castExpr->arg ); |
---|
507 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
508 | return handleDerefCalledFunction( untypedExpr ); |
---|
509 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
510 | return handleDerefCalledFunction( appExpr ); |
---|
511 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
512 | return getCalledFunction( addrExpr->arg ); |
---|
513 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { |
---|
514 | return getCalledFunction( commaExpr->arg2 ); |
---|
515 | } |
---|
516 | return nullptr; |
---|
517 | } |
---|
518 | |
---|
519 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) { |
---|
520 | assert( expr ); |
---|
521 | if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) { |
---|
522 | return varExpr->var; |
---|
523 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) { |
---|
524 | return memberExpr->member; |
---|
525 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) { |
---|
526 | return getCalledFunction( castExpr->arg ); |
---|
527 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) { |
---|
528 | return handleDerefCalledFunction( untypedExpr ); |
---|
529 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) { |
---|
530 | return handleDerefCalledFunction( appExpr ); |
---|
531 | } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) { |
---|
532 | return getCalledFunction( addrExpr->arg ); |
---|
533 | } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) { |
---|
534 | return getCalledFunction( commaExpr->arg2 ); |
---|
535 | } |
---|
536 | return nullptr; |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
541 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
542 | return getCalledFunction( appExpr->get_function() ); |
---|
543 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
544 | return getCalledFunction( untyped->get_function() ); |
---|
545 | } |
---|
546 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
547 | } |
---|
548 | |
---|
549 | const ast::DeclWithType * getFunction( const ast::Expr * expr ) { |
---|
550 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
551 | return getCalledFunction( appExpr->func ); |
---|
552 | } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
553 | return getCalledFunction( untyped->func ); |
---|
554 | } |
---|
555 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
556 | } |
---|
557 | |
---|
558 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
559 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
560 | if ( ! appExpr ) return nullptr; |
---|
561 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
562 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
563 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
564 | // will call all member dtors, and some members may have a user defined dtor. |
---|
565 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; |
---|
566 | } |
---|
567 | |
---|
568 | const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr ) { |
---|
569 | auto appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ); |
---|
570 | if ( ! appExpr ) return nullptr; |
---|
571 | |
---|
572 | const ast::DeclWithType * func = getCalledFunction( appExpr->func ); |
---|
573 | assertf( func, |
---|
574 | "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() ); |
---|
575 | |
---|
576 | // check for Intrinsic only -- don't want to remove all overridable ctor/dtor because |
---|
577 | // autogenerated ctor/dtor will call all member dtors, and some members may have a |
---|
578 | // user-defined dtor |
---|
579 | return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr; |
---|
580 | } |
---|
581 | |
---|
582 | namespace { |
---|
583 | template <typename Predicate> |
---|
584 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
585 | std::list< Expression * > callExprs; |
---|
586 | collectCtorDtorCalls( stmt, callExprs ); |
---|
587 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
588 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
589 | } |
---|
590 | |
---|
591 | template <typename Predicate> |
---|
592 | bool allofCtorDtor( const ast::Stmt * stmt, const Predicate & pred ) { |
---|
593 | std::vector< ast::ptr< ast::Expr > > callExprs = collectCtorDtorCalls( stmt ); |
---|
594 | return std::all_of( callExprs.begin(), callExprs.end(), pred ); |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
599 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
600 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
601 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->function->result ); |
---|
602 | assert( funcType ); |
---|
603 | return funcType->get_parameters().size() == 1; |
---|
604 | } |
---|
605 | return false; |
---|
606 | }); |
---|
607 | } |
---|
608 | |
---|
609 | bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt ) { |
---|
610 | return allofCtorDtor( stmt, []( const ast::Expr * callExpr ){ |
---|
611 | if ( const ast::ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
612 | const ast::FunctionType * funcType = |
---|
613 | GenPoly::getFunctionType( appExpr->func->result ); |
---|
614 | assert( funcType ); |
---|
615 | return funcType->params.size() == 1; |
---|
616 | } |
---|
617 | return false; |
---|
618 | }); |
---|
619 | } |
---|
620 | |
---|
621 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
622 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
623 | return isIntrinsicCallExpr( callExpr ); |
---|
624 | }); |
---|
625 | } |
---|
626 | |
---|
627 | namespace { |
---|
628 | template<typename CallExpr> |
---|
629 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
630 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
631 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
632 | if ( pos == 0 ) return arg; |
---|
633 | pos--; |
---|
634 | } |
---|
635 | assert( false ); |
---|
636 | } |
---|
637 | |
---|
638 | template<typename CallExpr> |
---|
639 | const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) { |
---|
640 | if( pos >= call->args.size() ) { |
---|
641 | assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", |
---|
642 | pos, toString( call ).c_str() ); |
---|
643 | } |
---|
644 | for ( const ast::Expr * arg : call->args ) { |
---|
645 | if ( pos == 0 ) return arg; |
---|
646 | --pos; |
---|
647 | } |
---|
648 | assert( false ); |
---|
649 | } |
---|
650 | } |
---|
651 | |
---|
652 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
653 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
654 | return callArg( appExpr, pos ); |
---|
655 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
656 | return callArg( untypedExpr, pos ); |
---|
657 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
658 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
659 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
660 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
661 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
662 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
663 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
664 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
665 | return getCallArg( copyCtor->callExpr, pos ); |
---|
666 | } else { |
---|
667 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) { |
---|
672 | if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) { |
---|
673 | return callArg( app, pos ); |
---|
674 | } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) { |
---|
675 | return callArg( untyped, pos ); |
---|
676 | } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) { |
---|
677 | const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; |
---|
678 | assertf( ! stmts.empty(), "TupleAssignExpr missing statements." ); |
---|
679 | auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() ); |
---|
680 | auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() ); |
---|
681 | assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr."); |
---|
682 | return getCallArg( tuple->exprs.front(), pos ); |
---|
683 | } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) { |
---|
684 | return getCallArg( ctor->callExpr, pos ); |
---|
685 | } else { |
---|
686 | assertf( false, "Unexpected expression type passed to getCallArg: %s", |
---|
687 | toString( call ).c_str() ); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | namespace { |
---|
692 | std::string funcName( Expression * func ); |
---|
693 | std::string funcName( const ast::Expr * func ); |
---|
694 | |
---|
695 | template<typename CallExpr> |
---|
696 | std::string handleDerefName( CallExpr * expr ) { |
---|
697 | // (*f)(x) => should get name "f" |
---|
698 | std::string name = getFunctionName( expr ); |
---|
699 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
700 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
701 | return funcName( expr->get_args().front() ); |
---|
702 | } |
---|
703 | |
---|
704 | template<typename CallExpr> |
---|
705 | std::string handleDerefName( const CallExpr * expr ) { |
---|
706 | // (*f)(x) => should get name "f" |
---|
707 | std::string name = getFunctionName( expr ); |
---|
708 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
709 | assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" ); |
---|
710 | return funcName( expr->args.front() ); |
---|
711 | } |
---|
712 | |
---|
713 | std::string funcName( Expression * func ) { |
---|
714 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
715 | return nameExpr->get_name(); |
---|
716 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
717 | return varExpr->get_var()->get_name(); |
---|
718 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
719 | return funcName( castExpr->get_arg() ); |
---|
720 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
721 | return memberExpr->get_member()->get_name(); |
---|
722 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
723 | return funcName( memberExpr->get_member() ); |
---|
724 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
725 | return handleDerefName( untypedExpr ); |
---|
726 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
727 | return handleDerefName( appExpr ); |
---|
728 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
729 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
730 | } else { |
---|
731 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
732 | } |
---|
733 | } |
---|
734 | |
---|
735 | std::string funcName( const ast::Expr * func ) { |
---|
736 | if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { |
---|
737 | return nameExpr->name; |
---|
738 | } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { |
---|
739 | return varExpr->var->name; |
---|
740 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { |
---|
741 | return funcName( castExpr->arg ); |
---|
742 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { |
---|
743 | return memberExpr->member->name; |
---|
744 | } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { |
---|
745 | return funcName( memberExpr->member ); |
---|
746 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { |
---|
747 | return handleDerefName( untypedExpr ); |
---|
748 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { |
---|
749 | return handleDerefName( appExpr ); |
---|
750 | } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { |
---|
751 | return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); |
---|
752 | } else { |
---|
753 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
754 | } |
---|
755 | } |
---|
756 | } |
---|
757 | |
---|
758 | std::string getFunctionName( Expression * expr ) { |
---|
759 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
760 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
761 | // can't possibly do anything reasonable. |
---|
762 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
763 | return funcName( appExpr->get_function() ); |
---|
764 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
765 | return funcName( untypedExpr->get_function() ); |
---|
766 | } else { |
---|
767 | std::cerr << expr << std::endl; |
---|
768 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
769 | } |
---|
770 | } |
---|
771 | |
---|
772 | std::string getFunctionName( const ast::Expr * expr ) { |
---|
773 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
774 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
775 | // can't possibly do anything reasonable. |
---|
776 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
777 | return funcName( appExpr->func ); |
---|
778 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
779 | return funcName( untypedExpr->func ); |
---|
780 | } else { |
---|
781 | std::cerr << expr << std::endl; |
---|
782 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | Type * getPointerBase( Type * type ) { |
---|
787 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
788 | return ptrType->get_base(); |
---|
789 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
790 | return arrayType->get_base(); |
---|
791 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
792 | return refType->get_base(); |
---|
793 | } else { |
---|
794 | return nullptr; |
---|
795 | } |
---|
796 | } |
---|
797 | const ast::Type* getPointerBase( const ast::Type* t ) { |
---|
798 | if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) { |
---|
799 | return p->base; |
---|
800 | } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) { |
---|
801 | return a->base; |
---|
802 | } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) { |
---|
803 | return r->base; |
---|
804 | } else return nullptr; |
---|
805 | } |
---|
806 | |
---|
807 | Type * isPointerType( Type * type ) { |
---|
808 | if ( getPointerBase( type ) ) return type; |
---|
809 | else return nullptr; |
---|
810 | } |
---|
811 | |
---|
812 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) { |
---|
813 | static FunctionDecl * assign = nullptr; |
---|
814 | if ( ! assign ) { |
---|
815 | // temporary? Generate a fake assignment operator to represent bitwise assignments. |
---|
816 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function. |
---|
817 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true ); |
---|
818 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr ); |
---|
819 | } |
---|
820 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) { |
---|
821 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { |
---|
822 | dst = new AddressExpr( dst ); |
---|
823 | } |
---|
824 | } else { |
---|
825 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) ); |
---|
826 | } |
---|
827 | if ( dynamic_cast< ReferenceType * >( src->result ) ) { |
---|
828 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) { |
---|
829 | src = new AddressExpr( src ); |
---|
830 | } |
---|
831 | // src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) ); |
---|
832 | } |
---|
833 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } ); |
---|
834 | } |
---|
835 | |
---|
836 | struct ConstExprChecker : public WithShortCircuiting { |
---|
837 | // most expressions are not const expr |
---|
838 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; } |
---|
839 | |
---|
840 | void previsit( AddressExpr *addressExpr ) { |
---|
841 | visit_children = false; |
---|
842 | |
---|
843 | // address of a variable or member expression is constexpr |
---|
844 | Expression * arg = addressExpr->get_arg(); |
---|
845 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
846 | } |
---|
847 | |
---|
848 | // these expressions may be const expr, depending on their children |
---|
849 | void previsit( SizeofExpr * ) {} |
---|
850 | void previsit( AlignofExpr * ) {} |
---|
851 | void previsit( UntypedOffsetofExpr * ) {} |
---|
852 | void previsit( OffsetofExpr * ) {} |
---|
853 | void previsit( OffsetPackExpr * ) {} |
---|
854 | void previsit( AttrExpr * ) {} |
---|
855 | void previsit( CommaExpr * ) {} |
---|
856 | void previsit( LogicalExpr * ) {} |
---|
857 | void previsit( ConditionalExpr * ) {} |
---|
858 | void previsit( CastExpr * ) {} |
---|
859 | void previsit( ConstantExpr * ) {} |
---|
860 | |
---|
861 | void previsit( VariableExpr * varExpr ) { |
---|
862 | visit_children = false; |
---|
863 | |
---|
864 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { |
---|
865 | long long int value; |
---|
866 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { |
---|
867 | // enumerators are const expr |
---|
868 | return; |
---|
869 | } |
---|
870 | } |
---|
871 | isConstExpr = false; |
---|
872 | } |
---|
873 | |
---|
874 | bool isConstExpr = true; |
---|
875 | }; |
---|
876 | |
---|
877 | bool isConstExpr( Expression * expr ) { |
---|
878 | if ( expr ) { |
---|
879 | PassVisitor<ConstExprChecker> checker; |
---|
880 | expr->accept( checker ); |
---|
881 | return checker.pass.isConstExpr; |
---|
882 | } |
---|
883 | return true; |
---|
884 | } |
---|
885 | |
---|
886 | bool isConstExpr( Initializer * init ) { |
---|
887 | if ( init ) { |
---|
888 | PassVisitor<ConstExprChecker> checker; |
---|
889 | init->accept( checker ); |
---|
890 | return checker.pass.isConstExpr; |
---|
891 | } // if |
---|
892 | // for all intents and purposes, no initializer means const expr |
---|
893 | return true; |
---|
894 | } |
---|
895 | |
---|
896 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
897 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
898 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
899 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
900 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
901 | |
---|
902 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { |
---|
903 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); |
---|
904 | if ( ! function ) return nullptr; |
---|
905 | if ( function->name != fname ) return nullptr; |
---|
906 | FunctionType * ftype = function->type; |
---|
907 | if ( ftype->parameters.size() != 2 ) return nullptr; |
---|
908 | |
---|
909 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
910 | Type * t2 = ftype->parameters.back()->get_type(); |
---|
911 | assert( t1 ); |
---|
912 | |
---|
913 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
914 | return function; |
---|
915 | } else { |
---|
916 | return nullptr; |
---|
917 | } |
---|
918 | } |
---|
919 | |
---|
920 | bool isCopyFunction( const ast::FunctionDecl * decl ) { |
---|
921 | const ast::FunctionType * ftype = decl->type; |
---|
922 | if ( ftype->params.size() != 2 ) return false; |
---|
923 | |
---|
924 | const ast::Type * t1 = getPointerBase( ftype->params.front()->get_type() ); |
---|
925 | if ( ! t1 ) return false; |
---|
926 | const ast::Type * t2 = ftype->params.back()->get_type(); |
---|
927 | |
---|
928 | return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} ); |
---|
929 | } |
---|
930 | |
---|
931 | FunctionDecl * isAssignment( Declaration * decl ) { |
---|
932 | return isCopyFunction( decl, "?=?" ); |
---|
933 | } |
---|
934 | FunctionDecl * isDestructor( Declaration * decl ) { |
---|
935 | if ( isDestructor( decl->get_name() ) ) { |
---|
936 | return dynamic_cast< FunctionDecl * >( decl ); |
---|
937 | } |
---|
938 | return nullptr; |
---|
939 | } |
---|
940 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { |
---|
941 | if ( isConstructor( decl->name ) ) { |
---|
942 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
943 | if ( func->type->parameters.size() == 1 ) { |
---|
944 | return func; |
---|
945 | } |
---|
946 | } |
---|
947 | } |
---|
948 | return nullptr; |
---|
949 | } |
---|
950 | FunctionDecl * isCopyConstructor( Declaration * decl ) { |
---|
951 | return isCopyFunction( decl, "?{}" ); |
---|
952 | } |
---|
953 | } |
---|