source: src/ResolvExpr/AlternativeFinder.cc@ 3b58d91

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

add AST nodes TupleIndexExpr, MemberTupleExpr, MassAssignExpr, and MultipleAssignExpr, modify parser to produce nodes for field tuples, modify UntypedMemberExpr to contain a list of members

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