Changeset 58fe85a for src/AST/TypeSubstitution.hpp
- Timestamp:
- Jan 7, 2021, 3:27:00 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 2b4daf2, 64aeca0
- Parents:
- 3c64c668 (diff), eef8dfb (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/AST/TypeSubstitution.hpp
r3c64c668 r58fe85a 44 44 TypeSubstitution &operator=( const TypeSubstitution &other ); 45 45 46 template< typename SynTreeClass > int apply( const SynTreeClass *& input ) const; 47 template< typename SynTreeClass > int applyFree( const SynTreeClass *& input ) const; 46 template< typename SynTreeClass > 47 struct ApplyResult { 48 ast::ptr<SynTreeClass> node; 49 int count; 50 }; 51 52 template< typename SynTreeClass > ApplyResult<SynTreeClass> apply( const SynTreeClass * input ) const; 53 template< typename SynTreeClass > ApplyResult<SynTreeClass> applyFree( const SynTreeClass * input ) const; 48 54 49 55 template< typename node_t, enum Node::ref_type ref_t > 50 56 int apply( ptr_base< node_t, ref_t > & input ) const { 51 57 const node_t * p = input.get(); 52 intret = apply(p);53 input = p;54 return ret ;58 auto ret = apply(p); 59 input = ret.node; 60 return ret.count; 55 61 } 56 62 … … 58 64 int applyFree( ptr_base< node_t, ref_t > & input ) const { 59 65 const node_t * p = input.get(); 60 intret = applyFree(p);61 input = p;62 return ret ;66 auto ret = applyFree(p); 67 input = ret.node; 68 return ret.count; 63 69 } 64 70 65 void add( std::string formalType, const Type *actualType ); 71 void add( const TypeInstType * formalType, const Type *actualType ); 72 void add( const TypeInstType::TypeEnvKey & key, const Type *actualType ); 66 73 void add( const TypeSubstitution &other ); 67 void remove( std::stringformalType );68 const Type *lookup( std::stringformalType ) const;74 void remove( const TypeInstType * formalType ); 75 const Type *lookup( const TypeInstType * formalType ) const; 69 76 bool empty() const; 70 71 void addVar( std::string formalExpr, const Expr *actualExpr );72 77 73 78 template< typename FormalIterator, typename ActualIterator > … … 92 97 void initialize( const TypeSubstitution &src, TypeSubstitution &dest ); 93 98 94 template<typename pass_type>99 template<typename core_t> 95 100 friend class Pass; 96 101 97 typedef std::unordered_map< std::string, ptr<Type> > TypeEnvType; 98 typedef std::unordered_map< std::string, ptr<Expr> > VarEnvType; 102 typedef std::unordered_map< TypeInstType::TypeEnvKey, ptr<Type> > TypeEnvType; 99 103 TypeEnvType typeEnv; 100 VarEnvType varEnv;101 104 102 105 public: … … 107 110 auto end() const -> decltype( typeEnv. end() ) { return typeEnv. end(); } 108 111 109 auto beginVar() -> decltype( varEnv.begin() ) { return varEnv.begin(); }110 auto endVar() -> decltype( varEnv. end() ) { return varEnv. end(); }111 auto beginVar() const -> decltype( varEnv.begin() ) { return varEnv.begin(); }112 auto endVar() const -> decltype( varEnv. end() ) { return varEnv. end(); }113 112 }; 114 113 114 // this is the only place where type parameters outside a function formal may be substituted. 115 115 template< typename FormalIterator, typename ActualIterator > 116 116 void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) { … … 123 123 if ( const TypeExpr *actual = actualIt->template as<TypeExpr>() ) { 124 124 if ( formal->name != "" ) { 125 typeEnv[ formal ->name] = actual->type;125 typeEnv[ formal ] = actual->type; 126 126 } // if 127 127 } else { … … 129 129 } // if 130 130 } else { 131 // TODO: type check the formal and actual parameters 132 if ( (*formalIt)->name != "" ) { 133 varEnv[ (*formalIt)->name ] = *actualIt; 134 } // if 131 135 132 } // if 136 133 } // for 137 134 } 135 136 138 137 139 138 template< typename FormalIterator, typename ActualIterator > … … 142 141 } 143 142 143 144 144 } // namespace ast 145 145 … … 147 147 // PassVisitor are defined before PassVisitor implementation accesses TypeSubstitution internals. 148 148 #include "Pass.hpp" 149 #include "Copy.hpp" 149 150 150 151 namespace ast { 151 152 152 153 // definitition must happen after PassVisitor is included so that WithGuards can be used 153 struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter> { 154 struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter>, public PureVisitor { 155 static size_t traceId; 154 156 155 157 Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {} 156 158 157 #if TIME_TO_CONVERT_PASSES 158 159 Type * postmutate( TypeInstType * aggregateUseType ); 160 Expression * postmutate( NameExpr * nameExpr ); 159 const Type * postvisit( const TypeInstType * aggregateUseType ); 161 160 162 161 /// Records type variable bindings from forall-statements 163 void pre mutate(Type * type );162 void previsit( const FunctionType * type ); 164 163 /// Records type variable bindings from forall-statements and instantiations of generic types 165 template< typename TypeClass > void handleAggregateType( TypeClass* type );164 // void handleAggregateType( const BaseInstType * type ); 166 165 167 void premutate( StructInstType * aggregateUseType ); 168 void premutate( UnionInstType * aggregateUseType ); 169 170 #endif 166 // void previsit( const StructInstType * aggregateUseType ); 167 // void previsit( const UnionInstType * aggregateUseType ); 171 168 172 169 const TypeSubstitution & sub; 173 170 int subCount = 0; 174 171 bool freeOnly; 175 typedef std::unordered_set< std::string> BoundVarsType;172 typedef std::unordered_set< TypeInstType::TypeEnvKey > BoundVarsType; 176 173 BoundVarsType boundVars; 177 174 … … 179 176 180 177 template< typename SynTreeClass > 181 int TypeSubstitution::apply( const SynTreeClass *&input ) const {178 TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::apply( const SynTreeClass * input ) const { 182 179 assert( input ); 183 180 Pass<Substituter> sub( *this, false ); 184 181 input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); 185 /// std::cerr << "substitution result is: "; 186 /// newType->print( std::cerr ); 187 /// std::cerr << std::endl; 188 return sub.pass.subCount; 182 return { input, sub.core.subCount }; 189 183 } 190 184 191 185 template< typename SynTreeClass > 192 int TypeSubstitution::applyFree( const SynTreeClass *&input ) const {186 TypeSubstitution::ApplyResult<SynTreeClass> TypeSubstitution::applyFree( const SynTreeClass * input ) const { 193 187 assert( input ); 194 188 Pass<Substituter> sub( *this, true ); 195 189 input = strict_dynamic_cast< const SynTreeClass * >( input->accept( sub ) ); 196 /// std::cerr << "substitution result is: "; 197 /// newType->print( std::cerr ); 198 /// std::cerr << std::endl; 199 return sub.pass.subCount; 190 return { input, sub.core.subCount }; 200 191 } 201 192
Note:
See TracChangeset
for help on using the changeset viewer.