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.h -- |
---|
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 : Tue Apr 30 22:52:47 2019 |
---|
13 | // Update Count : 9 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <cassert> // for assert |
---|
19 | #include <list> // for list<>::iterator, _List_iterator |
---|
20 | #include <unordered_map> |
---|
21 | #include <unordered_set> |
---|
22 | #include <string> // for string, operator!= |
---|
23 | #include <utility> // for pair |
---|
24 | |
---|
25 | #include "Fwd.hpp" // for UniqueId |
---|
26 | #include "ParseNode.hpp" |
---|
27 | #include "Type.hpp" |
---|
28 | #include "Common/SemanticError.h" // for SemanticError |
---|
29 | #include "Visitor.hpp" |
---|
30 | #include "Decl.hpp" |
---|
31 | #include "Expr.hpp" |
---|
32 | #include "Node.hpp" |
---|
33 | |
---|
34 | namespace ast { |
---|
35 | |
---|
36 | class TypeSubstitution : public Node { |
---|
37 | public: |
---|
38 | TypeSubstitution(); |
---|
39 | template< typename FormalIterator, typename ActualIterator > |
---|
40 | TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ); |
---|
41 | TypeSubstitution( const TypeSubstitution &other ); |
---|
42 | virtual ~TypeSubstitution(); |
---|
43 | |
---|
44 | TypeSubstitution &operator=( const TypeSubstitution &other ); |
---|
45 | |
---|
46 | template< typename SynTreeClass > |
---|
47 | struct ApplyResult { |
---|
48 | // const SynTreeClass * node; |
---|
49 | ast::ptr<SynTreeClass> node; |
---|
50 | int count; |
---|
51 | }; |
---|
52 | |
---|
53 | template< typename SynTreeClass > ApplyResult<SynTreeClass> apply( const SynTreeClass * input ) const; |
---|
54 | template< typename SynTreeClass > ApplyResult<SynTreeClass> applyFree( const SynTreeClass * input ) const; |
---|
55 | |
---|
56 | template< typename node_t, enum Node::ref_type ref_t > |
---|
57 | int apply( ptr_base< node_t, ref_t > & input ) const { |
---|
58 | const node_t * p = input.get(); |
---|
59 | auto ret = apply(p); |
---|
60 | input = ret.node; |
---|
61 | return ret.count; |
---|
62 | } |
---|
63 | |
---|
64 | template< typename node_t, enum Node::ref_type ref_t > |
---|
65 | int applyFree( ptr_base< node_t, ref_t > & input ) const { |
---|
66 | const node_t * p = input.get(); |
---|
67 | auto ret = applyFree(p); |
---|
68 | input = ret.node; |
---|
69 | return ret.count; |
---|
70 | } |
---|
71 | |
---|
72 | void add( std::string formalType, const Type *actualType ); |
---|
73 | void add( const TypeSubstitution &other ); |
---|
74 | void remove( std::string formalType ); |
---|
75 | const Type *lookup( std::string formalType ) const; |
---|
76 | bool empty() const; |
---|
77 | |
---|
78 | void addVar( std::string formalExpr, const Expr *actualExpr ); |
---|
79 | |
---|
80 | template< typename FormalIterator, typename ActualIterator > |
---|
81 | void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ); |
---|
82 | |
---|
83 | /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr |
---|
84 | static TypeSubstitution * newFromExpr( const Expr * expr, const TypeSubstitution * env ); |
---|
85 | |
---|
86 | void normalize(); |
---|
87 | |
---|
88 | const TypeSubstitution * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
89 | |
---|
90 | TypeSubstitution * clone() const override { return new TypeSubstitution( *this ); } |
---|
91 | |
---|
92 | private: |
---|
93 | |
---|
94 | // Mutator that performs the substitution |
---|
95 | struct Substituter; |
---|
96 | |
---|
97 | // TODO: worry about traversing into a forall-qualified function type or type decl with assertions |
---|
98 | |
---|
99 | void initialize( const TypeSubstitution &src, TypeSubstitution &dest ); |
---|
100 | |
---|
101 | template<typename core_t> |
---|
102 | friend class Pass; |
---|
103 | |
---|
104 | typedef std::unordered_map< std::string, ptr<Type> > TypeEnvType; |
---|
105 | typedef std::unordered_map< std::string, ptr<Expr> > VarEnvType; |
---|
106 | TypeEnvType typeEnv; |
---|
107 | VarEnvType varEnv; |
---|
108 | |
---|
109 | public: |
---|
110 | // has to come after declaration of typeEnv |
---|
111 | auto begin() -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } |
---|
112 | auto end() -> decltype( typeEnv. end() ) { return typeEnv. end(); } |
---|
113 | auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } |
---|
114 | auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); } |
---|
115 | |
---|
116 | auto beginVar() -> decltype( varEnv.begin() ) { return varEnv.begin(); } |
---|
117 | auto endVar() -> decltype( varEnv. end() ) { return varEnv. end(); } |
---|
118 | auto beginVar() const -> decltype( varEnv.begin() ) { return varEnv.begin(); } |
---|
119 | auto endVar() const -> decltype( varEnv. end() ) { return varEnv. end(); } |
---|
120 | }; |
---|
121 | |
---|
122 | template< typename FormalIterator, typename ActualIterator > |
---|
123 | void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
124 | // FormalIterator points to a TypeDecl |
---|
125 | // ActualIterator points to a Type |
---|
126 | FormalIterator formalIt = formalBegin; |
---|
127 | ActualIterator actualIt = actualBegin; |
---|
128 | for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) { |
---|
129 | if ( const TypeDecl *formal = formalIt->template as<TypeDecl>() ) { |
---|
130 | if ( const TypeExpr *actual = actualIt->template as<TypeExpr>() ) { |
---|
131 | if ( formal->name != "" ) { |
---|
132 | typeEnv[ formal->name ] = actual->type; |
---|
133 | } // if |
---|
134 | } else { |
---|
135 | SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) ); |
---|
136 | } // if |
---|
137 | } else { |
---|
138 | // TODO: type check the formal and actual parameters |
---|
139 | if ( (*formalIt)->name != "" ) { |
---|
140 | varEnv[ (*formalIt)->name ] = *actualIt; |
---|
141 | } // if |
---|
142 | } // if |
---|
143 | } // for |
---|
144 | } |
---|
145 | |
---|
146 | template< typename FormalIterator, typename ActualIterator > |
---|
147 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
148 | add( formalBegin, formalEnd, actualBegin ); |
---|
149 | } |
---|
150 | |
---|
151 | } // namespace ast |
---|
152 | |
---|
153 | // include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and |
---|
154 | // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals. |
---|
155 | #include "Pass.hpp" |
---|
156 | #include "Copy.hpp" |
---|
157 | |
---|
158 | namespace ast { |
---|
159 | |
---|
160 | // definitition must happen after PassVisitor is included so that WithGuards can be used |
---|
161 | struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> { |
---|
162 | static size_t traceId; |
---|
163 | |
---|
164 | Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} |
---|
165 | |
---|
166 | const Type * postvisit( const TypeInstType * aggregateUseType ); |
---|
167 | const Expr * postvisit( const NameExpr * nameExpr ); |
---|
168 | |
---|
169 | /// Records type variable bindings from forall-statements |
---|
170 | void previsit( const ParameterizedType * type ); |
---|
171 | /// Records type variable bindings from forall-statements and instantiations of generic types |
---|
172 | void handleAggregateType( const ReferenceToType * type ); |
---|
173 | |
---|
174 | void previsit( const StructInstType * aggregateUseType ); |
---|
175 | void previsit( const UnionInstType * aggregateUseType ); |
---|
176 | |
---|
177 | const TypeSubstitution & sub; |
---|
178 | int subCount = 0; |
---|
179 | bool freeOnly; |
---|
180 | typedef std::unordered_set< std::string > BoundVarsType; |
---|
181 | BoundVarsType boundVars; |
---|
182 | |
---|
183 | }; |
---|
184 | |
---|
185 | template< typename SynTreeClass > |
---|
186 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::apply( const SynTreeClass * input ) const { |
---|
187 | assert( input ); |
---|
188 | Pass<Substituter> sub( *this, false ); |
---|
189 | input = strict_dynamic_cast< const SynTreeClass * >( deepCopy(input)->accept( sub ) ); |
---|
190 | return { input, sub.core.subCount }; |
---|
191 | } |
---|
192 | |
---|
193 | template< typename SynTreeClass > |
---|
194 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::applyFree( const SynTreeClass * input ) const { |
---|
195 | assert( input ); |
---|
196 | Pass<Substituter> sub( *this, true ); |
---|
197 | input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); |
---|
198 | return { input, sub.core.subCount }; |
---|
199 | } |
---|
200 | |
---|
201 | } // namespace ast |
---|
202 | |
---|
203 | // Local Variables: // |
---|
204 | // tab-width: 4 // |
---|
205 | // mode: c++ // |
---|
206 | // compile-command: "make install" // |
---|
207 | // End: // |
---|