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 | // |
---|
7 | // Resolver.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 12:17:01 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Wed Jan 13 16:23:51 2016 |
---|
13 | // Update Count : 186 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Resolver.h" |
---|
17 | #include "AlternativeFinder.h" |
---|
18 | #include "Alternative.h" |
---|
19 | #include "RenameVars.h" |
---|
20 | #include "ResolveTypeof.h" |
---|
21 | #include "SynTree/Statement.h" |
---|
22 | #include "SynTree/Type.h" |
---|
23 | #include "SynTree/Expression.h" |
---|
24 | #include "SynTree/Initializer.h" |
---|
25 | #include "SymTab/Indexer.h" |
---|
26 | #include "utility.h" |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | namespace ResolvExpr { |
---|
32 | class Resolver : public SymTab::Indexer { |
---|
33 | public: |
---|
34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
35 | |
---|
36 | virtual void visit( FunctionDecl *functionDecl ); |
---|
37 | virtual void visit( ObjectDecl *functionDecl ); |
---|
38 | virtual void visit( TypeDecl *typeDecl ); |
---|
39 | |
---|
40 | virtual void visit( ArrayType * at ); |
---|
41 | |
---|
42 | virtual void visit( ExprStmt *exprStmt ); |
---|
43 | virtual void visit( AsmExpr *asmExpr ); |
---|
44 | virtual void visit( AsmStmt *asmStmt ); |
---|
45 | virtual void visit( IfStmt *ifStmt ); |
---|
46 | virtual void visit( WhileStmt *whileStmt ); |
---|
47 | virtual void visit( ForStmt *forStmt ); |
---|
48 | virtual void visit( SwitchStmt *switchStmt ); |
---|
49 | virtual void visit( ChooseStmt *switchStmt ); |
---|
50 | virtual void visit( CaseStmt *caseStmt ); |
---|
51 | virtual void visit( BranchStmt *branchStmt ); |
---|
52 | virtual void visit( ReturnStmt *returnStmt ); |
---|
53 | |
---|
54 | virtual void visit( SingleInit *singleInit ); |
---|
55 | virtual void visit( ListInit *listInit ); |
---|
56 | virtual void visit( ConstructorInit *ctorInit ); |
---|
57 | private: |
---|
58 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
59 | |
---|
60 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); |
---|
61 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); |
---|
62 | |
---|
63 | std::list< Type * > functionReturn; |
---|
64 | Type *initContext; |
---|
65 | Type *switchType; |
---|
66 | }; |
---|
67 | |
---|
68 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
69 | Resolver resolver; |
---|
70 | acceptAll( translationUnit, resolver ); |
---|
71 | #if 0 |
---|
72 | resolver.print( cerr ); |
---|
73 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
74 | (*i)->print( std::cerr ); |
---|
75 | (*i)->accept( resolver ); |
---|
76 | } // for |
---|
77 | #endif |
---|
78 | } |
---|
79 | |
---|
80 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
81 | TypeEnvironment env; |
---|
82 | return resolveInVoidContext( expr, indexer, env ); |
---|
83 | } |
---|
84 | |
---|
85 | namespace { |
---|
86 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
87 | expr->set_env( new TypeSubstitution ); |
---|
88 | env.makeSubstitution( *expr->get_env() ); |
---|
89 | } |
---|
90 | |
---|
91 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
92 | global_renamer.reset(); |
---|
93 | TypeEnvironment env; |
---|
94 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
95 | finishExpr( newExpr, env ); |
---|
96 | return newExpr; |
---|
97 | } |
---|
98 | |
---|
99 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
100 | TypeEnvironment env; |
---|
101 | AlternativeFinder finder( indexer, env ); |
---|
102 | finder.find( untyped ); |
---|
103 | #if 0 |
---|
104 | if ( finder.get_alternatives().size() != 1 ) { |
---|
105 | std::cout << "untyped expr is "; |
---|
106 | untyped->print( std::cout ); |
---|
107 | std::cout << std::endl << "alternatives are:"; |
---|
108 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
109 | i->print( std::cout ); |
---|
110 | } // for |
---|
111 | } // if |
---|
112 | #endif |
---|
113 | assert( finder.get_alternatives().size() == 1 ); |
---|
114 | Alternative &choice = finder.get_alternatives().front(); |
---|
115 | Expression *newExpr = choice.expr->clone(); |
---|
116 | finishExpr( newExpr, choice.env ); |
---|
117 | return newExpr; |
---|
118 | } |
---|
119 | |
---|
120 | bool isIntegralType( Type *type ) { |
---|
121 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
122 | return true; |
---|
123 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
124 | return bt->isInteger(); |
---|
125 | } else { |
---|
126 | return false; |
---|
127 | } // if |
---|
128 | } |
---|
129 | |
---|
130 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
131 | TypeEnvironment env; |
---|
132 | AlternativeFinder finder( indexer, env ); |
---|
133 | finder.find( untyped ); |
---|
134 | #if 0 |
---|
135 | if ( finder.get_alternatives().size() != 1 ) { |
---|
136 | std::cout << "untyped expr is "; |
---|
137 | untyped->print( std::cout ); |
---|
138 | std::cout << std::endl << "alternatives are:"; |
---|
139 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
140 | i->print( std::cout ); |
---|
141 | } // for |
---|
142 | } // if |
---|
143 | #endif |
---|
144 | Expression *newExpr = 0; |
---|
145 | const TypeEnvironment *newEnv = 0; |
---|
146 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
147 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
148 | if ( newExpr ) { |
---|
149 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
150 | } else { |
---|
151 | newExpr = i->expr->clone(); |
---|
152 | newEnv = &i->env; |
---|
153 | } // if |
---|
154 | } // if |
---|
155 | } // for |
---|
156 | if ( ! newExpr ) { |
---|
157 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
158 | } // if |
---|
159 | finishExpr( newExpr, *newEnv ); |
---|
160 | return newExpr; |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
166 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
167 | objectDecl->set_type( new_type ); |
---|
168 | initContext = new_type; |
---|
169 | SymTab::Indexer::visit( objectDecl ); |
---|
170 | } |
---|
171 | |
---|
172 | void Resolver::visit( ArrayType * at ) { |
---|
173 | if ( at->get_dimension() ) { |
---|
174 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
175 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); |
---|
176 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
177 | delete at->get_dimension(); |
---|
178 | at->set_dimension( newExpr ); |
---|
179 | } |
---|
180 | Visitor::visit( at ); |
---|
181 | } |
---|
182 | |
---|
183 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
184 | if ( typeDecl->get_base() ) { |
---|
185 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
186 | typeDecl->set_base( new_type ); |
---|
187 | } // if |
---|
188 | SymTab::Indexer::visit( typeDecl ); |
---|
189 | } |
---|
190 | |
---|
191 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
192 | #if 0 |
---|
193 | std::cout << "resolver visiting functiondecl "; |
---|
194 | functionDecl->print( std::cout ); |
---|
195 | std::cout << std::endl; |
---|
196 | #endif |
---|
197 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
198 | functionDecl->set_type( new_type ); |
---|
199 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
200 | functionReturn.clear(); |
---|
201 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
202 | functionReturn.push_back( (*i)->get_type() ); |
---|
203 | } // for |
---|
204 | SymTab::Indexer::visit( functionDecl ); |
---|
205 | functionReturn = oldFunctionReturn; |
---|
206 | } |
---|
207 | |
---|
208 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
209 | if ( exprStmt->get_expr() ) { |
---|
210 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
211 | delete exprStmt->get_expr(); |
---|
212 | exprStmt->set_expr( newExpr ); |
---|
213 | } // if |
---|
214 | } |
---|
215 | |
---|
216 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
217 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
218 | delete asmExpr->get_operand(); |
---|
219 | asmExpr->set_operand( newExpr ); |
---|
220 | if ( asmExpr->get_inout() ) { |
---|
221 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
222 | delete asmExpr->get_inout(); |
---|
223 | asmExpr->set_inout( newExpr ); |
---|
224 | } // if |
---|
225 | } |
---|
226 | |
---|
227 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
228 | acceptAll( asmStmt->get_input(), *this); |
---|
229 | acceptAll( asmStmt->get_output(), *this); |
---|
230 | } |
---|
231 | |
---|
232 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
233 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
234 | delete ifStmt->get_condition(); |
---|
235 | ifStmt->set_condition( newExpr ); |
---|
236 | Visitor::visit( ifStmt ); |
---|
237 | } |
---|
238 | |
---|
239 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
240 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
241 | delete whileStmt->get_condition(); |
---|
242 | whileStmt->set_condition( newExpr ); |
---|
243 | Visitor::visit( whileStmt ); |
---|
244 | } |
---|
245 | |
---|
246 | void Resolver::visit( ForStmt *forStmt ) { |
---|
247 | SymTab::Indexer::visit( forStmt ); |
---|
248 | |
---|
249 | if ( forStmt->get_condition() ) { |
---|
250 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
251 | delete forStmt->get_condition(); |
---|
252 | forStmt->set_condition( newExpr ); |
---|
253 | } // if |
---|
254 | |
---|
255 | if ( forStmt->get_increment() ) { |
---|
256 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
257 | delete forStmt->get_increment(); |
---|
258 | forStmt->set_increment( newExpr ); |
---|
259 | } // if |
---|
260 | } |
---|
261 | |
---|
262 | template< typename SwitchClass > |
---|
263 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
264 | Expression *newExpr; |
---|
265 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
266 | delete switchStmt->get_condition(); |
---|
267 | switchStmt->set_condition( newExpr ); |
---|
268 | |
---|
269 | visitor.Visitor::visit( switchStmt ); |
---|
270 | } |
---|
271 | |
---|
272 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
273 | handleSwitchStmt( switchStmt, *this ); |
---|
274 | } |
---|
275 | |
---|
276 | void Resolver::visit( ChooseStmt *switchStmt ) { |
---|
277 | handleSwitchStmt( switchStmt, *this ); |
---|
278 | } |
---|
279 | |
---|
280 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
281 | Visitor::visit( caseStmt ); |
---|
282 | } |
---|
283 | |
---|
284 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
285 | // must resolve the argument for a computed goto |
---|
286 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
287 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
288 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
289 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
290 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
291 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
292 | branchStmt->set_target( newExpr ); |
---|
293 | } // if |
---|
294 | } // if |
---|
295 | } |
---|
296 | |
---|
297 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
298 | if ( returnStmt->get_expr() ) { |
---|
299 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
300 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
301 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
302 | delete castExpr; |
---|
303 | returnStmt->set_expr( newExpr ); |
---|
304 | } // if |
---|
305 | } |
---|
306 | |
---|
307 | template< typename T > |
---|
308 | bool isCharType( T t ) { |
---|
309 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
310 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
311 | bt->get_kind() == BasicType::UnsignedChar; |
---|
312 | } |
---|
313 | return false; |
---|
314 | } |
---|
315 | |
---|
316 | void Resolver::visit( SingleInit *singleInit ) { |
---|
317 | if ( singleInit->get_value() ) { |
---|
318 | #if 0 |
---|
319 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
320 | string n = ne->get_name(); |
---|
321 | if (n == "0") { |
---|
322 | initContext = new BasicType(Type::Qualifiers(), |
---|
323 | BasicType::SignedInt); |
---|
324 | } else { |
---|
325 | DeclarationWithType * decl = lookupId(n); |
---|
326 | initContext = decl->get_type(); |
---|
327 | } |
---|
328 | } else if (ConstantExpr * e = |
---|
329 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
330 | Constant *c = e->get_constant(); |
---|
331 | initContext = c->get_type(); |
---|
332 | } else { |
---|
333 | assert(0); |
---|
334 | } |
---|
335 | #endif |
---|
336 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
337 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
338 | delete castExpr; |
---|
339 | singleInit->set_value( newExpr ); |
---|
340 | |
---|
341 | // check if initializing type is char[] |
---|
342 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
343 | if ( isCharType( at->get_base() ) ) { |
---|
344 | // check if the resolved type is char * |
---|
345 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { |
---|
346 | if ( isCharType( pt->get_base() ) ) { |
---|
347 | // strip cast if we're initializing a char[] with a char *, e.g. |
---|
348 | // char x[] = "hello"; |
---|
349 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
350 | singleInit->set_value( ce->get_arg() ); |
---|
351 | ce->set_arg( NULL ); |
---|
352 | delete ce; |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | } |
---|
357 | } // if |
---|
358 | // singleInit->get_value()->accept( *this ); |
---|
359 | } |
---|
360 | |
---|
361 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { |
---|
362 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
363 | assert( dt ); |
---|
364 | initContext = dt->get_type(); |
---|
365 | try { |
---|
366 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
367 | (*init)->accept( *this ); |
---|
368 | ++init; // made it past an initializer |
---|
369 | } catch( SemanticError & ) { |
---|
370 | // need to delve deeper, if you can |
---|
371 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { |
---|
372 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); |
---|
373 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { |
---|
374 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); |
---|
375 | } else { |
---|
376 | // member is not an aggregate type, so can't go any deeper |
---|
377 | |
---|
378 | // might need to rethink what is being thrown |
---|
379 | throw; |
---|
380 | } // if |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { |
---|
385 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { |
---|
386 | // want to resolve each initializer to the members of the struct, |
---|
387 | // but if there are more initializers than members we should stop |
---|
388 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
389 | for ( ; it != st->get_members().end(); ++it) { |
---|
390 | resolveSingleAggrInit( *it, init, initEnd ); |
---|
391 | } |
---|
392 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { |
---|
393 | // only resolve to the first member of a union |
---|
394 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); |
---|
395 | } // if |
---|
396 | } |
---|
397 | |
---|
398 | void Resolver::visit( ListInit * listInit ) { |
---|
399 | InitIterator iter = listInit->begin_initializers(); |
---|
400 | InitIterator end = listInit->end_initializers(); |
---|
401 | |
---|
402 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
403 | // resolve each member to the base type of the array |
---|
404 | for ( ; iter != end; ++iter ) { |
---|
405 | initContext = at->get_base(); |
---|
406 | (*iter)->accept( *this ); |
---|
407 | } // for |
---|
408 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { |
---|
409 | resolveAggrInit( st->get_baseStruct(), iter, end ); |
---|
410 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { |
---|
411 | resolveAggrInit( st->get_baseUnion(), iter, end ); |
---|
412 | } else { |
---|
413 | // basic types are handled here |
---|
414 | Visitor::visit( listInit ); |
---|
415 | } |
---|
416 | |
---|
417 | #if 0 |
---|
418 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
419 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
420 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
421 | initContext = at->get_base(); |
---|
422 | (*iter)->accept( *this ); |
---|
423 | } // for |
---|
424 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
425 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
426 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
427 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
428 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
429 | if ( (*iter2)->get_designators().empty() ) { |
---|
430 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
431 | initContext = dt->get_type(); |
---|
432 | (*iter2)->accept( *this ); |
---|
433 | ++iter1; |
---|
434 | } else { |
---|
435 | StructDecl *st = baseStruct; |
---|
436 | iter1 = st->get_members().begin(); |
---|
437 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
438 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
439 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
440 | assert( key ); |
---|
441 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
442 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
443 | (*iter1)->print( cout ); |
---|
444 | cout << key->get_name() << endl; |
---|
445 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
446 | assert( fred ); |
---|
447 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
448 | assert( mary ); |
---|
449 | st = mary->get_baseStruct(); |
---|
450 | iter1 = st->get_members().begin(); |
---|
451 | break; |
---|
452 | } // if |
---|
453 | } // for |
---|
454 | } // for |
---|
455 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
456 | assert( fred ); |
---|
457 | initContext = fred->get_type(); |
---|
458 | (*listInit->begin_initializers())->accept( *this ); |
---|
459 | } // if |
---|
460 | } // for |
---|
461 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
462 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
463 | initContext = dt->get_type(); |
---|
464 | (*listInit->begin_initializers())->accept( *this ); |
---|
465 | } // if |
---|
466 | #endif |
---|
467 | } |
---|
468 | |
---|
469 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
470 | TypeEnvironment env; |
---|
471 | AlternativeFinder finder( *this, env ); |
---|
472 | finder.find( ctorInit->get_ctor() ); |
---|
473 | if ( finder.get_alternatives().size() == 0 ) { |
---|
474 | // could not find valid constructor |
---|
475 | delete ctorInit->get_ctor(); |
---|
476 | ctorInit->set_ctor( NULL ); |
---|
477 | |
---|
478 | maybeAccept( ctorInit->get_init(), *this ); |
---|
479 | } else if ( finder.get_alternatives().size() == 1 ) { |
---|
480 | // found a constructor - can get rid of C-style initializer |
---|
481 | Alternative &choice = finder.get_alternatives().front(); |
---|
482 | Expression *newExpr = choice.expr->clone(); |
---|
483 | finishExpr( newExpr, choice.env ); |
---|
484 | ctorInit->set_ctor( newExpr ); |
---|
485 | delete ctorInit->get_init(); |
---|
486 | ctorInit->set_init( NULL ); |
---|
487 | } else { |
---|
488 | // too many constructors found |
---|
489 | assert(false); |
---|
490 | } |
---|
491 | } |
---|
492 | } // namespace ResolvExpr |
---|
493 | |
---|
494 | // Local Variables: // |
---|
495 | // tab-width: 4 // |
---|
496 | // mode: c++ // |
---|
497 | // compile-command: "make install" // |
---|
498 | // End: // |
---|