source: src/ResolvExpr/Resolver.cc@ 0a22cda

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 0a22cda was 0a22cda, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove unnecessary resolver-generated initialization casts

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