Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/TypeSubstitution.cpp

    r361bf01 r3e5dd913  
    3939void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
    4040        dest.typeEnv.clear();
    41         dest.varEnv.clear();
    4241        dest.add( src );
    4342}
     
    4746                typeEnv[ i->first ] = i->second;
    4847        } // for
    49         for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
    50                 varEnv[ i->first ] = i->second;
    51         } // for
    5248}
    5349
    54 void TypeSubstitution::add( std::string formalType, const Type *actualType ) {
    55         typeEnv[ formalType ] = actualType;
     50void TypeSubstitution::add( const TypeInstType * formalType, const Type *actualType ) {
     51        typeEnv[ *formalType ] = actualType;
    5652}
    5753
    58 void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) {
    59         varEnv[ formalExpr ] = actualExpr;
     54void TypeSubstitution::add( const TypeInstType::TypeEnvKey & key, const Type * actualType) {
     55        typeEnv[ key ] = actualType;
    6056}
    6157
    62 void TypeSubstitution::remove( std::string formalType ) {
    63         TypeEnvType::iterator i = typeEnv.find( formalType );
     58void TypeSubstitution::remove( const TypeInstType * formalType ) {
     59        TypeEnvType::iterator i = typeEnv.find( *formalType );
    6460        if ( i != typeEnv.end() ) {
    65                 typeEnv.erase( formalType );
     61                typeEnv.erase( *formalType );
    6662        } // if
    6763}
    6864
    69 const Type *TypeSubstitution::lookup( std::string formalType ) const {
    70         TypeEnvType::const_iterator i = typeEnv.find( formalType );
     65const Type *TypeSubstitution::lookup( const TypeInstType * formalType ) const {
     66        TypeEnvType::const_iterator i = typeEnv.find( *formalType );
    7167
    7268        // break on not in substitution set
     
    7571        // attempt to transitively follow TypeInstType links.
    7672        while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) {
    77                 const std::string& typeName = actualType->name;
    78 
    7973                // break cycles in the transitive follow
    80                 if ( formalType == typeName ) break;
     74                if ( *formalType == *actualType ) break;
    8175
    8276                // Look for the type this maps to, returning previous mapping if none-such
    83                 i = typeEnv.find( typeName );
     77                i = typeEnv.find( *actualType );
    8478                if ( i == typeEnv.end() ) return actualType;
    8579        }
     
    9084
    9185bool TypeSubstitution::empty() const {
    92         return typeEnv.empty() && varEnv.empty();
     86        return typeEnv.empty();
    9387}
    9488
     
    9892                TypeSubstitution * newEnv;
    9993                EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
    100                 void previsit( TypeDecl * tyDecl ) {
     94                void previsit( FunctionType * ftype ) {
    10195                        // transfer known bindings for seen type variables
    102                         if ( const Type * t = env->lookup( tyDecl->name ) ) {
    103                                 newEnv->add( tyDecl->name, t );
     96                        for (auto & formal : ftype->forall) {
     97                                if ( const Type * t = env->lookup( formal ) ) {
     98                                        newEnv->add( formal, t );
     99                                }
    104100                        }
    105101                }
     
    130126
    131127const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) {
    132         BoundVarsType::const_iterator bound = boundVars.find( inst->name );
     128        BoundVarsType::const_iterator bound = boundVars.find( *inst );
    133129        if ( bound != boundVars.end() ) return inst;
    134130
    135         TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name );
     131        TypeEnvType::const_iterator i = sub.typeEnv.find( *inst );
    136132        if ( i == sub.typeEnv.end() ) {
    137133                return inst;
     
    141137                // TODO: investigate preventing type variables from being bound to themselves in the first place.
    142138                if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) {
    143                         if ( inst->name == replacement->name ) {
     139                        if ( *inst == *replacement ) {
    144140                                return inst;
    145141                        }
     
    156152}
    157153
    158 const 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 
    168154void TypeSubstitution::Substituter::previsit( const FunctionType * ptype ) {
    169155        GuardValue( boundVars );
    170156        // bind type variables from forall-qualifiers
    171157        if ( freeOnly ) {
    172                 for ( const TypeDecl * tyvar : ptype->forall ) {
    173                                 boundVars.insert( tyvar->name );
     158                for ( auto & tyvar : ptype->forall ) {
     159                                boundVars.insert( *tyvar );
    174160                } // for
    175161        } // if
    176162}
    177163
     164/*
    178165void TypeSubstitution::Substituter::handleAggregateType( const BaseInstType * type ) {
    179166        GuardValue( boundVars );
     
    184171                        if ( ! type->params.empty() ) {
    185172                                for ( const TypeDecl * tyvar : decl->params ) {
    186                                         boundVars.insert( tyvar->name );
     173                                        boundVars.insert( *tyvar );
    187174                                } // for
    188175                        } // if
     
    198185        handleAggregateType( aggregateUseType );
    199186}
     187*/
    200188
    201189} // namespace ast
Note: See TracChangeset for help on using the changeset viewer.