[0dd3a2f] | 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 | // |
---|
[5382492] | 7 | // TypeSubstitution.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[6f95000] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Mar 16 15:54:35 2017 |
---|
| 13 | // Update Count : 4 |
---|
[0dd3a2f] | 14 | // |
---|
[51b7345] | 15 | |
---|
[ea6332d] | 16 | #include <ostream> // for ostream, basic_ostream, operator<<, endl |
---|
| 17 | |
---|
| 18 | #include "Type.h" // for TypeInstType, Type, StructInstType, UnionInstType |
---|
[51b7345] | 19 | #include "TypeSubstitution.h" |
---|
| 20 | |
---|
[0dd3a2f] | 21 | TypeSubstitution::TypeSubstitution() { |
---|
| 22 | } |
---|
[51b7345] | 23 | |
---|
[0dd3a2f] | 24 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) { |
---|
| 25 | initialize( other, *this ); |
---|
[51b7345] | 26 | } |
---|
| 27 | |
---|
[0dd3a2f] | 28 | TypeSubstitution::~TypeSubstitution() { |
---|
| 29 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 30 | delete( i->second ); |
---|
| 31 | } |
---|
| 32 | for ( VarEnvType::iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
| 33 | delete( i->second ); |
---|
| 34 | } |
---|
[51b7345] | 35 | } |
---|
| 36 | |
---|
[0dd3a2f] | 37 | TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) { |
---|
| 38 | if ( this == &other ) return *this; |
---|
| 39 | initialize( other, *this ); |
---|
| 40 | return *this; |
---|
[51b7345] | 41 | } |
---|
| 42 | |
---|
[0dd3a2f] | 43 | void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { |
---|
| 44 | dest.typeEnv.clear(); |
---|
| 45 | dest.varEnv.clear(); |
---|
| 46 | dest.add( src ); |
---|
[51b7345] | 47 | } |
---|
| 48 | |
---|
[0dd3a2f] | 49 | void TypeSubstitution::add( const TypeSubstitution &other ) { |
---|
| 50 | for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) { |
---|
| 51 | typeEnv[ i->first ] = i->second->clone(); |
---|
| 52 | } // for |
---|
| 53 | for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) { |
---|
| 54 | varEnv[ i->first ] = i->second->clone(); |
---|
| 55 | } // for |
---|
[51b7345] | 56 | } |
---|
| 57 | |
---|
[0dd3a2f] | 58 | void TypeSubstitution::add( std::string formalType, Type *actualType ) { |
---|
| 59 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
| 60 | if ( i != typeEnv.end() ) { |
---|
| 61 | delete i->second; |
---|
| 62 | } // if |
---|
| 63 | typeEnv[ formalType ] = actualType->clone(); |
---|
[51b7345] | 64 | } |
---|
| 65 | |
---|
[172d9342] | 66 | void TypeSubstitution::addVar( std::string formalExpr, Expression *actualExpr ) { |
---|
| 67 | varEnv[ formalExpr ] = actualExpr; |
---|
| 68 | } |
---|
| 69 | |
---|
[0dd3a2f] | 70 | void TypeSubstitution::remove( std::string formalType ) { |
---|
| 71 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
| 72 | if ( i != typeEnv.end() ) { |
---|
| 73 | delete i->second; |
---|
| 74 | typeEnv.erase( formalType ); |
---|
| 75 | } // if |
---|
[51b7345] | 76 | } |
---|
| 77 | |
---|
[0dd3a2f] | 78 | Type *TypeSubstitution::lookup( std::string formalType ) const { |
---|
| 79 | TypeEnvType::const_iterator i = typeEnv.find( formalType ); |
---|
[8c49c0e] | 80 | |
---|
[084184b] | 81 | // break on not in substitution set |
---|
| 82 | if ( i == typeEnv.end() ) return 0; |
---|
[8c49c0e] | 83 | |
---|
[084184b] | 84 | // attempt to transitively follow TypeInstType links. |
---|
| 85 | while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) { |
---|
| 86 | const std::string& typeName = actualType->get_name(); |
---|
[8c49c0e] | 87 | |
---|
[084184b] | 88 | // break cycles in the transitive follow |
---|
| 89 | if ( formalType == typeName ) break; |
---|
[8c49c0e] | 90 | |
---|
[084184b] | 91 | // Look for the type this maps to, returning previous mapping if none-such |
---|
| 92 | i = typeEnv.find( typeName ); |
---|
| 93 | if ( i == typeEnv.end() ) return actualType; |
---|
| 94 | } |
---|
[8c49c0e] | 95 | |
---|
[084184b] | 96 | // return type from substitution set |
---|
| 97 | return i->second; |
---|
[8c49c0e] | 98 | |
---|
[084184b] | 99 | #if 0 |
---|
[0dd3a2f] | 100 | if ( i == typeEnv.end() ) { |
---|
| 101 | return 0; |
---|
| 102 | } else { |
---|
| 103 | return i->second; |
---|
| 104 | } // if |
---|
[084184b] | 105 | #endif |
---|
[51b7345] | 106 | } |
---|
| 107 | |
---|
[0dd3a2f] | 108 | bool TypeSubstitution::empty() const { |
---|
| 109 | return typeEnv.empty() && varEnv.empty(); |
---|
[51b7345] | 110 | } |
---|
| 111 | |
---|
[2ec65ad] | 112 | namespace { |
---|
| 113 | struct EnvTrimmer { |
---|
[02fdb8e] | 114 | const TypeSubstitution * env; |
---|
| 115 | TypeSubstitution * newEnv; |
---|
| 116 | EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} |
---|
[2ec65ad] | 117 | void previsit( TypeDecl * tyDecl ) { |
---|
| 118 | // transfer known bindings for seen type variables |
---|
| 119 | if ( Type * t = env->lookup( tyDecl->name ) ) { |
---|
| 120 | newEnv->add( tyDecl->name, t ); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | }; |
---|
| 124 | } // namespace |
---|
| 125 | |
---|
| 126 | /// reduce environment to just the parts that are referenced in a given expression |
---|
[02fdb8e] | 127 | TypeSubstitution * TypeSubstitution::newFromExpr( Expression * expr, const TypeSubstitution * env ) { |
---|
[2ec65ad] | 128 | if ( env ) { |
---|
| 129 | TypeSubstitution * newEnv = new TypeSubstitution(); |
---|
| 130 | PassVisitor<EnvTrimmer> trimmer( env, newEnv ); |
---|
| 131 | expr->accept( trimmer ); |
---|
| 132 | return newEnv; |
---|
| 133 | } |
---|
| 134 | return nullptr; |
---|
| 135 | } |
---|
| 136 | |
---|
[0dd3a2f] | 137 | void TypeSubstitution::normalize() { |
---|
[e9a715d3] | 138 | PassVisitor<Substituter> sub( *this, true ); |
---|
[0dd3a2f] | 139 | do { |
---|
[e9a715d3] | 140 | sub.pass.subCount = 0; |
---|
| 141 | sub.pass.freeOnly = true; |
---|
[0dd3a2f] | 142 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
[e9a715d3] | 143 | i->second = i->second->acceptMutator( sub ); |
---|
[0dd3a2f] | 144 | } |
---|
[e9a715d3] | 145 | } while ( sub.pass.subCount ); |
---|
[51b7345] | 146 | } |
---|
| 147 | |
---|
[e9a715d3] | 148 | Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) { |
---|
| 149 | BoundVarsType::const_iterator bound = boundVars.find( inst->name ); |
---|
[0dd3a2f] | 150 | if ( bound != boundVars.end() ) return inst; |
---|
[5382492] | 151 | |
---|
[e9a715d3] | 152 | TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() ); |
---|
| 153 | if ( i == sub.typeEnv.end() ) { |
---|
[0dd3a2f] | 154 | return inst; |
---|
| 155 | } else { |
---|
[d0d5054] | 156 | // cut off infinite loop for the case where a type is bound to itself. |
---|
| 157 | // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here. |
---|
| 158 | // TODO: investigate preventing type variables from being bound to themselves in the first place. |
---|
| 159 | if ( TypeInstType * replacement = dynamic_cast< TypeInstType * >( i->second ) ) { |
---|
| 160 | if ( inst->name == replacement->name ) { |
---|
| 161 | return inst; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl; |
---|
[0dd3a2f] | 165 | subCount++; |
---|
[e9a715d3] | 166 | Type * newtype = i->second->clone(); |
---|
[6f95000] | 167 | newtype->get_qualifiers() |= inst->get_qualifiers(); |
---|
[0dd3a2f] | 168 | delete inst; |
---|
[d0d5054] | 169 | // Note: need to recursively apply substitution to the new type because normalize does not substitute bound vars, but bound vars must be substituted when not in freeOnly mode. |
---|
| 170 | return newtype->acceptMutator( *visitor ); |
---|
[0dd3a2f] | 171 | } // if |
---|
| 172 | } |
---|
| 173 | |
---|
[e9a715d3] | 174 | Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) { |
---|
| 175 | VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); |
---|
| 176 | if ( i == sub.varEnv.end() ) { |
---|
[0dd3a2f] | 177 | return nameExpr; |
---|
| 178 | } else { |
---|
| 179 | subCount++; |
---|
| 180 | delete nameExpr; |
---|
| 181 | return i->second->clone(); |
---|
| 182 | } // if |
---|
[51b7345] | 183 | } |
---|
| 184 | |
---|
[e9a715d3] | 185 | void TypeSubstitution::Substituter::premutate( Type * type ) { |
---|
| 186 | GuardValue( boundVars ); |
---|
[37a3b8f9] | 187 | // bind type variables from forall-qualifiers |
---|
[0dd3a2f] | 188 | if ( freeOnly ) { |
---|
[e9a715d3] | 189 | for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { |
---|
| 190 | boundVars.insert( (*tyvar)->name ); |
---|
[0dd3a2f] | 191 | } // for |
---|
| 192 | } // if |
---|
[51b7345] | 193 | } |
---|
| 194 | |
---|
[37a3b8f9] | 195 | template< typename TypeClass > |
---|
[e9a715d3] | 196 | void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) { |
---|
| 197 | GuardValue( boundVars ); |
---|
[37a3b8f9] | 198 | // bind type variables from forall-qualifiers |
---|
| 199 | if ( freeOnly ) { |
---|
[e9a715d3] | 200 | for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { |
---|
| 201 | boundVars.insert( (*tyvar)->name ); |
---|
[37a3b8f9] | 202 | } // for |
---|
[c7a3081] | 203 | // bind type variables from generic type instantiations |
---|
| 204 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
[e9a715d3] | 205 | if ( baseParameters && ! type->parameters.empty() ) { |
---|
[c7a3081] | 206 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
[e9a715d3] | 207 | boundVars.insert( (*tyvar)->name ); |
---|
[c7a3081] | 208 | } // for |
---|
| 209 | } // if |
---|
[ed94eac] | 210 | } // if |
---|
[44b7088] | 211 | } |
---|
| 212 | |
---|
[e9a715d3] | 213 | void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) { |
---|
| 214 | handleAggregateType( aggregateUseType ); |
---|
[89e6ffc] | 215 | } |
---|
| 216 | |
---|
[e9a715d3] | 217 | void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) { |
---|
| 218 | handleAggregateType( aggregateUseType ); |
---|
[89e6ffc] | 219 | } |
---|
| 220 | |
---|
[50377a4] | 221 | void TypeSubstitution::print( std::ostream &os, Indenter indent ) const { |
---|
| 222 | os << indent << "Types:" << std::endl; |
---|
[0dd3a2f] | 223 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
[50377a4] | 224 | os << indent+1 << i->first << " -> "; |
---|
| 225 | i->second->print( os, indent+2 ); |
---|
[0dd3a2f] | 226 | os << std::endl; |
---|
| 227 | } // for |
---|
[50377a4] | 228 | os << indent << "Non-types:" << std::endl; |
---|
[0dd3a2f] | 229 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
[50377a4] | 230 | os << indent+1 << i->first << " -> "; |
---|
| 231 | i->second->print( os, indent+2 ); |
---|
[0dd3a2f] | 232 | os << std::endl; |
---|
| 233 | } // for |
---|
[51b7345] | 234 | } |
---|
| 235 | |
---|
[5382492] | 236 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) { |
---|
| 237 | sub.print( out ); |
---|
| 238 | return out; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
[0dd3a2f] | 242 | // Local Variables: // |
---|
| 243 | // tab-width: 4 // |
---|
| 244 | // mode: c++ // |
---|
| 245 | // compile-command: "make install" // |
---|
| 246 | // End: // |
---|