[c671112] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // TypeSubstitution.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[dafe9e1] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Mon Jun 3 13:26:00 2017 |
---|
| 13 | // Update Count : 5 |
---|
[c671112] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Type.hpp" // for TypeInstType, Type, StructInstType, UnionInstType |
---|
| 17 | #include "TypeSubstitution.hpp" |
---|
| 18 | |
---|
| 19 | namespace ast { |
---|
| 20 | |
---|
| 21 | TypeSubstitution::TypeSubstitution() { |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) : Node() { |
---|
| 25 | initialize( other, *this ); |
---|
| 26 | } |
---|
| 27 | |
---|
[dafe9e1] | 28 | TypeSubstitution::~TypeSubstitution() {} |
---|
[c671112] | 29 | |
---|
| 30 | TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) { |
---|
| 31 | if ( this == &other ) return *this; |
---|
| 32 | initialize( other, *this ); |
---|
| 33 | return *this; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { |
---|
| 37 | dest.typeEnv.clear(); |
---|
| 38 | dest.varEnv.clear(); |
---|
| 39 | dest.add( src ); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void TypeSubstitution::add( const TypeSubstitution &other ) { |
---|
| 43 | for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) { |
---|
| 44 | typeEnv[ i->first ] = i->second; |
---|
| 45 | } // for |
---|
| 46 | for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) { |
---|
| 47 | varEnv[ i->first ] = i->second; |
---|
| 48 | } // for |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void TypeSubstitution::add( std::string formalType, const Type *actualType ) { |
---|
| 52 | typeEnv[ formalType ] = actualType; |
---|
| 53 | } |
---|
| 54 | |
---|
[172d9342] | 55 | void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) { |
---|
| 56 | varEnv[ formalExpr ] = actualExpr; |
---|
| 57 | } |
---|
| 58 | |
---|
[c671112] | 59 | void TypeSubstitution::remove( std::string formalType ) { |
---|
| 60 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
| 61 | if ( i != typeEnv.end() ) { |
---|
| 62 | typeEnv.erase( formalType ); |
---|
| 63 | } // if |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | const Type *TypeSubstitution::lookup( std::string formalType ) const { |
---|
| 67 | TypeEnvType::const_iterator i = typeEnv.find( formalType ); |
---|
| 68 | |
---|
| 69 | // break on not in substitution set |
---|
| 70 | if ( i == typeEnv.end() ) return 0; |
---|
| 71 | |
---|
| 72 | // attempt to transitively follow TypeInstType links. |
---|
| 73 | while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) { |
---|
| 74 | const std::string& typeName = actualType->name; |
---|
| 75 | |
---|
| 76 | // break cycles in the transitive follow |
---|
| 77 | if ( formalType == typeName ) break; |
---|
| 78 | |
---|
| 79 | // Look for the type this maps to, returning previous mapping if none-such |
---|
| 80 | i = typeEnv.find( typeName ); |
---|
| 81 | if ( i == typeEnv.end() ) return actualType; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | // return type from substitution set |
---|
| 85 | return i->second; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | bool TypeSubstitution::empty() const { |
---|
| 89 | return typeEnv.empty() && varEnv.empty(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | namespace { |
---|
| 93 | struct EnvTrimmer { |
---|
[55b6476] | 94 | const TypeSubstitution * env; |
---|
[c671112] | 95 | TypeSubstitution * newEnv; |
---|
| 96 | EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} |
---|
| 97 | void previsit( TypeDecl * tyDecl ) { |
---|
| 98 | // transfer known bindings for seen type variables |
---|
| 99 | if ( const Type * t = env->lookup( tyDecl->name ) ) { |
---|
| 100 | newEnv->add( tyDecl->name, t ); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | }; |
---|
| 104 | } // namespace |
---|
| 105 | |
---|
| 106 | /// reduce environment to just the parts that are referenced in a given expression |
---|
| 107 | TypeSubstitution * TypeSubstitution::newFromExpr( const Expr * expr, const TypeSubstitution * env ) { |
---|
| 108 | if ( env ) { |
---|
| 109 | TypeSubstitution * newEnv = new TypeSubstitution(); |
---|
| 110 | Pass<EnvTrimmer> trimmer( env, newEnv ); |
---|
| 111 | expr->accept( trimmer ); |
---|
| 112 | return newEnv; |
---|
| 113 | } |
---|
| 114 | return nullptr; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void TypeSubstitution::normalize() { |
---|
[55b6476] | 118 | Pass<Substituter> sub( *this, true ); |
---|
[c671112] | 119 | do { |
---|
| 120 | sub.pass.subCount = 0; |
---|
| 121 | sub.pass.freeOnly = true; |
---|
| 122 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
[55b6476] | 123 | i->second = i->second->accept( sub ); |
---|
[c671112] | 124 | } |
---|
| 125 | } while ( sub.pass.subCount ); |
---|
| 126 | } |
---|
| 127 | |
---|
[55b6476] | 128 | const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) { |
---|
[c671112] | 129 | BoundVarsType::const_iterator bound = boundVars.find( inst->name ); |
---|
| 130 | if ( bound != boundVars.end() ) return inst; |
---|
| 131 | |
---|
| 132 | TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name ); |
---|
| 133 | if ( i == sub.typeEnv.end() ) { |
---|
| 134 | return inst; |
---|
| 135 | } else { |
---|
| 136 | // cut off infinite loop for the case where a type is bound to itself. |
---|
| 137 | // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here. |
---|
| 138 | // TODO: investigate preventing type variables from being bound to themselves in the first place. |
---|
[55b6476] | 139 | if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) { |
---|
[c671112] | 140 | if ( inst->name == replacement->name ) { |
---|
| 141 | return inst; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl; |
---|
| 145 | subCount++; |
---|
[55b6476] | 146 | ptr<Type> newType = i->second; // force clone if needed |
---|
| 147 | add_qualifiers( newType, inst->qualifiers ); |
---|
[2890212] | 148 | // Note: need to recursively apply substitution to the new type because normalize does not |
---|
[55b6476] | 149 | // substitute bound vars, but bound vars must be substituted when not in freeOnly mode. |
---|
| 150 | newType = newType->accept( *visitor ); |
---|
| 151 | return newType.release(); |
---|
[c671112] | 152 | } // if |
---|
| 153 | } |
---|
| 154 | |
---|
[55b6476] | 155 | const Expr * TypeSubstitution::Substituter::postvisit( const NameExpr * nameExpr ) { |
---|
[c671112] | 156 | VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); |
---|
| 157 | if ( i == sub.varEnv.end() ) { |
---|
| 158 | return nameExpr; |
---|
| 159 | } else { |
---|
| 160 | subCount++; |
---|
[55b6476] | 161 | return i->second; |
---|
[c671112] | 162 | } // if |
---|
| 163 | } |
---|
| 164 | |
---|
[55b6476] | 165 | void TypeSubstitution::Substituter::previsit( const ParameterizedType * ptype ) { |
---|
[c671112] | 166 | GuardValue( boundVars ); |
---|
| 167 | // bind type variables from forall-qualifiers |
---|
| 168 | if ( freeOnly ) { |
---|
[55b6476] | 169 | for ( const TypeDecl * tyvar : ptype->forall ) { |
---|
| 170 | boundVars.insert( tyvar->name ); |
---|
[c671112] | 171 | } // for |
---|
| 172 | } // if |
---|
| 173 | } |
---|
| 174 | |
---|
[55b6476] | 175 | void TypeSubstitution::Substituter::handleAggregateType( const ReferenceToType * type ) { |
---|
[c671112] | 176 | GuardValue( boundVars ); |
---|
| 177 | // bind type variables from forall-qualifiers |
---|
| 178 | if ( freeOnly ) { |
---|
[55b6476] | 179 | for ( const TypeDecl * tyvar : type->forall ) { |
---|
| 180 | boundVars.insert( tyvar->name ); |
---|
[c671112] | 181 | } // for |
---|
| 182 | // bind type variables from generic type instantiations |
---|
[55b6476] | 183 | if ( auto decl = type->aggr() ) { |
---|
[417117e] | 184 | if ( ! type->params.empty() ) { |
---|
| 185 | for ( const TypeDecl * tyvar : decl->params ) { |
---|
[55b6476] | 186 | boundVars.insert( tyvar->name ); |
---|
| 187 | } // for |
---|
| 188 | } // if |
---|
| 189 | } |
---|
[c671112] | 190 | } // if |
---|
| 191 | } |
---|
| 192 | |
---|
[55b6476] | 193 | void TypeSubstitution::Substituter::previsit( const StructInstType * aggregateUseType ) { |
---|
[c671112] | 194 | handleAggregateType( aggregateUseType ); |
---|
| 195 | } |
---|
| 196 | |
---|
[55b6476] | 197 | void TypeSubstitution::Substituter::previsit( const UnionInstType *aggregateUseType ) { |
---|
[c671112] | 198 | handleAggregateType( aggregateUseType ); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | } // namespace ast |
---|
| 202 | |
---|
| 203 | // Local Variables: // |
---|
| 204 | // tab-width: 4 // |
---|
| 205 | // mode: c++ // |
---|
| 206 | // compile-command: "make install" // |
---|
| 207 | // End: // |
---|