Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/TypeSubstitution.cpp

    r3e5dd913 r361bf01  
    3939void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
    4040        dest.typeEnv.clear();
     41        dest.varEnv.clear();
    4142        dest.add( src );
    4243}
     
    4647                typeEnv[ i->first ] = i->second;
    4748        } // for
    48 }
    49 
    50 void TypeSubstitution::add( const TypeInstType * formalType, const Type *actualType ) {
    51         typeEnv[ *formalType ] = actualType;
    52 }
    53 
    54 void TypeSubstitution::add( const TypeInstType::TypeEnvKey & key, const Type * actualType) {
    55         typeEnv[ key ] = actualType;
    56 }
    57 
    58 void TypeSubstitution::remove( const TypeInstType * formalType ) {
    59         TypeEnvType::iterator i = typeEnv.find( *formalType );
     49        for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
     50                varEnv[ i->first ] = i->second;
     51        } // for
     52}
     53
     54void TypeSubstitution::add( std::string formalType, const Type *actualType ) {
     55        typeEnv[ formalType ] = actualType;
     56}
     57
     58void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) {
     59        varEnv[ formalExpr ] = actualExpr;
     60}
     61
     62void TypeSubstitution::remove( std::string formalType ) {
     63        TypeEnvType::iterator i = typeEnv.find( formalType );
    6064        if ( i != typeEnv.end() ) {
    61                 typeEnv.erase( *formalType );
    62         } // if
    63 }
    64 
    65 const Type *TypeSubstitution::lookup( const TypeInstType * formalType ) const {
    66         TypeEnvType::const_iterator i = typeEnv.find( *formalType );
     65                typeEnv.erase( formalType );
     66        } // if
     67}
     68
     69const Type *TypeSubstitution::lookup( std::string formalType ) const {
     70        TypeEnvType::const_iterator i = typeEnv.find( formalType );
    6771
    6872        // break on not in substitution set
     
    7175        // attempt to transitively follow TypeInstType links.
    7276        while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) {
     77                const std::string& typeName = actualType->name;
     78
    7379                // break cycles in the transitive follow
    74                 if ( *formalType == *actualType ) break;
     80                if ( formalType == typeName ) break;
    7581
    7682                // Look for the type this maps to, returning previous mapping if none-such
    77                 i = typeEnv.find( *actualType );
     83                i = typeEnv.find( typeName );
    7884                if ( i == typeEnv.end() ) return actualType;
    7985        }
     
    8490
    8591bool TypeSubstitution::empty() const {
    86         return typeEnv.empty();
     92        return typeEnv.empty() && varEnv.empty();
    8793}
    8894
     
    9298                TypeSubstitution * newEnv;
    9399                EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
    94                 void previsit( FunctionType * ftype ) {
     100                void previsit( TypeDecl * tyDecl ) {
    95101                        // transfer known bindings for seen type variables
    96                         for (auto & formal : ftype->forall) {
    97                                 if ( const Type * t = env->lookup( formal ) ) {
    98                                         newEnv->add( formal, t );
    99                                 }
     102                        if ( const Type * t = env->lookup( tyDecl->name ) ) {
     103                                newEnv->add( tyDecl->name, t );
    100104                        }
    101105                }
     
    126130
    127131const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) {
    128         BoundVarsType::const_iterator bound = boundVars.find( *inst );
     132        BoundVarsType::const_iterator bound = boundVars.find( inst->name );
    129133        if ( bound != boundVars.end() ) return inst;
    130134
    131         TypeEnvType::const_iterator i = sub.typeEnv.find( *inst );
     135        TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name );
    132136        if ( i == sub.typeEnv.end() ) {
    133137                return inst;
     
    137141                // TODO: investigate preventing type variables from being bound to themselves in the first place.
    138142                if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) {
    139                         if ( *inst == *replacement ) {
     143                        if ( inst->name == replacement->name ) {
    140144                                return inst;
    141145                        }
     
    152156}
    153157
     158const Expr * TypeSubstitution::Substituter::postvisit( const NameExpr * nameExpr ) {
     159        VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
     160        if ( i == sub.varEnv.end() ) {
     161                return nameExpr;
     162        } else {
     163                subCount++;
     164                return i->second;
     165        } // if
     166}
     167
    154168void TypeSubstitution::Substituter::previsit( const FunctionType * ptype ) {
    155169        GuardValue( boundVars );
    156170        // bind type variables from forall-qualifiers
    157171        if ( freeOnly ) {
    158                 for ( auto & tyvar : ptype->forall ) {
    159                                 boundVars.insert( *tyvar );
     172                for ( const TypeDecl * tyvar : ptype->forall ) {
     173                                boundVars.insert( tyvar->name );
    160174                } // for
    161175        } // if
    162176}
    163177
    164 /*
    165178void TypeSubstitution::Substituter::handleAggregateType( const BaseInstType * type ) {
    166179        GuardValue( boundVars );
     
    171184                        if ( ! type->params.empty() ) {
    172185                                for ( const TypeDecl * tyvar : decl->params ) {
    173                                         boundVars.insert( *tyvar );
     186                                        boundVars.insert( tyvar->name );
    174187                                } // for
    175188                        } // if
     
    185198        handleAggregateType( aggregateUseType );
    186199}
    187 */
    188200
    189201} // namespace ast
Note: See TracChangeset for help on using the changeset viewer.