Changeset 48ca586


Ignore:
Timestamp:
Jan 13, 2016, 2:31:28 PM (8 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
52bbd67
Parents:
4d7e8f5
Message:

addPolyRetParam now handles polymorphic generic types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r4d7e8f5 r48ca586  
    7070                  private:
    7171                        void passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
     72                        /// wraps a function application with a new temporary for the out-parameter return value
    7273                        Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
    73                         Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg );
     74                        /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment
     75                        void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params );
     76                        /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete).
     77                        /// If `doClone` is set to false, will not clone interior types
     78                        Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true );
     79                        /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value
     80                        Expression *addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg );
    7481                        Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
    7582                        void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars );
     
    385392                }
    386393
    387                 Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, std::string typeName, std::list< Expression *>::iterator &arg ) {
    388                         ResolvExpr::EqvClass eqvClass;
     394                void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) {
     395                        for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
     396                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
     397                                assert(paramType && "Aggregate parameters should be type expressions");
     398                                paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) );
     399                        }
     400                }
     401               
     402                Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) {
     403                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
     404                                Type *concrete = env->lookup( typeInst->get_name() );
     405                                if ( concrete == 0 ) {
     406                                        throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr );
     407                                } // if
     408                                return concrete;
     409                        } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
     410                                if ( doClone ) {
     411                                        structType = structType->clone();
     412                                }
     413                                replaceParametersWithConcrete( appExpr, structType->get_parameters() );
     414                                return structType;
     415                        } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
     416                                if ( doClone ) {
     417                                        unionType = unionType->clone();
     418                                }
     419                                replaceParametersWithConcrete( appExpr, unionType->get_parameters() );
     420                                return unionType;
     421                        }
     422                        return type;
     423                }
     424
     425                Expression *Pass1::addPolyRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg ) {
    389426                        assert( env );
    390                         Type *concrete = env->lookup( typeName );
    391                         if ( concrete == 0 ) {
    392                                 throw SemanticError( "Unbound type variable " + typeName + " in ", appExpr );
    393                         } // if
     427                        Type *concrete = replaceWithConcrete( appExpr, polyType );
    394428                        return addRetParam( appExpr, function, concrete, arg );
    395429                }
     
    787821
    788822                        if ( ReferenceToType *polyType = isPolyRet( function ) ) {
    789                                 ret = addPolyRetParam( appExpr, function, polyType->get_name(), arg );
     823                                ret = addPolyRetParam( appExpr, function, polyType, arg );
    790824                        } else if ( needsAdapter( function, scopeTyVars ) ) {
    791825                                // std::cerr << "needs adapter: ";
Note: See TracChangeset for help on using the changeset viewer.