| 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 : Tue Apr 30 22:52:47 2019
|
|---|
| 13 | // Update Count : 9
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert> // for assert
|
|---|
| 19 | #include <iosfwd> // for ostream
|
|---|
| 20 | #include <list> // for list<>::iterator, _List_iterator
|
|---|
| 21 | #include <unordered_map>
|
|---|
| 22 | #include <unordered_set>
|
|---|
| 23 | #include <string> // for string, operator!=
|
|---|
| 24 | #include <utility> // for pair
|
|---|
| 25 |
|
|---|
| 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/Type.h" // for Type, ArrayType (ptr only), BasicT...
|
|---|
| 30 |
|
|---|
| 31 | class TypeSubstitution {
|
|---|
| 32 | public:
|
|---|
| 33 | TypeSubstitution();
|
|---|
| 34 | template< typename FormalIterator, typename ActualIterator >
|
|---|
| 35 | TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
|
|---|
| 36 | TypeSubstitution( const TypeSubstitution &other );
|
|---|
| 37 | virtual ~TypeSubstitution();
|
|---|
| 38 |
|
|---|
| 39 | TypeSubstitution &operator=( const TypeSubstitution &other );
|
|---|
| 40 |
|
|---|
| 41 | template< typename SynTreeClass > int apply( SynTreeClass *&input ) const;
|
|---|
| 42 | template< typename SynTreeClass > int applyFree( SynTreeClass *&input ) const;
|
|---|
| 43 |
|
|---|
| 44 | void add( std::string formalType, Type *actualType );
|
|---|
| 45 | void add( const TypeSubstitution &other );
|
|---|
| 46 | void remove( std::string formalType );
|
|---|
| 47 | Type *lookup( std::string formalType ) const;
|
|---|
| 48 | bool empty() const;
|
|---|
| 49 |
|
|---|
| 50 | void addVar( std::string formalExpr, Expression *actualExpr );
|
|---|
| 51 |
|
|---|
| 52 | template< typename FormalIterator, typename ActualIterator >
|
|---|
| 53 | void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
|
|---|
| 54 |
|
|---|
| 55 | /// this function is unused...
|
|---|
| 56 | template< typename TypeInstListIterator >
|
|---|
| 57 | void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
|
|---|
| 58 |
|
|---|
| 59 | /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr
|
|---|
| 60 | static TypeSubstitution * newFromExpr( Expression * expr, const TypeSubstitution * env );
|
|---|
| 61 |
|
|---|
| 62 | void normalize();
|
|---|
| 63 |
|
|---|
| 64 | TypeSubstitution * acceptMutator( Mutator & m ) { return m.mutate( this ); }
|
|---|
| 65 |
|
|---|
| 66 | void print( std::ostream &os, Indenter indent = {} ) const;
|
|---|
| 67 | TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
|
|---|
| 68 | private:
|
|---|
| 69 |
|
|---|
| 70 | // Mutator that performs the substitution
|
|---|
| 71 | struct Substituter;
|
|---|
| 72 |
|
|---|
| 73 | // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
|
|---|
| 74 |
|
|---|
| 75 | void initialize( const TypeSubstitution &src, TypeSubstitution &dest );
|
|---|
| 76 |
|
|---|
| 77 | friend class Mutator;
|
|---|
| 78 |
|
|---|
| 79 | template<typename pass_type>
|
|---|
| 80 | friend class PassVisitor;
|
|---|
| 81 |
|
|---|
| 82 | typedef std::unordered_map< std::string, Type * > TypeEnvType;
|
|---|
| 83 | typedef std::unordered_map< std::string, Expression * > VarEnvType;
|
|---|
| 84 | TypeEnvType typeEnv;
|
|---|
| 85 | VarEnvType varEnv;
|
|---|
| 86 |
|
|---|
| 87 | public:
|
|---|
| 88 | // has to come after declaration of typeEnv
|
|---|
| 89 | auto begin() -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
|
|---|
| 90 | auto end() -> decltype( typeEnv. end() ) { return typeEnv. end(); }
|
|---|
| 91 | auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
|
|---|
| 92 | auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); }
|
|---|
| 93 |
|
|---|
| 94 | auto beginVar() -> decltype( varEnv.begin() ) { return varEnv.begin(); }
|
|---|
| 95 | auto endVar() -> decltype( varEnv. end() ) { return varEnv. end(); }
|
|---|
| 96 | auto beginVar() const -> decltype( varEnv.begin() ) { return varEnv.begin(); }
|
|---|
| 97 | auto endVar() const -> decltype( varEnv. end() ) { return varEnv. end(); }
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | template< typename FormalIterator, typename ActualIterator >
|
|---|
| 101 | void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
|
|---|
| 102 | // FormalIterator points to a TypeDecl
|
|---|
| 103 | // ActualIterator points to a Type
|
|---|
| 104 | FormalIterator formalIt = formalBegin;
|
|---|
| 105 | ActualIterator actualIt = actualBegin;
|
|---|
| 106 | for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) {
|
|---|
| 107 | if ( TypeDecl *formal = dynamic_cast< TypeDecl * >( *formalIt ) ) {
|
|---|
| 108 | if ( TypeExpr *actual = dynamic_cast< TypeExpr * >( *actualIt ) ) {
|
|---|
| 109 | if ( formal->get_name() != "" ) {
|
|---|
| 110 | TypeEnvType::iterator i = typeEnv.find( formal->get_name() );
|
|---|
| 111 | if ( i != typeEnv.end() ) {
|
|---|
| 112 | delete i->second;
|
|---|
| 113 | } // if
|
|---|
| 114 | typeEnv[ formal->get_name() ] = actual->get_type()->clone();
|
|---|
| 115 | } // if
|
|---|
| 116 | } else {
|
|---|
| 117 | SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) );
|
|---|
| 118 | } // if
|
|---|
| 119 | } else {
|
|---|
| 120 | // TODO: type check the formal and actual parameters
|
|---|
| 121 | if ( (*formalIt)->get_name() != "" ) {
|
|---|
| 122 | varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone();
|
|---|
| 123 | } // if
|
|---|
| 124 | } // if
|
|---|
| 125 | } // for
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | template< typename FormalIterator, typename ActualIterator >
|
|---|
| 129 | TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
|
|---|
| 130 | add( formalBegin, formalEnd, actualBegin );
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
|
|---|
| 134 | // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
|
|---|
| 135 | #include "Common/PassVisitor.h"
|
|---|
| 136 |
|
|---|
| 137 | // definitition must happen after PassVisitor is included so that WithGuards can be used
|
|---|
| 138 | struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> {
|
|---|
| 139 | Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
|
|---|
| 140 |
|
|---|
| 141 | Type * postmutate( TypeInstType * aggregateUseType );
|
|---|
| 142 | Expression * postmutate( NameExpr * nameExpr );
|
|---|
| 143 |
|
|---|
| 144 | /// Records type variable bindings from forall-statements
|
|---|
| 145 | void premutate( Type * type );
|
|---|
| 146 | /// Records type variable bindings from forall-statements and instantiations of generic types
|
|---|
| 147 | template< typename TypeClass > void handleAggregateType( TypeClass * type );
|
|---|
| 148 |
|
|---|
| 149 | void premutate( StructInstType * aggregateUseType );
|
|---|
| 150 | void premutate( UnionInstType * aggregateUseType );
|
|---|
| 151 |
|
|---|
| 152 | const TypeSubstitution & sub;
|
|---|
| 153 | int subCount = 0;
|
|---|
| 154 | bool freeOnly;
|
|---|
| 155 | typedef std::unordered_set< std::string > BoundVarsType;
|
|---|
| 156 | BoundVarsType boundVars;
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | template< typename SynTreeClass >
|
|---|
| 160 | int TypeSubstitution::apply( SynTreeClass *&input ) const {
|
|---|
| 161 | assert( input );
|
|---|
| 162 | PassVisitor<Substituter> sub( *this, false );
|
|---|
| 163 | input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
|
|---|
| 164 | assert( input );
|
|---|
| 165 | /// std::cerr << "substitution result is: ";
|
|---|
| 166 | /// newType->print( std::cerr );
|
|---|
| 167 | /// std::cerr << std::endl;
|
|---|
| 168 | return sub.pass.subCount;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | template< typename SynTreeClass >
|
|---|
| 172 | int TypeSubstitution::applyFree( SynTreeClass *&input ) const {
|
|---|
| 173 | assert( input );
|
|---|
| 174 | PassVisitor<Substituter> sub( *this, true );
|
|---|
| 175 | input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
|
|---|
| 176 | assert( input );
|
|---|
| 177 | /// std::cerr << "substitution result is: ";
|
|---|
| 178 | /// newType->print( std::cerr );
|
|---|
| 179 | /// std::cerr << std::endl;
|
|---|
| 180 | return sub.pass.subCount;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | template< typename TypeInstListIterator >
|
|---|
| 184 | void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) {
|
|---|
| 185 | // xxx - this function doesn't extract varEnv - is this intentional?
|
|---|
| 186 | while ( begin != end ) {
|
|---|
| 187 | TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() );
|
|---|
| 188 | if ( cur != typeEnv.end() ) {
|
|---|
| 189 | result.typeEnv[ cur->first ] = cur->second;
|
|---|
| 190 | typeEnv.erase( cur );
|
|---|
| 191 | } // if
|
|---|
| 192 | } // while
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /// Instantiate each member of the context given the actual parameters specified, and store the
|
|---|
| 196 | /// instantiations for use by the indexer
|
|---|
| 197 | template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
|
|---|
| 198 | void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) {
|
|---|
| 199 | TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
|
|---|
| 200 | for ( auto i = memberBegin; i != memberEnd; ++i ) {
|
|---|
| 201 | sub.apply( *i );
|
|---|
| 202 | *out++ = *i;
|
|---|
| 203 | } // for
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub );
|
|---|
| 207 |
|
|---|
| 208 | // Local Variables: //
|
|---|
| 209 | // tab-width: 4 //
|
|---|
| 210 | // mode: c++ //
|
|---|
| 211 | // compile-command: "make install" //
|
|---|
| 212 | // End: //
|
|---|