1 //===- DeclVisitor.h - Visitor for Decl subclasses --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DeclVisitor interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLVISITOR_H
15 #define LLVM_CLANG_AST_DECLVISITOR_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclFriend.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/DeclOpenMP.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "llvm/Support/ErrorHandling.h"
28 namespace declvisitor {
30 template <typename T> struct make_ptr { using type = T *; };
31 template <typename T> struct make_const_ptr { using type = const T *; };
33 /// \brief A simple visitor class that helps create declaration visitors.
34 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
37 #define PTR(CLASS) typename Ptr<CLASS>::type
38 #define DISPATCH(NAME, CLASS) \
39 return static_cast<ImplClass*>(this)->Visit##NAME(static_cast<PTR(CLASS)>(D))
41 RetTy Visit(PTR(Decl) D) {
42 switch (D->getKind()) {
43 #define DECL(DERIVED, BASE) \
44 case Decl::DERIVED: DISPATCH(DERIVED##Decl, DERIVED##Decl);
45 #define ABSTRACT_DECL(DECL)
46 #include "clang/AST/DeclNodes.inc"
48 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
51 // If the implementation chooses not to implement a certain visit
52 // method, fall back to the parent.
53 #define DECL(DERIVED, BASE) \
54 RetTy Visit##DERIVED##Decl(PTR(DERIVED##Decl) D) { DISPATCH(BASE, BASE); }
55 #include "clang/AST/DeclNodes.inc"
57 RetTy VisitDecl(PTR(Decl) D) { return RetTy(); }
63 } // namespace declvisitor
65 /// \brief A simple visitor class that helps create declaration visitors.
67 /// This class does not preserve constness of Decl pointers (see also
68 /// ConstDeclVisitor).
69 template<typename ImplClass, typename RetTy = void>
71 : public declvisitor::Base<declvisitor::make_ptr, ImplClass, RetTy> {};
73 /// \brief A simple visitor class that helps create declaration visitors.
75 /// This class preserves constness of Decl pointers (see also DeclVisitor).
76 template<typename ImplClass, typename RetTy = void>
77 class ConstDeclVisitor
78 : public declvisitor::Base<declvisitor::make_const_ptr, ImplClass, RetTy> {};
82 #endif // LLVM_CLANG_AST_DECLVISITOR_H