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
39e8230683SLang Hames char MyBaseType::ID = 0;
40e8230683SLang Hames char MyDerivedType::ID = 0;
41e8230683SLang Hames char MyOtherDerivedType::ID = 0;
42e8230683SLang Hames char MyDeeperDerivedType::ID = 0;
43e8230683SLang Hames
TEST(ExtensibleRTTI,isa)44e8230683SLang Hames TEST(ExtensibleRTTI, isa) {
45e8230683SLang Hames MyBaseType B;
46e8230683SLang Hames MyDerivedType D;
47e8230683SLang Hames MyDeeperDerivedType DD;
48e8230683SLang Hames
49e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(B));
50e8230683SLang Hames EXPECT_FALSE(isa<MyDerivedType>(B));
51e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(B));
52e8230683SLang Hames EXPECT_FALSE(isa<MyDeeperDerivedType>(B));
53e8230683SLang Hames
54e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(D));
55e8230683SLang Hames EXPECT_TRUE(isa<MyDerivedType>(D));
56e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(D));
57e8230683SLang Hames EXPECT_FALSE(isa<MyDeeperDerivedType>(D));
58e8230683SLang Hames
59e8230683SLang Hames EXPECT_TRUE(isa<MyBaseType>(DD));
60e8230683SLang Hames EXPECT_TRUE(isa<MyDerivedType>(DD));
61e8230683SLang Hames EXPECT_FALSE(isa<MyOtherDerivedType>(DD));
62e8230683SLang Hames EXPECT_TRUE(isa<MyDeeperDerivedType>(DD));
63e8230683SLang Hames }
64e8230683SLang Hames
TEST(ExtensibleRTTI,cast)65e8230683SLang Hames TEST(ExtensibleRTTI, cast) {
66e8230683SLang Hames MyDerivedType D;
67e8230683SLang Hames MyBaseType &BD = D;
68e8230683SLang Hames
69*52328dafSKrasimir Georgiev (void)cast<MyBaseType>(D);
70*52328dafSKrasimir Georgiev (void)cast<MyBaseType>(BD);
71*52328dafSKrasimir Georgiev (void)cast<MyDerivedType>(BD);
72e8230683SLang Hames }
73e8230683SLang Hames
TEST(ExtensibleRTTI,dyn_cast)74e8230683SLang Hames TEST(ExtensibleRTTI, dyn_cast) {
75e8230683SLang Hames MyBaseType B;
76e8230683SLang Hames MyDerivedType D;
77e8230683SLang Hames MyBaseType &BD = D;
78e8230683SLang Hames
79e8230683SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&B), nullptr);
80e8230683SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
81e8230683SLang Hames EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD);
82e8230683SLang Hames EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D);
83e8230683SLang Hames }
84e8230683SLang Hames
85e8230683SLang Hames } // namespace
86