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