[c671112] | 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" |
---|
[0b57626] | 27 | #include "Type.hpp" |
---|
[c671112] | 28 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 29 | #include "Visitor.hpp" |
---|
| 30 | #include "Decl.hpp" |
---|
| 31 | #include "Expr.hpp" |
---|
[76ed81f] | 32 | #include "Node.hpp" |
---|
[c671112] | 33 | |
---|
| 34 | namespace ast { |
---|
| 35 | |
---|
| 36 | class 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 | |
---|
[2890212] | 46 | template< typename SynTreeClass > |
---|
| 47 | struct ApplyResult { |
---|
[c7f834e] | 48 | ast::ptr<SynTreeClass> node; |
---|
[2890212] | 49 | int count; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | template< typename SynTreeClass > ApplyResult<SynTreeClass> apply( const SynTreeClass * input ) const; |
---|
| 53 | template< typename SynTreeClass > ApplyResult<SynTreeClass> applyFree( const SynTreeClass * input ) const; |
---|
[76ed81f] | 54 | |
---|
| 55 | template< typename node_t, enum Node::ref_type ref_t > |
---|
| 56 | int apply( ptr_base< node_t, ref_t > & input ) const { |
---|
| 57 | const node_t * p = input.get(); |
---|
[2890212] | 58 | auto ret = apply(p); |
---|
| 59 | input = ret.node; |
---|
| 60 | return ret.count; |
---|
[76ed81f] | 61 | } |
---|
| 62 | |
---|
| 63 | template< typename node_t, enum Node::ref_type ref_t > |
---|
| 64 | int applyFree( ptr_base< node_t, ref_t > & input ) const { |
---|
| 65 | const node_t * p = input.get(); |
---|
[2890212] | 66 | auto ret = applyFree(p); |
---|
| 67 | input = ret.node; |
---|
| 68 | return ret.count; |
---|
[76ed81f] | 69 | } |
---|
[c671112] | 70 | |
---|
[3e5dd913] | 71 | void add( const TypeInstType * formalType, const Type *actualType ); |
---|
| 72 | void add( const TypeInstType::TypeEnvKey & key, const Type *actualType ); |
---|
[c671112] | 73 | void add( const TypeSubstitution &other ); |
---|
[3e5dd913] | 74 | void remove( const TypeInstType * formalType ); |
---|
| 75 | const Type *lookup( const TypeInstType * formalType ) const; |
---|
[c671112] | 76 | bool empty() const; |
---|
| 77 | |
---|
| 78 | template< typename FormalIterator, typename ActualIterator > |
---|
| 79 | void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ); |
---|
| 80 | |
---|
| 81 | /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr |
---|
| 82 | static TypeSubstitution * newFromExpr( const Expr * expr, const TypeSubstitution * env ); |
---|
| 83 | |
---|
| 84 | void normalize(); |
---|
| 85 | |
---|
| 86 | const TypeSubstitution * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 87 | |
---|
| 88 | TypeSubstitution * clone() const override { return new TypeSubstitution( *this ); } |
---|
| 89 | |
---|
| 90 | private: |
---|
| 91 | |
---|
| 92 | // Mutator that performs the substitution |
---|
| 93 | struct Substituter; |
---|
| 94 | |
---|
| 95 | // TODO: worry about traversing into a forall-qualified function type or type decl with assertions |
---|
| 96 | |
---|
| 97 | void initialize( const TypeSubstitution &src, TypeSubstitution &dest ); |
---|
| 98 | |
---|
[7ff3e522] | 99 | template<typename core_t> |
---|
[c671112] | 100 | friend class Pass; |
---|
| 101 | |
---|
[3e5dd913] | 102 | typedef std::unordered_map< TypeInstType::TypeEnvKey, ptr<Type> > TypeEnvType; |
---|
[c671112] | 103 | TypeEnvType typeEnv; |
---|
| 104 | |
---|
| 105 | public: |
---|
| 106 | // has to come after declaration of typeEnv |
---|
| 107 | auto begin() -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } |
---|
| 108 | auto end() -> decltype( typeEnv. end() ) { return typeEnv. end(); } |
---|
| 109 | auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); } |
---|
| 110 | auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); } |
---|
[172d9342] | 111 | |
---|
[c671112] | 112 | }; |
---|
| 113 | |
---|
[3e5dd913] | 114 | // this is the only place where type parameters outside a function formal may be substituted. |
---|
[c671112] | 115 | template< typename FormalIterator, typename ActualIterator > |
---|
| 116 | void 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 != "" ) { |
---|
[3e5dd913] | 125 | typeEnv[ formal ] = actual->type; |
---|
[c671112] | 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 { |
---|
[3e5dd913] | 131 | |
---|
[c671112] | 132 | } // if |
---|
| 133 | } // for |
---|
| 134 | } |
---|
| 135 | |
---|
[3e5dd913] | 136 | |
---|
| 137 | |
---|
[c671112] | 138 | template< typename FormalIterator, typename ActualIterator > |
---|
| 139 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
| 140 | add( formalBegin, formalEnd, actualBegin ); |
---|
| 141 | } |
---|
| 142 | |
---|
[3e5dd913] | 143 | |
---|
[c671112] | 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" |
---|
[5c9b20c] | 149 | #include "Copy.hpp" |
---|
[c671112] | 150 | |
---|
| 151 | namespace ast { |
---|
| 152 | |
---|
| 153 | // definitition must happen after PassVisitor is included so that WithGuards can be used |
---|
[d3aa64f1] | 154 | struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter>, public PureVisitor { |
---|
[c15085d] | 155 | static size_t traceId; |
---|
[c671112] | 156 | |
---|
| 157 | Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} |
---|
| 158 | |
---|
[55b6476] | 159 | const Type * postvisit( const TypeInstType * aggregateUseType ); |
---|
[c671112] | 160 | |
---|
| 161 | /// Records type variable bindings from forall-statements |
---|
[361bf01] | 162 | void previsit( const FunctionType * type ); |
---|
[c671112] | 163 | /// Records type variable bindings from forall-statements and instantiations of generic types |
---|
[3e5dd913] | 164 | // void handleAggregateType( const BaseInstType * type ); |
---|
[c671112] | 165 | |
---|
[3e5dd913] | 166 | // void previsit( const StructInstType * aggregateUseType ); |
---|
| 167 | // void previsit( const UnionInstType * aggregateUseType ); |
---|
[c671112] | 168 | |
---|
| 169 | const TypeSubstitution & sub; |
---|
| 170 | int subCount = 0; |
---|
| 171 | bool freeOnly; |
---|
[3e5dd913] | 172 | typedef std::unordered_set< TypeInstType::TypeEnvKey > BoundVarsType; |
---|
[c671112] | 173 | BoundVarsType boundVars; |
---|
| 174 | |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | template< typename SynTreeClass > |
---|
[2890212] | 178 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::apply( const SynTreeClass * input ) const { |
---|
[c671112] | 179 | assert( input ); |
---|
| 180 | Pass<Substituter> sub( *this, false ); |
---|
[d3aa64f1] | 181 | input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); |
---|
[7ff3e522] | 182 | return { input, sub.core.subCount }; |
---|
[c671112] | 183 | } |
---|
| 184 | |
---|
| 185 | template< typename SynTreeClass > |
---|
[2890212] | 186 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::applyFree( const SynTreeClass * input ) const { |
---|
[c671112] | 187 | assert( input ); |
---|
| 188 | Pass<Substituter> sub( *this, true ); |
---|
[d3aa64f1] | 189 | input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); |
---|
[7ff3e522] | 190 | return { input, sub.core.subCount }; |
---|
[c671112] | 191 | } |
---|
| 192 | |
---|
| 193 | } // namespace ast |
---|
| 194 | |
---|
| 195 | // Local Variables: // |
---|
| 196 | // tab-width: 4 // |
---|
| 197 | // mode: c++ // |
---|
| 198 | // compile-command: "make install" // |
---|
| 199 | // End: // |
---|