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 nullptr; }
23 // set up two example classes
24 // with conversion facility
37 /* static bool classof(const bar *X) {
38 cerr << "Classof: " << X << "\n";
47 struct derived : public base {
48 static bool classof(const base *B) { return true; }
51 template <> struct isa_impl<foo, bar> {
52 static inline bool doit(const bar &Val) {
53 dbgs() << "Classof: " << &Val << "\n";
58 template <typename T> struct isa_impl<foo, T> {
59 static inline bool doit(const T &Val) { return false; }
63 return cast<foo>(this);
67 return cast_or_null<foo>(this);
71 return dyn_cast<foo>(this);
75 return dyn_cast_or_null<foo>(this);
81 template <> struct simplify_type<foo> {
82 typedef int SimpleType;
83 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
86 } // End llvm namespace
91 // Test the peculiar behavior of Use in simplify_type.
92 static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
93 "Use doesn't simplify correctly!");
94 static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
95 "Use doesn't simplify correctly!");
97 // Test that a regular class behaves as expected.
98 static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
99 "Unexpected simplify_type result!");
100 static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
101 "Unexpected simplify_type result!");
105 const foo *null_foo = nullptr;
110 extern const bar *B2;
111 // test various configurations of const
113 const bar *const B4 = B2;
115 TEST(CastingTest, isa) {
116 EXPECT_TRUE(isa<foo>(B1));
117 EXPECT_TRUE(isa<foo>(B2));
118 EXPECT_TRUE(isa<foo>(B3));
119 EXPECT_TRUE(isa<foo>(B4));
122 TEST(CastingTest, cast) {
123 foo &F1 = cast<foo>(B1);
124 EXPECT_NE(&F1, null_foo);
125 const foo *F3 = cast<foo>(B2);
126 EXPECT_NE(F3, null_foo);
127 const foo *F4 = cast<foo>(B2);
128 EXPECT_NE(F4, null_foo);
129 const foo &F5 = cast<foo>(B3);
130 EXPECT_NE(&F5, null_foo);
131 const foo *F6 = cast<foo>(B4);
132 EXPECT_NE(F6, null_foo);
133 // Can't pass null pointer to cast<>.
134 // foo *F7 = cast<foo>(fub());
135 // EXPECT_EQ(F7, null_foo);
137 EXPECT_NE(F8, null_foo);
139 std::unique_ptr<const bar> BP(B2);
140 auto FP = cast<foo>(std::move(BP));
141 static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value,
142 "Incorrect deduced return type!");
143 EXPECT_NE(FP.get(), null_foo);
147 TEST(CastingTest, cast_or_null) {
148 const foo *F11 = cast_or_null<foo>(B2);
149 EXPECT_NE(F11, null_foo);
150 const foo *F12 = cast_or_null<foo>(B2);
151 EXPECT_NE(F12, null_foo);
152 const foo *F13 = cast_or_null<foo>(B4);
153 EXPECT_NE(F13, null_foo);
154 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
155 EXPECT_EQ(F14, null_foo);
157 EXPECT_NE(F15, null_foo);
159 std::unique_ptr<const bar> BP(fub());
160 auto FP = cast_or_null<foo>(std::move(BP));
161 EXPECT_EQ(FP.get(), null_foo);
164 TEST(CastingTest, dyn_cast) {
165 const foo *F1 = dyn_cast<foo>(B2);
166 EXPECT_NE(F1, null_foo);
167 const foo *F2 = dyn_cast<foo>(B2);
168 EXPECT_NE(F2, null_foo);
169 const foo *F3 = dyn_cast<foo>(B4);
170 EXPECT_NE(F3, null_foo);
171 // Can't pass null pointer to dyn_cast<>.
172 // foo *F4 = dyn_cast<foo>(fub());
173 // EXPECT_EQ(F4, null_foo);
175 EXPECT_NE(F5, null_foo);
178 TEST(CastingTest, dyn_cast_or_null) {
179 const foo *F1 = dyn_cast_or_null<foo>(B2);
180 EXPECT_NE(F1, null_foo);
181 const foo *F2 = dyn_cast_or_null<foo>(B2);
182 EXPECT_NE(F2, null_foo);
183 const foo *F3 = dyn_cast_or_null<foo>(B4);
184 EXPECT_NE(F3, null_foo);
185 foo *F4 = dyn_cast_or_null<foo>(fub());
186 EXPECT_EQ(F4, null_foo);
188 EXPECT_NE(F5, null_foo);
191 std::unique_ptr<derived> newd() { return llvm::make_unique<derived>(); }
192 std::unique_ptr<base> newb() { return llvm::make_unique<derived>(); }
194 TEST(CastingTest, unique_dyn_cast) {
195 derived *OrigD = nullptr;
196 auto D = llvm::make_unique<derived>();
199 // Converting from D to itself is valid, it should return a new unique_ptr
200 // and the old one should become nullptr.
201 auto NewD = unique_dyn_cast<derived>(D);
202 ASSERT_EQ(OrigD, NewD.get());
203 ASSERT_EQ(nullptr, D);
205 // Converting from D to B is valid, B should have a value and D should be
207 auto B = unique_dyn_cast<base>(NewD);
208 ASSERT_EQ(OrigD, B.get());
209 ASSERT_EQ(nullptr, NewD);
211 // Converting from B to itself is valid, it should return a new unique_ptr
212 // and the old one should become nullptr.
213 auto NewB = unique_dyn_cast<base>(B);
214 ASSERT_EQ(OrigD, NewB.get());
215 ASSERT_EQ(nullptr, B);
217 // Converting from B to D is valid, D should have a value and B should be
219 D = unique_dyn_cast<derived>(NewB);
220 ASSERT_EQ(OrigD, D.get());
221 ASSERT_EQ(nullptr, NewB);
223 // Converting between unrelated types should fail. The original value should
224 // remain unchanged and it should return nullptr.
225 auto F = unique_dyn_cast<foo>(D);
226 ASSERT_EQ(nullptr, F);
227 ASSERT_EQ(OrigD, D.get());
229 // All of the above should also hold for temporaries.
230 auto D2 = unique_dyn_cast<derived>(newd());
231 EXPECT_NE(nullptr, D2);
233 auto B2 = unique_dyn_cast<derived>(newb());
234 EXPECT_NE(nullptr, B2);
236 auto B3 = unique_dyn_cast<base>(newb());
237 EXPECT_NE(nullptr, B3);
239 auto F2 = unique_dyn_cast<foo>(newb());
240 EXPECT_EQ(nullptr, F2);
243 // These lines are errors...
244 //foo *F20 = cast<foo>(B2); // Yields const foo*
245 //foo &F21 = cast<foo>(B3); // Yields const foo&
246 //foo *F22 = cast<foo>(B4); // Yields const foo*
247 //foo &F23 = cast_or_null<foo>(B1);
248 //const foo &F24 = cast_or_null<foo>(B3);
251 } // anonymous namespace
253 bar *llvm::fub() { return nullptr; }
256 namespace inferred_upcasting {
257 // This test case verifies correct behavior of inferred upcasts when the
258 // types are statically known to be OK to upcast. This is the case when,
259 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
261 // Note: This test will actually fail to compile without inferred
266 // No classof. We are testing that the upcast is inferred.
270 class Derived : public Base {
275 // Even with no explicit classof() in Base, we should still be able to cast
276 // Derived to its base class.
277 TEST(CastingTest, UpcastIsInferred) {
279 EXPECT_TRUE(isa<Base>(D));
280 Base *BP = dyn_cast<Base>(&D);
281 EXPECT_TRUE(BP != nullptr);
285 // This test verifies that the inferred upcast takes precedence over an
286 // explicitly written one. This is important because it verifies that the
287 // dynamic check gets optimized away.
288 class UseInferredUpcast {
291 static bool classof(const UseInferredUpcast *) {
296 TEST(CastingTest, InferredUpcastTakesPrecedence) {
297 UseInferredUpcast UIU;
298 // Since the explicit classof() returns false, this will fail if the
299 // explicit one is used.
300 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
303 } // end namespace inferred_upcasting
304 } // end anonymous namespace
305 // Test that we reject casts of temporaries (and so the illegal cast gets used).
306 namespace TemporaryCast {
308 IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
312 namespace pointer_wrappers {
316 Base(bool IsDerived = false) : IsDerived(IsDerived) {}
319 struct Derived : Base {
320 Derived() : Base(true) {}
321 static bool classof(const Base *B) { return B->IsDerived; }
327 PTy(Base *B) : B(B) {}
328 explicit operator bool() const { return get(); }
329 Base *get() const { return B; }
332 } // end namespace pointer_wrappers
337 template <> struct simplify_type<pointer_wrappers::PTy> {
338 typedef pointer_wrappers::Base *SimpleType;
339 static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
343 template <> struct simplify_type<const pointer_wrappers::PTy> {
344 typedef pointer_wrappers::Base *SimpleType;
345 static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
350 } // end namespace llvm
353 namespace pointer_wrappers {
356 pointer_wrappers::Base B;
357 pointer_wrappers::Derived D;
359 // Mutable "smart" pointers.
360 pointer_wrappers::PTy MN(nullptr);
361 pointer_wrappers::PTy MB(&B);
362 pointer_wrappers::PTy MD(&D);
364 // Const "smart" pointers.
365 const pointer_wrappers::PTy CN(nullptr);
366 const pointer_wrappers::PTy CB(&B);
367 const pointer_wrappers::PTy CD(&D);
369 TEST(CastingTest, smart_isa) {
370 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB));
371 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB));
372 EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD));
373 EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD));
376 TEST(CastingTest, smart_cast) {
377 EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D);
378 EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D);
381 TEST(CastingTest, smart_cast_or_null) {
382 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
383 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
384 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MD) == &D);
385 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CD) == &D);
388 TEST(CastingTest, smart_dyn_cast) {
389 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MB) == nullptr);
390 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CB) == nullptr);
391 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MD) == &D);
392 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CD) == &D);
395 TEST(CastingTest, smart_dyn_cast_or_null) {
396 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
397 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
398 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MB) == nullptr);
399 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CB) == nullptr);
400 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MD) == &D);
401 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CD) == &D);
404 } // end namespace pointer_wrappers