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 | |
---|
12 | TypeSubstitution::TypeSubstitution() |
---|
13 | { |
---|
14 | } |
---|
15 | |
---|
16 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) |
---|
17 | { |
---|
18 | initialize( other, *this ); |
---|
19 | } |
---|
20 | |
---|
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 | } |
---|
29 | } |
---|
30 | |
---|
31 | TypeSubstitution & |
---|
32 | TypeSubstitution::operator=( const TypeSubstitution &other ) |
---|
33 | { |
---|
34 | if( this == &other ) return *this; |
---|
35 | initialize( other, *this ); |
---|
36 | return *this; |
---|
37 | } |
---|
38 | |
---|
39 | void |
---|
40 | TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) |
---|
41 | { |
---|
42 | dest.typeEnv.clear(); |
---|
43 | dest.varEnv.clear(); |
---|
44 | dest.add( src ); |
---|
45 | } |
---|
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 | } |
---|
56 | } |
---|
57 | |
---|
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(); |
---|
66 | } |
---|
67 | |
---|
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 | } |
---|
76 | } |
---|
77 | |
---|
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 | } |
---|
87 | } |
---|
88 | |
---|
89 | bool |
---|
90 | TypeSubstitution::empty() const |
---|
91 | { |
---|
92 | return typeEnv.empty() && varEnv.empty(); |
---|
93 | } |
---|
94 | |
---|
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 ); |
---|
105 | } |
---|
106 | |
---|
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 { |
---|
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 | |
---|
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 | } |
---|
139 | } |
---|
140 | |
---|
141 | 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; |
---|
154 | } |
---|
155 | |
---|
156 | Type* |
---|
157 | TypeSubstitution::mutate(VoidType *basicType) |
---|
158 | { |
---|
159 | return handleType( basicType ); |
---|
160 | } |
---|
161 | |
---|
162 | Type* |
---|
163 | TypeSubstitution::mutate(BasicType *basicType) |
---|
164 | { |
---|
165 | return handleType( basicType ); |
---|
166 | } |
---|
167 | |
---|
168 | Type* |
---|
169 | TypeSubstitution::mutate(PointerType *pointerType) |
---|
170 | { |
---|
171 | return handleType( pointerType ); |
---|
172 | } |
---|
173 | |
---|
174 | Type* |
---|
175 | TypeSubstitution::mutate(ArrayType *arrayType) |
---|
176 | { |
---|
177 | return handleType( arrayType ); |
---|
178 | } |
---|
179 | |
---|
180 | Type* |
---|
181 | TypeSubstitution::mutate(FunctionType *functionType) |
---|
182 | { |
---|
183 | return handleType( functionType ); |
---|
184 | } |
---|
185 | |
---|
186 | Type* |
---|
187 | TypeSubstitution::mutate(StructInstType *aggregateUseType) |
---|
188 | { |
---|
189 | return handleType( aggregateUseType ); |
---|
190 | } |
---|
191 | |
---|
192 | Type* |
---|
193 | TypeSubstitution::mutate(UnionInstType *aggregateUseType) |
---|
194 | { |
---|
195 | return handleType( aggregateUseType ); |
---|
196 | } |
---|
197 | |
---|
198 | Type* |
---|
199 | TypeSubstitution::mutate(EnumInstType *aggregateUseType) |
---|
200 | { |
---|
201 | return handleType( aggregateUseType ); |
---|
202 | } |
---|
203 | |
---|
204 | Type* |
---|
205 | TypeSubstitution::mutate(ContextInstType *aggregateUseType) |
---|
206 | { |
---|
207 | return handleType( aggregateUseType ); |
---|
208 | } |
---|
209 | |
---|
210 | Type* |
---|
211 | TypeSubstitution::mutate(TupleType *tupleType) |
---|
212 | { |
---|
213 | return handleType( tupleType ); |
---|
214 | } |
---|
215 | |
---|
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 | } |
---|
231 | } |
---|
232 | |
---|