[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(); |
---|
[4f6dda0] | 39 | template< typename FormalContainer, typename ActualContainer > |
---|
| 40 | TypeSubstitution( FormalContainer formals, ActualContainer actuals ); |
---|
[c671112] | 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 | |
---|
[2890212] | 48 | template< typename SynTreeClass > |
---|
| 49 | struct ApplyResult { |
---|
[c7f834e] | 50 | ast::ptr<SynTreeClass> node; |
---|
[2890212] | 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; |
---|
[76ed81f] | 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(); |
---|
[2890212] | 60 | auto ret = apply(p); |
---|
| 61 | input = ret.node; |
---|
| 62 | return ret.count; |
---|
[76ed81f] | 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(); |
---|
[2890212] | 68 | auto ret = applyFree(p); |
---|
| 69 | input = ret.node; |
---|
| 70 | return ret.count; |
---|
[76ed81f] | 71 | } |
---|
[c671112] | 72 | |
---|
[3e5dd913] | 73 | void add( const TypeInstType * formalType, const Type *actualType ); |
---|
| 74 | void add( const TypeInstType::TypeEnvKey & key, const Type *actualType ); |
---|
[c671112] | 75 | void add( const TypeSubstitution &other ); |
---|
[3e5dd913] | 76 | void remove( const TypeInstType * formalType ); |
---|
| 77 | const Type *lookup( const TypeInstType * formalType ) const; |
---|
[c671112] | 78 | bool empty() const; |
---|
| 79 | |
---|
[4f6dda0] | 80 | template< typename FormalContainer, typename ActualContainer > |
---|
| 81 | void addAll( FormalContainer formals, ActualContainer actuals ); |
---|
[c671112] | 82 | template< typename FormalIterator, typename ActualIterator > |
---|
[4f6dda0] | 83 | void addAll( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ); |
---|
[c671112] | 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 | |
---|
[7ff3e522] | 103 | template<typename core_t> |
---|
[c671112] | 104 | friend class Pass; |
---|
| 105 | |
---|
[3e5dd913] | 106 | typedef std::unordered_map< TypeInstType::TypeEnvKey, ptr<Type> > TypeEnvType; |
---|
[c671112] | 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(); } |
---|
[172d9342] | 115 | |
---|
[c671112] | 116 | }; |
---|
| 117 | |
---|
[4f6dda0] | 118 | template< typename FormalContainer, typename ActualContainer > |
---|
| 119 | TypeSubstitution::TypeSubstitution( FormalContainer formals, ActualContainer actuals ) { |
---|
| 120 | assert( formals.size() == actuals.size() ); |
---|
| 121 | addAll( formals.begin(), formals.end(), actuals.begin() ); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | template< typename FormalIterator, typename ActualIterator > |
---|
| 125 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
| 126 | addAll( formalBegin, formalEnd, actualBegin ); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | template< typename FormalContainer, typename ActualContainer > |
---|
| 130 | void TypeSubstitution::addAll( FormalContainer formals, ActualContainer actuals ) { |
---|
| 131 | assert( formals.size() == actuals.size() ); |
---|
| 132 | addAll( formals.begin(), formals.end(), actuals.begin() ); |
---|
| 133 | } |
---|
| 134 | |
---|
[3e5dd913] | 135 | // this is the only place where type parameters outside a function formal may be substituted. |
---|
[c671112] | 136 | template< typename FormalIterator, typename ActualIterator > |
---|
[4f6dda0] | 137 | void TypeSubstitution::addAll( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
[c671112] | 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 != "" ) { |
---|
[3e5dd913] | 146 | typeEnv[ formal ] = actual->type; |
---|
[c671112] | 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 { |
---|
[4f6dda0] | 152 | // Is this an error? |
---|
[c671112] | 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" |
---|
[5c9b20c] | 162 | #include "Copy.hpp" |
---|
[c671112] | 163 | |
---|
| 164 | namespace ast { |
---|
| 165 | |
---|
| 166 | // definitition must happen after PassVisitor is included so that WithGuards can be used |
---|
[d3aa64f1] | 167 | struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter>, public PureVisitor { |
---|
[c15085d] | 168 | static size_t traceId; |
---|
[c671112] | 169 | |
---|
| 170 | Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} |
---|
| 171 | |
---|
[55b6476] | 172 | const Type * postvisit( const TypeInstType * aggregateUseType ); |
---|
[c671112] | 173 | |
---|
| 174 | /// Records type variable bindings from forall-statements |
---|
[361bf01] | 175 | void previsit( const FunctionType * type ); |
---|
[c671112] | 176 | /// Records type variable bindings from forall-statements and instantiations of generic types |
---|
[3e5dd913] | 177 | // void handleAggregateType( const BaseInstType * type ); |
---|
[c671112] | 178 | |
---|
[3e5dd913] | 179 | // void previsit( const StructInstType * aggregateUseType ); |
---|
| 180 | // void previsit( const UnionInstType * aggregateUseType ); |
---|
[c671112] | 181 | |
---|
| 182 | const TypeSubstitution & sub; |
---|
| 183 | int subCount = 0; |
---|
| 184 | bool freeOnly; |
---|
[3e5dd913] | 185 | typedef std::unordered_set< TypeInstType::TypeEnvKey > BoundVarsType; |
---|
[c671112] | 186 | BoundVarsType boundVars; |
---|
| 187 | |
---|
| 188 | }; |
---|
| 189 | |
---|
| 190 | template< typename SynTreeClass > |
---|
[2890212] | 191 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::apply( const SynTreeClass * input ) const { |
---|
[c671112] | 192 | assert( input ); |
---|
| 193 | Pass<Substituter> sub( *this, false ); |
---|
[d3aa64f1] | 194 | input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); |
---|
[7ff3e522] | 195 | return { input, sub.core.subCount }; |
---|
[c671112] | 196 | } |
---|
| 197 | |
---|
| 198 | template< typename SynTreeClass > |
---|
[2890212] | 199 | TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::applyFree( const SynTreeClass * input ) const { |
---|
[c671112] | 200 | assert( input ); |
---|
| 201 | Pass<Substituter> sub( *this, true ); |
---|
[d3aa64f1] | 202 | input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); |
---|
[7ff3e522] | 203 | return { input, sub.core.subCount }; |
---|
[c671112] | 204 | } |
---|
| 205 | |
---|
| 206 | } // namespace ast |
---|
| 207 | |
---|
| 208 | // Local Variables: // |
---|
| 209 | // tab-width: 4 // |
---|
| 210 | // mode: c++ // |
---|
| 211 | // compile-command: "make install" // |
---|
| 212 | // End: // |
---|