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