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