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