source: src/AST/TypeSubstitution.hpp @ f6e6a55

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since f6e6a55 was 4f6dda0, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Converted Implement Concurrent Keywords to the new AST. Includes updates to various helpers, including the virtual table and a lot of examine helpers.

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