source: src/SynTree/TypeSubstitution.cc@ cfe2f0a

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 cfe2f0a was e9a715d3, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Convert TypeSubstitution's apply functionality to PassVisitor

  • Property mode set to 100644
File size: 6.1 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 : Thu Mar 16 15:54:35 2017
13// Update Count : 4
14//
15
16#include <ostream> // for ostream, basic_ostream, operator<<, endl
17
18#include "Type.h" // for TypeInstType, Type, StructInstType, UnionInstType
19#include "TypeSubstitution.h"
20
21TypeSubstitution::TypeSubstitution() {
22}
23
24TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
25 initialize( other, *this );
26}
27
28TypeSubstitution::~TypeSubstitution() {
29 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
30 delete( i->second );
31 }
32 for ( VarEnvType::iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
33 delete( i->second );
34 }
35}
36
37TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
38 if ( this == &other ) return *this;
39 initialize( other, *this );
40 return *this;
41}
42
43void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
44 dest.typeEnv.clear();
45 dest.varEnv.clear();
46 dest.add( src );
47}
48
49void TypeSubstitution::add( const TypeSubstitution &other ) {
50 for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) {
51 typeEnv[ i->first ] = i->second->clone();
52 } // for
53 for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
54 varEnv[ i->first ] = i->second->clone();
55 } // for
56}
57
58void TypeSubstitution::add( std::string formalType, Type *actualType ) {
59 TypeEnvType::iterator i = typeEnv.find( formalType );
60 if ( i != typeEnv.end() ) {
61 delete i->second;
62 } // if
63 typeEnv[ formalType ] = actualType->clone();
64}
65
66void TypeSubstitution::remove( std::string formalType ) {
67 TypeEnvType::iterator i = typeEnv.find( formalType );
68 if ( i != typeEnv.end() ) {
69 delete i->second;
70 typeEnv.erase( formalType );
71 } // if
72}
73
74Type *TypeSubstitution::lookup( std::string formalType ) const {
75 TypeEnvType::const_iterator i = typeEnv.find( formalType );
76
77 // break on not in substitution set
78 if ( i == typeEnv.end() ) return 0;
79
80 // attempt to transitively follow TypeInstType links.
81 while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) {
82 const std::string& typeName = actualType->get_name();
83
84 // break cycles in the transitive follow
85 if ( formalType == typeName ) break;
86
87 // Look for the type this maps to, returning previous mapping if none-such
88 i = typeEnv.find( typeName );
89 if ( i == typeEnv.end() ) return actualType;
90 }
91
92 // return type from substitution set
93 return i->second;
94
95#if 0
96 if ( i == typeEnv.end() ) {
97 return 0;
98 } else {
99 return i->second;
100 } // if
101#endif
102}
103
104bool TypeSubstitution::empty() const {
105 return typeEnv.empty() && varEnv.empty();
106}
107
108void TypeSubstitution::normalize() {
109 PassVisitor<Substituter> sub( *this, true );
110 do {
111 sub.pass.subCount = 0;
112 sub.pass.freeOnly = true;
113 for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
114 i->second = i->second->acceptMutator( sub );
115 }
116 } while ( sub.pass.subCount );
117}
118
119Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) {
120 BoundVarsType::const_iterator bound = boundVars.find( inst->name );
121 if ( bound != boundVars.end() ) return inst;
122
123 TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() );
124 if ( i == sub.typeEnv.end() ) {
125 return inst;
126 } else {
127/// std::cerr << "found " << inst->get_name() << ", replacing with ";
128/// i->second->print( std::cerr );
129/// std::cerr << std::endl;
130 subCount++;
131 Type * newtype = i->second->clone();
132 newtype->get_qualifiers() |= inst->get_qualifiers();
133 delete inst;
134 return newtype;
135 } // if
136}
137
138Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) {
139 VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
140 if ( i == sub.varEnv.end() ) {
141 return nameExpr;
142 } else {
143 subCount++;
144 delete nameExpr;
145 return i->second->clone();
146 } // if
147}
148
149void TypeSubstitution::Substituter::premutate( Type * type ) {
150 GuardValue( boundVars );
151 // bind type variables from forall-qualifiers
152 if ( freeOnly ) {
153 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
154 boundVars.insert( (*tyvar)->name );
155 } // for
156 } // if
157}
158
159template< typename TypeClass >
160void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) {
161 GuardValue( boundVars );
162 // bind type variables from forall-qualifiers
163 if ( freeOnly ) {
164 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
165 boundVars.insert( (*tyvar)->name );
166 } // for
167 // bind type variables from generic type instantiations
168 std::list< TypeDecl* > *baseParameters = type->get_baseParameters();
169 if ( baseParameters && ! type->parameters.empty() ) {
170 for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) {
171 boundVars.insert( (*tyvar)->name );
172 } // for
173 } // if
174 } // if
175}
176
177void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) {
178 handleAggregateType( aggregateUseType );
179}
180
181void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) {
182 handleAggregateType( aggregateUseType );
183}
184
185void TypeSubstitution::print( std::ostream &os, Indenter indent ) const {
186 os << indent << "Types:" << std::endl;
187 for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
188 os << indent+1 << i->first << " -> ";
189 i->second->print( os, indent+2 );
190 os << std::endl;
191 } // for
192 os << indent << "Non-types:" << std::endl;
193 for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
194 os << indent+1 << i->first << " -> ";
195 i->second->print( os, indent+2 );
196 os << std::endl;
197 } // for
198}
199
200std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) {
201 sub.print( out );
202 return out;
203}
204
205
206// Local Variables: //
207// tab-width: 4 //
208// mode: c++ //
209// compile-command: "make install" //
210// End: //
Note: See TracBrowser for help on using the repository browser.