Changes in src/AST/TypeSubstitution.cpp [3e5dd913:361bf01]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/TypeSubstitution.cpp
r3e5dd913 r361bf01 39 39 void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { 40 40 dest.typeEnv.clear(); 41 dest.varEnv.clear(); 41 42 dest.add( src ); 42 43 } … … 46 47 typeEnv[ i->first ] = i->second; 47 48 } // 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 54 void TypeSubstitution::add( std::string formalType, const Type *actualType ) { 55 typeEnv[ formalType ] = actualType; 56 } 57 58 void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) { 59 varEnv[ formalExpr ] = actualExpr; 60 } 61 62 void TypeSubstitution::remove( std::string formalType ) { 63 TypeEnvType::iterator i = typeEnv.find( formalType ); 60 64 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 69 const Type *TypeSubstitution::lookup( std::string formalType ) const { 70 TypeEnvType::const_iterator i = typeEnv.find( formalType ); 67 71 68 72 // break on not in substitution set … … 71 75 // attempt to transitively follow TypeInstType links. 72 76 while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) { 77 const std::string& typeName = actualType->name; 78 73 79 // break cycles in the transitive follow 74 if ( *formalType == *actualType ) break;80 if ( formalType == typeName ) break; 75 81 76 82 // Look for the type this maps to, returning previous mapping if none-such 77 i = typeEnv.find( *actualType );83 i = typeEnv.find( typeName ); 78 84 if ( i == typeEnv.end() ) return actualType; 79 85 } … … 84 90 85 91 bool TypeSubstitution::empty() const { 86 return typeEnv.empty() ;92 return typeEnv.empty() && varEnv.empty(); 87 93 } 88 94 … … 92 98 TypeSubstitution * newEnv; 93 99 EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} 94 void previsit( FunctionType * ftype) {100 void previsit( TypeDecl * tyDecl ) { 95 101 // 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 ); 100 104 } 101 105 } … … 126 130 127 131 const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) { 128 BoundVarsType::const_iterator bound = boundVars.find( *inst);132 BoundVarsType::const_iterator bound = boundVars.find( inst->name ); 129 133 if ( bound != boundVars.end() ) return inst; 130 134 131 TypeEnvType::const_iterator i = sub.typeEnv.find( *inst);135 TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name ); 132 136 if ( i == sub.typeEnv.end() ) { 133 137 return inst; … … 137 141 // TODO: investigate preventing type variables from being bound to themselves in the first place. 138 142 if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) { 139 if ( *inst == *replacement) {143 if ( inst->name == replacement->name ) { 140 144 return inst; 141 145 } … … 152 156 } 153 157 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 154 168 void TypeSubstitution::Substituter::previsit( const FunctionType * ptype ) { 155 169 GuardValue( boundVars ); 156 170 // bind type variables from forall-qualifiers 157 171 if ( freeOnly ) { 158 for ( auto &tyvar : ptype->forall ) {159 boundVars.insert( *tyvar);172 for ( const TypeDecl * tyvar : ptype->forall ) { 173 boundVars.insert( tyvar->name ); 160 174 } // for 161 175 } // if 162 176 } 163 177 164 /*165 178 void TypeSubstitution::Substituter::handleAggregateType( const BaseInstType * type ) { 166 179 GuardValue( boundVars ); … … 171 184 if ( ! type->params.empty() ) { 172 185 for ( const TypeDecl * tyvar : decl->params ) { 173 boundVars.insert( *tyvar);186 boundVars.insert( tyvar->name ); 174 187 } // for 175 188 } // if … … 185 198 handleAggregateType( aggregateUseType ); 186 199 } 187 */188 200 189 201 } // namespace ast
Note: See TracChangeset
for help on using the changeset viewer.