[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 |
---|
| 12 | // Last Modified On : Sat Jul 22 09:52:24 2017 |
---|
| 13 | // Update Count : 3 |
---|
[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 |
---|
| 21 | #include <map> // for _Rb_tree_iterator, map, map<>::val... |
---|
| 22 | #include <set> // for set |
---|
| 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 ); |
---|
[5382492] | 37 | |
---|
[0dd3a2f] | 38 | TypeSubstitution &operator=( const TypeSubstitution &other ); |
---|
[5382492] | 39 | |
---|
[0dd3a2f] | 40 | template< typename SynTreeClass > int apply( SynTreeClass *&input ); |
---|
| 41 | template< typename SynTreeClass > int applyFree( SynTreeClass *&input ); |
---|
[5382492] | 42 | |
---|
[0dd3a2f] | 43 | void add( std::string formalType, Type *actualType ); |
---|
| 44 | void add( const TypeSubstitution &other ); |
---|
| 45 | void remove( std::string formalType ); |
---|
| 46 | Type *lookup( std::string formalType ) const; |
---|
| 47 | bool empty() const; |
---|
[5382492] | 48 | |
---|
[0dd3a2f] | 49 | template< typename FormalIterator, typename ActualIterator > |
---|
| 50 | void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ); |
---|
[5382492] | 51 | |
---|
[540de412] | 52 | /// this function is unused... |
---|
[0dd3a2f] | 53 | template< typename TypeInstListIterator > |
---|
| 54 | void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ); |
---|
[5382492] | 55 | |
---|
[2ec65ad] | 56 | /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr |
---|
| 57 | static TypeSubstitution * newFromExpr( Expression * expr, TypeSubstitution * env ); |
---|
| 58 | |
---|
[0dd3a2f] | 59 | void normalize(); |
---|
[51b7345] | 60 | |
---|
[8d7bef2] | 61 | void accept( Visitor& v ) { v.visit( this ); } |
---|
[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 | |
---|
[0dd3a2f] | 80 | typedef std::map< std::string, Type* > TypeEnvType; |
---|
| 81 | typedef std::map< std::string, Expression* > VarEnvType; |
---|
| 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 ) { |
---|
| 100 | if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) { |
---|
| 101 | if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) { |
---|
| 102 | if ( formal->get_name() != "" ) { |
---|
| 103 | typeEnv[ formal->get_name() ] = actual->get_type()->clone(); |
---|
| 104 | } // if |
---|
| 105 | } else { |
---|
[a16764a6] | 106 | SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) ); |
---|
[0dd3a2f] | 107 | } // if |
---|
| 108 | } else { |
---|
| 109 | // TODO: type check the formal and actual parameters |
---|
| 110 | if ( (*formalIt)->get_name() != "" ) { |
---|
| 111 | varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone(); |
---|
| 112 | } // if |
---|
| 113 | } // if |
---|
| 114 | } // for |
---|
[51b7345] | 115 | } |
---|
| 116 | |
---|
| 117 | template< typename FormalIterator, typename ActualIterator > |
---|
[e9a715d3] | 118 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
[0dd3a2f] | 119 | add( formalBegin, formalEnd, actualBegin ); |
---|
[51b7345] | 120 | } |
---|
| 121 | |
---|
[e9a715d3] | 122 | // include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and |
---|
| 123 | // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals. |
---|
| 124 | #include "Common/PassVisitor.h" |
---|
| 125 | |
---|
| 126 | // definitition must happen after PassVisitor is included so that WithGuards can be used |
---|
[d0d5054] | 127 | struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> { |
---|
[e9a715d3] | 128 | Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} |
---|
| 129 | |
---|
| 130 | Type * postmutate( TypeInstType * aggregateUseType ); |
---|
| 131 | Expression * postmutate( NameExpr * nameExpr ); |
---|
| 132 | |
---|
| 133 | /// Records type variable bindings from forall-statements |
---|
| 134 | void premutate( Type * type ); |
---|
| 135 | /// Records type variable bindings from forall-statements and instantiations of generic types |
---|
| 136 | template< typename TypeClass > void handleAggregateType( TypeClass * type ); |
---|
| 137 | |
---|
| 138 | void premutate( StructInstType * aggregateUseType ); |
---|
| 139 | void premutate( UnionInstType * aggregateUseType ); |
---|
| 140 | |
---|
| 141 | TypeSubstitution & sub; |
---|
| 142 | int subCount = 0; |
---|
| 143 | bool freeOnly; |
---|
| 144 | typedef std::set< std::string > BoundVarsType; |
---|
| 145 | BoundVarsType boundVars; |
---|
| 146 | }; |
---|
| 147 | |
---|
[51b7345] | 148 | template< typename SynTreeClass > |
---|
[0dd3a2f] | 149 | int TypeSubstitution::apply( SynTreeClass *&input ) { |
---|
| 150 | assert( input ); |
---|
[e9a715d3] | 151 | PassVisitor<Substituter> sub( *this, false ); |
---|
| 152 | input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); |
---|
[0dd3a2f] | 153 | assert( input ); |
---|
[e9a715d3] | 154 | /// std::cerr << "substitution result is: "; |
---|
| 155 | /// newType->print( std::cerr ); |
---|
| 156 | /// std::cerr << std::endl; |
---|
| 157 | return sub.pass.subCount; |
---|
[51b7345] | 158 | } |
---|
[5382492] | 159 | |
---|
[51b7345] | 160 | template< typename SynTreeClass > |
---|
[0dd3a2f] | 161 | int TypeSubstitution::applyFree( SynTreeClass *&input ) { |
---|
| 162 | assert( input ); |
---|
[e9a715d3] | 163 | PassVisitor<Substituter> sub( *this, true ); |
---|
| 164 | input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) ); |
---|
[0dd3a2f] | 165 | assert( input ); |
---|
[e9a715d3] | 166 | /// std::cerr << "substitution result is: "; |
---|
| 167 | /// newType->print( std::cerr ); |
---|
| 168 | /// std::cerr << std::endl; |
---|
| 169 | return sub.pass.subCount; |
---|
[51b7345] | 170 | } |
---|
[5382492] | 171 | |
---|
[51b7345] | 172 | template< typename TypeInstListIterator > |
---|
[0dd3a2f] | 173 | void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) { |
---|
[d8ba086] | 174 | // xxx - this function doesn't extract varEnv - is this intentional? |
---|
[0dd3a2f] | 175 | while ( begin != end ) { |
---|
| 176 | TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() ); |
---|
| 177 | if ( cur != typeEnv.end() ) { |
---|
| 178 | result.typeEnv[ cur->first ] = cur->second; |
---|
| 179 | typeEnv.erase( cur ); |
---|
| 180 | } // if |
---|
| 181 | } // while |
---|
[51b7345] | 182 | } |
---|
| 183 | |
---|
[82dd287] | 184 | /// Instantiate each member of the context given the actual parameters specified, and store the |
---|
| 185 | /// instantiations for use by the indexer |
---|
[51b7345] | 186 | template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator > |
---|
[0dd3a2f] | 187 | void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) { |
---|
| 188 | TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual ); |
---|
[be9036d] | 189 | for ( auto i = memberBegin; i != memberEnd; ++i ) { |
---|
[79970ed] | 190 | sub.apply( *i ); |
---|
| 191 | *out++ = *i; |
---|
[0dd3a2f] | 192 | } // for |
---|
[51b7345] | 193 | } |
---|
| 194 | |
---|
[5382492] | 195 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ); |
---|
| 196 | |
---|
[0dd3a2f] | 197 | // Local Variables: // |
---|
| 198 | // tab-width: 4 // |
---|
| 199 | // mode: c++ // |
---|
| 200 | // compile-command: "make install" // |
---|
| 201 | // End: // |
---|