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