Changeset e9a715d3 for src/SynTree/TypeSubstitution.h
- Timestamp:
- Jan 9, 2018, 4:03:54 PM (7 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/TypeSubstitution.h
r07c178f0 re9a715d3 27 27 #include "SynTree/Declaration.h" // for TypeDecl, Declaration (ptr only) 28 28 #include "SynTree/Expression.h" // for Expression (ptr only), NameExpr (p... 29 #include "SynTree/Mutator.h" // for Mutator30 29 #include "SynTree/Type.h" // for Type, ArrayType (ptr only), BasicT... 31 30 32 class TypeSubstitution : public Mutator { 33 typedef Mutator Parent; 31 class TypeSubstitution { 34 32 public: 35 33 TypeSubstitution(); … … 64 62 TypeSubstitution *clone() const { return new TypeSubstitution( *this ); } 65 63 private: 66 virtual Type* mutate(TypeInstType *aggregateUseType);67 virtual Expression* mutate(NameExpr *nameExpr);68 64 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; 87 67 88 68 // TODO: worry about traversing into a forall-qualified function type or type decl with assertions … … 97 77 typedef std::map< std::string, Type* > TypeEnvType; 98 78 typedef std::map< std::string, Expression* > VarEnvType; 99 typedef std::set< std::string > BoundVarsType;100 79 TypeEnvType typeEnv; 101 80 VarEnvType varEnv; 102 BoundVarsType boundVars;103 int subCount;104 bool freeOnly;105 81 }; 106 82 … … 134 110 135 111 template< typename FormalIterator, typename ActualIterator > 136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) 137 { 112 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { 138 113 add( formalBegin, formalEnd, actualBegin ); 139 114 } 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 121 struct 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 }; 140 141 141 142 template< typename SynTreeClass > 142 143 int TypeSubstitution::apply( SynTreeClass *&input ) { 143 144 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 ) ); 147 147 assert( input ); 148 /// std::c out<< "substitution result is: ";149 /// newType->print( std::c out);150 /// std::c out<< std::endl;151 return sub Count;148 /// std::cerr << "substitution result is: "; 149 /// newType->print( std::cerr ); 150 /// std::cerr << std::endl; 151 return sub.pass.subCount; 152 152 } 153 153 … … 155 155 int TypeSubstitution::applyFree( SynTreeClass *&input ) { 156 156 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 ) ); 160 159 assert( input ); 161 /// std::c out<< "substitution result is: ";162 /// newType->print( std::c out);163 /// std::c out<< std::endl;164 return sub Count;160 /// std::cerr << "substitution result is: "; 161 /// newType->print( std::cerr ); 162 /// std::cerr << std::endl; 163 return sub.pass.subCount; 165 164 } 166 165
Note: See TracChangeset
for help on using the changeset viewer.