source: src/ResolvExpr/Resolver.cc@ a2dbad10

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 a2dbad10 was 8f98b78, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Now properly resolve the calls to get_monitor

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