source: src/ResolvExpr/Resolver.cc@ ee897e4b

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 ee897e4b was eeaea53, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Removed undefined behavior when anonymous unions have no member

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