Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions llvm/include/llvm/Support/ExtensibleRTTI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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...;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still apply - I guess it only works if you want to call one of the base ctors and all the other bases support default construction?

Probably OK as a first step - but maybe needs a FIXME to add something like std::pair's piecewise construct (though interesting that tuple doesn't have that... )


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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be able to skip the parentsAreA helper:

return ClassId == classId() || (ParentTs::isA(ClassID) || ...);

(here's at least a little proof of concept I did: https://godbolt.org/z/vGca7jYbT )

}

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
Expand Down
56 changes: 46 additions & 10 deletions llvm/unittests/Support/ExtensibleRTTITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Loading