Skip to content

Commit e823068

Browse files
committed
[Support] Add support RTTI support for open class hierarchies.
This patch extracts the RTTI part of llvm::ErrorInfo into its own class (RTTIExtends) so that it can be used in other non-error hierarchies, and makes it compatible with the existing LLVM RTTI function templates (isa, cast, dyn_cast, dyn_cast_or_null) by adding the classof method. Differential Revision: https://reviews.llvm.org/D39111
1 parent ec99d6e commit e823068

File tree

5 files changed

+278
-0
lines changed

5 files changed

+278
-0
lines changed

llvm/docs/HowToSetUpLLVMStyleRTTI.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,58 @@ Rules of Thumb
412412
#. For each class in the hierarchy that has children, implement a
413413
``classof`` that checks a range of the first child's ``Kind`` and the
414414
last child's ``Kind``.
415+
416+
RTTI for Open Class Hierarchies
417+
===============================
418+
419+
Sometimes it is not possible to know all types in a hierarchy ahead of time.
420+
For example, in the shapes hierarchy described above the authors may have
421+
wanted their code to work for user defined shapes too. To support use cases
422+
that require open hierarchies LLVM provides the ``RTTIRoot`` and
423+
``RTTIExtends`` utilities.
424+
425+
The ``RTTIRoot`` class describes an interface for performing RTTI checks. The
426+
``RTTIExtends`` class template provides an implementation of this interface
427+
for classes derived from ``RTTIRoot``. ``RTTIExtends`` uses the "`Curiously
428+
Recurring Template Idiom`_", taking the class being defined as its first
429+
template argument and the parent class as the second argument. Any class that
430+
uses ``RTTIExtends`` must define a ``static char ID`` member, the address of
431+
which will be used to identify the type.
432+
433+
This open-hierarchy RTTI support should only be used if your use case requries
434+
it. Otherwise the standard LLVM RTTI system should be preferred.
435+
436+
.. _`Curiously Recurring Template Idiom`:
437+
https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
438+
439+
E.g.
440+
441+
.. code-block:: c++
442+
443+
class Shape : public RTTIExtends<Shape, RTTIRoot> {
444+
public:
445+
static char ID;
446+
virtual double computeArea() = 0;
447+
};
448+
449+
class Square : public RTTIExtends<Square, Shape> {
450+
double SideLength;
451+
public:
452+
static char ID;
453+
454+
Square(double S) : SideLength(S) {}
455+
double computeArea() override;
456+
};
457+
458+
class Circle : public RTTIExtends<Circle, Shape> {
459+
double Radius;
460+
public:
461+
static char ID;
462+
463+
Circle(double R) : Radius(R) {}
464+
double computeArea() override;
465+
};
466+
467+
char Shape::ID = 0;
468+
char Square::ID = 0;
469+
char Circle::ID = 0;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
61+
#ifndef LLVM_SUPPORT_EXTENSIBLERTTI_H
62+
#define LLVM_SUPPORT_EXTENSIBLERTTI_H
63+
64+
namespace llvm {
65+
66+
template <typename ThisT, typename ParentT> class RTTIExtends;
67+
68+
/// Base class for the extensible RTTI hierarchy.
69+
///
70+
/// This class defines virtual methods, dynamicClassID and isA, that enable
71+
/// type comparisons.
72+
class RTTIRoot {
73+
public:
74+
virtual ~RTTIRoot() = default;
75+
76+
/// Returns the class ID for this type.
77+
static const void *classID() { return &ID; }
78+
79+
/// Returns the class ID for the dynamic type of this RTTIRoot instance.
80+
virtual const void *dynamicClassID() const = 0;
81+
82+
/// Returns true if this class's ID matches the given class ID.
83+
virtual bool isA(const void *const ClassID) const {
84+
return ClassID == classID();
85+
}
86+
87+
/// Check whether this instance is a subclass of QueryT.
88+
template <typename QueryT>
89+
bool isA() const { return isA(QueryT::classID()); }
90+
91+
private:
92+
virtual void anchor();
93+
94+
static char ID;
95+
};
96+
97+
/// Inheritance utility for extensible RTTI.
98+
///
99+
/// Supports single inheritance only: A class can only have one
100+
/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
101+
/// though it can have many non-ExtensibleRTTI parents.
102+
///
103+
/// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
104+
/// newly introduced type, and the *second* argument is the parent class.
105+
///
106+
/// class MyType : public RTTIExtends<MyType, RTTIRoot> {
107+
/// public:
108+
/// static char ID;
109+
/// };
110+
///
111+
/// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> {
112+
/// public:
113+
/// static char ID;
114+
/// };
115+
///
116+
template <typename ThisT, typename ParentT>
117+
class RTTIExtends : public ParentT {
118+
public:
119+
// Inherit constructors from ParentT.
120+
using ParentT::ParentT;
121+
122+
static const void *classID() { return &ThisT::ID; }
123+
124+
const void *dynamicClassID() const override { return &ThisT::ID; }
125+
126+
bool isA(const void *const ClassID) const override {
127+
return ClassID == classID() || ParentT::isA(ClassID);
128+
}
129+
130+
static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
131+
};
132+
133+
} // end namespace llvm
134+
135+
#endif // LLVM_SUPPORT_EXTENSIBLERTTI_H

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ add_llvm_component_library(LLVMSupport
9191
ELFAttributes.cpp
9292
Error.cpp
9393
ErrorHandling.cpp
94+
ExtensibleRTTI.cpp
9495
FileCheck.cpp
9596
FileCollector.cpp
9697
FileUtilities.cpp

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_llvm_unittest(SupportTests
3232
ErrnoTest.cpp
3333
ErrorOrTest.cpp
3434
ErrorTest.cpp
35+
ExtensibleRTTITest.cpp
3536
FileCheckTest.cpp
3637
FileCollectorTest.cpp
3738
FileOutputBufferTest.cpp
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===//
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+
#include "llvm/Support/ExtensibleRTTI.h"
11+
#include "llvm/Support/Casting.h"
12+
13+
#include "gtest/gtest.h"
14+
15+
using namespace llvm;
16+
17+
namespace {
18+
19+
class MyBaseType : public RTTIExtends<MyBaseType, RTTIRoot> {
20+
public:
21+
static char ID;
22+
};
23+
24+
class MyDerivedType : public RTTIExtends<MyDerivedType, MyBaseType> {
25+
public:
26+
static char ID;
27+
};
28+
29+
class MyOtherDerivedType : public RTTIExtends<MyOtherDerivedType, MyBaseType> {
30+
public:
31+
static char ID;
32+
};
33+
34+
class MyDeeperDerivedType
35+
: public RTTIExtends<MyDeeperDerivedType, MyDerivedType> {
36+
public:
37+
static char ID;
38+
};
39+
40+
char MyBaseType::ID = 0;
41+
char MyDerivedType::ID = 0;
42+
char MyOtherDerivedType::ID = 0;
43+
char MyDeeperDerivedType::ID = 0;
44+
45+
TEST(ExtensibleRTTI, isa) {
46+
MyBaseType B;
47+
MyDerivedType D;
48+
MyDeeperDerivedType DD;
49+
50+
EXPECT_TRUE(isa<MyBaseType>(B));
51+
EXPECT_FALSE(isa<MyDerivedType>(B));
52+
EXPECT_FALSE(isa<MyOtherDerivedType>(B));
53+
EXPECT_FALSE(isa<MyDeeperDerivedType>(B));
54+
55+
EXPECT_TRUE(isa<MyBaseType>(D));
56+
EXPECT_TRUE(isa<MyDerivedType>(D));
57+
EXPECT_FALSE(isa<MyOtherDerivedType>(D));
58+
EXPECT_FALSE(isa<MyDeeperDerivedType>(D));
59+
60+
EXPECT_TRUE(isa<MyBaseType>(DD));
61+
EXPECT_TRUE(isa<MyDerivedType>(DD));
62+
EXPECT_FALSE(isa<MyOtherDerivedType>(DD));
63+
EXPECT_TRUE(isa<MyDeeperDerivedType>(DD));
64+
}
65+
66+
TEST(ExtensibleRTTI, cast) {
67+
MyDerivedType D;
68+
MyBaseType &BD = D;
69+
70+
cast<MyBaseType>(D);
71+
cast<MyBaseType>(BD);
72+
cast<MyDerivedType>(BD);
73+
}
74+
75+
TEST(ExtensibleRTTI, dyn_cast) {
76+
MyBaseType B;
77+
MyDerivedType D;
78+
MyBaseType &BD = D;
79+
80+
EXPECT_EQ(dyn_cast<MyDerivedType>(&B), nullptr);
81+
EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
82+
EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD);
83+
EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D);
84+
}
85+
86+
} // namespace

0 commit comments

Comments
 (0)