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 | |
---|
15 | #include "PassVisitor.proto.h" |
---|
16 | |
---|
17 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
18 | // Templated visitor type |
---|
19 | // To use declare a PassVisitor< YOUR VISITOR TYPE > |
---|
20 | // The visitor type should specify the previsit/postvisit for types that are desired. |
---|
21 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
22 | template< typename pass_type > |
---|
23 | class PassVisitor final : public Visitor, public Mutator { |
---|
24 | public: |
---|
25 | PassVisitor() = default; |
---|
26 | |
---|
27 | template< typename... Args > |
---|
28 | PassVisitor(Args &&... args) |
---|
29 | : pass( std::forward<Args>( args )... ) |
---|
30 | {} |
---|
31 | |
---|
32 | virtual ~PassVisitor() = default; |
---|
33 | |
---|
34 | pass_type pass; |
---|
35 | |
---|
36 | virtual void visit( ObjectDecl *objectDecl ) override final; |
---|
37 | virtual void visit( FunctionDecl *functionDecl ) override final; |
---|
38 | virtual void visit( StructDecl *aggregateDecl ) override final; |
---|
39 | virtual void visit( UnionDecl *aggregateDecl ) override final; |
---|
40 | virtual void visit( EnumDecl *aggregateDecl ) override final; |
---|
41 | virtual void visit( TraitDecl *aggregateDecl ) override final; |
---|
42 | virtual void visit( TypeDecl *typeDecl ) override final; |
---|
43 | virtual void visit( TypedefDecl *typeDecl ) override final; |
---|
44 | virtual void visit( AsmDecl *asmDecl ) override final; |
---|
45 | |
---|
46 | virtual void visit( CompoundStmt *compoundStmt ) override final; |
---|
47 | virtual void visit( ExprStmt *exprStmt ) override final; |
---|
48 | virtual void visit( AsmStmt *asmStmt ) override final; |
---|
49 | virtual void visit( IfStmt *ifStmt ) override final; |
---|
50 | virtual void visit( WhileStmt *whileStmt ) override final; |
---|
51 | virtual void visit( ForStmt *forStmt ) override final; |
---|
52 | virtual void visit( SwitchStmt *switchStmt ) override final; |
---|
53 | virtual void visit( CaseStmt *caseStmt ) override final; |
---|
54 | virtual void visit( BranchStmt *branchStmt ) override final; |
---|
55 | virtual void visit( ReturnStmt *returnStmt ) override final; |
---|
56 | virtual void visit( TryStmt *tryStmt ) override final; |
---|
57 | virtual void visit( CatchStmt *catchStmt ) override final; |
---|
58 | virtual void visit( FinallyStmt *finallyStmt ) override final; |
---|
59 | virtual void visit( NullStmt *nullStmt ) override final; |
---|
60 | virtual void visit( DeclStmt *declStmt ) override final; |
---|
61 | virtual void visit( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
62 | |
---|
63 | virtual void visit( ApplicationExpr *applicationExpr ) override final; |
---|
64 | virtual void visit( UntypedExpr *untypedExpr ) override final; |
---|
65 | virtual void visit( NameExpr *nameExpr ) override final; |
---|
66 | virtual void visit( CastExpr *castExpr ) override final; |
---|
67 | virtual void visit( AddressExpr *addressExpr ) override final; |
---|
68 | virtual void visit( LabelAddressExpr *labAddressExpr ) override final; |
---|
69 | virtual void visit( UntypedMemberExpr *memberExpr ) override final; |
---|
70 | virtual void visit( MemberExpr *memberExpr ) override final; |
---|
71 | virtual void visit( VariableExpr *variableExpr ) override final; |
---|
72 | virtual void visit( ConstantExpr *constantExpr ) override final; |
---|
73 | virtual void visit( SizeofExpr *sizeofExpr ) override final; |
---|
74 | virtual void visit( AlignofExpr *alignofExpr ) override final; |
---|
75 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
76 | virtual void visit( OffsetofExpr *offsetofExpr ) override final; |
---|
77 | virtual void visit( OffsetPackExpr *offsetPackExpr ) override final; |
---|
78 | virtual void visit( AttrExpr *attrExpr ) override final; |
---|
79 | virtual void visit( LogicalExpr *logicalExpr ) override final; |
---|
80 | virtual void visit( ConditionalExpr *conditionalExpr ) override final; |
---|
81 | virtual void visit( CommaExpr *commaExpr ) override final; |
---|
82 | virtual void visit( TypeExpr *typeExpr ) override final; |
---|
83 | virtual void visit( AsmExpr *asmExpr ) override final; |
---|
84 | virtual void visit( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
85 | virtual void visit( ConstructorExpr * ctorExpr ) override final; |
---|
86 | virtual void visit( CompoundLiteralExpr *compLitExpr ) override final; |
---|
87 | virtual void visit( UntypedValofExpr *valofExpr ) override final; |
---|
88 | virtual void visit( RangeExpr *rangeExpr ) override final; |
---|
89 | virtual void visit( UntypedTupleExpr *tupleExpr ) override final; |
---|
90 | virtual void visit( TupleExpr *tupleExpr ) override final; |
---|
91 | virtual void visit( TupleIndexExpr *tupleExpr ) override final; |
---|
92 | virtual void visit( MemberTupleExpr *tupleExpr ) override final; |
---|
93 | virtual void visit( TupleAssignExpr *assignExpr ) override final; |
---|
94 | virtual void visit( StmtExpr * stmtExpr ) override final; |
---|
95 | virtual void visit( UniqueExpr * uniqueExpr ) override final; |
---|
96 | |
---|
97 | virtual void visit( VoidType *basicType ) override final; |
---|
98 | virtual void visit( BasicType *basicType ) override final; |
---|
99 | virtual void visit( PointerType *pointerType ) override final; |
---|
100 | virtual void visit( ArrayType *arrayType ) override final; |
---|
101 | virtual void visit( FunctionType *functionType ) override final; |
---|
102 | virtual void visit( StructInstType *aggregateUseType ) override final; |
---|
103 | virtual void visit( UnionInstType *aggregateUseType ) override final; |
---|
104 | virtual void visit( EnumInstType *aggregateUseType ) override final; |
---|
105 | virtual void visit( TraitInstType *aggregateUseType ) override final; |
---|
106 | virtual void visit( TypeInstType *aggregateUseType ) override final; |
---|
107 | virtual void visit( TupleType *tupleType ) override final; |
---|
108 | virtual void visit( TypeofType *typeofType ) override final; |
---|
109 | virtual void visit( AttrType *attrType ) override final; |
---|
110 | virtual void visit( VarArgsType *varArgsType ) override final; |
---|
111 | virtual void visit( ZeroType *zeroType ) override final; |
---|
112 | virtual void visit( OneType *oneType ) override final; |
---|
113 | |
---|
114 | virtual void visit( SingleInit *singleInit ) override final; |
---|
115 | virtual void visit( ListInit *listInit ) override final; |
---|
116 | virtual void visit( ConstructorInit *ctorInit ) override final; |
---|
117 | |
---|
118 | virtual void visit( Subrange *subrange ) override final; |
---|
119 | |
---|
120 | virtual void visit( Constant *constant ) override final; |
---|
121 | |
---|
122 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final; |
---|
123 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final; |
---|
124 | virtual Declaration* mutate( StructDecl *aggregateDecl ) override final; |
---|
125 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final; |
---|
126 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final; |
---|
127 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final; |
---|
128 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final; |
---|
129 | virtual Declaration* mutate( TypedefDecl *typeDecl ) override final; |
---|
130 | virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final; |
---|
131 | |
---|
132 | virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final; |
---|
133 | virtual Statement* mutate( ExprStmt *exprStmt ) override final; |
---|
134 | virtual Statement* mutate( AsmStmt *asmStmt ) override final; |
---|
135 | virtual Statement* mutate( IfStmt *ifStmt ) override final; |
---|
136 | virtual Statement* mutate( WhileStmt *whileStmt ) override final; |
---|
137 | virtual Statement* mutate( ForStmt *forStmt ) override final; |
---|
138 | virtual Statement* mutate( SwitchStmt *switchStmt ) override final; |
---|
139 | virtual Statement* mutate( CaseStmt *caseStmt ) override final; |
---|
140 | virtual Statement* mutate( BranchStmt *branchStmt ) override final; |
---|
141 | virtual Statement* mutate( ReturnStmt *returnStmt ) override final; |
---|
142 | virtual Statement* mutate( TryStmt *returnStmt ) override final; |
---|
143 | virtual Statement* mutate( CatchStmt *catchStmt ) override final; |
---|
144 | virtual Statement* mutate( FinallyStmt *catchStmt ) override final; |
---|
145 | virtual NullStmt* mutate( NullStmt *nullStmt ) override final; |
---|
146 | virtual Statement* mutate( DeclStmt *declStmt ) override final; |
---|
147 | virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final; |
---|
148 | |
---|
149 | virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final; |
---|
150 | virtual Expression* mutate( UntypedExpr *untypedExpr ) override final; |
---|
151 | virtual Expression* mutate( NameExpr *nameExpr ) override final; |
---|
152 | virtual Expression* mutate( AddressExpr *castExpr ) override final; |
---|
153 | virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final; |
---|
154 | virtual Expression* mutate( CastExpr *castExpr ) override final; |
---|
155 | virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final; |
---|
156 | virtual Expression* mutate( MemberExpr *memberExpr ) override final; |
---|
157 | virtual Expression* mutate( VariableExpr *variableExpr ) override final; |
---|
158 | virtual Expression* mutate( ConstantExpr *constantExpr ) override final; |
---|
159 | virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final; |
---|
160 | virtual Expression* mutate( AlignofExpr *alignofExpr ) override final; |
---|
161 | virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final; |
---|
162 | virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final; |
---|
163 | virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final; |
---|
164 | virtual Expression* mutate( AttrExpr *attrExpr ) override final; |
---|
165 | virtual Expression* mutate( LogicalExpr *logicalExpr ) override final; |
---|
166 | virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final; |
---|
167 | virtual Expression* mutate( CommaExpr *commaExpr ) override final; |
---|
168 | virtual Expression* mutate( TypeExpr *typeExpr ) override final; |
---|
169 | virtual Expression* mutate( AsmExpr *asmExpr ) override final; |
---|
170 | virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final; |
---|
171 | virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final; |
---|
172 | virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final; |
---|
173 | virtual Expression* mutate( UntypedValofExpr *valofExpr ) override final; |
---|
174 | virtual Expression* mutate( RangeExpr *rangeExpr ) override final; |
---|
175 | virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final; |
---|
176 | virtual Expression* mutate( TupleExpr *tupleExpr ) override final; |
---|
177 | virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final; |
---|
178 | virtual Expression* mutate( MemberTupleExpr *tupleExpr ) override final; |
---|
179 | virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final; |
---|
180 | virtual Expression* mutate( StmtExpr * stmtExpr ) override final; |
---|
181 | virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final; |
---|
182 | |
---|
183 | virtual Type* mutate( VoidType *basicType ) override final; |
---|
184 | virtual Type* mutate( BasicType *basicType ) override final; |
---|
185 | virtual Type* mutate( PointerType *pointerType ) override final; |
---|
186 | virtual Type* mutate( ArrayType *arrayType ) override final; |
---|
187 | virtual Type* mutate( FunctionType *functionType ) override final; |
---|
188 | virtual Type* mutate( StructInstType *aggregateUseType ) override final; |
---|
189 | virtual Type* mutate( UnionInstType *aggregateUseType ) override final; |
---|
190 | virtual Type* mutate( EnumInstType *aggregateUseType ) override final; |
---|
191 | virtual Type* mutate( TraitInstType *aggregateUseType ) override final; |
---|
192 | virtual Type* mutate( TypeInstType *aggregateUseType ) override final; |
---|
193 | virtual Type* mutate( TupleType *tupleType ) override final; |
---|
194 | virtual Type* mutate( TypeofType *typeofType ) override final; |
---|
195 | virtual Type* mutate( AttrType *attrType ) override final; |
---|
196 | virtual Type* mutate( VarArgsType *varArgsType ) override final; |
---|
197 | virtual Type* mutate( ZeroType *zeroType ) override final; |
---|
198 | virtual Type* mutate( OneType *oneType ) override final; |
---|
199 | |
---|
200 | virtual Initializer* mutate( SingleInit *singleInit ) override final; |
---|
201 | virtual Initializer* mutate( ListInit *listInit ) override final; |
---|
202 | virtual Initializer* mutate( ConstructorInit *ctorInit ) override final; |
---|
203 | |
---|
204 | virtual Subrange *mutate( Subrange *subrange ) override final; |
---|
205 | |
---|
206 | virtual Constant *mutate( Constant *constant ) override final; |
---|
207 | |
---|
208 | private: |
---|
209 | template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
210 | template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
211 | |
---|
212 | template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); } |
---|
213 | template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); } |
---|
214 | |
---|
215 | void call_beginScope() { begin_scope_impl( pass, 0 ); } |
---|
216 | void call_endScope () { end_scope_impl ( pass, 0 ); } |
---|
217 | |
---|
218 | void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } |
---|
219 | |
---|
220 | void visitStatementList( std::list< Statement* > &statements ); |
---|
221 | void mutateStatementList( std::list< Statement* > &statements ); |
---|
222 | |
---|
223 | Statement * visitStatement( Statement * stmt ); |
---|
224 | Statement * mutateStatement( Statement * stmt ); |
---|
225 | |
---|
226 | void visitExpression( Expression * expr ); |
---|
227 | Expression * mutateExpression( Expression * expr ); |
---|
228 | |
---|
229 | |
---|
230 | TypeSubstitution ** get_env_ptr () { return env_impl ( pass, 0); } |
---|
231 | std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } |
---|
232 | std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } |
---|
233 | bool visit_children() { bool* skip = skip_children_impl(pass, 0); return ! (skip && *skip); } |
---|
234 | }; |
---|
235 | |
---|
236 | #include "PassVisitor.impl.h" |
---|