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 : Rob Schluntz |
---|
12 | // Last Modified On : Tue Apr 26 11:15:29 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 | |
---|
75 | // break on not in substitution set |
---|
76 | if ( i == typeEnv.end() ) return 0; |
---|
77 | |
---|
78 | // attempt to transitively follow TypeInstType links. |
---|
79 | while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) { |
---|
80 | const std::string& typeName = actualType->get_name(); |
---|
81 | |
---|
82 | // break cycles in the transitive follow |
---|
83 | if ( formalType == typeName ) break; |
---|
84 | |
---|
85 | // Look for the type this maps to, returning previous mapping if none-such |
---|
86 | i = typeEnv.find( typeName ); |
---|
87 | if ( i == typeEnv.end() ) return actualType; |
---|
88 | } |
---|
89 | |
---|
90 | // return type from substitution set |
---|
91 | return i->second; |
---|
92 | |
---|
93 | #if 0 |
---|
94 | if ( i == typeEnv.end() ) { |
---|
95 | return 0; |
---|
96 | } else { |
---|
97 | return i->second; |
---|
98 | } // if |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | bool TypeSubstitution::empty() const { |
---|
103 | return typeEnv.empty() && varEnv.empty(); |
---|
104 | } |
---|
105 | |
---|
106 | void TypeSubstitution::normalize() { |
---|
107 | do { |
---|
108 | subCount = 0; |
---|
109 | freeOnly = true; |
---|
110 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
111 | i->second = i->second->acceptMutator( *this ); |
---|
112 | } |
---|
113 | } while ( subCount ); |
---|
114 | } |
---|
115 | |
---|
116 | Type * TypeSubstitution::mutate( TypeInstType *inst ) { |
---|
117 | BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() ); |
---|
118 | if ( bound != boundVars.end() ) return inst; |
---|
119 | |
---|
120 | TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() ); |
---|
121 | if ( i == typeEnv.end() ) { |
---|
122 | return inst; |
---|
123 | } else { |
---|
124 | /// std::cout << "found " << inst->get_name() << ", replacing with "; |
---|
125 | /// i->second->print( std::cout ); |
---|
126 | /// std::cout << std::endl; |
---|
127 | subCount++; |
---|
128 | Type *newtype = i->second->clone(); |
---|
129 | newtype->get_qualifiers() += inst->get_qualifiers(); |
---|
130 | delete inst; |
---|
131 | return newtype; |
---|
132 | } // if |
---|
133 | } |
---|
134 | |
---|
135 | Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) { |
---|
136 | VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() ); |
---|
137 | if ( i == varEnv.end() ) { |
---|
138 | return nameExpr; |
---|
139 | } else { |
---|
140 | subCount++; |
---|
141 | delete nameExpr; |
---|
142 | return i->second->clone(); |
---|
143 | } // if |
---|
144 | } |
---|
145 | |
---|
146 | template< typename TypeClass > |
---|
147 | Type *TypeSubstitution::handleType( TypeClass *type ) { |
---|
148 | BoundVarsType oldBoundVars( boundVars ); |
---|
149 | // bind type variables from forall-qualifiers |
---|
150 | if ( freeOnly ) { |
---|
151 | for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().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 | template< typename TypeClass > |
---|
161 | Type *TypeSubstitution::handleAggregateType( TypeClass *type ) { |
---|
162 | BoundVarsType oldBoundVars( boundVars ); |
---|
163 | // bind type variables from forall-qualifiers |
---|
164 | if ( freeOnly ) { |
---|
165 | for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
166 | boundVars.insert( (*tyvar )->get_name() ); |
---|
167 | } // for |
---|
168 | } // if |
---|
169 | // bind type variables from generic type instantiations |
---|
170 | std::list< TypeDecl* > *baseParameters = type->get_baseParameters(); |
---|
171 | if ( baseParameters && ! type->get_parameters().empty() ) { |
---|
172 | for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) { |
---|
173 | boundVars.insert( (*tyvar)->get_name() ); |
---|
174 | } // for |
---|
175 | } // if |
---|
176 | Type *ret = Mutator::mutate( type ); |
---|
177 | boundVars = oldBoundVars; |
---|
178 | return ret; |
---|
179 | } |
---|
180 | |
---|
181 | Type * TypeSubstitution::mutate( VoidType *voidType ) { |
---|
182 | return handleType( voidType ); |
---|
183 | } |
---|
184 | |
---|
185 | Type * TypeSubstitution::mutate( BasicType *basicType ) { |
---|
186 | return handleType( basicType ); |
---|
187 | } |
---|
188 | |
---|
189 | Type * TypeSubstitution::mutate( PointerType *pointerType ) { |
---|
190 | return handleType( pointerType ); |
---|
191 | } |
---|
192 | |
---|
193 | Type * TypeSubstitution::mutate( ArrayType *arrayType ) { |
---|
194 | return handleType( arrayType ); |
---|
195 | } |
---|
196 | |
---|
197 | Type * TypeSubstitution::mutate( FunctionType *functionType ) { |
---|
198 | return handleType( functionType ); |
---|
199 | } |
---|
200 | |
---|
201 | Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) { |
---|
202 | return handleAggregateType( aggregateUseType ); |
---|
203 | } |
---|
204 | |
---|
205 | Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) { |
---|
206 | return handleAggregateType( aggregateUseType ); |
---|
207 | } |
---|
208 | |
---|
209 | Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) { |
---|
210 | return handleType( aggregateUseType ); |
---|
211 | } |
---|
212 | |
---|
213 | Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) { |
---|
214 | return handleType( aggregateUseType ); |
---|
215 | } |
---|
216 | |
---|
217 | Type * TypeSubstitution::mutate( TupleType *tupleType ) { |
---|
218 | return handleType( tupleType ); |
---|
219 | } |
---|
220 | |
---|
221 | Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) { |
---|
222 | return handleType( varArgsType ); |
---|
223 | } |
---|
224 | |
---|
225 | Type * TypeSubstitution::mutate( ZeroType *zeroType ) { |
---|
226 | return handleType( zeroType ); |
---|
227 | } |
---|
228 | |
---|
229 | Type * TypeSubstitution::mutate( OneType *oneType ) { |
---|
230 | return handleType( oneType ); |
---|
231 | } |
---|
232 | |
---|
233 | TypeSubstitution * TypeSubstitution::acceptMutator( Mutator & mutator ) { |
---|
234 | for ( auto & p : typeEnv ) { |
---|
235 | p.second = maybeMutate( p.second, mutator ); |
---|
236 | } |
---|
237 | for ( auto & p : varEnv ) { |
---|
238 | p.second = maybeMutate( p.second, mutator ); |
---|
239 | } |
---|
240 | return this; |
---|
241 | } |
---|
242 | |
---|
243 | void TypeSubstitution::print( std::ostream &os, int indent ) const { |
---|
244 | os << std::string( indent, ' ' ) << "Types:" << std::endl; |
---|
245 | for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
246 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
247 | i->second->print( os, indent+4 ); |
---|
248 | os << std::endl; |
---|
249 | } // for |
---|
250 | os << std::string( indent, ' ' ) << "Non-types:" << std::endl; |
---|
251 | for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) { |
---|
252 | os << std::string( indent+2, ' ' ) << i->first << " -> "; |
---|
253 | i->second->print( os, indent+4 ); |
---|
254 | os << std::endl; |
---|
255 | } // for |
---|
256 | } |
---|
257 | |
---|
258 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) { |
---|
259 | sub.print( out ); |
---|
260 | return out; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | // Local Variables: // |
---|
265 | // tab-width: 4 // |
---|
266 | // mode: c++ // |
---|
267 | // compile-command: "make install" // |
---|
268 | // End: // |
---|