1 | #pragma once |
---|
2 | |
---|
3 | // IWYU pragma: private, include "Common/PassVisitor.h" |
---|
4 | |
---|
5 | #include <stack> |
---|
6 | #include <type_traits> |
---|
7 | |
---|
8 | #include "Common/Stats.h" |
---|
9 | #include "Common/utility.h" |
---|
10 | |
---|
11 | #include "SynTree/Mutator.h" |
---|
12 | #include "SynTree/Visitor.h" |
---|
13 | |
---|
14 | #include "SymTab/Indexer.h" |
---|
15 | |
---|
16 | #include "SynTree/Attribute.h" |
---|
17 | #include "SynTree/Initializer.h" |
---|
18 | #include "SynTree/Statement.h" |
---|
19 | #include "SynTree/Type.h" |
---|
20 | #include "SynTree/Declaration.h" |
---|
21 | #include "SynTree/Expression.h" |
---|
22 | #include "SynTree/Constant.h" |
---|
23 | |
---|
24 | class TypeSubstitution; |
---|
25 | |
---|
26 | #include "PassVisitor.proto.h" |
---|
27 | |
---|
28 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
29 | // Templated visitor type |
---|
30 | // To use declare a PassVisitor< YOUR VISITOR TYPE > |
---|
31 | // The visitor type should specify the previsit/postvisit/premutate/postmutate for types that are desired. |
---|
32 | // Note: previsit/postvisit/premutate/postmutate must be **public** members |
---|
33 | // |
---|
34 | // Several additional features are available through inheritance |
---|
35 | // | WithTypeSubstitution - provides polymorphic TypeSubstitution * env for the current expression |
---|
36 | // | WithStmtsToAdd - provides the ability to insert statements before or after the current statement by adding new statements into |
---|
37 | // stmtsToAddBefore or stmtsToAddAfter respectively. |
---|
38 | // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children |
---|
39 | // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable |
---|
40 | // will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates. |
---|
41 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
42 | template< typename pass_type > |
---|
43 | class PassVisitor final : public Visitor, public Mutator { |
---|
44 | public: |
---|
45 | |
---|
46 | template< typename... Args > |
---|
47 | PassVisitor(Args &&... args) |
---|
48 | : pass( std::forward<Args>( args )... ) |
---|
49 | { |
---|
50 | typedef PassVisitor<pass_type> this_t; |
---|
51 | this_t * const * visitor = visitor_impl(pass, 0); |
---|
52 | if(visitor) { |
---|
53 | *const_cast<this_t **>( visitor ) = this; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | virtual ~PassVisitor() = default; |
---|
58 | |
---|
59 | pass_type pass; |
---|
60 | |
---|
61 | virtual void visit( ObjectDecl * objectDecl ) override final; |
---|
62 | virtual void visit( const ObjectDecl * objectDecl ) override final; |
---|
63 | virtual void visit( FunctionDecl * functionDecl ) override final; |
---|
64 | virtual void visit( const FunctionDecl * functionDecl ) override final; |
---|
65 | virtual void visit( StructDecl * aggregateDecl ) override final; |
---|
66 | virtual void visit( const StructDecl * aggregateDecl ) override final; |
---|
67 | virtual void visit( UnionDecl * aggregateDecl ) override final; |
---|
68 | virtual void visit( const UnionDecl * aggregateDecl ) override final; |
---|
69 | virtual void visit( EnumDecl * aggregateDecl ) override final; |
---|
70 | virtual void visit( const EnumDecl * aggregateDecl ) override final; |
---|
71 | virtual void visit( TraitDecl * aggregateDecl ) override final; |
---|
72 | virtual void visit( const TraitDecl * aggregateDecl ) override final; |
---|
73 | virtual void visit( TypeDecl * typeDecl ) override final; |
---|
74 | virtual void visit( const TypeDecl * typeDecl ) override final; |
---|
75 | virtual void visit( TypedefDecl * typeDecl ) override final; |
---|
76 | virtual void visit( const TypedefDecl * typeDecl ) override final; |
---|
77 | virtual void visit( AsmDecl * asmDecl ) override final; |
---|
78 | virtual void visit( const AsmDecl * asmDecl ) override final; |
---|
79 | virtual void visit( StaticAssertDecl * assertDecl ) override final; |
---|
80 | virtual void visit( const StaticAssertDecl * assertDecl ) override final; |
---|
81 | |
---|
82 | virtual void visit( CompoundStmt * compoundStmt ) override final; |
---|
83 | virtual void visit( const CompoundStmt * compoundStmt ) override final; |
---|
84 | virtual void visit( ExprStmt * exprStmt ) override final; |
---|
85 | virtual void visit( const ExprStmt * exprStmt ) override final; |
---|
86 | virtual void visit( AsmStmt * asmStmt ) override final; |
---|
87 | virtual void visit( const AsmStmt * asmStmt ) override final; |
---|
88 | virtual void visit( DirectiveStmt * dirStmt ) override final; |
---|
89 | virtual void visit( const DirectiveStmt * dirStmt ) override final; |
---|
90 | virtual void visit( IfStmt * ifStmt ) override final; |
---|
91 | virtual void visit( const IfStmt * ifStmt ) override final; |
---|
92 | virtual void visit( WhileStmt * whileStmt ) override final; |
---|
93 | virtual void visit( const WhileStmt * whileStmt ) override final; |
---|
94 | virtual void visit( ForStmt * forStmt ) override final; |
---|
95 | virtual void visit( const ForStmt * forStmt ) override final; |
---|
96 | virtual void visit( SwitchStmt * switchStmt ) override final; |
---|
97 | virtual void visit( const SwitchStmt * switchStmt ) override final; |
---|
98 | virtual void visit( CaseStmt * caseStmt ) override final; |
---|
99 | virtual void visit( const CaseStmt * caseStmt ) override final; |
---|
100 | virtual void visit( BranchStmt * branchStmt ) override final; |
---|
101 | virtual void visit( const BranchStmt * branchStmt ) override final; |
---|
102 | virtual void visit( ReturnStmt * returnStmt ) override final; |
---|
103 | virtual void visit( const ReturnStmt * returnStmt ) override final; |
---|
104 | virtual void visit( ThrowStmt * throwStmt ) override final; |
---|
105 | virtual void visit( const ThrowStmt * throwStmt ) override final; |
---|
106 | virtual void visit( TryStmt * tryStmt ) override final; |
---|
107 | virtual void visit( const TryStmt * tryStmt ) override final; |
---|
108 | virtual void visit( CatchStmt * catchStmt ) override final; |
---|
109 | virtual void visit( const CatchStmt * catchStmt ) override final; |
---|
110 | virtual void visit( FinallyStmt * finallyStmt ) override final; |
---|
111 | virtual void visit( const FinallyStmt * finallyStmt ) override final; |
---|
112 | virtual void visit( SuspendStmt * suspendStmt ) override final; |
---|
113 | virtual void visit( const SuspendStmt * suspendStmt ) override final; |
---|
114 | virtual void visit( WaitForStmt * waitforStmt ) override final; |
---|
115 | virtual void visit( const WaitForStmt * waitforStmt ) override final; |
---|
116 | virtual void visit( WithStmt * withStmt ) override final; |
---|
117 | virtual void visit( const WithStmt * withStmt ) override final; |
---|
118 | virtual void visit( NullStmt * nullStmt ) override final; |
---|
119 | virtual void visit( const NullStmt * nullStmt ) override final; |
---|
120 | virtual void visit( DeclStmt * declStmt ) override final; |
---|
121 | virtual void visit( const DeclStmt * declStmt ) override final; |
---|
122 | virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) override final; |
---|
123 | virtual void visit( const ImplicitCtorDtorStmt * impCtorDtorStmt ) override final; |
---|
124 | |
---|
125 | virtual void visit( ApplicationExpr * applicationExpr ) override final; |
---|
126 | virtual void visit( const ApplicationExpr * applicationExpr ) override final; |
---|
127 | virtual void visit( UntypedExpr * untypedExpr ) override final; |
---|
128 | virtual void visit( const UntypedExpr * untypedExpr ) override final; |
---|
129 | virtual void visit( NameExpr * nameExpr ) override final; |
---|
130 | virtual void visit( const NameExpr * nameExpr ) override final; |
---|
131 | virtual void visit( CastExpr * castExpr ) override final; |
---|
132 | virtual void visit( const CastExpr * castExpr ) override final; |
---|
133 | virtual void visit( KeywordCastExpr * castExpr ) override final; |
---|
134 | virtual void visit( const KeywordCastExpr * castExpr ) override final; |
---|
135 | virtual void visit( VirtualCastExpr * castExpr ) override final; |
---|
136 | virtual void visit( const VirtualCastExpr * castExpr ) override final; |
---|
137 | virtual void visit( AddressExpr * addressExpr ) override final; |
---|
138 | virtual void visit( const AddressExpr * addressExpr ) override final; |
---|
139 | virtual void visit( LabelAddressExpr * labAddressExpr ) override final; |
---|
140 | virtual void visit( const LabelAddressExpr * labAddressExpr ) override final; |
---|
141 | virtual void visit( UntypedMemberExpr * memberExpr ) override final; |
---|
142 | virtual void visit( const UntypedMemberExpr * memberExpr ) override final; |
---|
143 | virtual void visit( MemberExpr * memberExpr ) override final; |
---|
144 | virtual void visit( const MemberExpr * memberExpr ) override final; |
---|
145 | virtual void visit( VariableExpr * variableExpr ) override final; |
---|
146 | virtual void visit( const VariableExpr * variableExpr ) override final; |
---|
147 | virtual void visit( ConstantExpr * constantExpr ) override final; |
---|
148 | virtual void visit( const ConstantExpr * constantExpr ) override final; |
---|
149 | virtual void visit( SizeofExpr * sizeofExpr ) override final; |
---|
150 | virtual void visit( const SizeofExpr * sizeofExpr ) override final; |
---|
151 | virtual void visit( AlignofExpr * alignofExpr ) override final; |
---|
152 | virtual void visit( const AlignofExpr * alignofExpr ) override final; |
---|
153 | virtual void visit( UntypedOffsetofExpr * offsetofExpr ) override final; |
---|
154 | virtual void visit( const UntypedOffsetofExpr * offsetofExpr ) override final; |
---|
155 | virtual void visit( OffsetofExpr * offsetofExpr ) override final; |
---|
156 | virtual void visit( const OffsetofExpr * offsetofExpr ) override final; |
---|
157 | virtual void visit( OffsetPackExpr * offsetPackExpr ) override final; |
---|
158 | virtual void visit( const OffsetPackExpr * offsetPackExpr ) override final; |
---|
159 | virtual void visit( LogicalExpr * logicalExpr ) override final; |
---|
160 | virtual void visit( const LogicalExpr * logicalExpr ) override final; |
---|
161 | virtual void visit( ConditionalExpr * conditionalExpr ) override final; |
---|
162 | virtual void visit( const ConditionalExpr * conditionalExpr ) override final; |
---|
163 | virtual void visit( CommaExpr * commaExpr ) override final; |
---|
164 | virtual void visit( const CommaExpr * commaExpr ) override final; |
---|
165 | virtual void visit( TypeExpr * typeExpr ) override final; |
---|
166 | virtual void visit( const TypeExpr * typeExpr ) override final; |
---|
167 | virtual void visit( AsmExpr * asmExpr ) override final; |
---|
168 | virtual void visit( const AsmExpr * asmExpr ) override final; |
---|
169 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) override final; |
---|
170 | virtual void visit( const ImplicitCopyCtorExpr * impCpCtorExpr ) override final; |
---|
171 | virtual void visit( ConstructorExpr * ctorExpr ) override final; |
---|
172 | virtual void visit( const ConstructorExpr * ctorExpr ) override final; |
---|
173 | virtual void visit( CompoundLiteralExpr * compLitExpr ) override final; |
---|
174 | virtual void visit( const CompoundLiteralExpr * compLitExpr ) override final; |
---|
175 | virtual void visit( RangeExpr * rangeExpr ) override final; |
---|
176 | virtual void visit( const RangeExpr * rangeExpr ) override final; |
---|
177 | virtual void visit( UntypedTupleExpr * tupleExpr ) override final; |
---|
178 | virtual void visit( const UntypedTupleExpr * tupleExpr ) override final; |
---|
179 | virtual void visit( TupleExpr * tupleExpr ) override final; |
---|
180 | virtual void visit( const TupleExpr * tupleExpr ) override final; |
---|
181 | virtual void visit( TupleIndexExpr * tupleExpr ) override final; |
---|
182 | virtual void visit( const TupleIndexExpr * tupleExpr ) override final; |
---|
183 | virtual void visit( TupleAssignExpr * assignExpr ) override final; |
---|
184 | virtual void visit( const TupleAssignExpr * assignExpr ) override final; |
---|
185 | virtual void visit( StmtExpr * stmtExpr ) override final; |
---|
186 | virtual void visit( const StmtExpr * stmtExpr ) override final; |
---|
187 | virtual void visit( UniqueExpr * uniqueExpr ) override final; |
---|
188 | virtual void visit( const UniqueExpr * uniqueExpr ) override final; |
---|
189 | virtual void visit( UntypedInitExpr * initExpr ) override final; |
---|
190 | virtual void visit( const UntypedInitExpr * initExpr ) override final; |
---|
191 | virtual void visit( InitExpr * initExpr ) override final; |
---|
192 | virtual void visit( const InitExpr * initExpr ) override final; |
---|
193 | virtual void visit( DeletedExpr * delExpr ) override final; |
---|
194 | virtual void visit( const DeletedExpr * delExpr ) override final; |
---|
195 | virtual void visit( DefaultArgExpr * argExpr ) override final; |
---|
196 | virtual void visit( const DefaultArgExpr * argExpr ) override final; |
---|
197 | virtual void visit( GenericExpr * genExpr ) override final; |
---|
198 | virtual void visit( const GenericExpr * genExpr ) override final; |
---|
199 | |
---|
200 | virtual void visit( VoidType * basicType ) override final; |
---|
201 | virtual void visit( const VoidType * basicType ) override final; |
---|
202 | virtual void visit( BasicType * basicType ) override final; |
---|
203 | virtual void visit( const BasicType * basicType ) override final; |
---|
204 | virtual void visit( PointerType * pointerType ) override final; |
---|
205 | virtual void visit( const PointerType * pointerType ) override final; |
---|
206 | virtual void visit( ArrayType * arrayType ) override final; |
---|
207 | virtual void visit( const ArrayType * arrayType ) override final; |
---|
208 | virtual void visit( ReferenceType * referenceType ) override final; |
---|
209 | virtual void visit( const ReferenceType * referenceType ) override final; |
---|
210 | virtual void visit( QualifiedType * qualType ) override final; |
---|
211 | virtual void visit( const QualifiedType * qualType ) override final; |
---|
212 | virtual void visit( FunctionType * functionType ) override final; |
---|
213 | virtual void visit( const FunctionType * functionType ) override final; |
---|
214 | virtual void visit( StructInstType * aggregateUseType ) override final; |
---|
215 | virtual void visit( const StructInstType * aggregateUseType ) override final; |
---|
216 | virtual void visit( UnionInstType * aggregateUseType ) override final; |
---|
217 | virtual void visit( const UnionInstType * aggregateUseType ) override final; |
---|
218 | virtual void visit( EnumInstType * aggregateUseType ) override final; |
---|
219 | virtual void visit( const EnumInstType * aggregateUseType ) override final; |
---|
220 | virtual void visit( TraitInstType * aggregateUseType ) override final; |
---|
221 | virtual void visit( const TraitInstType * aggregateUseType ) override final; |
---|
222 | virtual void visit( TypeInstType * aggregateUseType ) override final; |
---|
223 | virtual void visit( const TypeInstType * aggregateUseType ) override final; |
---|
224 | virtual void visit( TupleType * tupleType ) override final; |
---|
225 | virtual void visit( const TupleType * tupleType ) override final; |
---|
226 | virtual void visit( TypeofType * typeofType ) override final; |
---|
227 | virtual void visit( const TypeofType * typeofType ) override final; |
---|
228 | virtual void visit( AttrType * attrType ) override final; |
---|
229 | virtual void visit( const AttrType * attrType ) override final; |
---|
230 | virtual void visit( VarArgsType * varArgsType ) override final; |
---|
231 | virtual void visit( const VarArgsType * varArgsType ) override final; |
---|
232 | virtual void visit( ZeroType * zeroType ) override final; |
---|
233 | virtual void visit( const ZeroType * zeroType ) override final; |
---|
234 | virtual void visit( OneType * oneType ) override final; |
---|
235 | virtual void visit( const OneType * oneType ) override final; |
---|
236 | virtual void visit( GlobalScopeType * globalType ) override final; |
---|
237 | virtual void visit( const GlobalScopeType * globalType ) override final; |
---|
238 | |
---|
239 | virtual void visit( Designation * designation ) override final; |
---|
240 | virtual void visit( const Designation * designation ) override final; |
---|
241 | virtual void visit( SingleInit * singleInit ) override final; |
---|
242 | virtual void visit( const SingleInit * singleInit ) override final; |
---|
243 | virtual void visit( ListInit * listInit ) override final; |
---|
244 | virtual void visit( const ListInit * listInit ) override final; |
---|
245 | virtual void visit( ConstructorInit * ctorInit ) override final; |
---|
246 | virtual void visit( const ConstructorInit * ctorInit ) override final; |
---|
247 | |
---|
248 | virtual void visit( Constant * constant ) override final; |
---|
249 | virtual void visit( const Constant * constant ) override final; |
---|
250 | |
---|
251 | virtual void visit( Attribute * attribute ) override final; |
---|
252 | virtual void visit( const Attribute * attribute ) override final; |
---|
253 | |
---|
254 | virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) override final; |
---|
255 | virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ) override final; |
---|
256 | virtual Declaration * mutate( StructDecl * aggregateDecl ) override final; |
---|
257 | virtual Declaration * mutate( UnionDecl * aggregateDecl ) override final; |
---|
258 | virtual Declaration * mutate( EnumDecl * aggregateDecl ) override final; |
---|
259 | virtual Declaration * mutate( TraitDecl * aggregateDecl ) override final; |
---|
260 | virtual Declaration * mutate( TypeDecl * typeDecl ) override final; |
---|
261 | virtual Declaration * mutate( TypedefDecl * typeDecl ) override final; |
---|
262 | virtual AsmDecl * mutate( AsmDecl * asmDecl ) override final; |
---|
263 | virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) override final; |
---|
264 | |
---|
265 | virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override final; |
---|
266 | virtual Statement * mutate( ExprStmt * exprStmt ) override final; |
---|
267 | virtual Statement * mutate( AsmStmt * asmStmt ) override final; |
---|
268 | virtual Statement * mutate( DirectiveStmt * dirStmt ) override final; |
---|
269 | virtual Statement * mutate( IfStmt * ifStmt ) override final; |
---|
270 | virtual Statement * mutate( WhileStmt * whileStmt ) override final; |
---|
271 | virtual Statement * mutate( ForStmt * forStmt ) override final; |
---|
272 | virtual Statement * mutate( SwitchStmt * switchStmt ) override final; |
---|
273 | virtual Statement * mutate( CaseStmt * caseStmt ) override final; |
---|
274 | virtual Statement * mutate( BranchStmt * branchStmt ) override final; |
---|
275 | virtual Statement * mutate( ReturnStmt * returnStmt ) override final; |
---|
276 | virtual Statement * mutate( ThrowStmt * throwStmt ) override final; |
---|
277 | virtual Statement * mutate( TryStmt * tryStmt ) override final; |
---|
278 | virtual Statement * mutate( CatchStmt * catchStmt ) override final; |
---|
279 | virtual Statement * mutate( FinallyStmt * finallyStmt ) override final; |
---|
280 | virtual Statement * mutate( SuspendStmt * suspendStmt ) override final; |
---|
281 | virtual Statement * mutate( WaitForStmt * waitforStmt ) override final; |
---|
282 | virtual Declaration * mutate( WithStmt * withStmt ) override final; |
---|
283 | virtual NullStmt * mutate( NullStmt * nullStmt ) override final; |
---|
284 | virtual Statement * mutate( DeclStmt * declStmt ) override final; |
---|
285 | virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ) override final; |
---|
286 | |
---|
287 | virtual Expression * mutate( ApplicationExpr * applicationExpr ) override final; |
---|
288 | virtual Expression * mutate( UntypedExpr * untypedExpr ) override final; |
---|
289 | virtual Expression * mutate( NameExpr * nameExpr ) override final; |
---|
290 | virtual Expression * mutate( AddressExpr * addrExpr ) override final; |
---|
291 | virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) override final; |
---|
292 | virtual Expression * mutate( CastExpr * castExpr ) override final; |
---|
293 | virtual Expression * mutate( KeywordCastExpr * castExpr ) override final; |
---|
294 | virtual Expression * mutate( VirtualCastExpr * castExpr ) override final; |
---|
295 | virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override final; |
---|
296 | virtual Expression * mutate( MemberExpr * memberExpr ) override final; |
---|
297 | virtual Expression * mutate( VariableExpr * variableExpr ) override final; |
---|
298 | virtual Expression * mutate( ConstantExpr * constantExpr ) override final; |
---|
299 | virtual Expression * mutate( SizeofExpr * sizeofExpr ) override final; |
---|
300 | virtual Expression * mutate( AlignofExpr * alignofExpr ) override final; |
---|
301 | virtual Expression * mutate( UntypedOffsetofExpr * offsetofExpr ) override final; |
---|
302 | virtual Expression * mutate( OffsetofExpr * offsetofExpr ) override final; |
---|
303 | virtual Expression * mutate( OffsetPackExpr * offsetPackExpr ) override final; |
---|
304 | virtual Expression * mutate( LogicalExpr * logicalExpr ) override final; |
---|
305 | virtual Expression * mutate( ConditionalExpr * conditionalExpr ) override final; |
---|
306 | virtual Expression * mutate( CommaExpr * commaExpr ) override final; |
---|
307 | virtual Expression * mutate( TypeExpr * typeExpr ) override final; |
---|
308 | virtual Expression * mutate( AsmExpr * asmExpr ) override final; |
---|
309 | virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) override final; |
---|
310 | virtual Expression * mutate( ConstructorExpr * ctorExpr ) override final; |
---|
311 | virtual Expression * mutate( CompoundLiteralExpr * compLitExpr ) override final; |
---|
312 | virtual Expression * mutate( RangeExpr * rangeExpr ) override final; |
---|
313 | virtual Expression * mutate( UntypedTupleExpr * tupleExpr ) override final; |
---|
314 | virtual Expression * mutate( TupleExpr * tupleExpr ) override final; |
---|
315 | virtual Expression * mutate( TupleIndexExpr * tupleExpr ) override final; |
---|
316 | virtual Expression * mutate( TupleAssignExpr * assignExpr ) override final; |
---|
317 | virtual Expression * mutate( StmtExpr * stmtExpr ) override final; |
---|
318 | virtual Expression * mutate( UniqueExpr * uniqueExpr ) override final; |
---|
319 | virtual Expression * mutate( UntypedInitExpr * initExpr ) override final; |
---|
320 | virtual Expression * mutate( InitExpr * initExpr ) override final; |
---|
321 | virtual Expression * mutate( DeletedExpr * delExpr ) override final; |
---|
322 | virtual Expression * mutate( DefaultArgExpr * argExpr ) override final; |
---|
323 | virtual Expression * mutate( GenericExpr * genExpr ) override final; |
---|
324 | |
---|
325 | virtual Type * mutate( VoidType * basicType ) override final; |
---|
326 | virtual Type * mutate( BasicType * basicType ) override final; |
---|
327 | virtual Type * mutate( PointerType * pointerType ) override final; |
---|
328 | virtual Type * mutate( ArrayType * arrayType ) override final; |
---|
329 | virtual Type * mutate( ReferenceType * referenceType ) override final; |
---|
330 | virtual Type * mutate( QualifiedType * qualType ) override final; |
---|
331 | virtual Type * mutate( FunctionType * functionType ) override final; |
---|
332 | virtual Type * mutate( StructInstType * aggregateUseType ) override final; |
---|
333 | virtual Type * mutate( UnionInstType * aggregateUseType ) override final; |
---|
334 | virtual Type * mutate( EnumInstType * aggregateUseType ) override final; |
---|
335 | virtual Type * mutate( TraitInstType * aggregateUseType ) override final; |
---|
336 | virtual Type * mutate( TypeInstType * aggregateUseType ) override final; |
---|
337 | virtual Type * mutate( TupleType * tupleType ) override final; |
---|
338 | virtual Type * mutate( TypeofType * typeofType ) override final; |
---|
339 | virtual Type * mutate( AttrType * attrType ) override final; |
---|
340 | virtual Type * mutate( VarArgsType * varArgsType ) override final; |
---|
341 | virtual Type * mutate( ZeroType * zeroType ) override final; |
---|
342 | virtual Type * mutate( OneType * oneType ) override final; |
---|
343 | virtual Type * mutate( GlobalScopeType * globalType ) override final; |
---|
344 | |
---|
345 | virtual Designation * mutate( Designation * designation ) override final; |
---|
346 | virtual Initializer * mutate( SingleInit * singleInit ) override final; |
---|
347 | virtual Initializer * mutate( ListInit * listInit ) override final; |
---|
348 | virtual Initializer * mutate( ConstructorInit * ctorInit ) override final; |
---|
349 | |
---|
350 | virtual Constant * mutate( Constant * constant ) override final; |
---|
351 | |
---|
352 | virtual Attribute * mutate( Attribute * attribute ) override final; |
---|
353 | |
---|
354 | virtual TypeSubstitution * mutate( TypeSubstitution * sub ) final; |
---|
355 | |
---|
356 | private: |
---|
357 | bool inFunction = false; |
---|
358 | |
---|
359 | template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
360 | template<typename pass_t> friend void acceptAll( const std::list< const Declaration * > &decls, PassVisitor< pass_t >& visitor ); |
---|
361 | template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
362 | template< typename TreeType, typename pass_t > friend void maybeAccept_impl( TreeType * tree, PassVisitor< pass_t > & visitor ); |
---|
363 | template< typename TreeType, typename pass_t > friend void maybeAccept_impl( const TreeType * tree, PassVisitor< pass_t > & visitor ); |
---|
364 | template< typename TreeType, typename pass_t > friend void maybeMutate_impl( TreeType *& tree, PassVisitor< pass_t > & mutator ); |
---|
365 | template< typename Container, typename pass_t > friend void maybeAccept_impl( Container & container, PassVisitor< pass_t > & visitor ); |
---|
366 | template< typename Container, typename pass_t > friend void maybeAccept_impl( const Container & container, PassVisitor< pass_t > & visitor ); |
---|
367 | template< typename Container, typename pass_t > friend void maybeMutate_impl( Container & container, PassVisitor< pass_t > & mutator ); |
---|
368 | |
---|
369 | template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
370 | template<typename node_type> void call_previsit ( const node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
371 | template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
372 | template<typename node_type> void call_postvisit( const node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
373 | |
---|
374 | template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); } |
---|
375 | template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); } |
---|
376 | |
---|
377 | void call_beginScope() { begin_scope_impl( pass, 0 ); } |
---|
378 | void call_endScope () { end_scope_impl ( pass, 0 ); } |
---|
379 | |
---|
380 | void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } |
---|
381 | |
---|
382 | template< typename func_t > |
---|
383 | void handleStatementList( std::list< Statement * > & statements, func_t func ); |
---|
384 | void visitStatementList ( std::list< Statement* > &statements ); |
---|
385 | void mutateStatementList( std::list< Statement* > &statements ); |
---|
386 | void visitStatementList ( const std::list< Statement * > & statements ); |
---|
387 | |
---|
388 | template< typename func_t > |
---|
389 | Statement * handleStatement( Statement * stmt, func_t func ); |
---|
390 | Statement * visitStatement ( Statement * stmt ); |
---|
391 | Statement * mutateStatement( Statement * stmt ); |
---|
392 | void visitStatement ( const Statement * stmt ); |
---|
393 | |
---|
394 | template< typename func_t > |
---|
395 | Expression * handleExpression( Expression * expr, func_t func ); |
---|
396 | Expression * visitExpression ( Expression * expr ); |
---|
397 | Expression * mutateExpression( Expression * expr ); |
---|
398 | void visitExpression ( const Expression * expr ); |
---|
399 | |
---|
400 | |
---|
401 | auto get_env_ptr () -> decltype(env_impl( pass, 0)) { return env_impl( pass, 0); } |
---|
402 | std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } |
---|
403 | std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } |
---|
404 | std::list< Declaration* > * get_beforeDecls() { return declsToAddBefore_impl( pass, 0); } |
---|
405 | std::list< Declaration* > * get_afterDecls () { return declsToAddAfter_impl ( pass, 0); } |
---|
406 | |
---|
407 | bool get_visit_children () { bool_ref * ptr = visit_children_impl(pass, 0); return ptr ? *ptr : true; } |
---|
408 | bool_ref * get_visit_children_ptr() { return visit_children_impl(pass, 0); } |
---|
409 | |
---|
410 | void indexerScopeEnter () { indexer_impl_enterScope ( pass, 0 ); } |
---|
411 | void indexerScopeLeave () { indexer_impl_leaveScope ( pass, 0 ); } |
---|
412 | void indexerAddId ( const DeclarationWithType * node ) { indexer_impl_addId ( pass, 0, node ); } |
---|
413 | void indexerAddType ( const NamedTypeDecl * node ) { indexer_impl_addType ( pass, 0, node ); } |
---|
414 | void indexerAddStruct ( const std::string & id ) { indexer_impl_addStruct ( pass, 0, id ); } |
---|
415 | void indexerAddStruct ( const StructDecl * node ) { indexer_impl_addStruct ( pass, 0, node ); } |
---|
416 | void indexerAddStructFwd( const StructDecl * node ) { indexer_impl_addStructFwd( pass, 0, node ); } |
---|
417 | void indexerAddEnum ( const EnumDecl * node ) { indexer_impl_addEnum ( pass, 0, node ); } |
---|
418 | void indexerAddUnion ( const std::string & id ) { indexer_impl_addUnion ( pass, 0, id ); } |
---|
419 | void indexerAddUnion ( const UnionDecl * node ) { indexer_impl_addUnion ( pass, 0, node ); } |
---|
420 | void indexerAddUnionFwd ( const UnionDecl * node ) { indexer_impl_addUnionFwd ( pass, 0, node ); } |
---|
421 | void indexerAddTrait ( const TraitDecl * node ) { indexer_impl_addTrait ( pass, 0, node ); } |
---|
422 | void indexerAddWith ( const std::list< Expression * > & exprs, const Declaration * withStmt ) { indexer_impl_addWith( pass, 0, exprs, withStmt ); } |
---|
423 | |
---|
424 | |
---|
425 | template< typename TreeType, typename VisitorType > |
---|
426 | friend inline void indexerScopedAccept( TreeType * tree, VisitorType & visitor ); |
---|
427 | |
---|
428 | template< typename TreeType, typename VisitorType > |
---|
429 | friend inline void indexerScopedAccept( const TreeType * tree, VisitorType & visitor ); |
---|
430 | |
---|
431 | template< typename TreeType, typename VisitorType > |
---|
432 | friend inline void indexerScopedMutate( TreeType *& tree, VisitorType & visitor ); |
---|
433 | }; |
---|
434 | |
---|
435 | template<typename pass_type, typename T> |
---|
436 | void GuardValue( pass_type * pass, T& val ) { |
---|
437 | pass->at_cleanup( [ val ]( void * newVal ) { |
---|
438 | * static_cast< T * >( newVal ) = val; |
---|
439 | }, static_cast< void * >( & val ) ); |
---|
440 | } |
---|
441 | |
---|
442 | class WithTypeSubstitution { |
---|
443 | protected: |
---|
444 | WithTypeSubstitution() = default; |
---|
445 | ~WithTypeSubstitution() = default; |
---|
446 | |
---|
447 | public: |
---|
448 | TypeSubstitution * env = nullptr; |
---|
449 | }; |
---|
450 | |
---|
451 | class WithConstTypeSubstitution { |
---|
452 | protected: |
---|
453 | WithConstTypeSubstitution() = default; |
---|
454 | ~WithConstTypeSubstitution() = default; |
---|
455 | |
---|
456 | public: |
---|
457 | const TypeSubstitution * env = nullptr; |
---|
458 | }; |
---|
459 | |
---|
460 | class WithStmtsToAdd { |
---|
461 | protected: |
---|
462 | WithStmtsToAdd() = default; |
---|
463 | ~WithStmtsToAdd() = default; |
---|
464 | |
---|
465 | public: |
---|
466 | std::list< Statement* > stmtsToAddBefore; |
---|
467 | std::list< Statement* > stmtsToAddAfter; |
---|
468 | }; |
---|
469 | |
---|
470 | class WithDeclsToAdd { |
---|
471 | protected: |
---|
472 | WithDeclsToAdd() = default; |
---|
473 | ~WithDeclsToAdd() { |
---|
474 | assert( declsToAddBefore.empty() ); |
---|
475 | } |
---|
476 | |
---|
477 | public: |
---|
478 | std::list< Declaration* > declsToAddBefore; |
---|
479 | std::list< Declaration* > declsToAddAfter; |
---|
480 | }; |
---|
481 | |
---|
482 | class WithShortCircuiting { |
---|
483 | protected: |
---|
484 | WithShortCircuiting() = default; |
---|
485 | ~WithShortCircuiting() = default; |
---|
486 | |
---|
487 | public: |
---|
488 | bool_ref visit_children; |
---|
489 | }; |
---|
490 | |
---|
491 | class WithGuards { |
---|
492 | protected: |
---|
493 | WithGuards() = default; |
---|
494 | ~WithGuards() = default; |
---|
495 | |
---|
496 | public: |
---|
497 | at_cleanup_t at_cleanup; |
---|
498 | |
---|
499 | template< typename T > |
---|
500 | void GuardValue( T& val ) { |
---|
501 | at_cleanup( [ val ]( void * newVal ) { |
---|
502 | * static_cast< T * >( newVal ) = val; |
---|
503 | }, static_cast< void * >( & val ) ); |
---|
504 | } |
---|
505 | |
---|
506 | template< typename T > |
---|
507 | void GuardScope( T& val ) { |
---|
508 | val.beginScope(); |
---|
509 | at_cleanup( []( void * val ) { |
---|
510 | static_cast< T * >( val )->endScope(); |
---|
511 | }, static_cast< void * >( & val ) ); |
---|
512 | } |
---|
513 | |
---|
514 | template< typename Func > |
---|
515 | void GuardAction( Func func ) { |
---|
516 | at_cleanup( [func](__attribute__((unused)) void *) { func(); }, nullptr ); |
---|
517 | } |
---|
518 | }; |
---|
519 | |
---|
520 | template<typename pass_type> |
---|
521 | class WithVisitorRef { |
---|
522 | protected: |
---|
523 | WithVisitorRef() {} |
---|
524 | ~WithVisitorRef() {} |
---|
525 | |
---|
526 | public: |
---|
527 | PassVisitor<pass_type> * const visitor = nullptr; |
---|
528 | }; |
---|
529 | |
---|
530 | class WithIndexer { |
---|
531 | protected: |
---|
532 | WithIndexer() {} |
---|
533 | ~WithIndexer() {} |
---|
534 | |
---|
535 | public: |
---|
536 | SymTab::Indexer indexer; |
---|
537 | }; |
---|
538 | |
---|
539 | #include "Common/Stats.h" |
---|
540 | |
---|
541 | extern struct PassVisitorStats { |
---|
542 | size_t depth = 0; |
---|
543 | Stats::Counters::MaxCounter<double> * max = nullptr; |
---|
544 | Stats::Counters::AverageCounter<double> * avg = nullptr; |
---|
545 | } pass_visitor_stats; |
---|
546 | |
---|
547 | #include "SynTree/TypeSubstitution.h" |
---|
548 | #include "PassVisitor.impl.h" |
---|