source: src/SynTree/TypeSubstitution.h@ f89a111

new-env
Last change on this file since f89a111 was eff03a94, checked in by Aaron Moss <a3moss@…>, 7 years ago

Fixed infinite loop bugs

  • Property mode set to 100644
File size: 7.8 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 : Sat Jul 22 09:52:24 2017
13// Update Count : 3
14//
15
16#pragma once
17
18#include <cassert> // for assert
19#include <iosfwd> // for ostream
20#include <list> // for list<>::iterator, _List_iterator
21#include <map> // for _Rb_tree_iterator, map, map<>::val...
22#include <set> // for set
23#include <string> // for string, operator!=
24#include <utility> // for pair
25
26#include "Common/SemanticError.h" // for SemanticError
27#include "SynTree/Declaration.h" // for TypeDecl, Declaration (ptr only)
28#include "SynTree/Expression.h" // for Expression (ptr only), NameExpr (p...
29#include "SynTree/Type.h" // for Type, ArrayType (ptr only), BasicT...
30
31class TypeSubstitution {
32 public:
33 TypeSubstitution();
34 template< typename FormalIterator, typename ActualIterator >
35 TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
36 TypeSubstitution( const TypeSubstitution &other );
37
38 TypeSubstitution &operator=( const TypeSubstitution &other );
39
40 template< typename SynTreeClass > int apply( SynTreeClass *&input );
41 template< typename SynTreeClass > int applyFree( SynTreeClass *&input );
42
43 void add( std::string formalType, Type *actualType );
44 void add( const TypeSubstitution &other );
45 void remove( std::string formalType );
46 Type *lookup( std::string formalType ) const;
47 bool empty() const;
48
49 template< typename FormalIterator, typename ActualIterator >
50 void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
51
52 /// this function is unused...
53 template< typename TypeInstListIterator >
54 void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
55
56 /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr
57 static TypeSubstitution * newFromExpr( Expression * expr, TypeSubstitution * env );
58
59 void normalize();
60
61 void accept( Visitor& v ) { v.visit( this ); }
62 TypeSubstitution * acceptMutator( Mutator & m ) { return m.mutate( this ); }
63
64 void print( std::ostream &os, Indenter indent = {} ) const;
65 TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
66 private:
67
68 // Mutator that performs the substitution
69 struct Substituter;
70
71 // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
72
73 void initialize( const TypeSubstitution &src, TypeSubstitution &dest );
74
75 friend class Mutator;
76
77 template<typename pass_type>
78 friend class PassVisitor;
79
80 typedef std::map< std::string, Type* > TypeEnvType;
81 typedef std::map< std::string, Expression* > VarEnvType;
82 TypeEnvType typeEnv;
83 VarEnvType varEnv;
84
85 public:
86 // has to come after declaration of typeEnv
87 auto begin() -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
88 auto end() -> decltype( typeEnv. end() ) { return typeEnv. end(); }
89 auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
90 auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); }
91};
92
93template< typename FormalIterator, typename ActualIterator >
94void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
95 // FormalIterator points to a TypeDecl
96 // ActualIterator points to a Type
97 FormalIterator formalIt = formalBegin;
98 ActualIterator actualIt = actualBegin;
99 for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) {
100 if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) {
101 if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) {
102 if ( formal->get_name() != "" ) {
103 typeEnv[ formal->get_name() ] = actual->get_type()->clone();
104 } // if
105 } else {
106 SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) );
107 } // if
108 } else {
109 // TODO: type check the formal and actual parameters
110 if ( (*formalIt)->get_name() != "" ) {
111 varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone();
112 } // if
113 } // if
114 } // for
115}
116
117template< typename FormalIterator, typename ActualIterator >
118TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
119 add( formalBegin, formalEnd, actualBegin );
120}
121
122// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
123// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
124#include "Common/PassVisitor.h"
125
126// definitition must happen after PassVisitor is included so that WithGuards can be used
127struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> {
128 Substituter( TypeSubstitution & sub, bool freeOnly )
129 : sub( sub ), freeOnly( freeOnly ), boundVars(), equivVars() {}
130
131 Type * postmutate( TypeInstType * aggregateUseType );
132 Expression * postmutate( NameExpr * nameExpr );
133
134 /// Records type variable bindings from forall-statements
135 void premutate( Type * type );
136 /// Records type variable bindings from forall-statements and instantiations of generic types
137 template< typename TypeClass > void handleAggregateType( TypeClass * type );
138
139 void premutate( StructInstType * aggregateUseType );
140 void premutate( UnionInstType * aggregateUseType );
141
142 TypeSubstitution & sub;
143 int subCount = 0;
144 bool freeOnly;
145 typedef std::set< std::string > BoundVarsType;
146 BoundVarsType boundVars;
147 std::vector<TypeInstType*> equivVars; ///< equivalent names of type variables
148};
149
150template< typename SynTreeClass >
151int TypeSubstitution::apply( SynTreeClass *&input ) {
152 assert( input );
153 PassVisitor<Substituter> sub( *this, false );
154 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
155 assert( input );
156/// std::cerr << "substitution result is: ";
157/// newType->print( std::cerr );
158/// std::cerr << std::endl;
159 return sub.pass.subCount;
160}
161
162template< typename SynTreeClass >
163int TypeSubstitution::applyFree( SynTreeClass *&input ) {
164 assert( input );
165 PassVisitor<Substituter> sub( *this, true );
166 input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
167 assert( input );
168/// std::cerr << "substitution result is: ";
169/// newType->print( std::cerr );
170/// std::cerr << std::endl;
171 return sub.pass.subCount;
172}
173
174template< typename TypeInstListIterator >
175void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) {
176 // xxx - this function doesn't extract varEnv - is this intentional?
177 while ( begin != end ) {
178 TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() );
179 if ( cur != typeEnv.end() ) {
180 result.typeEnv[ cur->first ] = cur->second;
181 typeEnv.erase( cur );
182 } // if
183 } // while
184}
185
186/// Instantiate each member of the context given the actual parameters specified, and store the
187/// instantiations for use by the indexer
188template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
189void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) {
190 TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
191 for ( auto i = memberBegin; i != memberEnd; ++i ) {
192 sub.apply( *i );
193 *out++ = *i;
194 } // for
195}
196
197std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub );
198
199// Local Variables: //
200// tab-width: 4 //
201// mode: c++ //
202// compile-command: "make install" //
203// End: //
Note: See TracBrowser for help on using the repository browser.