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