ExtensibleRTTI.h 4.0 KB

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