source: src/InitTweak/InitTweak.cc@ 696bf6e

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 696bf6e was 29bc63e, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add isConstructable helper to InitTweak

  • Property mode set to 100644
File size: 22.9 KB
RevLine 
[d180746]1#include <algorithm> // for find, all_of
[e3e16bc]2#include <cassert> // for assertf, assert, strict_dynamic_cast
[d180746]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
[2b46a13]11#include "InitTweak.h"
[d180746]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
[29bc63e]24#include "Tuples/Tuples.h" // for Tuples::isTtype
[d180746]25
26class UntypedValofExpr;
[2b46a13]27
28namespace InitTweak {
[64071c2]29 namespace {
30 class HasDesignations : public Visitor {
31 public:
32 bool hasDesignations = false;
[e4d829b]33 virtual void visit( Designation * des ) {
34 if ( ! des->get_designators().empty() ) hasDesignations = true;
35 else Visitor::visit( des );
[64071c2]36 }
37 };
[2b46a13]38
[dcd73d1]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
[4d2434a]60 class InitFlattener : public Visitor {
[64071c2]61 public:
62 virtual void visit( SingleInit * singleInit );
63 virtual void visit( ListInit * listInit );
64 std::list< Expression * > argList;
65 };
[2b46a13]66
[4d2434a]67 void InitFlattener::visit( SingleInit * singleInit ) {
[64071c2]68 argList.push_back( singleInit->get_value()->clone() );
69 }
[2b46a13]70
[4d2434a]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 ) {
[64071c2]75 (*it)->accept( *this );
76 }
77 }
78 }
[2b46a13]79
[64071c2]80 std::list< Expression * > makeInitList( Initializer * init ) {
[4d2434a]81 InitFlattener flattener;
82 maybeAccept( init, flattener );
83 return flattener.argList;
[64071c2]84 }
[2b46a13]85
[64071c2]86 bool isDesignated( Initializer * init ) {
87 HasDesignations finder;
88 maybeAccept( init, finder );
89 return finder.hasDesignations;
[dcd73d1]90 }
91
92 bool checkInitDepth( ObjectDecl * objDecl ) {
93 InitDepthChecker checker( objDecl->get_type() );
94 maybeAccept( objDecl->get_init(), checker );
95 return checker.depthOkay;
[64071c2]96 }
[2b46a13]97
[39f84a4]98 class InitExpander::ExpanderImpl {
99 public:
100 virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0;
[4d2434a]101 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0;
[39f84a4]102 };
103
104 class InitImpl : public InitExpander::ExpanderImpl {
105 public:
[4d2434a]106 InitImpl( Initializer * init ) : init( init ) {}
[39f84a4]107
[7e003011]108 virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) {
[39f84a4]109 // this is wrong, but just a placeholder for now
[4d2434a]110 // if ( ! flattened ) flatten( indices );
111 // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >();
112 return makeInitList( init );
[39f84a4]113 }
[4d2434a]114
115 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
[39f84a4]116 private:
[4d2434a]117 Initializer * init;
[39f84a4]118 };
119
120 class ExprImpl : public InitExpander::ExpanderImpl {
121 public:
122 ExprImpl( Expression * expr ) : arg( expr ) {}
123
[9b4c936]124 ~ExprImpl() { delete arg; }
125
[39f84a4]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 }
[4d2434a]142
143 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
[39f84a4]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
[4d2434a]167 void InitExpander::clearArrayIndices() {
[9b4c936]168 deleteAll( indices );
[4d2434a]169 indices.clear();
170 }
171
172 namespace {
[f9cebb5]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
[4d2434a]177 template< typename OutIterator >
[f9cebb5]178 void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
[4d2434a]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
[22bc276]186 *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), nullptr );
[4d2434a]187
188 UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) );
[175ad32b]189 increment->get_args().push_back( index->clone() );
[4d2434a]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
[f9cebb5]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
[4d2434a]202 if ( idx == idxEnd ) {
203 if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) {
204 for ( Initializer * init : *listInit ) {
[f9cebb5]205 buildCallExpr( callExpr->clone(), index, dimension, init, out );
[4d2434a]206 }
207 } else {
[f9cebb5]208 buildCallExpr( callExpr->clone(), index, dimension, init, out );
[4d2434a]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 }
[f9cebb5]220
221 static UniqueName targetLabel( "L__autogen__" );
222 Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } );
[4d2434a]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 ) );
[f9cebb5]235 stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) );
[4d2434a]236 CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts );
237 branches.push_back( caseStmt );
238 }
239 *out++ = new SwitchStmt( noLabels, index->clone(), branches );
[f9cebb5]240 *out++ = new NullStmt( std::list<Label>{ switchLabel } );
[4d2434a]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 ) {
[22bc276]252 if ( ! init ) return nullptr;
[f9cebb5]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;
[22bc276]257 return nullptr;
[4d2434a]258 } else {
[22bc276]259 init = nullptr; // init was consumed in creating the list init
[f9cebb5]260 return block;
[4d2434a]261 }
[39f84a4]262 }
263
[22bc276]264 Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) {
265 return nullptr;
[4d2434a]266 }
267
268 Statement * InitExpander::buildListInit( UntypedExpr * dst ) {
269 return expander->buildListInit( dst, indices );
270 }
271
[22bc276]272 bool tryConstruct( DeclarationWithType * dwt ) {
273 ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
274 if ( ! objDecl ) return false;
[64071c2]275 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
[22bc276]276 (objDecl->get_init() == nullptr ||
277 ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() ))
278 && ! objDecl->get_storageClasses().is_extern
[29bc63e]279 && isConstructable( objDecl->type );
280 }
281
282 bool isConstructable( Type * type ) {
283 return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type );
[64071c2]284 }
[2b46a13]285
[4d2434a]286 class CallFinder : public Visitor {
287 public:
288 typedef Visitor Parent;
289 CallFinder( const std::list< std::string > & names ) : names( names ) {}
290
291 virtual void visit( ApplicationExpr * appExpr ) {
292 handleCallExpr( appExpr );
293 }
294
295 virtual void visit( UntypedExpr * untypedExpr ) {
296 handleCallExpr( untypedExpr );
297 }
298
299 std::list< Expression * > * matches;
300 private:
301 const std::list< std::string > names;
302
303 template< typename CallExpr >
304 void handleCallExpr( CallExpr * expr ) {
305 Parent::visit( expr );
306 std::string fname = getFunctionName( expr );
307 if ( std::find( names.begin(), names.end(), fname ) != names.end() ) {
308 matches->push_back( expr );
[cad355a]309 }
[64071c2]310 }
[4d2434a]311 };
312
313 void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) {
314 static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } );
315 finder.matches = &matches;
316 maybeAccept( stmt, finder );
[64071c2]317 }
[4d2434a]318
319 Expression * getCtorDtorCall( Statement * stmt ) {
320 std::list< Expression * > matches;
321 collectCtorDtorCalls( stmt, matches );
322 assert( matches.size() <= 1 );
[22bc276]323 return matches.size() == 1 ? matches.front() : nullptr;
[4d2434a]324 }
325
[aedfd91]326 namespace {
[599b386]327 DeclarationWithType * getCalledFunction( Expression * expr );
328
329 template<typename CallExpr>
330 DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) {
331 // (*f)(x) => should get "f"
332 std::string name = getFunctionName( expr );
333 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
[b128d3e]334 assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" );
[599b386]335 return getCalledFunction( expr->get_args().front() );
336 }
337
[ee1635c8]338 DeclarationWithType * getCalledFunction( Expression * expr ) {
339 assert( expr );
340 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
341 return varExpr->get_var();
342 } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) {
343 return memberExpr->get_member();
344 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
345 return getCalledFunction( castExpr->get_arg() );
[599b386]346 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) {
347 return handleDerefCalledFunction( untypedExpr );
348 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
349 return handleDerefCalledFunction( appExpr );
[f3b0a07]350 } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
351 return getCalledFunction( addrExpr->get_arg() );
[ee1635c8]352 }
353 return nullptr;
[aedfd91]354 }
355 }
[70f89d00]356
[b7b8674]357 DeclarationWithType * getFunction( Expression * expr ) {
358 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
359 return getCalledFunction( appExpr->get_function() );
360 } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) {
361 return getCalledFunction( untyped->get_function() );
362 }
363 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
364 }
365
[aedfd91]366 ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
367 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
[22bc276]368 if ( ! appExpr ) return nullptr;
[ee1635c8]369 DeclarationWithType * function = getCalledFunction( appExpr->get_function() );
[f3b0a07]370 assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() );
[64071c2]371 // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
372 // will call all member dtors, and some members may have a user defined dtor.
[22bc276]373 return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr;
[aedfd91]374 }
375
[a465caff]376 namespace {
377 template <typename Predicate>
378 bool allofCtorDtor( Statement * stmt, const Predicate & pred ) {
379 std::list< Expression * > callExprs;
380 collectCtorDtorCalls( stmt, callExprs );
381 // if ( callExprs.empty() ) return false; // xxx - do I still need this check?
382 return std::all_of( callExprs.begin(), callExprs.end(), pred);
383 }
384 }
385
[f9cebb5]386 bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
[a465caff]387 return allofCtorDtor( stmt, []( Expression * callExpr ){
[4d2434a]388 if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
[906e24d]389 FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() );
[4d2434a]390 assert( funcType );
391 return funcType->get_parameters().size() == 1;
392 }
393 return false;
394 });
[64071c2]395 }
[f1b1e4c]396
[a465caff]397 bool isIntrinsicCallStmt( Statement * stmt ) {
398 return allofCtorDtor( stmt, []( Expression * callExpr ) {
399 return isIntrinsicCallExpr( callExpr );
400 });
401 }
402
[64071c2]403 namespace {
404 template<typename CallExpr>
405 Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
[a61ad31]406 if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() );
[64071c2]407 for ( Expression *& arg : callExpr->get_args() ) {
408 if ( pos == 0 ) return arg;
409 pos--;
410 }
411 assert( false );
412 }
413 }
[f1b1e4c]414
[64071c2]415 Expression *& getCallArg( Expression * callExpr, unsigned int pos ) {
416 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
417 return callArg( appExpr, pos );
418 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
419 return callArg( untypedExpr, pos );
[f3b0a07]420 } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) {
421 std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids();
422 assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." );
[e3e16bc]423 ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() );
424 TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() );
[f3b0a07]425 assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." );
426 return getCallArg( tuple->get_exprs().front(), pos );
[62a05d1]427 } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) {
428 return getCallArg( copyCtor->callExpr, pos );
[64071c2]429 } else {
[f3b0a07]430 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() );
[64071c2]431 }
432 }
[f1b1e4c]433
[64071c2]434 namespace {
[599b386]435 std::string funcName( Expression * func );
436
437 template<typename CallExpr>
438 std::string handleDerefName( CallExpr * expr ) {
439 // (*f)(x) => should get name "f"
440 std::string name = getFunctionName( expr );
441 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
[b128d3e]442 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
[599b386]443 return funcName( expr->get_args().front() );
444 }
445
[c738ca4]446 std::string funcName( Expression * func ) {
[64071c2]447 if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
448 return nameExpr->get_name();
449 } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
450 return varExpr->get_var()->get_name();
[c738ca4]451 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
452 return funcName( castExpr->get_arg() );
[ee1635c8]453 } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) {
454 return memberExpr->get_member()->get_name();
[96a10cdd]455 } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) {
[fd782b2]456 return funcName( memberExpr->get_member() );
[599b386]457 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) {
458 return handleDerefName( untypedExpr );
459 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) {
460 return handleDerefName( appExpr );
[19a9822]461 } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) {
462 return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) );
[64071c2]463 } else {
[19a9822]464 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
[64071c2]465 }
466 }
467 }
[70f89d00]468
[64071c2]469 std::string getFunctionName( Expression * expr ) {
[599b386]470 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
471 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
472 // can't possibly do anything reasonable.
[64071c2]473 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
[c738ca4]474 return funcName( appExpr->get_function() );
[64071c2]475 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
[c738ca4]476 return funcName( untypedExpr->get_function() );
[64071c2]477 } else {
[c738ca4]478 std::cerr << expr << std::endl;
[d1969a6]479 assertf( false, "Unexpected expression type passed to getFunctionName" );
[64071c2]480 }
481 }
[10a7775]482
[64071c2]483 Type * getPointerBase( Type * type ) {
484 if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
485 return ptrType->get_base();
486 } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
487 return arrayType->get_base();
[ce8c12f]488 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
489 return refType->get_base();
[64071c2]490 } else {
[22bc276]491 return nullptr;
[64071c2]492 }
493 }
[10a7775]494
[64071c2]495 Type * isPointerType( Type * type ) {
496 if ( getPointerBase( type ) ) return type;
[22bc276]497 else return nullptr;
[64071c2]498 }
[40e636a]499
500 class ConstExprChecker : public Visitor {
501 public:
502 ConstExprChecker() : isConstExpr( true ) {}
503
[65dc863]504 using Visitor::visit;
505
[5809461]506 virtual void visit( ApplicationExpr * ) { isConstExpr = false; }
507 virtual void visit( UntypedExpr * ) { isConstExpr = false; }
508 virtual void visit( NameExpr * ) { isConstExpr = false; }
[1ba88a0]509 // virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
510 virtual void visit( AddressExpr *addressExpr ) {
511 // address of a variable or member expression is constexpr
512 Expression * arg = addressExpr->get_arg();
513 if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false;
514 }
[5809461]515 virtual void visit( UntypedMemberExpr * ) { isConstExpr = false; }
516 virtual void visit( MemberExpr * ) { isConstExpr = false; }
517 virtual void visit( VariableExpr * ) { isConstExpr = false; }
[40e636a]518 // these might be okay?
519 // virtual void visit( SizeofExpr *sizeofExpr );
520 // virtual void visit( AlignofExpr *alignofExpr );
521 // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
522 // virtual void visit( OffsetofExpr *offsetofExpr );
523 // virtual void visit( OffsetPackExpr *offsetPackExpr );
524 // virtual void visit( AttrExpr *attrExpr );
525 // virtual void visit( CommaExpr *commaExpr );
526 // virtual void visit( LogicalExpr *logicalExpr );
527 // virtual void visit( ConditionalExpr *conditionalExpr );
[5809461]528 virtual void visit( TypeExpr * ) { isConstExpr = false; }
529 virtual void visit( AsmExpr * ) { isConstExpr = false; }
530 virtual void visit( UntypedValofExpr * ) { isConstExpr = false; }
531 virtual void visit( CompoundLiteralExpr * ) { isConstExpr = false; }
532 virtual void visit( UntypedTupleExpr * ) { isConstExpr = false; }
533 virtual void visit( TupleExpr * ) { isConstExpr = false; }
534 virtual void visit( TupleAssignExpr * ) { isConstExpr = false; }
[40e636a]535
536 bool isConstExpr;
537 };
538
539 bool isConstExpr( Expression * expr ) {
540 if ( expr ) {
541 ConstExprChecker checker;
542 expr->accept( checker );
543 return checker.isConstExpr;
544 }
545 return true;
546 }
547
548 bool isConstExpr( Initializer * init ) {
549 if ( init ) {
550 ConstExprChecker checker;
551 init->accept( checker );
552 return checker.isConstExpr;
553 } // if
554 // for all intents and purposes, no initializer means const expr
555 return true;
556 }
557
[79970ed]558 bool isConstructor( const std::string & str ) { return str == "?{}"; }
559 bool isDestructor( const std::string & str ) { return str == "^?{}"; }
[ee1635c8]560 bool isAssignment( const std::string & str ) { return str == "?=?"; }
[79970ed]561 bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); }
[ee1635c8]562 bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); }
[4d4882a]563
[ee1635c8]564 FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) {
[4d4882a]565 FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl );
566 if ( ! function ) return 0;
[ee1635c8]567 if ( function->get_name() != fname ) return 0;
[4d4882a]568 FunctionType * ftype = function->get_functionType();
569 if ( ftype->get_parameters().size() != 2 ) return 0;
570
[ce8c12f]571 Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() );
[4d4882a]572 Type * t2 = ftype->get_parameters().back()->get_type();
[ce8c12f]573 assert( t1 );
[4d4882a]574
[ce8c12f]575 if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) {
[4d4882a]576 return function;
577 } else {
[ee1635c8]578 return nullptr;
[4d4882a]579 }
580 }
[ee1635c8]581
[207c7e1d]582 FunctionDecl * isAssignment( Declaration * decl ) {
583 return isCopyFunction( decl, "?=?" );
584 }
585 FunctionDecl * isDestructor( Declaration * decl ) {
586 if ( isDestructor( decl->get_name() ) ) {
587 return dynamic_cast< FunctionDecl * >( decl );
588 }
589 return nullptr;
590 }
591 FunctionDecl * isDefaultConstructor( Declaration * decl ) {
592 if ( isConstructor( decl->get_name() ) ) {
593 if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
594 if ( func->get_functionType()->get_parameters().size() == 1 ) {
595 return func;
596 }
597 }
598 }
599 return nullptr;
600 }
[ee1635c8]601 FunctionDecl * isCopyConstructor( Declaration * decl ) {
602 return isCopyFunction( decl, "?{}" );
603 }
[2b46a13]604}
Note: See TracBrowser for help on using the repository browser.