source: src/SynTree/TypeSubstitution.cc@ 2162c2c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 2162c2c was e33f321, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

mutate env on each expression type to ensure type mappings are up to date

  • Property mode set to 100644
File size: 7.5 KB
Line 
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
19TypeSubstitution::TypeSubstitution() {
20}
21
22TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
23 initialize( other, *this );
24}
25
26TypeSubstitution::~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
35TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
36 if ( this == &other ) return *this;
37 initialize( other, *this );
38 return *this;
39}
40
41void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
42 dest.typeEnv.clear();
43 dest.varEnv.clear();
44 dest.add( src );
45}
46
47void 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
56void 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
64void 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
72Type *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
102bool TypeSubstitution::empty() const {
103 return typeEnv.empty() && varEnv.empty();
104}
105
106void 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
116Type * 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
135Expression * 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
146template< typename TypeClass >
147Type *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
160template< typename TypeClass >
161Type *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
181Type * TypeSubstitution::mutate( VoidType *voidType ) {
182 return handleType( voidType );
183}
184
185Type * TypeSubstitution::mutate( BasicType *basicType ) {
186 return handleType( basicType );
187}
188
189Type * TypeSubstitution::mutate( PointerType *pointerType ) {
190 return handleType( pointerType );
191}
192
193Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
194 return handleType( arrayType );
195}
196
197Type * TypeSubstitution::mutate( FunctionType *functionType ) {
198 return handleType( functionType );
199}
200
201Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
202 return handleAggregateType( aggregateUseType );
203}
204
205Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
206 return handleAggregateType( aggregateUseType );
207}
208
209Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
210 return handleType( aggregateUseType );
211}
212
213Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
214 return handleType( aggregateUseType );
215}
216
217Type * TypeSubstitution::mutate( TupleType *tupleType ) {
218 return handleType( tupleType );
219}
220
221Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
222 return handleType( varArgsType );
223}
224
225Type * TypeSubstitution::mutate( ZeroType *zeroType ) {
226 return handleType( zeroType );
227}
228
229Type * TypeSubstitution::mutate( OneType *oneType ) {
230 return handleType( oneType );
231}
232
233TypeSubstitution * 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
243void 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
258std::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: //
Note: See TracBrowser for help on using the repository browser.