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