source: src/ResolvExpr/Resolver.cc@ 0f35657

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 0f35657 was 8f9cc50, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into tuples

  • Property mode set to 100644
File size: 20.8 KB
RevLine 
[a32b204]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[71f4e4f]7// Resolver.cc --
[a32b204]8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:17:01 2015
[4e06c1e]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 12 17:45:42 2016
13// Update Count : 204
[a32b204]14//
15
[51b73452]16#include "Resolver.h"
17#include "AlternativeFinder.h"
18#include "Alternative.h"
19#include "RenameVars.h"
20#include "ResolveTypeof.h"
[906e24d]21#include "typeops.h"
[51b73452]22#include "SynTree/Statement.h"
23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26#include "SymTab/Indexer.h"
[5f98ce5]27#include "SymTab/Autogen.h"
[d3b7937]28#include "Common/utility.h"
[7b3f66b]29#include "InitTweak/InitTweak.h"
[51b73452]30
[d9a0e76]31#include <iostream>
32using namespace std;
[51b73452]33
[d9a0e76]34namespace ResolvExpr {
[62e5546]35 class Resolver final : public SymTab::Indexer {
[a32b204]36 public:
[77971f6]37 Resolver() : SymTab::Indexer( false ) {}
[71f4e4f]38
[62e5546]39 using SymTab::Indexer::visit;
40 virtual void visit( FunctionDecl *functionDecl ) override;
41 virtual void visit( ObjectDecl *functionDecl ) override;
42 virtual void visit( TypeDecl *typeDecl ) override;
43 virtual void visit( EnumDecl * enumDecl ) override;
[94b4364]44
[62e5546]45 virtual void visit( ArrayType * at ) override;
46 virtual void visit( PointerType * at ) override;
[d9a0e76]47
[62e5546]48 virtual void visit( ExprStmt *exprStmt ) override;
49 virtual void visit( AsmExpr *asmExpr ) override;
50 virtual void visit( AsmStmt *asmStmt ) override;
51 virtual void visit( IfStmt *ifStmt ) override;
52 virtual void visit( WhileStmt *whileStmt ) override;
53 virtual void visit( ForStmt *forStmt ) override;
54 virtual void visit( SwitchStmt *switchStmt ) override;
55 virtual void visit( CaseStmt *caseStmt ) override;
56 virtual void visit( BranchStmt *branchStmt ) override;
57 virtual void visit( ReturnStmt *returnStmt ) override;
[d9a0e76]58
[62e5546]59 virtual void visit( SingleInit *singleInit ) override;
60 virtual void visit( ListInit *listInit ) override;
61 virtual void visit( ConstructorInit *ctorInit ) override;
[a32b204]62 private:
[94b4364]63 typedef std::list< Initializer * >::iterator InitIterator;
64
[40e636a]65 template< typename PtrType >
66 void handlePtrType( PtrType * type );
67
[30b65d8]68 void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
69 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
[f1e012b]70 void fallbackInit( ConstructorInit * ctorInit );
[b726084]71
[77971f6]72 Type * functionReturn = nullptr;
73 Type *initContext = nullptr;
[a436947]74 bool inEnumDecl = false;
[a32b204]75 };
[d9a0e76]76
[a32b204]77 void resolve( std::list< Declaration * > translationUnit ) {
78 Resolver resolver;
79 acceptAll( translationUnit, resolver );
[d9a0e76]80#if 0
[a32b204]81 resolver.print( cerr );
82 for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
83 (*i)->print( std::cerr );
84 (*i)->accept( resolver );
85 } // for
[d9a0e76]86#endif
87 }
88
[a32b204]89 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
90 TypeEnvironment env;
91 return resolveInVoidContext( expr, indexer, env );
[d9a0e76]92 }
[a32b204]93
[db4ecc5]94
[a32b204]95 namespace {
96 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
97 expr->set_env( new TypeSubstitution );
98 env.makeSubstitution( *expr->get_env() );
99 }
[db4ecc5]100 } // namespace
[a32b204]101
[db4ecc5]102 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
103 global_renamer.reset();
104 TypeEnvironment env;
105 Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
106 finishExpr( newExpr, env );
107 return newExpr;
108 }
[71f4e4f]109
[db4ecc5]110 namespace {
[a32b204]111 Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
112 TypeEnvironment env;
113 AlternativeFinder finder( indexer, env );
114 finder.find( untyped );
[d9a0e76]115#if 0
[a32b204]116 if ( finder.get_alternatives().size() != 1 ) {
117 std::cout << "untyped expr is ";
118 untyped->print( std::cout );
119 std::cout << std::endl << "alternatives are:";
120 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
121 i->print( std::cout );
122 } // for
123 } // if
[d9a0e76]124#endif
[a32b204]125 assert( finder.get_alternatives().size() == 1 );
126 Alternative &choice = finder.get_alternatives().front();
127 Expression *newExpr = choice.expr->clone();
128 finishExpr( newExpr, choice.env );
129 return newExpr;
130 }
[d9a0e76]131
[a32b204]132 bool isIntegralType( Type *type ) {
133 if ( dynamic_cast< EnumInstType * >( type ) ) {
134 return true;
135 } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
136 return bt->isInteger();
[89e6ffc]137 } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) {
138 return true;
[a32b204]139 } else {
140 return false;
141 } // if
142 }
[71f4e4f]143
[a32b204]144 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
145 TypeEnvironment env;
146 AlternativeFinder finder( indexer, env );
147 finder.find( untyped );
[d9a0e76]148#if 0
[a32b204]149 if ( finder.get_alternatives().size() != 1 ) {
150 std::cout << "untyped expr is ";
151 untyped->print( std::cout );
152 std::cout << std::endl << "alternatives are:";
153 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
154 i->print( std::cout );
155 } // for
156 } // if
[d9a0e76]157#endif
[a32b204]158 Expression *newExpr = 0;
159 const TypeEnvironment *newEnv = 0;
160 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
[906e24d]161 if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) {
[a32b204]162 if ( newExpr ) {
163 throw SemanticError( "Too many interpretations for case control expression", untyped );
164 } else {
165 newExpr = i->expr->clone();
166 newEnv = &i->env;
167 } // if
168 } // if
169 } // for
170 if ( ! newExpr ) {
171 throw SemanticError( "No interpretations for case control expression", untyped );
172 } // if
173 finishExpr( newExpr, *newEnv );
174 return newExpr;
175 }
[71f4e4f]176
[a32b204]177 }
[71f4e4f]178
[a32b204]179 void Resolver::visit( ObjectDecl *objectDecl ) {
180 Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
181 objectDecl->set_type( new_type );
[3cfe27f]182 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
183 // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
184 // initContext because of a function type can contain object declarations in the return and parameter types. So
185 // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
186 // the RHS.
187 Type *temp = initContext;
[a32b204]188 initContext = new_type;
[a436947]189 if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
190 // enumerator initializers should not use the enum type to initialize, since
191 // the enum type is still incomplete at this point. Use signed int instead.
192 initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
193 }
[a32b204]194 SymTab::Indexer::visit( objectDecl );
[a436947]195 if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
196 // delete newly created signed int type
197 delete initContext;
198 }
[3cfe27f]199 initContext = temp;
[bfbf97f]200 }
201
[40e636a]202 template< typename PtrType >
203 void Resolver::handlePtrType( PtrType * type ) {
204 if ( type->get_dimension() ) {
205 CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
[bfbf97f]206 Expression *newExpr = findSingleExpression( castExpr, *this );
[40e636a]207 delete type->get_dimension();
208 type->set_dimension( newExpr );
[d1d17f5]209 }
[40e636a]210 }
211
212 void Resolver::visit( ArrayType * at ) {
213 handlePtrType( at );
[bfbf97f]214 Visitor::visit( at );
[a32b204]215 }
[94b4364]216
[40e636a]217 void Resolver::visit( PointerType * pt ) {
218 handlePtrType( pt );
219 Visitor::visit( pt );
220 }
221
[a32b204]222 void Resolver::visit( TypeDecl *typeDecl ) {
223 if ( typeDecl->get_base() ) {
224 Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
225 typeDecl->set_base( new_type );
226 } // if
227 SymTab::Indexer::visit( typeDecl );
228 }
[94b4364]229
[a32b204]230 void Resolver::visit( FunctionDecl *functionDecl ) {
[d9a0e76]231#if 0
[a32b204]232 std::cout << "resolver visiting functiondecl ";
233 functionDecl->print( std::cout );
234 std::cout << std::endl;
[d9a0e76]235#endif
[a32b204]236 Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
237 functionDecl->set_type( new_type );
[906e24d]238 ValueGuard< Type * > oldFunctionReturn( functionReturn );
239 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
[a32b204]240 SymTab::Indexer::visit( functionDecl );
241 }
[51b73452]242
[a436947]243 void Resolver::visit( EnumDecl * enumDecl ) {
244 // in case we decide to allow nested enums
245 bool oldInEnumDecl = inEnumDecl;
246 inEnumDecl = true;
247 SymTab::Indexer::visit( enumDecl );
248 inEnumDecl = oldInEnumDecl;
249 }
250
[a32b204]251 void Resolver::visit( ExprStmt *exprStmt ) {
252 if ( exprStmt->get_expr() ) {
253 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
254 delete exprStmt->get_expr();
255 exprStmt->set_expr( newExpr );
256 } // if
257 }
[51b73452]258
[7f5566b]259 void Resolver::visit( AsmExpr *asmExpr ) {
260 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
261 delete asmExpr->get_operand();
262 asmExpr->set_operand( newExpr );
263 if ( asmExpr->get_inout() ) {
264 newExpr = findVoidExpression( asmExpr->get_inout(), *this );
265 delete asmExpr->get_inout();
266 asmExpr->set_inout( newExpr );
267 } // if
268 }
269
270 void Resolver::visit( AsmStmt *asmStmt ) {
271 acceptAll( asmStmt->get_input(), *this);
272 acceptAll( asmStmt->get_output(), *this);
273 }
274
[a32b204]275 void Resolver::visit( IfStmt *ifStmt ) {
276 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
277 delete ifStmt->get_condition();
278 ifStmt->set_condition( newExpr );
279 Visitor::visit( ifStmt );
280 }
[51b73452]281
[a32b204]282 void Resolver::visit( WhileStmt *whileStmt ) {
283 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
284 delete whileStmt->get_condition();
285 whileStmt->set_condition( newExpr );
286 Visitor::visit( whileStmt );
287 }
[51b73452]288
[a32b204]289 void Resolver::visit( ForStmt *forStmt ) {
[145f1fc]290 SymTab::Indexer::visit( forStmt );
291
[a32b204]292 if ( forStmt->get_condition() ) {
[145f1fc]293 Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
[a32b204]294 delete forStmt->get_condition();
295 forStmt->set_condition( newExpr );
296 } // if
[71f4e4f]297
[a32b204]298 if ( forStmt->get_increment() ) {
[145f1fc]299 Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
[a32b204]300 delete forStmt->get_increment();
301 forStmt->set_increment( newExpr );
302 } // if
303 }
[51b73452]304
[a32b204]305 template< typename SwitchClass >
306 void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
307 Expression *newExpr;
308 newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
309 delete switchStmt->get_condition();
310 switchStmt->set_condition( newExpr );
[71f4e4f]311
[a32b204]312 visitor.Visitor::visit( switchStmt );
313 }
[51b73452]314
[a32b204]315 void Resolver::visit( SwitchStmt *switchStmt ) {
316 handleSwitchStmt( switchStmt, *this );
317 }
[51b73452]318
[a32b204]319 void Resolver::visit( CaseStmt *caseStmt ) {
320 Visitor::visit( caseStmt );
321 }
[51b73452]322
[de62360d]323 void Resolver::visit( BranchStmt *branchStmt ) {
324 // must resolve the argument for a computed goto
325 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
[2871210]326 if ( Expression * arg = branchStmt->get_computedTarget() ) {
[de62360d]327 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
328 PointerType pt( Type::Qualifiers(), v.clone() );
329 CastExpr * castExpr = new CastExpr( arg, pt.clone() );
330 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
331 branchStmt->set_target( newExpr );
332 } // if
333 } // if
334 }
335
[a32b204]336 void Resolver::visit( ReturnStmt *returnStmt ) {
337 if ( returnStmt->get_expr() ) {
[906e24d]338 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
[a32b204]339 Expression *newExpr = findSingleExpression( castExpr, *this );
340 delete castExpr;
341 returnStmt->set_expr( newExpr );
342 } // if
343 }
[51b73452]344
[b5c5684]345 template< typename T >
346 bool isCharType( T t ) {
347 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
[71f4e4f]348 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
[b5c5684]349 bt->get_kind() == BasicType::UnsignedChar;
350 }
351 return false;
352 }
353
[a32b204]354 void Resolver::visit( SingleInit *singleInit ) {
355 if ( singleInit->get_value() ) {
[bdd516a]356#if 0
[a32b204]357 if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
358 string n = ne->get_name();
359 if (n == "0") {
[71f4e4f]360 initContext = new BasicType(Type::Qualifiers(),
[a32b204]361 BasicType::SignedInt);
362 } else {
[52f85e0]363 DeclarationWithType * decl = lookupId( n );
[a32b204]364 initContext = decl->get_type();
365 }
[71f4e4f]366 } else if (ConstantExpr * e =
[a32b204]367 dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
368 Constant *c = e->get_constant();
369 initContext = c->get_type();
370 } else {
371 assert(0);
372 }
[bdd516a]373#endif
[a32b204]374 CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
375 Expression *newExpr = findSingleExpression( castExpr, *this );
376 delete castExpr;
377 singleInit->set_value( newExpr );
[b5c5684]378
379 // check if initializing type is char[]
380 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
381 if ( isCharType( at->get_base() ) ) {
382 // check if the resolved type is char *
[906e24d]383 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
[b5c5684]384 if ( isCharType( pt->get_base() ) ) {
[52f85e0]385 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
[b5c5684]386 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
387 singleInit->set_value( ce->get_arg() );
388 ce->set_arg( NULL );
[71f4e4f]389 delete ce;
[b5c5684]390 }
391 }
392 }
393 }
[a32b204]394 } // if
[6c3744e]395// singleInit->get_value()->accept( *this );
[a32b204]396 }
[51b73452]397
[30b65d8]398 template< typename AggrInst >
399 TypeSubstitution makeGenericSubstitutuion( AggrInst * inst ) {
400 std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
401 std::list< Expression * > typeSubs = inst->get_parameters();
402 TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
403 return subs;
404 }
405
406 ReferenceToType * isStructOrUnion( Type * type ) {
407 if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) {
408 return sit;
409 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) {
410 return uit;
411 }
412 return nullptr;
413 }
414
415 void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) {
[94b4364]416 DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
417 assert( dt );
[30b65d8]418 // need to substitute for generic types, so that casts are to concrete types
419 initContext = dt->get_type()->clone();
420 sub.apply( initContext );
421
[94b4364]422 try {
423 if ( init == initEnd ) return; // stop when there are no more initializers
424 (*init)->accept( *this );
425 ++init; // made it past an initializer
426 } catch( SemanticError & ) {
427 // need to delve deeper, if you can
[30b65d8]428 if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
429 resolveAggrInit( type, init, initEnd );
[94b4364]430 } else {
[1869adf]431 // member is not an aggregate type, so can't go any deeper
432
[94b4364]433 // might need to rethink what is being thrown
434 throw;
435 } // if
436 }
437 }
438
[30b65d8]439 void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) {
440
441 if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) {
442 TypeSubstitution sub = makeGenericSubstitutuion( sit );
443 StructDecl * st = sit->get_baseStruct();
[94b4364]444 // want to resolve each initializer to the members of the struct,
445 // but if there are more initializers than members we should stop
446 list< Declaration * >::iterator it = st->get_members().begin();
447 for ( ; it != st->get_members().end(); ++it) {
[30b65d8]448 resolveSingleAggrInit( *it, init, initEnd, sub );
[94b4364]449 }
[30b65d8]450 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) {
451 TypeSubstitution sub = makeGenericSubstitutuion( sit );
452 UnionDecl * un = uit->get_baseUnion();
[94b4364]453 // only resolve to the first member of a union
[30b65d8]454 resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub );
[94b4364]455 } // if
456 }
457
458 void Resolver::visit( ListInit * listInit ) {
[4d2434a]459 InitIterator iter = listInit->begin();
460 InitIterator end = listInit->end();
[94b4364]461
[b5c5684]462 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
[94b4364]463 // resolve each member to the base type of the array
464 for ( ; iter != end; ++iter ) {
[b5c5684]465 initContext = at->get_base();
466 (*iter)->accept( *this );
467 } // for
[65660bd]468 } else if ( TupleType * tt = dynamic_cast< TupleType * > ( initContext ) ) {
469 for ( Type * t : *tt ) {
470 if ( iter == end ) break;
471 initContext = t;
472 (*iter++)->accept( *this );
473 }
[30b65d8]474 } else if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
475 resolveAggrInit( type, iter, end );
[679864e1]476 } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
[242d458]477 Type * base = tt->get_baseType()->get_base();
478 if ( base ) {
479 // know the implementation type, so try using that as the initContext
480 initContext = base;
481 visit( listInit );
482 } else {
483 // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
484 Visitor::visit( listInit );
485 }
[b5c5684]486 } else {
[89e6ffc]487 assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext )
488 || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) );
[b5c5684]489 // basic types are handled here
490 Visitor::visit( listInit );
491 }
492
[bdd516a]493#if 0
[a32b204]494 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
495 std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
496 for ( ; iter != listInit->end_initializers(); ++iter ) {
497 initContext = at->get_base();
498 (*iter)->accept( *this );
499 } // for
500 } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
501 StructDecl *baseStruct = st->get_baseStruct();
502 std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
503 std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
504 for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
505 if ( (*iter2)->get_designators().empty() ) {
506 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
507 initContext = dt->get_type();
508 (*iter2)->accept( *this );
509 ++iter1;
510 } else {
511 StructDecl *st = baseStruct;
512 iter1 = st->get_members().begin();
513 std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
514 for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
515 NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
516 assert( key );
517 for ( ; iter1 != st->get_members().end(); ++iter1 ) {
518 if ( key->get_name() == (*iter1)->get_name() ) {
519 (*iter1)->print( cout );
520 cout << key->get_name() << endl;
521 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
522 assert( fred );
523 StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
524 assert( mary );
525 st = mary->get_baseStruct();
526 iter1 = st->get_members().begin();
527 break;
528 } // if
529 } // for
530 } // for
531 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
532 assert( fred );
533 initContext = fred->get_type();
534 (*listInit->begin_initializers())->accept( *this );
535 } // if
536 } // for
537 } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
538 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
539 initContext = dt->get_type();
540 (*listInit->begin_initializers())->accept( *this );
[2c2242c]541 } // if
[bdd516a]542#endif
[a32b204]543 }
[71f4e4f]544
[f1e012b]545 // ConstructorInit - fall back on C-style initializer
546 void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
547 // could not find valid constructor, or found an intrinsic constructor
548 // fall back on C-style initializer
549 delete ctorInit->get_ctor();
550 ctorInit->set_ctor( NULL );
[71a145de]551 delete ctorInit->get_dtor();
552 ctorInit->set_dtor( NULL );
[f1e012b]553 maybeAccept( ctorInit->get_init(), *this );
554 }
555
[71f4e4f]556 void Resolver::visit( ConstructorInit *ctorInit ) {
[1ba88a0]557 // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
558 maybeAccept( ctorInit->get_ctor(), *this );
559 maybeAccept( ctorInit->get_dtor(), *this );
[071a31a]560
[5b2f5bb]561 // found a constructor - can get rid of C-style initializer
562 delete ctorInit->get_init();
563 ctorInit->set_init( NULL );
[ec79847]564
565 // intrinsic single parameter constructors and destructors do nothing. Since this was
566 // implicitly generated, there's no way for it to have side effects, so get rid of it
567 // to clean up generated code.
[f9cebb5]568 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
[ec79847]569 delete ctorInit->get_ctor();
570 ctorInit->set_ctor( NULL );
571 }
[f9cebb5]572
573 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
[ec79847]574 delete ctorInit->get_dtor();
575 ctorInit->set_dtor( NULL );
576 }
[a465caff]577
578 // xxx - todo -- what about arrays?
579 // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
580 // // can reduce the constructor down to a SingleInit using the
581 // // second argument from the ctor call, since
582 // delete ctorInit->get_ctor();
583 // ctorInit->set_ctor( NULL );
584
585 // Expression * arg =
586 // ctorInit->set_init( new SingleInit( arg ) );
587 // }
[71f4e4f]588 }
[51b73452]589} // namespace ResolvExpr
[a32b204]590
591// Local Variables: //
592// tab-width: 4 //
593// mode: c++ //
594// compile-command: "make install" //
595// End: //
Note: See TracBrowser for help on using the repository browser.