source: src/InitTweak/InitTweak.cc@ 366cf9b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 366cf9b was 3351cc0, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Updated benchmark creation for coroutines and better fix for inittweak destructor

  • Property mode set to 100644
File size: 23.8 KB
Line 
1#include <algorithm> // for find, all_of
2#include <cassert> // for assertf, assert, strict_dynamic_cast
3#include <iostream> // for ostream, cerr, endl
4#include <iterator> // for back_insert_iterator, back_inserter
5#include <memory> // for __shared_ptr
6
7#include "Common/SemanticError.h" // for SemanticError
8#include "Common/UniqueName.h" // for UniqueName
9#include "Common/utility.h" // for toString, deleteAll, maybeClone
10#include "GenPoly/GenPoly.h" // for getFunctionType
11#include "InitTweak.h"
12#include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
13#include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers
14#include "SymTab/Indexer.h" // for Indexer
15#include "SynTree/Attribute.h" // for Attribute
16#include "SynTree/Constant.h" // for Constant
17#include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType
18#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati...
19#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
20#include "SynTree/Label.h" // for Label, noLabels
21#include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt
22#include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType
23#include "SynTree/Visitor.h" // for Visitor, maybeAccept
24#include "Tuples/Tuples.h" // for Tuples::isTtype
25
26class UntypedValofExpr;
27
28namespace InitTweak {
29 namespace {
30 class HasDesignations : public Visitor {
31 public:
32 bool hasDesignations = false;
33 virtual void visit( Designation * des ) {
34 if ( ! des->get_designators().empty() ) hasDesignations = true;
35 else Visitor::visit( des );
36 }
37 };
38
39 class InitDepthChecker : public Visitor {
40 public:
41 bool depthOkay = true;
42 Type * type;
43 int curDepth = 0, maxDepth = 0;
44 InitDepthChecker( Type * type ) : type( type ) {
45 Type * t = type;
46 while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
47 maxDepth++;
48 t = at->get_base();
49 }
50 maxDepth++;
51 }
52 virtual void visit( ListInit * listInit ) {
53 curDepth++;
54 if ( curDepth > maxDepth ) depthOkay = false;
55 Visitor::visit( listInit );
56 curDepth--;
57 }
58 };
59
60 class InitFlattener : public Visitor {
61 public:
62 virtual void visit( SingleInit * singleInit );
63 virtual void visit( ListInit * listInit );
64 std::list< Expression * > argList;
65 };
66
67 void InitFlattener::visit( SingleInit * singleInit ) {
68 argList.push_back( singleInit->get_value()->clone() );
69 }
70
71 void InitFlattener::visit( ListInit * listInit ) {
72 // flatten nested list inits
73 std::list<Initializer*>::iterator it = listInit->begin();
74 for ( ; it != listInit->end(); ++it ) {
75 (*it)->accept( *this );
76 }
77 }
78 }
79
80 std::list< Expression * > makeInitList( Initializer * init ) {
81 InitFlattener flattener;
82 maybeAccept( init, flattener );
83 return flattener.argList;
84 }
85
86 bool isDesignated( Initializer * init ) {
87 HasDesignations finder;
88 maybeAccept( init, finder );
89 return finder.hasDesignations;
90 }
91
92 bool checkInitDepth( ObjectDecl * objDecl ) {
93 InitDepthChecker checker( objDecl->get_type() );
94 maybeAccept( objDecl->get_init(), checker );
95 return checker.depthOkay;
96 }
97
98 class InitExpander::ExpanderImpl {
99 public:
100 virtual ~ExpanderImpl() = default;
101 virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0;
102 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0;
103 };
104
105 class InitImpl : public InitExpander::ExpanderImpl {
106 public:
107 InitImpl( Initializer * init ) : init( init ) {}
108 virtual ~InitImpl() = default;
109
110 virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) {
111 // this is wrong, but just a placeholder for now
112 // if ( ! flattened ) flatten( indices );
113 // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >();
114 return makeInitList( init );
115 }
116
117 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
118 private:
119 Initializer * init;
120 };
121
122 class ExprImpl : public InitExpander::ExpanderImpl {
123 public:
124 ExprImpl( Expression * expr ) : arg( expr ) {}
125 virtual ~ExprImpl() { delete arg; }
126
127 virtual std::list< Expression * > next( std::list< Expression * > & indices ) {
128 std::list< Expression * > ret;
129 Expression * expr = maybeClone( arg );
130 if ( expr ) {
131 for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) {
132 // go through indices and layer on subscript exprs ?[?]
133 ++it;
134 UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") );
135 subscriptExpr->get_args().push_back( expr );
136 subscriptExpr->get_args().push_back( (*it)->clone() );
137 expr = subscriptExpr;
138 }
139 ret.push_back( expr );
140 }
141 return ret;
142 }
143
144 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
145 private:
146 Expression * arg;
147 };
148
149 InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {}
150
151 InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {}
152
153 std::list< Expression * > InitExpander::operator*() {
154 return cur;
155 }
156
157 InitExpander & InitExpander::operator++() {
158 cur = expander->next( indices );
159 return *this;
160 }
161
162 // use array indices list to build switch statement
163 void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) {
164 indices.push_back( index );
165 indices.push_back( dimension );
166 }
167
168 void InitExpander::clearArrayIndices() {
169 deleteAll( indices );
170 indices.clear();
171 }
172
173 bool InitExpander::addReference() {
174 bool added = false;
175 for ( Expression *& expr : cur ) {
176 expr = new AddressExpr( expr );
177 added = true;
178 }
179 return added;
180 }
181
182 namespace {
183 /// given index i, dimension d, initializer init, and callExpr f, generates
184 /// if (i < d) f(..., init)
185 /// ++i;
186 /// so that only elements within the range of the array are constructed
187 template< typename OutIterator >
188 void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
189 UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") );
190 cond->get_args().push_back( index->clone() );
191 cond->get_args().push_back( dimension->clone() );
192
193 std::list< Expression * > args = makeInitList( init );
194 callExpr->get_args().splice( callExpr->get_args().end(), args );
195
196 *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), nullptr );
197
198 UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) );
199 increment->get_args().push_back( index->clone() );
200 *out++ = new ExprStmt( noLabels, increment );
201 }
202
203 template< typename OutIterator >
204 void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) {
205 if ( idx == idxEnd ) return;
206 Expression * index = *idx++;
207 assert( idx != idxEnd );
208 Expression * dimension = *idx++;
209
210 // xxx - may want to eventually issue a warning here if we can detect
211 // that the number of elements exceeds to dimension of the array
212 if ( idx == idxEnd ) {
213 if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) {
214 for ( Initializer * init : *listInit ) {
215 buildCallExpr( callExpr->clone(), index, dimension, init, out );
216 }
217 } else {
218 buildCallExpr( callExpr->clone(), index, dimension, init, out );
219 }
220 } else {
221 std::list< Statement * > branches;
222
223 unsigned long cond = 0;
224 ListInit * listInit = dynamic_cast< ListInit * >( init );
225 if ( ! listInit ) {
226 // xxx - this shouldn't be an error, but need a way to
227 // terminate without creating output, so should catch this error
228 throw SemanticError( "unbalanced list initializers" );
229 }
230
231 static UniqueName targetLabel( "L__autogen__" );
232 Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } );
233 for ( Initializer * init : *listInit ) {
234 Expression * condition;
235 // check for designations
236 // if ( init-> ) {
237 condition = new ConstantExpr( Constant::from_ulong( cond ) );
238 ++cond;
239 // } else {
240 // condition = // ... take designation
241 // cond = // ... take designation+1
242 // }
243 std::list< Statement * > stmts;
244 build( callExpr, idx, idxEnd, init, back_inserter( stmts ) );
245 stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) );
246 CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts );
247 branches.push_back( caseStmt );
248 }
249 *out++ = new SwitchStmt( noLabels, index->clone(), branches );
250 *out++ = new NullStmt( std::list<Label>{ switchLabel } );
251 }
252 }
253 }
254
255 // if array came with an initializer list: initialize each element
256 // may have more initializers than elements in the array - need to check at each index that
257 // we haven't exceeded size.
258 // may have fewer initializers than elements in the array - need to default construct
259 // remaining elements.
260 // To accomplish this, generate switch statement, consuming all of expander's elements
261 Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) {
262 if ( ! init ) return nullptr;
263 CompoundStmt * block = new CompoundStmt( noLabels );
264 build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) );
265 if ( block->get_kids().empty() ) {
266 delete block;
267 return nullptr;
268 } else {
269 init = nullptr; // init was consumed in creating the list init
270 return block;
271 }
272 }
273
274 Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) {
275 return nullptr;
276 }
277
278 Statement * InitExpander::buildListInit( UntypedExpr * dst ) {
279 return expander->buildListInit( dst, indices );
280 }
281
282 Type * getTypeofThis( FunctionType * ftype ) {
283 assertf( ftype, "getTypeofThis: nullptr ftype" );
284 ObjectDecl * thisParam = getParamThis( ftype );
285 ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type );
286 return refType->base;
287 }
288
289 ObjectDecl * getParamThis( FunctionType * ftype ) {
290 assertf( ftype, "getParamThis: nullptr ftype" );
291 auto & params = ftype->parameters;
292 assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() );
293 return strict_dynamic_cast< ObjectDecl * >( params.front() );
294 }
295
296 bool tryConstruct( DeclarationWithType * dwt ) {
297 ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
298 if ( ! objDecl ) return false;
299 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
300 (objDecl->get_init() == nullptr ||
301 ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() ))
302 && ! objDecl->get_storageClasses().is_extern
303 && isConstructable( objDecl->type );
304 }
305
306 bool isConstructable( Type * type ) {
307 return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type );
308 }
309
310 class CallFinder : public Visitor {
311 public:
312 typedef Visitor Parent;
313 CallFinder( const std::list< std::string > & names ) : names( names ) {}
314
315 virtual void visit( ApplicationExpr * appExpr ) {
316 handleCallExpr( appExpr );
317 }
318
319 virtual void visit( UntypedExpr * untypedExpr ) {
320 handleCallExpr( untypedExpr );
321 }
322
323 std::list< Expression * > * matches;
324 private:
325 const std::list< std::string > names;
326
327 template< typename CallExpr >
328 void handleCallExpr( CallExpr * expr ) {
329 Parent::visit( expr );
330 std::string fname = getFunctionName( expr );
331 if ( std::find( names.begin(), names.end(), fname ) != names.end() ) {
332 matches->push_back( expr );
333 }
334 }
335 };
336
337 void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) {
338 static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } );
339 finder.matches = &matches;
340 maybeAccept( stmt, finder );
341 }
342
343 Expression * getCtorDtorCall( Statement * stmt ) {
344 std::list< Expression * > matches;
345 collectCtorDtorCalls( stmt, matches );
346 assert( matches.size() <= 1 );
347 return matches.size() == 1 ? matches.front() : nullptr;
348 }
349
350 namespace {
351 DeclarationWithType * getCalledFunction( Expression * expr );
352
353 template<typename CallExpr>
354 DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) {
355 // (*f)(x) => should get "f"
356 std::string name = getFunctionName( expr );
357 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
358 assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" );
359 return getCalledFunction( expr->get_args().front() );
360 }
361
362 DeclarationWithType * getCalledFunction( Expression * expr ) {
363 assert( expr );
364 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
365 return varExpr->var;
366 } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) {
367 return memberExpr->member;
368 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
369 return getCalledFunction( castExpr->arg );
370 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) {
371 return handleDerefCalledFunction( untypedExpr );
372 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
373 return handleDerefCalledFunction( appExpr );
374 } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
375 return getCalledFunction( addrExpr->arg );
376 } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) {
377 return getCalledFunction( commaExpr->arg2 );
378 }
379 return nullptr;
380 }
381 }
382
383 DeclarationWithType * getFunction( Expression * expr ) {
384 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
385 return getCalledFunction( appExpr->get_function() );
386 } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) {
387 return getCalledFunction( untyped->get_function() );
388 }
389 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
390 }
391
392 ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
393 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
394 if ( ! appExpr ) return nullptr;
395 DeclarationWithType * function = getCalledFunction( appExpr->get_function() );
396 assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() );
397 // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
398 // will call all member dtors, and some members may have a user defined dtor.
399 return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr;
400 }
401
402 namespace {
403 template <typename Predicate>
404 bool allofCtorDtor( Statement * stmt, const Predicate & pred ) {
405 std::list< Expression * > callExprs;
406 collectCtorDtorCalls( stmt, callExprs );
407 // if ( callExprs.empty() ) return false; // xxx - do I still need this check?
408 return std::all_of( callExprs.begin(), callExprs.end(), pred);
409 }
410 }
411
412 bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
413 return allofCtorDtor( stmt, []( Expression * callExpr ){
414 if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
415 FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() );
416 assert( funcType );
417 return funcType->get_parameters().size() == 1;
418 }
419 return false;
420 });
421 }
422
423 bool isIntrinsicCallStmt( Statement * stmt ) {
424 return allofCtorDtor( stmt, []( Expression * callExpr ) {
425 return isIntrinsicCallExpr( callExpr );
426 });
427 }
428
429 namespace {
430 template<typename CallExpr>
431 Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
432 if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() );
433 for ( Expression *& arg : callExpr->get_args() ) {
434 if ( pos == 0 ) return arg;
435 pos--;
436 }
437 assert( false );
438 }
439 }
440
441 Expression *& getCallArg( Expression * callExpr, unsigned int pos ) {
442 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
443 return callArg( appExpr, pos );
444 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
445 return callArg( untypedExpr, pos );
446 } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) {
447 std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids();
448 assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." );
449 ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() );
450 TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() );
451 assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." );
452 return getCallArg( tuple->get_exprs().front(), pos );
453 } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) {
454 return getCallArg( copyCtor->callExpr, pos );
455 } else {
456 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() );
457 }
458 }
459
460 namespace {
461 std::string funcName( Expression * func );
462
463 template<typename CallExpr>
464 std::string handleDerefName( CallExpr * expr ) {
465 // (*f)(x) => should get name "f"
466 std::string name = getFunctionName( expr );
467 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
468 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
469 return funcName( expr->get_args().front() );
470 }
471
472 std::string funcName( Expression * func ) {
473 if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
474 return nameExpr->get_name();
475 } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
476 return varExpr->get_var()->get_name();
477 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
478 return funcName( castExpr->get_arg() );
479 } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) {
480 return memberExpr->get_member()->get_name();
481 } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) {
482 return funcName( memberExpr->get_member() );
483 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) {
484 return handleDerefName( untypedExpr );
485 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) {
486 return handleDerefName( appExpr );
487 } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) {
488 return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) );
489 } else {
490 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
491 }
492 }
493 }
494
495 std::string getFunctionName( Expression * expr ) {
496 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
497 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
498 // can't possibly do anything reasonable.
499 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
500 return funcName( appExpr->get_function() );
501 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
502 return funcName( untypedExpr->get_function() );
503 } else {
504 std::cerr << expr << std::endl;
505 assertf( false, "Unexpected expression type passed to getFunctionName" );
506 }
507 }
508
509 Type * getPointerBase( Type * type ) {
510 if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
511 return ptrType->get_base();
512 } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
513 return arrayType->get_base();
514 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
515 return refType->get_base();
516 } else {
517 return nullptr;
518 }
519 }
520
521 Type * isPointerType( Type * type ) {
522 if ( getPointerBase( type ) ) return type;
523 else return nullptr;
524 }
525
526 class ConstExprChecker : public Visitor {
527 public:
528 ConstExprChecker() : isConstExpr( true ) {}
529
530 using Visitor::visit;
531
532 virtual void visit( ApplicationExpr * ) { isConstExpr = false; }
533 virtual void visit( UntypedExpr * ) { isConstExpr = false; }
534 virtual void visit( NameExpr * ) { isConstExpr = false; }
535 // virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
536 virtual void visit( AddressExpr *addressExpr ) {
537 // address of a variable or member expression is constexpr
538 Expression * arg = addressExpr->get_arg();
539 if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false;
540 }
541 virtual void visit( UntypedMemberExpr * ) { isConstExpr = false; }
542 virtual void visit( MemberExpr * ) { isConstExpr = false; }
543 virtual void visit( VariableExpr * ) { isConstExpr = false; }
544 // these might be okay?
545 // virtual void visit( SizeofExpr *sizeofExpr );
546 // virtual void visit( AlignofExpr *alignofExpr );
547 // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
548 // virtual void visit( OffsetofExpr *offsetofExpr );
549 // virtual void visit( OffsetPackExpr *offsetPackExpr );
550 // virtual void visit( AttrExpr *attrExpr );
551 // virtual void visit( CommaExpr *commaExpr );
552 // virtual void visit( LogicalExpr *logicalExpr );
553 // virtual void visit( ConditionalExpr *conditionalExpr );
554 virtual void visit( TypeExpr * ) { isConstExpr = false; }
555 virtual void visit( AsmExpr * ) { isConstExpr = false; }
556 virtual void visit( UntypedValofExpr * ) { isConstExpr = false; }
557 virtual void visit( CompoundLiteralExpr * ) { isConstExpr = false; }
558 virtual void visit( UntypedTupleExpr * ) { isConstExpr = false; }
559 virtual void visit( TupleExpr * ) { isConstExpr = false; }
560 virtual void visit( TupleAssignExpr * ) { isConstExpr = false; }
561
562 bool isConstExpr;
563 };
564
565 bool isConstExpr( Expression * expr ) {
566 if ( expr ) {
567 ConstExprChecker checker;
568 expr->accept( checker );
569 return checker.isConstExpr;
570 }
571 return true;
572 }
573
574 bool isConstExpr( Initializer * init ) {
575 if ( init ) {
576 ConstExprChecker checker;
577 init->accept( checker );
578 return checker.isConstExpr;
579 } // if
580 // for all intents and purposes, no initializer means const expr
581 return true;
582 }
583
584 bool isConstructor( const std::string & str ) { return str == "?{}"; }
585 bool isDestructor( const std::string & str ) { return str == "^?{}"; }
586 bool isAssignment( const std::string & str ) { return str == "?=?"; }
587 bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); }
588 bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); }
589
590 FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) {
591 FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl );
592 if ( ! function ) return nullptr;
593 if ( function->name != fname ) return nullptr;
594 FunctionType * ftype = function->type;
595 if ( ftype->parameters.size() != 2 ) return nullptr;
596
597 Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() );
598 Type * t2 = ftype->parameters.back()->get_type();
599 assert( t1 );
600
601 if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) {
602 return function;
603 } else {
604 return nullptr;
605 }
606 }
607
608 FunctionDecl * isAssignment( Declaration * decl ) {
609 return isCopyFunction( decl, "?=?" );
610 }
611 FunctionDecl * isDestructor( Declaration * decl ) {
612 if ( isDestructor( decl->get_name() ) ) {
613 return dynamic_cast< FunctionDecl * >( decl );
614 }
615 return nullptr;
616 }
617 FunctionDecl * isDefaultConstructor( Declaration * decl ) {
618 if ( isConstructor( decl->name ) ) {
619 if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
620 if ( func->type->parameters.size() == 1 ) {
621 return func;
622 }
623 }
624 }
625 return nullptr;
626 }
627 FunctionDecl * isCopyConstructor( Declaration * decl ) {
628 return isCopyFunction( decl, "?{}" );
629 }
630}
Note: See TracBrowser for help on using the repository browser.