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