source: src/SynTree/TypeSubstitution.h @ 68f9c43

new-envwith_gc
Last change on this file since 68f9c43 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

  • 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 : 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        TypeSubstitution * acceptMutator( Mutator & m ) { return m.mutate( this ); }
62
63        void print( std::ostream &os, Indenter indent = {} ) const;
64        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
65  private:
66
67        // Mutator that performs the substitution
68        struct Substituter;
69
70        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
71
72        void initialize( const TypeSubstitution &src, TypeSubstitution &dest );
73
74        friend class Mutator;
75
76        template<typename pass_type>
77        friend class PassVisitor;
78
79        typedef std::map< std::string, Type* > TypeEnvType;
80        typedef std::map< std::string, Expression* > VarEnvType;
81        TypeEnvType typeEnv;
82        VarEnvType varEnv;
83
84  public:
85        // has to come after declaration of typeEnv
86        auto begin()       -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
87        auto   end()       -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
88        auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
89        auto   end() const -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
90};
91
92template< typename FormalIterator, typename ActualIterator >
93void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
94        // FormalIterator points to a TypeDecl
95        // ActualIterator points to a Type
96        FormalIterator formalIt = formalBegin;
97        ActualIterator actualIt = actualBegin;
98        for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) {
99                if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) {
100                        if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) {
101                                if ( formal->get_name() != "" ) {
102                                        typeEnv[ formal->get_name() ] = actual->get_type()->clone();
103                                } // if
104                        } else {
105                                SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) );
106                        } // if
107                } else {
108                        // TODO: type check the formal and actual parameters
109                        if ( (*formalIt)->get_name() != "" ) {
110                                varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone();
111                        } // if
112                } // if
113        } // for
114}
115
116template< typename FormalIterator, typename ActualIterator >
117TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
118        add( formalBegin, formalEnd, actualBegin );
119}
120
121// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
122// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
123#include "Common/PassVisitor.h"
124
125// definitition must happen after PassVisitor is included so that WithGuards can be used
126struct TypeSubstitution::Substituter : public WithGuards {
127                Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
128
129                Type * postmutate( TypeInstType * aggregateUseType );
130                Expression * postmutate( NameExpr * nameExpr );
131
132                /// Records type variable bindings from forall-statements
133                void premutate( Type * type );
134                /// Records type variable bindings from forall-statements and instantiations of generic types
135                template< typename TypeClass > void handleAggregateType( TypeClass * type );
136
137                void premutate( StructInstType * aggregateUseType );
138                void premutate( UnionInstType * aggregateUseType );
139
140                TypeSubstitution & sub;
141                int subCount = 0;
142                bool freeOnly;
143                typedef std::set< std::string > BoundVarsType;
144                BoundVarsType boundVars;
145};
146
147template< typename SynTreeClass >
148int TypeSubstitution::apply( SynTreeClass *&input ) {
149        assert( input );
150        PassVisitor<Substituter> sub( *this, false );
151        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
152        assert( input );
153///     std::cerr << "substitution result is: ";
154///     newType->print( std::cerr );
155///     std::cerr << std::endl;
156        return sub.pass.subCount;
157}
158
159template< typename SynTreeClass >
160int TypeSubstitution::applyFree( SynTreeClass *&input ) {
161        assert( input );
162        PassVisitor<Substituter> sub( *this, true );
163        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
164        assert( input );
165///     std::cerr << "substitution result is: ";
166///     newType->print( std::cerr );
167///     std::cerr << std::endl;
168        return sub.pass.subCount;
169}
170
171template< typename TypeInstListIterator >
172void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) {
173        // xxx - this function doesn't extract varEnv - is this intentional?
174        while ( begin != end ) {
175                TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() );
176                if ( cur != typeEnv.end() ) {
177                        result.typeEnv[ cur->first ] = cur->second;
178                        typeEnv.erase( cur );
179                } // if
180        } // while
181}
182
183/// Instantiate each member of the context given the actual parameters specified, and store the
184/// instantiations for use by the indexer
185template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
186void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) {
187        TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
188        for ( auto i = memberBegin; i != memberEnd; ++i ) {
189                sub.apply( *i );
190                *out++ = *i;
191        } // for
192}
193
194std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub );
195
196// Local Variables: //
197// tab-width: 4 //
198// mode: c++ //
199// compile-command: "make install" //
200// End: //
Note: See TracBrowser for help on using the repository browser.