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
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
25class UntypedValofExpr;
26
27namespace InitTweak {
28 namespace {
29 class HasDesignations : public Visitor {
30 public:
31 bool hasDesignations = false;
32 virtual void visit( Designation * des ) {
33 if ( ! des->get_designators().empty() ) hasDesignations = true;
34 else Visitor::visit( des );
35 }
36 };
37
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
59 class InitFlattener : public Visitor {
60 public:
61 virtual void visit( SingleInit * singleInit );
62 virtual void visit( ListInit * listInit );
63 std::list< Expression * > argList;
64 };
65
66 void InitFlattener::visit( SingleInit * singleInit ) {
67 argList.push_back( singleInit->get_value()->clone() );
68 }
69
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 ) {
74 (*it)->accept( *this );
75 }
76 }
77 }
78
79 std::list< Expression * > makeInitList( Initializer * init ) {
80 InitFlattener flattener;
81 maybeAccept( init, flattener );
82 return flattener.argList;
83 }
84
85 bool isDesignated( Initializer * init ) {
86 HasDesignations finder;
87 maybeAccept( init, finder );
88 return finder.hasDesignations;
89 }
90
91 bool checkInitDepth( ObjectDecl * objDecl ) {
92 InitDepthChecker checker( objDecl->get_type() );
93 maybeAccept( objDecl->get_init(), checker );
94 return checker.depthOkay;
95 }
96
97 class InitExpander::ExpanderImpl {
98 public:
99 virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0;
100 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0;
101 };
102
103 class InitImpl : public InitExpander::ExpanderImpl {
104 public:
105 InitImpl( Initializer * init ) : init( init ) {}
106
107 virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) {
108 // this is wrong, but just a placeholder for now
109 // if ( ! flattened ) flatten( indices );
110 // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >();
111 return makeInitList( init );
112 }
113
114 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
115 private:
116 Initializer * init;
117 };
118
119 class ExprImpl : public InitExpander::ExpanderImpl {
120 public:
121 ExprImpl( Expression * expr ) : arg( expr ) {}
122
123 ~ExprImpl() { delete arg; }
124
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 }
141
142 virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices );
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
166 void InitExpander::clearArrayIndices() {
167 deleteAll( indices );
168 indices.clear();
169 }
170
171 namespace {
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
176 template< typename OutIterator >
177 void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) {
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
185 *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), nullptr );
186
187 UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) );
188 increment->get_args().push_back( index->clone() );
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
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
201 if ( idx == idxEnd ) {
202 if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) {
203 for ( Initializer * init : *listInit ) {
204 buildCallExpr( callExpr->clone(), index, dimension, init, out );
205 }
206 } else {
207 buildCallExpr( callExpr->clone(), index, dimension, init, out );
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 }
219
220 static UniqueName targetLabel( "L__autogen__" );
221 Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } );
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 ) );
234 stmts.push_back( new BranchStmt( noLabels, switchLabel, BranchStmt::Break ) );
235 CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts );
236 branches.push_back( caseStmt );
237 }
238 *out++ = new SwitchStmt( noLabels, index->clone(), branches );
239 *out++ = new NullStmt( std::list<Label>{ switchLabel } );
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 ) {
251 if ( ! init ) return nullptr;
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;
256 return nullptr;
257 } else {
258 init = nullptr; // init was consumed in creating the list init
259 return block;
260 }
261 }
262
263 Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) {
264 return nullptr;
265 }
266
267 Statement * InitExpander::buildListInit( UntypedExpr * dst ) {
268 return expander->buildListInit( dst, indices );
269 }
270
271 bool tryConstruct( DeclarationWithType * dwt ) {
272 ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
273 if ( ! objDecl ) return false;
274 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
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 );
279 }
280
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 );
304 }
305 }
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 );
312 }
313
314 Expression * getCtorDtorCall( Statement * stmt ) {
315 std::list< Expression * > matches;
316 collectCtorDtorCalls( stmt, matches );
317 assert( matches.size() <= 1 );
318 return matches.size() == 1 ? matches.front() : nullptr;
319 }
320
321 namespace {
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() );
329 assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" );
330 return getCalledFunction( expr->get_args().front() );
331 }
332
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() );
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 );
345 } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
346 return getCalledFunction( addrExpr->get_arg() );
347 }
348 return nullptr;
349 }
350 }
351
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
361 ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
362 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
363 if ( ! appExpr ) return nullptr;
364 DeclarationWithType * function = getCalledFunction( appExpr->get_function() );
365 assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() );
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.
368 return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr;
369 }
370
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
381 bool isIntrinsicSingleArgCallStmt( Statement * stmt ) {
382 return allofCtorDtor( stmt, []( Expression * callExpr ){
383 if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
384 FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_result() );
385 assert( funcType );
386 return funcType->get_parameters().size() == 1;
387 }
388 return false;
389 });
390 }
391
392 bool isIntrinsicCallStmt( Statement * stmt ) {
393 return allofCtorDtor( stmt, []( Expression * callExpr ) {
394 return isIntrinsicCallExpr( callExpr );
395 });
396 }
397
398 namespace {
399 template<typename CallExpr>
400 Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
401 if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() );
402 for ( Expression *& arg : callExpr->get_args() ) {
403 if ( pos == 0 ) return arg;
404 pos--;
405 }
406 assert( false );
407 }
408 }
409
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 );
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." );
418 ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() );
419 TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() );
420 assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." );
421 return getCallArg( tuple->get_exprs().front(), pos );
422 } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) {
423 return getCallArg( copyCtor->callExpr, pos );
424 } else {
425 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() );
426 }
427 }
428
429 namespace {
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() );
437 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
438 return funcName( expr->get_args().front() );
439 }
440
441 std::string funcName( Expression * func ) {
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();
446 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
447 return funcName( castExpr->get_arg() );
448 } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) {
449 return memberExpr->get_member()->get_name();
450 } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) {
451 return funcName( memberExpr->get_member() );
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 );
456 } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) {
457 return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) );
458 } else {
459 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
460 }
461 }
462 }
463
464 std::string getFunctionName( Expression * expr ) {
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.
468 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
469 return funcName( appExpr->get_function() );
470 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
471 return funcName( untypedExpr->get_function() );
472 } else {
473 std::cerr << expr << std::endl;
474 assertf( false, "Unexpected expression type passed to getFunctionName" );
475 }
476 }
477
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();
483 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
484 return refType->get_base();
485 } else {
486 return nullptr;
487 }
488 }
489
490 Type * isPointerType( Type * type ) {
491 if ( getPointerBase( type ) ) return type;
492 else return nullptr;
493 }
494
495 class ConstExprChecker : public Visitor {
496 public:
497 ConstExprChecker() : isConstExpr( true ) {}
498
499 using Visitor::visit;
500
501 virtual void visit( ApplicationExpr * ) { isConstExpr = false; }
502 virtual void visit( UntypedExpr * ) { isConstExpr = false; }
503 virtual void visit( NameExpr * ) { isConstExpr = false; }
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 }
510 virtual void visit( UntypedMemberExpr * ) { isConstExpr = false; }
511 virtual void visit( MemberExpr * ) { isConstExpr = false; }
512 virtual void visit( VariableExpr * ) { isConstExpr = false; }
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 );
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; }
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
553 bool isConstructor( const std::string & str ) { return str == "?{}"; }
554 bool isDestructor( const std::string & str ) { return str == "^?{}"; }
555 bool isAssignment( const std::string & str ) { return str == "?=?"; }
556 bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); }
557 bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); }
558
559 FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) {
560 FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl );
561 if ( ! function ) return 0;
562 if ( function->get_name() != fname ) return 0;
563 FunctionType * ftype = function->get_functionType();
564 if ( ftype->get_parameters().size() != 2 ) return 0;
565
566 Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() );
567 Type * t2 = ftype->get_parameters().back()->get_type();
568 assert( t1 );
569
570 if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) {
571 return function;
572 } else {
573 return nullptr;
574 }
575 }
576
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 }
596 FunctionDecl * isCopyConstructor( Declaration * decl ) {
597 return isCopyFunction( decl, "?{}" );
598 }
599}
Note: See TracBrowser for help on using the repository browser.