| 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 | // | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef TYPESUBSTITUTION_H | 
|---|
| 17 | #define TYPESUBSTITUTION_H | 
|---|
| 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 |  | 
|---|
| 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(); | 
|---|
| 54 |  | 
|---|
| 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 | /// Records type variable bindings from forall-statements | 
|---|
| 62 | template< typename TypeClass > Type *handleType( TypeClass *type ); | 
|---|
| 63 | /// Records type variable bindings from forall-statements and instantiations of generic types | 
|---|
| 64 | template< typename TypeClass > Type *handleAggregateType( TypeClass *type ); | 
|---|
| 65 |  | 
|---|
| 66 | virtual Type* mutate(VoidType *basicType); | 
|---|
| 67 | virtual Type* mutate(BasicType *basicType); | 
|---|
| 68 | virtual Type* mutate(PointerType *pointerType); | 
|---|
| 69 | virtual Type* mutate(ArrayType *arrayType); | 
|---|
| 70 | virtual Type* mutate(FunctionType *functionType); | 
|---|
| 71 | virtual Type* mutate(StructInstType *aggregateUseType); | 
|---|
| 72 | virtual Type* mutate(UnionInstType *aggregateUseType); | 
|---|
| 73 | virtual Type* mutate(EnumInstType *aggregateUseType); | 
|---|
| 74 | virtual Type* mutate(ContextInstType *aggregateUseType); | 
|---|
| 75 | virtual Type* mutate(TupleType *tupleType); | 
|---|
| 76 | virtual Type* mutate(VarArgsType *varArgsType); | 
|---|
| 77 |  | 
|---|
| 78 | // TODO: worry about traversing into a forall-qualified function type or type decl with assertions | 
|---|
| 79 |  | 
|---|
| 80 | void initialize( const TypeSubstitution &src, TypeSubstitution &dest ); | 
|---|
| 81 |  | 
|---|
| 82 | typedef std::map< std::string, Type* > TypeEnvType; | 
|---|
| 83 | typedef std::map< std::string, Expression* > VarEnvType; | 
|---|
| 84 | typedef std::set< std::string > BoundVarsType; | 
|---|
| 85 | TypeEnvType typeEnv; | 
|---|
| 86 | VarEnvType varEnv; | 
|---|
| 87 | BoundVarsType boundVars; | 
|---|
| 88 | int subCount; | 
|---|
| 89 | bool freeOnly; | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | template< typename FormalIterator, typename ActualIterator > | 
|---|
| 93 | void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { | 
|---|
| 94 | // FormalIterator points to a TypeDecl | 
|---|
| 95 | // ActualIterator points to a Type | 
|---|
| 96 | FormalIterator formalIt = formalBegin; | 
|---|
| 97 | ActualIterator actualIt = actualBegin; | 
|---|
| 98 | for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) { | 
|---|
| 99 | if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) { | 
|---|
| 100 | if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) { | 
|---|
| 101 | if ( formal->get_name() != "" ) { | 
|---|
| 102 | TypeEnvType::iterator i = typeEnv.find( formal->get_name() ); | 
|---|
| 103 | if ( i != typeEnv.end() ) { | 
|---|
| 104 | delete i->second; | 
|---|
| 105 | } // if | 
|---|
| 106 | typeEnv[ formal->get_name() ] = actual->get_type()->clone(); | 
|---|
| 107 | } // if | 
|---|
| 108 | } else { | 
|---|
| 109 | throw SemanticError( "Attempt to provide non-type parameter for type parameter", formal ); | 
|---|
| 110 | } // if | 
|---|
| 111 | } else { | 
|---|
| 112 | // TODO: type check the formal and actual parameters | 
|---|
| 113 | if ( (*formalIt)->get_name() != "" ) { | 
|---|
| 114 | varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone(); | 
|---|
| 115 | } // if | 
|---|
| 116 | } // if | 
|---|
| 117 | } // for | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | template< typename FormalIterator, typename ActualIterator > | 
|---|
| 121 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) | 
|---|
| 122 | { | 
|---|
| 123 | add( formalBegin, formalEnd, actualBegin ); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | template< typename SynTreeClass > | 
|---|
| 127 | int TypeSubstitution::apply( SynTreeClass *&input ) { | 
|---|
| 128 | assert( input ); | 
|---|
| 129 | subCount = 0; | 
|---|
| 130 | freeOnly = false; | 
|---|
| 131 | input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); | 
|---|
| 132 | assert( input ); | 
|---|
| 133 | ///     std::cout << "substitution result is: "; | 
|---|
| 134 | ///     newType->print( std::cout ); | 
|---|
| 135 | ///     std::cout << std::endl; | 
|---|
| 136 | return subCount; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | template< typename SynTreeClass > | 
|---|
| 140 | int TypeSubstitution::applyFree( SynTreeClass *&input ) { | 
|---|
| 141 | assert( input ); | 
|---|
| 142 | subCount = 0; | 
|---|
| 143 | freeOnly = true; | 
|---|
| 144 | input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); | 
|---|
| 145 | assert( input ); | 
|---|
| 146 | ///     std::cout << "substitution result is: "; | 
|---|
| 147 | ///     newType->print( std::cout ); | 
|---|
| 148 | ///     std::cout << std::endl; | 
|---|
| 149 | return subCount; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | template< typename TypeInstListIterator > | 
|---|
| 153 | void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) { | 
|---|
| 154 | while ( begin != end ) { | 
|---|
| 155 | TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() ); | 
|---|
| 156 | if ( cur != typeEnv.end() ) { | 
|---|
| 157 | result.typeEnv[ cur->first ] = cur->second; | 
|---|
| 158 | typeEnv.erase( cur ); | 
|---|
| 159 | } // if | 
|---|
| 160 | } // while | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | /// Instantiate each member of the context given the actual parameters specified, and store the | 
|---|
| 164 | /// instantiations for use by the indexer | 
|---|
| 165 | template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator > | 
|---|
| 166 | void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) { | 
|---|
| 167 | TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual ); | 
|---|
| 168 | for ( std::list< Declaration* >::iterator i = memberBegin; i != memberEnd; ++i ) { | 
|---|
| 169 | Declaration *newdecl = (*i)->clone(); | 
|---|
| 170 | sub.apply( newdecl ); | 
|---|
| 171 | *out++ = newdecl; | 
|---|
| 172 | } // for | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | #endif // TYPESUBSTITUTION_H | 
|---|
| 176 |  | 
|---|
| 177 | // Local Variables: // | 
|---|
| 178 | // tab-width: 4 // | 
|---|
| 179 | // mode: c++ // | 
|---|
| 180 | // compile-command: "make install" // | 
|---|
| 181 | // End: // | 
|---|