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