Changeset f5478c8 for src/ResolvExpr


Ignore:
Timestamp:
Nov 24, 2017, 8:57:00 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2b716ec
Parents:
50abab9 (diff), 3de176d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/ResolvExpr
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Alternative.cc

    r50abab9 rf5478c8  
    1818#include <ostream>                       // for operator<<, ostream, basic_o...
    1919#include <string>                        // for operator<<, char_traits, string
     20#include <utility>                       // for move
    2021
    2122#include "Common/utility.h"              // for maybeClone
     
    8182                os << std::endl;
    8283        }
     84
     85        void splice( AltList& dst, AltList& src ) {
     86                dst.reserve( dst.size() + src.size() );
     87                for ( Alternative& alt : src ) {
     88                        dst.push_back( std::move(alt) );
     89                }
     90                src.clear();
     91        }
     92
     93        void spliceBegin( AltList& dst, AltList& src ) {
     94                splice( src, dst );
     95                dst.swap( src );
     96        }
     97
    8398} // namespace ResolvExpr
    8499
  • src/ResolvExpr/Alternative.h

    r50abab9 rf5478c8  
    1717
    1818#include <iosfwd>             // for ostream
    19 #include <list>               // for list
     19#include <vector>             // for vector
    2020
    2121#include "Cost.h"             // for Cost
     
    2525
    2626namespace ResolvExpr {
    27         struct Alternative;
    28 
    29         typedef std::list< Alternative > AltList;
    30 
    3127        struct Alternative {
    3228                Alternative();
     
    4137                void print( std::ostream &os, Indenter indent = {} ) const;
    4238
     39                /// Returns the stored expression, but released from management of this Alternative
     40                Expression* release_expr() {
     41                        Expression* tmp = expr;
     42                        expr = nullptr;
     43                        return tmp;
     44                }
     45
    4346                Cost cost;
    4447                Cost cvtCost;
     
    4649                TypeEnvironment env;
    4750        };
     51
     52        typedef std::vector< Alternative > AltList;
     53
     54        /// Moves all elements from src to the end of dst
     55        void splice( AltList& dst, AltList& src );
     56
     57        /// Moves all elements from src to the beginning of dst
     58        void spliceBegin( AltList& dst, AltList& src );
    4859} // namespace ResolvExpr
    4960
  • src/ResolvExpr/AlternativeFinder.cc

    r50abab9 rf5478c8  
    1616#include <algorithm>               // for copy
    1717#include <cassert>                 // for strict_dynamic_cast, assert, assertf
     18#include <cstddef>                 // for size_t
    1819#include <iostream>                // for operator<<, cerr, ostream, endl
    1920#include <iterator>                // for back_insert_iterator, back_inserter
    2021#include <list>                    // for _List_iterator, list, _List_const_...
    2122#include <map>                     // for _Rb_tree_iterator, map, _Rb_tree_c...
    22 #include <memory>                  // for allocator_traits<>::value_type
     23#include <memory>                  // for allocator_traits<>::value_type, unique_ptr
    2324#include <utility>                 // for pair
    2425#include <vector>                  // for vector
     
    2930#include "Common/utility.h"        // for deleteAll, printAll, CodeLocation
    3031#include "Cost.h"                  // for Cost, Cost::zero, operator<<, Cost...
     32#include "ExplodedActual.h"        // for ExplodedActual
    3133#include "InitTweak/InitTweak.h"   // for getFunctionName
    3234#include "RenameVars.h"            // for RenameVars, global_renamer
     
    5052#define PRINT( text ) if ( resolvep ) { text }
    5153//#define DEBUG_COST
     54
     55using std::move;
     56
     57/// copies any copyable type
     58template<typename T>
     59T copy(const T& x) { return x; }
    5260
    5361namespace ResolvExpr {
     
    179187                expr->accept( *this );
    180188                if ( failFast && alternatives.empty() ) {
     189                        PRINT(
     190                                std::cerr << "No reasonable alternatives for expression " << expr << std::endl;
     191                        )
    181192                        throw SemanticError( "No reasonable alternatives for expression ", expr );
    182193                }
     
    187198                                printAlts( alternatives, std::cerr );
    188199                        )
    189                         AltList::iterator oldBegin = alternatives.begin();
    190                         pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ) );
    191                         if ( failFast && alternatives.begin() == oldBegin ) {
     200                        AltList pruned;
     201                        pruneAlternatives( alternatives.begin(), alternatives.end(), back_inserter( pruned ) );
     202                        if ( failFast && pruned.empty() ) {
    192203                                std::ostringstream stream;
    193204                                AltList winners;
     
    199210                                throw SemanticError( stream.str() );
    200211                        }
    201                         alternatives.erase( oldBegin, alternatives.end() );
     212                        alternatives = move(pruned);
    202213                        PRINT(
    203214                                std::cerr << "there are " << oldsize << " alternatives before elimination" << std::endl;
     
    571582        /// State to iteratively build a match of parameter expressions to arguments
    572583        struct ArgPack {
    573                 AltList actuals;                 ///< Arguments included in this pack
    574                 TypeEnvironment env;             ///< Environment for this pack
    575                 AssertionSet need;               ///< Assertions outstanding for this pack
    576                 AssertionSet have;               ///< Assertions found for this pack
    577                 OpenVarSet openVars;             ///< Open variables for this pack
    578                 unsigned nextArg;                ///< Index of next argument in arguments list
    579                 std::vector<Alternative> expls;  ///< Exploded actuals left over from last match
    580                 unsigned nextExpl;               ///< Index of next exploded alternative to use
    581                 std::vector<unsigned> tupleEls;  /// Number of elements in current tuple element(s)
     584                std::size_t parent;                ///< Index of parent pack
     585                std::unique_ptr<Expression> expr;  ///< The argument stored here
     586                Cost cost;                         ///< The cost of this argument
     587                TypeEnvironment env;               ///< Environment for this pack
     588                AssertionSet need;                 ///< Assertions outstanding for this pack
     589                AssertionSet have;                 ///< Assertions found for this pack
     590                OpenVarSet openVars;               ///< Open variables for this pack
     591                unsigned nextArg;                  ///< Index of next argument in arguments list
     592                unsigned tupleStart;               ///< Number of tuples that start at this index
     593                unsigned nextExpl;                 ///< Index of next exploded element
     594                unsigned explAlt;                  ///< Index of alternative for nextExpl > 0
     595
     596                ArgPack()
     597                        : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0),
     598
     599                          tupleStart(0), nextExpl(0), explAlt(0) {}
    582600
    583601                ArgPack(const TypeEnvironment& env, const AssertionSet& need, const AssertionSet& have,
    584602                                const OpenVarSet& openVars)
    585                         : actuals(), env(env), need(need), have(have), openVars(openVars), nextArg(0),
    586                           expls(), nextExpl(0), tupleEls() {}
    587 
    588                 /// Starts a new tuple expression
    589                 void beginTuple() {
    590                         if ( ! tupleEls.empty() ) ++tupleEls.back();
    591                         tupleEls.push_back(0);
     603                        : parent(0), expr(), cost(Cost::zero), env(env), need(need), have(have),
     604                          openVars(openVars), nextArg(0), tupleStart(0), nextExpl(0), explAlt(0) {}
     605
     606                ArgPack(std::size_t parent, Expression* expr, TypeEnvironment&& env, AssertionSet&& need,
     607                                AssertionSet&& have, OpenVarSet&& openVars, unsigned nextArg,
     608                                unsigned tupleStart = 0, Cost cost = Cost::zero, unsigned nextExpl = 0,
     609                                unsigned explAlt = 0 )
     610                        : parent(parent), expr(expr->clone()), cost(cost), env(move(env)), need(move(need)),
     611                          have(move(have)), openVars(move(openVars)), nextArg(nextArg), tupleStart(tupleStart),
     612                          nextExpl(nextExpl), explAlt(explAlt) {}
     613
     614                ArgPack(const ArgPack& o, TypeEnvironment&& env, AssertionSet&& need, AssertionSet&& have,
     615                                OpenVarSet&& openVars, unsigned nextArg, Cost added )
     616                        : parent(o.parent), expr(o.expr ? o.expr->clone() : nullptr), cost(o.cost + added),
     617                          env(move(env)), need(move(need)), have(move(have)), openVars(move(openVars)),
     618                          nextArg(nextArg), tupleStart(o.tupleStart), nextExpl(0), explAlt(0) {}
     619
     620                /// true iff this pack is in the middle of an exploded argument
     621                bool hasExpl() const { return nextExpl > 0; }
     622
     623                /// Gets the list of exploded alternatives for this pack
     624                const ExplodedActual& getExpl( const ExplodedArgs& args ) const {
     625                        return args[nextArg-1][explAlt];
    592626                }
    593627
    594628                /// Ends a tuple expression, consolidating the appropriate actuals
    595                 void endTuple() {
    596                         // set up new Tuple alternative
     629                void endTuple( const std::vector<ArgPack>& packs ) {
     630                        // add all expressions in tuple to list, summing cost
    597631                        std::list<Expression*> exprs;
    598                         Cost cost = Cost::zero;
    599 
    600                         // transfer elements into alternative
    601                         for (unsigned i = 0; i < tupleEls.back(); ++i) {
    602                                 exprs.push_front( actuals.back().expr );
    603                                 actuals.back().expr = nullptr;
    604                                 cost += actuals.back().cost;
    605                                 actuals.pop_back();
    606                         }
    607                         tupleEls.pop_back();
    608 
    609                         // build new alternative
    610                         actuals.emplace_back( new TupleExpr( exprs ), this->env, cost );
    611                 }
    612 
    613                 /// Clones and adds an actual, returns this
    614                 ArgPack& withArg( Expression* expr, Cost cost = Cost::zero ) {
    615                         actuals.emplace_back( expr->clone(), this->env, cost );
    616                         if ( ! tupleEls.empty() ) ++tupleEls.back();
    617                         return *this;
     632                        const ArgPack* pack = this;
     633                        if ( expr ) { exprs.push_front( expr.release() ); }
     634                        while ( pack->tupleStart == 0 ) {
     635                                pack = &packs[pack->parent];
     636                                exprs.push_front( pack->expr->clone() );
     637                                cost += pack->cost;
     638                        }
     639                        // reset pack to appropriate tuple
     640                        expr.reset( new TupleExpr( exprs ) );
     641                        tupleStart = pack->tupleStart - 1;
     642                        parent = pack->parent;
    618643                }
    619644        };
     
    621646        /// Instantiates an argument to match a formal, returns false if no results left
    622647        bool instantiateArgument( Type* formalType, Initializer* initializer,
    623                         const std::vector< AlternativeFinder >& args,
    624                         std::vector<ArgPack>& results, std::vector<ArgPack>& nextResults,
    625                         const SymTab::Indexer& indexer ) {
     648                        const ExplodedArgs& args, std::vector<ArgPack>& results, std::size_t& genStart,
     649                        const SymTab::Indexer& indexer, unsigned nTuples = 0 ) {
    626650                if ( TupleType* tupleType = dynamic_cast<TupleType*>( formalType ) ) {
    627651                        // formalType is a TupleType - group actuals into a TupleExpr
    628                         for ( ArgPack& result : results ) { result.beginTuple(); }
     652                        ++nTuples;
    629653                        for ( Type* type : *tupleType ) {
    630654                                // xxx - dropping initializer changes behaviour from previous, but seems correct
    631                                 if ( ! instantiateArgument( type, nullptr, args, results, nextResults, indexer ) )
     655                                if ( ! instantiateArgument(
     656                                                type, nullptr, args, results, genStart, indexer, nTuples ) )
    632657                                        return false;
    633                         }
    634                         for ( ArgPack& result : results ) { result.endTuple(); }
     658                                nTuples = 0;
     659                        }
     660                        // re-consititute tuples for final generation
     661                        for ( auto i = genStart; i < results.size(); ++i ) {
     662                                results[i].endTuple( results );
     663                        }
    635664                        return true;
    636665                } else if ( TypeInstType* ttype = Tuples::isTtype( formalType ) ) {
    637666                        // formalType is a ttype, consumes all remaining arguments
    638667                        // xxx - mixing default arguments with variadic??
    639                         std::vector<ArgPack> finalResults{};  /// list of completed tuples
    640                         // start tuples
    641                         for ( ArgPack& result : results ) {
    642                                 result.beginTuple();
    643 
    644                                 // use rest of exploded tuple if present
    645                                 while ( result.nextExpl < result.expls.size() ) {
    646                                         const Alternative& actual = result.expls[result.nextExpl];
    647                                         result.env.addActual( actual.env, result.openVars );
    648                                         result.withArg( actual.expr );
    649                                         ++result.nextExpl;
    650                                 }
    651                         }
     668
     669                        // completed tuples; will be spliced to end of results to finish
     670                        std::vector<ArgPack> finalResults{};
     671
    652672                        // iterate until all results completed
    653                         while ( ! results.empty() ) {
     673                        std::size_t genEnd;
     674                        ++nTuples;
     675                        do {
     676                                genEnd = results.size();
     677
    654678                                // add another argument to results
    655                                 for ( ArgPack& result : results ) {
    656                                         // finish result when out of arguments
    657                                         if ( result.nextArg >= args.size() ) {
    658                                                 Type* argType = result.actuals.back().expr->get_result();
    659                                                 if ( result.tupleEls.back() == 1 && Tuples::isTtype( argType ) ) {
    660                                                         // the case where a ttype value is passed directly is special, e.g. for
    661                                                         // argument forwarding purposes
    662                                                         // xxx - what if passing multiple arguments, last of which is ttype?
    663                                                         // xxx - what would happen if unify was changed so that unifying tuple
    664                                                         // types flattened both before unifying lists? then pass in TupleType
    665                                                         // (ttype) below.
    666                                                         result.tupleEls.pop_back();
    667                                                 } else {
    668                                                         // collapse leftover arguments into tuple
    669                                                         result.endTuple();
    670                                                         argType = result.actuals.back().expr->get_result();
     679                                for ( std::size_t i = genStart; i < genEnd; ++i ) {
     680                                        auto nextArg = results[i].nextArg;
     681
     682                                        // use next element of exploded tuple if present
     683                                        if ( results[i].hasExpl() ) {
     684                                                const ExplodedActual& expl = results[i].getExpl( args );
     685
     686                                                unsigned nextExpl = results[i].nextExpl + 1;
     687                                                if ( nextExpl == expl.exprs.size() ) {
     688                                                        nextExpl = 0;
    671689                                                }
    672                                                 // check unification for ttype before adding to final
    673                                                 if ( unify( ttype, argType, result.env, result.need, result.have,
    674                                                                 result.openVars, indexer ) ) {
    675                                                         finalResults.push_back( std::move(result) );
    676                                                 }
     690
     691                                                results.emplace_back(
     692                                                        i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env),
     693                                                        copy(results[i].need), copy(results[i].have),
     694                                                        copy(results[i].openVars), nextArg, nTuples, Cost::zero, nextExpl,
     695                                                        results[i].explAlt );
     696
    677697                                                continue;
    678698                                        }
    679699
     700                                        // finish result when out of arguments
     701                                        if ( nextArg >= args.size() ) {
     702                                                ArgPack newResult{
     703                                                        results[i].env, results[i].need, results[i].have,
     704                                                        results[i].openVars };
     705                                                newResult.nextArg = nextArg;
     706                                                Type* argType;
     707
     708                                                if ( nTuples > 0 ) {
     709                                                        // first iteration, push empty tuple expression
     710                                                        newResult.parent = i;
     711                                                        std::list<Expression*> emptyList;
     712                                                        newResult.expr.reset( new TupleExpr( emptyList ) );
     713                                                        argType = newResult.expr->get_result();
     714                                                } else {
     715                                                        // clone result to collect tuple
     716                                                        newResult.parent = results[i].parent;
     717                                                        newResult.cost = results[i].cost;
     718                                                        newResult.tupleStart = results[i].tupleStart;
     719                                                        newResult.expr.reset( results[i].expr->clone() );
     720                                                        argType = newResult.expr->get_result();
     721
     722                                                        if ( results[i].tupleStart > 0 && Tuples::isTtype( argType ) ) {
     723                                                                // the case where a ttype value is passed directly is special,
     724                                                                // e.g. for argument forwarding purposes
     725                                                                // xxx - what if passing multiple arguments, last of which is
     726                                                                //       ttype?
     727                                                                // xxx - what would happen if unify was changed so that unifying
     728                                                                //       tuple
     729                                                                // types flattened both before unifying lists? then pass in
     730                                                                // TupleType (ttype) below.
     731                                                                --newResult.tupleStart;
     732                                                        } else {
     733                                                                // collapse leftover arguments into tuple
     734                                                                newResult.endTuple( results );
     735                                                                argType = newResult.expr->get_result();
     736                                                        }
     737                                                }
     738
     739                                                // check unification for ttype before adding to final
     740                                                if ( unify( ttype, argType, newResult.env, newResult.need, newResult.have,
     741                                                                newResult.openVars, indexer ) ) {
     742                                                        finalResults.push_back( move(newResult) );
     743                                                }
     744
     745                                                continue;
     746                                        }
     747
    680748                                        // add each possible next argument
    681                                         for ( const Alternative& actual : args[result.nextArg] ) {
    682                                                 ArgPack aResult = result;  // copy to clone everything
    683                                                 // add details of actual to result
    684                                                 aResult.env.addActual( actual.env, aResult.openVars );
    685                                                 Cost cost = actual.cost;
    686 
    687                                                 // explode argument
    688                                                 std::vector<Alternative> exploded;
    689                                                 Tuples::explode( actual, indexer, back_inserter( exploded ) );
    690 
    691                                                 // add exploded argument to tuple
    692                                                 for ( Alternative& aActual : exploded ) {
    693                                                         aResult.withArg( aActual.expr, cost );
    694                                                         cost = Cost::zero;
     749                                        for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
     750                                                const ExplodedActual& expl = args[nextArg][j];
     751
     752                                                // fresh copies of parent parameters for this iteration
     753                                                TypeEnvironment env = results[i].env;
     754                                                OpenVarSet openVars = results[i].openVars;
     755
     756                                                env.addActual( expl.env, openVars );
     757
     758                                                // skip empty tuple arguments by (near-)cloning parent into next gen
     759                                                if ( expl.exprs.empty() ) {
     760                                                        results.emplace_back(
     761                                                                results[i], move(env), copy(results[i].need),
     762                                                                copy(results[i].have), move(openVars), nextArg + 1, expl.cost );
     763
     764                                                        continue;
    695765                                                }
    696                                                 ++aResult.nextArg;
    697                                                 nextResults.push_back( std::move(aResult) );
     766
     767                                                // add new result
     768                                                results.emplace_back(
     769                                                        i, expl.exprs.front().get(), move(env), copy(results[i].need),
     770                                                        copy(results[i].have), move(openVars), nextArg + 1,
     771                                                        nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
    698772                                        }
    699773                                }
    700774
    701775                                // reset for next round
    702                                 results.swap( nextResults );
    703                                 nextResults.clear();
    704                         }
    705                         results.swap( finalResults );
    706                         return ! results.empty();
     776                                genStart = genEnd;
     777                                nTuples = 0;
     778                        } while ( genEnd != results.size() );
     779
     780                        // splice final results onto results
     781                        for ( std::size_t i = 0; i < finalResults.size(); ++i ) {
     782                                results.push_back( move(finalResults[i]) );
     783                        }
     784                        return ! finalResults.empty();
    707785                }
    708786
    709787                // iterate each current subresult
    710                 for ( unsigned iResult = 0; iResult < results.size(); ++iResult ) {
    711                         ArgPack& result = results[iResult];
    712 
    713                         if ( result.nextExpl < result.expls.size() ) {
    714                                 // use remainder of exploded tuple if present
    715                                 const Alternative& actual = result.expls[result.nextExpl];
    716                                 result.env.addActual( actual.env, result.openVars );
    717                                 Type* actualType = actual.expr->get_result();
     788                std::size_t genEnd = results.size();
     789                for ( std::size_t i = genStart; i < genEnd; ++i ) {
     790                        auto nextArg = results[i].nextArg;
     791
     792                        // use remainder of exploded tuple if present
     793                        if ( results[i].hasExpl() ) {
     794                                const ExplodedActual& expl = results[i].getExpl( args );
     795                                Expression* expr = expl.exprs[results[i].nextExpl].get();
     796
     797                                TypeEnvironment env = results[i].env;
     798                                AssertionSet need = results[i].need, have = results[i].have;
     799                                OpenVarSet openVars = results[i].openVars;
     800
     801                                Type* actualType = expr->get_result();
    718802
    719803                                PRINT(
     
    725809                                )
    726810
    727                                 if ( unify( formalType, actualType, result.env, result.need, result.have,
    728                                                 result.openVars, indexer ) ) {
    729                                         ++result.nextExpl;
    730                                         nextResults.push_back( std::move(result.withArg( actual.expr )) );
     811                                if ( unify( formalType, actualType, env, need, have, openVars, indexer ) ) {
     812                                        unsigned nextExpl = results[i].nextExpl + 1;
     813                                        if ( nextExpl == expl.exprs.size() ) {
     814                                                nextExpl = 0;
     815                                        }
     816
     817                                        results.emplace_back(
     818                                                i, expr, move(env), move(need), move(have), move(openVars), nextArg,
     819                                                nTuples, Cost::zero, nextExpl, results[i].explAlt );
    731820                                }
    732821
    733822                                continue;
    734                         } else if ( result.nextArg >= args.size() ) {
    735                                 // use default initializers if out of arguments
     823                        }
     824
     825                        // use default initializers if out of arguments
     826                        if ( nextArg >= args.size() ) {
    736827                                if ( ConstantExpr* cnstExpr = getDefaultValue( initializer ) ) {
    737828                                        if ( Constant* cnst = dynamic_cast<Constant*>( cnstExpr->get_constant() ) ) {
    738                                                 if ( unify( formalType, cnst->get_type(), result.env, result.need,
    739                                                                 result.have, result.openVars, indexer ) ) {
    740                                                         nextResults.push_back( std::move(result.withArg( cnstExpr )) );
     829                                                TypeEnvironment env = results[i].env;
     830                                                AssertionSet need = results[i].need, have = results[i].have;
     831                                                OpenVarSet openVars = results[i].openVars;
     832
     833                                                if ( unify( formalType, cnst->get_type(), env, need, have, openVars,
     834                                                                indexer ) ) {
     835                                                        results.emplace_back(
     836                                                                i, cnstExpr, move(env), move(need), move(have),
     837                                                                move(openVars), nextArg, nTuples );
    741838                                                }
    742839                                        }
    743840                                }
     841
    744842                                continue;
    745843                        }
    746844
    747845                        // Check each possible next argument
    748                         for ( const Alternative& actual : args[result.nextArg] ) {
    749                                 ArgPack aResult = result;  // copy to clone everything
    750                                 // add details of actual to result
    751                                 aResult.env.addActual( actual.env, aResult.openVars );
    752 
    753                                 // explode argument
    754                                 std::vector<Alternative> exploded;
    755                                 Tuples::explode( actual, indexer, back_inserter( exploded ) );
    756                                 if ( exploded.empty() ) {
    757                                         // skip empty tuple arguments
    758                                         ++aResult.nextArg;
    759                                         results.push_back( std::move(aResult) );
     846                        for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
     847                                const ExplodedActual& expl = args[nextArg][j];
     848
     849                                // fresh copies of parent parameters for this iteration
     850                                TypeEnvironment env = results[i].env;
     851                                AssertionSet need = results[i].need, have = results[i].have;
     852                                OpenVarSet openVars = results[i].openVars;
     853
     854                                env.addActual( expl.env, openVars );
     855
     856                                // skip empty tuple arguments by (near-)cloning parent into next gen
     857                                if ( expl.exprs.empty() ) {
     858                                        results.emplace_back(
     859                                                results[i], move(env), move(need), move(have), move(openVars),
     860                                                nextArg + 1, expl.cost );
     861
    760862                                        continue;
    761863                                }
    762864
    763865                                // consider only first exploded actual
    764                                 const Alternative& aActual = exploded.front();
    765                                 Type* actualType = aActual.expr->get_result()->clone();
     866                                Expression* expr = expl.exprs.front().get();
     867                                Type* actualType = expr->get_result()->clone();
    766868
    767869                                PRINT(
     
    774876
    775877                                // attempt to unify types
    776                                 if ( unify( formalType, actualType, aResult.env, aResult.need, aResult.have, aResult.openVars, indexer ) ) {
    777                                         // add argument
    778                                         aResult.withArg( aActual.expr, actual.cost );
    779                                         ++aResult.nextArg;
    780                                         if ( exploded.size() > 1 ) {
    781                                                 // other parts of tuple left over
    782                                                 aResult.expls = std::move( exploded );
    783                                                 aResult.nextExpl = 1;
    784                                         }
    785                                         nextResults.push_back( std::move(aResult) );
     878                                if ( unify( formalType, actualType, env, need, have, openVars, indexer ) ) {
     879                                        // add new result
     880                                        results.emplace_back(
     881                                                i, expr, move(env), move(need), move(have), move(openVars), nextArg + 1,
     882                                                nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
    786883                                }
    787884                        }
     
    789886
    790887                // reset for next parameter
    791                 results.swap( nextResults );
    792                 nextResults.clear();
    793 
    794                 return ! results.empty();
     888                genStart = genEnd;
     889
     890                return genEnd != results.size();
     891        }
     892
     893        template<typename OutputIterator>
     894        void AlternativeFinder::validateFunctionAlternative( const Alternative &func, ArgPack& result,
     895                        const std::vector<ArgPack>& results, OutputIterator out ) {
     896                ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
     897                // sum cost and accumulate actuals
     898                std::list<Expression*>& args = appExpr->get_args();
     899                Cost cost = Cost::zero;
     900                const ArgPack* pack = &result;
     901                while ( pack->expr ) {
     902                        args.push_front( pack->expr->clone() );
     903                        cost += pack->cost;
     904                        pack = &results[pack->parent];
     905                }
     906                // build and validate new alternative
     907                Alternative newAlt( appExpr, result.env, cost );
     908                PRINT(
     909                        std::cerr << "instantiate function success: " << appExpr << std::endl;
     910                        std::cerr << "need assertions:" << std::endl;
     911                        printAssertionSet( result.need, std::cerr, 8 );
     912                )
     913                inferParameters( result.need, result.have, newAlt, result.openVars, out );
    795914        }
    796915
    797916        template<typename OutputIterator>
    798917        void AlternativeFinder::makeFunctionAlternatives( const Alternative &func,
    799                         FunctionType *funcType, const std::vector< AlternativeFinder > &args,
    800                         OutputIterator out ) {
     918                        FunctionType *funcType, const ExplodedArgs &args, OutputIterator out ) {
    801919                OpenVarSet funcOpenVars;
    802920                AssertionSet funcNeed, funcHave;
     
    818936
    819937                // iteratively build matches, one parameter at a time
    820                 std::vector<ArgPack> results{ ArgPack{ funcEnv, funcNeed, funcHave, funcOpenVars } };
    821                 std::vector<ArgPack> nextResults{};
     938                std::vector<ArgPack> results;
     939                results.push_back( ArgPack{ funcEnv, funcNeed, funcHave, funcOpenVars } );
     940                std::size_t genStart = 0;
     941
    822942                for ( DeclarationWithType* formal : funcType->get_parameters() ) {
    823943                        ObjectDecl* obj = strict_dynamic_cast< ObjectDecl* >( formal );
    824944                        if ( ! instantiateArgument(
    825                                         obj->get_type(), obj->get_init(), args, results, nextResults, indexer ) )
     945                                        obj->get_type(), obj->get_init(), args, results, genStart, indexer ) )
    826946                                return;
    827947                }
    828948
    829                 // filter out results that don't use all the arguments, and aren't variadic
    830                 std::vector<ArgPack> finalResults{};
    831949                if ( funcType->get_isVarArgs() ) {
    832                         for ( ArgPack& result : results ) {
    833                                 // use rest of exploded tuple if present
    834                                 while ( result.nextExpl < result.expls.size() ) {
    835                                         const Alternative& actual = result.expls[result.nextExpl];
    836                                         result.env.addActual( actual.env, result.openVars );
    837                                         result.withArg( actual.expr );
    838                                         ++result.nextExpl;
    839                                 }
    840                         }
    841 
    842                         while ( ! results.empty() ) {
    843                                 // build combinations for all remaining arguments
    844                                 for ( ArgPack& result : results ) {
    845                                         // keep if used all arguments
    846                                         if ( result.nextArg >= args.size() ) {
    847                                                 finalResults.push_back( std::move(result) );
     950                        // append any unused arguments to vararg pack
     951                        std::size_t genEnd;
     952                        do {
     953                                genEnd = results.size();
     954
     955                                // iterate results
     956                                for ( std::size_t i = genStart; i < genEnd; ++i ) {
     957                                        auto nextArg = results[i].nextArg;
     958
     959                                        // use remainder of exploded tuple if present
     960                                        if ( results[i].hasExpl() ) {
     961                                                const ExplodedActual& expl = results[i].getExpl( args );
     962
     963                                                unsigned nextExpl = results[i].nextExpl + 1;
     964                                                if ( nextExpl == expl.exprs.size() ) {
     965                                                        nextExpl = 0;
     966                                                }
     967
     968                                                results.emplace_back(
     969                                                        i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env),
     970                                                        copy(results[i].need), copy(results[i].have),
     971                                                        copy(results[i].openVars), nextArg, 0, Cost::zero, nextExpl,
     972                                                        results[i].explAlt );
     973
    848974                                                continue;
    849975                                        }
    850976
     977                                        // finish result when out of arguments
     978                                        if ( nextArg >= args.size() ) {
     979                                                validateFunctionAlternative( func, results[i], results, out );
     980
     981                                                continue;
     982                                        }
     983
    851984                                        // add each possible next argument
    852                                         for ( const Alternative& actual : args[result.nextArg] ) {
    853                                                 ArgPack aResult = result; // copy to clone everything
    854                                                 // add details of actual to result
    855                                                 aResult.env.addActual( actual.env, aResult.openVars );
    856                                                 Cost cost = actual.cost;
    857 
    858                                                 // explode argument
    859                                                 std::vector<Alternative> exploded;
    860                                                 Tuples::explode( actual, indexer, back_inserter( exploded ) );
    861 
    862                                                 // add exploded argument to arg list
    863                                                 for ( Alternative& aActual : exploded ) {
    864                                                         aResult.withArg( aActual.expr, cost );
    865                                                         cost = Cost::zero;
     985                                        for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
     986                                                const ExplodedActual& expl = args[nextArg][j];
     987
     988                                                // fresh copies of parent parameters for this iteration
     989                                                TypeEnvironment env = results[i].env;
     990                                                OpenVarSet openVars = results[i].openVars;
     991
     992                                                env.addActual( expl.env, openVars );
     993
     994                                                // skip empty tuple arguments by (near-)cloning parent into next gen
     995                                                if ( expl.exprs.empty() ) {
     996                                                        results.emplace_back(
     997                                                                results[i], move(env), copy(results[i].need),
     998                                                                copy(results[i].have), move(openVars), nextArg + 1, expl.cost );
     999
     1000                                                        continue;
    8661001                                                }
    867                                                 ++aResult.nextArg;
    868                                                 nextResults.push_back( std::move(aResult) );
     1002
     1003                                                // add new result
     1004                                                results.emplace_back(
     1005                                                        i, expl.exprs.front().get(), move(env), copy(results[i].need),
     1006                                                        copy(results[i].have), move(openVars), nextArg + 1, 0,
     1007                                                        expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
    8691008                                        }
    8701009                                }
    8711010
    872                                 // reset for next round
    873                                 results.swap( nextResults );
    874                                 nextResults.clear();
    875                         }
     1011                                genStart = genEnd;
     1012                        } while ( genEnd != results.size() );
    8761013                } else {
    8771014                        // filter out results that don't use all the arguments
    878                         for ( ArgPack& result : results ) {
    879                                 if ( result.nextExpl >= result.expls.size() && result.nextArg >= args.size() ) {
    880                                         finalResults.push_back( std::move(result) );
     1015                        for ( std::size_t i = genStart; i < results.size(); ++i ) {
     1016                                ArgPack& result = results[i];
     1017                                if ( ! result.hasExpl() && result.nextArg >= args.size() ) {
     1018                                        validateFunctionAlternative( func, result, results, out );
    8811019                                }
    8821020                        }
    883                 }
    884 
    885                 // validate matching combos, add to final result list
    886                 for ( ArgPack& result : finalResults ) {
    887                         ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
    888                         Alternative newAlt( appExpr, result.env, sumCost( result.actuals ) );
    889                         makeExprList( result.actuals, appExpr->get_args() );
    890                         PRINT(
    891                                 std::cerr << "instantiate function success: " << appExpr << std::endl;
    892                                 std::cerr << "need assertions:" << std::endl;
    893                                 printAssertionSet( result.need, std::cerr, 8 );
    894                         )
    895                         inferParameters( result.need, result.have, newAlt, result.openVars, out );
    8961021                }
    8971022        }
     
    9201045                        printAlts( funcOpFinder.alternatives, std::cerr, 1 );
    9211046                )
     1047
     1048                // pre-explode arguments
     1049                ExplodedArgs argExpansions;
     1050                argExpansions.reserve( argAlternatives.size() );
     1051
     1052                for ( const AlternativeFinder& arg : argAlternatives ) {
     1053                        argExpansions.emplace_back();
     1054                        auto& argE = argExpansions.back();
     1055                        argE.reserve( arg.alternatives.size() );
     1056
     1057                        for ( const Alternative& actual : arg ) {
     1058                                argE.emplace_back( actual, indexer );
     1059                        }
     1060                }
    9221061
    9231062                AltList candidates;
     
    9341073                                                Alternative newFunc( *func );
    9351074                                                referenceToRvalueConversion( newFunc.expr );
    936                                                 makeFunctionAlternatives( newFunc, function, argAlternatives,
     1075                                                makeFunctionAlternatives( newFunc, function, argExpansions,
    9371076                                                        std::back_inserter( candidates ) );
    9381077                                        }
     
    9431082                                                        Alternative newFunc( *func );
    9441083                                                        referenceToRvalueConversion( newFunc.expr );
    945                                                         makeFunctionAlternatives( newFunc, function, argAlternatives,
     1084                                                        makeFunctionAlternatives( newFunc, function, argExpansions,
    9461085                                                                std::back_inserter( candidates ) );
    9471086                                                } // if
     
    9551094                // try each function operator ?() with each function alternative
    9561095                if ( ! funcOpFinder.alternatives.empty() ) {
    957                         // add function alternatives to front of argument list
    958                         argAlternatives.insert( argAlternatives.begin(), std::move(funcFinder) );
     1096                        // add exploded function alternatives to front of argument list
     1097                        std::vector<ExplodedActual> funcE;
     1098                        funcE.reserve( funcFinder.alternatives.size() );
     1099                        for ( const Alternative& actual : funcFinder ) {
     1100                                funcE.emplace_back( actual, indexer );
     1101                        }
     1102                        argExpansions.insert( argExpansions.begin(), move(funcE) );
    9591103
    9601104                        for ( AltList::iterator funcOp = funcOpFinder.alternatives.begin();
     
    9681112                                                        Alternative newFunc( *funcOp );
    9691113                                                        referenceToRvalueConversion( newFunc.expr );
    970                                                         makeFunctionAlternatives( newFunc, function, argAlternatives,
     1114                                                        makeFunctionAlternatives( newFunc, function, argExpansions,
    9711115                                                                std::back_inserter( candidates ) );
    9721116                                                }
     
    9821126
    9831127                // compute conversionsion costs
    984                 for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) {
    985                         Cost cvtCost = computeApplicationConversionCost( *withFunc, indexer );
     1128                for ( Alternative& withFunc : candidates ) {
     1129                        Cost cvtCost = computeApplicationConversionCost( withFunc, indexer );
    9861130
    9871131                        PRINT(
    988                                 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc->expr );
     1132                                ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc.expr );
    9891133                                PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
    9901134                                FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() );
     
    9951139                                printAll( appExpr->get_args(), std::cerr, 8 );
    9961140                                std::cerr << "bindings are:" << std::endl;
    997                                 withFunc->env.print( std::cerr, 8 );
     1141                                withFunc.env.print( std::cerr, 8 );
    9981142                                std::cerr << "cost of conversion is:" << cvtCost << std::endl;
    9991143                        )
    10001144                        if ( cvtCost != Cost::infinity ) {
    1001                                 withFunc->cvtCost = cvtCost;
    1002                                 alternatives.push_back( *withFunc );
     1145                                withFunc.cvtCost = cvtCost;
     1146                                alternatives.push_back( withFunc );
    10031147                        } // if
    10041148                } // for
    10051149
    1006                 candidates.clear();
    1007                 candidates.splice( candidates.end(), alternatives );
     1150                candidates = move(alternatives);
    10081151
    10091152                // use a new list so that alternatives are not examined by addAnonConversions twice.
     
    10111154                findMinCost( candidates.begin(), candidates.end(), std::back_inserter( winners ) );
    10121155
    1013                 // function may return struct or union value, in which case we need to add alternatives for implicit
    1014                 // conversions to each of the anonymous members, must happen after findMinCost since anon conversions
    1015                 // are never the cheapest expression
     1156                // function may return struct or union value, in which case we need to add alternatives
     1157                // for implicitconversions to each of the anonymous members, must happen after findMinCost
     1158                // since anon conversions are never the cheapest expression
    10161159                for ( const Alternative & alt : winners ) {
    10171160                        addAnonConversions( alt );
    10181161                }
    1019                 alternatives.splice( alternatives.begin(), winners );
     1162                spliceBegin( alternatives, winners );
    10201163
    10211164                if ( alternatives.empty() && targetType && ! targetType->isVoid() ) {
     
    10411184                AlternativeFinder finder( indexer, env );
    10421185                finder.find( addressExpr->get_arg() );
    1043                 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
    1044                         if ( isLvalue( i->expr ) ) {
    1045                                 alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) );
     1186                for ( Alternative& alt : finder.alternatives ) {
     1187                        if ( isLvalue( alt.expr ) ) {
     1188                                alternatives.push_back(
     1189                                        Alternative{ new AddressExpr( alt.expr->clone() ), alt.env, alt.cost } );
    10461190                        } // if
    10471191                } // for
     
    10491193
    10501194        void AlternativeFinder::visit( LabelAddressExpr * expr ) {
    1051                 alternatives.push_back( Alternative( expr->clone(), env, Cost::zero) );
     1195                alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } );
    10521196        }
    10531197
     
    10911235
    10921236                AltList candidates;
    1093                 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
     1237                for ( Alternative & alt : finder.alternatives ) {
    10941238                        AssertionSet needAssertions, haveAssertions;
    10951239                        OpenVarSet openVars;
     
    10991243                        // that are cast directly.  The candidate is invalid if it has fewer results than there are types to cast
    11001244                        // to.
    1101                         int discardedValues = i->expr->get_result()->size() - castExpr->get_result()->size();
     1245                        int discardedValues = alt.expr->get_result()->size() - castExpr->get_result()->size();
    11021246                        if ( discardedValues < 0 ) continue;
    11031247                        // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not
    11041248                        // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3]))
    11051249                        // unification run for side-effects
    1106                         unify( castExpr->get_result(), i->expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer );
    1107                         Cost thisCost = castCost( i->expr->get_result(), castExpr->get_result(), indexer, i->env );
     1250                        unify( castExpr->get_result(), alt.expr->get_result(), alt.env, needAssertions,
     1251                                haveAssertions, openVars, indexer );
     1252                        Cost thisCost = castCost( alt.expr->get_result(), castExpr->get_result(), indexer,
     1253                                alt.env );
     1254                        PRINT(
     1255                                std::cerr << "working on cast with result: " << castExpr->result << std::endl;
     1256                                std::cerr << "and expr type: " << alt.expr->result << std::endl;
     1257                                std::cerr << "env: " << alt.env << std::endl;
     1258                        )
    11081259                        if ( thisCost != Cost::infinity ) {
     1260                                PRINT(
     1261                                        std::cerr << "has finite cost." << std::endl;
     1262                                )
    11091263                                // count one safe conversion for each value that is thrown away
    11101264                                thisCost.incSafe( discardedValues );
    1111                                 Alternative newAlt( restructureCast( i->expr->clone(), toType ), i->env, i->cost, thisCost );
    1112                                 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) );
     1265                                Alternative newAlt( restructureCast( alt.expr->clone(), toType ), alt.env,
     1266                                        alt.cost, thisCost );
     1267                                inferParameters( needAssertions, haveAssertions, newAlt, openVars,
     1268                                        back_inserter( candidates ) );
    11131269                        } // if
    11141270                } // for
     
    13971553
    13981554        void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) {
    1399                 std::list< AlternativeFinder > subExprAlternatives;
    1400                 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) );
    1401                 std::list< AltList > possibilities;
    1402                 combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) );
    1403                 for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) {
     1555                std::vector< AlternativeFinder > subExprAlternatives;
     1556                findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(),
     1557                        back_inserter( subExprAlternatives ) );
     1558                std::vector< AltList > possibilities;
     1559                combos( subExprAlternatives.begin(), subExprAlternatives.end(),
     1560                        back_inserter( possibilities ) );
     1561                for ( const AltList& alts : possibilities ) {
    14041562                        std::list< Expression * > exprs;
    1405                         makeExprList( *i, exprs );
     1563                        makeExprList( alts, exprs );
    14061564
    14071565                        TypeEnvironment compositeEnv;
    1408                         simpleCombineEnvironments( i->begin(), i->end(), compositeEnv );
    1409                         alternatives.push_back( Alternative( new TupleExpr( exprs ) , compositeEnv, sumCost( *i ) ) );
     1566                        simpleCombineEnvironments( alts.begin(), alts.end(), compositeEnv );
     1567                        alternatives.push_back(
     1568                                Alternative{ new TupleExpr( exprs ), compositeEnv, sumCost( alts ) } );
    14101569                } // for
    14111570        }
  • src/ResolvExpr/AlternativeFinder.h

    r50abab9 rf5478c8  
    2121
    2222#include "Alternative.h"                 // for AltList, Alternative
     23#include "ExplodedActual.h"              // for ExplodedActual
    2324#include "ResolvExpr/Cost.h"             // for Cost, Cost::infinity
    2425#include "ResolvExpr/TypeEnvironment.h"  // for AssertionSet, OpenVarSet
     
    3132
    3233namespace ResolvExpr {
     34        struct ArgPack;
     35
     36        /// First index is which argument, second index is which alternative for that argument,
     37        /// third index is which exploded element of that alternative
     38        using ExplodedArgs = std::vector< std::vector< ExplodedActual > >;
     39
    3340        class AlternativeFinder : public Visitor {
    3441          public:
     
    3643
    3744                AlternativeFinder( const AlternativeFinder& o )
    38                         : indexer(o.indexer), alternatives(o.alternatives), env(o.env), 
     45                        : indexer(o.indexer), alternatives(o.alternatives), env(o.env),
    3946                          targetType(o.targetType) {}
    40                
     47
    4148                AlternativeFinder( AlternativeFinder&& o )
    42                         : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), 
     49                        : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env),
    4350                          targetType(o.targetType) {}
    44                
     51
    4552                AlternativeFinder& operator= ( const AlternativeFinder& o ) {
    4653                        if (&o == this) return *this;
    47                        
     54
    4855                        // horrific nasty hack to rebind references...
    4956                        alternatives.~AltList();
     
    5461                AlternativeFinder& operator= ( AlternativeFinder&& o ) {
    5562                        if (&o == this) return *this;
    56                        
     63
    5764                        // horrific nasty hack to rebind references...
    5865                        alternatives.~AltList();
     
    126133                /// Adds alternatives for offsetof expressions, given the base type and name of the member
    127134                template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
     135                /// Takes a final result and checks if its assertions can be satisfied
    128136                template<typename OutputIterator>
    129                 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const std::vector< AlternativeFinder >& args, OutputIterator out );
     137                void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );
     138                /// Finds matching alternatives for a function, given a set of arguments
     139                template<typename OutputIterator>
     140                void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out );
     141                /// Checks if assertion parameters match for a new alternative
    130142                template< typename OutputIterator >
    131143                void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
  • src/ResolvExpr/PtrsAssignable.cc

    r50abab9 rf5478c8  
    6868
    6969        void PtrsAssignable::visit( __attribute((unused)) VoidType *voidType ) {
    70                 if ( ! dynamic_cast< FunctionType* >( dest ) ) {
    71                         // T * = void * is safe for any T that is not a function type.
    72                         // xxx - this should be unsafe...
    73                         result = 1;
    74                 } // if
     70                // T * = void * is disallowed - this is a change from C, where any
     71                // void * can be assigned or passed to a non-void pointer without a cast.
    7572        }
    7673
  • src/ResolvExpr/Resolver.cc

    r50abab9 rf5478c8  
    1818#include <memory>                        // for allocator, allocator_traits<...
    1919#include <tuple>                         // for get
     20#include <vector>
    2021
    2122#include "Alternative.h"                 // for Alternative, AltList
     
    411412
    412413                        // Find all alternatives for all arguments in canonical form
    413                         std::list< AlternativeFinder > argAlternatives;
     414                        std::vector< AlternativeFinder > argAlternatives;
    414415                        funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) );
    415416
    416417                        // List all combinations of arguments
    417                         std::list< AltList > possibilities;
     418                        std::vector< AltList > possibilities;
    418419                        combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
    419420
  • src/ResolvExpr/TypeEnvironment.cc

    r50abab9 rf5478c8  
    212212        }
    213213
     214        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) {
     215                env.print( out );
     216                return out;
     217        }
    214218} // namespace ResolvExpr
    215219
  • src/ResolvExpr/TypeEnvironment.h

    r50abab9 rf5478c8  
    8686                TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
    8787
    88                 /// Iteratively adds the environment of a new actual (with allowWidening = false), 
     88                /// Iteratively adds the environment of a new actual (with allowWidening = false),
    8989                /// and extracts open variables.
    9090                void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
     
    114114                return sub.applyFree( type );
    115115        }
     116
     117        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
    116118} // namespace ResolvExpr
    117119
  • src/ResolvExpr/module.mk

    r50abab9 rf5478c8  
    3232       ResolvExpr/Occurs.cc \
    3333       ResolvExpr/TypeEnvironment.cc \
    34        ResolvExpr/CurrentObject.cc
     34       ResolvExpr/CurrentObject.cc \
     35       ResolvExpr/ExplodedActual.cc
  • src/ResolvExpr/typeops.h

    r50abab9 rf5478c8  
    1616#pragma once
    1717
     18#include <vector>
     19
    1820#include "SynTree/SynTree.h"
    1921#include "SynTree/Type.h"
     
    2830        void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
    2931                typedef typename InputIterator::value_type SetType;
    30                 typedef typename std::list< typename SetType::value_type > ListType;
     32                typedef typename std::vector< typename SetType::value_type > ListType;
    3133
    3234                if ( begin == end )     {
     
    3840                begin++;
    3941
    40                 std::list< ListType > recursiveResult;
     42                std::vector< ListType > recursiveResult;
    4143                combos( begin, end, back_inserter( recursiveResult ) );
    4244
    43                 for ( typename std::list< ListType >::const_iterator i = recursiveResult.begin(); i != recursiveResult.end(); ++i ) {
    44                         for ( typename ListType::const_iterator j = current->begin(); j != current->end(); ++j ) {
    45                                 ListType result;
    46                                 std::back_insert_iterator< ListType > inserter = back_inserter( result );
    47                                 *inserter++ = *j;
    48                                 std::copy( i->begin(), i->end(), inserter );
    49                                 *out++ = result;
    50                         } // for
    51                 } // for
     45                for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) {
     46                        ListType result;
     47                        std::back_insert_iterator< ListType > inserter = back_inserter( result );
     48                        *inserter++ = j;
     49                        std::copy( i.begin(), i.end(), inserter );
     50                        *out++ = result;
     51                }
    5252        }
    5353
Note: See TracChangeset for help on using the changeset viewer.