[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 | |
---|
[e33f321] | 61 | TypeSubstitution * acceptMutator( Mutator & mutator ); |
---|
| 62 | |
---|
[0dd3a2f] | 63 | void print( std::ostream &os, int indent = 0 ) const; |
---|
| 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 | |
---|
[0dd3a2f] | 92 | typedef std::map< std::string, Type* > TypeEnvType; |
---|
| 93 | typedef std::map< std::string, Expression* > VarEnvType; |
---|
| 94 | typedef std::set< std::string > BoundVarsType; |
---|
| 95 | TypeEnvType typeEnv; |
---|
| 96 | VarEnvType varEnv; |
---|
| 97 | BoundVarsType boundVars; |
---|
| 98 | int subCount; |
---|
| 99 | bool freeOnly; |
---|
[51b7345] | 100 | }; |
---|
| 101 | |
---|
| 102 | template< typename FormalIterator, typename ActualIterator > |
---|
[0dd3a2f] | 103 | void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { |
---|
| 104 | // FormalIterator points to a TypeDecl |
---|
| 105 | // ActualIterator points to a Type |
---|
| 106 | FormalIterator formalIt = formalBegin; |
---|
| 107 | ActualIterator actualIt = actualBegin; |
---|
| 108 | for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) { |
---|
| 109 | if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) { |
---|
| 110 | if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) { |
---|
| 111 | if ( formal->get_name() != "" ) { |
---|
| 112 | TypeEnvType::iterator i = typeEnv.find( formal->get_name() ); |
---|
| 113 | if ( i != typeEnv.end() ) { |
---|
| 114 | delete i->second; |
---|
| 115 | } // if |
---|
| 116 | typeEnv[ formal->get_name() ] = actual->get_type()->clone(); |
---|
| 117 | } // if |
---|
| 118 | } else { |
---|
[c93bc28] | 119 | throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal ); |
---|
[0dd3a2f] | 120 | } // if |
---|
| 121 | } else { |
---|
| 122 | // TODO: type check the formal and actual parameters |
---|
| 123 | if ( (*formalIt)->get_name() != "" ) { |
---|
| 124 | varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone(); |
---|
| 125 | } // if |
---|
| 126 | } // if |
---|
| 127 | } // for |
---|
[51b7345] | 128 | } |
---|
| 129 | |
---|
| 130 | template< typename FormalIterator, typename ActualIterator > |
---|
| 131 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) |
---|
| 132 | { |
---|
[0dd3a2f] | 133 | add( formalBegin, formalEnd, actualBegin ); |
---|
[51b7345] | 134 | } |
---|
| 135 | |
---|
| 136 | template< typename SynTreeClass > |
---|
[0dd3a2f] | 137 | int TypeSubstitution::apply( SynTreeClass *&input ) { |
---|
| 138 | assert( input ); |
---|
| 139 | subCount = 0; |
---|
| 140 | freeOnly = false; |
---|
| 141 | input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); |
---|
| 142 | assert( input ); |
---|
[51b7345] | 143 | /// std::cout << "substitution result is: "; |
---|
| 144 | /// newType->print( std::cout ); |
---|
| 145 | /// std::cout << std::endl; |
---|
[0dd3a2f] | 146 | return subCount; |
---|
[51b7345] | 147 | } |
---|
[5382492] | 148 | |
---|
[51b7345] | 149 | template< typename SynTreeClass > |
---|
[0dd3a2f] | 150 | int TypeSubstitution::applyFree( SynTreeClass *&input ) { |
---|
| 151 | assert( input ); |
---|
| 152 | subCount = 0; |
---|
| 153 | freeOnly = true; |
---|
| 154 | input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) ); |
---|
| 155 | assert( input ); |
---|
[51b7345] | 156 | /// std::cout << "substitution result is: "; |
---|
| 157 | /// newType->print( std::cout ); |
---|
| 158 | /// std::cout << std::endl; |
---|
[0dd3a2f] | 159 | return subCount; |
---|
[51b7345] | 160 | } |
---|
[5382492] | 161 | |
---|
[51b7345] | 162 | template< typename TypeInstListIterator > |
---|
[0dd3a2f] | 163 | void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) { |
---|
[d8ba086] | 164 | // xxx - this function doesn't extract varEnv - is this intentional? |
---|
[0dd3a2f] | 165 | while ( begin != end ) { |
---|
| 166 | TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() ); |
---|
| 167 | if ( cur != typeEnv.end() ) { |
---|
| 168 | result.typeEnv[ cur->first ] = cur->second; |
---|
| 169 | typeEnv.erase( cur ); |
---|
| 170 | } // if |
---|
| 171 | } // while |
---|
[51b7345] | 172 | } |
---|
| 173 | |
---|
[82dd287] | 174 | /// Instantiate each member of the context given the actual parameters specified, and store the |
---|
| 175 | /// instantiations for use by the indexer |
---|
[51b7345] | 176 | template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator > |
---|
[0dd3a2f] | 177 | void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) { |
---|
| 178 | TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual ); |
---|
| 179 | for ( std::list< Declaration* >::iterator i = memberBegin; i != memberEnd; ++i ) { |
---|
[79970ed] | 180 | sub.apply( *i ); |
---|
| 181 | *out++ = *i; |
---|
[0dd3a2f] | 182 | } // for |
---|
[51b7345] | 183 | } |
---|
| 184 | |
---|
[5382492] | 185 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ); |
---|
| 186 | |
---|
[0dd3a2f] | 187 | // Local Variables: // |
---|
| 188 | // tab-width: 4 // |
---|
| 189 | // mode: c++ // |
---|
| 190 | // compile-command: "make install" // |
---|
| 191 | // End: // |
---|