Changes in / [02cea2d:4e7f0f1]


Ignore:
Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r02cea2d r4e7f0f1  
    174174                        }
    175175                }
     176
     177                // flatten tuple type into list of types
     178                template< typename OutputIterator >
     179                void flatten( Type * type, OutputIterator out ) {
     180                        if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
     181                                for ( Type * t : *tupleType ) {
     182                                        flatten( t, out );
     183                                }
     184                        } else {
     185                                *out++ = type;
     186                        }
     187                }
    176188        }
    177189
     
    268280                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    269281                std::list< Expression* >& actuals = appExpr->get_args();
     282
     283                std::list< Type * > formalTypes;
     284                std::list< Type * >::iterator formalType = formalTypes.end();
     285
    270286                for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
     287
    271288                        PRINT(
    272289                                std::cerr << "actual expression:" << std::endl;
     
    277294                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    278295                        Cost actualCost;
    279                         for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
    280                                 if ( formal == formals.end() ) {
    281                                         if ( function->get_isVarArgs() ) {
    282                                                 convCost += Cost( 1, 0, 0 );
    283                                                 break;
    284                                         } else {
    285                                                 return Cost::infinity;
     296                        for ( std::list< Type* >::iterator actualType = (*actualExpr)->get_results().begin(); actualType != (*actualExpr)->get_results().end(); ++actualType ) {
     297
     298                                // tuple handling code
     299                                if ( formalType == formalTypes.end() ) {
     300                                        // the type of the formal parameter may be a tuple type. To make this easier to work with,
     301                                        // flatten the tuple type and traverse the resulting list of types, incrementing the formal
     302                                        // iterator once its types have been extracted. Once a particular formal parameter's type has
     303                                        // been exhausted load the next formal parameter's type.
     304                                        if ( formal == formals.end() ) {
     305                                                if ( function->get_isVarArgs() ) {
     306                                                        convCost += Cost( 1, 0, 0 );
     307                                                        break;
     308                                                } else {
     309                                                        return Cost::infinity;
     310                                                }
    286311                                        }
     312                                        formalTypes.clear();
     313                                        flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
     314                                        formalType = formalTypes.begin();
     315                                        ++formal;
    287316                                }
     317
    288318                                PRINT(
    289319                                        std::cerr << std::endl << "converting ";
    290                                         (*actual)->print( std::cerr, 8 );
     320                                        (*actualType)->print( std::cerr, 8 );
    291321                                        std::cerr << std::endl << " to ";
    292322                                        (*formal)->get_type()->print( std::cerr, 8 );
    293323                                )
    294                                 Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
     324                                Cost newCost = conversionCost( *actualType, *formalType, indexer, alt.env );
    295325                                PRINT(
    296326                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
     
    303333                                actualCost += newCost;
    304334
    305                                 convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actual, alt.env, indexer ), 0 );
    306 
    307                                 formal++;
     335                                convCost += Cost( 0, polyCost( *formalType, alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 );
     336
     337                                formalType++;
    308338                        }
    309339                        if ( actualCost != Cost( 0, 0, 0 ) ) {
     
    380410                */
    381411                std::list< DeclarationWithType* >::iterator formal = formals.begin();
     412
     413                std::list< Type * > formalTypes;
     414                std::list< Type * >::iterator formalType = formalTypes.end();
     415
    382416                for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    383                         for ( std::list< Type* >::iterator actual = actualExpr->expr->get_results().begin(); actual != actualExpr->expr->get_results().end(); ++actual ) {
    384                                 if ( formal == formals.end() ) {
    385                                         return isVarArgs;
     417                        std::list< Type* > & actualTypes = actualExpr->expr->get_results();
     418                        for ( std::list< Type* >::iterator actualType = actualTypes.begin(); actualType != actualTypes.end(); ++actualType ) {
     419                                if ( formalType == formalTypes.end() ) {
     420                                        // the type of the formal parameter may be a tuple type. To make this easier to work with,
     421                                        // flatten the tuple type and traverse the resulting list of types, incrementing the formal
     422                                        // iterator once its types have been extracted. Once a particular formal parameter's type has
     423                                        // been exhausted load the next formal parameter's type.
     424                                        if ( formal == formals.end() ) {
     425                                                return isVarArgs;
     426                                        }
     427                                        formalTypes.clear();
     428                                        flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
     429                                        formalType = formalTypes.begin();
     430                                        ++formal;
    386431                                }
    387432                                PRINT(
     
    389434                                        (*formal)->get_type()->print( std::cerr );
    390435                                        std::cerr << std::endl << "actual type is ";
    391                                         (*actual)->print( std::cerr );
     436                                        (*actualType)->print( std::cerr );
    392437                                        std::cerr << std::endl;
    393438                                )
    394                                 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
     439                                if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    395440                                        return false;
    396441                                }
    397                                 formal++;
    398                         }
    399                 }
     442                                ++formalType;
     443                        }
     444                }
     445
     446                // xxx - a tuple type was not completely matched
     447                // partially handle the tuple with default arguments??
     448                if ( formalType != formalTypes.end() ) return false;
     449
    400450                // Handling of default values
    401451                while ( formal != formals.end() ) {
  • src/ResolvExpr/AlternativeFinder.h

    r02cea2d r4e7f0f1  
    6767                virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
    6868          public:  // xxx - temporary hack - should make Tuples::TupleAssignment a friend
     69          /// Runs a new alternative finder on each element in [begin, end)
     70          /// and writes each alternative finder to out.
    6971                template< typename InputIterator, typename OutputIterator >
    7072                void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
  • src/SynTree/Type.h

    r02cea2d r4e7f0f1  
    352352        virtual ~TupleType();
    353353
     354        typedef std::list<Type*> value_type;
     355        typedef value_type::iterator iterator;
     356
    354357        std::list<Type*>& get_types() { return types; }
     358
     359        iterator begin() { return types.begin(); }
     360        iterator end() { return types.end(); }
    355361
    356362        virtual TupleType *clone() const { return new TupleType( *this ); }
Note: See TracChangeset for help on using the changeset viewer.