source: src/InitTweak/InitTweak.cc@ 05807e9

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 05807e9 was 22bc276, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Convert HoistArrayDimension to PassVisitor, cleanup in InitTweak and CodeGen

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