Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r3b58d91 r02cea2d  
    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                 }
    188176        }
    189177
     
    251239
    252240        template< typename StructOrUnionType >
    253         void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, Expression * member ) {
    254                 NameExpr * nameExpr = safe_dynamic_cast< NameExpr * >( member );
    255                 const std::string & name = nameExpr->get_name();
    256 
     241        void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const std::string &name ) {
    257242                std::list< Declaration* > members;
    258243                aggInst->lookup( name, members );
     
    283268                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    284269                std::list< Expression* >& actuals = appExpr->get_args();
    285 
    286                 std::list< Type * > formalTypes;
    287                 std::list< Type * >::iterator formalType = formalTypes.end();
    288 
    289270                for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    290 
    291271                        PRINT(
    292272                                std::cerr << "actual expression:" << std::endl;
     
    297277                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    298278                        Cost actualCost;
    299                         for ( std::list< Type* >::iterator actualType = (*actualExpr)->get_results().begin(); actualType != (*actualExpr)->get_results().end(); ++actualType ) {
    300 
    301                                 // tuple handling code
    302                                 if ( formalType == formalTypes.end() ) {
    303                                         // the type of the formal parameter may be a tuple type. To make this easier to work with,
    304                                         // flatten the tuple type and traverse the resulting list of types, incrementing the formal
    305                                         // iterator once its types have been extracted. Once a particular formal parameter's type has
    306                                         // been exhausted load the next formal parameter's type.
    307                                         if ( formal == formals.end() ) {
    308                                                 if ( function->get_isVarArgs() ) {
    309                                                         convCost += Cost( 1, 0, 0 );
    310                                                         break;
    311                                                 } else {
    312                                                         return Cost::infinity;
    313                                                 }
     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;
    314286                                        }
    315                                         formalTypes.clear();
    316                                         flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
    317                                         formalType = formalTypes.begin();
    318                                         ++formal;
    319287                                }
    320 
    321288                                PRINT(
    322289                                        std::cerr << std::endl << "converting ";
    323                                         (*actualType)->print( std::cerr, 8 );
     290                                        (*actual)->print( std::cerr, 8 );
    324291                                        std::cerr << std::endl << " to ";
    325292                                        (*formal)->get_type()->print( std::cerr, 8 );
    326293                                )
    327                                 Cost newCost = conversionCost( *actualType, *formalType, indexer, alt.env );
     294                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
    328295                                PRINT(
    329296                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
     
    336303                                actualCost += newCost;
    337304
    338                                 convCost += Cost( 0, polyCost( *formalType, alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 );
    339 
    340                                 formalType++;
     305                                convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actual, alt.env, indexer ), 0 );
     306
     307                                formal++;
    341308                        }
    342309                        if ( actualCost != Cost( 0, 0, 0 ) ) {
     
    413380                */
    414381                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    415 
    416                 std::list< Type * > formalTypes;
    417                 std::list< Type * >::iterator formalType = formalTypes.end();
    418 
    419382                for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    420                         std::list< Type* > & actualTypes = actualExpr->expr->get_results();
    421                         for ( std::list< Type* >::iterator actualType = actualTypes.begin(); actualType != actualTypes.end(); ++actualType ) {
    422                                 if ( formalType == formalTypes.end() ) {
    423                                         // the type of the formal parameter may be a tuple type. To make this easier to work with,
    424                                         // flatten the tuple type and traverse the resulting list of types, incrementing the formal
    425                                         // iterator once its types have been extracted. Once a particular formal parameter's type has
    426                                         // been exhausted load the next formal parameter's type.
    427                                         if ( formal == formals.end() ) {
    428                                                 return isVarArgs;
    429                                         }
    430                                         formalTypes.clear();
    431                                         flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
    432                                         formalType = formalTypes.begin();
    433                                         ++formal;
     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;
    434386                                }
    435387                                PRINT(
     
    437389                                        (*formal)->get_type()->print( std::cerr );
    438390                                        std::cerr << std::endl << "actual type is ";
    439                                         (*actualType)->print( std::cerr );
     391                                        (*actual)->print( std::cerr );
    440392                                        std::cerr << std::endl;
    441393                                )
    442                                 if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
     394                                if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    443395                                        return false;
    444396                                }
    445                                 ++formalType;
    446                         }
    447                 }
    448 
    449                 // xxx - a tuple type was not completely matched
    450                 // partially handle the tuple with default arguments??
    451                 if ( formalType != formalTypes.end() ) return false;
    452 
     397                                formal++;
     398                        }
     399                }
    453400                // Handling of default values
    454401                while ( formal != formals.end() ) {
     
    842789                        renameTypes( alternatives.back().expr );
    843790                        if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
    844                                 NameExpr nameExpr( "" );
    845                                 addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), &nameExpr );
     791                                addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" );
    846792                        } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
    847                                 NameExpr nameExpr( "" );
    848                                 addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), &nameExpr );
     793                                addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" );
    849794                        } // if
    850795                } // for
Note: See TracChangeset for help on using the changeset viewer.