CommentVisitor.h revision 8d3ba23f2d9e6c87794d059412a0808c9cbacb25
1//===--- CommentVisitor.h - Visitor for Comment subclasses ------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/Comment.h"
11#include "llvm/Support/ErrorHandling.h"
12
13namespace clang {
14namespace comments {
15
16template <typename T> struct make_ptr       { typedef       T *type; };
17template <typename T> struct make_const_ptr { typedef const T *type; };
18
19template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
20class CommentVisitorBase {
21public:
22#define PTR(CLASS) typename Ptr<CLASS>::type
23#define DISPATCH(NAME, CLASS) \
24 return static_cast<ImplClass*>(this)->visit ## NAME(static_cast<PTR(CLASS)>(C))
25
26  RetTy visit(PTR(Comment) C) {
27    switch (C->getCommentKind()) {
28    default: llvm_unreachable("Unknown comment kind!");
29#define ABSTRACT_COMMENT(COMMENT)
30#define COMMENT(CLASS, PARENT) \
31    case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS);
32#include "clang/AST/CommentNodes.inc"
33#undef ABSTRACT_COMMENT
34#undef COMMENT
35    }
36  }
37
38  // If the derived class does not implement a certain Visit* method, fall back
39  // on Visit* method for the superclass.
40#define ABSTRACT_COMMENT(COMMENT) COMMENT
41#define COMMENT(CLASS, PARENT) \
42  RetTy visit ## CLASS(PTR(CLASS) C) { DISPATCH(PARENT, PARENT); }
43#include "clang/AST/CommentNodes.inc"
44#undef ABSTRACT_COMMENT
45#undef COMMENT
46
47  RetTy visitComment(PTR(Comment) C) { return RetTy(); }
48
49#undef PTR
50#undef DISPATCH
51};
52
53template<typename ImplClass, typename RetTy=void>
54class CommentVisitor :
55    public CommentVisitorBase<make_ptr, ImplClass, RetTy> {};
56
57template<typename ImplClass, typename RetTy=void>
58class ConstCommentVisitor :
59    public CommentVisitorBase<make_const_ptr, ImplClass, RetTy> {};
60
61} // end namespace comments
62} // end namespace clang
63
64