source: src/SynTree/TypeSubstitution.h @ 172d9342

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 172d9342 was 172d9342, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

added old-to-new conversion for TypeSubstitution?, within a framework for expressions

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