clang API Documentation

CommentVisitor.h
Go to the documentation of this file.
00001 //===--- CommentVisitor.h - Visitor for Comment subclasses ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_AST_COMMENTVISITOR_H
00011 #define LLVM_CLANG_AST_COMMENTVISITOR_H
00012 
00013 #include "clang/AST/Comment.h"
00014 #include "llvm/Support/ErrorHandling.h"
00015 
00016 namespace clang {
00017 namespace comments {
00018 
00019 template <typename T> struct make_ptr       { typedef       T *type; };
00020 template <typename T> struct make_const_ptr { typedef const T *type; };
00021 
00022 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
00023 class CommentVisitorBase {
00024 public:
00025 #define PTR(CLASS) typename Ptr<CLASS>::type
00026 #define DISPATCH(NAME, CLASS) \
00027  return static_cast<ImplClass*>(this)->visit ## NAME(static_cast<PTR(CLASS)>(C))
00028 
00029   RetTy visit(PTR(Comment) C) {
00030     if (!C)
00031       return RetTy();
00032 
00033     switch (C->getCommentKind()) {
00034     default: llvm_unreachable("Unknown comment kind!");
00035 #define ABSTRACT_COMMENT(COMMENT)
00036 #define COMMENT(CLASS, PARENT) \
00037     case Comment::CLASS##Kind: DISPATCH(CLASS, CLASS);
00038 #include "clang/AST/CommentNodes.inc"
00039 #undef ABSTRACT_COMMENT
00040 #undef COMMENT
00041     }
00042   }
00043 
00044   // If the derived class does not implement a certain Visit* method, fall back
00045   // on Visit* method for the superclass.
00046 #define ABSTRACT_COMMENT(COMMENT) COMMENT
00047 #define COMMENT(CLASS, PARENT) \
00048   RetTy visit ## CLASS(PTR(CLASS) C) { DISPATCH(PARENT, PARENT); }
00049 #include "clang/AST/CommentNodes.inc"
00050 #undef ABSTRACT_COMMENT
00051 #undef COMMENT
00052 
00053   RetTy visitComment(PTR(Comment) C) { return RetTy(); }
00054 
00055 #undef PTR
00056 #undef DISPATCH
00057 };
00058 
00059 template<typename ImplClass, typename RetTy=void>
00060 class CommentVisitor :
00061     public CommentVisitorBase<make_ptr, ImplClass, RetTy> {};
00062 
00063 template<typename ImplClass, typename RetTy=void>
00064 class ConstCommentVisitor :
00065     public CommentVisitorBase<make_const_ptr, ImplClass, RetTy> {};
00066 
00067 } // end namespace comments
00068 } // end namespace clang
00069 
00070 #endif