|
| 1 | +//===------------- rtti.h - RTTI support for ORC RT -------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// \file |
| 10 | +// |
| 11 | +// Provides an extensible RTTI mechanism, that can be used regardless of whether |
| 12 | +// the runtime is built with -frtti or not. This is predominantly used to |
| 13 | +// support error handling. |
| 14 | +// |
| 15 | +// The RTTIRoot class defines methods for comparing type ids. Implementations |
| 16 | +// of these methods can be injected into new classes using the RTTIExtends |
| 17 | +// class template. |
| 18 | +// |
| 19 | +// E.g. |
| 20 | +// |
| 21 | +// @code{.cpp} |
| 22 | +// class MyBaseClass : public RTTIExtends<MyBaseClass, RTTIRoot> { |
| 23 | +// public: |
| 24 | +// virtual void foo() = 0; |
| 25 | +// }; |
| 26 | +// |
| 27 | +// class MyDerivedClass1 : public RTTIExtends<MyDerivedClass1, MyBaseClass> { |
| 28 | +// public: |
| 29 | +// void foo() override {} |
| 30 | +// }; |
| 31 | +// |
| 32 | +// class MyDerivedClass2 : public RTTIExtends<MyDerivedClass2, MyBaseClass> { |
| 33 | +// public: |
| 34 | +// void foo() override {} |
| 35 | +// }; |
| 36 | +// |
| 37 | +// void fn() { |
| 38 | +// std::unique_ptr<MyBaseClass> B = std::make_unique<MyDerivedClass1>(); |
| 39 | +// outs() << isa<MyBaseClass>(B) << "\n"; // Outputs "1". |
| 40 | +// outs() << isa<MyDerivedClass1>(B) << "\n"; // Outputs "1". |
| 41 | +// outs() << isa<MyDerivedClass2>(B) << "\n"; // Outputs "0'. |
| 42 | +// } |
| 43 | +// |
| 44 | +// @endcode |
| 45 | +// |
| 46 | +// Note: |
| 47 | +// This header was adapted from llvm/Support/ExtensibleRTTI.h, however the |
| 48 | +// data structures are not shared and the code need not be kept in sync. |
| 49 | +// |
| 50 | +//===----------------------------------------------------------------------===// |
| 51 | + |
| 52 | +#ifndef ORC_RT_RTTI_H |
| 53 | +#define ORC_RT_RTTI_H |
| 54 | + |
| 55 | +namespace orc_rt { |
| 56 | + |
| 57 | +template <typename ThisT, typename ParentT> class RTTIExtends; |
| 58 | + |
| 59 | +/// Base class for the extensible RTTI hierarchy. |
| 60 | +/// |
| 61 | +/// This class defines virtual methods, dynamicClassID and isA, that enable |
| 62 | +/// type comparisons. |
| 63 | +class RTTIRoot { |
| 64 | +public: |
| 65 | + virtual ~RTTIRoot() = default; |
| 66 | + |
| 67 | + /// Returns the class ID for this type. |
| 68 | + static const void *classID() noexcept { return &ID; } |
| 69 | + |
| 70 | + /// Returns the class ID for the dynamic type of this RTTIRoot instance. |
| 71 | + virtual const void *dynamicClassID() const noexcept = 0; |
| 72 | + |
| 73 | + /// Returns true if this class's ID matches the given class ID. |
| 74 | + virtual bool isA(const void *const ClassID) const noexcept { |
| 75 | + return ClassID == classID(); |
| 76 | + } |
| 77 | + |
| 78 | + /// Check whether this instance is a subclass of QueryT. |
| 79 | + template <typename QueryT> bool isA() const noexcept { |
| 80 | + return isA(QueryT::classID()); |
| 81 | + } |
| 82 | + |
| 83 | + static bool classof(const RTTIRoot *R) noexcept { return R->isA<RTTIRoot>(); } |
| 84 | + |
| 85 | +private: |
| 86 | + virtual void anchor(); |
| 87 | + |
| 88 | + static char ID; |
| 89 | +}; |
| 90 | + |
| 91 | +/// Inheritance utility for extensible RTTI. |
| 92 | +/// |
| 93 | +/// Supports single inheritance only: A class can only have one |
| 94 | +/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work), |
| 95 | +/// though it can have many non-ExtensibleRTTI parents. |
| 96 | +/// |
| 97 | +/// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the |
| 98 | +/// newly introduced type, and the *second* argument is the parent class. |
| 99 | +/// |
| 100 | +/// class MyType : public RTTIExtends<MyType, RTTIRoot> { |
| 101 | +/// ... |
| 102 | +/// }; |
| 103 | +/// |
| 104 | +/// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> { |
| 105 | +/// ... |
| 106 | +/// }; |
| 107 | +/// |
| 108 | +template <typename ThisT, typename ParentT> class RTTIExtends : public ParentT { |
| 109 | +public: |
| 110 | + // Inherit constructors and isA methods from ParentT. |
| 111 | + using ParentT::isA; |
| 112 | + using ParentT::ParentT; |
| 113 | + |
| 114 | + static char ID; |
| 115 | + |
| 116 | + static const void *classID() noexcept { return &ThisT::ID; } |
| 117 | + |
| 118 | + const void *dynamicClassID() const noexcept override { return &ThisT::ID; } |
| 119 | + |
| 120 | + bool isA(const void *const ClassID) const noexcept override { |
| 121 | + return ClassID == classID() || ParentT::isA(ClassID); |
| 122 | + } |
| 123 | + |
| 124 | + static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); } |
| 125 | +}; |
| 126 | + |
| 127 | +template <typename ThisT, typename ParentT> |
| 128 | +char RTTIExtends<ThisT, ParentT>::ID = 0; |
| 129 | + |
| 130 | +/// Returns true if the given value is an instance of the template type |
| 131 | +/// parameter. |
| 132 | +template <typename To, typename From> bool isa(const From &Value) noexcept { |
| 133 | + return To::classof(&Value); |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace orc_rt |
| 137 | + |
| 138 | +#endif // ORC_RT_RTTI_H |
0 commit comments