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 : Andrew Beach |
---|
12 | // Last Modified On : Mon Jun 3 13:26:00 2017 |
---|
13 | // Update Count : 5 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Type.hpp" // for TypeInstType, Type, StructInstType, UnionInstType |
---|
17 | #include "TypeSubstitution.hpp" |
---|
18 | |
---|
19 | namespace ast { |
---|
20 | |
---|
21 | TypeSubstitution::TypeSubstitution() { |
---|
22 | } |
---|
23 | |
---|
24 | TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) : Node() { |
---|
25 | initialize( other, *this ); |
---|
26 | } |
---|
27 | |
---|
28 | TypeSubstitution::~TypeSubstitution() {} |
---|
29 | |
---|
30 | TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) { |
---|
31 | if ( this == &other ) return *this; |
---|
32 | initialize( other, *this ); |
---|
33 | return *this; |
---|
34 | } |
---|
35 | |
---|
36 | void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) { |
---|
37 | dest.typeEnv.clear(); |
---|
38 | dest.varEnv.clear(); |
---|
39 | dest.add( src ); |
---|
40 | } |
---|
41 | |
---|
42 | void TypeSubstitution::add( const TypeSubstitution &other ) { |
---|
43 | for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) { |
---|
44 | typeEnv[ i->first ] = i->second; |
---|
45 | } // for |
---|
46 | for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) { |
---|
47 | varEnv[ i->first ] = i->second; |
---|
48 | } // for |
---|
49 | } |
---|
50 | |
---|
51 | void TypeSubstitution::add( std::string formalType, const Type *actualType ) { |
---|
52 | typeEnv[ formalType ] = actualType; |
---|
53 | } |
---|
54 | |
---|
55 | void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) { |
---|
56 | varEnv[ formalExpr ] = actualExpr; |
---|
57 | } |
---|
58 | |
---|
59 | void TypeSubstitution::remove( std::string formalType ) { |
---|
60 | TypeEnvType::iterator i = typeEnv.find( formalType ); |
---|
61 | if ( i != typeEnv.end() ) { |
---|
62 | typeEnv.erase( formalType ); |
---|
63 | } // if |
---|
64 | } |
---|
65 | |
---|
66 | const Type *TypeSubstitution::lookup( std::string formalType ) const { |
---|
67 | TypeEnvType::const_iterator i = typeEnv.find( formalType ); |
---|
68 | |
---|
69 | // break on not in substitution set |
---|
70 | if ( i == typeEnv.end() ) return 0; |
---|
71 | |
---|
72 | // attempt to transitively follow TypeInstType links. |
---|
73 | while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) { |
---|
74 | const std::string& typeName = actualType->name; |
---|
75 | |
---|
76 | // break cycles in the transitive follow |
---|
77 | if ( formalType == typeName ) break; |
---|
78 | |
---|
79 | // Look for the type this maps to, returning previous mapping if none-such |
---|
80 | i = typeEnv.find( typeName ); |
---|
81 | if ( i == typeEnv.end() ) return actualType; |
---|
82 | } |
---|
83 | |
---|
84 | // return type from substitution set |
---|
85 | return i->second; |
---|
86 | } |
---|
87 | |
---|
88 | bool TypeSubstitution::empty() const { |
---|
89 | return typeEnv.empty() && varEnv.empty(); |
---|
90 | } |
---|
91 | |
---|
92 | namespace { |
---|
93 | struct EnvTrimmer { |
---|
94 | const TypeSubstitution * env; |
---|
95 | TypeSubstitution * newEnv; |
---|
96 | EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){} |
---|
97 | void previsit( TypeDecl * tyDecl ) { |
---|
98 | // transfer known bindings for seen type variables |
---|
99 | if ( const Type * t = env->lookup( tyDecl->name ) ) { |
---|
100 | newEnv->add( tyDecl->name, t ); |
---|
101 | } |
---|
102 | } |
---|
103 | }; |
---|
104 | } // namespace |
---|
105 | |
---|
106 | /// reduce environment to just the parts that are referenced in a given expression |
---|
107 | TypeSubstitution * TypeSubstitution::newFromExpr( const Expr * expr, const TypeSubstitution * env ) { |
---|
108 | if ( env ) { |
---|
109 | TypeSubstitution * newEnv = new TypeSubstitution(); |
---|
110 | Pass<EnvTrimmer> trimmer( env, newEnv ); |
---|
111 | expr->accept( trimmer ); |
---|
112 | return newEnv; |
---|
113 | } |
---|
114 | return nullptr; |
---|
115 | } |
---|
116 | |
---|
117 | void TypeSubstitution::normalize() { |
---|
118 | Pass<Substituter> sub( *this, true ); |
---|
119 | do { |
---|
120 | sub.pass.subCount = 0; |
---|
121 | sub.pass.freeOnly = true; |
---|
122 | for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) { |
---|
123 | i->second = i->second->accept( sub ); |
---|
124 | } |
---|
125 | } while ( sub.pass.subCount ); |
---|
126 | } |
---|
127 | |
---|
128 | const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) { |
---|
129 | BoundVarsType::const_iterator bound = boundVars.find( inst->name ); |
---|
130 | if ( bound != boundVars.end() ) return inst; |
---|
131 | |
---|
132 | TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name ); |
---|
133 | if ( i == sub.typeEnv.end() ) { |
---|
134 | return inst; |
---|
135 | } else { |
---|
136 | // cut off infinite loop for the case where a type is bound to itself. |
---|
137 | // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here. |
---|
138 | // TODO: investigate preventing type variables from being bound to themselves in the first place. |
---|
139 | if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) { |
---|
140 | if ( inst->name == replacement->name ) { |
---|
141 | return inst; |
---|
142 | } |
---|
143 | } |
---|
144 | // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl; |
---|
145 | subCount++; |
---|
146 | ptr<Type> newType = i->second; // force clone if needed |
---|
147 | add_qualifiers( newType, inst->qualifiers ); |
---|
148 | // Note: need to recursively apply substitution to the new type because normalize does not |
---|
149 | // substitute bound vars, but bound vars must be substituted when not in freeOnly mode. |
---|
150 | newType = newType->accept( *visitor ); |
---|
151 | return newType.release(); |
---|
152 | } // if |
---|
153 | } |
---|
154 | |
---|
155 | const Expr * TypeSubstitution::Substituter::postvisit( const NameExpr * nameExpr ) { |
---|
156 | VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name ); |
---|
157 | if ( i == sub.varEnv.end() ) { |
---|
158 | return nameExpr; |
---|
159 | } else { |
---|
160 | subCount++; |
---|
161 | delete nameExpr; |
---|
162 | return i->second; |
---|
163 | } // if |
---|
164 | } |
---|
165 | |
---|
166 | void TypeSubstitution::Substituter::previsit( const ParameterizedType * ptype ) { |
---|
167 | GuardValue( boundVars ); |
---|
168 | // bind type variables from forall-qualifiers |
---|
169 | if ( freeOnly ) { |
---|
170 | for ( const TypeDecl * tyvar : ptype->forall ) { |
---|
171 | boundVars.insert( tyvar->name ); |
---|
172 | } // for |
---|
173 | } // if |
---|
174 | } |
---|
175 | |
---|
176 | void TypeSubstitution::Substituter::handleAggregateType( const ReferenceToType * type ) { |
---|
177 | GuardValue( boundVars ); |
---|
178 | // bind type variables from forall-qualifiers |
---|
179 | if ( freeOnly ) { |
---|
180 | for ( const TypeDecl * tyvar : type->forall ) { |
---|
181 | boundVars.insert( tyvar->name ); |
---|
182 | } // for |
---|
183 | // bind type variables from generic type instantiations |
---|
184 | if ( auto decl = type->aggr() ) { |
---|
185 | if ( ! type->params.empty() ) { |
---|
186 | for ( const TypeDecl * tyvar : decl->params ) { |
---|
187 | boundVars.insert( tyvar->name ); |
---|
188 | } // for |
---|
189 | } // if |
---|
190 | } |
---|
191 | } // if |
---|
192 | } |
---|
193 | |
---|
194 | void TypeSubstitution::Substituter::previsit( const StructInstType * aggregateUseType ) { |
---|
195 | handleAggregateType( aggregateUseType ); |
---|
196 | } |
---|
197 | |
---|
198 | void TypeSubstitution::Substituter::previsit( const UnionInstType *aggregateUseType ) { |
---|
199 | handleAggregateType( aggregateUseType ); |
---|
200 | } |
---|
201 | |
---|
202 | } // namespace ast |
---|
203 | |
---|
204 | // Local Variables: // |
---|
205 | // tab-width: 4 // |
---|
206 | // mode: c++ // |
---|
207 | // compile-command: "make install" // |
---|
208 | // End: // |
---|