DeclVisitor.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclVisitor.h - Visitor for Decl subclasses --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the DeclVisitor interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_DECLVISITOR_H
  18. #define LLVM_CLANG_AST_DECLVISITOR_H
  19. #include "clang/AST/Decl.h"
  20. #include "clang/AST/DeclBase.h"
  21. #include "clang/AST/DeclCXX.h"
  22. #include "clang/AST/DeclFriend.h"
  23. #include "clang/AST/DeclObjC.h"
  24. #include "clang/AST/DeclOpenMP.h"
  25. #include "clang/AST/DeclTemplate.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. namespace clang {
  29. namespace declvisitor {
  30. /// A simple visitor class that helps create declaration visitors.
  31. template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
  32. class Base {
  33. public:
  34. #define PTR(CLASS) typename Ptr<CLASS>::type
  35. #define DISPATCH(NAME, CLASS) \
  36. return static_cast<ImplClass*>(this)->Visit##NAME(static_cast<PTR(CLASS)>(D))
  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. // If the implementation chooses not to implement a certain visit
  47. // method, fall back to the parent.
  48. #define DECL(DERIVED, BASE) \
  49. RetTy Visit##DERIVED##Decl(PTR(DERIVED##Decl) D) { DISPATCH(BASE, BASE); }
  50. #include "clang/AST/DeclNodes.inc"
  51. RetTy VisitDecl(PTR(Decl) D) { return RetTy(); }
  52. #undef PTR
  53. #undef DISPATCH
  54. };
  55. } // namespace declvisitor
  56. /// A simple visitor class that helps create declaration visitors.
  57. ///
  58. /// This class does not preserve constness of Decl pointers (see also
  59. /// ConstDeclVisitor).
  60. template <typename ImplClass, typename RetTy = void>
  61. class DeclVisitor
  62. : public declvisitor::Base<std::add_pointer, ImplClass, RetTy> {};
  63. /// A simple visitor class that helps create declaration visitors.
  64. ///
  65. /// This class preserves constness of Decl pointers (see also DeclVisitor).
  66. template <typename ImplClass, typename RetTy = void>
  67. class ConstDeclVisitor
  68. : public declvisitor::Base<llvm::make_const_ptr, ImplClass, RetTy> {};
  69. } // namespace clang
  70. #endif // LLVM_CLANG_AST_DECLVISITOR_H
  71. #ifdef __GNUC__
  72. #pragma GCC diagnostic pop
  73. #endif