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