source: translator/SynTree/TypeSubstitution.cc @ 48e99f2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 48e99f2 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 */
7
8#include "Type.h"
9#include "TypeSubstitution.h"
10
11
12TypeSubstitution::TypeSubstitution()
13{
14}
15
16TypeSubstitution::TypeSubstitution( const TypeSubstitution &other )
17{
18    initialize( other, *this );
19}
20
21TypeSubstitution::~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    }
29}
30
31TypeSubstitution &
32TypeSubstitution::operator=( const TypeSubstitution &other )
33{
34    if( this == &other ) return *this;
35    initialize( other, *this );
36    return *this;
37}
38
39void 
40TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest )
41{
42    dest.typeEnv.clear();
43    dest.varEnv.clear();
44    dest.add( src );
45}
46
47void 
48TypeSubstitution::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    }
56}
57
58void 
59TypeSubstitution::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();
66}
67
68void 
69TypeSubstitution::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    }
76}
77
78Type *
79TypeSubstitution::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    }
87}
88
89bool 
90TypeSubstitution::empty() const
91{
92    return typeEnv.empty() && varEnv.empty();
93}
94
95void
96TypeSubstitution::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 );
105}
106
107Type* 
108TypeSubstitution::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 {
117///         std::cout << "found " << inst->get_name() << ", replacing with ";
118///         i->second->print( std::cout );
119///         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    }
126}
127
128Expression* 
129TypeSubstitution::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    }
139}
140
141template< typename TypeClass >
142Type *
143TypeSubstitution::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;
154}
155
156Type* 
157TypeSubstitution::mutate(VoidType *basicType)
158{
159    return handleType( basicType );
160}
161
162Type* 
163TypeSubstitution::mutate(BasicType *basicType)
164{
165    return handleType( basicType );
166}
167
168Type* 
169TypeSubstitution::mutate(PointerType *pointerType)
170{
171    return handleType( pointerType );
172}
173
174Type* 
175TypeSubstitution::mutate(ArrayType *arrayType)
176{
177    return handleType( arrayType );
178}
179
180Type* 
181TypeSubstitution::mutate(FunctionType *functionType)
182{
183    return handleType( functionType );
184}
185
186Type* 
187TypeSubstitution::mutate(StructInstType *aggregateUseType)
188{
189    return handleType( aggregateUseType );
190}
191
192Type* 
193TypeSubstitution::mutate(UnionInstType *aggregateUseType)
194{
195    return handleType( aggregateUseType );
196}
197
198Type* 
199TypeSubstitution::mutate(EnumInstType *aggregateUseType)
200{
201    return handleType( aggregateUseType );
202}
203
204Type* 
205TypeSubstitution::mutate(ContextInstType *aggregateUseType)
206{
207    return handleType( aggregateUseType );
208}
209
210Type* 
211TypeSubstitution::mutate(TupleType *tupleType)
212{
213    return handleType( tupleType );
214}
215
216void 
217TypeSubstitution::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    }
231}
232
Note: See TracBrowser for help on using the repository browser.