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