[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 | // |
---|
| 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 : Mon May 18 11:10:04 2015 |
---|
| 13 | // Update Count : 2 |
---|
| 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 ); |
---|
| 74 | if ( i == typeEnv.end() ) { |
---|
| 75 | return 0; |
---|
| 76 | } else { |
---|
| 77 | return i->second; |
---|
| 78 | } // if |
---|
[51b7345] | 79 | } |
---|
| 80 | |
---|
[0dd3a2f] | 81 | bool TypeSubstitution::empty() const { |
---|
| 82 | return typeEnv.empty() && varEnv.empty(); |
---|
[51b7345] | 83 | } |
---|
| 84 | |
---|
[0dd3a2f] | 85 | void TypeSubstitution::normalize() { |
---|
| 86 | do { |
---|
| 87 | subCount = 0; |
---|
| 88 | freeOnly = true; |
---|
| 89 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 90 | i->second = i->second->acceptMutator( *this ); |
---|
| 91 | } |
---|
| 92 | } while ( subCount ); |
---|
[51b7345] | 93 | } |
---|
| 94 | |
---|
[0dd3a2f] | 95 | Type * TypeSubstitution::mutate( TypeInstType *inst ) { |
---|
| 96 | BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() ); |
---|
| 97 | if ( bound != boundVars.end() ) return inst; |
---|
| 98 | |
---|
| 99 | TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() ); |
---|
| 100 | if ( i == typeEnv.end() ) { |
---|
| 101 | return inst; |
---|
| 102 | } else { |
---|
[51b7345] | 103 | /// std::cout << "found " << inst->get_name() << ", replacing with "; |
---|
| 104 | /// i->second->print( std::cout ); |
---|
| 105 | /// std::cout << std::endl; |
---|
[0dd3a2f] | 106 | subCount++; |
---|
| 107 | Type *newtype = i->second->clone(); |
---|
| 108 | newtype->get_qualifiers() += inst->get_qualifiers(); |
---|
| 109 | delete inst; |
---|
| 110 | return newtype; |
---|
| 111 | } // if |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) { |
---|
| 115 | VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); |
---|
| 116 | if ( i == varEnv.end() ) { |
---|
| 117 | return nameExpr; |
---|
| 118 | } else { |
---|
| 119 | subCount++; |
---|
| 120 | delete nameExpr; |
---|
| 121 | return i->second->clone(); |
---|
| 122 | } // if |
---|
[51b7345] | 123 | } |
---|
| 124 | |
---|
| 125 | template< typename TypeClass > |
---|
[0dd3a2f] | 126 | Type *TypeSubstitution::handleType( TypeClass *type ) { |
---|
| 127 | BoundVarsType oldBoundVars( boundVars ); |
---|
[37a3b8f9] | 128 | // bind type variables from forall-qualifiers |
---|
[0dd3a2f] | 129 | if ( freeOnly ) { |
---|
| 130 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
| 131 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 132 | } // for |
---|
| 133 | } // if |
---|
| 134 | Type *ret = Mutator::mutate( type ); |
---|
| 135 | boundVars = oldBoundVars; |
---|
| 136 | return ret; |
---|
[51b7345] | 137 | } |
---|
| 138 | |
---|
[37a3b8f9] | 139 | template< typename TypeClass > |
---|
| 140 | Type *TypeSubstitution::handleAggregateType( TypeClass *type ) { |
---|
| 141 | BoundVarsType oldBoundVars( boundVars ); |
---|
| 142 | // bind type variables from forall-qualifiers |
---|
| 143 | if ( freeOnly ) { |
---|
| 144 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
| 145 | boundVars.insert( (*tyvar )->get_name() ); |
---|
| 146 | } // for |
---|
| 147 | } // if |
---|
| 148 | // bind type variables from generic type instantiations |
---|
[ed94eac] | 149 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
| 150 | if ( baseParameters && ! type->get_parameters().empty() ) { |
---|
| 151 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
| 152 | boundVars.insert( (*tyvar)->get_name() ); |
---|
| 153 | } // for |
---|
| 154 | } // if |
---|
[37a3b8f9] | 155 | Type *ret = Mutator::mutate( type ); |
---|
| 156 | boundVars = oldBoundVars; |
---|
| 157 | return ret; |
---|
| 158 | } |
---|
| 159 | |
---|
[0dd3a2f] | 160 | Type * TypeSubstitution::mutate( VoidType *basicType ) { |
---|
| 161 | return handleType( basicType ); |
---|
[51b7345] | 162 | } |
---|
| 163 | |
---|
[0dd3a2f] | 164 | Type * TypeSubstitution::mutate( BasicType *basicType ) { |
---|
| 165 | return handleType( basicType ); |
---|
[51b7345] | 166 | } |
---|
| 167 | |
---|
[0dd3a2f] | 168 | Type * TypeSubstitution::mutate( PointerType *pointerType ) { |
---|
| 169 | return handleType( pointerType ); |
---|
[51b7345] | 170 | } |
---|
| 171 | |
---|
[0dd3a2f] | 172 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) { |
---|
| 173 | return handleType( arrayType ); |
---|
[51b7345] | 174 | } |
---|
| 175 | |
---|
[0dd3a2f] | 176 | Type * TypeSubstitution::mutate( FunctionType *functionType ) { |
---|
| 177 | return handleType( functionType ); |
---|
[51b7345] | 178 | } |
---|
| 179 | |
---|
[0dd3a2f] | 180 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 181 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 182 | } |
---|
| 183 | |
---|
[0dd3a2f] | 184 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { |
---|
[37a3b8f9] | 185 | return handleAggregateType( aggregateUseType ); |
---|
[51b7345] | 186 | } |
---|
| 187 | |
---|
[0dd3a2f] | 188 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { |
---|
| 189 | return handleType( aggregateUseType ); |
---|
[51b7345] | 190 | } |
---|
| 191 | |
---|
[0dd3a2f] | 192 | Type * TypeSubstitution::mutate( ContextInstType *aggregateUseType ) { |
---|
| 193 | return handleType( aggregateUseType ); |
---|
[51b7345] | 194 | } |
---|
| 195 | |
---|
[0dd3a2f] | 196 | Type * TypeSubstitution::mutate( TupleType *tupleType ) { |
---|
| 197 | return handleType( tupleType ); |
---|
[51b7345] | 198 | } |
---|
| 199 | |
---|
[44b7088] | 200 | Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) { |
---|
| 201 | return handleType( varArgsType ); |
---|
| 202 | } |
---|
| 203 | |
---|
[0dd3a2f] | 204 | void TypeSubstitution::print( std::ostream &os, int indent ) const { |
---|
| 205 | os << std::string( indent, ' ' ) << "Types:" << std::endl; |
---|
| 206 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
| 207 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 208 | i->second->print( os, indent+4 ); |
---|
| 209 | os << std::endl; |
---|
| 210 | } // for |
---|
| 211 | os << std::string( indent, ' ' ) << "Non-types:" << std::endl; |
---|
| 212 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
| 213 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
| 214 | i->second->print( os, indent+4 ); |
---|
| 215 | os << std::endl; |
---|
| 216 | } // for |
---|
[51b7345] | 217 | } |
---|
| 218 | |
---|
[0dd3a2f] | 219 | // Local Variables: // |
---|
| 220 | // tab-width: 4 // |
---|
| 221 | // mode: c++ // |
---|
| 222 | // compile-command: "make install" // |
---|
| 223 | // End: // |
---|