[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 |
---|
[5382492] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Tue Apr 26 11:15:29 2016 |
---|
[4040425] | 13 | // Update Count : 3 |
---|
[0dd3a2f] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "Type.h" |
---|
| 17 | #include "TypeSubstitution.h" |
---|
| 18 | |
---|
[0dd3a2f] | 19 | TypeSubstitution::TypeSubstitution() { |
---|
| 20 | } |
---|
[51b7345] | 21 | |
---|
[0dd3a2f] | 22 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) { |
---|
| 23 | initialize( other, *this ); |
---|
[51b7345] | 24 | } |
---|
| 25 | |
---|
[0dd3a2f] | 26 | TypeSubstitution::~TypeSubstitution() { |
---|
| 27 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 28 | delete( i->second ); |
---|
| 29 | } |
---|
| 30 | for ( VarEnvType::iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
| 31 | delete( i->second ); |
---|
| 32 | } |
---|
[51b7345] | 33 | } |
---|
| 34 | |
---|
[0dd3a2f] | 35 | TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) { |
---|
| 36 | if ( this == &other ) return *this; |
---|
| 37 | initialize( other, *this ); |
---|
| 38 | return *this; |
---|
[51b7345] | 39 | } |
---|
| 40 | |
---|
[0dd3a2f] | 41 | void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { |
---|
| 42 | dest.typeEnv.clear(); |
---|
| 43 | dest.varEnv.clear(); |
---|
| 44 | dest.add( src ); |
---|
[51b7345] | 45 | } |
---|
| 46 | |
---|
[0dd3a2f] | 47 | void TypeSubstitution::add( const TypeSubstitution &other ) { |
---|
| 48 | for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) { |
---|
| 49 | typeEnv[ i->first ] = i->second->clone(); |
---|
| 50 | } // for |
---|
| 51 | for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) { |
---|
| 52 | varEnv[ i->first ] = i->second->clone(); |
---|
| 53 | } // for |
---|
[51b7345] | 54 | } |
---|
| 55 | |
---|
[0dd3a2f] | 56 | void TypeSubstitution::add( std::string formalType, Type *actualType ) { |
---|
| 57 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
| 58 | if ( i != typeEnv.end() ) { |
---|
| 59 | delete i->second; |
---|
| 60 | } // if |
---|
| 61 | typeEnv[ formalType ] = actualType->clone(); |
---|
[51b7345] | 62 | } |
---|
| 63 | |
---|
[0dd3a2f] | 64 | void TypeSubstitution::remove( std::string formalType ) { |
---|
| 65 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
| 66 | if ( i != typeEnv.end() ) { |
---|
| 67 | delete i->second; |
---|
| 68 | typeEnv.erase( formalType ); |
---|
| 69 | } // if |
---|
[51b7345] | 70 | } |
---|
| 71 | |
---|
[0dd3a2f] | 72 | Type *TypeSubstitution::lookup( std::string formalType ) const { |
---|
| 73 | TypeEnvType::const_iterator i = typeEnv.find( formalType ); |
---|
[084184b] | 74 | |
---|
| 75 | // break on not in substitution set |
---|
| 76 | if ( i == typeEnv.end() ) return 0; |
---|
| 77 | |
---|
| 78 | // attempt to transitively follow TypeInstType links. |
---|
| 79 | while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) { |
---|
| 80 | const std::string& typeName = actualType->get_name(); |
---|
| 81 | |
---|
| 82 | // break cycles in the transitive follow |
---|
| 83 | if ( formalType == typeName ) break; |
---|
| 84 | |
---|
| 85 | // Look for the type this maps to, returning previous mapping if none-such |
---|
| 86 | i = typeEnv.find( typeName ); |
---|
| 87 | if ( i == typeEnv.end() ) return actualType; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | // return type from substitution set |
---|
| 91 | return i->second; |
---|
| 92 | |
---|
| 93 | #if 0 |
---|
[0dd3a2f] | 94 | if ( i == typeEnv.end() ) { |
---|
| 95 | return 0; |
---|
| 96 | } else { |
---|
| 97 | return i->second; |
---|
| 98 | } // if |
---|
[084184b] | 99 | #endif |
---|
[51b7345] | 100 | } |
---|
| 101 | |
---|
[0dd3a2f] | 102 | bool TypeSubstitution::empty() const { |
---|
| 103 | return typeEnv.empty() && varEnv.empty(); |
---|
[51b7345] | 104 | } |
---|
| 105 | |
---|
[0dd3a2f] | 106 | void TypeSubstitution::normalize() { |
---|
| 107 | do { |
---|
| 108 | subCount = 0; |
---|
| 109 | freeOnly = true; |
---|
| 110 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 111 | i->second = i->second->acceptMutator( *this ); |
---|
| 112 | } |
---|
| 113 | } while ( subCount ); |
---|
[51b7345] | 114 | } |
---|
| 115 | |
---|
[0dd3a2f] | 116 | Type * TypeSubstitution::mutate( TypeInstType *inst ) { |
---|
| 117 | BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() ); |
---|
| 118 | if ( bound != boundVars.end() ) return inst; |
---|
[5382492] | 119 | |
---|
[0dd3a2f] | 120 | TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() ); |
---|
| 121 | if ( i == typeEnv.end() ) { |
---|
| 122 | return inst; |
---|
| 123 | } else { |
---|
[51b7345] | 124 | /// std::cout << "found " << inst->get_name() << ", replacing with "; |
---|
| 125 | /// i->second->print( std::cout ); |
---|
| 126 | /// std::cout << std::endl; |
---|
[0dd3a2f] | 127 | subCount++; |
---|
| 128 | Type *newtype = i->second->clone(); |
---|
| 129 | newtype->get_qualifiers() += inst->get_qualifiers(); |
---|
| 130 | delete inst; |
---|
| 131 | return newtype; |
---|
| 132 | } // if |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) { |
---|
| 136 | VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); |
---|
| 137 | if ( i == varEnv.end() ) { |
---|
| 138 | return nameExpr; |
---|
| 139 | } else { |
---|
| 140 | subCount++; |
---|
| 141 | delete nameExpr; |
---|
| 142 | return i->second->clone(); |
---|
| 143 | } // if |
---|
[51b7345] | 144 | } |
---|
| 145 | |
---|
| 146 | template< typename TypeClass > |
---|
[0dd3a2f] | 147 | Type *TypeSubstitution::handleType( TypeClass *type ) { |
---|
| 148 | BoundVarsType oldBoundVars( boundVars ); |
---|
[37a3b8f9] | 149 | // bind type variables from forall-qualifiers |
---|
[0dd3a2f] | 150 | if ( freeOnly ) { |
---|
| 151 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
| 152 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 153 | } // for |
---|
| 154 | } // if |
---|
| 155 | Type *ret = Mutator::mutate( type ); |
---|
| 156 | boundVars = oldBoundVars; |
---|
| 157 | return ret; |
---|
[51b7345] | 158 | } |
---|
| 159 | |
---|
[37a3b8f9] | 160 | template< typename TypeClass > |
---|
| 161 | Type *TypeSubstitution::handleAggregateType( TypeClass *type ) { |
---|
| 162 | BoundVarsType oldBoundVars( boundVars ); |
---|
| 163 | // bind type variables from forall-qualifiers |
---|
| 164 | if ( freeOnly ) { |
---|
| 165 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
| 166 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 167 | } // for |
---|
| 168 | } // if |
---|
| 169 | // bind type variables from generic type instantiations |
---|
[ed94eac] | 170 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
| 171 | if ( baseParameters && ! type->get_parameters().empty() ) { |
---|
| 172 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
| 173 | boundVars.insert( (*tyvar)->get_name() ); |
---|
| 174 | } // for |
---|
| 175 | } // if |
---|
[37a3b8f9] | 176 | Type *ret = Mutator::mutate( type ); |
---|
| 177 | boundVars = oldBoundVars; |
---|
| 178 | return ret; |
---|
| 179 | } |
---|
| 180 | |
---|
[0dd3a2f] | 181 | Type * TypeSubstitution::mutate( VoidType *basicType ) { |
---|
| 182 | return handleType( basicType ); |
---|
[51b7345] | 183 | } |
---|
| 184 | |
---|
[0dd3a2f] | 185 | Type * TypeSubstitution::mutate( BasicType *basicType ) { |
---|
| 186 | return handleType( basicType ); |
---|
[51b7345] | 187 | } |
---|
| 188 | |
---|
[0dd3a2f] | 189 | Type * TypeSubstitution::mutate( PointerType *pointerType ) { |
---|
| 190 | return handleType( pointerType ); |
---|
[51b7345] | 191 | } |
---|
| 192 | |
---|
[0dd3a2f] | 193 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) { |
---|
| 194 | return handleType( arrayType ); |
---|
[51b7345] | 195 | } |
---|
| 196 | |
---|
[0dd3a2f] | 197 | Type * TypeSubstitution::mutate( FunctionType *functionType ) { |
---|
| 198 | return handleType( functionType ); |
---|
[51b7345] | 199 | } |
---|
| 200 | |
---|
[0dd3a2f] | 201 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 202 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 203 | } |
---|
| 204 | |
---|
[0dd3a2f] | 205 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 206 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 207 | } |
---|
| 208 | |
---|
[0dd3a2f] | 209 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { |
---|
| 210 | return handleType( aggregateUseType ); |
---|
[51b7345] | 211 | } |
---|
| 212 | |
---|
[4040425] | 213 | Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) { |
---|
[0dd3a2f] | 214 | return handleType( aggregateUseType ); |
---|
[51b7345] | 215 | } |
---|
| 216 | |
---|
[0dd3a2f] | 217 | Type * TypeSubstitution::mutate( TupleType *tupleType ) { |
---|
| 218 | return handleType( tupleType ); |
---|
[51b7345] | 219 | } |
---|
| 220 | |
---|
[44b7088] | 221 | Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) { |
---|
| 222 | return handleType( varArgsType ); |
---|
| 223 | } |
---|
| 224 | |
---|
[0dd3a2f] | 225 | void TypeSubstitution::print( std::ostream &os, int indent ) const { |
---|
| 226 | os << std::string( indent, ' ' ) << "Types:" << std::endl; |
---|
| 227 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 228 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 229 | i->second->print( os, indent+4 ); |
---|
| 230 | os << std::endl; |
---|
| 231 | } // for |
---|
| 232 | os << std::string( indent, ' ' ) << "Non-types:" << std::endl; |
---|
| 233 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
| 234 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 235 | i->second->print( os, indent+4 ); |
---|
| 236 | os << std::endl; |
---|
| 237 | } // for |
---|
[51b7345] | 238 | } |
---|
| 239 | |
---|
[5382492] | 240 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) { |
---|
| 241 | sub.print( out ); |
---|
| 242 | return out; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
[0dd3a2f] | 246 | // Local Variables: // |
---|
| 247 | // tab-width: 4 // |
---|
| 248 | // mode: c++ // |
---|
| 249 | // compile-command: "make install" // |
---|
| 250 | // End: // |
---|