source: src/ResolvExpr/AlternativeFinder.cc @ 87b5bf0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 87b5bf0 was 22cad76, checked in by Aaron Moss <a3moss@…>, 8 years ago

Second attempt to break resolver loop

  • Property mode set to 100644
File size: 42.0 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 Jun 13 16:13:54 2016
13// Update Count     : 25
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 "Designators/Processor.h"
41#include "Tuples/TupleAssignment.h"
42#include "Tuples/NameMatcher.h"
43#include "Common/utility.h"
44#include "InitTweak/InitTweak.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
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(
188                                std::cerr << "findSubExprs" << std::endl;
189                                printAlts( finder.alternatives, std::cerr );
190                        )
191                        *out++ = finder;
192                }
193        }
194
195        AlternativeFinder::AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env )
196                : indexer( indexer ), env( env ) {
197        }
198
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(
210                        std::cerr << "alternatives before prune:" << std::endl;
211                        printAlts( alternatives, std::cerr );
212                )
213                AltList::iterator oldBegin = alternatives.begin();
214                pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer );
215                if ( alternatives.begin() == oldBegin ) {
216                        std::ostringstream stream;
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 );
223                        throw SemanticError( stream.str() );
224                }
225                alternatives.erase( oldBegin, alternatives.end() );
226                PRINT(
227                        std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
228                )
229        }
230
231        void AlternativeFinder::findWithAdjustment( Expression *expr ) {
232                find( expr, true );
233        }
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                }
247        }
248
249        void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) {
250                alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) );
251        }
252
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(
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 );
271                        )
272                        std::list< DeclarationWithType* >::iterator startFormal = formal;
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(
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 );
288                                )
289                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
290                                PRINT(
291                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
292                                )
293
294                                if ( newCost == Cost::infinity ) {
295                                        return newCost;
296                                }
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
322                }
323                if ( formal != formals.end() ) {
324                        return Cost::infinity;
325                }
326
327                for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) {
328                        PRINT(
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 );
333                                )
334                                Cost newCost = conversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env );
335                        PRINT(
336                                std::cerr << std::endl << "cost of conversion is " << newCost << std::endl;
337                                )
338                                if ( newCost == Cost::infinity ) {
339                                        return newCost;
340                                }
341                        convCost += newCost;
342
343                        convCost += Cost( 0, polyCost( assert->second.formalType, alt.env, indexer ) + polyCost( assert->second.actualType, alt.env, indexer ), 0 );
344                }
345
346                return convCost;
347        }
348
349        /// Adds type variables to the open variable set and marks their assertions
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                        }
356///     needAssertions.insert( needAssertions.end(), (*tyvar)->get_assertions().begin(), (*tyvar)->get_assertions().end() );
357                }
358        }
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;
388                                )
389                                if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
390                                        return false;
391                                }
392                                formal++;
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;
411        }
412
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;
415       
416        static const int recursionLimit = 10;  ///< Limit to depth of recursion satisfaction
417        static const unsigned recursionParentLimit = 1;  ///< Limit to the number of times an assertion can recursively use itself
418
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                }
425        }
426       
427        template< typename ForwardIterator, typename OutputIterator >
428        void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, const AssertionParentSet &needParents, 
429                                                 int level, const SymTab::Indexer &indexer, OutputIterator out ) {
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 );
441                                )
442                                inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, needParents, level+1, indexer, out );
443                                return;
444                        }
445                }
446
447                ForwardIterator cur = begin++;
448                if ( ! cur->second ) {
449                        inferRecursive( begin, end, newAlt, openVars, decls, newNeed, needParents, level, indexer, out );
450                }
451                DeclarationWithType *curDecl = cur->first;
452                PRINT(
453                        std::cerr << "inferRecursive: assertion is ";
454                        curDecl->print( std::cerr );
455                        std::cerr << std::endl;
456                )
457                std::list< DeclarationWithType* > candidates;
458                decls.lookupId( curDecl->get_name(), candidates );
459///   if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; }
460                for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) {
461                        PRINT(
462                                std::cerr << "inferRecursive: candidate is ";
463                                (*candidate)->print( std::cerr );
464                                std::cerr << std::endl;
465                        )
466                       
467                        AssertionSet newHave, newerNeed( newNeed );
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;
479                        )
480                        if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
481                                PRINT(
482                                        std::cerr << "success!" << std::endl;
483                                )
484                                SymTab::Indexer newDecls( decls );
485                                addToIndexer( newHave, newDecls );
486                                Alternative newerAlt( newAlt );
487                                newerAlt.env = newEnv;
488                                assert( (*candidate)->get_uniqueId() );
489                                DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) );
490                                AssertionParentSet newNeedParents( needParents );
491                                // skip repeatingly-self-recursive assertion satisfaction
492                                if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue;
493                                Expression *varExpr = new VariableExpr( candDecl );
494                                deleteAll( varExpr->get_results() );
495                                varExpr->get_results().clear();
496                                varExpr->get_results().push_front( adjType->clone() );
497                                PRINT(
498                                        std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " ";
499                                        curDecl->print( std::cerr );
500                                        std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " ";
501                                        (*candidate)->print( std::cerr );
502                                        std::cerr << std::endl;
503                                )
504                                ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
505                                // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
506                                appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
507                                inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, newNeedParents, level, indexer, out );
508                        } else {
509                                delete adjType;
510                        }
511                }
512        }
513
514        template< typename OutputIterator >
515        void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
516//      PRINT(
517//          std::cerr << "inferParameters: assertions needed are" << std::endl;
518//          printAll( need, std::cerr, 8 );
519//          )
520                SymTab::Indexer decls( indexer );
521                PRINT(
522                        std::cerr << "============= original indexer" << std::endl;
523                        indexer.print( std::cerr );
524                        std::cerr << "============= new indexer" << std::endl;
525                        decls.print( std::cerr );
526                )
527                addToIndexer( have, decls );
528                AssertionSet newNeed;
529                AssertionParentSet needParents;
530                inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, needParents, 0, indexer, out );
531//      PRINT(
532//          std::cerr << "declaration 14 is ";
533//          Declaration::declFromId
534//          *out++ = newAlt;
535//          )
536        }
537
538        template< typename OutputIterator >
539        void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, AltList &actualAlt, OutputIterator out ) {
540                OpenVarSet openVars;
541                AssertionSet resultNeed, resultHave;
542                TypeEnvironment resultEnv;
543                makeUnifiableVars( funcType, openVars, resultNeed );
544                if ( instantiateFunction( funcType->get_parameters(), actualAlt, funcType->get_isVarArgs(), openVars, resultEnv, resultNeed, resultHave ) ) {
545                        ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
546                        Alternative newAlt( appExpr, resultEnv, sumCost( actualAlt ) );
547                        makeExprList( actualAlt, appExpr->get_args() );
548                        PRINT(
549                                std::cerr << "need assertions:" << std::endl;
550                                printAssertionSet( resultNeed, std::cerr, 8 );
551                        )
552                        inferParameters( resultNeed, resultHave, newAlt, openVars, out );
553                }
554        }
555
556        void AlternativeFinder::visit( UntypedExpr *untypedExpr ) {
557                bool doneInit = false;
558                AlternativeFinder funcOpFinder( indexer, env );
559
560                AlternativeFinder funcFinder( indexer, env );
561
562                {
563                        std::string fname = InitTweak::getFunctionName( untypedExpr );
564                        if ( fname == "&&" ) {
565                                VoidType v = Type::Qualifiers();                // resolve to type void *
566                                PointerType pt( Type::Qualifiers(), v.clone() );
567                                UntypedExpr *vexpr = untypedExpr->clone();
568                                vexpr->get_results().push_front( pt.clone() );
569                                alternatives.push_back( Alternative( vexpr, env, Cost()) );
570                                return;
571                        }
572                }
573
574                funcFinder.findWithAdjustment( untypedExpr->get_function() );
575                std::list< AlternativeFinder > argAlternatives;
576                findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), back_inserter( argAlternatives ) );
577
578                std::list< AltList > possibilities;
579                combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
580
581                Tuples::TupleAssignSpotter tassign( this );
582                if ( tassign.isTupleAssignment( untypedExpr, possibilities ) ) {
583                        // take care of possible tuple assignments, or discard expression
584                        return;
585                } // else ...
586
587                AltList candidates;
588                SemanticError errors;
589
590                for ( AltList::const_iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) {
591                        try {
592                                PRINT(
593                                        std::cerr << "working on alternative: " << std::endl;
594                                        func->print( std::cerr, 8 );
595                                )
596                                // check if the type is pointer to function
597                                PointerType *pointer;
598                                if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) {
599                                        if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
600                                                for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
601                                                        // XXX
602                                                        //Designators::check_alternative( function, *actualAlt );
603                                                        makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
604                                                }
605                                        } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( pointer->get_base() ) ) {
606                                                EqvClass eqvClass;
607                                                if ( func->env.lookup( typeInst->get_name(), eqvClass ) && eqvClass.type ) {
608                                                        if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) {
609                                                                for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
610                                                                        makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
611                                                                } // for
612                                                        } // if
613                                                } // if
614                                        } // if
615                                } else {
616                                        // seek a function operator that's compatible
617                                        if ( ! doneInit ) {
618                                                doneInit = true;
619                                                NameExpr *opExpr = new NameExpr( "?()" );
620                                                try {
621                                                        funcOpFinder.findWithAdjustment( opExpr );
622                                                } catch( SemanticError &e ) {
623                                                        // it's ok if there aren't any defined function ops
624                                                }
625                                                PRINT(
626                                                        std::cerr << "known function ops:" << std::endl;
627                                                        printAlts( funcOpFinder.alternatives, std::cerr, 8 );
628                                                )
629                                        }
630
631                                        for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) {
632                                                // check if the type is pointer to function
633                                                PointerType *pointer;
634                                                if ( funcOp->expr->get_results().size() == 1
635                                                        && ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_results().front() ) ) ) {
636                                                        if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
637                                                                for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
638                                                                        AltList currentAlt;
639                                                                        currentAlt.push_back( *func );
640                                                                        currentAlt.insert( currentAlt.end(), actualAlt->begin(), actualAlt->end() );
641                                                                        makeFunctionAlternatives( *funcOp, function, currentAlt, std::back_inserter( candidates ) );
642                                                                } // for
643                                                        } // if
644                                                } // if
645                                        } // for
646                                } // if
647                        } catch ( SemanticError &e ) {
648                                errors.append( e );
649                        }
650                } // for
651
652                // Implement SFINAE; resolution errors are only errors if there aren't any non-erroneous resolutions
653                if ( candidates.empty() && ! errors.isEmpty() ) { throw errors; }
654
655                for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) {
656                        Cost cvtCost = computeConversionCost( *withFunc, indexer );
657
658                        PRINT(
659                                ApplicationExpr *appExpr = dynamic_cast< ApplicationExpr* >( withFunc->expr );
660                                assert( appExpr );
661                                PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
662                                assert( pointer );
663                                FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
664                                assert( function );
665                                std::cerr << "Case +++++++++++++" << std::endl;
666                                std::cerr << "formals are:" << std::endl;
667                                printAll( function->get_parameters(), std::cerr, 8 );
668                                std::cerr << "actuals are:" << std::endl;
669                                printAll( appExpr->get_args(), std::cerr, 8 );
670                                std::cerr << "bindings are:" << std::endl;
671                                withFunc->env.print( std::cerr, 8 );
672                                std::cerr << "cost of conversion is:" << cvtCost << std::endl;
673                        )
674                        if ( cvtCost != Cost::infinity ) {
675                                withFunc->cvtCost = cvtCost;
676                                alternatives.push_back( *withFunc );
677                        } // if
678                } // for
679                candidates.clear();
680                candidates.splice( candidates.end(), alternatives );
681
682                findMinCost( candidates.begin(), candidates.end(), std::back_inserter( alternatives ) );
683        }
684
685        bool isLvalue( Expression *expr ) {
686                for ( std::list< Type* >::const_iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) {
687                        if ( !(*i)->get_isLvalue() ) return false;
688                } // for
689                return true;
690        }
691
692        void AlternativeFinder::visit( AddressExpr *addressExpr ) {
693                AlternativeFinder finder( indexer, env );
694                finder.find( addressExpr->get_arg() );
695                for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
696                        if ( isLvalue( i->expr ) ) {
697                                alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) );
698                        } // if
699                } // for
700        }
701
702        void AlternativeFinder::visit( CastExpr *castExpr ) {
703                for ( std::list< Type* >::iterator i = castExpr->get_results().begin(); i != castExpr->get_results().end(); ++i ) {
704                        SymTab::validateType( *i, &indexer );
705                        adjustExprType( *i, env, indexer );
706                } // for
707
708                AlternativeFinder finder( indexer, env );
709                finder.findWithAdjustment( castExpr->get_arg() );
710
711                AltList candidates;
712                for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
713                        AssertionSet needAssertions, haveAssertions;
714                        OpenVarSet openVars;
715
716                        // It's possible that a cast can throw away some values in a multiply-valued expression.  (An example is a
717                        // cast-to-void, which casts from one value to zero.)  Figure out the prefix of the subexpression results
718                        // that are cast directly.  The candidate is invalid if it has fewer results than there are types to cast
719                        // to.
720                        int discardedValues = (*i).expr->get_results().size() - castExpr->get_results().size();
721                        if ( discardedValues < 0 ) continue;
722                        std::list< Type* >::iterator candidate_end = (*i).expr->get_results().begin();
723                        std::advance( candidate_end, castExpr->get_results().size() );
724                        // unification run for side-effects
725                        unifyList( castExpr->get_results().begin(), castExpr->get_results().end(),
726                                           (*i).expr->get_results().begin(), candidate_end,
727                                   i->env, needAssertions, haveAssertions, openVars, indexer );
728                        Cost thisCost = castCostList( (*i).expr->get_results().begin(), candidate_end,
729                                                                                  castExpr->get_results().begin(), castExpr->get_results().end(),
730                                                                                  indexer, i->env );
731                        if ( thisCost != Cost::infinity ) {
732                                // count one safe conversion for each value that is thrown away
733                                thisCost += Cost( 0, 0, discardedValues );
734                                CastExpr *newExpr = castExpr->clone();
735                                newExpr->set_arg( i->expr->clone() );
736                                candidates.push_back( Alternative( newExpr, i->env, i->cost, thisCost ) );
737                        } // if
738                } // for
739
740                // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the
741                // cvtCost member to the cost member (since the old cost is now irrelevant).  Thus, calling findMinCost twice
742                // selects first based on argument cost, then on conversion cost.
743                AltList minArgCost;
744                findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) );
745                findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) );
746        }
747
748        void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) {
749                AlternativeFinder funcFinder( indexer, env );
750                funcFinder.findWithAdjustment( memberExpr->get_aggregate() );
751
752                for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) {
753                        if ( agg->expr->get_results().size() == 1 ) {
754                                if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_results().front() ) ) {
755                                        addAggMembers( structInst, agg->expr, agg->cost, memberExpr->get_member() );
756                                } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_results().front() ) ) {
757                                        addAggMembers( unionInst, agg->expr, agg->cost, memberExpr->get_member() );
758                                } // if
759                        } // if
760                } // for
761        }
762
763        void AlternativeFinder::visit( MemberExpr *memberExpr ) {
764                alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) );
765        }
766
767        void AlternativeFinder::visit( NameExpr *nameExpr ) {
768                std::list< DeclarationWithType* > declList;
769                indexer.lookupId( nameExpr->get_name(), declList );
770                PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; )
771                for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
772                        VariableExpr newExpr( *i, nameExpr->get_argName() );
773                        newExpr.set_extension( nameExpr->get_extension() );
774                        alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
775                        PRINT(
776                                std::cerr << "decl is ";
777                                (*i)->print( std::cerr );
778                                std::cerr << std::endl;
779                                std::cerr << "newExpr is ";
780                                newExpr.print( std::cerr );
781                                std::cerr << std::endl;
782                        )
783                        renameTypes( alternatives.back().expr );
784                        if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
785                                addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" );
786                        } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
787                                addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" );
788                        } // if
789                } // for
790        }
791
792        void AlternativeFinder::visit( VariableExpr *variableExpr ) {
793                alternatives.push_back( Alternative( variableExpr->clone(), env, Cost::zero ) );
794        }
795
796        void AlternativeFinder::visit( ConstantExpr *constantExpr ) {
797                alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) );
798        }
799
800        void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) {
801                if ( sizeofExpr->get_isType() ) {
802                        alternatives.push_back( Alternative( sizeofExpr->clone(), env, Cost::zero ) );
803                } else {
804                        // find all alternatives for the argument to sizeof
805                        AlternativeFinder finder( indexer, env );
806                        finder.find( sizeofExpr->get_expr() );
807                        // find the lowest cost alternative among the alternatives, otherwise ambiguous
808                        AltList winners;
809                        findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
810                        if ( winners.size() != 1 ) {
811                                throw SemanticError( "Ambiguous expression in sizeof operand: ", sizeofExpr->get_expr() );
812                        } // if
813                        // return the lowest cost alternative for the argument
814                        Alternative &choice = winners.front();
815                        alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
816                } // if
817        }
818
819        void AlternativeFinder::visit( AlignofExpr *alignofExpr ) {
820                if ( alignofExpr->get_isType() ) {
821                        alternatives.push_back( Alternative( alignofExpr->clone(), env, Cost::zero ) );
822                } else {
823                        // find all alternatives for the argument to sizeof
824                        AlternativeFinder finder( indexer, env );
825                        finder.find( alignofExpr->get_expr() );
826                        // find the lowest cost alternative among the alternatives, otherwise ambiguous
827                        AltList winners;
828                        findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
829                        if ( winners.size() != 1 ) {
830                                throw SemanticError( "Ambiguous expression in alignof operand: ", alignofExpr->get_expr() );
831                        } // if
832                        // return the lowest cost alternative for the argument
833                        Alternative &choice = winners.front();
834                        alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
835                } // if
836        }
837
838        template< typename StructOrUnionType >
839        void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {
840                std::list< Declaration* > members;
841                aggInst->lookup( name, members );
842                for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
843                        if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
844                                alternatives.push_back( Alternative( new OffsetofExpr( aggInst->clone(), dwt->clone() ), env, Cost::zero ) );
845                                renameTypes( alternatives.back().expr );
846                        } else {
847                                assert( false );
848                        }
849                }
850        }
851
852        void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
853                AlternativeFinder funcFinder( indexer, env );
854                if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) {
855                        addOffsetof( structInst, offsetofExpr->get_member() );
856                } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( offsetofExpr->get_type() ) ) {
857                        addOffsetof( unionInst, offsetofExpr->get_member() );
858                }
859        }
860
861        void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) {
862                alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
863        }
864
865        void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) {
866                alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) );
867        }
868
869        void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) {
870                // assume no polymorphism
871                // assume no implicit conversions
872                assert( function->get_parameters().size() == 1 );
873                PRINT(
874                        std::cerr << "resolvAttr: funcDecl is ";
875                        funcDecl->print( std::cerr );
876                        std::cerr << " argType is ";
877                        argType->print( std::cerr );
878                        std::cerr << std::endl;
879                )
880                if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
881                        alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
882                        for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
883                                alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() );
884                        } // for
885                } // if
886        }
887
888        void AlternativeFinder::visit( AttrExpr *attrExpr ) {
889                // assume no 'pointer-to-attribute'
890                NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
891                assert( nameExpr );
892                std::list< DeclarationWithType* > attrList;
893                indexer.lookupId( nameExpr->get_name(), attrList );
894                if ( attrExpr->get_isType() || attrExpr->get_expr() ) {
895                        for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
896                                // check if the type is function
897                                if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) {
898                                        // assume exactly one parameter
899                                        if ( function->get_parameters().size() == 1 ) {
900                                                if ( attrExpr->get_isType() ) {
901                                                        resolveAttr( *i, function, attrExpr->get_type(), env );
902                                                } else {
903                                                        AlternativeFinder finder( indexer, env );
904                                                        finder.find( attrExpr->get_expr() );
905                                                        for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) {
906                                                                if ( choice->expr->get_results().size() == 1 ) {
907                                                                        resolveAttr(*i, function, choice->expr->get_results().front(), choice->env );
908                                                                } // fi
909                                                        } // for
910                                                } // if
911                                        } // if
912                                } // if
913                        } // for
914                } else {
915                        for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
916                                VariableExpr newExpr( *i );
917                                alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
918                                renameTypes( alternatives.back().expr );
919                        } // for
920                } // if
921        }
922
923        void AlternativeFinder::visit( LogicalExpr *logicalExpr ) {
924                AlternativeFinder firstFinder( indexer, env );
925                firstFinder.findWithAdjustment( logicalExpr->get_arg1() );
926                for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
927                        AlternativeFinder secondFinder( indexer, first->env );
928                        secondFinder.findWithAdjustment( logicalExpr->get_arg2() );
929                        for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
930                                LogicalExpr *newExpr = new LogicalExpr( first->expr->clone(), second->expr->clone(), logicalExpr->get_isAnd() );
931                                alternatives.push_back( Alternative( newExpr, second->env, first->cost + second->cost ) );
932                        }
933                }
934        }
935
936        void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) {
937                AlternativeFinder firstFinder( indexer, env );
938                firstFinder.findWithAdjustment( conditionalExpr->get_arg1() );
939                for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
940                        AlternativeFinder secondFinder( indexer, first->env );
941                        secondFinder.findWithAdjustment( conditionalExpr->get_arg2() );
942                        for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
943                                AlternativeFinder thirdFinder( indexer, second->env );
944                                thirdFinder.findWithAdjustment( conditionalExpr->get_arg3() );
945                                for ( AltList::const_iterator third = thirdFinder.alternatives.begin(); third != thirdFinder.alternatives.end(); ++third ) {
946                                        OpenVarSet openVars;
947                                        AssertionSet needAssertions, haveAssertions;
948                                        Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost );
949                                        std::list< Type* > commonTypes;
950                                        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 ) ) {
951                                                ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() );
952                                                std::list< Type* >::const_iterator original = second->expr->get_results().begin();
953                                                std::list< Type* >::const_iterator commonType = commonTypes.begin();
954                                                for ( ; original != second->expr->get_results().end() && commonType != commonTypes.end(); ++original, ++commonType ) {
955                                                        if ( *commonType ) {
956                                                                newExpr->get_results().push_back( *commonType );
957                                                        } else {
958                                                                newExpr->get_results().push_back( (*original)->clone() );
959                                                        } // if
960                                                } // for
961                                                newAlt.expr = newExpr;
962                                                inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) );
963                                        } // if
964                                } // for
965                        } // for
966                } // for
967        }
968
969        void AlternativeFinder::visit( CommaExpr *commaExpr ) {
970                TypeEnvironment newEnv( env );
971                Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv );
972                AlternativeFinder secondFinder( indexer, newEnv );
973                secondFinder.findWithAdjustment( commaExpr->get_arg2() );
974                for ( AltList::const_iterator alt = secondFinder.alternatives.begin(); alt != secondFinder.alternatives.end(); ++alt ) {
975                        alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt->expr->clone() ), alt->env, alt->cost ) );
976                } // for
977                delete newFirstArg;
978        }
979
980        void AlternativeFinder::visit( TupleExpr *tupleExpr ) {
981                std::list< AlternativeFinder > subExprAlternatives;
982                findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) );
983                std::list< AltList > possibilities;
984                combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) );
985                for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) {
986                        TupleExpr *newExpr = new TupleExpr;
987                        makeExprList( *i, newExpr->get_exprs() );
988                        for ( std::list< Expression* >::const_iterator resultExpr = newExpr->get_exprs().begin(); resultExpr != newExpr->get_exprs().end(); ++resultExpr ) {
989                                for ( std::list< Type* >::const_iterator resultType = (*resultExpr)->get_results().begin(); resultType != (*resultExpr)->get_results().end(); ++resultType ) {
990                                        newExpr->get_results().push_back( (*resultType)->clone() );
991                                } // for
992                        } // for
993
994                        TypeEnvironment compositeEnv;
995                        simpleCombineEnvironments( i->begin(), i->end(), compositeEnv );
996                        alternatives.push_back( Alternative( newExpr, compositeEnv, sumCost( *i ) ) );
997                } // for
998        }
999
1000        void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
1001                alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) );
1002        }
1003} // namespace ResolvExpr
1004
1005// Local Variables: //
1006// tab-width: 4 //
1007// mode: c++ //
1008// compile-command: "make install" //
1009// End: //
Note: See TracBrowser for help on using the repository browser.