Changeset 0dd3a2f for translator/SynTree/TypeSubstitution.cc
- Timestamp:
- May 18, 2015, 11:20:23 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 51587aa
- Parents:
- a32b204
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/SynTree/TypeSubstitution.cc
ra32b204 r0dd3a2f 1 /* 2 * This file is part of the Cforall project 3 * 4 * $Id: TypeSubstitution.cc,v 1.9 2005/08/29 20:59:26 rcbilson Exp $ 5 * 6 */ 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 // 7 15 8 16 #include "Type.h" 9 17 #include "TypeSubstitution.h" 10 18 11 12 TypeSubstitution::TypeSubstitution() 13 { 19 TypeSubstitution::TypeSubstitution() { 14 20 } 15 21 16 TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) 17 { 18 initialize( other, *this ); 22 TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) { 23 initialize( other, *this ); 19 24 } 20 25 21 TypeSubstitution::~TypeSubstitution() 22 { 23 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 24 delete( i->second ); 25 } 26 for ( VarEnvType::iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { 27 delete( i->second ); 28 } 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 } 29 33 } 30 34 31 TypeSubstitution & 32 TypeSubstitution::operator=( const TypeSubstitution &other ) 33 { 34 if ( this == &other ) return *this; 35 initialize( other, *this ); 36 return *this; 35 TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) { 36 if ( this == &other ) return *this; 37 initialize( other, *this ); 38 return *this; 37 39 } 38 40 39 void 40 TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) 41 { 42 dest.typeEnv.clear(); 43 dest.varEnv.clear(); 44 dest.add( src ); 41 void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { 42 dest.typeEnv.clear(); 43 dest.varEnv.clear(); 44 dest.add( src ); 45 45 } 46 46 47 void 48 TypeSubstitution::add( const TypeSubstitution &other ) 49 { 50 for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) { 51 typeEnv[ i->first ] = i->second->clone(); 52 } 53 for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) { 54 varEnv[ i->first ] = i->second->clone(); 55 } 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 56 54 } 57 55 58 void 59 TypeSubstitution::add( std::string formalType, Type *actualType ) 60 { 61 TypeEnvType::iterator i = typeEnv.find( formalType ); 62 if ( i != typeEnv.end() ) { 63 delete i->second; 64 } 65 typeEnv[ formalType ] = actualType->clone(); 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(); 66 62 } 67 63 68 void 69 TypeSubstitution::remove( std::string formalType ) 70 { 71 TypeEnvType::iterator i = typeEnv.find( formalType ); 72 if ( i != typeEnv.end() ) { 73 delete i->second; 74 typeEnv.erase( formalType ); 75 } 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 76 70 } 77 71 78 Type * 79 TypeSubstitution::lookup( std::string formalType ) const 80 { 81 TypeEnvType::const_iterator i = typeEnv.find( formalType ); 82 if ( i == typeEnv.end() ) { 83 return 0; 84 } else { 85 return i->second; 86 } 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 87 79 } 88 80 89 bool 90 TypeSubstitution::empty() const 91 { 92 return typeEnv.empty() && varEnv.empty(); 81 bool TypeSubstitution::empty() const { 82 return typeEnv.empty() && varEnv.empty(); 93 83 } 94 84 95 void 96 TypeSubstitution::normalize() 97 { 98 do { 99 subCount = 0; 100 freeOnly = true; 101 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 102 i->second = i->second->acceptMutator( *this ); 103 } 104 } while ( subCount ); 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 ); 105 93 } 106 94 107 Type* 108 TypeSubstitution::mutate(TypeInstType *inst) 109 { 110 BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() ); 111 if ( bound != boundVars.end() ) return inst; 112 113 TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() ); 114 if ( i == typeEnv.end() ) { 115 return inst; 116 } else { 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 { 117 103 /// std::cout << "found " << inst->get_name() << ", replacing with "; 118 104 /// i->second->print( std::cout ); 119 105 /// std::cout << std::endl; 120 subCount++;121 Type *newtype = i->second->clone();122 newtype->get_qualifiers() += inst->get_qualifiers();123 delete inst;124 return newtype;125 } 106 subCount++; 107 Type *newtype = i->second->clone(); 108 newtype->get_qualifiers() += inst->get_qualifiers(); 109 delete inst; 110 return newtype; 111 } // if 126 112 } 127 113 128 Expression* 129 TypeSubstitution::mutate(NameExpr *nameExpr) 130 { 131 VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); 132 if ( i == varEnv.end() ) { 133 return nameExpr; 134 } else { 135 subCount++; 136 delete nameExpr; 137 return i->second->clone(); 138 } 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 139 123 } 140 124 141 125 template< typename TypeClass > 142 Type * 143 TypeSubstitution::handleType( TypeClass *type ) 144 { 145 BoundVarsType oldBoundVars( boundVars ); 146 if ( freeOnly ) { 147 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { 148 boundVars.insert( (*tyvar)->get_name() ); 149 } 150 } 151 Type *ret = Mutator::mutate( type ); 152 boundVars = oldBoundVars; 153 return ret; 126 Type *TypeSubstitution::handleType( TypeClass *type ) { 127 BoundVarsType oldBoundVars( boundVars ); 128 if ( freeOnly ) { 129 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { 130 boundVars.insert( (*tyvar )->get_name() ); 131 } // for 132 } // if 133 Type *ret = Mutator::mutate( type ); 134 boundVars = oldBoundVars; 135 return ret; 154 136 } 155 137 156 Type* 157 TypeSubstitution::mutate(VoidType *basicType) 158 { 159 return handleType( basicType ); 138 Type * TypeSubstitution::mutate( VoidType *basicType ) { 139 return handleType( basicType ); 160 140 } 161 141 162 Type* 163 TypeSubstitution::mutate(BasicType *basicType) 164 { 165 return handleType( basicType ); 142 Type * TypeSubstitution::mutate( BasicType *basicType ) { 143 return handleType( basicType ); 166 144 } 167 145 168 Type* 169 TypeSubstitution::mutate(PointerType *pointerType) 170 { 171 return handleType( pointerType ); 146 Type * TypeSubstitution::mutate( PointerType *pointerType ) { 147 return handleType( pointerType ); 172 148 } 173 149 174 Type* 175 TypeSubstitution::mutate(ArrayType *arrayType) 176 { 177 return handleType( arrayType ); 150 Type * TypeSubstitution::mutate( ArrayType *arrayType ) { 151 return handleType( arrayType ); 178 152 } 179 153 180 Type* 181 TypeSubstitution::mutate(FunctionType *functionType) 182 { 183 return handleType( functionType ); 154 Type * TypeSubstitution::mutate( FunctionType *functionType ) { 155 return handleType( functionType ); 184 156 } 185 157 186 Type* 187 TypeSubstitution::mutate(StructInstType *aggregateUseType) 188 { 189 return handleType( aggregateUseType ); 158 Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { 159 return handleType( aggregateUseType ); 190 160 } 191 161 192 Type* 193 TypeSubstitution::mutate(UnionInstType *aggregateUseType) 194 { 195 return handleType( aggregateUseType ); 162 Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { 163 return handleType( aggregateUseType ); 196 164 } 197 165 198 Type* 199 TypeSubstitution::mutate(EnumInstType *aggregateUseType) 200 { 201 return handleType( aggregateUseType ); 166 Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { 167 return handleType( aggregateUseType ); 202 168 } 203 169 204 Type* 205 TypeSubstitution::mutate(ContextInstType *aggregateUseType) 206 { 207 return handleType( aggregateUseType ); 170 Type * TypeSubstitution::mutate( ContextInstType *aggregateUseType ) { 171 return handleType( aggregateUseType ); 208 172 } 209 173 210 Type* 211 TypeSubstitution::mutate(TupleType *tupleType) 212 { 213 return handleType( tupleType ); 174 Type * TypeSubstitution::mutate( TupleType *tupleType ) { 175 return handleType( tupleType ); 214 176 } 215 177 216 void 217 TypeSubstitution::print( std::ostream &os, int indent ) const 218 { 219 os << std::string( indent, ' ' ) << "Types:" << std::endl; 220 for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 221 os << std::string( indent+2, ' ' ) << i->first << " -> "; 222 i->second->print( os, indent+4 ); 223 os << std::endl; 224 } 225 os << std::string( indent, ' ' ) << "Non-types:" << std::endl; 226 for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { 227 os << std::string( indent+2, ' ' ) << i->first << " -> "; 228 i->second->print( os, indent+4 ); 229 os << std::endl; 230 } 178 void TypeSubstitution::print( std::ostream &os, int indent ) const { 179 os << std::string( indent, ' ' ) << "Types:" << std::endl; 180 for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { 181 os << std::string( indent+2, ' ' ) << i->first << " -> "; 182 i->second->print( os, indent+4 ); 183 os << std::endl; 184 } // for 185 os << std::string( indent, ' ' ) << "Non-types:" << std::endl; 186 for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { 187 os << std::string( indent+2, ' ' ) << i->first << " -> "; 188 i->second->print( os, indent+4 ); 189 os << std::endl; 190 } // for 231 191 } 232 192 193 // Local Variables: // 194 // tab-width: 4 // 195 // mode: c++ // 196 // compile-command: "make install" // 197 // End: //
Note: See TracChangeset
for help on using the changeset viewer.