Changeset 0362d42 for src


Ignore:
Timestamp:
Sep 1, 2016, 3:27:57 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
1ced874
Parents:
fba44f8
Message:

modify instantiate function so that it allows formal parameters with tuple type to unify

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    rfba44f8 r0362d42  
    267267                std::list< DeclarationWithType* >::iterator formal = formals.begin();
    268268                std::list< Expression* >& actuals = appExpr->get_args();
     269
    269270                for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    270271                        PRINT(
     
    276277                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    277278                        Cost actualCost;
    278                         for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
     279                        for ( std::list< Type* >::iterator actualType = (*actualExpr)->get_results().begin(); actualType != (*actualExpr)->get_results().end(); ++actualType ) {
     280                                // xxx - need tuple handling code here
    279281                                if ( formal == formals.end() ) {
    280282                                        if ( function->get_isVarArgs() ) {
     
    287289                                PRINT(
    288290                                        std::cerr << std::endl << "converting ";
    289                                         (*actual)->print( std::cerr, 8 );
     291                                        (*actualType)->print( std::cerr, 8 );
    290292                                        std::cerr << std::endl << " to ";
    291293                                        (*formal)->get_type()->print( std::cerr, 8 );
    292294                                )
    293                                 Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
     295                                Cost newCost = conversionCost( *actualType, (*formal)->get_type(), indexer, alt.env );
    294296                                PRINT(
    295297                                        std::cerr << std::endl << "cost is" << newCost << std::endl;
     
    302304                                actualCost += newCost;
    303305
    304                                 convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actual, alt.env, indexer ), 0 );
     306                                convCost += Cost( 0, polyCost( (*formal)->get_type(), alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 );
    305307
    306308                                formal++;
     
    362364        }
    363365
     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
    364377        bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) {
    365378                simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv );
     
    379392                */
    380393                std::list< DeclarationWithType* >::iterator formal = formals.begin();
     394
     395                std::list< Type * > formalTypes;
     396                std::list< Type * >::iterator formalType = formalTypes.end();
     397
    381398                for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
    382                         for ( std::list< Type* >::iterator actual = actualExpr->expr->get_results().begin(); actual != actualExpr->expr->get_results().end(); ++actual ) {
    383                                 if ( formal == formals.end() ) {
    384                                         return isVarArgs;
     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;
    385413                                }
    386414                                PRINT(
     
    388416                                        (*formal)->get_type()->print( std::cerr );
    389417                                        std::cerr << std::endl << "actual type is ";
    390                                         (*actual)->print( std::cerr );
     418                                        (*actualType)->print( std::cerr );
    391419                                        std::cerr << std::endl;
    392420                                )
    393                                 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
     421                                if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    394422                                        return false;
    395423                                }
    396                                 formal++;
    397                         }
    398                 }
     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
    399432                // Handling of default values
    400433                while ( formal != formals.end() ) {
  • src/ResolvExpr/AlternativeFinder.h

    rfba44f8 r0362d42  
    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

    rfba44f8 r0362d42  
    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.