1 | #pragma once |
---|
2 | |
---|
3 | #include <stack> |
---|
4 | |
---|
5 | #include "SynTree/Mutator.h" |
---|
6 | #include "SynTree/Visitor.h" |
---|
7 | |
---|
8 | #include "SynTree/Initializer.h" |
---|
9 | #include "SynTree/Statement.h" |
---|
10 | #include "SynTree/Type.h" |
---|
11 | #include "SynTree/Declaration.h" |
---|
12 | #include "SynTree/Expression.h" |
---|
13 | #include "SynTree/Constant.h" |
---|
14 | #include "SynTree/TypeSubstitution.h" |
---|
15 | |
---|
16 | #include "PassVisitor.proto.h" |
---|
17 | |
---|
18 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
19 | // Templated visitor type |
---|
20 | // To use declare a PassVisitor< YOUR VISITOR TYPE > |
---|
21 | // The visitor type should specify the previsit/postvisit/premutate/postmutate for types that are desired. |
---|
22 | // Note: previsit/postvisit/premutate/postmutate must be **public** members |
---|
23 | // |
---|
24 | // Several additional features are available through inheritance |
---|
25 | // | WithTypeSubstitution - provides polymorphic TypeSubstitution * env for the current expression |
---|
26 | // | WithStmtsToAdd - provides the ability to insert statements before or after the current statement by adding new statements into |
---|
27 | // stmtsToAddBefore or stmtsToAddAfter respectively. |
---|
28 | // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children |
---|
29 | // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable |
---|
30 | // will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates. |
---|
31 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
32 | template< typename pass_type > |
---|
33 | class PassVisitor final : public Visitor, public Mutator { |
---|
34 | public: |
---|
35 | |
---|
36 | template< typename... Args > |
---|
37 | PassVisitor(Args &&... args) |
---|
38 | : pass( std::forward<Args>( args )... ) |
---|
39 | { |
---|
40 | typedef PassVisitor<pass_type> this_t; |
---|
41 | this_t * const * visitor = visitor_impl(pass, 0); |
---|
42 | if(visitor) { |
---|
43 | *const_cast<this_t **>( visitor ) = this; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | virtual ~PassVisitor() = default; |
---|
48 | |
---|
49 | pass_type pass; |
---|
50 | |
---|
51 | virtual void visit( ObjectDecl *objectDecl ) override final; |
---|
52 | virtual void visit( FunctionDecl *functionDecl ) override final; |
---|
53 | virtual void visit( StructDecl *aggregateDecl ) override final; |
---|
54 | virtual void visit( UnionDecl *aggregateDecl ) override final; |
---|
55 | virtual void visit( EnumDecl *aggregateDecl ) override final; |
---|
56 | virtual void visit( TraitDecl *aggregateDecl ) override final; |
---|
57 | virtual void visit( TypeDecl *typeDecl ) override final; |
---|
58 | virtual void visit( TypedefDecl *typeDecl ) override final; |
---|
59 | virtual void visit( AsmDecl *asmDecl ) override final; |
---|
60 | |
---|
61 | virtual void visit( CompoundStmt *compoundStmt ) override final; |
---|
62 | virtual void visit( ExprStmt *exprStmt ) override final; |
---|
63 | virtual void visit( AsmStmt *asmStmt ) override final; |
---|
64 | virtual void visit( IfStmt *ifStmt ) override final; |
---|
65 | virtual void visit( WhileStmt *whileStmt ) override final; |
---|
66 | virtual void visit( ForStmt *forStmt ) override final; |
---|
67 | virtual void visit( SwitchStmt *switchStmt ) override final; |
---|
68 | virtual void visit( CaseStmt *caseStmt ) override final; |
---|
69 | virtual void visit( BranchStmt *branchStmt ) override final; |
---|
70 | virtual void visit( ReturnStmt *returnStmt ) override final; |
---|
71 | virtual void visit( ThrowStmt *throwStmt ) override final; |
---|
72 | virtual void visit( TryStmt *tryStmt ) override final; |
---|
73 | virtual void visit( CatchStmt *catchStmt ) override final; |
---|
74 | virtual void visit( FinallyStmt *finallyStmt ) override final; |
---|
75 | virtual void visit( NullStmt *nullStmt ) override final; |
---|
76 | virtual void visit( DeclStmt *declStmt ) override final; |
---|
77 | virtual void visit( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
78 | |
---|
79 | virtual void visit( ApplicationExpr *applicationExpr ) override final; |
---|
80 | virtual void visit( UntypedExpr *untypedExpr ) override final; |
---|
81 | virtual void visit( NameExpr *nameExpr ) override final; |
---|
82 | virtual void visit( CastExpr *castExpr ) override final; |
---|
83 | virtual void visit( AddressExpr *addressExpr ) override final; |
---|
84 | virtual void visit( LabelAddressExpr *labAddressExpr ) override final; |
---|
85 | virtual void visit( UntypedMemberExpr *memberExpr ) override final; |
---|
86 | virtual void visit( MemberExpr *memberExpr ) override final; |
---|
87 | virtual void visit( VariableExpr *variableExpr ) override final; |
---|
88 | virtual void visit( ConstantExpr *constantExpr ) override final; |
---|
89 | virtual void visit( SizeofExpr *sizeofExpr ) override final; |
---|
90 | virtual void visit( AlignofExpr *alignofExpr ) override final; |
---|
91 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
92 | virtual void visit( OffsetofExpr *offsetofExpr ) override final; |
---|
93 | virtual void visit( OffsetPackExpr *offsetPackExpr ) override final; |
---|
94 | virtual void visit( AttrExpr *attrExpr ) override final; |
---|
95 | virtual void visit( LogicalExpr *logicalExpr ) override final; |
---|
96 | virtual void visit( ConditionalExpr *conditionalExpr ) override final; |
---|
97 | virtual void visit( CommaExpr *commaExpr ) override final; |
---|
98 | virtual void visit( TypeExpr *typeExpr ) override final; |
---|
99 | virtual void visit( AsmExpr *asmExpr ) override final; |
---|
100 | virtual void visit( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
101 | virtual void visit( ConstructorExpr * ctorExpr ) override final; |
---|
102 | virtual void visit( CompoundLiteralExpr *compLitExpr ) override final; |
---|
103 | virtual void visit( RangeExpr *rangeExpr ) override final; |
---|
104 | virtual void visit( UntypedTupleExpr *tupleExpr ) override final; |
---|
105 | virtual void visit( TupleExpr *tupleExpr ) override final; |
---|
106 | virtual void visit( TupleIndexExpr *tupleExpr ) override final; |
---|
107 | virtual void visit( TupleAssignExpr *assignExpr ) override final; |
---|
108 | virtual void visit( StmtExpr * stmtExpr ) override final; |
---|
109 | virtual void visit( UniqueExpr * uniqueExpr ) override final; |
---|
110 | |
---|
111 | virtual void visit( VoidType *basicType ) override final; |
---|
112 | virtual void visit( BasicType *basicType ) override final; |
---|
113 | virtual void visit( PointerType *pointerType ) override final; |
---|
114 | virtual void visit( ArrayType *arrayType ) override final; |
---|
115 | virtual void visit( FunctionType *functionType ) override final; |
---|
116 | virtual void visit( StructInstType *aggregateUseType ) override final; |
---|
117 | virtual void visit( UnionInstType *aggregateUseType ) override final; |
---|
118 | virtual void visit( EnumInstType *aggregateUseType ) override final; |
---|
119 | virtual void visit( TraitInstType *aggregateUseType ) override final; |
---|
120 | virtual void visit( TypeInstType *aggregateUseType ) override final; |
---|
121 | virtual void visit( TupleType *tupleType ) override final; |
---|
122 | virtual void visit( TypeofType *typeofType ) override final; |
---|
123 | virtual void visit( AttrType *attrType ) override final; |
---|
124 | virtual void visit( VarArgsType *varArgsType ) override final; |
---|
125 | virtual void visit( ZeroType *zeroType ) override final; |
---|
126 | virtual void visit( OneType *oneType ) override final; |
---|
127 | |
---|
128 | virtual void visit( SingleInit *singleInit ) override final; |
---|
129 | virtual void visit( ListInit *listInit ) override final; |
---|
130 | virtual void visit( ConstructorInit *ctorInit ) override final; |
---|
131 | |
---|
132 | virtual void visit( Subrange *subrange ) override final; |
---|
133 | |
---|
134 | virtual void visit( Constant *constant ) override final; |
---|
135 | |
---|
136 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final; |
---|
137 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final; |
---|
138 | virtual Declaration* mutate( StructDecl *aggregateDecl ) override final; |
---|
139 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final; |
---|
140 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final; |
---|
141 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final; |
---|
142 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final; |
---|
143 | virtual Declaration* mutate( TypedefDecl *typeDecl ) override final; |
---|
144 | virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final; |
---|
145 | |
---|
146 | virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final; |
---|
147 | virtual Statement* mutate( ExprStmt *exprStmt ) override final; |
---|
148 | virtual Statement* mutate( AsmStmt *asmStmt ) override final; |
---|
149 | virtual Statement* mutate( IfStmt *ifStmt ) override final; |
---|
150 | virtual Statement* mutate( WhileStmt *whileStmt ) override final; |
---|
151 | virtual Statement* mutate( ForStmt *forStmt ) override final; |
---|
152 | virtual Statement* mutate( SwitchStmt *switchStmt ) override final; |
---|
153 | virtual Statement* mutate( CaseStmt *caseStmt ) override final; |
---|
154 | virtual Statement* mutate( BranchStmt *branchStmt ) override final; |
---|
155 | virtual Statement* mutate( ReturnStmt *returnStmt ) override final; |
---|
156 | virtual Statement* mutate( ThrowStmt *throwStmt ) override final; |
---|
157 | virtual Statement* mutate( TryStmt *returnStmt ) override final; |
---|
158 | virtual Statement* mutate( CatchStmt *catchStmt ) override final; |
---|
159 | virtual Statement* mutate( FinallyStmt *catchStmt ) override final; |
---|
160 | virtual NullStmt* mutate( NullStmt *nullStmt ) override final; |
---|
161 | virtual Statement* mutate( DeclStmt *declStmt ) override final; |
---|
162 | virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
163 | |
---|
164 | virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final; |
---|
165 | virtual Expression* mutate( UntypedExpr *untypedExpr ) override final; |
---|
166 | virtual Expression* mutate( NameExpr *nameExpr ) override final; |
---|
167 | virtual Expression* mutate( AddressExpr *castExpr ) override final; |
---|
168 | virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final; |
---|
169 | virtual Expression* mutate( CastExpr *castExpr ) override final; |
---|
170 | virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final; |
---|
171 | virtual Expression* mutate( MemberExpr *memberExpr ) override final; |
---|
172 | virtual Expression* mutate( VariableExpr *variableExpr ) override final; |
---|
173 | virtual Expression* mutate( ConstantExpr *constantExpr ) override final; |
---|
174 | virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final; |
---|
175 | virtual Expression* mutate( AlignofExpr *alignofExpr ) override final; |
---|
176 | virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
177 | virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final; |
---|
178 | virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final; |
---|
179 | virtual Expression* mutate( AttrExpr *attrExpr ) override final; |
---|
180 | virtual Expression* mutate( LogicalExpr *logicalExpr ) override final; |
---|
181 | virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final; |
---|
182 | virtual Expression* mutate( CommaExpr *commaExpr ) override final; |
---|
183 | virtual Expression* mutate( TypeExpr *typeExpr ) override final; |
---|
184 | virtual Expression* mutate( AsmExpr *asmExpr ) override final; |
---|
185 | virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
186 | virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final; |
---|
187 | virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final; |
---|
188 | virtual Expression* mutate( RangeExpr *rangeExpr ) override final; |
---|
189 | virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final; |
---|
190 | virtual Expression* mutate( TupleExpr *tupleExpr ) override final; |
---|
191 | virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final; |
---|
192 | virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final; |
---|
193 | virtual Expression* mutate( StmtExpr * stmtExpr ) override final; |
---|
194 | virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final; |
---|
195 | |
---|
196 | virtual Type* mutate( VoidType *basicType ) override final; |
---|
197 | virtual Type* mutate( BasicType *basicType ) override final; |
---|
198 | virtual Type* mutate( PointerType *pointerType ) override final; |
---|
199 | virtual Type* mutate( ArrayType *arrayType ) override final; |
---|
200 | virtual Type* mutate( FunctionType *functionType ) override final; |
---|
201 | virtual Type* mutate( StructInstType *aggregateUseType ) override final; |
---|
202 | virtual Type* mutate( UnionInstType *aggregateUseType ) override final; |
---|
203 | virtual Type* mutate( EnumInstType *aggregateUseType ) override final; |
---|
204 | virtual Type* mutate( TraitInstType *aggregateUseType ) override final; |
---|
205 | virtual Type* mutate( TypeInstType *aggregateUseType ) override final; |
---|
206 | virtual Type* mutate( TupleType *tupleType ) override final; |
---|
207 | virtual Type* mutate( TypeofType *typeofType ) override final; |
---|
208 | virtual Type* mutate( AttrType *attrType ) override final; |
---|
209 | virtual Type* mutate( VarArgsType *varArgsType ) override final; |
---|
210 | virtual Type* mutate( ZeroType *zeroType ) override final; |
---|
211 | virtual Type* mutate( OneType *oneType ) override final; |
---|
212 | |
---|
213 | virtual Initializer* mutate( SingleInit *singleInit ) override final; |
---|
214 | virtual Initializer* mutate( ListInit *listInit ) override final; |
---|
215 | virtual Initializer* mutate( ConstructorInit *ctorInit ) override final; |
---|
216 | |
---|
217 | virtual Subrange *mutate( Subrange *subrange ) override final; |
---|
218 | |
---|
219 | virtual Constant *mutate( Constant *constant ) override final; |
---|
220 | |
---|
221 | private: |
---|
222 | template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
223 | template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
224 | |
---|
225 | template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
226 | template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
227 | |
---|
228 | template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); } |
---|
229 | template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); } |
---|
230 | |
---|
231 | void call_beginScope() { begin_scope_impl( pass, 0 ); } |
---|
232 | void call_endScope () { end_scope_impl ( pass, 0 ); } |
---|
233 | |
---|
234 | void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } |
---|
235 | |
---|
236 | template< typename func_t > |
---|
237 | void handleStatementList( std::list< Statement * > & statements, func_t func ); |
---|
238 | void visitStatementList ( std::list< Statement* > &statements ); |
---|
239 | void mutateStatementList( std::list< Statement* > &statements ); |
---|
240 | |
---|
241 | template< typename func_t > |
---|
242 | Statement * handleStatement( Statement * stmt, func_t func ); |
---|
243 | Statement * visitStatement ( Statement * stmt ); |
---|
244 | Statement * mutateStatement( Statement * stmt ); |
---|
245 | |
---|
246 | template< typename func_t > |
---|
247 | Expression * handleExpression( Expression * expr, func_t func ); |
---|
248 | Expression * visitExpression ( Expression * expr ); |
---|
249 | Expression * mutateExpression( Expression * expr ); |
---|
250 | |
---|
251 | |
---|
252 | TypeSubstitution ** get_env_ptr () { return env_impl ( pass, 0); } |
---|
253 | std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } |
---|
254 | std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } |
---|
255 | std::list< Declaration* > * get_beforeDecls() { return declsToAddBefore_impl( pass, 0); } |
---|
256 | std::list< Declaration* > * get_afterDecls () { return declsToAddAfter_impl ( pass, 0); } |
---|
257 | |
---|
258 | void set_visit_children( bool& ref ) { bool_ref * ptr = visit_children_impl(pass, 0); if(ptr) ptr->set( ref ); } |
---|
259 | }; |
---|
260 | |
---|
261 | template<typename pass_type, typename T> |
---|
262 | void GuardValue( pass_type * pass, T& val ) { |
---|
263 | pass->at_cleanup( [ val ]( void * newVal ) { |
---|
264 | * static_cast< T * >( newVal ) = val; |
---|
265 | }, static_cast< void * >( & val ) ); |
---|
266 | } |
---|
267 | |
---|
268 | class WithTypeSubstitution { |
---|
269 | protected: |
---|
270 | WithTypeSubstitution() = default; |
---|
271 | ~WithTypeSubstitution() = default; |
---|
272 | |
---|
273 | public: |
---|
274 | TypeSubstitution * env = nullptr; |
---|
275 | }; |
---|
276 | |
---|
277 | class WithStmtsToAdd { |
---|
278 | protected: |
---|
279 | WithStmtsToAdd() = default; |
---|
280 | ~WithStmtsToAdd() = default; |
---|
281 | |
---|
282 | public: |
---|
283 | std::list< Statement* > stmtsToAddBefore; |
---|
284 | std::list< Statement* > stmtsToAddAfter; |
---|
285 | }; |
---|
286 | |
---|
287 | class WithDeclsToAdd { |
---|
288 | protected: |
---|
289 | WithDeclsToAdd() = default; |
---|
290 | ~WithDeclsToAdd() = default; |
---|
291 | |
---|
292 | public: |
---|
293 | std::list< Declaration* > declsToAddBefore; |
---|
294 | std::list< Declaration* > declsToAddAfter; |
---|
295 | }; |
---|
296 | |
---|
297 | class WithShortCircuiting { |
---|
298 | protected: |
---|
299 | WithShortCircuiting() = default; |
---|
300 | ~WithShortCircuiting() = default; |
---|
301 | |
---|
302 | public: |
---|
303 | bool_ref visit_children; |
---|
304 | }; |
---|
305 | |
---|
306 | class WithGuards { |
---|
307 | protected: |
---|
308 | WithGuards() = default; |
---|
309 | ~WithGuards() = default; |
---|
310 | |
---|
311 | public: |
---|
312 | at_cleanup_t at_cleanup; |
---|
313 | |
---|
314 | template< typename T > |
---|
315 | void GuardValue( T& val ) { |
---|
316 | at_cleanup( [ val ]( void * newVal ) { |
---|
317 | * static_cast< T * >( newVal ) = val; |
---|
318 | }, static_cast< void * >( & val ) ); |
---|
319 | } |
---|
320 | |
---|
321 | template< typename T > |
---|
322 | void GuardScope( T& val ) { |
---|
323 | val.beginScope(); |
---|
324 | at_cleanup( []( void * val ) { |
---|
325 | static_cast< T * >( val )->endScope(); |
---|
326 | }, static_cast< void * >( & val ) ); |
---|
327 | } |
---|
328 | |
---|
329 | template< typename Func > |
---|
330 | void GuardAction( Func func ) { |
---|
331 | at_cleanup( [func](__attribute__((unused)) void *) { func(); }, nullptr ); |
---|
332 | } |
---|
333 | }; |
---|
334 | |
---|
335 | template<typename pass_type> |
---|
336 | class WithVisitorRef { |
---|
337 | protected: |
---|
338 | WithVisitorRef() {} |
---|
339 | ~WithVisitorRef() {} |
---|
340 | |
---|
341 | public: |
---|
342 | PassVisitor<pass_type> * const visitor = nullptr; |
---|
343 | }; |
---|
344 | |
---|
345 | #include "PassVisitor.impl.h" |
---|