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