1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Casting.h"
11 #include "llvm/IR/User.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
18 // Used to test illegal cast. If a cast doesn't match any of the "real" ones,
19 // it will match this one.
21 template <typename T> IllegalCast *cast(...) { return 0; }
23 // set up two example classes
24 // with conversion facility
37 /* static bool classof(const bar *X) {
38 cerr << "Classof: " << X << "\n";
43 template <> struct isa_impl<foo, bar> {
44 static inline bool doit(const bar &Val) {
45 dbgs() << "Classof: " << &Val << "\n";
51 return cast<foo>(this);
55 return cast_or_null<foo>(this);
59 return dyn_cast<foo>(this);
63 return dyn_cast_or_null<foo>(this);
69 template <> struct simplify_type<foo> {
70 typedef int SimpleType;
71 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
74 } // End llvm namespace
79 // Test the peculiar behavior of Use in simplify_type.
80 int Check1[is_same<simplify_type<Use>::SimpleType, Value *>::value ? 1 : -1];
81 int Check2[is_same<simplify_type<Use *>::SimpleType, Value *>::value ? 1 : -1];
83 // Test that a regular class behaves as expected.
84 int Check3[is_same<simplify_type<foo>::SimpleType, int>::value ? 1 : -1];
85 int Check4[is_same<simplify_type<foo *>::SimpleType, foo *>::value ? 1 : -1];
89 const foo *null_foo = NULL;
95 // test various configurations of const
97 const bar *const B4 = B2;
99 TEST(CastingTest, isa) {
100 EXPECT_TRUE(isa<foo>(B1));
101 EXPECT_TRUE(isa<foo>(B2));
102 EXPECT_TRUE(isa<foo>(B3));
103 EXPECT_TRUE(isa<foo>(B4));
106 TEST(CastingTest, cast) {
107 foo &F1 = cast<foo>(B1);
108 EXPECT_NE(&F1, null_foo);
109 const foo *F3 = cast<foo>(B2);
110 EXPECT_NE(F3, null_foo);
111 const foo *F4 = cast<foo>(B2);
112 EXPECT_NE(F4, null_foo);
113 const foo &F5 = cast<foo>(B3);
114 EXPECT_NE(&F5, null_foo);
115 const foo *F6 = cast<foo>(B4);
116 EXPECT_NE(F6, null_foo);
117 // Can't pass null pointer to cast<>.
118 // foo *F7 = cast<foo>(fub());
119 // EXPECT_EQ(F7, null_foo);
121 EXPECT_NE(F8, null_foo);
124 TEST(CastingTest, cast_or_null) {
125 const foo *F11 = cast_or_null<foo>(B2);
126 EXPECT_NE(F11, null_foo);
127 const foo *F12 = cast_or_null<foo>(B2);
128 EXPECT_NE(F12, null_foo);
129 const foo *F13 = cast_or_null<foo>(B4);
130 EXPECT_NE(F13, null_foo);
131 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
132 EXPECT_EQ(F14, null_foo);
134 EXPECT_NE(F15, null_foo);
137 TEST(CastingTest, dyn_cast) {
138 const foo *F1 = dyn_cast<foo>(B2);
139 EXPECT_NE(F1, null_foo);
140 const foo *F2 = dyn_cast<foo>(B2);
141 EXPECT_NE(F2, null_foo);
142 const foo *F3 = dyn_cast<foo>(B4);
143 EXPECT_NE(F3, null_foo);
144 // Can't pass null pointer to dyn_cast<>.
145 // foo *F4 = dyn_cast<foo>(fub());
146 // EXPECT_EQ(F4, null_foo);
148 EXPECT_NE(F5, null_foo);
151 TEST(CastingTest, dyn_cast_or_null) {
152 const foo *F1 = dyn_cast_or_null<foo>(B2);
153 EXPECT_NE(F1, null_foo);
154 const foo *F2 = dyn_cast_or_null<foo>(B2);
155 EXPECT_NE(F2, null_foo);
156 const foo *F3 = dyn_cast_or_null<foo>(B4);
157 EXPECT_NE(F3, null_foo);
158 foo *F4 = dyn_cast_or_null<foo>(fub());
159 EXPECT_EQ(F4, null_foo);
161 EXPECT_NE(F5, null_foo);
164 // These lines are errors...
165 //foo *F20 = cast<foo>(B2); // Yields const foo*
166 //foo &F21 = cast<foo>(B3); // Yields const foo&
167 //foo *F22 = cast<foo>(B4); // Yields const foo*
168 //foo &F23 = cast_or_null<foo>(B1);
169 //const foo &F24 = cast_or_null<foo>(B3);
172 } // anonymous namespace
174 bar *llvm::fub() { return 0; }
177 namespace inferred_upcasting {
178 // This test case verifies correct behavior of inferred upcasts when the
179 // types are statically known to be OK to upcast. This is the case when,
180 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
182 // Note: This test will actually fail to compile without inferred
187 // No classof. We are testing that the upcast is inferred.
191 class Derived : public Base {
196 // Even with no explicit classof() in Base, we should still be able to cast
197 // Derived to its base class.
198 TEST(CastingTest, UpcastIsInferred) {
200 EXPECT_TRUE(isa<Base>(D));
201 Base *BP = dyn_cast<Base>(&D);
202 EXPECT_TRUE(BP != NULL);
206 // This test verifies that the inferred upcast takes precedence over an
207 // explicitly written one. This is important because it verifies that the
208 // dynamic check gets optimized away.
209 class UseInferredUpcast {
212 static bool classof(const UseInferredUpcast *) {
217 TEST(CastingTest, InferredUpcastTakesPrecedence) {
218 UseInferredUpcast UIU;
219 // Since the explicit classof() returns false, this will fail if the
220 // explicit one is used.
221 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
224 } // end namespace inferred_upcasting
225 } // end anonymous namespace
226 // Test that we reject casts of temporaries (and so the illegal cast gets used).
227 namespace TemporaryCast {
229 IllegalCast *testIllegalCast() { return cast<foo>(pod()); }