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 : Andrew Beach |
---|
12 | // Last Modified On : Tus Aug 8 16:06:00 2017 |
---|
13 | // Update Count : 212 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stddef.h> // for NULL |
---|
17 | #include <cassert> // for strict_dynamic_cast, assert |
---|
18 | #include <memory> // for allocator, allocator_traits<... |
---|
19 | #include <tuple> // for get |
---|
20 | |
---|
21 | #include "Alternative.h" // for Alternative, AltList |
---|
22 | #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... |
---|
23 | #include "Common/SemanticError.h" // for SemanticError |
---|
24 | #include "Common/utility.h" // for ValueGuard, group_iterate |
---|
25 | #include "CurrentObject.h" // for CurrentObject |
---|
26 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt |
---|
27 | #include "RenameVars.h" // for RenameVars, global_renamer |
---|
28 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
29 | #include "ResolveTypeof.h" // for resolveTypeof |
---|
30 | #include "Resolver.h" |
---|
31 | #include "SymTab/Autogen.h" // for SizeType |
---|
32 | #include "SymTab/Indexer.h" // for Indexer |
---|
33 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar... |
---|
34 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr |
---|
35 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
36 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt |
---|
37 | #include "SynTree/Type.h" // for Type, BasicType, PointerType |
---|
38 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
39 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
40 | #include "typeops.h" // for extractResultType |
---|
41 | |
---|
42 | using namespace std; |
---|
43 | |
---|
44 | namespace ResolvExpr { |
---|
45 | class Resolver final : public SymTab::Indexer { |
---|
46 | public: |
---|
47 | Resolver() : SymTab::Indexer( false ) {} |
---|
48 | Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) { |
---|
49 | if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) { |
---|
50 | functionReturn = res->functionReturn; |
---|
51 | currentObject = res->currentObject; |
---|
52 | inEnumDecl = res->inEnumDecl; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | typedef SymTab::Indexer Parent; |
---|
57 | using Parent::visit; |
---|
58 | virtual void visit( FunctionDecl *functionDecl ) override; |
---|
59 | virtual void visit( ObjectDecl *functionDecl ) override; |
---|
60 | virtual void visit( TypeDecl *typeDecl ) override; |
---|
61 | virtual void visit( EnumDecl * enumDecl ) override; |
---|
62 | |
---|
63 | virtual void visit( ArrayType * at ) override; |
---|
64 | virtual void visit( PointerType * at ) override; |
---|
65 | |
---|
66 | virtual void visit( ExprStmt *exprStmt ) override; |
---|
67 | virtual void visit( AsmExpr *asmExpr ) override; |
---|
68 | virtual void visit( AsmStmt *asmStmt ) override; |
---|
69 | virtual void visit( IfStmt *ifStmt ) override; |
---|
70 | virtual void visit( WhileStmt *whileStmt ) override; |
---|
71 | virtual void visit( ForStmt *forStmt ) override; |
---|
72 | virtual void visit( SwitchStmt *switchStmt ) override; |
---|
73 | virtual void visit( CaseStmt *caseStmt ) override; |
---|
74 | virtual void visit( BranchStmt *branchStmt ) override; |
---|
75 | virtual void visit( ReturnStmt *returnStmt ) override; |
---|
76 | virtual void visit( ThrowStmt *throwStmt ) override; |
---|
77 | virtual void visit( CatchStmt *catchStmt ) override; |
---|
78 | |
---|
79 | virtual void visit( SingleInit *singleInit ) override; |
---|
80 | virtual void visit( ListInit *listInit ) override; |
---|
81 | virtual void visit( ConstructorInit *ctorInit ) override; |
---|
82 | private: |
---|
83 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
84 | |
---|
85 | template< typename PtrType > |
---|
86 | void handlePtrType( PtrType * type ); |
---|
87 | |
---|
88 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & ); |
---|
89 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub ); |
---|
90 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
91 | |
---|
92 | Type * functionReturn = nullptr; |
---|
93 | CurrentObject currentObject = nullptr; |
---|
94 | bool inEnumDecl = false; |
---|
95 | }; |
---|
96 | |
---|
97 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
98 | Resolver resolver; |
---|
99 | acceptAll( translationUnit, resolver ); |
---|
100 | } |
---|
101 | |
---|
102 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
103 | TypeEnvironment env; |
---|
104 | return resolveInVoidContext( expr, indexer, env ); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | namespace { |
---|
109 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
110 | expr->set_env( new TypeSubstitution ); |
---|
111 | env.makeSubstitution( *expr->get_env() ); |
---|
112 | } |
---|
113 | } // namespace |
---|
114 | |
---|
115 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
116 | global_renamer.reset(); |
---|
117 | TypeEnvironment env; |
---|
118 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
119 | finishExpr( newExpr, env ); |
---|
120 | return newExpr; |
---|
121 | } |
---|
122 | |
---|
123 | namespace { |
---|
124 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
125 | TypeEnvironment env; |
---|
126 | AlternativeFinder finder( indexer, env ); |
---|
127 | finder.find( untyped ); |
---|
128 | #if 0 |
---|
129 | if ( finder.get_alternatives().size() != 1 ) { |
---|
130 | std::cout << "untyped expr is "; |
---|
131 | untyped->print( std::cout ); |
---|
132 | std::cout << std::endl << "alternatives are:"; |
---|
133 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
134 | i->print( std::cout ); |
---|
135 | } // for |
---|
136 | } // if |
---|
137 | #endif |
---|
138 | assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." ); |
---|
139 | Alternative &choice = finder.get_alternatives().front(); |
---|
140 | Expression *newExpr = choice.expr->clone(); |
---|
141 | finishExpr( newExpr, choice.env ); |
---|
142 | return newExpr; |
---|
143 | } |
---|
144 | |
---|
145 | bool isIntegralType( Type *type ) { |
---|
146 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
147 | return true; |
---|
148 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
149 | return bt->isInteger(); |
---|
150 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
151 | return true; |
---|
152 | } else { |
---|
153 | return false; |
---|
154 | } // if |
---|
155 | } |
---|
156 | |
---|
157 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
158 | TypeEnvironment env; |
---|
159 | AlternativeFinder finder( indexer, env ); |
---|
160 | finder.find( untyped ); |
---|
161 | #if 0 |
---|
162 | if ( finder.get_alternatives().size() != 1 ) { |
---|
163 | std::cout << "untyped expr is "; |
---|
164 | untyped->print( std::cout ); |
---|
165 | std::cout << std::endl << "alternatives are:"; |
---|
166 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
167 | i->print( std::cout ); |
---|
168 | } // for |
---|
169 | } // if |
---|
170 | #endif |
---|
171 | Expression *newExpr = 0; |
---|
172 | const TypeEnvironment *newEnv = 0; |
---|
173 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
174 | if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { |
---|
175 | if ( newExpr ) { |
---|
176 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
177 | } else { |
---|
178 | newExpr = i->expr->clone(); |
---|
179 | newEnv = &i->env; |
---|
180 | } // if |
---|
181 | } // if |
---|
182 | } // for |
---|
183 | if ( ! newExpr ) { |
---|
184 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
185 | } // if |
---|
186 | finishExpr( newExpr, *newEnv ); |
---|
187 | return newExpr; |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
193 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
194 | objectDecl->set_type( new_type ); |
---|
195 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
196 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
197 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
198 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
199 | // the RHS. |
---|
200 | ValueGuard<CurrentObject> temp( currentObject ); |
---|
201 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
202 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
203 | // enumerator initializers should not use the enum type to initialize, since |
---|
204 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
205 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
206 | } |
---|
207 | Parent::visit( objectDecl ); |
---|
208 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
209 | // delete newly created signed int type |
---|
210 | // delete currentObject.getType(); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | template< typename PtrType > |
---|
215 | void Resolver::handlePtrType( PtrType * type ) { |
---|
216 | if ( type->get_dimension() ) { |
---|
217 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
218 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
219 | delete type->get_dimension(); |
---|
220 | type->set_dimension( newExpr ); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | void Resolver::visit( ArrayType * at ) { |
---|
225 | handlePtrType( at ); |
---|
226 | Parent::visit( at ); |
---|
227 | } |
---|
228 | |
---|
229 | void Resolver::visit( PointerType * pt ) { |
---|
230 | handlePtrType( pt ); |
---|
231 | Parent::visit( pt ); |
---|
232 | } |
---|
233 | |
---|
234 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
235 | if ( typeDecl->get_base() ) { |
---|
236 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
237 | typeDecl->set_base( new_type ); |
---|
238 | } // if |
---|
239 | Parent::visit( typeDecl ); |
---|
240 | } |
---|
241 | |
---|
242 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
243 | #if 0 |
---|
244 | std::cout << "resolver visiting functiondecl "; |
---|
245 | functionDecl->print( std::cout ); |
---|
246 | std::cout << std::endl; |
---|
247 | #endif |
---|
248 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
249 | functionDecl->set_type( new_type ); |
---|
250 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); |
---|
251 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); |
---|
252 | Parent::visit( functionDecl ); |
---|
253 | |
---|
254 | // default value expressions have an environment which shouldn't be there and trips up later passes. |
---|
255 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently |
---|
256 | // see how it's useful. |
---|
257 | for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) { |
---|
258 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
259 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) { |
---|
260 | delete init->get_value()->get_env(); |
---|
261 | init->get_value()->set_env( nullptr ); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | void Resolver::visit( EnumDecl * enumDecl ) { |
---|
268 | // in case we decide to allow nested enums |
---|
269 | ValueGuard< bool > oldInEnumDecl( inEnumDecl ); |
---|
270 | inEnumDecl = true; |
---|
271 | Parent::visit( enumDecl ); |
---|
272 | } |
---|
273 | |
---|
274 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
275 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); |
---|
276 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
277 | delete exprStmt->get_expr(); |
---|
278 | exprStmt->set_expr( newExpr ); |
---|
279 | } |
---|
280 | |
---|
281 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
282 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
283 | delete asmExpr->get_operand(); |
---|
284 | asmExpr->set_operand( newExpr ); |
---|
285 | if ( asmExpr->get_inout() ) { |
---|
286 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
287 | delete asmExpr->get_inout(); |
---|
288 | asmExpr->set_inout( newExpr ); |
---|
289 | } // if |
---|
290 | } |
---|
291 | |
---|
292 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
293 | acceptAll( asmStmt->get_input(), *this); |
---|
294 | acceptAll( asmStmt->get_output(), *this); |
---|
295 | } |
---|
296 | |
---|
297 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
298 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
299 | delete ifStmt->get_condition(); |
---|
300 | ifStmt->set_condition( newExpr ); |
---|
301 | Parent::visit( ifStmt ); |
---|
302 | } |
---|
303 | |
---|
304 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
305 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
306 | delete whileStmt->get_condition(); |
---|
307 | whileStmt->set_condition( newExpr ); |
---|
308 | Parent::visit( whileStmt ); |
---|
309 | } |
---|
310 | |
---|
311 | void Resolver::visit( ForStmt *forStmt ) { |
---|
312 | Parent::visit( forStmt ); |
---|
313 | |
---|
314 | if ( forStmt->get_condition() ) { |
---|
315 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
316 | delete forStmt->get_condition(); |
---|
317 | forStmt->set_condition( newExpr ); |
---|
318 | } // if |
---|
319 | |
---|
320 | if ( forStmt->get_increment() ) { |
---|
321 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
322 | delete forStmt->get_increment(); |
---|
323 | forStmt->set_increment( newExpr ); |
---|
324 | } // if |
---|
325 | } |
---|
326 | |
---|
327 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
328 | ValueGuard< CurrentObject > oldCurrentObject( currentObject ); |
---|
329 | Expression *newExpr; |
---|
330 | newExpr = findIntegralExpression( switchStmt->get_condition(), *this ); |
---|
331 | delete switchStmt->get_condition(); |
---|
332 | switchStmt->set_condition( newExpr ); |
---|
333 | |
---|
334 | currentObject = CurrentObject( newExpr->get_result() ); |
---|
335 | Parent::visit( switchStmt ); |
---|
336 | } |
---|
337 | |
---|
338 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
339 | if ( caseStmt->get_condition() ) { |
---|
340 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
341 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
342 | CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() ); |
---|
343 | Expression * newExpr = findSingleExpression( castExpr, *this ); |
---|
344 | castExpr = strict_dynamic_cast< CastExpr * >( newExpr ); |
---|
345 | caseStmt->set_condition( castExpr->get_arg() ); |
---|
346 | castExpr->set_arg( nullptr ); |
---|
347 | delete castExpr; |
---|
348 | } |
---|
349 | Parent::visit( caseStmt ); |
---|
350 | } |
---|
351 | |
---|
352 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
353 | // must resolve the argument for a computed goto |
---|
354 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
355 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
356 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
357 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
358 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
359 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
360 | branchStmt->set_target( newExpr ); |
---|
361 | } // if |
---|
362 | } // if |
---|
363 | } |
---|
364 | |
---|
365 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
366 | if ( returnStmt->get_expr() ) { |
---|
367 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); |
---|
368 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
369 | delete castExpr; |
---|
370 | returnStmt->set_expr( newExpr ); |
---|
371 | } // if |
---|
372 | } |
---|
373 | |
---|
374 | void Resolver::visit( ThrowStmt *throwStmt ) { |
---|
375 | // TODO: Replace *exception type with &exception type. |
---|
376 | if ( throwStmt->get_expr() ) { |
---|
377 | StructDecl * exception_decl = |
---|
378 | lookupStruct( "__cfaehm__base_exception_t" ); |
---|
379 | assert( exception_decl ); |
---|
380 | Expression * wrapped = new CastExpr( |
---|
381 | throwStmt->get_expr(), |
---|
382 | new PointerType( |
---|
383 | noQualifiers, |
---|
384 | new StructInstType( |
---|
385 | noQualifiers, |
---|
386 | exception_decl |
---|
387 | ) |
---|
388 | ) |
---|
389 | ); |
---|
390 | Expression * newExpr = findSingleExpression( wrapped, *this ); |
---|
391 | throwStmt->set_expr( newExpr ); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | void Resolver::visit( CatchStmt *catchStmt ) { |
---|
396 | // inline Indexer::visit so that the exception variable is still in-scope for |
---|
397 | // findSingleExpression() below |
---|
398 | Parent::enterScope(); |
---|
399 | Visitor::visit( catchStmt ); |
---|
400 | |
---|
401 | if ( catchStmt->get_cond() ) { |
---|
402 | Expression * wrapped = new CastExpr( |
---|
403 | catchStmt->get_cond(), |
---|
404 | new BasicType( noQualifiers, BasicType::Bool ) |
---|
405 | ); |
---|
406 | catchStmt->set_cond( findSingleExpression( wrapped, *this ) ); |
---|
407 | } |
---|
408 | |
---|
409 | Parent::leaveScope(); |
---|
410 | } |
---|
411 | |
---|
412 | template< typename T > |
---|
413 | bool isCharType( T t ) { |
---|
414 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
415 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
416 | bt->get_kind() == BasicType::UnsignedChar; |
---|
417 | } |
---|
418 | return false; |
---|
419 | } |
---|
420 | |
---|
421 | void Resolver::visit( SingleInit *singleInit ) { |
---|
422 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
423 | UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() ); |
---|
424 | Expression * newExpr = findSingleExpression( untyped, *this ); |
---|
425 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); |
---|
426 | |
---|
427 | // move cursor to the object that is actually initialized |
---|
428 | currentObject.setNext( initExpr->get_designation() ); |
---|
429 | |
---|
430 | // discard InitExpr wrapper and retain relevant pieces |
---|
431 | newExpr = initExpr->get_expr(); |
---|
432 | newExpr->set_env( initExpr->get_env() ); |
---|
433 | initExpr->set_expr( nullptr ); |
---|
434 | initExpr->set_env( nullptr ); |
---|
435 | delete initExpr; |
---|
436 | |
---|
437 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions) |
---|
438 | Type * initContext = currentObject.getCurrentType(); |
---|
439 | |
---|
440 | // check if actual object's type is char[] |
---|
441 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
442 | if ( isCharType( at->get_base() ) ) { |
---|
443 | // check if the resolved type is char * |
---|
444 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
445 | if ( isCharType( pt->get_base() ) ) { |
---|
446 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
447 | CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr ); |
---|
448 | newExpr = ce->get_arg(); |
---|
449 | ce->set_arg( nullptr ); |
---|
450 | delete ce; |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | // set initializer expr to resolved express |
---|
457 | singleInit->set_value( newExpr ); |
---|
458 | |
---|
459 | // move cursor to next object in preparation for next initializer |
---|
460 | currentObject.increment(); |
---|
461 | } |
---|
462 | |
---|
463 | void Resolver::visit( ListInit * listInit ) { |
---|
464 | // move cursor into brace-enclosed initializer-list |
---|
465 | currentObject.enterListInit(); |
---|
466 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element |
---|
467 | std::list<Designation *> newDesignations; |
---|
468 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
469 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving |
---|
470 | // the initializer against that object. |
---|
471 | Designation * des = std::get<0>(p); |
---|
472 | Initializer * init = std::get<1>(p); |
---|
473 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
474 | init->accept( *this ); |
---|
475 | } |
---|
476 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
477 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
478 | currentObject.exitListInit(); |
---|
479 | |
---|
480 | // xxx - this part has not be folded into CurrentObject yet |
---|
481 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
482 | // Type * base = tt->get_baseType()->get_base(); |
---|
483 | // if ( base ) { |
---|
484 | // // know the implementation type, so try using that as the initContext |
---|
485 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
486 | // currentObject = &tmpObj; |
---|
487 | // visit( listInit ); |
---|
488 | // } else { |
---|
489 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
490 | // Parent::visit( listInit ); |
---|
491 | // } |
---|
492 | // } else { |
---|
493 | } |
---|
494 | |
---|
495 | // ConstructorInit - fall back on C-style initializer |
---|
496 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
497 | // could not find valid constructor, or found an intrinsic constructor |
---|
498 | // fall back on C-style initializer |
---|
499 | delete ctorInit->get_ctor(); |
---|
500 | ctorInit->set_ctor( NULL ); |
---|
501 | delete ctorInit->get_dtor(); |
---|
502 | ctorInit->set_dtor( NULL ); |
---|
503 | maybeAccept( ctorInit->get_init(), *this ); |
---|
504 | } |
---|
505 | |
---|
506 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
507 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
508 | assert( ctorInit ); |
---|
509 | Resolver resolver( indexer ); |
---|
510 | ctorInit->accept( resolver ); |
---|
511 | } |
---|
512 | |
---|
513 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
514 | assert( stmtExpr ); |
---|
515 | Resolver resolver( indexer ); |
---|
516 | stmtExpr->accept( resolver ); |
---|
517 | } |
---|
518 | |
---|
519 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
520 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
521 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
522 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
523 | |
---|
524 | // found a constructor - can get rid of C-style initializer |
---|
525 | delete ctorInit->get_init(); |
---|
526 | ctorInit->set_init( NULL ); |
---|
527 | |
---|
528 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
529 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
530 | // to clean up generated code. |
---|
531 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
532 | delete ctorInit->get_ctor(); |
---|
533 | ctorInit->set_ctor( NULL ); |
---|
534 | } |
---|
535 | |
---|
536 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
537 | delete ctorInit->get_dtor(); |
---|
538 | ctorInit->set_dtor( NULL ); |
---|
539 | } |
---|
540 | |
---|
541 | // xxx - todo -- what about arrays? |
---|
542 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
543 | // // can reduce the constructor down to a SingleInit using the |
---|
544 | // // second argument from the ctor call, since |
---|
545 | // delete ctorInit->get_ctor(); |
---|
546 | // ctorInit->set_ctor( NULL ); |
---|
547 | |
---|
548 | // Expression * arg = |
---|
549 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
550 | // } |
---|
551 | } |
---|
552 | } // namespace ResolvExpr |
---|
553 | |
---|
554 | // Local Variables: // |
---|
555 | // tab-width: 4 // |
---|
556 | // mode: c++ // |
---|
557 | // compile-command: "make install" // |
---|
558 | // End: // |
---|