xref: /freebsd/contrib/llvm-project/clang/include/clang/AST/CommentVisitor.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===- CommentVisitor.h - Visitor for Comment subclasses --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_AST_COMMENTVISITOR_H
10 #define LLVM_CLANG_AST_COMMENTVISITOR_H
11 
12 #include "clang/AST/Comment.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/ErrorHandling.h"
15 
16 namespace clang {
17 namespace comments {
18 template <template <typename> class Ptr, typename ImplClass,
19           typename RetTy = void, class... ParamTys>
20 class CommentVisitorBase {
21 public:
22 #define PTR(CLASS) typename Ptr<CLASS>::type
23 #define DISPATCH(NAME, CLASS)                                                  \
24   return static_cast<ImplClass *>(this)->visit##NAME(                          \
25       static_cast<PTR(CLASS)>(C), std::forward<ParamTys>(P)...)
26 
27   RetTy visit(PTR(Comment) C, ParamTys... P) {
28     if (!C)
29       return RetTy();
30 
31     switch (C->getCommentKind()) {
32     default: llvm_unreachable("Unknown comment kind!");
33 #define ABSTRACT_COMMENT(COMMENT)
34 #define COMMENT(CLASS, PARENT)                                                 \
35   case CommentKind::CLASS:                                                     \
36     DISPATCH(CLASS, CLASS);
37 #include "clang/AST/CommentNodes.inc"
38 #undef ABSTRACT_COMMENT
39 #undef COMMENT
40     }
41   }
42 
43   // If the derived class does not implement a certain Visit* method, fall back
44   // on Visit* method for the superclass.
45 #define ABSTRACT_COMMENT(COMMENT) COMMENT
46 #define COMMENT(CLASS, PARENT)                                                 \
47   RetTy visit##CLASS(PTR(CLASS) C, ParamTys... P) { DISPATCH(PARENT, PARENT); }
48 #include "clang/AST/CommentNodes.inc"
49 #undef ABSTRACT_COMMENT
50 #undef COMMENT
51 
52   RetTy visitComment(PTR(Comment) C, ParamTys... P) { return RetTy(); }
53 
54 #undef PTR
55 #undef DISPATCH
56 };
57 
58 template <typename ImplClass, typename RetTy = void, class... ParamTys>
59 class CommentVisitor : public CommentVisitorBase<std::add_pointer, ImplClass,
60                                                  RetTy, ParamTys...> {};
61 
62 template <typename ImplClass, typename RetTy = void, class... ParamTys>
63 class ConstCommentVisitor
64     : public CommentVisitorBase<llvm::make_const_ptr, ImplClass, RetTy,
65                                 ParamTys...> {};
66 
67 } // namespace comments
68 } // namespace clang
69 
70 #endif // LLVM_CLANG_AST_COMMENTVISITOR_H
71