AttrVisitor.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- AttrVisitor.h - Visitor for Attr 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 AttrVisitor interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_ATTRVISITOR_H
  18. #define LLVM_CLANG_AST_ATTRVISITOR_H
  19. #include "clang/AST/Attr.h"
  20. namespace clang {
  21. namespace attrvisitor {
  22. /// A simple visitor class that helps create attribute visitors.
  23. template <template <typename> class Ptr, typename ImplClass,
  24. typename RetTy = void, class... ParamTys>
  25. class Base {
  26. public:
  27. #define PTR(CLASS) typename Ptr<CLASS>::type
  28. #define DISPATCH(NAME) \
  29. return static_cast<ImplClass *>(this)->Visit##NAME(static_cast<PTR(NAME)>(A))
  30. RetTy Visit(PTR(Attr) A) {
  31. switch (A->getKind()) {
  32. #define ATTR(NAME) \
  33. case attr::NAME: \
  34. DISPATCH(NAME##Attr);
  35. #include "clang/Basic/AttrList.inc"
  36. }
  37. llvm_unreachable("Attr that isn't part of AttrList.inc!");
  38. }
  39. // If the implementation chooses not to implement a certain visit
  40. // method, fall back to the parent.
  41. #define ATTR(NAME) \
  42. RetTy Visit##NAME##Attr(PTR(NAME##Attr) A) { DISPATCH(Attr); }
  43. #include "clang/Basic/AttrList.inc"
  44. RetTy VisitAttr(PTR(Attr)) { return RetTy(); }
  45. #undef PTR
  46. #undef DISPATCH
  47. };
  48. } // namespace attrvisitor
  49. /// A simple visitor class that helps create attribute visitors.
  50. ///
  51. /// This class does not preserve constness of Attr pointers (see
  52. /// also ConstAttrVisitor).
  53. template <typename ImplClass, typename RetTy = void, typename... ParamTys>
  54. class AttrVisitor : public attrvisitor::Base<std::add_pointer, ImplClass, RetTy,
  55. ParamTys...> {};
  56. /// A simple visitor class that helps create attribute visitors.
  57. ///
  58. /// This class preserves constness of Attr pointers (see also
  59. /// AttrVisitor).
  60. template <typename ImplClass, typename RetTy = void, typename... ParamTys>
  61. class ConstAttrVisitor
  62. : public attrvisitor::Base<llvm::make_const_ptr, ImplClass, RetTy,
  63. ParamTys...> {};
  64. } // namespace clang
  65. #endif // LLVM_CLANG_AST_ATTRVISITOR_H
  66. #ifdef __GNUC__
  67. #pragma GCC diagnostic pop
  68. #endif