source: src/SynTree/TypeSubstitution.cc@ fbfb38e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 fbfb38e was 4040425, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

change keyword type to otype and context to trait

  • Property mode set to 100644
File size: 6.4 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 : 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
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 if ( i == typeEnv.end() ) {
75 return 0;
76 } else {
77 return i->second;
78 } // if
79}
80
81bool TypeSubstitution::empty() const {
82 return typeEnv.empty() && varEnv.empty();
83}
84
85void 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
95Type * 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
114Expression * 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
125template< typename TypeClass >
126Type *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
139template< typename TypeClass >
140Type *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
160Type * TypeSubstitution::mutate( VoidType *basicType ) {
161 return handleType( basicType );
162}
163
164Type * TypeSubstitution::mutate( BasicType *basicType ) {
165 return handleType( basicType );
166}
167
168Type * TypeSubstitution::mutate( PointerType *pointerType ) {
169 return handleType( pointerType );
170}
171
172Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
173 return handleType( arrayType );
174}
175
176Type * TypeSubstitution::mutate( FunctionType *functionType ) {
177 return handleType( functionType );
178}
179
180Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
181 return handleAggregateType( aggregateUseType );
182}
183
184Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
185 return handleAggregateType( aggregateUseType );
186}
187
188Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
189 return handleType( aggregateUseType );
190}
191
192Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
193 return handleType( aggregateUseType );
194}
195
196Type * TypeSubstitution::mutate( TupleType *tupleType ) {
197 return handleType( tupleType );
198}
199
200Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
201 return handleType( varArgsType );
202}
203
204void 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: //
Note: See TracBrowser for help on using the repository browser.