blob: ad564aa366df15d18c979385ca2e742d600a989a [file] [log] [blame]
Gabor Greifb3c90d92010-07-20 19:35:55 +00001//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
Gabor Greifee57dae2010-07-20 16:32:20 +00002//
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
Gabor Greifee57dae2010-07-20 16:32:20 +000010#include "llvm/Support/Casting.h"
Gabor Greifb3c90d92010-07-20 19:35:55 +000011#include "llvm/Support/Debug.h"
12#include "llvm/Support/raw_ostream.h"
Gabor Greifee57dae2010-07-20 16:32:20 +000013
14#include "gtest/gtest.h"
15#include <cstdlib>
16
Gabor Greife8950972010-07-20 17:06:28 +000017namespace llvm {
18
19// set up two example classes
20// with conversion facility
21//
22struct bar {
23 bar() {}
Gabor Greifd1594672010-07-22 15:24:48 +000024 struct foo *baz();
25 struct foo *caz();
Gabor Greiff06eb372010-07-22 15:37:20 +000026 struct foo *daz();
27 struct foo *naz();
Gabor Greife8950972010-07-20 17:06:28 +000028private:
29 bar(const bar &);
30};
31struct foo {
32 void ext() const;
33 /* static bool classof(const bar *X) {
34 cerr << "Classof: " << X << "\n";
35 return true;
36 }*/
37};
38
Gabor Greiff06eb372010-07-22 15:37:20 +000039template <> struct isa_impl<foo, bar> {
Gabor Greife8950972010-07-20 17:06:28 +000040 static inline bool doit(const bar &Val) {
41 dbgs() << "Classof: " << &Val << "\n";
42 return true;
43 }
44};
45
Gabor Greifd1594672010-07-22 15:24:48 +000046foo *bar::baz() {
Gabor Greife8950972010-07-20 17:06:28 +000047 return cast<foo>(this);
Gabor Greifd1594672010-07-22 15:24:48 +000048}
49
50foo *bar::caz() {
51 return cast_or_null<foo>(this);
52}
53
Gabor Greiff06eb372010-07-22 15:37:20 +000054foo *bar::daz() {
Gabor Greifd1594672010-07-22 15:24:48 +000055 return dyn_cast<foo>(this);
Gabor Greiff06eb372010-07-22 15:37:20 +000056}
57
58foo *bar::naz() {
59 return dyn_cast_or_null<foo>(this);
60}
Gabor Greife8950972010-07-20 17:06:28 +000061
62
63bar *fub();
64} // End llvm namespace
65
Gabor Greifee57dae2010-07-20 16:32:20 +000066using namespace llvm;
67
68namespace {
69
Gabor Greif08993c02010-07-20 16:51:18 +000070const foo *null_foo = NULL;
71
NAKAMURA Takumi2a535772012-01-22 12:14:35 +000072bar B;
Gabor Greifee57dae2010-07-20 16:32:20 +000073extern bar &B1;
NAKAMURA Takumi2a535772012-01-22 12:14:35 +000074bar &B1 = B;
Gabor Greifee57dae2010-07-20 16:32:20 +000075extern const bar *B2;
Gabor Greif08993c02010-07-20 16:51:18 +000076// test various configurations of const
77const bar &B3 = B1;
78const bar *const B4 = B2;
Gabor Greifee57dae2010-07-20 16:32:20 +000079
Gabor Greifaf8e2ef2010-07-20 16:38:12 +000080TEST(CastingTest, isa) {
Gabor Greifee57dae2010-07-20 16:32:20 +000081 EXPECT_TRUE(isa<foo>(B1));
Gabor Greifaf8e2ef2010-07-20 16:38:12 +000082 EXPECT_TRUE(isa<foo>(B2));
83 EXPECT_TRUE(isa<foo>(B3));
84 EXPECT_TRUE(isa<foo>(B4));
Gabor Greifee57dae2010-07-20 16:32:20 +000085}
86
Gabor Greif08993c02010-07-20 16:51:18 +000087TEST(CastingTest, cast) {
88 foo &F1 = cast<foo>(B1);
89 EXPECT_NE(&F1, null_foo);
90 const foo *F3 = cast<foo>(B2);
91 EXPECT_NE(F3, null_foo);
92 const foo *F4 = cast<foo>(B2);
93 EXPECT_NE(F4, null_foo);
Gabor Greife8950972010-07-20 17:06:28 +000094 const foo &F5 = cast<foo>(B3);
95 EXPECT_NE(&F5, null_foo);
96 const foo *F6 = cast<foo>(B4);
97 EXPECT_NE(F6, null_foo);
Richard Smith79b59a22012-08-21 20:39:25 +000098 // Can't pass null pointer to cast<>.
99 // foo *F7 = cast<foo>(fub());
100 // EXPECT_EQ(F7, null_foo);
Gabor Greifd1594672010-07-22 15:24:48 +0000101 foo *F8 = B1.baz();
102 EXPECT_NE(F8, null_foo);
Gabor Greif08993c02010-07-20 16:51:18 +0000103}
104
105TEST(CastingTest, cast_or_null) {
106 const foo *F11 = cast_or_null<foo>(B2);
107 EXPECT_NE(F11, null_foo);
108 const foo *F12 = cast_or_null<foo>(B2);
109 EXPECT_NE(F12, null_foo);
110 const foo *F13 = cast_or_null<foo>(B4);
111 EXPECT_NE(F13, null_foo);
112 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
113 EXPECT_EQ(F14, null_foo);
Gabor Greifd1594672010-07-22 15:24:48 +0000114 foo *F15 = B1.caz();
115 EXPECT_NE(F15, null_foo);
116}
117
118TEST(CastingTest, dyn_cast) {
Gabor Greif46a35012010-07-22 15:28:30 +0000119 const foo *F1 = dyn_cast<foo>(B2);
120 EXPECT_NE(F1, null_foo);
121 const foo *F2 = dyn_cast<foo>(B2);
122 EXPECT_NE(F2, null_foo);
123 const foo *F3 = dyn_cast<foo>(B4);
Gabor Greifd1594672010-07-22 15:24:48 +0000124 EXPECT_NE(F3, null_foo);
Richard Smith79b59a22012-08-21 20:39:25 +0000125 // Can't pass null pointer to dyn_cast<>.
126 // foo *F4 = dyn_cast<foo>(fub());
Gabor Greiff06eb372010-07-22 15:37:20 +0000127 // EXPECT_EQ(F4, null_foo);
128 foo *F5 = B1.daz();
129 EXPECT_NE(F5, null_foo);
130}
131
132TEST(CastingTest, dyn_cast_or_null) {
133 const foo *F1 = dyn_cast_or_null<foo>(B2);
134 EXPECT_NE(F1, null_foo);
135 const foo *F2 = dyn_cast_or_null<foo>(B2);
136 EXPECT_NE(F2, null_foo);
137 const foo *F3 = dyn_cast_or_null<foo>(B4);
138 EXPECT_NE(F3, null_foo);
139 foo *F4 = dyn_cast_or_null<foo>(fub());
Gabor Greif46a35012010-07-22 15:28:30 +0000140 EXPECT_EQ(F4, null_foo);
Gabor Greiff06eb372010-07-22 15:37:20 +0000141 foo *F5 = B1.naz();
142 EXPECT_NE(F5, null_foo);
Gabor Greif08993c02010-07-20 16:51:18 +0000143}
144
Gabor Greife8950972010-07-20 17:06:28 +0000145// These lines are errors...
146//foo *F20 = cast<foo>(B2); // Yields const foo*
147//foo &F21 = cast<foo>(B3); // Yields const foo&
148//foo *F22 = cast<foo>(B4); // Yields const foo*
149//foo &F23 = cast_or_null<foo>(B1);
150//const foo &F24 = cast_or_null<foo>(B3);
151
Gabor Greifee57dae2010-07-20 16:32:20 +0000152const bar *B2 = &B;
153} // anonymous namespace
Gabor Greife8950972010-07-20 17:06:28 +0000154
155bar *llvm::fub() { return 0; }
Sean Silva8b8fa7b2012-10-11 23:30:40 +0000156
157namespace {
158namespace inferred_upcasting {
159// This test case verifies correct behavior of inferred upcasts when the
160// types are statically known to be OK to upcast. This is the case when,
161// for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
162
163// Note: This test will actually fail to compile without inferred
164// upcasting.
165
166class Base {
167public:
168 // No classof. We are testing that the upcast is inferred.
169 Base() {}
170};
171
172class Derived : public Base {
173public:
174 Derived() {}
175};
176
177// Even with no explicit classof() in Base, we should still be able to cast
178// Derived to its base class.
179TEST(CastingTest, UpcastIsInferred) {
180 Derived D;
181 EXPECT_TRUE(isa<Base>(D));
182 Base *BP = dyn_cast<Base>(&D);
183 EXPECT_TRUE(BP != NULL);
184}
185
186
187// This test verifies that the inferred upcast takes precedence over an
188// explicitly written one. This is important because it verifies that the
189// dynamic check gets optimized away.
190class UseInferredUpcast {
191public:
192 int Dummy;
193 static bool classof(const UseInferredUpcast *) {
194 return false;
195 }
196};
197
198TEST(CastingTest, InferredUpcastTakesPrecedence) {
199 UseInferredUpcast UIU;
200 // Since the explicit classof() returns false, this will fail if the
201 // explicit one is used.
202 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
203}
204
205} // end namespace inferred_upcasting
206} // end anonymous namespace