[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 | |
---|
[0dd3a2f] | 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 |
---|
[51b7345] | 72 | } |
---|
| 73 | |
---|
[0dd3a2f] | 74 | Type *TypeSubstitution::lookup( std::string formalType ) const { |
---|
| 75 | TypeEnvType::const_iterator i = typeEnv.find( formalType ); |
---|
[8c49c0e] | 76 | |
---|
[084184b] | 77 | // break on not in substitution set |
---|
| 78 | if ( i == typeEnv.end() ) return 0; |
---|
[8c49c0e] | 79 | |
---|
[084184b] | 80 | // attempt to transitively follow TypeInstType links. |
---|
| 81 | while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) { |
---|
| 82 | const std::string& typeName = actualType->get_name(); |
---|
[8c49c0e] | 83 | |
---|
[084184b] | 84 | // break cycles in the transitive follow |
---|
| 85 | if ( formalType == typeName ) break; |
---|
[8c49c0e] | 86 | |
---|
[084184b] | 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 | } |
---|
[8c49c0e] | 91 | |
---|
[084184b] | 92 | // return type from substitution set |
---|
| 93 | return i->second; |
---|
[8c49c0e] | 94 | |
---|
[084184b] | 95 | #if 0 |
---|
[0dd3a2f] | 96 | if ( i == typeEnv.end() ) { |
---|
| 97 | return 0; |
---|
| 98 | } else { |
---|
| 99 | return i->second; |
---|
| 100 | } // if |
---|
[084184b] | 101 | #endif |
---|
[51b7345] | 102 | } |
---|
| 103 | |
---|
[0dd3a2f] | 104 | bool TypeSubstitution::empty() const { |
---|
| 105 | return typeEnv.empty() && varEnv.empty(); |
---|
[51b7345] | 106 | } |
---|
| 107 | |
---|
[0dd3a2f] | 108 | void TypeSubstitution::normalize() { |
---|
| 109 | do { |
---|
| 110 | subCount = 0; |
---|
| 111 | freeOnly = true; |
---|
| 112 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 113 | i->second = i->second->acceptMutator( *this ); |
---|
| 114 | } |
---|
| 115 | } while ( subCount ); |
---|
[51b7345] | 116 | } |
---|
| 117 | |
---|
[0dd3a2f] | 118 | Type * TypeSubstitution::mutate( TypeInstType *inst ) { |
---|
| 119 | BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() ); |
---|
| 120 | if ( bound != boundVars.end() ) return inst; |
---|
[5382492] | 121 | |
---|
[0dd3a2f] | 122 | TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() ); |
---|
| 123 | if ( i == typeEnv.end() ) { |
---|
| 124 | return inst; |
---|
| 125 | } else { |
---|
[51b7345] | 126 | /// std::cout << "found " << inst->get_name() << ", replacing with "; |
---|
| 127 | /// i->second->print( std::cout ); |
---|
| 128 | /// std::cout << std::endl; |
---|
[0dd3a2f] | 129 | subCount++; |
---|
| 130 | Type *newtype = i->second->clone(); |
---|
[6f95000] | 131 | newtype->get_qualifiers() |= inst->get_qualifiers(); |
---|
[0dd3a2f] | 132 | delete inst; |
---|
| 133 | return newtype; |
---|
| 134 | } // if |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) { |
---|
| 138 | VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); |
---|
| 139 | if ( i == varEnv.end() ) { |
---|
| 140 | return nameExpr; |
---|
| 141 | } else { |
---|
| 142 | subCount++; |
---|
| 143 | delete nameExpr; |
---|
| 144 | return i->second->clone(); |
---|
| 145 | } // if |
---|
[51b7345] | 146 | } |
---|
| 147 | |
---|
| 148 | template< typename TypeClass > |
---|
[0dd3a2f] | 149 | Type *TypeSubstitution::handleType( TypeClass *type ) { |
---|
| 150 | BoundVarsType oldBoundVars( boundVars ); |
---|
[37a3b8f9] | 151 | // bind type variables from forall-qualifiers |
---|
[0dd3a2f] | 152 | if ( freeOnly ) { |
---|
[8c49c0e] | 153 | for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
[0dd3a2f] | 154 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 155 | } // for |
---|
| 156 | } // if |
---|
| 157 | Type *ret = Mutator::mutate( type ); |
---|
| 158 | boundVars = oldBoundVars; |
---|
| 159 | return ret; |
---|
[51b7345] | 160 | } |
---|
| 161 | |
---|
[37a3b8f9] | 162 | template< typename TypeClass > |
---|
| 163 | Type *TypeSubstitution::handleAggregateType( TypeClass *type ) { |
---|
| 164 | BoundVarsType oldBoundVars( boundVars ); |
---|
| 165 | // bind type variables from forall-qualifiers |
---|
| 166 | if ( freeOnly ) { |
---|
[8c49c0e] | 167 | for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
[37a3b8f9] | 168 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 169 | } // for |
---|
[c7a3081] | 170 | // bind type variables from generic type instantiations |
---|
| 171 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
| 172 | if ( baseParameters && ! type->get_parameters().empty() ) { |
---|
| 173 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
| 174 | boundVars.insert( (*tyvar)->get_name() ); |
---|
| 175 | } // for |
---|
| 176 | } // if |
---|
[ed94eac] | 177 | } // if |
---|
[37a3b8f9] | 178 | Type *ret = Mutator::mutate( type ); |
---|
| 179 | boundVars = oldBoundVars; |
---|
| 180 | return ret; |
---|
| 181 | } |
---|
| 182 | |
---|
[89e6ffc] | 183 | Type * TypeSubstitution::mutate( VoidType *voidType ) { |
---|
| 184 | return handleType( voidType ); |
---|
[51b7345] | 185 | } |
---|
| 186 | |
---|
[0dd3a2f] | 187 | Type * TypeSubstitution::mutate( BasicType *basicType ) { |
---|
| 188 | return handleType( basicType ); |
---|
[51b7345] | 189 | } |
---|
| 190 | |
---|
[0dd3a2f] | 191 | Type * TypeSubstitution::mutate( PointerType *pointerType ) { |
---|
| 192 | return handleType( pointerType ); |
---|
[51b7345] | 193 | } |
---|
| 194 | |
---|
[0dd3a2f] | 195 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) { |
---|
| 196 | return handleType( arrayType ); |
---|
[51b7345] | 197 | } |
---|
| 198 | |
---|
[0dd3a2f] | 199 | Type * TypeSubstitution::mutate( FunctionType *functionType ) { |
---|
| 200 | return handleType( functionType ); |
---|
[51b7345] | 201 | } |
---|
| 202 | |
---|
[0dd3a2f] | 203 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 204 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 205 | } |
---|
| 206 | |
---|
[0dd3a2f] | 207 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 208 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 209 | } |
---|
| 210 | |
---|
[0dd3a2f] | 211 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { |
---|
| 212 | return handleType( aggregateUseType ); |
---|
[51b7345] | 213 | } |
---|
| 214 | |
---|
[4040425] | 215 | Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) { |
---|
[0dd3a2f] | 216 | return handleType( aggregateUseType ); |
---|
[51b7345] | 217 | } |
---|
| 218 | |
---|
[0dd3a2f] | 219 | Type * TypeSubstitution::mutate( TupleType *tupleType ) { |
---|
| 220 | return handleType( tupleType ); |
---|
[51b7345] | 221 | } |
---|
| 222 | |
---|
[44b7088] | 223 | Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) { |
---|
| 224 | return handleType( varArgsType ); |
---|
| 225 | } |
---|
| 226 | |
---|
[89e6ffc] | 227 | Type * TypeSubstitution::mutate( ZeroType *zeroType ) { |
---|
| 228 | return handleType( zeroType ); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | Type * TypeSubstitution::mutate( OneType *oneType ) { |
---|
| 232 | return handleType( oneType ); |
---|
| 233 | } |
---|
| 234 | |
---|
[e33f321] | 235 | TypeSubstitution * TypeSubstitution::acceptMutator( Mutator & mutator ) { |
---|
| 236 | for ( auto & p : typeEnv ) { |
---|
| 237 | p.second = maybeMutate( p.second, mutator ); |
---|
| 238 | } |
---|
| 239 | for ( auto & p : varEnv ) { |
---|
| 240 | p.second = maybeMutate( p.second, mutator ); |
---|
| 241 | } |
---|
| 242 | return this; |
---|
| 243 | } |
---|
| 244 | |
---|
[0dd3a2f] | 245 | void TypeSubstitution::print( std::ostream &os, int indent ) const { |
---|
| 246 | os << std::string( indent, ' ' ) << "Types:" << std::endl; |
---|
| 247 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 248 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 249 | i->second->print( os, indent+4 ); |
---|
| 250 | os << std::endl; |
---|
| 251 | } // for |
---|
| 252 | os << std::string( indent, ' ' ) << "Non-types:" << std::endl; |
---|
| 253 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
| 254 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 255 | i->second->print( os, indent+4 ); |
---|
| 256 | os << std::endl; |
---|
| 257 | } // for |
---|
[51b7345] | 258 | } |
---|
| 259 | |
---|
[5382492] | 260 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) { |
---|
| 261 | sub.print( out ); |
---|
| 262 | return out; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
[0dd3a2f] | 266 | // Local Variables: // |
---|
| 267 | // tab-width: 4 // |
---|
| 268 | // mode: c++ // |
---|
| 269 | // compile-command: "make install" // |
---|
| 270 | // End: // |
---|