source: src/ResolvExpr/Resolver.cc@ 30b65d8

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 30b65d8 was 30b65d8, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

always construct polymorphic types, substitute generic type parameters when resolving initializers

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