| 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 : Wed Mar 2 17:29:15 2016
|
|---|
| 13 | // Update Count : 3
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Type.h"
|
|---|
| 17 | #include "TypeSubstitution.h"
|
|---|
| 18 |
|
|---|
| 19 | TypeSubstitution::TypeSubstitution() {
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
|
|---|
| 23 | initialize( other, *this );
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 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 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
|
|---|
| 36 | if ( this == &other ) return *this;
|
|---|
| 37 | initialize( other, *this );
|
|---|
| 38 | return *this;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
|
|---|
| 42 | dest.typeEnv.clear();
|
|---|
| 43 | dest.varEnv.clear();
|
|---|
| 44 | dest.add( src );
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 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
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 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();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 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
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 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
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | bool TypeSubstitution::empty() const {
|
|---|
| 82 | return typeEnv.empty() && varEnv.empty();
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 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 );
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 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 {
|
|---|
| 103 | /// std::cout << "found " << inst->get_name() << ", replacing with ";
|
|---|
| 104 | /// i->second->print( std::cout );
|
|---|
| 105 | /// std::cout << std::endl;
|
|---|
| 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
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | template< typename TypeClass >
|
|---|
| 126 | Type *TypeSubstitution::handleType( TypeClass *type ) {
|
|---|
| 127 | BoundVarsType oldBoundVars( boundVars );
|
|---|
| 128 | // bind type variables from forall-qualifiers
|
|---|
| 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;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 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
|
|---|
| 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
|
|---|
| 155 | Type *ret = Mutator::mutate( type );
|
|---|
| 156 | boundVars = oldBoundVars;
|
|---|
| 157 | return ret;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | Type * TypeSubstitution::mutate( VoidType *basicType ) {
|
|---|
| 161 | return handleType( basicType );
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | Type * TypeSubstitution::mutate( BasicType *basicType ) {
|
|---|
| 165 | return handleType( basicType );
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | Type * TypeSubstitution::mutate( PointerType *pointerType ) {
|
|---|
| 169 | return handleType( pointerType );
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
|
|---|
| 173 | return handleType( arrayType );
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | Type * TypeSubstitution::mutate( FunctionType *functionType ) {
|
|---|
| 177 | return handleType( functionType );
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
|
|---|
| 181 | return handleAggregateType( aggregateUseType );
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
|
|---|
| 185 | return handleAggregateType( aggregateUseType );
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
|
|---|
| 189 | return handleType( aggregateUseType );
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
|
|---|
| 193 | return handleType( aggregateUseType );
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | Type * TypeSubstitution::mutate( TupleType *tupleType ) {
|
|---|
| 197 | return handleType( tupleType );
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
|
|---|
| 201 | return handleType( varArgsType );
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 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
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | // Local Variables: //
|
|---|
| 220 | // tab-width: 4 //
|
|---|
| 221 | // mode: c++ //
|
|---|
| 222 | // compile-command: "make install" //
|
|---|
| 223 | // End: //
|
|---|