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