source: src/ResolvExpr/Resolver.cc@ 9d79f93

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

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

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