1e8230683SLang Hames //===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===// 2e8230683SLang Hames // 3c874dd53SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4c874dd53SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information. 5c874dd53SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8230683SLang Hames // 7e8230683SLang Hames //===----------------------------------------------------------------------===// 8e8230683SLang Hames 9e8230683SLang Hames #include "llvm/Support/ExtensibleRTTI.h" 10e8230683SLang Hames #include "llvm/Support/Casting.h" 11e8230683SLang Hames 12e8230683SLang Hames #include "gtest/gtest.h" 13e8230683SLang Hames 14e8230683SLang Hames using namespace llvm; 15e8230683SLang Hames 16e8230683SLang Hames namespace { 17e8230683SLang Hames 18e8230683SLang Hames class MyBaseType : public RTTIExtends<MyBaseType, RTTIRoot> { 19e8230683SLang Hames public: 20e8230683SLang Hames static char ID; 21e8230683SLang Hames }; 22e8230683SLang Hames 23e8230683SLang Hames class MyDerivedType : public RTTIExtends<MyDerivedType, MyBaseType> { 24e8230683SLang Hames public: 25e8230683SLang Hames static char ID; 26e8230683SLang Hames }; 27e8230683SLang Hames 28e8230683SLang Hames class MyOtherDerivedType : public RTTIExtends<MyOtherDerivedType, MyBaseType> { 29e8230683SLang Hames public: 30e8230683SLang Hames static char ID; 31e8230683SLang Hames }; 32e8230683SLang Hames 33e8230683SLang Hames class MyDeeperDerivedType 34e8230683SLang Hames : public RTTIExtends<MyDeeperDerivedType, MyDerivedType> { 35e8230683SLang Hames public: 36e8230683SLang Hames static char ID; 37e8230683SLang Hames }; 38e8230683SLang Hames 39*633a6c91SLang Hames class MyMultipleInheritanceType 40*633a6c91SLang Hames : public RTTIExtends<MyMultipleInheritanceType, MyDerivedType, 41*633a6c91SLang Hames MyOtherDerivedType> { 42*633a6c91SLang Hames public: 43*633a6c91SLang Hames static char ID; 44*633a6c91SLang Hames }; 45*633a6c91SLang Hames 46*633a6c91SLang Hames class MyTypeWithConstructor 47*633a6c91SLang Hames : public RTTIExtends<MyTypeWithConstructor, MyBaseType> { 48*633a6c91SLang Hames public: 49*633a6c91SLang Hames static char ID; 50*633a6c91SLang Hames 51*633a6c91SLang Hames MyTypeWithConstructor(int) {} 52*633a6c91SLang Hames }; 53*633a6c91SLang Hames 54*633a6c91SLang Hames class MyDerivedTypeWithConstructor 55*633a6c91SLang Hames : public RTTIExtends<MyDerivedTypeWithConstructor, MyTypeWithConstructor> { 56*633a6c91SLang Hames public: 57*633a6c91SLang Hames static char ID; 58*633a6c91SLang Hames 59*633a6c91SLang Hames MyDerivedTypeWithConstructor(int x) : RTTIExtends(x) {} 60*633a6c91SLang Hames }; 61*633a6c91SLang Hames 62e8230683SLang Hames char MyBaseType::ID = 0; 63e8230683SLang Hames char MyDerivedType::ID = 0; 64e8230683SLang Hames char MyOtherDerivedType::ID = 0; 65e8230683SLang Hames char MyDeeperDerivedType::ID = 0; 66*633a6c91SLang Hames char MyMultipleInheritanceType::ID = 0; 67*633a6c91SLang Hames char MyTypeWithConstructor::ID = 0; 68*633a6c91SLang Hames char MyDerivedTypeWithConstructor::ID = 0; 69e8230683SLang Hames 70e8230683SLang Hames TEST(ExtensibleRTTI, isa) { 71e8230683SLang Hames MyBaseType B; 72e8230683SLang Hames MyDerivedType D; 73e8230683SLang Hames MyDeeperDerivedType DD; 74*633a6c91SLang Hames MyMultipleInheritanceType MI; 75e8230683SLang Hames 76e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(B)); 77e8230683SLang Hames EXPECT_FALSE(isa<MyDerivedType>(B)); 78e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(B)); 79e8230683SLang Hames EXPECT_FALSE(isa<MyDeeperDerivedType>(B)); 80e8230683SLang Hames 81e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(D)); 82e8230683SLang Hames EXPECT_TRUE(isa<MyDerivedType>(D)); 83e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(D)); 84e8230683SLang Hames EXPECT_FALSE(isa<MyDeeperDerivedType>(D)); 85e8230683SLang Hames 86e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(DD)); 87e8230683SLang Hames EXPECT_TRUE(isa<MyDerivedType>(DD)); 88e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(DD)); 89e8230683SLang Hames EXPECT_TRUE(isa<MyDeeperDerivedType>(DD)); 90*633a6c91SLang Hames 91*633a6c91SLang Hames EXPECT_TRUE(isa<MyBaseType>(MI)); 92*633a6c91SLang Hames EXPECT_TRUE(isa<MyDerivedType>(MI)); 93*633a6c91SLang Hames EXPECT_TRUE(isa<MyOtherDerivedType>(MI)); 94*633a6c91SLang Hames EXPECT_FALSE(isa<MyDeeperDerivedType>(MI)); 95*633a6c91SLang Hames EXPECT_TRUE(isa<MyMultipleInheritanceType>(MI)); 96e8230683SLang Hames } 97e8230683SLang Hames 98e8230683SLang Hames TEST(ExtensibleRTTI, cast) { 99*633a6c91SLang Hames MyMultipleInheritanceType MI; 100*633a6c91SLang Hames MyDerivedType &D = MI; 101*633a6c91SLang Hames MyOtherDerivedType &OD = MI; 102*633a6c91SLang Hames MyBaseType &B = D; 103e8230683SLang Hames 104*633a6c91SLang Hames EXPECT_EQ(&cast<MyBaseType>(D), &B); 105*633a6c91SLang Hames EXPECT_EQ(&cast<MyDerivedType>(MI), &D); 106*633a6c91SLang Hames EXPECT_EQ(&cast<MyOtherDerivedType>(MI), &OD); 107*633a6c91SLang Hames EXPECT_EQ(&cast<MyMultipleInheritanceType>(MI), &MI); 108e8230683SLang Hames } 109e8230683SLang Hames 110e8230683SLang Hames TEST(ExtensibleRTTI, dyn_cast) { 111*633a6c91SLang Hames MyMultipleInheritanceType MI; 112*633a6c91SLang Hames MyDerivedType &D = MI; 113*633a6c91SLang Hames MyOtherDerivedType &OD = MI; 114e8230683SLang Hames MyBaseType &BD = D; 115*633a6c91SLang Hames MyBaseType &BOD = OD; 116e8230683SLang Hames 117e8230683SLang Hames EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD); 118e8230683SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D); 119*633a6c91SLang Hames 120*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyBaseType>(&BOD), &BOD); 121*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&BOD), &OD); 122*633a6c91SLang Hames 123*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyBaseType>(&D), &BD); 124*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D); 125*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&D), &MI); 126*633a6c91SLang Hames 127*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyBaseType>(&OD), &BOD); 128*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&OD), &OD); 129*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&OD), &MI); 130*633a6c91SLang Hames 131*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&MI), &D); 132*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&MI), &MI); 133*633a6c91SLang Hames 134*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&MI), &D); 135*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyOtherDerivedType>(&MI), &OD); 136*633a6c91SLang Hames EXPECT_EQ(dyn_cast<MyMultipleInheritanceType>(&MI), &MI); 137*633a6c91SLang Hames } 138*633a6c91SLang Hames 139*633a6c91SLang Hames TEST(ExtensibleRTTI, multiple_inheritance_constructor) { 140*633a6c91SLang Hames MyDerivedTypeWithConstructor V(42); 141e8230683SLang Hames } 142e8230683SLang Hames 143e8230683SLang Hames } // namespace 144