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