[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 { |
---|
[e9a715d3] | 134 | /// std::cerr << "found " << inst->get_name() << ", replacing with "; |
---|
| 135 | /// i->second->print( std::cerr ); |
---|
| 136 | /// std::cerr << std::endl; |
---|
[0dd3a2f] | 137 | subCount++; |
---|
[e9a715d3] | 138 | Type * newtype = i->second->clone(); |
---|
[6f95000] | 139 | newtype->get_qualifiers() |= inst->get_qualifiers(); |
---|
[0dd3a2f] | 140 | return newtype; |
---|
| 141 | } // if |
---|
| 142 | } |
---|
| 143 | |
---|
[e9a715d3] | 144 | Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) { |
---|
| 145 | VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); |
---|
| 146 | if ( i == sub.varEnv.end() ) { |
---|
[0dd3a2f] | 147 | return nameExpr; |
---|
| 148 | } else { |
---|
| 149 | subCount++; |
---|
| 150 | return i->second->clone(); |
---|
| 151 | } // if |
---|
[51b7345] | 152 | } |
---|
| 153 | |
---|
[e9a715d3] | 154 | void TypeSubstitution::Substituter::premutate( Type * type ) { |
---|
| 155 | GuardValue( boundVars ); |
---|
[37a3b8f9] | 156 | // bind type variables from forall-qualifiers |
---|
[0dd3a2f] | 157 | if ( freeOnly ) { |
---|
[e9a715d3] | 158 | for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { |
---|
| 159 | boundVars.insert( (*tyvar)->name ); |
---|
[0dd3a2f] | 160 | } // for |
---|
| 161 | } // if |
---|
[51b7345] | 162 | } |
---|
| 163 | |
---|
[37a3b8f9] | 164 | template< typename TypeClass > |
---|
[e9a715d3] | 165 | void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) { |
---|
| 166 | GuardValue( boundVars ); |
---|
[37a3b8f9] | 167 | // bind type variables from forall-qualifiers |
---|
| 168 | if ( freeOnly ) { |
---|
[e9a715d3] | 169 | for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { |
---|
| 170 | boundVars.insert( (*tyvar)->name ); |
---|
[37a3b8f9] | 171 | } // for |
---|
[c7a3081] | 172 | // bind type variables from generic type instantiations |
---|
| 173 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
[e9a715d3] | 174 | if ( baseParameters && ! type->parameters.empty() ) { |
---|
[c7a3081] | 175 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
[e9a715d3] | 176 | boundVars.insert( (*tyvar)->name ); |
---|
[c7a3081] | 177 | } // for |
---|
| 178 | } // if |
---|
[ed94eac] | 179 | } // if |
---|
[44b7088] | 180 | } |
---|
| 181 | |
---|
[e9a715d3] | 182 | void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) { |
---|
| 183 | handleAggregateType( aggregateUseType ); |
---|
[89e6ffc] | 184 | } |
---|
| 185 | |
---|
[e9a715d3] | 186 | void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) { |
---|
| 187 | handleAggregateType( aggregateUseType ); |
---|
[89e6ffc] | 188 | } |
---|
| 189 | |
---|
[50377a4] | 190 | void TypeSubstitution::print( std::ostream &os, Indenter indent ) const { |
---|
| 191 | os << indent << "Types:" << std::endl; |
---|
[0dd3a2f] | 192 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
[50377a4] | 193 | os << indent+1 << i->first << " -> "; |
---|
| 194 | i->second->print( os, indent+2 ); |
---|
[0dd3a2f] | 195 | os << std::endl; |
---|
| 196 | } // for |
---|
[50377a4] | 197 | os << indent << "Non-types:" << std::endl; |
---|
[0dd3a2f] | 198 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
[50377a4] | 199 | os << indent+1 << i->first << " -> "; |
---|
| 200 | i->second->print( os, indent+2 ); |
---|
[0dd3a2f] | 201 | os << std::endl; |
---|
| 202 | } // for |
---|
[51b7345] | 203 | } |
---|
| 204 | |
---|
[5382492] | 205 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) { |
---|
| 206 | sub.print( out ); |
---|
| 207 | return out; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
[0dd3a2f] | 211 | // Local Variables: // |
---|
| 212 | // tab-width: 4 // |
---|
| 213 | // mode: c++ // |
---|
| 214 | // compile-command: "make install" // |
---|
| 215 | // End: // |
---|