[llvm] [Support] Extend ExtensibleRTTI utility to support basic multiple inheritance. (PR #112643)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 24 14:10:05 PDT 2024


https://github.com/lhames updated https://github.com/llvm/llvm-project/pull/112643

>From be115ab64ef3286af94aea2aa540cb030ea6085b Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Tue, 1 Oct 2024 18:15:09 +1000
Subject: [PATCH 1/3] [Support] Extend ExtensibleRTTI utility to support basic
 multiple inheritance.

Clients can now pass multiple parent classes to RTTIExtends. Each parent class
will be inherited from publicly (and non-virtually). The isa, cast, and dyn_cast
methods will now work as expected for types with multiple inheritance.
---
 llvm/include/llvm/Support/ExtensibleRTTI.h    | 29 +++++++---
 llvm/unittests/Support/ExtensibleRTTITest.cpp | 56 +++++++++++++++----
 2 files changed, 66 insertions(+), 19 deletions(-)

diff --git a/llvm/include/llvm/Support/ExtensibleRTTI.h b/llvm/include/llvm/Support/ExtensibleRTTI.h
index d3193be6f529e1..a0b700891ad102 100644
--- a/llvm/include/llvm/Support/ExtensibleRTTI.h
+++ b/llvm/include/llvm/Support/ExtensibleRTTI.h
@@ -81,10 +81,6 @@ class RTTIRoot {
     return ClassID == classID();
   }
 
-  /// Check whether this instance is a subclass of QueryT.
-  template <typename QueryT>
-  bool isA() const { return isA(QueryT::classID()); }
-
 private:
   virtual void anchor();
 
@@ -110,21 +106,36 @@ class RTTIRoot {
 ///   static char ID;
 /// };
 ///
-template <typename ThisT, typename ParentT>
-class RTTIExtends : public ParentT {
+template <typename ThisT, typename... ParentTs>
+class RTTIExtends : public ParentTs... {
 public:
   // Inherit constructors from ParentT.
-  using ParentT::ParentT;
+  using ParentTs::ParentTs...;
 
   static const void *classID() { return &ThisT::ID; }
 
   const void *dynamicClassID() const override { return &ThisT::ID; }
 
+  /// Check whether this instance is a subclass of QueryT.
+  template <typename QueryT> bool isA() const { return isA(QueryT::classID()); }
+
   bool isA(const void *const ClassID) const override {
-    return ClassID == classID() || ParentT::isA(ClassID);
+    return ClassID == classID() || parentsAreA<ParentTs...>(ClassID);
+  }
+
+  template <typename T> static bool classof(const T *R) {
+    return R->template isA<ThisT>();
   }
 
-  static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
+private:
+  template <typename T> bool parentsAreA(const void *const ClassID) const {
+    return T::isA(ClassID);
+  }
+
+  template <typename T, typename T2, typename... Ts>
+  bool parentsAreA(const void *const ClassID) const {
+    return T::isA(ClassID) || parentsAreA<T2, Ts...>(ClassID);
+  }
 };
 
 } // end namespace llvm
diff --git a/llvm/unittests/Support/ExtensibleRTTITest.cpp b/llvm/unittests/Support/ExtensibleRTTITest.cpp
index 9715d26a9de297..08fc6a43d8b761 100644
--- a/llvm/unittests/Support/ExtensibleRTTITest.cpp
+++ b/llvm/unittests/Support/ExtensibleRTTITest.cpp
@@ -36,15 +36,24 @@ class MyDeeperDerivedType
   static char ID;
 };
 
+class MyMultipleInheritanceType
+    : public RTTIExtends<MyMultipleInheritanceType, MyDerivedType,
+                         MyOtherDerivedType> {
+public:
+  static char ID;
+};
+
 char MyBaseType::ID = 0;
 char MyDerivedType::ID = 0;
 char MyOtherDerivedType::ID = 0;
 char MyDeeperDerivedType::ID = 0;
+char MyMultipleInheritanceType::ID = 0;
 
 TEST(ExtensibleRTTI, isa) {
   MyBaseType B;
   MyDerivedType D;
   MyDeeperDerivedType DD;
+  MyMultipleInheritanceType MI;
 
   EXPECT_TRUE(isa<MyBaseType>(B));
   EXPECT_FALSE(isa<MyDerivedType>(B));
@@ -60,26 +69,53 @@ TEST(ExtensibleRTTI, isa) {
   EXPECT_TRUE(isa<MyDerivedType>(DD));
   EXPECT_FALSE(isa<MyOtherDerivedType>(DD));
   EXPECT_TRUE(isa<MyDeeperDerivedType>(DD));
+
+  EXPECT_TRUE(isa<MyBaseType>(MI));
+  EXPECT_TRUE(isa<MyDerivedType>(MI));
+  EXPECT_TRUE(isa<MyOtherDerivedType>(MI));
+  EXPECT_FALSE(isa<MyDeeperDerivedType>(MI));
+  EXPECT_TRUE(isa<MyMultipleInheritanceType>(MI));
 }
 
 TEST(ExtensibleRTTI, cast) {
-  MyDerivedType D;
-  MyBaseType &BD = D;
-
-  (void)cast<MyBaseType>(D);
-  (void)cast<MyBaseType>(BD);
-  (void)cast<MyDerivedType>(BD);
+  MyMultipleInheritanceType MI;
+  MyDerivedType &D = MI;
+  MyOtherDerivedType &OD = MI;
+  MyBaseType &B = D;
+
+  EXPECT_EQ(&cast<MyBaseType>(D), &B);
+  EXPECT_EQ(&cast<MyDerivedType>(MI), &D);
+  EXPECT_EQ(&cast<MyOtherDerivedType>(MI), &OD);
+  EXPECT_EQ(&cast<MyMultipleInheritanceType>(MI), &MI);
 }
 
 TEST(ExtensibleRTTI, dyn_cast) {
-  MyBaseType B;
-  MyDerivedType D;
+  MyMultipleInheritanceType MI;
+  MyDerivedType &D = MI;
+  MyOtherDerivedType &OD = MI;
   MyBaseType &BD = D;
+  MyBaseType &BOD = OD;
 
-  EXPECT_EQ(dyn_cast<MyDerivedType>(&B), nullptr);
-  EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
   EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD);
   EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D);
+
+  EXPECT_EQ(dyn_cast<MyBaseType>(&BOD), &BOD);
+  EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&BOD), &OD);
+
+  EXPECT_EQ(dyn_cast<MyBaseType>(&D), &BD);
+  EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
+  EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&D), &MI);
+
+  EXPECT_EQ(dyn_cast<MyBaseType>(&OD), &BOD);
+  EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&OD), &OD);
+  EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&OD), &MI);
+
+  EXPECT_EQ(dyn_cast<MyDerivedType>(&MI), &D);
+  EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&MI), &MI);
+
+  EXPECT_EQ(dyn_cast<MyDerivedType>(&MI), &D);
+  EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&MI), &OD);
+  EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&MI), &MI);
 }
 
 } // namespace

>From 5430d2be790063a890695659f3044b0c3575660e Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Tue, 22 Oct 2024 10:43:43 +1100
Subject: [PATCH 2/3] Address @dblaikie's review feedback.

---
 llvm/include/llvm/Support/ExtensibleRTTI.h | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/llvm/include/llvm/Support/ExtensibleRTTI.h b/llvm/include/llvm/Support/ExtensibleRTTI.h
index a0b700891ad102..b1af46b7549ce0 100644
--- a/llvm/include/llvm/Support/ExtensibleRTTI.h
+++ b/llvm/include/llvm/Support/ExtensibleRTTI.h
@@ -120,22 +120,12 @@ class RTTIExtends : public ParentTs... {
   template <typename QueryT> bool isA() const { return isA(QueryT::classID()); }
 
   bool isA(const void *const ClassID) const override {
-    return ClassID == classID() || parentsAreA<ParentTs...>(ClassID);
+    return ClassID == classID() || (ParentTs::isA(ClassID) || ...);
   }
 
   template <typename T> static bool classof(const T *R) {
     return R->template isA<ThisT>();
   }
-
-private:
-  template <typename T> bool parentsAreA(const void *const ClassID) const {
-    return T::isA(ClassID);
-  }
-
-  template <typename T, typename T2, typename... Ts>
-  bool parentsAreA(const void *const ClassID) const {
-    return T::isA(ClassID) || parentsAreA<T2, Ts...>(ClassID);
-  }
 };
 
 } // end namespace llvm

>From 2f77f640fc8198ac406d9115f77556fb45c8f9d0 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Fri, 25 Oct 2024 07:35:06 +1100
Subject: [PATCH 3/3] Update comments, explicitly inherit only the first base
 class's constructors.

---
 llvm/include/llvm/Support/ExtensibleRTTI.h    | 35 ++++++++++++++-----
 llvm/unittests/Support/ExtensibleRTTITest.cpp | 22 ++++++++++++
 2 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/Support/ExtensibleRTTI.h b/llvm/include/llvm/Support/ExtensibleRTTI.h
index b1af46b7549ce0..e11e3bd06e93c8 100644
--- a/llvm/include/llvm/Support/ExtensibleRTTI.h
+++ b/llvm/include/llvm/Support/ExtensibleRTTI.h
@@ -89,13 +89,15 @@ class RTTIRoot {
 
 /// Inheritance utility for extensible RTTI.
 ///
-/// Supports single inheritance only: A class can only have one
-/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
-/// though it can have many non-ExtensibleRTTI parents.
+/// Multiple inheritance is supported, but RTTIExtends only inherits
+/// constructors from the first base class. All subsequent bases will be
+/// default constructed. Virtual and non-public inheritance are not supported.
 ///
 /// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
-/// newly introduced type, and the *second* argument is the parent class.
+/// newly introduced type, and the *second and later* arguments are the parent
+/// classes.
 ///
+/// @code{.cpp}
 /// class MyType : public RTTIExtends<MyType, RTTIRoot> {
 /// public:
 ///   static char ID;
@@ -106,11 +108,25 @@ class RTTIRoot {
 ///   static char ID;
 /// };
 ///
-template <typename ThisT, typename... ParentTs>
-class RTTIExtends : public ParentTs... {
+/// class MyOtherType : public RTTIExtends<MyOtherType, MyType> {
+/// public:
+///   static char ID;
+/// };
+///
+/// class MyMultipleInheritanceType
+///   : public RTTIExtends<MyMultipleInheritanceType,
+///                        MyDerivedType, MyOtherType> {
+/// public:
+///   static char ID;
+/// };
+///
+/// @endcode
+///
+template <typename ThisT, typename ParentT, typename... ParentTs>
+class RTTIExtends : public ParentT, public ParentTs... {
 public:
-  // Inherit constructors from ParentT.
-  using ParentTs::ParentTs...;
+  // Inherit constructors from the first Parent.
+  using ParentT::ParentT;
 
   static const void *classID() { return &ThisT::ID; }
 
@@ -120,7 +136,8 @@ class RTTIExtends : public ParentTs... {
   template <typename QueryT> bool isA() const { return isA(QueryT::classID()); }
 
   bool isA(const void *const ClassID) const override {
-    return ClassID == classID() || (ParentTs::isA(ClassID) || ...);
+    return ClassID == classID() || ParentT::isA(ClassID) ||
+           (ParentTs::isA(ClassID) || ...);
   }
 
   template <typename T> static bool classof(const T *R) {
diff --git a/llvm/unittests/Support/ExtensibleRTTITest.cpp b/llvm/unittests/Support/ExtensibleRTTITest.cpp
index 08fc6a43d8b761..8b0ff89745b206 100644
--- a/llvm/unittests/Support/ExtensibleRTTITest.cpp
+++ b/llvm/unittests/Support/ExtensibleRTTITest.cpp
@@ -43,11 +43,29 @@ class MyMultipleInheritanceType
   static char ID;
 };
 
+class MyTypeWithConstructor
+    : public RTTIExtends<MyTypeWithConstructor, MyBaseType> {
+public:
+  static char ID;
+
+  MyTypeWithConstructor(int) {}
+};
+
+class MyDerivedTypeWithConstructor
+    : public RTTIExtends<MyDerivedTypeWithConstructor, MyTypeWithConstructor> {
+public:
+  static char ID;
+
+  MyDerivedTypeWithConstructor(int x) : RTTIExtends(x) {}
+};
+
 char MyBaseType::ID = 0;
 char MyDerivedType::ID = 0;
 char MyOtherDerivedType::ID = 0;
 char MyDeeperDerivedType::ID = 0;
 char MyMultipleInheritanceType::ID = 0;
+char MyTypeWithConstructor::ID = 0;
+char MyDerivedTypeWithConstructor::ID = 0;
 
 TEST(ExtensibleRTTI, isa) {
   MyBaseType B;
@@ -118,4 +136,8 @@ TEST(ExtensibleRTTI, dyn_cast) {
   EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&MI), &MI);
 }
 
+TEST(ExtensibleRTTI, multiple_inheritance_constructor) {
+  MyDerivedTypeWithConstructor V(42);
+}
+
 } // namespace



More information about the llvm-commits mailing list