Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r0362d42 r85517ddb  
    4242#include "Common/utility.h"
    4343#include "InitTweak/InitTweak.h"
     44#include "ResolveTypeof.h"
    4445
    4546extern bool resolvep;
     
    267268                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    268269                std::list< Expression* >& actuals = appExpr->get_args();
    269 
    270270                for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    271271                        PRINT(
     
    277277                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    278278                        Cost actualCost;
    279                         for ( std::list< Type* >::iterator actualType = (*actualExpr)->get_results().begin(); actualType != (*actualExpr)->get_results().end(); ++actualType ) {
    280                                 // xxx - need tuple handling code here
     279                        for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
    281280                                if ( formal == formals.end() ) {
    282281                                        if ( function->get_isVarArgs() ) {
     
    289288                                PRINT(
    290289                                        std::cerr << std::endl << "converting ";
    291                                         (*actualType)->print( std::cerr, 8 );
     290                                        (*actual)->print( std::cerr, 8 );
    292291                                        std::cerr << std::endl << " to ";
    293292                                        (*formal)->get_type()->print( std::cerr, 8 );
    294293                                )
    295                                 Cost newCost = conversionCost( *actualType, (*formal)->get_type(), indexer, alt.env );
     294                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
    296295                                PRINT(
    297296                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
     
    304303                                actualCost += newCost;
    305304
    306                                 convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 );
     305                                convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actual, alt.env, indexer ), 0 );
    307306
    308307                                formal++;
     
    364363        }
    365364
    366         template< typename OutputIterator >
    367         void flatten( Type * type, OutputIterator out ) {
    368                 if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
    369                         for ( Type * t : *tupleType ) {
    370                                 flatten( t, out );
    371                         }
    372                 } else {
    373                         *out++ = type;
    374                 }
    375         }
    376 
    377365        bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) {
    378366                simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv );
     
    392380                */
    393381                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    394 
    395                 std::list< Type * > formalTypes;
    396                 std::list< Type * >::iterator formalType = formalTypes.end();
    397 
    398382                for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    399                         std::list< Type* > & actualTypes = actualExpr->expr->get_results();
    400                         for ( std::list< Type* >::iterator actualType = actualTypes.begin(); actualType != actualTypes.end(); ++actualType ) {
    401                                 if ( formalType == formalTypes.end() ) {
    402                                         // the type of the formal parameter may be a tuple type. To make this easier to work with,
    403                                         // flatten the tuple type and traverse the resulting list of types, incrementing the formal
    404                                         // iterator once its types have been extracted. Once a particular formal parameter's type has
    405                                         // been exhausted load the next formal parameter's type.
    406                                         if ( formal == formals.end() ) {
    407                                                 return isVarArgs;
    408                                         }
    409                                         formalTypes.clear();
    410                                         flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
    411                                         formalType = formalTypes.begin();
    412                                         ++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;
    413386                                }
    414387                                PRINT(
     
    416389                                        (*formal)->get_type()->print( std::cerr );
    417390                                        std::cerr << std::endl << "actual type is ";
    418                                         (*actualType)->print( std::cerr );
     391                                        (*actual)->print( std::cerr );
    419392                                        std::cerr << std::endl;
    420393                                )
    421                                 if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
     394                                if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    422395                                        return false;
    423396                                }
    424                                 ++formalType;
    425                         }
    426                 }
    427 
    428                 // xxx - a tuple type was not completely matched
    429                 // partially handle the tuple with default arguments??
    430                 if ( formalType != formalTypes.end() ) return false;
    431 
     397                                formal++;
     398                        }
     399                }
    432400                // Handling of default values
    433401                while ( formal != formals.end() ) {
     
    740708        void AlternativeFinder::visit( CastExpr *castExpr ) {
    741709                for ( std::list< Type* >::iterator i = castExpr->get_results().begin(); i != castExpr->get_results().end(); ++i ) {
     710                        *i = resolveTypeof( *i, indexer );
    742711                        SymTab::validateType( *i, &indexer );
    743712                        adjustExprType( *i, env, indexer );
     
    828797
    829798        void AlternativeFinder::visit( VariableExpr *variableExpr ) {
    830                 alternatives.push_back( Alternative( variableExpr->clone(), env, Cost::zero ) );
     799                // not sufficient to clone here, because variable's type may have changed
     800                // since the VariableExpr was originally created.
     801                alternatives.push_back( Alternative( new VariableExpr( variableExpr->get_var() ), env, Cost::zero ) );
    831802        }
    832803
     
    837808        void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) {
    838809                if ( sizeofExpr->get_isType() ) {
     810                        // xxx - resolveTypeof?
    839811                        alternatives.push_back( Alternative( sizeofExpr->clone(), env, Cost::zero ) );
    840812                } else {
     
    856828        void AlternativeFinder::visit( AlignofExpr *alignofExpr ) {
    857829                if ( alignofExpr->get_isType() ) {
     830                        // xxx - resolveTypeof?
    858831                        alternatives.push_back( Alternative( alignofExpr->clone(), env, Cost::zero ) );
    859832                } else {
     
    889862        void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
    890863                AlternativeFinder funcFinder( indexer, env );
     864                // xxx - resolveTypeof?
    891865                if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) {
    892866                        addOffsetof( structInst, offsetofExpr->get_member() );
Note: See TracChangeset for help on using the changeset viewer.