source: src/InitTweak/InitTweak.cc@ 10295d8

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 10295d8 was 549c006, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Implemented out of order waitfor for destructors

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