1 //===- CommentVisitor.h - Visitor for Comment subclasses --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_AST_COMMENTVISITOR_H
10 #define LLVM_CLANG_AST_COMMENTVISITOR_H
12 #include "clang/AST/Comment.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/ErrorHandling.h"
18 template <template <typename> class Ptr, typename ImplClass,
19 typename RetTy = void, class... ParamTys>
20 class CommentVisitorBase {
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)...)
27 RetTy visit(PTR(Comment) C, ParamTys... P) {
31 switch (C->getCommentKind()) {
32 default: llvm_unreachable("Unknown comment kind!");
33 #define ABSTRACT_COMMENT(COMMENT)
34 #define COMMENT(CLASS, PARENT) \
35 case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS);
36 #include "clang/AST/CommentNodes.inc"
37 #undef ABSTRACT_COMMENT
42 // If the derived class does not implement a certain Visit* method, fall back
43 // on Visit* method for the superclass.
44 #define ABSTRACT_COMMENT(COMMENT) COMMENT
45 #define COMMENT(CLASS, PARENT) \
46 RetTy visit##CLASS(PTR(CLASS) C, ParamTys... P) { DISPATCH(PARENT, PARENT); }
47 #include "clang/AST/CommentNodes.inc"
48 #undef ABSTRACT_COMMENT
51 RetTy visitComment(PTR(Comment) C, ParamTys... P) { return RetTy(); }
57 template <typename ImplClass, typename RetTy = void, class... ParamTys>
58 class CommentVisitor : public CommentVisitorBase<std::add_pointer, ImplClass,
59 RetTy, ParamTys...> {};
61 template <typename ImplClass, typename RetTy = void, class... ParamTys>
62 class ConstCommentVisitor
63 : public CommentVisitorBase<llvm::make_const_ptr, ImplClass, RetTy,
66 } // namespace comments
69 #endif // LLVM_CLANG_AST_COMMENTVISITOR_H