source: src/InitTweak/InitTweak.cc@ 96fc67b

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 96fc67b was 1a5ad8c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update autogen to generate reference rebind for reference member copy constructors

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