1 | #pragma once |
---|
2 | |
---|
3 | #include "SynTree/Mutator.h" |
---|
4 | #include "SynTree/Visitor.h" |
---|
5 | |
---|
6 | #include "SynTree/Initializer.h" |
---|
7 | #include "SynTree/Statement.h" |
---|
8 | #include "SynTree/Type.h" |
---|
9 | #include "SynTree/Declaration.h" |
---|
10 | #include "SynTree/Expression.h" |
---|
11 | #include "SynTree/Constant.h" |
---|
12 | |
---|
13 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
14 | // Deep magic (a.k.a template meta programming) to make the templated visitor work |
---|
15 | // Basically the goal is to make 2 previsit_impl |
---|
16 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of |
---|
17 | // 'pass.previsit( node )' that compiles will be used for that node for that type |
---|
18 | // This requires that this option only compile for passes that actually define an appropriate visit. |
---|
19 | // SFINAE will make sure the compilation errors in this function don't halt the build. |
---|
20 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE |
---|
21 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. |
---|
22 | // This is needed only to eliminate the need for passes to specify any kind of handlers. |
---|
23 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. |
---|
24 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 |
---|
25 | // the first implementation takes priority in regards to overloading. |
---|
26 | // Mutator functions work along the same principal |
---|
27 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
28 | // Visit |
---|
29 | template<typename pass_type, typename node_type> |
---|
30 | static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.previsit( node ), void() ) { |
---|
31 | pass.previsit( node ); |
---|
32 | } |
---|
33 | |
---|
34 | template<typename pass_type, typename node_type> |
---|
35 | static inline void previsit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {} |
---|
36 | |
---|
37 | |
---|
38 | template<typename pass_type, typename node_type> |
---|
39 | static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postvisit( node ), void() ) { |
---|
40 | pass.postvisit( node ); |
---|
41 | } |
---|
42 | |
---|
43 | template<typename pass_type, typename node_type> |
---|
44 | static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {} |
---|
45 | |
---|
46 | // Mutate |
---|
47 | template<typename pass_type, typename node_type> |
---|
48 | static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.premutate( node ), void() ) { |
---|
49 | return pass.premutate( node ); |
---|
50 | } |
---|
51 | |
---|
52 | template<typename pass_type, typename node_type> |
---|
53 | static inline void premutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {} |
---|
54 | |
---|
55 | |
---|
56 | template<typename return_type, typename pass_type, typename node_type> |
---|
57 | static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postmutate( node ) ) { |
---|
58 | return pass.postmutate( node ); |
---|
59 | } |
---|
60 | |
---|
61 | template<typename return_type, typename pass_type, typename node_type> |
---|
62 | static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } |
---|
63 | |
---|
64 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
65 | // Templated visitor type |
---|
66 | // To use declare a PassVisitor< YOUR VISITOR TYPE > |
---|
67 | // The visitor type should specify the previsit/postvisit for types that are desired. |
---|
68 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
69 | template< typename pass_type > |
---|
70 | class PassVisitor final : public Visitor, public Mutator { |
---|
71 | public: |
---|
72 | PassVisitor() = default; |
---|
73 | |
---|
74 | template< typename... Args > |
---|
75 | PassVisitor(Args &&... args) |
---|
76 | : pass( std::forward<Args>( args )... ) |
---|
77 | {} |
---|
78 | |
---|
79 | virtual ~PassVisitor() = default; |
---|
80 | private: |
---|
81 | pass_type pass; |
---|
82 | |
---|
83 | public: |
---|
84 | |
---|
85 | virtual void visit( ObjectDecl *objectDecl ) override final; |
---|
86 | virtual void visit( FunctionDecl *functionDecl ) override final; |
---|
87 | virtual void visit( StructDecl *aggregateDecl ) override final; |
---|
88 | virtual void visit( UnionDecl *aggregateDecl ) override final; |
---|
89 | virtual void visit( EnumDecl *aggregateDecl ) override final; |
---|
90 | virtual void visit( TraitDecl *aggregateDecl ) override final; |
---|
91 | virtual void visit( TypeDecl *typeDecl ) override final; |
---|
92 | virtual void visit( TypedefDecl *typeDecl ) override final; |
---|
93 | virtual void visit( AsmDecl *asmDecl ) override final; |
---|
94 | |
---|
95 | virtual void visit( CompoundStmt *compoundStmt ) override final; |
---|
96 | virtual void visit( ExprStmt *exprStmt ) override final; |
---|
97 | virtual void visit( AsmStmt *asmStmt ) override final; |
---|
98 | virtual void visit( IfStmt *ifStmt ) override final; |
---|
99 | virtual void visit( WhileStmt *whileStmt ) override final; |
---|
100 | virtual void visit( ForStmt *forStmt ) override final; |
---|
101 | virtual void visit( SwitchStmt *switchStmt ) override final; |
---|
102 | virtual void visit( CaseStmt *caseStmt ) override final; |
---|
103 | virtual void visit( BranchStmt *branchStmt ) override final; |
---|
104 | virtual void visit( ReturnStmt *returnStmt ) override final; |
---|
105 | virtual void visit( TryStmt *tryStmt ) override final; |
---|
106 | virtual void visit( CatchStmt *catchStmt ) override final; |
---|
107 | virtual void visit( FinallyStmt *finallyStmt ) override final; |
---|
108 | virtual void visit( NullStmt *nullStmt ) override final; |
---|
109 | virtual void visit( DeclStmt *declStmt ) override final; |
---|
110 | virtual void visit( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
111 | |
---|
112 | virtual void visit( ApplicationExpr *applicationExpr ) override final; |
---|
113 | virtual void visit( UntypedExpr *untypedExpr ) override final; |
---|
114 | virtual void visit( NameExpr *nameExpr ) override final; |
---|
115 | virtual void visit( CastExpr *castExpr ) override final; |
---|
116 | virtual void visit( AddressExpr *addressExpr ) override final; |
---|
117 | virtual void visit( LabelAddressExpr *labAddressExpr ) override final; |
---|
118 | virtual void visit( UntypedMemberExpr *memberExpr ) override final; |
---|
119 | virtual void visit( MemberExpr *memberExpr ) override final; |
---|
120 | virtual void visit( VariableExpr *variableExpr ) override final; |
---|
121 | virtual void visit( ConstantExpr *constantExpr ) override final; |
---|
122 | virtual void visit( SizeofExpr *sizeofExpr ) override final; |
---|
123 | virtual void visit( AlignofExpr *alignofExpr ) override final; |
---|
124 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
125 | virtual void visit( OffsetofExpr *offsetofExpr ) override final; |
---|
126 | virtual void visit( OffsetPackExpr *offsetPackExpr ) override final; |
---|
127 | virtual void visit( AttrExpr *attrExpr ) override final; |
---|
128 | virtual void visit( LogicalExpr *logicalExpr ) override final; |
---|
129 | virtual void visit( ConditionalExpr *conditionalExpr ) override final; |
---|
130 | virtual void visit( CommaExpr *commaExpr ) override final; |
---|
131 | virtual void visit( TypeExpr *typeExpr ) override final; |
---|
132 | virtual void visit( AsmExpr *asmExpr ) override final; |
---|
133 | virtual void visit( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
134 | virtual void visit( ConstructorExpr * ctorExpr ) override final; |
---|
135 | virtual void visit( CompoundLiteralExpr *compLitExpr ) override final; |
---|
136 | virtual void visit( UntypedValofExpr *valofExpr ) override final; |
---|
137 | virtual void visit( RangeExpr *rangeExpr ) override final; |
---|
138 | virtual void visit( UntypedTupleExpr *tupleExpr ) override final; |
---|
139 | virtual void visit( TupleExpr *tupleExpr ) override final; |
---|
140 | virtual void visit( TupleIndexExpr *tupleExpr ) override final; |
---|
141 | virtual void visit( MemberTupleExpr *tupleExpr ) override final; |
---|
142 | virtual void visit( TupleAssignExpr *assignExpr ) override final; |
---|
143 | virtual void visit( StmtExpr * stmtExpr ) override final; |
---|
144 | virtual void visit( UniqueExpr * uniqueExpr ) override final; |
---|
145 | |
---|
146 | virtual void visit( VoidType *basicType ) override final; |
---|
147 | virtual void visit( BasicType *basicType ) override final; |
---|
148 | virtual void visit( PointerType *pointerType ) override final; |
---|
149 | virtual void visit( ArrayType *arrayType ) override final; |
---|
150 | virtual void visit( FunctionType *functionType ) override final; |
---|
151 | virtual void visit( StructInstType *aggregateUseType ) override final; |
---|
152 | virtual void visit( UnionInstType *aggregateUseType ) override final; |
---|
153 | virtual void visit( EnumInstType *aggregateUseType ) override final; |
---|
154 | virtual void visit( TraitInstType *aggregateUseType ) override final; |
---|
155 | virtual void visit( TypeInstType *aggregateUseType ) override final; |
---|
156 | virtual void visit( TupleType *tupleType ) override final; |
---|
157 | virtual void visit( TypeofType *typeofType ) override final; |
---|
158 | virtual void visit( AttrType *attrType ) override final; |
---|
159 | virtual void visit( VarArgsType *varArgsType ) override final; |
---|
160 | virtual void visit( ZeroType *zeroType ) override final; |
---|
161 | virtual void visit( OneType *oneType ) override final; |
---|
162 | |
---|
163 | virtual void visit( SingleInit *singleInit ) override final; |
---|
164 | virtual void visit( ListInit *listInit ) override final; |
---|
165 | virtual void visit( ConstructorInit *ctorInit ) override final; |
---|
166 | |
---|
167 | virtual void visit( Subrange *subrange ) override final; |
---|
168 | |
---|
169 | virtual void visit( Constant *constant ) override final; |
---|
170 | |
---|
171 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final; |
---|
172 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final; |
---|
173 | virtual Declaration* mutate( StructDecl *aggregateDecl ) override final; |
---|
174 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final; |
---|
175 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final; |
---|
176 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final; |
---|
177 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final; |
---|
178 | virtual Declaration* mutate( TypedefDecl *typeDecl ) override final; |
---|
179 | virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final; |
---|
180 | |
---|
181 | virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final; |
---|
182 | virtual Statement* mutate( ExprStmt *exprStmt ) override final; |
---|
183 | virtual Statement* mutate( AsmStmt *asmStmt ) override final; |
---|
184 | virtual Statement* mutate( IfStmt *ifStmt ) override final; |
---|
185 | virtual Statement* mutate( WhileStmt *whileStmt ) override final; |
---|
186 | virtual Statement* mutate( ForStmt *forStmt ) override final; |
---|
187 | virtual Statement* mutate( SwitchStmt *switchStmt ) override final; |
---|
188 | virtual Statement* mutate( CaseStmt *caseStmt ) override final; |
---|
189 | virtual Statement* mutate( BranchStmt *branchStmt ) override final; |
---|
190 | virtual Statement* mutate( ReturnStmt *returnStmt ) override final; |
---|
191 | virtual Statement* mutate( TryStmt *returnStmt ) override final; |
---|
192 | virtual Statement* mutate( CatchStmt *catchStmt ) override final; |
---|
193 | virtual Statement* mutate( FinallyStmt *catchStmt ) override final; |
---|
194 | virtual NullStmt* mutate( NullStmt *nullStmt ) override final; |
---|
195 | virtual Statement* mutate( DeclStmt *declStmt ) override final; |
---|
196 | virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
197 | |
---|
198 | virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final; |
---|
199 | virtual Expression* mutate( UntypedExpr *untypedExpr ) override final; |
---|
200 | virtual Expression* mutate( NameExpr *nameExpr ) override final; |
---|
201 | virtual Expression* mutate( AddressExpr *castExpr ) override final; |
---|
202 | virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final; |
---|
203 | virtual Expression* mutate( CastExpr *castExpr ) override final; |
---|
204 | virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final; |
---|
205 | virtual Expression* mutate( MemberExpr *memberExpr ) override final; |
---|
206 | virtual Expression* mutate( VariableExpr *variableExpr ) override final; |
---|
207 | virtual Expression* mutate( ConstantExpr *constantExpr ) override final; |
---|
208 | virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final; |
---|
209 | virtual Expression* mutate( AlignofExpr *alignofExpr ) override final; |
---|
210 | virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
211 | virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final; |
---|
212 | virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final; |
---|
213 | virtual Expression* mutate( AttrExpr *attrExpr ) override final; |
---|
214 | virtual Expression* mutate( LogicalExpr *logicalExpr ) override final; |
---|
215 | virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final; |
---|
216 | virtual Expression* mutate( CommaExpr *commaExpr ) override final; |
---|
217 | virtual Expression* mutate( TypeExpr *typeExpr ) override final; |
---|
218 | virtual Expression* mutate( AsmExpr *asmExpr ) override final; |
---|
219 | virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
220 | virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final; |
---|
221 | virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final; |
---|
222 | virtual Expression* mutate( UntypedValofExpr *valofExpr ) override final; |
---|
223 | virtual Expression* mutate( RangeExpr *rangeExpr ) override final; |
---|
224 | virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final; |
---|
225 | virtual Expression* mutate( TupleExpr *tupleExpr ) override final; |
---|
226 | virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final; |
---|
227 | virtual Expression* mutate( MemberTupleExpr *tupleExpr ) override final; |
---|
228 | virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final; |
---|
229 | virtual Expression* mutate( StmtExpr * stmtExpr ) override final; |
---|
230 | virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final; |
---|
231 | |
---|
232 | virtual Type* mutate( VoidType *basicType ) override final; |
---|
233 | virtual Type* mutate( BasicType *basicType ) override final; |
---|
234 | virtual Type* mutate( PointerType *pointerType ) override final; |
---|
235 | virtual Type* mutate( ArrayType *arrayType ) override final; |
---|
236 | virtual Type* mutate( FunctionType *functionType ) override final; |
---|
237 | virtual Type* mutate( StructInstType *aggregateUseType ) override final; |
---|
238 | virtual Type* mutate( UnionInstType *aggregateUseType ) override final; |
---|
239 | virtual Type* mutate( EnumInstType *aggregateUseType ) override final; |
---|
240 | virtual Type* mutate( TraitInstType *aggregateUseType ) override final; |
---|
241 | virtual Type* mutate( TypeInstType *aggregateUseType ) override final; |
---|
242 | virtual Type* mutate( TupleType *tupleType ) override final; |
---|
243 | virtual Type* mutate( TypeofType *typeofType ) override final; |
---|
244 | virtual Type* mutate( AttrType *attrType ) override final; |
---|
245 | virtual Type* mutate( VarArgsType *varArgsType ) override final; |
---|
246 | virtual Type* mutate( ZeroType *zeroType ) override final; |
---|
247 | virtual Type* mutate( OneType *oneType ) override final; |
---|
248 | |
---|
249 | virtual Initializer* mutate( SingleInit *singleInit ) override final; |
---|
250 | virtual Initializer* mutate( ListInit *listInit ) override final; |
---|
251 | virtual Initializer* mutate( ConstructorInit *ctorInit ) override final; |
---|
252 | |
---|
253 | virtual Subrange *mutate( Subrange *subrange ) override final; |
---|
254 | |
---|
255 | virtual Constant *mutate( Constant *constant ) override final; |
---|
256 | |
---|
257 | private: |
---|
258 | template<typename node_type> |
---|
259 | auto call_previsit ( node_type * node ) |
---|
260 | -> decltype( previsit_impl ( pass, node, 0 ), void() ) |
---|
261 | { |
---|
262 | previsit_impl ( pass, node, 0 ); |
---|
263 | } |
---|
264 | |
---|
265 | template<typename node_type> |
---|
266 | auto call_postvisit( node_type * node ) |
---|
267 | -> decltype( postvisit_impl( pass, node, 0 ), void() ) |
---|
268 | { |
---|
269 | postvisit_impl( pass, node, 0 ); |
---|
270 | } |
---|
271 | |
---|
272 | template<typename node_type> |
---|
273 | auto call_premutate ( node_type * node ) |
---|
274 | -> decltype( premutate_impl( pass, node, 0 ), void() ) |
---|
275 | { |
---|
276 | premutate_impl( pass, node, 0 ); |
---|
277 | } |
---|
278 | |
---|
279 | template<typename return_type, typename node_type> |
---|
280 | auto call_postmutate ( node_type * node ) |
---|
281 | -> decltype( postmutate_impl<return_type>( pass, node, 0 ) ) |
---|
282 | { |
---|
283 | return postmutate_impl<return_type>( pass, node, 0 ); |
---|
284 | } |
---|
285 | }; |
---|
286 | |
---|
287 | #include "PassVisitor.impl.h" |
---|