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( ReferenceType *referenceType ) override final; |
---|
116 | virtual void visit( FunctionType *functionType ) override final; |
---|
117 | virtual void visit( StructInstType *aggregateUseType ) override final; |
---|
118 | virtual void visit( UnionInstType *aggregateUseType ) override final; |
---|
119 | virtual void visit( EnumInstType *aggregateUseType ) override final; |
---|
120 | virtual void visit( TraitInstType *aggregateUseType ) override final; |
---|
121 | virtual void visit( TypeInstType *aggregateUseType ) override final; |
---|
122 | virtual void visit( TupleType *tupleType ) override final; |
---|
123 | virtual void visit( TypeofType *typeofType ) override final; |
---|
124 | virtual void visit( AttrType *attrType ) override final; |
---|
125 | virtual void visit( VarArgsType *varArgsType ) override final; |
---|
126 | virtual void visit( ZeroType *zeroType ) override final; |
---|
127 | virtual void visit( OneType *oneType ) override final; |
---|
128 | |
---|
129 | virtual void visit( SingleInit *singleInit ) override final; |
---|
130 | virtual void visit( ListInit *listInit ) override final; |
---|
131 | virtual void visit( ConstructorInit *ctorInit ) override final; |
---|
132 | |
---|
133 | virtual void visit( Subrange *subrange ) override final; |
---|
134 | |
---|
135 | virtual void visit( Constant *constant ) override final; |
---|
136 | |
---|
137 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final; |
---|
138 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final; |
---|
139 | virtual Declaration* mutate( StructDecl *aggregateDecl ) override final; |
---|
140 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final; |
---|
141 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final; |
---|
142 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final; |
---|
143 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final; |
---|
144 | virtual Declaration* mutate( TypedefDecl *typeDecl ) override final; |
---|
145 | virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final; |
---|
146 | |
---|
147 | virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final; |
---|
148 | virtual Statement* mutate( ExprStmt *exprStmt ) override final; |
---|
149 | virtual Statement* mutate( AsmStmt *asmStmt ) override final; |
---|
150 | virtual Statement* mutate( IfStmt *ifStmt ) override final; |
---|
151 | virtual Statement* mutate( WhileStmt *whileStmt ) override final; |
---|
152 | virtual Statement* mutate( ForStmt *forStmt ) override final; |
---|
153 | virtual Statement* mutate( SwitchStmt *switchStmt ) override final; |
---|
154 | virtual Statement* mutate( CaseStmt *caseStmt ) override final; |
---|
155 | virtual Statement* mutate( BranchStmt *branchStmt ) override final; |
---|
156 | virtual Statement* mutate( ReturnStmt *returnStmt ) override final; |
---|
157 | virtual Statement* mutate( ThrowStmt *throwStmt ) override final; |
---|
158 | virtual Statement* mutate( TryStmt *returnStmt ) override final; |
---|
159 | virtual Statement* mutate( CatchStmt *catchStmt ) override final; |
---|
160 | virtual Statement* mutate( FinallyStmt *catchStmt ) override final; |
---|
161 | virtual NullStmt* mutate( NullStmt *nullStmt ) override final; |
---|
162 | virtual Statement* mutate( DeclStmt *declStmt ) override final; |
---|
163 | virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
164 | |
---|
165 | virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final; |
---|
166 | virtual Expression* mutate( UntypedExpr *untypedExpr ) override final; |
---|
167 | virtual Expression* mutate( NameExpr *nameExpr ) override final; |
---|
168 | virtual Expression* mutate( AddressExpr *castExpr ) override final; |
---|
169 | virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final; |
---|
170 | virtual Expression* mutate( CastExpr *castExpr ) override final; |
---|
171 | virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final; |
---|
172 | virtual Expression* mutate( MemberExpr *memberExpr ) override final; |
---|
173 | virtual Expression* mutate( VariableExpr *variableExpr ) override final; |
---|
174 | virtual Expression* mutate( ConstantExpr *constantExpr ) override final; |
---|
175 | virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final; |
---|
176 | virtual Expression* mutate( AlignofExpr *alignofExpr ) override final; |
---|
177 | virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
178 | virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final; |
---|
179 | virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final; |
---|
180 | virtual Expression* mutate( AttrExpr *attrExpr ) override final; |
---|
181 | virtual Expression* mutate( LogicalExpr *logicalExpr ) override final; |
---|
182 | virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final; |
---|
183 | virtual Expression* mutate( CommaExpr *commaExpr ) override final; |
---|
184 | virtual Expression* mutate( TypeExpr *typeExpr ) override final; |
---|
185 | virtual Expression* mutate( AsmExpr *asmExpr ) override final; |
---|
186 | virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
187 | virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final; |
---|
188 | virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final; |
---|
189 | virtual Expression* mutate( RangeExpr *rangeExpr ) override final; |
---|
190 | virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final; |
---|
191 | virtual Expression* mutate( TupleExpr *tupleExpr ) override final; |
---|
192 | virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final; |
---|
193 | virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final; |
---|
194 | virtual Expression* mutate( StmtExpr * stmtExpr ) override final; |
---|
195 | virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final; |
---|
196 | |
---|
197 | virtual Type* mutate( VoidType *basicType ) override final; |
---|
198 | virtual Type* mutate( BasicType *basicType ) override final; |
---|
199 | virtual Type* mutate( PointerType *pointerType ) override final; |
---|
200 | virtual Type* mutate( ArrayType *arrayType ) override final; |
---|
201 | virtual Type* mutate( ReferenceType *referenceType ) override final; |
---|
202 | virtual Type* mutate( FunctionType *functionType ) override final; |
---|
203 | virtual Type* mutate( StructInstType *aggregateUseType ) override final; |
---|
204 | virtual Type* mutate( UnionInstType *aggregateUseType ) override final; |
---|
205 | virtual Type* mutate( EnumInstType *aggregateUseType ) override final; |
---|
206 | virtual Type* mutate( TraitInstType *aggregateUseType ) override final; |
---|
207 | virtual Type* mutate( TypeInstType *aggregateUseType ) override final; |
---|
208 | virtual Type* mutate( TupleType *tupleType ) override final; |
---|
209 | virtual Type* mutate( TypeofType *typeofType ) override final; |
---|
210 | virtual Type* mutate( AttrType *attrType ) override final; |
---|
211 | virtual Type* mutate( VarArgsType *varArgsType ) override final; |
---|
212 | virtual Type* mutate( ZeroType *zeroType ) override final; |
---|
213 | virtual Type* mutate( OneType *oneType ) override final; |
---|
214 | |
---|
215 | virtual Initializer* mutate( SingleInit *singleInit ) override final; |
---|
216 | virtual Initializer* mutate( ListInit *listInit ) override final; |
---|
217 | virtual Initializer* mutate( ConstructorInit *ctorInit ) override final; |
---|
218 | |
---|
219 | virtual Subrange *mutate( Subrange *subrange ) override final; |
---|
220 | |
---|
221 | virtual Constant *mutate( Constant *constant ) override final; |
---|
222 | |
---|
223 | private: |
---|
224 | template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
225 | template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
226 | |
---|
227 | template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
228 | template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
229 | |
---|
230 | template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); } |
---|
231 | template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); } |
---|
232 | |
---|
233 | void call_beginScope() { begin_scope_impl( pass, 0 ); } |
---|
234 | void call_endScope () { end_scope_impl ( pass, 0 ); } |
---|
235 | |
---|
236 | void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } |
---|
237 | |
---|
238 | template< typename func_t > |
---|
239 | void handleStatementList( std::list< Statement * > & statements, func_t func ); |
---|
240 | void visitStatementList ( std::list< Statement* > &statements ); |
---|
241 | void mutateStatementList( std::list< Statement* > &statements ); |
---|
242 | |
---|
243 | template< typename func_t > |
---|
244 | Statement * handleStatement( Statement * stmt, func_t func ); |
---|
245 | Statement * visitStatement ( Statement * stmt ); |
---|
246 | Statement * mutateStatement( Statement * stmt ); |
---|
247 | |
---|
248 | template< typename func_t > |
---|
249 | Expression * handleExpression( Expression * expr, func_t func ); |
---|
250 | Expression * visitExpression ( Expression * expr ); |
---|
251 | Expression * mutateExpression( Expression * expr ); |
---|
252 | |
---|
253 | |
---|
254 | TypeSubstitution ** get_env_ptr () { return env_impl ( pass, 0); } |
---|
255 | std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } |
---|
256 | std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } |
---|
257 | std::list< Declaration* > * get_beforeDecls() { return declsToAddBefore_impl( pass, 0); } |
---|
258 | std::list< Declaration* > * get_afterDecls () { return declsToAddAfter_impl ( pass, 0); } |
---|
259 | |
---|
260 | void set_visit_children( bool& ref ) { bool_ref * ptr = visit_children_impl(pass, 0); if(ptr) ptr->set( ref ); } |
---|
261 | }; |
---|
262 | |
---|
263 | template<typename pass_type, typename T> |
---|
264 | void GuardValue( pass_type * pass, T& val ) { |
---|
265 | pass->at_cleanup( [ val ]( void * newVal ) { |
---|
266 | * static_cast< T * >( newVal ) = val; |
---|
267 | }, static_cast< void * >( & val ) ); |
---|
268 | } |
---|
269 | |
---|
270 | class WithTypeSubstitution { |
---|
271 | protected: |
---|
272 | WithTypeSubstitution() = default; |
---|
273 | ~WithTypeSubstitution() = default; |
---|
274 | |
---|
275 | public: |
---|
276 | TypeSubstitution * env = nullptr; |
---|
277 | }; |
---|
278 | |
---|
279 | class WithStmtsToAdd { |
---|
280 | protected: |
---|
281 | WithStmtsToAdd() = default; |
---|
282 | ~WithStmtsToAdd() = default; |
---|
283 | |
---|
284 | public: |
---|
285 | std::list< Statement* > stmtsToAddBefore; |
---|
286 | std::list< Statement* > stmtsToAddAfter; |
---|
287 | }; |
---|
288 | |
---|
289 | class WithDeclsToAdd { |
---|
290 | protected: |
---|
291 | WithDeclsToAdd() = default; |
---|
292 | ~WithDeclsToAdd() = default; |
---|
293 | |
---|
294 | public: |
---|
295 | std::list< Declaration* > declsToAddBefore; |
---|
296 | std::list< Declaration* > declsToAddAfter; |
---|
297 | }; |
---|
298 | |
---|
299 | class WithShortCircuiting { |
---|
300 | protected: |
---|
301 | WithShortCircuiting() = default; |
---|
302 | ~WithShortCircuiting() = default; |
---|
303 | |
---|
304 | public: |
---|
305 | bool_ref visit_children; |
---|
306 | }; |
---|
307 | |
---|
308 | class WithGuards { |
---|
309 | protected: |
---|
310 | WithGuards() = default; |
---|
311 | ~WithGuards() = default; |
---|
312 | |
---|
313 | public: |
---|
314 | at_cleanup_t at_cleanup; |
---|
315 | |
---|
316 | template< typename T > |
---|
317 | void GuardValue( T& val ) { |
---|
318 | at_cleanup( [ val ]( void * newVal ) { |
---|
319 | * static_cast< T * >( newVal ) = val; |
---|
320 | }, static_cast< void * >( & val ) ); |
---|
321 | } |
---|
322 | |
---|
323 | template< typename T > |
---|
324 | void GuardScope( T& val ) { |
---|
325 | val.beginScope(); |
---|
326 | at_cleanup( []( void * val ) { |
---|
327 | static_cast< T * >( val )->endScope(); |
---|
328 | }, static_cast< void * >( & val ) ); |
---|
329 | } |
---|
330 | |
---|
331 | template< typename Func > |
---|
332 | void GuardAction( Func func ) { |
---|
333 | at_cleanup( [func](__attribute__((unused)) void *) { func(); }, nullptr ); |
---|
334 | } |
---|
335 | }; |
---|
336 | |
---|
337 | template<typename pass_type> |
---|
338 | class WithVisitorRef { |
---|
339 | protected: |
---|
340 | WithVisitorRef() {} |
---|
341 | ~WithVisitorRef() {} |
---|
342 | |
---|
343 | public: |
---|
344 | PassVisitor<pass_type> * const visitor = nullptr; |
---|
345 | }; |
---|
346 | |
---|
347 | #include "PassVisitor.impl.h" |
---|