source: src/ResolvExpr/AlternativeFinder.cc@ a1d7679

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 a1d7679 was a1d7679, checked in by Aaron Moss <a3moss@…>, 9 years ago

Need a 4th level of recursive assertions to pass tests

  • Property mode set to 100644
File size: 42.1 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//
[6ed1d4b]7// AlternativeFinder.cc --
[a32b204]8//
9// Author : Richard C. Bilson
10// Created On : Sat May 16 23:52:08 2015
[e04ef3a]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Jun 13 16:13:54 2016
13// Update Count : 25
[a32b204]14//
15
[51b73452]16#include <list>
17#include <iterator>
18#include <algorithm>
19#include <functional>
20#include <cassert>
[ebf5689]21#include <unordered_map>
22#include <utility>
23#include <vector>
[51b73452]24
25#include "AlternativeFinder.h"
26#include "Alternative.h"
27#include "Cost.h"
28#include "typeops.h"
29#include "Unify.h"
30#include "RenameVars.h"
31#include "SynTree/Type.h"
32#include "SynTree/Declaration.h"
33#include "SynTree/Expression.h"
34#include "SynTree/Initializer.h"
35#include "SynTree/Visitor.h"
36#include "SymTab/Indexer.h"
37#include "SymTab/Mangler.h"
38#include "SynTree/TypeSubstitution.h"
39#include "SymTab/Validate.h"
40#include "Designators/Processor.h"
41#include "Tuples/TupleAssignment.h"
42#include "Tuples/NameMatcher.h"
[d3b7937]43#include "Common/utility.h"
[70f89d00]44#include "InitTweak/InitTweak.h"
[51b73452]45
[b87a5ed]46extern bool resolvep;
[6ed1d4b]47#define PRINT( text ) if ( resolvep ) { text }
[51b73452]48//#define DEBUG_COST
49
50namespace ResolvExpr {
[a32b204]51 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) {
52 CastExpr *castToVoid = new CastExpr( expr );
53
54 AlternativeFinder finder( indexer, env );
55 finder.findWithAdjustment( castToVoid );
56
57 // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0
58 // interpretations, an exception has already been thrown.
59 assert( finder.get_alternatives().size() == 1 );
60 CastExpr *newExpr = dynamic_cast< CastExpr* >( finder.get_alternatives().front().expr );
61 assert( newExpr );
62 env = finder.get_alternatives().front().env;
63 return newExpr->get_arg()->clone();
64 }
65
66 namespace {
67 void printAlts( const AltList &list, std::ostream &os, int indent = 0 ) {
68 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
69 i->print( os, indent );
70 os << std::endl;
71 }
72 }
[d9a0e76]73
[a32b204]74 void makeExprList( const AltList &in, std::list< Expression* > &out ) {
75 for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) {
76 out.push_back( i->expr->clone() );
77 }
78 }
[d9a0e76]79
[a32b204]80 Cost sumCost( const AltList &in ) {
81 Cost total;
82 for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) {
83 total += i->cost;
84 }
85 return total;
86 }
[d9a0e76]87
[a32b204]88 struct PruneStruct {
89 bool isAmbiguous;
90 AltList::iterator candidate;
91 PruneStruct() {}
92 PruneStruct( AltList::iterator candidate ): isAmbiguous( false ), candidate( candidate ) {}
93 };
94
[0f19d763]95 /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations
[a32b204]96 template< typename InputIterator, typename OutputIterator >
97 void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out, const SymTab::Indexer &indexer ) {
98 // select the alternatives that have the minimum conversion cost for a particular set of result types
99 std::map< std::string, PruneStruct > selected;
100 for ( AltList::iterator candidate = begin; candidate != end; ++candidate ) {
101 PruneStruct current( candidate );
102 std::string mangleName;
103 for ( std::list< Type* >::const_iterator retType = candidate->expr->get_results().begin(); retType != candidate->expr->get_results().end(); ++retType ) {
104 Type *newType = (*retType)->clone();
105 candidate->env.apply( newType );
106 mangleName += SymTab::Mangler::mangle( newType );
107 delete newType;
108 }
109 std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName );
110 if ( mapPlace != selected.end() ) {
111 if ( candidate->cost < mapPlace->second.candidate->cost ) {
112 PRINT(
[6ed1d4b]113 std::cerr << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
[7c64920]114 )
[0f19d763]115 selected[ mangleName ] = current;
[a32b204]116 } else if ( candidate->cost == mapPlace->second.candidate->cost ) {
117 PRINT(
[6ed1d4b]118 std::cerr << "marking ambiguous" << std::endl;
[7c64920]119 )
[0f19d763]120 mapPlace->second.isAmbiguous = true;
[a32b204]121 }
122 } else {
123 selected[ mangleName ] = current;
124 }
125 }
[d9a0e76]126
127 PRINT(
[6ed1d4b]128 std::cerr << "there are " << selected.size() << " alternatives before elimination" << std::endl;
[7c64920]129 )
[a32b204]130
[0f19d763]131 // accept the alternatives that were unambiguous
132 for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) {
133 if ( ! target->second.isAmbiguous ) {
134 Alternative &alt = *target->second.candidate;
135 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) {
136 alt.env.applyFree( *result );
[a32b204]137 }
[0f19d763]138 *out++ = alt;
[a32b204]139 }
[0f19d763]140 }
[a32b204]141
[d9a0e76]142 }
[a32b204]143
144 template< typename InputIterator, typename OutputIterator >
145 void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
146 AltList alternatives;
147
148 // select the alternatives that have the minimum parameter cost
149 Cost minCost = Cost::infinity;
150 for ( AltList::iterator i = begin; i != end; ++i ) {
151 if ( i->cost < minCost ) {
152 minCost = i->cost;
153 i->cost = i->cvtCost;
154 alternatives.clear();
155 alternatives.push_back( *i );
156 } else if ( i->cost == minCost ) {
157 i->cost = i->cvtCost;
158 alternatives.push_back( *i );
159 }
160 }
161 std::copy( alternatives.begin(), alternatives.end(), out );
[d9a0e76]162 }
163
[a32b204]164 template< typename InputIterator >
165 void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
166 while ( begin != end ) {
167 result.simpleCombine( (*begin++).env );
168 }
169 }
[d9a0e76]170
[a32b204]171 void renameTypes( Expression *expr ) {
172 for ( std::list< Type* >::iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) {
173 (*i)->accept( global_renamer );
174 }
[d9a0e76]175 }
176 }
177
[a32b204]178 template< typename InputIterator, typename OutputIterator >
179 void AlternativeFinder::findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ) {
180 while ( begin != end ) {
181 AlternativeFinder finder( indexer, env );
182 finder.findWithAdjustment( *begin );
183 // XXX either this
184 //Designators::fixDesignations( finder, (*begin++)->get_argName() );
185 // or XXX this
186 begin++;
187 PRINT(
[6ed1d4b]188 std::cerr << "findSubExprs" << std::endl;
189 printAlts( finder.alternatives, std::cerr );
[7c64920]190 )
[0f19d763]191 *out++ = finder;
[a32b204]192 }
[d9a0e76]193 }
194
[a32b204]195 AlternativeFinder::AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env )
196 : indexer( indexer ), env( env ) {
[d9a0e76]197 }
[51b73452]198
[a32b204]199 void AlternativeFinder::find( Expression *expr, bool adjust ) {
200 expr->accept( *this );
201 if ( alternatives.empty() ) {
202 throw SemanticError( "No reasonable alternatives for expression ", expr );
203 }
204 for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) {
205 if ( adjust ) {
206 adjustExprTypeList( i->expr->get_results().begin(), i->expr->get_results().end(), i->env, indexer );
207 }
208 }
209 PRINT(
[6ed1d4b]210 std::cerr << "alternatives before prune:" << std::endl;
211 printAlts( alternatives, std::cerr );
[7c64920]212 )
[0f19d763]213 AltList::iterator oldBegin = alternatives.begin();
[a32b204]214 pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer );
215 if ( alternatives.begin() == oldBegin ) {
[5f2f2d7]216 std::ostringstream stream;
[a32b204]217 stream << "Can't choose between alternatives for expression ";
218 expr->print( stream );
219 stream << "Alternatives are:";
220 AltList winners;
221 findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) );
222 printAlts( winners, stream, 8 );
[5f2f2d7]223 throw SemanticError( stream.str() );
[a32b204]224 }
225 alternatives.erase( oldBegin, alternatives.end() );
226 PRINT(
[6ed1d4b]227 std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
[7c64920]228 )
[0f19d763]229 }
[d9a0e76]230
[a32b204]231 void AlternativeFinder::findWithAdjustment( Expression *expr ) {
232 find( expr, true );
[d9a0e76]233 }
[a32b204]234
235 template< typename StructOrUnionType >
236 void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const std::string &name ) {
237 std::list< Declaration* > members;
238 aggInst->lookup( name, members );
239 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
240 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
241 alternatives.push_back( Alternative( new MemberExpr( dwt->clone(), expr->clone() ), env, newCost ) );
242 renameTypes( alternatives.back().expr );
243 } else {
244 assert( false );
245 }
246 }
[d9a0e76]247 }
[a32b204]248
249 void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) {
250 alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) );
[d9a0e76]251 }
252
[a32b204]253 Cost computeConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) {
254 ApplicationExpr *appExpr = dynamic_cast< ApplicationExpr* >( alt.expr );
255 assert( appExpr );
256 PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
257 assert( pointer );
258 FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
259 assert( function );
260
261 Cost convCost( 0, 0, 0 );
262 std::list< DeclarationWithType* >& formals = function->get_parameters();
263 std::list< DeclarationWithType* >::iterator formal = formals.begin();
264 std::list< Expression* >& actuals = appExpr->get_args();
265 for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
266 PRINT(
[6ed1d4b]267 std::cerr << "actual expression:" << std::endl;
268 (*actualExpr)->print( std::cerr, 8 );
269 std::cerr << "--- results are" << std::endl;
270 printAll( (*actualExpr)->get_results(), std::cerr, 8 );
[7c64920]271 )
272 std::list< DeclarationWithType* >::iterator startFormal = formal;
[a32b204]273 Cost actualCost;
274 for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
275 if ( formal == formals.end() ) {
276 if ( function->get_isVarArgs() ) {
277 convCost += Cost( 1, 0, 0 );
278 break;
279 } else {
280 return Cost::infinity;
281 }
282 }
283 PRINT(
[6ed1d4b]284 std::cerr << std::endl << "converting ";
285 (*actual)->print( std::cerr, 8 );
286 std::cerr << std::endl << " to ";
287 (*formal)->get_type()->print( std::cerr, 8 );
[7c64920]288 )
289 Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
[a32b204]290 PRINT(
[6ed1d4b]291 std::cerr << std::endl << "cost is" << newCost << std::endl;
[7c64920]292 )
[a32b204]293
[7c64920]294 if ( newCost == Cost::infinity ) {
295 return newCost;
296 }
[a32b204]297 convCost += newCost;
298 actualCost += newCost;
299
300 convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actual, alt.env, indexer ), 0 );
301
302 formal++;
303 }
304 if ( actualCost != Cost( 0, 0, 0 ) ) {
305 std::list< DeclarationWithType* >::iterator startFormalPlusOne = startFormal;
306 startFormalPlusOne++;
307 if ( formal == startFormalPlusOne ) {
308 // not a tuple type
309 Type *newType = (*startFormal)->get_type()->clone();
310 alt.env.apply( newType );
311 *actualExpr = new CastExpr( *actualExpr, newType );
312 } else {
313 TupleType *newType = new TupleType( Type::Qualifiers() );
314 for ( std::list< DeclarationWithType* >::iterator i = startFormal; i != formal; ++i ) {
315 newType->get_types().push_back( (*i)->get_type()->clone() );
316 }
317 alt.env.apply( newType );
318 *actualExpr = new CastExpr( *actualExpr, newType );
319 }
320 }
321
[d9a0e76]322 }
[a32b204]323 if ( formal != formals.end() ) {
324 return Cost::infinity;
[d9a0e76]325 }
326
[a32b204]327 for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) {
328 PRINT(
[6ed1d4b]329 std::cerr << std::endl << "converting ";
330 assert->second.actualType->print( std::cerr, 8 );
331 std::cerr << std::endl << " to ";
332 assert->second.formalType->print( std::cerr, 8 );
[a32b204]333 )
334 Cost newCost = conversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env );
335 PRINT(
[6ed1d4b]336 std::cerr << std::endl << "cost of conversion is " << newCost << std::endl;
[a32b204]337 )
338 if ( newCost == Cost::infinity ) {
339 return newCost;
340 }
341 convCost += newCost;
[d9a0e76]342
[a32b204]343 convCost += Cost( 0, polyCost( assert->second.formalType, alt.env, indexer ) + polyCost( assert->second.actualType, alt.env, indexer ), 0 );
344 }
[d9a0e76]345
[a32b204]346 return convCost;
347 }
[d9a0e76]348
[8c84ebd]349 /// Adds type variables to the open variable set and marks their assertions
[a32b204]350 void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ) {
351 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
352 unifiableVars[ (*tyvar)->get_name() ] = (*tyvar)->get_kind();
353 for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
354 needAssertions[ *assert ] = true;
355 }
[d9a0e76]356/// needAssertions.insert( needAssertions.end(), (*tyvar)->get_assertions().begin(), (*tyvar)->get_assertions().end() );
357 }
358 }
[a32b204]359
360 bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) {
361 simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv );
362 // make sure we don't widen any existing bindings
363 for ( TypeEnvironment::iterator i = resultEnv.begin(); i != resultEnv.end(); ++i ) {
364 i->allowWidening = false;
365 }
366 resultEnv.extractOpenVars( openVars );
367
368 /*
369 Tuples::NameMatcher matcher( formals );
370 try {
371 matcher.match( actuals );
372 } catch ( Tuples::NoMatch &e ) {
373 std::cerr << "Alternative doesn't match: " << e.message << std::endl;
374 }
375 */
376 std::list< DeclarationWithType* >::iterator formal = formals.begin();
377 for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
378 for ( std::list< Type* >::iterator actual = actualExpr->expr->get_results().begin(); actual != actualExpr->expr->get_results().end(); ++actual ) {
379 if ( formal == formals.end() ) {
380 return isVarArgs;
381 }
382 PRINT(
383 std::cerr << "formal type is ";
384 (*formal)->get_type()->print( std::cerr );
385 std::cerr << std::endl << "actual type is ";
386 (*actual)->print( std::cerr );
387 std::cerr << std::endl;
[7c64920]388 )
[0f19d763]389 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
390 return false;
391 }
[d9a0e76]392 formal++;
[a32b204]393 }
394 }
395 // Handling of default values
396 while ( formal != formals.end() ) {
397 if ( ObjectDecl *od = dynamic_cast<ObjectDecl *>( *formal ) )
398 if ( SingleInit *si = dynamic_cast<SingleInit *>( od->get_init() ))
399 // so far, only constant expressions are accepted as default values
400 if ( ConstantExpr *cnstexpr = dynamic_cast<ConstantExpr *>( si->get_value()) )
401 if ( Constant *cnst = dynamic_cast<Constant *>( cnstexpr->get_constant() ) )
402 if ( unify( (*formal)->get_type(), cnst->get_type(), resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
403 // XXX Don't know if this is right
404 actuals.push_back( Alternative( cnstexpr->clone(), env, Cost::zero ) );
405 formal++;
406 if ( formal == formals.end()) break;
407 }
408 return false;
409 }
410 return true;
[d9a0e76]411 }
[51b73452]412
[89b686a]413 // /// Map of declaration uniqueIds (intended to be the assertions in an AssertionSet) to their parents and the number of times they've been included
414 //typedef std::unordered_map< UniqueId, std::unordered_map< UniqueId, unsigned > > AssertionParentSet;
[ebf5689]415
[a1d7679]416 static const int recursionLimit = /*10*/ 4; ///< Limit to depth of recursion satisfaction
[89b686a]417 //static const unsigned recursionParentLimit = 1; ///< Limit to the number of times an assertion can recursively use itself
[51b73452]418
[a32b204]419 void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) {
420 for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) {
421 if ( i->second == true ) {
422 i->first->accept( indexer );
423 }
424 }
[d9a0e76]425 }
[ebf5689]426
[a32b204]427 template< typename ForwardIterator, typename OutputIterator >
[89b686a]428 void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, /*const AssertionParentSet &needParents,*/
[ebf5689]429 int level, const SymTab::Indexer &indexer, OutputIterator out ) {
[a32b204]430 if ( begin == end ) {
431 if ( newNeed.empty() ) {
432 *out++ = newAlt;
433 return;
434 } else if ( level >= recursionLimit ) {
435 throw SemanticError( "Too many recursive assertions" );
436 } else {
437 AssertionSet newerNeed;
438 PRINT(
439 std::cerr << "recursing with new set:" << std::endl;
440 printAssertionSet( newNeed, std::cerr, 8 );
[7c64920]441 )
[89b686a]442 inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, /*needParents,*/ level+1, indexer, out );
[a32b204]443 return;
444 }
445 }
446
447 ForwardIterator cur = begin++;
448 if ( ! cur->second ) {
[89b686a]449 inferRecursive( begin, end, newAlt, openVars, decls, newNeed, /*needParents,*/ level, indexer, out );
[a32b204]450 }
451 DeclarationWithType *curDecl = cur->first;
[d9a0e76]452 PRINT(
[a32b204]453 std::cerr << "inferRecursive: assertion is ";
454 curDecl->print( std::cerr );
455 std::cerr << std::endl;
[7c64920]456 )
[0f19d763]457 std::list< DeclarationWithType* > candidates;
[a32b204]458 decls.lookupId( curDecl->get_name(), candidates );
[6ed1d4b]459/// if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; }
[a32b204]460 for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) {
461 PRINT(
[6ed1d4b]462 std::cerr << "inferRecursive: candidate is ";
463 (*candidate)->print( std::cerr );
464 std::cerr << std::endl;
[7c64920]465 )
[ebf5689]466
[0f19d763]467 AssertionSet newHave, newerNeed( newNeed );
[a32b204]468 TypeEnvironment newEnv( newAlt.env );
469 OpenVarSet newOpenVars( openVars );
470 Type *adjType = (*candidate)->get_type()->clone();
471 adjustExprType( adjType, newEnv, indexer );
472 adjType->accept( global_renamer );
473 PRINT(
474 std::cerr << "unifying ";
475 curDecl->get_type()->print( std::cerr );
476 std::cerr << " with ";
477 adjType->print( std::cerr );
478 std::cerr << std::endl;
[7c64920]479 )
[0f19d763]480 if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
481 PRINT(
482 std::cerr << "success!" << std::endl;
[a32b204]483 )
[0f19d763]484 SymTab::Indexer newDecls( decls );
485 addToIndexer( newHave, newDecls );
486 Alternative newerAlt( newAlt );
487 newerAlt.env = newEnv;
488 assert( (*candidate)->get_uniqueId() );
[22cad76]489 DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) );
[89b686a]490 //AssertionParentSet newNeedParents( needParents );
[22cad76]491 // skip repeatingly-self-recursive assertion satisfaction
[89b686a]492 // DOESN'T WORK: grandchild nodes conflict with their cousins
493 //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue;
[22cad76]494 Expression *varExpr = new VariableExpr( candDecl );
[0f19d763]495 deleteAll( varExpr->get_results() );
496 varExpr->get_results().clear();
497 varExpr->get_results().push_front( adjType->clone() );
498 PRINT(
[6ed1d4b]499 std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " ";
500 curDecl->print( std::cerr );
501 std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " ";
502 (*candidate)->print( std::cerr );
503 std::cerr << std::endl;
[7c64920]504 )
[0f19d763]505 ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
506 // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
507 appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
[89b686a]508 inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out );
[0f19d763]509 } else {
510 delete adjType;
511 }
[a32b204]512 }
[d9a0e76]513 }
514
[a32b204]515 template< typename OutputIterator >
516 void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
[d9a0e76]517// PRINT(
[6ed1d4b]518// std::cerr << "inferParameters: assertions needed are" << std::endl;
519// printAll( need, std::cerr, 8 );
[d9a0e76]520// )
[a32b204]521 SymTab::Indexer decls( indexer );
522 PRINT(
[6ed1d4b]523 std::cerr << "============= original indexer" << std::endl;
524 indexer.print( std::cerr );
525 std::cerr << "============= new indexer" << std::endl;
526 decls.print( std::cerr );
[7c64920]527 )
[0f19d763]528 addToIndexer( have, decls );
[a32b204]529 AssertionSet newNeed;
[89b686a]530 //AssertionParentSet needParents;
531 inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, /*needParents,*/ 0, indexer, out );
[d9a0e76]532// PRINT(
[6ed1d4b]533// std::cerr << "declaration 14 is ";
[d9a0e76]534// Declaration::declFromId
535// *out++ = newAlt;
536// )
537 }
538
[a32b204]539 template< typename OutputIterator >
540 void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, AltList &actualAlt, OutputIterator out ) {
541 OpenVarSet openVars;
542 AssertionSet resultNeed, resultHave;
543 TypeEnvironment resultEnv;
544 makeUnifiableVars( funcType, openVars, resultNeed );
545 if ( instantiateFunction( funcType->get_parameters(), actualAlt, funcType->get_isVarArgs(), openVars, resultEnv, resultNeed, resultHave ) ) {
546 ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
547 Alternative newAlt( appExpr, resultEnv, sumCost( actualAlt ) );
548 makeExprList( actualAlt, appExpr->get_args() );
549 PRINT(
[6ed1d4b]550 std::cerr << "need assertions:" << std::endl;
551 printAssertionSet( resultNeed, std::cerr, 8 );
[7c64920]552 )
[0f19d763]553 inferParameters( resultNeed, resultHave, newAlt, openVars, out );
[d9a0e76]554 }
555 }
556
[a32b204]557 void AlternativeFinder::visit( UntypedExpr *untypedExpr ) {
558 bool doneInit = false;
559 AlternativeFinder funcOpFinder( indexer, env );
[d9a0e76]560
[6ed1d4b]561 AlternativeFinder funcFinder( indexer, env );
562
563 {
[70f89d00]564 std::string fname = InitTweak::getFunctionName( untypedExpr );
565 if ( fname == "&&" ) {
[2871210]566 VoidType v = Type::Qualifiers(); // resolve to type void *
567 PointerType pt( Type::Qualifiers(), v.clone() );
568 UntypedExpr *vexpr = untypedExpr->clone();
569 vexpr->get_results().push_front( pt.clone() );
570 alternatives.push_back( Alternative( vexpr, env, Cost()) );
[a32b204]571 return;
572 }
573 }
[d9a0e76]574
[a32b204]575 funcFinder.findWithAdjustment( untypedExpr->get_function() );
576 std::list< AlternativeFinder > argAlternatives;
577 findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), back_inserter( argAlternatives ) );
[d9a0e76]578
[a32b204]579 std::list< AltList > possibilities;
580 combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
[d9a0e76]581
[a32b204]582 Tuples::TupleAssignSpotter tassign( this );
583 if ( tassign.isTupleAssignment( untypedExpr, possibilities ) ) {
584 // take care of possible tuple assignments, or discard expression
585 return;
586 } // else ...
[d9a0e76]587
[a32b204]588 AltList candidates;
[91b8a17]589 SemanticError errors;
[d9a0e76]590
[a32b204]591 for ( AltList::const_iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) {
[91b8a17]592 try {
593 PRINT(
594 std::cerr << "working on alternative: " << std::endl;
595 func->print( std::cerr, 8 );
596 )
597 // check if the type is pointer to function
598 PointerType *pointer;
599 if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) {
600 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
601 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
602 // XXX
603 //Designators::check_alternative( function, *actualAlt );
604 makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
605 }
606 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( pointer->get_base() ) ) {
607 EqvClass eqvClass;
608 if ( func->env.lookup( typeInst->get_name(), eqvClass ) && eqvClass.type ) {
609 if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) {
610 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
611 makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
612 } // for
613 } // if
[a32b204]614 } // if
615 } // if
[91b8a17]616 } else {
617 // seek a function operator that's compatible
618 if ( ! doneInit ) {
619 doneInit = true;
620 NameExpr *opExpr = new NameExpr( "?()" );
621 try {
622 funcOpFinder.findWithAdjustment( opExpr );
623 } catch( SemanticError &e ) {
624 // it's ok if there aren't any defined function ops
625 }
626 PRINT(
627 std::cerr << "known function ops:" << std::endl;
628 printAlts( funcOpFinder.alternatives, std::cerr, 8 );
629 )
[a32b204]630 }
631
[91b8a17]632 for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) {
633 // check if the type is pointer to function
634 PointerType *pointer;
635 if ( funcOp->expr->get_results().size() == 1
636 && ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_results().front() ) ) ) {
637 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
638 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
639 AltList currentAlt;
640 currentAlt.push_back( *func );
641 currentAlt.insert( currentAlt.end(), actualAlt->begin(), actualAlt->end() );
642 makeFunctionAlternatives( *funcOp, function, currentAlt, std::back_inserter( candidates ) );
643 } // for
644 } // if
[a32b204]645 } // if
[91b8a17]646 } // for
647 } // if
648 } catch ( SemanticError &e ) {
649 errors.append( e );
650 }
[a32b204]651 } // for
652
[91b8a17]653 // Implement SFINAE; resolution errors are only errors if there aren't any non-erroneous resolutions
654 if ( candidates.empty() && ! errors.isEmpty() ) { throw errors; }
655
[a32b204]656 for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) {
657 Cost cvtCost = computeConversionCost( *withFunc, indexer );
658
659 PRINT(
660 ApplicationExpr *appExpr = dynamic_cast< ApplicationExpr* >( withFunc->expr );
661 assert( appExpr );
662 PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
663 assert( pointer );
664 FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
665 assert( function );
[6ed1d4b]666 std::cerr << "Case +++++++++++++" << std::endl;
667 std::cerr << "formals are:" << std::endl;
668 printAll( function->get_parameters(), std::cerr, 8 );
669 std::cerr << "actuals are:" << std::endl;
670 printAll( appExpr->get_args(), std::cerr, 8 );
671 std::cerr << "bindings are:" << std::endl;
672 withFunc->env.print( std::cerr, 8 );
673 std::cerr << "cost of conversion is:" << cvtCost << std::endl;
[7c64920]674 )
675 if ( cvtCost != Cost::infinity ) {
676 withFunc->cvtCost = cvtCost;
677 alternatives.push_back( *withFunc );
678 } // if
[a32b204]679 } // for
680 candidates.clear();
681 candidates.splice( candidates.end(), alternatives );
682
683 findMinCost( candidates.begin(), candidates.end(), std::back_inserter( alternatives ) );
684 }
685
686 bool isLvalue( Expression *expr ) {
687 for ( std::list< Type* >::const_iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) {
688 if ( !(*i)->get_isLvalue() ) return false;
689 } // for
690 return true;
691 }
692
693 void AlternativeFinder::visit( AddressExpr *addressExpr ) {
694 AlternativeFinder finder( indexer, env );
695 finder.find( addressExpr->get_arg() );
696 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
697 if ( isLvalue( i->expr ) ) {
698 alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) );
699 } // if
700 } // for
701 }
702
703 void AlternativeFinder::visit( CastExpr *castExpr ) {
704 for ( std::list< Type* >::iterator i = castExpr->get_results().begin(); i != castExpr->get_results().end(); ++i ) {
705 SymTab::validateType( *i, &indexer );
706 adjustExprType( *i, env, indexer );
707 } // for
708
709 AlternativeFinder finder( indexer, env );
710 finder.findWithAdjustment( castExpr->get_arg() );
711
712 AltList candidates;
713 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
714 AssertionSet needAssertions, haveAssertions;
715 OpenVarSet openVars;
716
717 // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a
718 // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results
719 // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast
720 // to.
721 int discardedValues = (*i).expr->get_results().size() - castExpr->get_results().size();
722 if ( discardedValues < 0 ) continue;
723 std::list< Type* >::iterator candidate_end = (*i).expr->get_results().begin();
724 std::advance( candidate_end, castExpr->get_results().size() );
[adcdd2f]725 // unification run for side-effects
726 unifyList( castExpr->get_results().begin(), castExpr->get_results().end(),
727 (*i).expr->get_results().begin(), candidate_end,
728 i->env, needAssertions, haveAssertions, openVars, indexer );
[a32b204]729 Cost thisCost = castCostList( (*i).expr->get_results().begin(), candidate_end,
[adcdd2f]730 castExpr->get_results().begin(), castExpr->get_results().end(),
731 indexer, i->env );
[a32b204]732 if ( thisCost != Cost::infinity ) {
733 // count one safe conversion for each value that is thrown away
734 thisCost += Cost( 0, 0, discardedValues );
735 CastExpr *newExpr = castExpr->clone();
736 newExpr->set_arg( i->expr->clone() );
737 candidates.push_back( Alternative( newExpr, i->env, i->cost, thisCost ) );
738 } // if
739 } // for
740
741 // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the
742 // cvtCost member to the cost member (since the old cost is now irrelevant). Thus, calling findMinCost twice
743 // selects first based on argument cost, then on conversion cost.
744 AltList minArgCost;
745 findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) );
746 findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) );
747 }
748
749 void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) {
750 AlternativeFinder funcFinder( indexer, env );
751 funcFinder.findWithAdjustment( memberExpr->get_aggregate() );
752
753 for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) {
754 if ( agg->expr->get_results().size() == 1 ) {
755 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_results().front() ) ) {
756 addAggMembers( structInst, agg->expr, agg->cost, memberExpr->get_member() );
757 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_results().front() ) ) {
758 addAggMembers( unionInst, agg->expr, agg->cost, memberExpr->get_member() );
759 } // if
760 } // if
761 } // for
762 }
763
764 void AlternativeFinder::visit( MemberExpr *memberExpr ) {
765 alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) );
766 }
767
768 void AlternativeFinder::visit( NameExpr *nameExpr ) {
769 std::list< DeclarationWithType* > declList;
770 indexer.lookupId( nameExpr->get_name(), declList );
771 PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; )
[0f19d763]772 for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
773 VariableExpr newExpr( *i, nameExpr->get_argName() );
[e04ef3a]774 newExpr.set_extension( nameExpr->get_extension() );
[0f19d763]775 alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
776 PRINT(
777 std::cerr << "decl is ";
778 (*i)->print( std::cerr );
779 std::cerr << std::endl;
780 std::cerr << "newExpr is ";
781 newExpr.print( std::cerr );
782 std::cerr << std::endl;
[7c64920]783 )
[0f19d763]784 renameTypes( alternatives.back().expr );
785 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
786 addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" );
787 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
788 addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" );
789 } // if
790 } // for
[a32b204]791 }
792
793 void AlternativeFinder::visit( VariableExpr *variableExpr ) {
794 alternatives.push_back( Alternative( variableExpr->clone(), env, Cost::zero ) );
795 }
796
797 void AlternativeFinder::visit( ConstantExpr *constantExpr ) {
798 alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) );
799 }
800
801 void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) {
802 if ( sizeofExpr->get_isType() ) {
803 alternatives.push_back( Alternative( sizeofExpr->clone(), env, Cost::zero ) );
804 } else {
805 // find all alternatives for the argument to sizeof
806 AlternativeFinder finder( indexer, env );
807 finder.find( sizeofExpr->get_expr() );
808 // find the lowest cost alternative among the alternatives, otherwise ambiguous
809 AltList winners;
810 findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
811 if ( winners.size() != 1 ) {
812 throw SemanticError( "Ambiguous expression in sizeof operand: ", sizeofExpr->get_expr() );
813 } // if
814 // return the lowest cost alternative for the argument
815 Alternative &choice = winners.front();
816 alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
[47534159]817 } // if
818 }
819
820 void AlternativeFinder::visit( AlignofExpr *alignofExpr ) {
821 if ( alignofExpr->get_isType() ) {
822 alternatives.push_back( Alternative( alignofExpr->clone(), env, Cost::zero ) );
823 } else {
824 // find all alternatives for the argument to sizeof
825 AlternativeFinder finder( indexer, env );
826 finder.find( alignofExpr->get_expr() );
827 // find the lowest cost alternative among the alternatives, otherwise ambiguous
828 AltList winners;
829 findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
830 if ( winners.size() != 1 ) {
831 throw SemanticError( "Ambiguous expression in alignof operand: ", alignofExpr->get_expr() );
832 } // if
833 // return the lowest cost alternative for the argument
834 Alternative &choice = winners.front();
835 alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
[a32b204]836 } // if
837 }
838
[2a4b088]839 template< typename StructOrUnionType >
840 void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {
841 std::list< Declaration* > members;
842 aggInst->lookup( name, members );
843 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
844 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
845 alternatives.push_back( Alternative( new OffsetofExpr( aggInst->clone(), dwt->clone() ), env, Cost::zero ) );
846 renameTypes( alternatives.back().expr );
847 } else {
848 assert( false );
849 }
850 }
851 }
[6ed1d4b]852
[2a4b088]853 void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
854 AlternativeFinder funcFinder( indexer, env );
855 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) {
856 addOffsetof( structInst, offsetofExpr->get_member() );
857 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( offsetofExpr->get_type() ) ) {
858 addOffsetof( unionInst, offsetofExpr->get_member() );
859 }
860 }
[6ed1d4b]861
[25a054f]862 void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) {
863 alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
[afc1045]864 }
865
866 void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) {
867 alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) );
[25a054f]868 }
869
[a32b204]870 void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) {
871 // assume no polymorphism
872 // assume no implicit conversions
873 assert( function->get_parameters().size() == 1 );
874 PRINT(
[6ed1d4b]875 std::cerr << "resolvAttr: funcDecl is ";
876 funcDecl->print( std::cerr );
877 std::cerr << " argType is ";
878 argType->print( std::cerr );
879 std::cerr << std::endl;
[7c64920]880 )
881 if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
882 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
883 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
884 alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() );
885 } // for
886 } // if
[a32b204]887 }
888
889 void AlternativeFinder::visit( AttrExpr *attrExpr ) {
890 // assume no 'pointer-to-attribute'
891 NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
892 assert( nameExpr );
893 std::list< DeclarationWithType* > attrList;
894 indexer.lookupId( nameExpr->get_name(), attrList );
895 if ( attrExpr->get_isType() || attrExpr->get_expr() ) {
896 for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
897 // check if the type is function
898 if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) {
899 // assume exactly one parameter
900 if ( function->get_parameters().size() == 1 ) {
901 if ( attrExpr->get_isType() ) {
902 resolveAttr( *i, function, attrExpr->get_type(), env );
903 } else {
904 AlternativeFinder finder( indexer, env );
905 finder.find( attrExpr->get_expr() );
906 for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) {
907 if ( choice->expr->get_results().size() == 1 ) {
908 resolveAttr(*i, function, choice->expr->get_results().front(), choice->env );
909 } // fi
910 } // for
911 } // if
912 } // if
913 } // if
914 } // for
915 } else {
916 for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
917 VariableExpr newExpr( *i );
918 alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
919 renameTypes( alternatives.back().expr );
920 } // for
921 } // if
922 }
923
924 void AlternativeFinder::visit( LogicalExpr *logicalExpr ) {
925 AlternativeFinder firstFinder( indexer, env );
926 firstFinder.findWithAdjustment( logicalExpr->get_arg1() );
927 for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
928 AlternativeFinder secondFinder( indexer, first->env );
929 secondFinder.findWithAdjustment( logicalExpr->get_arg2() );
930 for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
931 LogicalExpr *newExpr = new LogicalExpr( first->expr->clone(), second->expr->clone(), logicalExpr->get_isAnd() );
932 alternatives.push_back( Alternative( newExpr, second->env, first->cost + second->cost ) );
[d9a0e76]933 }
934 }
935 }
[51b73452]936
[a32b204]937 void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) {
938 AlternativeFinder firstFinder( indexer, env );
939 firstFinder.findWithAdjustment( conditionalExpr->get_arg1() );
940 for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
941 AlternativeFinder secondFinder( indexer, first->env );
942 secondFinder.findWithAdjustment( conditionalExpr->get_arg2() );
943 for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
944 AlternativeFinder thirdFinder( indexer, second->env );
945 thirdFinder.findWithAdjustment( conditionalExpr->get_arg3() );
946 for ( AltList::const_iterator third = thirdFinder.alternatives.begin(); third != thirdFinder.alternatives.end(); ++third ) {
947 OpenVarSet openVars;
948 AssertionSet needAssertions, haveAssertions;
949 Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost );
950 std::list< Type* > commonTypes;
951 if ( unifyList( second->expr->get_results().begin(), second->expr->get_results().end(), third->expr->get_results().begin(), third->expr->get_results().end(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonTypes ) ) {
952 ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() );
953 std::list< Type* >::const_iterator original = second->expr->get_results().begin();
954 std::list< Type* >::const_iterator commonType = commonTypes.begin();
955 for ( ; original != second->expr->get_results().end() && commonType != commonTypes.end(); ++original, ++commonType ) {
956 if ( *commonType ) {
957 newExpr->get_results().push_back( *commonType );
958 } else {
959 newExpr->get_results().push_back( (*original)->clone() );
960 } // if
961 } // for
962 newAlt.expr = newExpr;
963 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) );
964 } // if
965 } // for
966 } // for
967 } // for
968 }
969
970 void AlternativeFinder::visit( CommaExpr *commaExpr ) {
971 TypeEnvironment newEnv( env );
972 Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv );
973 AlternativeFinder secondFinder( indexer, newEnv );
974 secondFinder.findWithAdjustment( commaExpr->get_arg2() );
975 for ( AltList::const_iterator alt = secondFinder.alternatives.begin(); alt != secondFinder.alternatives.end(); ++alt ) {
976 alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt->expr->clone() ), alt->env, alt->cost ) );
977 } // for
978 delete newFirstArg;
979 }
980
981 void AlternativeFinder::visit( TupleExpr *tupleExpr ) {
982 std::list< AlternativeFinder > subExprAlternatives;
983 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) );
984 std::list< AltList > possibilities;
985 combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) );
986 for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) {
987 TupleExpr *newExpr = new TupleExpr;
988 makeExprList( *i, newExpr->get_exprs() );
989 for ( std::list< Expression* >::const_iterator resultExpr = newExpr->get_exprs().begin(); resultExpr != newExpr->get_exprs().end(); ++resultExpr ) {
990 for ( std::list< Type* >::const_iterator resultType = (*resultExpr)->get_results().begin(); resultType != (*resultExpr)->get_results().end(); ++resultType ) {
991 newExpr->get_results().push_back( (*resultType)->clone() );
992 } // for
993 } // for
994
995 TypeEnvironment compositeEnv;
996 simpleCombineEnvironments( i->begin(), i->end(), compositeEnv );
997 alternatives.push_back( Alternative( newExpr, compositeEnv, sumCost( *i ) ) );
998 } // for
[d9a0e76]999 }
[dc2e7e0]1000
1001 void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
1002 alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) );
1003 }
[51b73452]1004} // namespace ResolvExpr
[a32b204]1005
1006// Local Variables: //
1007// tab-width: 4 //
1008// mode: c++ //
1009// compile-command: "make install" //
1010// End: //
Note: See TracBrowser for help on using the repository browser.