@@ -52,7 +52,11 @@ class ASTContext;
5252// / WalkUpFromX or post-order traversal).
5353// /
5454// / \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+
5660public:
5761 // / Whether this visitor should recurse into template instantiations.
5862 bool ShouldVisitTemplateInstantiations = false ;
@@ -68,36 +72,38 @@ class DynamicRecursiveASTVisitor {
6872 bool ShouldVisitLambdaBody = true ;
6973
7074protected:
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 ;
7883
7984public:
8085 virtual void anchor ();
81- virtual ~DynamicRecursiveASTVisitor () = default ;
86+ virtual ~DynamicRecursiveASTVisitorBase () = default ;
8287
8388 // / Recursively visits an entire AST, starting from the TranslationUnitDecl.
8489 // / \returns false if visitation was terminated early.
85- virtual bool TraverseAST (ASTContext &AST);
90+ virtual bool TraverseAST (MaybeConst< ASTContext> &AST);
8691
8792 // / Recursively visit an attribute, by dispatching to
8893 // / Traverse*Attr() based on the argument's dynamic type.
8994 // /
9095 // / \returns false if the visitation was terminated early, true
9196 // / otherwise (including when the argument is a Null type location).
92- virtual bool TraverseAttr (Attr *At);
97+ virtual bool TraverseAttr (MaybeConst< Attr> *At);
9398
9499 // / Recursively visit a constructor initializer. This
95100 // / automatically dispatches to another visitor for the initializer
96101 // / expression, but not for the name of the initializer, so may
97102 // / be overridden for clients that need access to the name.
98103 // /
99104 // / \returns false if the visitation was terminated early, true otherwise.
100- virtual bool TraverseConstructorInitializer (CXXCtorInitializer *Init);
105+ virtual bool
106+ TraverseConstructorInitializer (MaybeConst<CXXCtorInitializer> *Init);
101107
102108 // / Recursively visit a base specifier. This can be overridden by a
103109 // / subclass.
@@ -110,7 +116,7 @@ class DynamicRecursiveASTVisitor {
110116 // /
111117 // / \returns false if the visitation was terminated early, true
112118 // / otherwise (including when the argument is NULL).
113- virtual bool TraverseDecl (Decl *D);
119+ virtual bool TraverseDecl (MaybeConst< Decl> *D);
114120
115121 // / Recursively visit a name with its location information.
116122 // /
@@ -121,13 +127,14 @@ class DynamicRecursiveASTVisitor {
121127 // / will be used to initialize the capture.
122128 // /
123129 // / \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);
126133
127134 // / Recursively visit a C++ nested-name-specifier.
128135 // /
129136 // / \returns false if the visitation was terminated early, true otherwise.
130- virtual bool TraverseNestedNameSpecifier (NestedNameSpecifier *NNS);
137+ virtual bool TraverseNestedNameSpecifier (MaybeConst< NestedNameSpecifier> *NNS);
131138
132139 // / Recursively visit a C++ nested-name-specifier with location
133140 // / information.
@@ -140,7 +147,7 @@ class DynamicRecursiveASTVisitor {
140147 // /
141148 // / \returns false if the visitation was terminated early, true
142149 // / otherwise (including when the argument is nullptr).
143- virtual bool TraverseStmt (Stmt *S);
150+ virtual bool TraverseStmt (MaybeConst< Stmt> *S);
144151
145152 // / Recursively visit a template argument and dispatch to the
146153 // / appropriate method for the argument type.
@@ -190,74 +197,86 @@ class DynamicRecursiveASTVisitor {
190197
191198 // / Traverse a concept (requirement).
192199 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 ; }
200+ virtual bool TraverseConceptRequirement (MaybeConst<concepts::Requirement> *R);
201+
202+ virtual bool
203+ TraverseConceptTypeRequirement (MaybeConst<concepts::TypeRequirement> *R);
204+
205+ virtual bool
206+ TraverseConceptExprRequirement (MaybeConst<concepts::ExprRequirement> *R);
207+
208+ virtual bool
209+ TraverseConceptNestedRequirement (MaybeConst<concepts::NestedRequirement> *R);
210+
211+ virtual bool TraverseConceptReference (MaybeConst<ConceptReference> *CR);
212+ virtual bool VisitConceptReference (MaybeConst<ConceptReference> *CR) {
213+ return true ;
214+ }
199215
200216 // / 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 ; }
217+ virtual bool VisitAttr (MaybeConst< Attr> *A) { return true ; }
218+ virtual bool VisitDecl (MaybeConst< Decl> *D) { return true ; }
219+ virtual bool VisitStmt (MaybeConst< Stmt> *S) { return true ; }
220+ virtual bool VisitType (MaybeConst< Type> *T) { return true ; }
205221 virtual bool VisitTypeLoc (TypeLoc TL) { return true ; }
206222
207223 // / 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); }
224+ bool WalkUpFromDecl (MaybeConst< Decl> *D) { return VisitDecl (D); }
225+ bool WalkUpFromStmt (MaybeConst< Stmt> *S) { return VisitStmt (S); }
226+ bool WalkUpFromType (MaybeConst< Type> *T) { return VisitType (T); }
211227 bool WalkUpFromTypeLoc (TypeLoc TL) { return VisitTypeLoc (TL); }
212228
213229 // / Invoked before visiting a statement or expression via data recursion.
214230 // /
215231 // / \returns false to skip visiting the node, true otherwise.
216- virtual bool dataTraverseStmtPre (Stmt *S) { return true ; }
232+ virtual bool dataTraverseStmtPre (MaybeConst< Stmt> *S) { return true ; }
217233
218234 // / Invoked after visiting a statement or expression via data recursion.
219235 // / This is not invoked if the previously invoked \c dataTraverseStmtPre
220236 // / returned false.
221237 // /
222238 // / \returns false if the visitation was terminated early, true otherwise.
223- virtual bool dataTraverseStmtPost (Stmt *S) { return true ; }
224- virtual bool dataTraverseNode (Stmt *S);
239+ virtual bool dataTraverseStmtPost (MaybeConst< Stmt> *S) { return true ; }
240+ virtual bool dataTraverseNode (MaybeConst< Stmt> *S);
225241
226242#define DEF_TRAVERSE_TMPL_INST (kind ) \
227- virtual bool TraverseTemplateInstantiations (kind##TemplateDecl *D);
243+ virtual bool TraverseTemplateInstantiations ( \
244+ MaybeConst<kind##TemplateDecl> *D);
228245 DEF_TRAVERSE_TMPL_INST (Class)
229246 DEF_TRAVERSE_TMPL_INST (Var)
230247 DEF_TRAVERSE_TMPL_INST (Function)
231248#undef DEF_TRAVERSE_TMPL_INST
232249
233250 // Decls.
234251#define ABSTRACT_DECL (DECL )
235- #define DECL (CLASS, BASE ) virtual bool Traverse##CLASS##Decl(CLASS##Decl *D);
252+ #define DECL (CLASS, BASE ) \
253+ virtual bool Traverse##CLASS##Decl(MaybeConst<CLASS##Decl> *D);
236254#include " clang/AST/DeclNodes.inc"
237255
238256#define DECL (CLASS, BASE ) \
239- bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D); \
240- virtual bool Visit##CLASS##Decl(CLASS##Decl *D) { return true ; }
257+ bool WalkUpFrom##CLASS##Decl(MaybeConst< CLASS##Decl> *D); \
258+ virtual bool Visit##CLASS##Decl(MaybeConst< CLASS##Decl> *D) { return true ; }
241259#include " clang/AST/DeclNodes.inc"
242260
243261 // Stmts.
244262#define ABSTRACT_STMT (STMT )
245- #define STMT (CLASS, PARENT ) virtual bool Traverse##CLASS(CLASS *S);
263+ #define STMT (CLASS, PARENT ) virtual bool Traverse##CLASS(MaybeConst< CLASS> *S);
246264#include " clang/AST/StmtNodes.inc"
247265
248266#define STMT (CLASS, PARENT ) \
249- bool WalkUpFrom##CLASS(CLASS *S); \
250- virtual bool Visit##CLASS(CLASS *S) { return true ; }
267+ bool WalkUpFrom##CLASS(MaybeConst< CLASS> *S); \
268+ virtual bool Visit##CLASS(MaybeConst< CLASS> *S) { return true ; }
251269#include " clang/AST/StmtNodes.inc"
252270
253271 // Types.
254272#define ABSTRACT_TYPE (CLASS, BASE )
255- #define TYPE (CLASS, BASE ) virtual bool Traverse##CLASS##Type(CLASS##Type *T);
273+ #define TYPE (CLASS, BASE ) \
274+ virtual bool Traverse##CLASS##Type(MaybeConst<CLASS##Type> *T);
256275#include " clang/AST/TypeNodes.inc"
257276
258277#define TYPE (CLASS, BASE ) \
259- bool WalkUpFrom##CLASS##Type(CLASS##Type *T); \
260- virtual bool Visit##CLASS##Type(CLASS##Type *T) { return true ; }
278+ bool WalkUpFrom##CLASS##Type(MaybeConst< CLASS##Type> *T); \
279+ virtual bool Visit##CLASS##Type(MaybeConst< CLASS##Type> *T) { return true ; }
261280#include " clang/AST/TypeNodes.inc"
262281
263282 // TypeLocs.
@@ -271,6 +290,14 @@ class DynamicRecursiveASTVisitor {
271290 virtual bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true ; }
272291#include " clang/AST/TypeLocNodes.def"
273292};
293+
294+ extern template class DynamicRecursiveASTVisitorBase <false >;
295+ extern template class DynamicRecursiveASTVisitorBase <true >;
296+
297+ using DynamicRecursiveASTVisitor =
298+ DynamicRecursiveASTVisitorBase</* Const=*/ false >;
299+ using ConstDynamicRecursiveASTVisitor =
300+ DynamicRecursiveASTVisitorBase</* Const=*/ true >;
274301} // namespace clang
275302
276303#endif // LLVM_CLANG_AST_DYNAMIC_RECURSIVE_AST_VISITOR_H
0 commit comments