Ignore:
Timestamp:
Jan 9, 2018, 4:03:54 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
b8a4f47
Parents:
07c178f0
Message:

Convert TypeSubstitution?'s apply functionality to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/TypeSubstitution.h

    r07c178f0 re9a715d3  
    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();
     
    6462        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
    6563  private:
    66         virtual Type* mutate(TypeInstType *aggregateUseType);
    67         virtual Expression* mutate(NameExpr *nameExpr);
    6864
    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);
     65        // Mutator that performs the substitution
     66        struct Substituter;
    8767
    8868        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
     
    9777        typedef std::map< std::string, Type* > TypeEnvType;
    9878        typedef std::map< std::string, Expression* > VarEnvType;
    99         typedef std::set< std::string > BoundVarsType;
    10079        TypeEnvType typeEnv;
    10180        VarEnvType varEnv;
    102         BoundVarsType boundVars;
    103         int subCount;
    104         bool freeOnly;
    10581};
    10682
     
    134110
    135111template< typename FormalIterator, typename ActualIterator >
    136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
    137 {
     112TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
    138113        add( formalBegin, formalEnd, actualBegin );
    139114}
     115
     116// include needs to happen after TypeSubstitution is defined so that both TypeSubstitution and
     117// PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals.
     118#include "Common/PassVisitor.h"
     119
     120// definitition must happen after PassVisitor is included so that WithGuards can be used
     121struct TypeSubstitution::Substituter : public WithGuards {
     122                Substituter( TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
     123
     124                Type * postmutate( TypeInstType * aggregateUseType );
     125                Expression * postmutate( NameExpr * nameExpr );
     126
     127                /// Records type variable bindings from forall-statements
     128                void premutate( Type * type );
     129                /// Records type variable bindings from forall-statements and instantiations of generic types
     130                template< typename TypeClass > void handleAggregateType( TypeClass * type );
     131
     132                void premutate( StructInstType * aggregateUseType );
     133                void premutate( UnionInstType * aggregateUseType );
     134
     135                TypeSubstitution & sub;
     136                int subCount = 0;
     137                bool freeOnly;
     138                typedef std::set< std::string > BoundVarsType;
     139                BoundVarsType boundVars;
     140};
    140141
    141142template< typename SynTreeClass >
    142143int TypeSubstitution::apply( SynTreeClass *&input ) {
    143144        assert( input );
    144         subCount = 0;
    145         freeOnly = false;
    146         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
     145        PassVisitor<Substituter> sub( *this, false );
     146        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
    147147        assert( input );
    148 ///     std::cout << "substitution result is: ";
    149 ///     newType->print( std::cout );
    150 ///     std::cout << std::endl;
    151         return subCount;
     148///     std::cerr << "substitution result is: ";
     149///     newType->print( std::cerr );
     150///     std::cerr << std::endl;
     151        return sub.pass.subCount;
    152152}
    153153
     
    155155int TypeSubstitution::applyFree( SynTreeClass *&input ) {
    156156        assert( input );
    157         subCount = 0;
    158         freeOnly = true;
    159         input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
     157        PassVisitor<Substituter> sub( *this, true );
     158        input = dynamic_cast< SynTreeClass * >( input->acceptMutator( sub ) );
    160159        assert( input );
    161 ///     std::cout << "substitution result is: ";
    162 ///     newType->print( std::cout );
    163 ///     std::cout << std::endl;
    164         return subCount;
     160///     std::cerr << "substitution result is: ";
     161///     newType->print( std::cerr );
     162///     std::cerr << std::endl;
     163        return sub.pass.subCount;
    165164}
    166165
Note: See TracChangeset for help on using the changeset viewer.