Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/TypeSubstitution.h

    rf9feab8 r90152a4  
    2727#include "SynTree/Declaration.h"   // for TypeDecl, Declaration (ptr only)
    2828#include "SynTree/Expression.h"    // for Expression (ptr only), NameExpr (p...
    29 #include "SynTree/Mutator.h"       // for Mutator
    3029#include "SynTree/Type.h"          // for Type, ArrayType (ptr only), BasicT...
    3130
    32 class TypeSubstitution : public Mutator {
    33         typedef Mutator Parent;
     31class TypeSubstitution {
    3432  public:
    3533        TypeSubstitution();
     
    5755        void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
    5856
     57        /// create a new TypeSubstitution using bindings from env containing all of the type variables in expr
     58        static TypeSubstitution * newFromExpr( Expression * expr, TypeSubstitution * env );
     59
    5960        void normalize();
    6061
     
    6465        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
    6566  private:
    66         virtual Type* mutate(TypeInstType *aggregateUseType);
    67         virtual Expression* mutate(NameExpr *nameExpr);
    68 
    69         /// Records type variable bindings from forall-statements
    70         template< typename TypeClass > Type *handleType( TypeClass *type );
    71         /// Records type variable bindings from forall-statements and instantiations of generic types
    72         template< typename TypeClass > Type *handleAggregateType( TypeClass *type );
    73 
    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);
    82         virtual Type* mutate(TraitInstType *aggregateUseType);
    83         virtual Type* mutate(TupleType *tupleType);
    84         virtual Type* mutate(VarArgsType *varArgsType);
    85         virtual Type* mutate(ZeroType *zeroType);
    86         virtual Type* mutate(OneType *oneType);
     67
     68        // Mutator that performs the substitution
     69        struct Substituter;
    8770
    8871        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
     
    9780        typedef std::map< std::string, Type* > TypeEnvType;
    9881        typedef std::map< std::string, Expression* > VarEnvType;
    99         typedef std::set< std::string > BoundVarsType;
    10082        TypeEnvType typeEnv;
    10183        VarEnvType varEnv;
    102         BoundVarsType boundVars;
    103         int subCount;
    104         bool freeOnly;
     84
     85  public:
     86        // has to come after declaration of typeEnv
     87        auto begin()       -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
     88        auto   end()       -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
     89        auto begin() const -> decltype( typeEnv.begin() ) { return typeEnv.begin(); }
     90        auto   end() const -> decltype( typeEnv.  end() ) { return typeEnv.  end(); }
    10591};
    10692
     
    122108                                } // if
    123109                        } else {
    124                                 throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal );
     110                                SemanticError( formal, toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ) );
    125111                        } // if
    126112                } else {
     
    134120
    135121template< typename FormalIterator, typename ActualIterator >
    136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
    137 {
     122TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
    138123        add( formalBegin, formalEnd, actualBegin );
    139124}
     125
     126// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
     127// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
     128#include "Common/PassVisitor.h"
     129
     130// definitition must happen after PassVisitor is included so that WithGuards can be used
     131struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> {
     132                Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
     133
     134                Type * postmutate( TypeInstType * aggregateUseType );
     135                Expression * postmutate( NameExpr * nameExpr );
     136
     137                /// Records type variable bindings from forall-statements
     138                void premutate( Type * type );
     139                /// Records type variable bindings from forall-statements and instantiations of generic types
     140                template< typename TypeClass > void handleAggregateType( TypeClass * type );
     141
     142                void premutate( StructInstType * aggregateUseType );
     143                void premutate( UnionInstType * aggregateUseType );
     144
     145                TypeSubstitution & sub;
     146                int subCount = 0;
     147                bool freeOnly;
     148                typedef std::set< std::string > BoundVarsType;
     149                BoundVarsType boundVars;
     150};
    140151
    141152template< typename SynTreeClass >
    142153int TypeSubstitution::apply( SynTreeClass *&input ) {
    143154        assert( input );
    144         subCount = 0;
    145         freeOnly = false;
    146         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    147         assert( input );
    148 ///     std::cout << "substitution result is: ";
    149 ///     newType->print( std::cout );
    150 ///     std::cout << std::endl;
    151         return subCount;
     155        PassVisitor<Substituter> sub( *this, false );
     156        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
     157        assert( input );
     158///     std::cerr << "substitution result is: ";
     159///     newType->print( std::cerr );
     160///     std::cerr << std::endl;
     161        return sub.pass.subCount;
    152162}
    153163
     
    155165int TypeSubstitution::applyFree( SynTreeClass *&input ) {
    156166        assert( input );
    157         subCount = 0;
    158         freeOnly = true;
    159         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    160         assert( input );
    161 ///     std::cout << "substitution result is: ";
    162 ///     newType->print( std::cout );
    163 ///     std::cout << std::endl;
    164         return subCount;
     167        PassVisitor<Substituter> sub( *this, true );
     168        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
     169        assert( input );
     170///     std::cerr << "substitution result is: ";
     171///     newType->print( std::cerr );
     172///     std::cerr << std::endl;
     173        return sub.pass.subCount;
    165174}
    166175
Note: See TracChangeset for help on using the changeset viewer.