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 : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Jul 12 17:45:42 2016 |
---|
13 | // Update Count : 204 |
---|
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 "SymTab/Autogen.h" |
---|
27 | #include "Common/utility.h" |
---|
28 | #include "InitTweak/InitTweak.h" |
---|
29 | |
---|
30 | #include <iostream> |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | namespace ResolvExpr { |
---|
34 | class Resolver : public SymTab::Indexer { |
---|
35 | public: |
---|
36 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
37 | |
---|
38 | virtual void visit( FunctionDecl *functionDecl ); |
---|
39 | virtual void visit( ObjectDecl *functionDecl ); |
---|
40 | virtual void visit( TypeDecl *typeDecl ); |
---|
41 | virtual void visit( EnumDecl * enumDecl ); |
---|
42 | |
---|
43 | virtual void visit( ArrayType * at ); |
---|
44 | virtual void visit( PointerType * at ); |
---|
45 | |
---|
46 | virtual void visit( ExprStmt *exprStmt ); |
---|
47 | virtual void visit( AsmExpr *asmExpr ); |
---|
48 | virtual void visit( AsmStmt *asmStmt ); |
---|
49 | virtual void visit( IfStmt *ifStmt ); |
---|
50 | virtual void visit( WhileStmt *whileStmt ); |
---|
51 | virtual void visit( ForStmt *forStmt ); |
---|
52 | virtual void visit( SwitchStmt *switchStmt ); |
---|
53 | virtual void visit( CaseStmt *caseStmt ); |
---|
54 | virtual void visit( BranchStmt *branchStmt ); |
---|
55 | virtual void visit( ReturnStmt *returnStmt ); |
---|
56 | |
---|
57 | virtual void visit( SingleInit *singleInit ); |
---|
58 | virtual void visit( ListInit *listInit ); |
---|
59 | virtual void visit( ConstructorInit *ctorInit ); |
---|
60 | private: |
---|
61 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
62 | |
---|
63 | template< typename PtrType > |
---|
64 | void handlePtrType( PtrType * type ); |
---|
65 | |
---|
66 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); |
---|
67 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); |
---|
68 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
69 | std::list< Type * > functionReturn; |
---|
70 | Type *initContext; |
---|
71 | Type *switchType; |
---|
72 | bool inEnumDecl = false; |
---|
73 | }; |
---|
74 | |
---|
75 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
76 | Resolver resolver; |
---|
77 | acceptAll( translationUnit, resolver ); |
---|
78 | #if 0 |
---|
79 | resolver.print( cerr ); |
---|
80 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
81 | (*i)->print( std::cerr ); |
---|
82 | (*i)->accept( resolver ); |
---|
83 | } // for |
---|
84 | #endif |
---|
85 | } |
---|
86 | |
---|
87 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
88 | TypeEnvironment env; |
---|
89 | return resolveInVoidContext( expr, indexer, env ); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | namespace { |
---|
94 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
95 | expr->set_env( new TypeSubstitution ); |
---|
96 | env.makeSubstitution( *expr->get_env() ); |
---|
97 | } |
---|
98 | } // namespace |
---|
99 | |
---|
100 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
101 | global_renamer.reset(); |
---|
102 | TypeEnvironment env; |
---|
103 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
104 | finishExpr( newExpr, env ); |
---|
105 | return newExpr; |
---|
106 | } |
---|
107 | |
---|
108 | namespace { |
---|
109 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
110 | TypeEnvironment env; |
---|
111 | AlternativeFinder finder( indexer, env ); |
---|
112 | finder.find( untyped ); |
---|
113 | #if 0 |
---|
114 | if ( finder.get_alternatives().size() != 1 ) { |
---|
115 | std::cout << "untyped expr is "; |
---|
116 | untyped->print( std::cout ); |
---|
117 | std::cout << std::endl << "alternatives are:"; |
---|
118 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
119 | i->print( std::cout ); |
---|
120 | } // for |
---|
121 | } // if |
---|
122 | #endif |
---|
123 | assert( finder.get_alternatives().size() == 1 ); |
---|
124 | Alternative &choice = finder.get_alternatives().front(); |
---|
125 | Expression *newExpr = choice.expr->clone(); |
---|
126 | finishExpr( newExpr, choice.env ); |
---|
127 | return newExpr; |
---|
128 | } |
---|
129 | |
---|
130 | bool isIntegralType( Type *type ) { |
---|
131 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
132 | return true; |
---|
133 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
134 | return bt->isInteger(); |
---|
135 | } else { |
---|
136 | return false; |
---|
137 | } // if |
---|
138 | } |
---|
139 | |
---|
140 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
141 | TypeEnvironment env; |
---|
142 | AlternativeFinder finder( indexer, env ); |
---|
143 | finder.find( untyped ); |
---|
144 | #if 0 |
---|
145 | if ( finder.get_alternatives().size() != 1 ) { |
---|
146 | std::cout << "untyped expr is "; |
---|
147 | untyped->print( std::cout ); |
---|
148 | std::cout << std::endl << "alternatives are:"; |
---|
149 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
150 | i->print( std::cout ); |
---|
151 | } // for |
---|
152 | } // if |
---|
153 | #endif |
---|
154 | Expression *newExpr = 0; |
---|
155 | const TypeEnvironment *newEnv = 0; |
---|
156 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
157 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
158 | if ( newExpr ) { |
---|
159 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
160 | } else { |
---|
161 | newExpr = i->expr->clone(); |
---|
162 | newEnv = &i->env; |
---|
163 | } // if |
---|
164 | } // if |
---|
165 | } // for |
---|
166 | if ( ! newExpr ) { |
---|
167 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
168 | } // if |
---|
169 | finishExpr( newExpr, *newEnv ); |
---|
170 | return newExpr; |
---|
171 | } |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
176 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
177 | objectDecl->set_type( new_type ); |
---|
178 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
179 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
180 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
181 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
182 | // the RHS. |
---|
183 | Type *temp = initContext; |
---|
184 | initContext = new_type; |
---|
185 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
186 | // enumerator initializers should not use the enum type to initialize, since |
---|
187 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
188 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); |
---|
189 | } |
---|
190 | SymTab::Indexer::visit( objectDecl ); |
---|
191 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
192 | // delete newly created signed int type |
---|
193 | delete initContext; |
---|
194 | } |
---|
195 | initContext = temp; |
---|
196 | } |
---|
197 | |
---|
198 | template< typename PtrType > |
---|
199 | void Resolver::handlePtrType( PtrType * type ) { |
---|
200 | if ( type->get_dimension() ) { |
---|
201 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
202 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
203 | delete type->get_dimension(); |
---|
204 | type->set_dimension( newExpr ); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | void Resolver::visit( ArrayType * at ) { |
---|
209 | handlePtrType( at ); |
---|
210 | Visitor::visit( at ); |
---|
211 | } |
---|
212 | |
---|
213 | void Resolver::visit( PointerType * pt ) { |
---|
214 | handlePtrType( pt ); |
---|
215 | Visitor::visit( pt ); |
---|
216 | } |
---|
217 | |
---|
218 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
219 | if ( typeDecl->get_base() ) { |
---|
220 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
221 | typeDecl->set_base( new_type ); |
---|
222 | } // if |
---|
223 | SymTab::Indexer::visit( typeDecl ); |
---|
224 | } |
---|
225 | |
---|
226 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
227 | #if 0 |
---|
228 | std::cout << "resolver visiting functiondecl "; |
---|
229 | functionDecl->print( std::cout ); |
---|
230 | std::cout << std::endl; |
---|
231 | #endif |
---|
232 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
233 | functionDecl->set_type( new_type ); |
---|
234 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
235 | functionReturn.clear(); |
---|
236 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
237 | functionReturn.push_back( (*i)->get_type() ); |
---|
238 | } // for |
---|
239 | SymTab::Indexer::visit( functionDecl ); |
---|
240 | functionReturn = oldFunctionReturn; |
---|
241 | } |
---|
242 | |
---|
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 | |
---|
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 | } |
---|
258 | |
---|
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 | |
---|
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 | } |
---|
281 | |
---|
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 | } |
---|
288 | |
---|
289 | void Resolver::visit( ForStmt *forStmt ) { |
---|
290 | SymTab::Indexer::visit( forStmt ); |
---|
291 | |
---|
292 | if ( forStmt->get_condition() ) { |
---|
293 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
294 | delete forStmt->get_condition(); |
---|
295 | forStmt->set_condition( newExpr ); |
---|
296 | } // if |
---|
297 | |
---|
298 | if ( forStmt->get_increment() ) { |
---|
299 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
300 | delete forStmt->get_increment(); |
---|
301 | forStmt->set_increment( newExpr ); |
---|
302 | } // if |
---|
303 | } |
---|
304 | |
---|
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 ); |
---|
311 | |
---|
312 | visitor.Visitor::visit( switchStmt ); |
---|
313 | } |
---|
314 | |
---|
315 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
316 | handleSwitchStmt( switchStmt, *this ); |
---|
317 | } |
---|
318 | |
---|
319 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
320 | Visitor::visit( caseStmt ); |
---|
321 | } |
---|
322 | |
---|
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 |
---|
326 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
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 | |
---|
336 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
337 | if ( returnStmt->get_expr() ) { |
---|
338 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
339 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
340 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
341 | delete castExpr; |
---|
342 | returnStmt->set_expr( newExpr ); |
---|
343 | } // if |
---|
344 | } |
---|
345 | |
---|
346 | template< typename T > |
---|
347 | bool isCharType( T t ) { |
---|
348 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
349 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
350 | bt->get_kind() == BasicType::UnsignedChar; |
---|
351 | } |
---|
352 | return false; |
---|
353 | } |
---|
354 | |
---|
355 | void Resolver::visit( SingleInit *singleInit ) { |
---|
356 | if ( singleInit->get_value() ) { |
---|
357 | #if 0 |
---|
358 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
359 | string n = ne->get_name(); |
---|
360 | if (n == "0") { |
---|
361 | initContext = new BasicType(Type::Qualifiers(), |
---|
362 | BasicType::SignedInt); |
---|
363 | } else { |
---|
364 | DeclarationWithType * decl = lookupId( n ); |
---|
365 | initContext = decl->get_type(); |
---|
366 | } |
---|
367 | } else if (ConstantExpr * e = |
---|
368 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
369 | Constant *c = e->get_constant(); |
---|
370 | initContext = c->get_type(); |
---|
371 | } else { |
---|
372 | assert(0); |
---|
373 | } |
---|
374 | #endif |
---|
375 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
376 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
377 | delete castExpr; |
---|
378 | singleInit->set_value( newExpr ); |
---|
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 * |
---|
384 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { |
---|
385 | if ( isCharType( pt->get_base() ) ) { |
---|
386 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
387 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
388 | singleInit->set_value( ce->get_arg() ); |
---|
389 | ce->set_arg( NULL ); |
---|
390 | delete ce; |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|
395 | } // if |
---|
396 | // singleInit->get_value()->accept( *this ); |
---|
397 | } |
---|
398 | |
---|
399 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { |
---|
400 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
401 | assert( dt ); |
---|
402 | initContext = dt->get_type(); |
---|
403 | try { |
---|
404 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
405 | (*init)->accept( *this ); |
---|
406 | ++init; // made it past an initializer |
---|
407 | } catch( SemanticError & ) { |
---|
408 | // need to delve deeper, if you can |
---|
409 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { |
---|
410 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); |
---|
411 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { |
---|
412 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); |
---|
413 | } else { |
---|
414 | // member is not an aggregate type, so can't go any deeper |
---|
415 | |
---|
416 | // might need to rethink what is being thrown |
---|
417 | throw; |
---|
418 | } // if |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { |
---|
423 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { |
---|
424 | // want to resolve each initializer to the members of the struct, |
---|
425 | // but if there are more initializers than members we should stop |
---|
426 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
427 | for ( ; it != st->get_members().end(); ++it) { |
---|
428 | resolveSingleAggrInit( *it, init, initEnd ); |
---|
429 | } |
---|
430 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { |
---|
431 | // only resolve to the first member of a union |
---|
432 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); |
---|
433 | } // if |
---|
434 | } |
---|
435 | |
---|
436 | void Resolver::visit( ListInit * listInit ) { |
---|
437 | InitIterator iter = listInit->begin(); |
---|
438 | InitIterator end = listInit->end(); |
---|
439 | |
---|
440 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
441 | // resolve each member to the base type of the array |
---|
442 | for ( ; iter != end; ++iter ) { |
---|
443 | initContext = at->get_base(); |
---|
444 | (*iter)->accept( *this ); |
---|
445 | } // for |
---|
446 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { |
---|
447 | resolveAggrInit( st->get_baseStruct(), iter, end ); |
---|
448 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { |
---|
449 | resolveAggrInit( st->get_baseUnion(), iter, end ); |
---|
450 | } else { |
---|
451 | // basic types are handled here |
---|
452 | Visitor::visit( listInit ); |
---|
453 | } |
---|
454 | |
---|
455 | #if 0 |
---|
456 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
457 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
458 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
459 | initContext = at->get_base(); |
---|
460 | (*iter)->accept( *this ); |
---|
461 | } // for |
---|
462 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
463 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
464 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
465 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
466 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
467 | if ( (*iter2)->get_designators().empty() ) { |
---|
468 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
469 | initContext = dt->get_type(); |
---|
470 | (*iter2)->accept( *this ); |
---|
471 | ++iter1; |
---|
472 | } else { |
---|
473 | StructDecl *st = baseStruct; |
---|
474 | iter1 = st->get_members().begin(); |
---|
475 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
476 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
477 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
478 | assert( key ); |
---|
479 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
480 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
481 | (*iter1)->print( cout ); |
---|
482 | cout << key->get_name() << endl; |
---|
483 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
484 | assert( fred ); |
---|
485 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
486 | assert( mary ); |
---|
487 | st = mary->get_baseStruct(); |
---|
488 | iter1 = st->get_members().begin(); |
---|
489 | break; |
---|
490 | } // if |
---|
491 | } // for |
---|
492 | } // for |
---|
493 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
494 | assert( fred ); |
---|
495 | initContext = fred->get_type(); |
---|
496 | (*listInit->begin_initializers())->accept( *this ); |
---|
497 | } // if |
---|
498 | } // for |
---|
499 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
500 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
501 | initContext = dt->get_type(); |
---|
502 | (*listInit->begin_initializers())->accept( *this ); |
---|
503 | } // if |
---|
504 | #endif |
---|
505 | } |
---|
506 | |
---|
507 | // ConstructorInit - fall back on C-style initializer |
---|
508 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
509 | // could not find valid constructor, or found an intrinsic constructor |
---|
510 | // fall back on C-style initializer |
---|
511 | delete ctorInit->get_ctor(); |
---|
512 | ctorInit->set_ctor( NULL ); |
---|
513 | delete ctorInit->get_dtor(); |
---|
514 | ctorInit->set_dtor( NULL ); |
---|
515 | maybeAccept( ctorInit->get_init(), *this ); |
---|
516 | } |
---|
517 | |
---|
518 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
519 | try { |
---|
520 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
521 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
522 | } catch ( SemanticError ) { |
---|
523 | // no alternatives for the constructor initializer - fallback on C-style initializer |
---|
524 | // xxx - not sure if this makes a ton of sense - should maybe never be able to have this situation? |
---|
525 | fallbackInit( ctorInit ); |
---|
526 | return; |
---|
527 | } |
---|
528 | |
---|
529 | // found a constructor - can get rid of C-style initializer |
---|
530 | delete ctorInit->get_init(); |
---|
531 | ctorInit->set_init( NULL ); |
---|
532 | |
---|
533 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
534 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
535 | // to clean up generated code. |
---|
536 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
537 | delete ctorInit->get_ctor(); |
---|
538 | ctorInit->set_ctor( NULL ); |
---|
539 | } |
---|
540 | |
---|
541 | // xxx - todo |
---|
542 | // if ( InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
543 | // // can reduce the constructor down to a SingleInit using the |
---|
544 | // // second argument from the ctor call |
---|
545 | // } |
---|
546 | |
---|
547 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
548 | delete ctorInit->get_dtor(); |
---|
549 | ctorInit->set_dtor( NULL ); |
---|
550 | } |
---|
551 | } |
---|
552 | } // namespace ResolvExpr |
---|
553 | |
---|
554 | // Local Variables: // |
---|
555 | // tab-width: 4 // |
---|
556 | // mode: c++ // |
---|
557 | // compile-command: "make install" // |
---|
558 | // End: // |
---|