source: src/ResolvExpr/Resolver.cc@ 695e00d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 695e00d was 695e00d, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 27.2 KB
Line 
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/PassVisitor.h" // for PassVisitor
24#include "Common/SemanticError.h" // for SemanticError
25#include "Common/utility.h" // for ValueGuard, group_iterate
26#include "CurrentObject.h" // for CurrentObject
27#include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt
28#include "RenameVars.h" // for RenameVars, global_renamer
29#include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment
30#include "ResolveTypeof.h" // for resolveTypeof
31#include "Resolver.h"
32#include "SymTab/Autogen.h" // for SizeType
33#include "SymTab/Indexer.h" // for Indexer
34#include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar...
35#include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr
36#include "SynTree/Initializer.h" // for ConstructorInit, SingleInit
37#include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt
38#include "SynTree/Type.h" // for Type, BasicType, PointerType
39#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
40#include "SynTree/Visitor.h" // for acceptAll, maybeAccept
41#include "typeops.h" // for extractResultType
42#include "Unify.h" // for unify
43
44using namespace std;
45
46namespace ResolvExpr {
47 struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting {
48 Resolver() {}
49 Resolver( const SymTab::Indexer & other ) {
50 indexer = other;
51 }
52
53 void previsit( FunctionDecl *functionDecl );
54 void postvisit( FunctionDecl *functionDecl );
55 void previsit( ObjectDecl *functionDecl );
56 void previsit( TypeDecl *typeDecl );
57 void previsit( EnumDecl * enumDecl );
58
59 void previsit( ArrayType * at );
60 void previsit( PointerType * at );
61
62 void previsit( ExprStmt *exprStmt );
63 void previsit( AsmExpr *asmExpr );
64 void previsit( AsmStmt *asmStmt );
65 void previsit( IfStmt *ifStmt );
66 void previsit( WhileStmt *whileStmt );
67 void previsit( ForStmt *forStmt );
68 void previsit( SwitchStmt *switchStmt );
69 void previsit( CaseStmt *caseStmt );
70 void previsit( BranchStmt *branchStmt );
71 void previsit( ReturnStmt *returnStmt );
72 void previsit( ThrowStmt *throwStmt );
73 void previsit( CatchStmt *catchStmt );
74 void previsit( WaitForStmt * stmt );
75
76 void previsit( SingleInit *singleInit );
77 void previsit( ListInit *listInit );
78 void previsit( ConstructorInit *ctorInit );
79 private:
80 typedef std::list< Initializer * >::iterator InitIterator;
81
82 template< typename PtrType >
83 void handlePtrType( PtrType * type );
84
85 void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
86 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
87 void fallbackInit( ConstructorInit * ctorInit );
88
89 Type * functionReturn = nullptr;
90 CurrentObject currentObject = nullptr;
91 bool inEnumDecl = false;
92 };
93
94 void resolve( std::list< Declaration * > translationUnit ) {
95 PassVisitor<Resolver> resolver;
96 acceptAll( translationUnit, resolver );
97 }
98
99 // used in resolveTypeof
100 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
101 TypeEnvironment env;
102 return resolveInVoidContext( expr, indexer, env );
103 }
104
105 namespace {
106 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
107 expr->set_env( new TypeSubstitution );
108 env.makeSubstitution( *expr->get_env() );
109 }
110 } // namespace
111
112 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
113 global_renamer.reset();
114 TypeEnvironment env;
115 Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
116 finishExpr( newExpr, env );
117 return newExpr;
118 }
119
120 namespace {
121 Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
122 TypeEnvironment env;
123 AlternativeFinder finder( indexer, env );
124 finder.find( untyped );
125#if 0
126 if ( finder.get_alternatives().size() != 1 ) {
127 std::cout << "untyped expr is ";
128 untyped->print( std::cout );
129 std::cout << std::endl << "alternatives are:";
130 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
131 i->print( std::cout );
132 } // for
133 } // if
134#endif
135 assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." );
136 Alternative &choice = finder.get_alternatives().front();
137 Expression *newExpr = choice.expr->clone();
138 finishExpr( newExpr, choice.env );
139 return newExpr;
140 }
141
142 bool isIntegralType( Type *type ) {
143 if ( dynamic_cast< EnumInstType * >( type ) ) {
144 return true;
145 } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
146 return bt->isInteger();
147 } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) {
148 return true;
149 } else {
150 return false;
151 } // if
152 }
153
154 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
155 TypeEnvironment env;
156 AlternativeFinder finder( indexer, env );
157 finder.find( untyped );
158#if 0
159 if ( finder.get_alternatives().size() != 1 ) {
160 std::cout << "untyped expr is ";
161 untyped->print( std::cout );
162 std::cout << std::endl << "alternatives are:";
163 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
164 i->print( std::cout );
165 } // for
166 } // if
167#endif
168 Expression *newExpr = 0;
169 const TypeEnvironment *newEnv = 0;
170 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
171 if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) {
172 if ( newExpr ) {
173 throw SemanticError( "Too many interpretations for case control expression", untyped );
174 } else {
175 newExpr = i->expr->clone();
176 newEnv = &i->env;
177 } // if
178 } // if
179 } // for
180 if ( ! newExpr ) {
181 throw SemanticError( "No interpretations for case control expression", untyped );
182 } // if
183 finishExpr( newExpr, *newEnv );
184 return newExpr;
185 }
186
187 }
188
189 void Resolver::previsit( ObjectDecl *objectDecl ) {
190 Type *new_type = resolveTypeof( objectDecl->get_type(), indexer );
191 objectDecl->set_type( new_type );
192 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
193 // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
194 // initContext because of a function type can contain object declarations in the return and parameter types. So
195 // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
196 // the RHS.
197 GuardValue( currentObject );
198 currentObject = CurrentObject( objectDecl->get_type() );
199 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
200 // enumerator initializers should not use the enum type to initialize, since
201 // the enum type is still incomplete at this point. Use signed int instead.
202 currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
203 }
204 }
205
206 template< typename PtrType >
207 void Resolver::handlePtrType( PtrType * type ) {
208 if ( type->get_dimension() ) {
209 CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
210 Expression *newExpr = findSingleExpression( castExpr, indexer );
211 delete type->get_dimension();
212 type->set_dimension( newExpr );
213 }
214 }
215
216 void Resolver::previsit( ArrayType * at ) {
217 handlePtrType( at );
218 }
219
220 void Resolver::previsit( PointerType * pt ) {
221 handlePtrType( pt );
222 }
223
224 void Resolver::previsit( TypeDecl *typeDecl ) {
225 if ( typeDecl->get_base() ) {
226 Type *new_type = resolveTypeof( typeDecl->get_base(), indexer );
227 typeDecl->set_base( new_type );
228 } // if
229 }
230
231 void Resolver::previsit( FunctionDecl *functionDecl ) {
232#if 0
233 std::cerr << "resolver visiting functiondecl ";
234 functionDecl->print( std::cerr );
235 std::cerr << std::endl;
236#endif
237 Type *new_type = resolveTypeof( functionDecl->get_type(), indexer );
238 functionDecl->set_type( new_type );
239 GuardValue( functionReturn );
240 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
241 }
242
243
244 void Resolver::postvisit( FunctionDecl *functionDecl ) {
245 // default value expressions have an environment which shouldn't be there and trips up later passes.
246 // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
247 // see how it's useful.
248 for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) {
249 if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) {
250 if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) {
251 delete init->get_value()->get_env();
252 init->get_value()->set_env( nullptr );
253 }
254 }
255 }
256 }
257
258 void Resolver::previsit( EnumDecl * ) {
259 // in case we decide to allow nested enums
260 GuardValue( inEnumDecl );
261 inEnumDecl = true;
262 }
263
264 void Resolver::previsit( ExprStmt *exprStmt ) {
265 visit_children = false;
266 assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
267 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
268 delete exprStmt->get_expr();
269 exprStmt->set_expr( newExpr );
270 }
271
272 void Resolver::previsit( AsmExpr *asmExpr ) {
273 visit_children = false;
274 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
275 delete asmExpr->get_operand();
276 asmExpr->set_operand( newExpr );
277 if ( asmExpr->get_inout() ) {
278 newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
279 delete asmExpr->get_inout();
280 asmExpr->set_inout( newExpr );
281 } // if
282 }
283
284 void Resolver::previsit( AsmStmt *asmStmt ) {
285 visit_children = false;
286 acceptAll( asmStmt->get_input(), *visitor );
287 acceptAll( asmStmt->get_output(), *visitor );
288 }
289
290 void Resolver::previsit( IfStmt *ifStmt ) {
291 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
292 delete ifStmt->get_condition();
293 ifStmt->set_condition( newExpr );
294 }
295
296 void Resolver::previsit( WhileStmt *whileStmt ) {
297 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
298 delete whileStmt->get_condition();
299 whileStmt->set_condition( newExpr );
300 }
301
302 void Resolver::previsit( ForStmt *forStmt ) {
303 if ( forStmt->get_condition() ) {
304 Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
305 delete forStmt->get_condition();
306 forStmt->set_condition( newExpr );
307 } // if
308
309 if ( forStmt->get_increment() ) {
310 Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
311 delete forStmt->get_increment();
312 forStmt->set_increment( newExpr );
313 } // if
314 }
315
316 void Resolver::previsit( SwitchStmt *switchStmt ) {
317 GuardValue( currentObject );
318 Expression *newExpr;
319 newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
320 delete switchStmt->get_condition();
321 switchStmt->set_condition( newExpr );
322
323 currentObject = CurrentObject( newExpr->get_result() );
324 }
325
326 void Resolver::previsit( CaseStmt *caseStmt ) {
327 if ( caseStmt->get_condition() ) {
328 std::list< InitAlternative > initAlts = currentObject.getOptions();
329 assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
330 CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
331 Expression * newExpr = findSingleExpression( castExpr, indexer );
332 castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
333 caseStmt->set_condition( castExpr->get_arg() );
334 castExpr->set_arg( nullptr );
335 delete castExpr;
336 }
337 }
338
339 void Resolver::previsit( BranchStmt *branchStmt ) {
340 visit_children = false;
341 // must resolve the argument for a computed goto
342 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
343 if ( Expression * arg = branchStmt->get_computedTarget() ) {
344 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
345 PointerType pt( Type::Qualifiers(), v.clone() );
346 CastExpr * castExpr = new CastExpr( arg, pt.clone() );
347 Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
348 branchStmt->set_target( newExpr );
349 } // if
350 } // if
351 }
352
353 void Resolver::previsit( ReturnStmt *returnStmt ) {
354 visit_children = false;
355 if ( returnStmt->get_expr() ) {
356 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
357 Expression *newExpr = findSingleExpression( castExpr, indexer );
358 delete castExpr;
359 returnStmt->set_expr( newExpr );
360 } // if
361 }
362
363 void Resolver::previsit( ThrowStmt *throwStmt ) {
364 visit_children = false;
365 // TODO: Replace *exception type with &exception type.
366 if ( throwStmt->get_expr() ) {
367 StructDecl * exception_decl =
368 indexer.lookupStruct( "__cfaehm__base_exception_t" );
369 assert( exception_decl );
370 Expression * wrapped = new CastExpr(
371 throwStmt->get_expr(),
372 new PointerType(
373 noQualifiers,
374 new StructInstType(
375 noQualifiers,
376 exception_decl
377 )
378 )
379 );
380 Expression * newExpr = findSingleExpression( wrapped, indexer );
381 throwStmt->set_expr( newExpr );
382 }
383 }
384
385 void Resolver::previsit( CatchStmt *catchStmt ) {
386 if ( catchStmt->get_cond() ) {
387 Expression * wrapped = new CastExpr(
388 catchStmt->get_cond(),
389 new BasicType( noQualifiers, BasicType::Bool )
390 );
391 catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
392 }
393 }
394
395 inline void resolveAsIf( Expression *& expr, Resolver & resolver ) {
396 if( !expr ) return;
397 Expression * newExpr = findSingleExpression( expr, resolver );
398 delete expr;
399 expr = newExpr;
400 }
401
402 inline void resolveAsType( Expression *& expr, Type * type, Resolver & resolver ) {
403 if( !expr ) return;
404 Expression * newExpr = findSingleExpression( new CastExpr( expr, type ), resolver );
405 delete expr;
406 expr = newExpr;
407 }
408
409 template< typename iterator_t >
410 inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) {
411 while( it != end && !(*it)->get_type()->get_mutex() ) {
412 it++;
413 }
414
415 return it != end;
416 }
417
418 void Resolver::previsit( WaitForStmt * stmt ) {
419
420 // Resolve all clauses first
421 for( auto& clause : stmt->clauses ) {
422
423 TypeEnvironment env;
424 AlternativeFinder funcFinder( *this, env );
425
426 // Find all alternatives for a function in canonical form
427 funcFinder.findWithAdjustment( clause.target.function );
428
429 if ( funcFinder.get_alternatives().empty() ) {
430 stringstream ss;
431 ss << "Use of undeclared indentifier '";
432 ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name;
433 ss << "' in call to waitfor";
434 throw SemanticError( ss.str() );
435 }
436
437 // Find all alternatives for all arguments in canonical form
438 std::list< AlternativeFinder > argAlternatives;
439 funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) );
440
441 // List all combinations of arguments
442 std::list< AltList > possibilities;
443 combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
444
445 AltList func_candidates;
446 std::vector< AltList > args_candidates;
447
448 // For every possible function :
449 // try matching the arguments to the parameters
450 // not the other way around because we have more arguments than parameters
451 SemanticError errors;
452 for ( Alternative & func : funcFinder.get_alternatives() ) {
453 try {
454 PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() );
455 if( !pointer ) {
456 throw SemanticError( "candidate not viable: not a pointer type\n", func.expr->get_result() );
457 }
458
459 FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() );
460 if( !function ) {
461 throw SemanticError( "candidate not viable: not a function type\n", pointer->get_base() );
462 }
463
464
465 {
466 auto param = function->parameters.begin();
467 auto param_end = function->parameters.end();
468
469 if( !advance_to_mutex( param, param_end ) ) {
470 throw SemanticError("candidate function not viable: no mutex parameters\n", function);
471 }
472 }
473
474 Alternative newFunc( func );
475 // Strip reference from function
476 referenceToRvalueConversion( newFunc.expr );
477
478 // For all the set of arguments we have try to match it with the parameter of the current function alternative
479 for ( auto & argsList : possibilities ) {
480
481 try {
482 // Declare data structures need for resolution
483 OpenVarSet openVars;
484 AssertionSet resultNeed, resultHave;
485 TypeEnvironment resultEnv;
486
487 // Load type variables from arguemnts into one shared space
488 simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv );
489
490 // Make sure we don't widen any existing bindings
491 for ( auto & i : resultEnv ) {
492 i.allowWidening = false;
493 }
494
495 // Find any unbound type variables
496 resultEnv.extractOpenVars( openVars );
497
498 auto param = function->parameters.begin();
499 auto param_end = function->parameters.end();
500
501 // For every arguments of its set, check if it matches one of the parameter
502 // The order is important
503 for( auto & arg : argsList ) {
504
505 // Ignore non-mutex arguments
506 if( !advance_to_mutex( param, param_end ) ) {
507 // We ran out of parameters but still have arguments
508 // this function doesn't match
509 throw SemanticError("candidate function not viable: too many mutex arguments\n", function);
510 }
511
512 // Check if the argument matches the parameter type in the current scope
513 if( ! unify( (*param)->get_type(), arg.expr->get_result(), resultEnv, resultNeed, resultHave, openVars, *this ) ) {
514 // Type doesn't match
515 stringstream ss;
516 ss << "candidate function not viable: no known convertion from '";
517 arg.expr->get_result()->print( ss );
518 ss << "' to '";
519 (*param)->get_type()->print( ss );
520 ss << "'\n";
521 throw SemanticError(ss.str(), function);
522 }
523
524 param++;
525 }
526
527 // All arguments match !
528
529 // Check if parameters are missing
530 if( advance_to_mutex( param, param_end ) ) {
531 // We ran out of arguments but still have parameters left
532 // this function doesn't match
533 throw SemanticError("candidate function not viable: too few mutex arguments\n", function);
534 }
535
536 // All parameters match !
537
538 // Finish the expressions to tie in the proper environments
539 finishExpr( newFunc.expr, resultEnv );
540 for( Alternative & alt : argsList ) {
541 finishExpr( alt.expr, resultEnv );
542 }
543
544 // This is a match store it and save it for later
545 func_candidates.push_back( newFunc );
546 args_candidates.push_back( argsList );
547
548 }
549 catch( SemanticError &e ) {
550 errors.append( e );
551 }
552 }
553 }
554 catch( SemanticError &e ) {
555 errors.append( e );
556 }
557 }
558
559 // Make sure we got the right number of arguments
560 if( func_candidates.empty() ) { SemanticError top( "No alternatives for function in call to waitfor" ); top.append( errors ); throw top; }
561 if( args_candidates.empty() ) { SemanticError top( "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; }
562 if( func_candidates.size() > 1 ) { SemanticError top( "Ambiguous function in call to waitfor" ); top.append( errors ); throw top; }
563 if( args_candidates.size() > 1 ) { SemanticError top( "Ambiguous arguments in call to waitfor" ); top.append( errors ); throw top; }
564
565
566 // Swap the results from the alternative with the unresolved values.
567 // Alternatives will handle deletion on destruction
568 std::swap( clause.target.function, func_candidates.front().expr );
569 for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) {
570 std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr );
571 }
572
573 // Resolve the conditions as if it were an IfStmt
574 // Resolve the statments normally
575 resolveAsIf( clause.condition, *this );
576 clause.statement->accept( *this );
577 }
578
579
580 if( stmt->timeout.statement ) {
581 // Resolve the timeout as an size_t for now
582 // Resolve the conditions as if it were an IfStmt
583 // Resolve the statments normally
584 resolveAsType( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), *this );
585 resolveAsIf ( stmt->timeout.condition, *this );
586 stmt->timeout.statement->accept( *this );
587 }
588
589 if( stmt->orelse.statement ) {
590 // Resolve the conditions as if it were an IfStmt
591 // Resolve the statments normally
592 resolveAsIf( stmt->orelse.condition, *this );
593 stmt->orelse.statement->accept( *this );
594 }
595 }
596
597 template< typename T >
598 bool isCharType( T t ) {
599 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
600 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
601 bt->get_kind() == BasicType::UnsignedChar;
602 }
603 return false;
604 }
605
606 void Resolver::previsit( SingleInit *singleInit ) {
607 visit_children = false;
608 // resolve initialization using the possibilities as determined by the currentObject cursor
609 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
610 Expression * newExpr = findSingleExpression( untyped, indexer );
611 InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
612
613 // move cursor to the object that is actually initialized
614 currentObject.setNext( initExpr->get_designation() );
615
616 // discard InitExpr wrapper and retain relevant pieces
617 newExpr = initExpr->get_expr();
618 newExpr->set_env( initExpr->get_env() );
619 initExpr->set_expr( nullptr );
620 initExpr->set_env( nullptr );
621 delete initExpr;
622
623 // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
624 Type * initContext = currentObject.getCurrentType();
625
626 // check if actual object's type is char[]
627 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
628 if ( isCharType( at->get_base() ) ) {
629 // check if the resolved type is char *
630 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
631 if ( isCharType( pt->get_base() ) ) {
632 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
633 CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr );
634 newExpr = ce->get_arg();
635 ce->set_arg( nullptr );
636 delete ce;
637 }
638 }
639 }
640 }
641
642 // set initializer expr to resolved express
643 singleInit->set_value( newExpr );
644
645 // move cursor to next object in preparation for next initializer
646 currentObject.increment();
647 }
648
649 void Resolver::previsit( ListInit * listInit ) {
650 visit_children = false;
651 // move cursor into brace-enclosed initializer-list
652 currentObject.enterListInit();
653 // xxx - fix this so that the list isn't copied, iterator should be used to change current element
654 std::list<Designation *> newDesignations;
655 for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) {
656 // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
657 // the initializer against that object.
658 Designation * des = std::get<0>(p);
659 Initializer * init = std::get<1>(p);
660 newDesignations.push_back( currentObject.findNext( des ) );
661 init->accept( *visitor );
662 }
663 // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
664 listInit->get_designations() = newDesignations; // xxx - memory management
665 currentObject.exitListInit();
666
667 // xxx - this part has not be folded into CurrentObject yet
668 // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
669 // Type * base = tt->get_baseType()->get_base();
670 // if ( base ) {
671 // // know the implementation type, so try using that as the initContext
672 // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr );
673 // currentObject = &tmpObj;
674 // visit( listInit );
675 // } else {
676 // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
677 // Parent::visit( listInit );
678 // }
679 // } else {
680 }
681
682 // ConstructorInit - fall back on C-style initializer
683 void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
684 // could not find valid constructor, or found an intrinsic constructor
685 // fall back on C-style initializer
686 delete ctorInit->get_ctor();
687 ctorInit->set_ctor( NULL );
688 delete ctorInit->get_dtor();
689 ctorInit->set_dtor( NULL );
690 maybeAccept( ctorInit->get_init(), *visitor );
691 }
692
693 // needs to be callable from outside the resolver, so this is a standalone function
694 void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
695 assert( ctorInit );
696 PassVisitor<Resolver> resolver( indexer );
697 ctorInit->accept( resolver );
698 }
699
700 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
701 assert( stmtExpr );
702 PassVisitor<Resolver> resolver( indexer );
703 stmtExpr->accept( resolver );
704 }
705
706 void Resolver::previsit( ConstructorInit *ctorInit ) {
707 visit_children = false;
708 // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
709 maybeAccept( ctorInit->get_ctor(), *visitor );
710 maybeAccept( ctorInit->get_dtor(), *visitor );
711
712 // found a constructor - can get rid of C-style initializer
713 delete ctorInit->get_init();
714 ctorInit->set_init( NULL );
715
716 // intrinsic single parameter constructors and destructors do nothing. Since this was
717 // implicitly generated, there's no way for it to have side effects, so get rid of it
718 // to clean up generated code.
719 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
720 delete ctorInit->get_ctor();
721 ctorInit->set_ctor( NULL );
722 }
723
724 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
725 delete ctorInit->get_dtor();
726 ctorInit->set_dtor( NULL );
727 }
728
729 // xxx - todo -- what about arrays?
730 // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
731 // // can reduce the constructor down to a SingleInit using the
732 // // second argument from the ctor call, since
733 // delete ctorInit->get_ctor();
734 // ctorInit->set_ctor( NULL );
735
736 // Expression * arg =
737 // ctorInit->set_init( new SingleInit( arg ) );
738 // }
739 }
740} // namespace ResolvExpr
741
742// Local Variables: //
743// tab-width: 4 //
744// mode: c++ //
745// compile-command: "make install" //
746// End: //
Note: See TracBrowser for help on using the repository browser.