@@ -52,7 +52,11 @@ class ASTContext;
52
52
// / WalkUpFromX or post-order traversal).
53
53
// /
54
54
// / \see RecursiveASTVisitor.
55
- class DynamicRecursiveASTVisitor {
55
+ template <bool IsConst> class DynamicRecursiveASTVisitorBase {
56
+ protected:
57
+ template <typename ASTNode>
58
+ using MaybeConst = std::conditional_t <IsConst, const ASTNode, ASTNode>;
59
+
56
60
public:
57
61
// / Whether this visitor should recurse into template instantiations.
58
62
bool ShouldVisitTemplateInstantiations = false ;
@@ -68,36 +72,38 @@ class DynamicRecursiveASTVisitor {
68
72
bool ShouldVisitLambdaBody = true ;
69
73
70
74
protected:
71
- DynamicRecursiveASTVisitor () = default ;
72
- DynamicRecursiveASTVisitor (DynamicRecursiveASTVisitor &&) = default ;
73
- DynamicRecursiveASTVisitor (const DynamicRecursiveASTVisitor &) = default ;
74
- DynamicRecursiveASTVisitor &
75
- operator =(DynamicRecursiveASTVisitor &&) = default ;
76
- DynamicRecursiveASTVisitor &
77
- operator =(const DynamicRecursiveASTVisitor &) = default ;
75
+ DynamicRecursiveASTVisitorBase () = default ;
76
+ DynamicRecursiveASTVisitorBase (DynamicRecursiveASTVisitorBase &&) = default ;
77
+ DynamicRecursiveASTVisitorBase (const DynamicRecursiveASTVisitorBase &) =
78
+ default ;
79
+ DynamicRecursiveASTVisitorBase &
80
+ operator =(DynamicRecursiveASTVisitorBase &&) = default ;
81
+ DynamicRecursiveASTVisitorBase &
82
+ operator =(const DynamicRecursiveASTVisitorBase &) = default ;
78
83
79
84
public:
80
85
virtual void anchor ();
81
- virtual ~DynamicRecursiveASTVisitor () = default ;
86
+ virtual ~DynamicRecursiveASTVisitorBase () = default ;
82
87
83
88
// / Recursively visits an entire AST, starting from the TranslationUnitDecl.
84
89
// / \returns false if visitation was terminated early.
85
- virtual bool TraverseAST (ASTContext &AST);
90
+ virtual bool TraverseAST (MaybeConst< ASTContext> &AST);
86
91
87
92
// / Recursively visit an attribute, by dispatching to
88
93
// / Traverse*Attr() based on the argument's dynamic type.
89
94
// /
90
95
// / \returns false if the visitation was terminated early, true
91
96
// / otherwise (including when the argument is a Null type location).
92
- virtual bool TraverseAttr (Attr *At);
97
+ virtual bool TraverseAttr (MaybeConst< Attr> *At);
93
98
94
99
// / Recursively visit a constructor initializer. This
95
100
// / automatically dispatches to another visitor for the initializer
96
101
// / expression, but not for the name of the initializer, so may
97
102
// / be overridden for clients that need access to the name.
98
103
// /
99
104
// / \returns false if the visitation was terminated early, true otherwise.
100
- virtual bool TraverseConstructorInitializer (CXXCtorInitializer *Init);
105
+ virtual bool
106
+ TraverseConstructorInitializer (MaybeConst<CXXCtorInitializer> *Init);
101
107
102
108
// / Recursively visit a base specifier. This can be overridden by a
103
109
// / subclass.
@@ -110,7 +116,7 @@ class DynamicRecursiveASTVisitor {
110
116
// /
111
117
// / \returns false if the visitation was terminated early, true
112
118
// / otherwise (including when the argument is NULL).
113
- virtual bool TraverseDecl (Decl *D);
119
+ virtual bool TraverseDecl (MaybeConst< Decl> *D);
114
120
115
121
// / Recursively visit a name with its location information.
116
122
// /
@@ -121,13 +127,15 @@ class DynamicRecursiveASTVisitor {
121
127
// / will be used to initialize the capture.
122
128
// /
123
129
// / \returns false if the visitation was terminated early, true otherwise.
124
- virtual bool TraverseLambdaCapture (LambdaExpr *LE, const LambdaCapture *C,
125
- Expr *Init);
130
+ virtual bool TraverseLambdaCapture (MaybeConst<LambdaExpr> *LE,
131
+ const LambdaCapture *C,
132
+ MaybeConst<Expr> *Init);
126
133
127
134
// / Recursively visit a C++ nested-name-specifier.
128
135
// /
129
136
// / \returns false if the visitation was terminated early, true otherwise.
130
- virtual bool TraverseNestedNameSpecifier (NestedNameSpecifier *NNS);
137
+ virtual bool
138
+ TraverseNestedNameSpecifier (MaybeConst<NestedNameSpecifier> *NNS);
131
139
132
140
// / Recursively visit a C++ nested-name-specifier with location
133
141
// / information.
@@ -140,7 +148,7 @@ class DynamicRecursiveASTVisitor {
140
148
// /
141
149
// / \returns false if the visitation was terminated early, true
142
150
// / otherwise (including when the argument is nullptr).
143
- virtual bool TraverseStmt (Stmt *S);
151
+ virtual bool TraverseStmt (MaybeConst< Stmt> *S);
144
152
145
153
// / Recursively visit a template argument and dispatch to the
146
154
// / appropriate method for the argument type.
@@ -190,74 +198,86 @@ class DynamicRecursiveASTVisitor {
190
198
191
199
// / Traverse a concept (requirement).
192
200
virtual bool TraverseTypeConstraint (const TypeConstraint *C);
193
- virtual bool TraverseConceptRequirement (concepts::Requirement *R);
194
- virtual bool TraverseConceptTypeRequirement (concepts::TypeRequirement *R);
195
- virtual bool TraverseConceptExprRequirement (concepts::ExprRequirement *R);
196
- virtual bool TraverseConceptNestedRequirement (concepts::NestedRequirement *R);
197
- virtual bool TraverseConceptReference (ConceptReference *CR);
198
- virtual bool VisitConceptReference (ConceptReference *CR) { return true ; }
201
+ virtual bool TraverseConceptRequirement (MaybeConst<concepts::Requirement> *R);
202
+
203
+ virtual bool
204
+ TraverseConceptTypeRequirement (MaybeConst<concepts::TypeRequirement> *R);
205
+
206
+ virtual bool
207
+ TraverseConceptExprRequirement (MaybeConst<concepts::ExprRequirement> *R);
208
+
209
+ virtual bool
210
+ TraverseConceptNestedRequirement (MaybeConst<concepts::NestedRequirement> *R);
211
+
212
+ virtual bool TraverseConceptReference (MaybeConst<ConceptReference> *CR);
213
+ virtual bool VisitConceptReference (MaybeConst<ConceptReference> *CR) {
214
+ return true ;
215
+ }
199
216
200
217
// / Visit a node.
201
- virtual bool VisitAttr (Attr *A) { return true ; }
202
- virtual bool VisitDecl (Decl *D) { return true ; }
203
- virtual bool VisitStmt (Stmt *S) { return true ; }
204
- virtual bool VisitType (Type *T) { return true ; }
218
+ virtual bool VisitAttr (MaybeConst< Attr> *A) { return true ; }
219
+ virtual bool VisitDecl (MaybeConst< Decl> *D) { return true ; }
220
+ virtual bool VisitStmt (MaybeConst< Stmt> *S) { return true ; }
221
+ virtual bool VisitType (MaybeConst< Type> *T) { return true ; }
205
222
virtual bool VisitTypeLoc (TypeLoc TL) { return true ; }
206
223
207
224
// / Walk up from a node.
208
- bool WalkUpFromDecl (Decl *D) { return VisitDecl (D); }
209
- bool WalkUpFromStmt (Stmt *S) { return VisitStmt (S); }
210
- bool WalkUpFromType (Type *T) { return VisitType (T); }
225
+ bool WalkUpFromDecl (MaybeConst< Decl> *D) { return VisitDecl (D); }
226
+ bool WalkUpFromStmt (MaybeConst< Stmt> *S) { return VisitStmt (S); }
227
+ bool WalkUpFromType (MaybeConst< Type> *T) { return VisitType (T); }
211
228
bool WalkUpFromTypeLoc (TypeLoc TL) { return VisitTypeLoc (TL); }
212
229
213
230
// / Invoked before visiting a statement or expression via data recursion.
214
231
// /
215
232
// / \returns false to skip visiting the node, true otherwise.
216
- virtual bool dataTraverseStmtPre (Stmt *S) { return true ; }
233
+ virtual bool dataTraverseStmtPre (MaybeConst< Stmt> *S) { return true ; }
217
234
218
235
// / Invoked after visiting a statement or expression via data recursion.
219
236
// / This is not invoked if the previously invoked \c dataTraverseStmtPre
220
237
// / returned false.
221
238
// /
222
239
// / \returns false if the visitation was terminated early, true otherwise.
223
- virtual bool dataTraverseStmtPost (Stmt *S) { return true ; }
224
- virtual bool dataTraverseNode (Stmt *S);
240
+ virtual bool dataTraverseStmtPost (MaybeConst< Stmt> *S) { return true ; }
241
+ virtual bool dataTraverseNode (MaybeConst< Stmt> *S);
225
242
226
243
#define DEF_TRAVERSE_TMPL_INST (kind ) \
227
- virtual bool TraverseTemplateInstantiations (kind##TemplateDecl *D);
244
+ virtual bool TraverseTemplateInstantiations ( \
245
+ MaybeConst<kind##TemplateDecl> *D);
228
246
DEF_TRAVERSE_TMPL_INST (Class)
229
247
DEF_TRAVERSE_TMPL_INST (Var)
230
248
DEF_TRAVERSE_TMPL_INST (Function)
231
249
#undef DEF_TRAVERSE_TMPL_INST
232
250
233
251
// Decls.
234
252
#define ABSTRACT_DECL (DECL )
235
- #define DECL (CLASS, BASE ) virtual bool Traverse##CLASS##Decl(CLASS##Decl *D);
253
+ #define DECL (CLASS, BASE ) \
254
+ virtual bool Traverse##CLASS##Decl(MaybeConst<CLASS##Decl> *D);
236
255
#include " clang/AST/DeclNodes.inc"
237
256
238
257
#define DECL (CLASS, BASE ) \
239
- bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D); \
240
- virtual bool Visit##CLASS##Decl(CLASS##Decl *D) { return true ; }
258
+ bool WalkUpFrom##CLASS##Decl(MaybeConst< CLASS##Decl> *D); \
259
+ virtual bool Visit##CLASS##Decl(MaybeConst< CLASS##Decl> *D) { return true ; }
241
260
#include " clang/AST/DeclNodes.inc"
242
261
243
262
// Stmts.
244
263
#define ABSTRACT_STMT (STMT )
245
- #define STMT (CLASS, PARENT ) virtual bool Traverse##CLASS(CLASS *S);
264
+ #define STMT (CLASS, PARENT ) virtual bool Traverse##CLASS(MaybeConst< CLASS> *S);
246
265
#include " clang/AST/StmtNodes.inc"
247
266
248
267
#define STMT (CLASS, PARENT ) \
249
- bool WalkUpFrom##CLASS(CLASS *S); \
250
- virtual bool Visit##CLASS(CLASS *S) { return true ; }
268
+ bool WalkUpFrom##CLASS(MaybeConst< CLASS> *S); \
269
+ virtual bool Visit##CLASS(MaybeConst< CLASS> *S) { return true ; }
251
270
#include " clang/AST/StmtNodes.inc"
252
271
253
272
// Types.
254
273
#define ABSTRACT_TYPE (CLASS, BASE )
255
- #define TYPE (CLASS, BASE ) virtual bool Traverse##CLASS##Type(CLASS##Type *T);
274
+ #define TYPE (CLASS, BASE ) \
275
+ virtual bool Traverse##CLASS##Type(MaybeConst<CLASS##Type> *T);
256
276
#include " clang/AST/TypeNodes.inc"
257
277
258
278
#define TYPE (CLASS, BASE ) \
259
- bool WalkUpFrom##CLASS##Type(CLASS##Type *T); \
260
- virtual bool Visit##CLASS##Type(CLASS##Type *T) { return true ; }
279
+ bool WalkUpFrom##CLASS##Type(MaybeConst< CLASS##Type> *T); \
280
+ virtual bool Visit##CLASS##Type(MaybeConst< CLASS##Type> *T) { return true ; }
261
281
#include " clang/AST/TypeNodes.inc"
262
282
263
283
// TypeLocs.
@@ -271,6 +291,14 @@ class DynamicRecursiveASTVisitor {
271
291
virtual bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true ; }
272
292
#include " clang/AST/TypeLocNodes.def"
273
293
};
294
+
295
+ extern template class DynamicRecursiveASTVisitorBase <false >;
296
+ extern template class DynamicRecursiveASTVisitorBase <true >;
297
+
298
+ using DynamicRecursiveASTVisitor =
299
+ DynamicRecursiveASTVisitorBase</* Const=*/ false >;
300
+ using ConstDynamicRecursiveASTVisitor =
301
+ DynamicRecursiveASTVisitorBase</* Const=*/ true >;
274
302
} // namespace clang
275
303
276
304
#endif // LLVM_CLANG_AST_DYNAMIC_RECURSIVE_AST_VISITOR_H
0 commit comments