[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 ); |
---|
| 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; |
---|
[51b7345] | 136 | } |
---|
| 137 | |
---|
[0dd3a2f] | 138 | Type * TypeSubstitution::mutate( VoidType *basicType ) { |
---|
| 139 | return handleType( basicType ); |
---|
[51b7345] | 140 | } |
---|
| 141 | |
---|
[0dd3a2f] | 142 | Type * TypeSubstitution::mutate( BasicType *basicType ) { |
---|
| 143 | return handleType( basicType ); |
---|
[51b7345] | 144 | } |
---|
| 145 | |
---|
[0dd3a2f] | 146 | Type * TypeSubstitution::mutate( PointerType *pointerType ) { |
---|
| 147 | return handleType( pointerType ); |
---|
[51b7345] | 148 | } |
---|
| 149 | |
---|
[0dd3a2f] | 150 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) { |
---|
| 151 | return handleType( arrayType ); |
---|
[51b7345] | 152 | } |
---|
| 153 | |
---|
[0dd3a2f] | 154 | Type * TypeSubstitution::mutate( FunctionType *functionType ) { |
---|
| 155 | return handleType( functionType ); |
---|
[51b7345] | 156 | } |
---|
| 157 | |
---|
[0dd3a2f] | 158 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { |
---|
| 159 | return handleType( aggregateUseType ); |
---|
[51b7345] | 160 | } |
---|
| 161 | |
---|
[0dd3a2f] | 162 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { |
---|
| 163 | return handleType( aggregateUseType ); |
---|
[51b7345] | 164 | } |
---|
| 165 | |
---|
[0dd3a2f] | 166 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { |
---|
| 167 | return handleType( aggregateUseType ); |
---|
[51b7345] | 168 | } |
---|
| 169 | |
---|
[0dd3a2f] | 170 | Type * TypeSubstitution::mutate( ContextInstType *aggregateUseType ) { |
---|
| 171 | return handleType( aggregateUseType ); |
---|
[51b7345] | 172 | } |
---|
| 173 | |
---|
[0dd3a2f] | 174 | Type * TypeSubstitution::mutate( TupleType *tupleType ) { |
---|
| 175 | return handleType( tupleType ); |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
[0dd3a2f] | 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 |
---|
[51b7345] | 191 | } |
---|
| 192 | |
---|
[0dd3a2f] | 193 | // Local Variables: // |
---|
| 194 | // tab-width: 4 // |
---|
| 195 | // mode: c++ // |
---|
| 196 | // compile-command: "make install" // |
---|
| 197 | // End: // |
---|