source: src/AST/TypeSubstitution.hpp@ 3c6e417

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3c6e417 was 0b57626, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Silenced a warning and removed trailing whitespace.

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