source: src/ResolvExpr/Resolver.cc@ 7bcb8eb

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 7bcb8eb 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
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
[8b11840]99 void resolveDecl( Declaration * decl, const SymTab::Indexer &indexer ) {
100 PassVisitor<Resolver> resolver( indexer );
101 maybeAccept( decl, resolver );
102 }
103
[a4ca48c]104 // used in resolveTypeof
[a32b204]105 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
106 TypeEnvironment env;
107 return resolveInVoidContext( expr, indexer, env );
[d9a0e76]108 }
[a32b204]109
110 namespace {
111 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
112 expr->set_env( new TypeSubstitution );
113 env.makeSubstitution( *expr->get_env() );
114 }
[db4ecc5]115 } // namespace
[a32b204]116
[db4ecc5]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 }
[71f4e4f]124
[8f98b78]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 }
[d9a0e76]145
[8f98b78]146 namespace {
[a32b204]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();
[89e6ffc]152 } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) {
153 return true;
[a32b204]154 } else {
155 return false;
156 } // if
157 }
[71f4e4f]158
[a32b204]159 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
160 TypeEnvironment env;
161 AlternativeFinder finder( indexer, env );
162 finder.find( untyped );
[d9a0e76]163#if 0
[a32b204]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
[d9a0e76]172#endif
[a32b204]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 ) {
[906e24d]176 if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) {
[a32b204]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 }
[71f4e4f]191
[a32b204]192 }
[71f4e4f]193
[a4ca48c]194 void Resolver::previsit( ObjectDecl *objectDecl ) {
195 Type *new_type = resolveTypeof( objectDecl->get_type(), indexer );
[a32b204]196 objectDecl->set_type( new_type );
[3cfe27f]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.
[a4ca48c]202 GuardValue( currentObject );
[e4d829b]203 currentObject = CurrentObject( objectDecl->get_type() );
204 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
[a436947]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.
[e4d829b]207 currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[a436947]208 }
[bfbf97f]209 }
210
[40e636a]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() );
[a4ca48c]215 Expression *newExpr = findSingleExpression( castExpr, indexer );
[40e636a]216 delete type->get_dimension();
217 type->set_dimension( newExpr );
[d1d17f5]218 }
[40e636a]219 }
220
[a4ca48c]221 void Resolver::previsit( ArrayType * at ) {
[40e636a]222 handlePtrType( at );
[a32b204]223 }
[94b4364]224
[a4ca48c]225 void Resolver::previsit( PointerType * pt ) {
[40e636a]226 handlePtrType( pt );
227 }
228
[a4ca48c]229 void Resolver::previsit( TypeDecl *typeDecl ) {
[a32b204]230 if ( typeDecl->get_base() ) {
[a4ca48c]231 Type *new_type = resolveTypeof( typeDecl->get_base(), indexer );
[a32b204]232 typeDecl->set_base( new_type );
233 } // if
234 }
[94b4364]235
[a4ca48c]236 void Resolver::previsit( FunctionDecl *functionDecl ) {
[d9a0e76]237#if 0
[a4ca48c]238 std::cerr << "resolver visiting functiondecl ";
239 functionDecl->print( std::cerr );
240 std::cerr << std::endl;
[d9a0e76]241#endif
[a4ca48c]242 Type *new_type = resolveTypeof( functionDecl->get_type(), indexer );
[a32b204]243 functionDecl->set_type( new_type );
[a4ca48c]244 GuardValue( functionReturn );
[906e24d]245 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
[a4ca48c]246 }
[88d1066]247
[a4ca48c]248
249 void Resolver::postvisit( FunctionDecl *functionDecl ) {
[88d1066]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 }
[a32b204]261 }
[51b73452]262
[a4ca48c]263 void Resolver::previsit( EnumDecl * ) {
[a436947]264 // in case we decide to allow nested enums
[a4ca48c]265 GuardValue( inEnumDecl );
[a436947]266 inEnumDecl = true;
267 }
268
[a4ca48c]269 void Resolver::previsit( ExprStmt *exprStmt ) {
270 visit_children = false;
[1d2b64f]271 assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
[a4ca48c]272 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
[1d2b64f]273 delete exprStmt->get_expr();
274 exprStmt->set_expr( newExpr );
[a32b204]275 }
[51b73452]276
[a4ca48c]277 void Resolver::previsit( AsmExpr *asmExpr ) {
278 visit_children = false;
279 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
[7f5566b]280 delete asmExpr->get_operand();
281 asmExpr->set_operand( newExpr );
282 if ( asmExpr->get_inout() ) {
[a4ca48c]283 newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
[7f5566b]284 delete asmExpr->get_inout();
285 asmExpr->set_inout( newExpr );
286 } // if
287 }
288
[a4ca48c]289 void Resolver::previsit( AsmStmt *asmStmt ) {
290 visit_children = false;
291 acceptAll( asmStmt->get_input(), *visitor );
292 acceptAll( asmStmt->get_output(), *visitor );
[7f5566b]293 }
294
[a4ca48c]295 void Resolver::previsit( IfStmt *ifStmt ) {
296 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
[a32b204]297 delete ifStmt->get_condition();
298 ifStmt->set_condition( newExpr );
299 }
[51b73452]300
[a4ca48c]301 void Resolver::previsit( WhileStmt *whileStmt ) {
302 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
[a32b204]303 delete whileStmt->get_condition();
304 whileStmt->set_condition( newExpr );
305 }
[51b73452]306
[a4ca48c]307 void Resolver::previsit( ForStmt *forStmt ) {
[a32b204]308 if ( forStmt->get_condition() ) {
[a4ca48c]309 Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
[a32b204]310 delete forStmt->get_condition();
311 forStmt->set_condition( newExpr );
312 } // if
[71f4e4f]313
[a32b204]314 if ( forStmt->get_increment() ) {
[a4ca48c]315 Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
[a32b204]316 delete forStmt->get_increment();
317 forStmt->set_increment( newExpr );
318 } // if
319 }
[51b73452]320
[a4ca48c]321 void Resolver::previsit( SwitchStmt *switchStmt ) {
322 GuardValue( currentObject );
[a32b204]323 Expression *newExpr;
[a4ca48c]324 newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
[a32b204]325 delete switchStmt->get_condition();
326 switchStmt->set_condition( newExpr );
[71f4e4f]327
[e4d829b]328 currentObject = CurrentObject( newExpr->get_result() );
[a32b204]329 }
[51b73452]330
[a4ca48c]331 void Resolver::previsit( CaseStmt *caseStmt ) {
[32b8144]332 if ( caseStmt->get_condition() ) {
[e4d829b]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() );
[a4ca48c]336 Expression * newExpr = findSingleExpression( castExpr, indexer );
[e3e16bc]337 castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
[32b8144]338 caseStmt->set_condition( castExpr->get_arg() );
339 castExpr->set_arg( nullptr );
340 delete castExpr;
341 }
[a32b204]342 }
[51b73452]343
[a4ca48c]344 void Resolver::previsit( BranchStmt *branchStmt ) {
345 visit_children = false;
[de62360d]346 // must resolve the argument for a computed goto
347 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
[2871210]348 if ( Expression * arg = branchStmt->get_computedTarget() ) {
[de62360d]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() );
[a4ca48c]352 Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
[de62360d]353 branchStmt->set_target( newExpr );
354 } // if
355 } // if
356 }
357
[a4ca48c]358 void Resolver::previsit( ReturnStmt *returnStmt ) {
359 visit_children = false;
[a32b204]360 if ( returnStmt->get_expr() ) {
[906e24d]361 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
[a4ca48c]362 Expression *newExpr = findSingleExpression( castExpr, indexer );
[a32b204]363 delete castExpr;
364 returnStmt->set_expr( newExpr );
365 } // if
366 }
[51b73452]367
[a4ca48c]368 void Resolver::previsit( ThrowStmt *throwStmt ) {
369 visit_children = false;
[cbce272]370 // TODO: Replace *exception type with &exception type.
[307a732]371 if ( throwStmt->get_expr() ) {
[cbce272]372 StructDecl * exception_decl =
[a4ca48c]373 indexer.lookupStruct( "__cfaehm__base_exception_t" );
[cbce272]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 );
[a4ca48c]385 Expression * newExpr = findSingleExpression( wrapped, indexer );
[307a732]386 throwStmt->set_expr( newExpr );
387 }
388 }
389
[a4ca48c]390 void Resolver::previsit( CatchStmt *catchStmt ) {
[cbce272]391 if ( catchStmt->get_cond() ) {
392 Expression * wrapped = new CastExpr(
393 catchStmt->get_cond(),
394 new BasicType( noQualifiers, BasicType::Bool )
395 );
[a4ca48c]396 catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
[cbce272]397 }
398 }
399
[8f98b78]400 inline void resolveAsIf( Expression *& expr, SymTab::Indexer & indexer ) {
[1dcd9554]401 if( !expr ) return;
[8f98b78]402 Expression * newExpr = findSingleExpression( expr, indexer );
[1dcd9554]403 delete expr;
404 expr = newExpr;
405 }
406
[8f98b78]407 inline void resolveAsType( Expression *& expr, Type * type, SymTab::Indexer & indexer ) {
[1dcd9554]408 if( !expr ) return;
[8f98b78]409 Expression * newExpr = findSingleExpression( new CastExpr( expr, type ), indexer );
[1dcd9554]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
[695e00d]423 void Resolver::previsit( WaitForStmt * stmt ) {
[8f98b78]424 visit_children = false;
[1dcd9554]425
426 // Resolve all clauses first
427 for( auto& clause : stmt->clauses ) {
428
429 TypeEnvironment env;
[8f98b78]430 AlternativeFinder funcFinder( indexer, env );
[1dcd9554]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
[8f98b78]519 if( ! unify( (*param)->get_type(), arg.expr->get_result(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) {
[1dcd9554]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
[8f98b78]581 resolveAsIf( clause.condition, this->indexer );
582 clause.statement->accept( *visitor );
[1dcd9554]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
[8f98b78]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 );
[1dcd9554]593 }
594
595 if( stmt->orelse.statement ) {
596 // Resolve the conditions as if it were an IfStmt
597 // Resolve the statments normally
[8f98b78]598 resolveAsIf( stmt->orelse.condition, this->indexer );
599 stmt->orelse.statement->accept( *visitor );
[1dcd9554]600 }
601 }
602
[b5c5684]603 template< typename T >
604 bool isCharType( T t ) {
605 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
[71f4e4f]606 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
[b5c5684]607 bt->get_kind() == BasicType::UnsignedChar;
608 }
609 return false;
610 }
611
[a4ca48c]612 void Resolver::previsit( SingleInit *singleInit ) {
613 visit_children = false;
[62423350]614 // resolve initialization using the possibilities as determined by the currentObject cursor
[e4d829b]615 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
[a4ca48c]616 Expression * newExpr = findSingleExpression( untyped, indexer );
[e3e16bc]617 InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
[62423350]618
619 // move cursor to the object that is actually initialized
[e4d829b]620 currentObject.setNext( initExpr->get_designation() );
[62423350]621
622 // discard InitExpr wrapper and retain relevant pieces
623 newExpr = initExpr->get_expr();
[435e75f]624 newExpr->set_env( initExpr->get_env() );
[62423350]625 initExpr->set_expr( nullptr );
626 initExpr->set_env( nullptr );
[e4d829b]627 delete initExpr;
628
[62423350]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";
[e3e16bc]639 CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr );
[62423350]640 newExpr = ce->get_arg();
641 ce->set_arg( nullptr );
642 delete ce;
643 }
644 }
645 }
646 }
[94b4364]647
[62423350]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 }
[94b4364]654
[a4ca48c]655 void Resolver::previsit( ListInit * listInit ) {
656 visit_children = false;
[62423350]657 // move cursor into brace-enclosed initializer-list
[e4d829b]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()) ) {
[62423350]662 // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
663 // the initializer against that object.
[e4d829b]664 Designation * des = std::get<0>(p);
665 Initializer * init = std::get<1>(p);
666 newDesignations.push_back( currentObject.findNext( des ) );
[a4ca48c]667 init->accept( *visitor );
[b5c5684]668 }
[62423350]669 // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
[e4d829b]670 listInit->get_designations() = newDesignations; // xxx - memory management
671 currentObject.exitListInit();
672
[62423350]673 // xxx - this part has not be folded into CurrentObject yet
[e4d829b]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 {
[a32b204]686 }
[71f4e4f]687
[f1e012b]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 );
[71a145de]694 delete ctorInit->get_dtor();
695 ctorInit->set_dtor( NULL );
[a4ca48c]696 maybeAccept( ctorInit->get_init(), *visitor );
[f1e012b]697 }
698
[1d2b64f]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 );
[a4ca48c]702 PassVisitor<Resolver> resolver( indexer );
[1d2b64f]703 ctorInit->accept( resolver );
704 }
705
706 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
707 assert( stmtExpr );
[a4ca48c]708 PassVisitor<Resolver> resolver( indexer );
[1d2b64f]709 stmtExpr->accept( resolver );
710 }
711
[a4ca48c]712 void Resolver::previsit( ConstructorInit *ctorInit ) {
713 visit_children = false;
[1ba88a0]714 // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
[a4ca48c]715 maybeAccept( ctorInit->get_ctor(), *visitor );
716 maybeAccept( ctorInit->get_dtor(), *visitor );
[071a31a]717
[5b2f5bb]718 // found a constructor - can get rid of C-style initializer
719 delete ctorInit->get_init();
720 ctorInit->set_init( NULL );
[ec79847]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.
[f9cebb5]725 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
[ec79847]726 delete ctorInit->get_ctor();
727 ctorInit->set_ctor( NULL );
728 }
[f9cebb5]729
730 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
[ec79847]731 delete ctorInit->get_dtor();
732 ctorInit->set_dtor( NULL );
733 }
[a465caff]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 // }
[71f4e4f]745 }
[51b73452]746} // namespace ResolvExpr
[a32b204]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.