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#ifndef LLVM_CLANG_AST_COMMENTVISITOR_H
11#define LLVM_CLANG_AST_COMMENTVISITOR_H
12
13#include "clang/AST/Comment.h"
14#include "llvm/Support/ErrorHandling.h"
15
16namespace clang {
17namespace comments {
18
19template <typename T> struct make_ptr       { typedef       T *type; };
20template <typename T> struct make_const_ptr { typedef const T *type; };
21
22template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
23class CommentVisitorBase {
24public:
25#define PTR(CLASS) typename Ptr<CLASS>::type
26#define DISPATCH(NAME, CLASS) \
27 return static_cast<ImplClass*>(this)->visit ## NAME(static_cast<PTR(CLASS)>(C))
28
29  RetTy visit(PTR(Comment) C) {
30    if (!C)
31      return RetTy();
32
33    switch (C->getCommentKind()) {
34    default: llvm_unreachable("Unknown comment kind!");
35#define ABSTRACT_COMMENT(COMMENT)
36#define COMMENT(CLASS, PARENT) \
37    case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS);
38#include "clang/AST/CommentNodes.inc"
39#undef ABSTRACT_COMMENT
40#undef COMMENT
41    }
42  }
43
44  // If the derived class does not implement a certain Visit* method, fall back
45  // on Visit* method for the superclass.
46#define ABSTRACT_COMMENT(COMMENT) COMMENT
47#define COMMENT(CLASS, PARENT) \
48  RetTy visit ## CLASS(PTR(CLASS) C) { DISPATCH(PARENT, PARENT); }
49#include "clang/AST/CommentNodes.inc"
50#undef ABSTRACT_COMMENT
51#undef COMMENT
52
53  RetTy visitComment(PTR(Comment) C) { return RetTy(); }
54
55#undef PTR
56#undef DISPATCH
57};
58
59template<typename ImplClass, typename RetTy=void>
60class CommentVisitor :
61    public CommentVisitorBase<make_ptr, ImplClass, RetTy> {};
62
63template<typename ImplClass, typename RetTy=void>
64class ConstCommentVisitor :
65    public CommentVisitorBase<make_const_ptr, ImplClass, RetTy> {};
66
67} // end namespace comments
68} // end namespace clang
69
70#endif
71