DeclVisitor.h revision ae54121c15cdd38f415f6fdda48215ab5e4dcee1
1//===--- DeclVisitor.h - Visitor for Decl 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// This file defines the DeclVisitor interface. 11// 12//===----------------------------------------------------------------------===// 13#ifndef LLVM_CLANG_AST_DECLVISITOR_H 14#define LLVM_CLANG_AST_DECLVISITOR_H 15 16#include "clang/AST/Decl.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclFriend.h" 19#include "clang/AST/DeclObjC.h" 20#include "clang/AST/DeclTemplate.h" 21 22namespace clang { 23namespace declvisitor { 24 25template <typename T> struct make_ptr { typedef T *type; }; 26template <typename T> struct make_const_ptr { typedef const T *type; }; 27 28/// \brief A simple visitor class that helps create declaration visitors. 29template<template <typename> class Ptr, typename ImplClass, typename RetTy=void> 30class Base { 31public: 32 33#define PTR(CLASS) typename Ptr<CLASS>::type 34#define DISPATCH(NAME, CLASS) \ 35 return static_cast<ImplClass*>(this)->Visit##NAME(static_cast<PTR(CLASS)>(D)) 36 37 RetTy Visit(PTR(Decl) D) { 38 switch (D->getKind()) { 39#define DECL(DERIVED, BASE) \ 40 case Decl::DERIVED: DISPATCH(DERIVED##Decl, DERIVED##Decl); 41#define ABSTRACT_DECL(DECL) 42#include "clang/AST/DeclNodes.inc" 43 } 44 llvm_unreachable("Decl that isn't part of DeclNodes.inc!"); 45 } 46 47 // If the implementation chooses not to implement a certain visit 48 // method, fall back to the parent. 49#define DECL(DERIVED, BASE) \ 50 RetTy Visit##DERIVED##Decl(PTR(DERIVED##Decl) D) { DISPATCH(BASE, BASE); } 51#include "clang/AST/DeclNodes.inc" 52 53 RetTy VisitDecl(PTR(Decl) D) { return RetTy(); } 54 55#undef PTR 56#undef DISPATCH 57}; 58 59} // end namespace declvisitor 60 61/// \brief A simple visitor class that helps create declaration visitors. 62/// 63/// This class does not preserve constness of Decl pointers (see also 64/// ConstDeclVisitor). 65template<typename ImplClass, typename RetTy=void> 66class DeclVisitor 67 : public declvisitor::Base<declvisitor::make_ptr, ImplClass, RetTy> {}; 68 69/// \brief A simple visitor class that helps create declaration visitors. 70/// 71/// This class preserves constness of Decl pointers (see also DeclVisitor). 72template<typename ImplClass, typename RetTy=void> 73class ConstDeclVisitor 74 : public declvisitor::Base<declvisitor::make_const_ptr, ImplClass, RetTy> {}; 75 76} // end namespace clang 77 78#endif // LLVM_CLANG_AST_DECLVISITOR_H 79