ExtensibleRTTI.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/ExtensibleRTTI.h - ExtensibleRTTI support --*- 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. // \file
  15. //
  16. // Defines an extensible RTTI mechanism designed to work with Casting.h.
  17. //
  18. // Extensible RTTI differs from LLVM's primary RTTI mechanism (see
  19. // llvm.org/docs/HowToSetUpLLVMStyleRTTI.html) by supporting open type
  20. // hierarchies, where new types can be added from outside libraries without
  21. // needing to change existing code. LLVM's primary RTTI mechanism should be
  22. // preferred where possible, but where open hierarchies are needed this system
  23. // can be used.
  24. //
  25. // The RTTIRoot class defines methods for comparing type ids. Implementations
  26. // of these methods can be injected into new classes using the RTTIExtends
  27. // class template.
  28. //
  29. // E.g.
  30. //
  31. // @code{.cpp}
  32. // class MyBaseClass : public RTTIExtends<MyBaseClass, RTTIRoot> {
  33. // public:
  34. // static char ID;
  35. // virtual void foo() = 0;
  36. // };
  37. //
  38. // class MyDerivedClass1 : public RTTIExtends<MyDerivedClass1, MyBaseClass> {
  39. // public:
  40. // static char ID;
  41. // void foo() override {}
  42. // };
  43. //
  44. // class MyDerivedClass2 : public RTTIExtends<MyDerivedClass2, MyBaseClass> {
  45. // public:
  46. // static char ID;
  47. // void foo() override {}
  48. // };
  49. //
  50. // char MyBaseClass::ID = 0;
  51. // char MyDerivedClass1::ID = 0;
  52. // char MyDerivedClass2:: ID = 0;
  53. //
  54. // void fn() {
  55. // std::unique_ptr<MyBaseClass> B = llvm::make_unique<MyDerivedClass1>();
  56. // llvm::outs() << isa<MyBaseClass>(B) << "\n"; // Outputs "1".
  57. // llvm::outs() << isa<MyDerivedClass1>(B) << "\n"; // Outputs "1".
  58. // llvm::outs() << isa<MyDerivedClass2>(B) << "\n"; // Outputs "0'.
  59. // }
  60. //
  61. // @endcode
  62. //
  63. //===----------------------------------------------------------------------===//
  64. #ifndef LLVM_SUPPORT_EXTENSIBLERTTI_H
  65. #define LLVM_SUPPORT_EXTENSIBLERTTI_H
  66. namespace llvm {
  67. /// Base class for the extensible RTTI hierarchy.
  68. ///
  69. /// This class defines virtual methods, dynamicClassID and isA, that enable
  70. /// type comparisons.
  71. class RTTIRoot {
  72. public:
  73. virtual ~RTTIRoot() = default;
  74. /// Returns the class ID for this type.
  75. static const void *classID() { return &ID; }
  76. /// Returns the class ID for the dynamic type of this RTTIRoot instance.
  77. virtual const void *dynamicClassID() const = 0;
  78. /// Returns true if this class's ID matches the given class ID.
  79. virtual bool isA(const void *const ClassID) const {
  80. return ClassID == classID();
  81. }
  82. /// Check whether this instance is a subclass of QueryT.
  83. template <typename QueryT>
  84. bool isA() const { return isA(QueryT::classID()); }
  85. private:
  86. virtual void anchor();
  87. static char ID;
  88. };
  89. /// Inheritance utility for extensible RTTI.
  90. ///
  91. /// Supports single inheritance only: A class can only have one
  92. /// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
  93. /// though it can have many non-ExtensibleRTTI parents.
  94. ///
  95. /// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
  96. /// newly introduced type, and the *second* argument is the parent class.
  97. ///
  98. /// class MyType : public RTTIExtends<MyType, RTTIRoot> {
  99. /// public:
  100. /// static char ID;
  101. /// };
  102. ///
  103. /// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> {
  104. /// public:
  105. /// static char ID;
  106. /// };
  107. ///
  108. template <typename ThisT, typename ParentT>
  109. class RTTIExtends : public ParentT {
  110. public:
  111. // Inherit constructors from ParentT.
  112. using ParentT::ParentT;
  113. static const void *classID() { return &ThisT::ID; }
  114. const void *dynamicClassID() const override { return &ThisT::ID; }
  115. bool isA(const void *const ClassID) const override {
  116. return ClassID == classID() || ParentT::isA(ClassID);
  117. }
  118. static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
  119. };
  120. } // end namespace llvm
  121. #endif // LLVM_SUPPORT_EXTENSIBLERTTI_H
  122. #ifdef __GNUC__
  123. #pragma GCC diagnostic pop
  124. #endif