Changeset 90152a4 for src/SynTree/TypeSubstitution.h
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SynTree/TypeSubstitution.h
rf9feab8 r90152a4 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(); … … 57 55 void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ); 58 56 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 59 60 void normalize(); 60 61 … … 64 65 TypeSubstitution *clone() const { return new TypeSubstitution( *this ); } 65 66 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; 87 70 88 71 // TODO: worry about traversing into a forall-qualified function type or type decl with assertions … … 97 80 typedef std::map< std::string, Type* > TypeEnvType; 98 81 typedef std::map< std::string, Expression* > VarEnvType; 99 typedef std::set< std::string > BoundVarsType;100 82 TypeEnvType typeEnv; 101 83 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(); } 105 91 }; 106 92 … … 122 108 } // if 123 109 } 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 " ) ); 125 111 } // if 126 112 } else { … … 134 120 135 121 template< typename FormalIterator, typename ActualIterator > 136 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) 137 { 122 TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { 138 123 add( formalBegin, formalEnd, actualBegin ); 139 124 } 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 131 struct 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 }; 140 151 141 152 template< typename SynTreeClass > 142 153 int TypeSubstitution::apply( SynTreeClass *&input ) { 143 154 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; 152 162 } 153 163 … … 155 165 int TypeSubstitution::applyFree( SynTreeClass *&input ) { 156 166 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; 165 174 } 166 175
Note:
See TracChangeset
for help on using the changeset viewer.