blob: 9a818f6bdebde085b069a52eb346655df24244d4 [file] [log] [blame]
Gabor Greifdd8ba3c2010-07-20 19:35:55 +00001//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
Gabor Greif704524a2010-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 Greif704524a2010-07-20 16:32:20 +000010#include "llvm/Support/Casting.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000011#include "llvm/IR/User.h"
Gabor Greifdd8ba3c2010-07-20 19:35:55 +000012#include "llvm/Support/Debug.h"
13#include "llvm/Support/raw_ostream.h"
Gabor Greif704524a2010-07-20 16:32:20 +000014#include "gtest/gtest.h"
15#include <cstdlib>
16
Gabor Greifc4dc3932010-07-20 17:06:28 +000017namespace llvm {
Rafael Espindola9ed17612013-07-18 02:42:40 +000018// Used to test illegal cast. If a cast doesn't match any of the "real" ones,
19// it will match this one.
20struct IllegalCast;
Craig Topper66f09ad2014-06-08 22:29:17 +000021template <typename T> IllegalCast *cast(...) { return nullptr; }
Gabor Greifc4dc3932010-07-20 17:06:28 +000022
23// set up two example classes
24// with conversion facility
25//
26struct bar {
27 bar() {}
Gabor Greifb9e69772010-07-22 15:24:48 +000028 struct foo *baz();
29 struct foo *caz();
Gabor Greiffd23a972010-07-22 15:37:20 +000030 struct foo *daz();
31 struct foo *naz();
Gabor Greifc4dc3932010-07-20 17:06:28 +000032private:
33 bar(const bar &);
34};
35struct foo {
36 void ext() const;
37 /* static bool classof(const bar *X) {
38 cerr << "Classof: " << X << "\n";
39 return true;
40 }*/
41};
42
Zachary Turner2bb94cd2017-04-12 19:59:37 +000043struct base {
44 virtual ~base() {}
45};
46
47struct derived : public base {
48 static bool classof(const base *B) { return true; }
49};
50
Gabor Greiffd23a972010-07-22 15:37:20 +000051template <> struct isa_impl<foo, bar> {
Gabor Greifc4dc3932010-07-20 17:06:28 +000052 static inline bool doit(const bar &Val) {
53 dbgs() << "Classof: " << &Val << "\n";
54 return true;
55 }
56};
57
Zachary Turner2bb94cd2017-04-12 19:59:37 +000058template <typename T> struct isa_impl<foo, T> {
59 static inline bool doit(const T &Val) { return false; }
60};
61
Gabor Greifb9e69772010-07-22 15:24:48 +000062foo *bar::baz() {
Gabor Greifc4dc3932010-07-20 17:06:28 +000063 return cast<foo>(this);
Gabor Greifb9e69772010-07-22 15:24:48 +000064}
65
66foo *bar::caz() {
67 return cast_or_null<foo>(this);
68}
69
Gabor Greiffd23a972010-07-22 15:37:20 +000070foo *bar::daz() {
Gabor Greifb9e69772010-07-22 15:24:48 +000071 return dyn_cast<foo>(this);
Gabor Greiffd23a972010-07-22 15:37:20 +000072}
73
74foo *bar::naz() {
75 return dyn_cast_or_null<foo>(this);
76}
Gabor Greifc4dc3932010-07-20 17:06:28 +000077
78
79bar *fub();
Rafael Espindola9ed17612013-07-18 02:42:40 +000080
81template <> struct simplify_type<foo> {
82 typedef int SimpleType;
83 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
84};
85
Gabor Greifc4dc3932010-07-20 17:06:28 +000086} // End llvm namespace
87
Gabor Greif704524a2010-07-20 16:32:20 +000088using namespace llvm;
89
Rafael Espindola9ed17612013-07-18 02:42:40 +000090
91// Test the peculiar behavior of Use in simplify_type.
Benjamin Kramerf04ddd02014-03-07 14:42:25 +000092static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
93 "Use doesn't simplify correctly!");
94static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
95 "Use doesn't simplify correctly!");
Rafael Espindola9ed17612013-07-18 02:42:40 +000096
97// Test that a regular class behaves as expected.
Benjamin Kramerf04ddd02014-03-07 14:42:25 +000098static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
99 "Unexpected simplify_type result!");
100static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
101 "Unexpected simplify_type result!");
Rafael Espindola9ed17612013-07-18 02:42:40 +0000102
Gabor Greif704524a2010-07-20 16:32:20 +0000103namespace {
104
Craig Topper66f09ad2014-06-08 22:29:17 +0000105const foo *null_foo = nullptr;
Gabor Greif1804d302010-07-20 16:51:18 +0000106
NAKAMURA Takumi31194402012-01-22 12:14:35 +0000107bar B;
Gabor Greif704524a2010-07-20 16:32:20 +0000108extern bar &B1;
NAKAMURA Takumi31194402012-01-22 12:14:35 +0000109bar &B1 = B;
Gabor Greif704524a2010-07-20 16:32:20 +0000110extern const bar *B2;
Gabor Greif1804d302010-07-20 16:51:18 +0000111// test various configurations of const
112const bar &B3 = B1;
113const bar *const B4 = B2;
Gabor Greif704524a2010-07-20 16:32:20 +0000114
Gabor Greif3eae1b32010-07-20 16:38:12 +0000115TEST(CastingTest, isa) {
Gabor Greif704524a2010-07-20 16:32:20 +0000116 EXPECT_TRUE(isa<foo>(B1));
Gabor Greif3eae1b32010-07-20 16:38:12 +0000117 EXPECT_TRUE(isa<foo>(B2));
118 EXPECT_TRUE(isa<foo>(B3));
119 EXPECT_TRUE(isa<foo>(B4));
Gabor Greif704524a2010-07-20 16:32:20 +0000120}
121
Gabor Greif1804d302010-07-20 16:51:18 +0000122TEST(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);
Gabor Greifc4dc3932010-07-20 17:06:28 +0000129 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);
Richard Smithc4379c32012-08-21 20:39:25 +0000133 // Can't pass null pointer to cast<>.
134 // foo *F7 = cast<foo>(fub());
135 // EXPECT_EQ(F7, null_foo);
Gabor Greifb9e69772010-07-22 15:24:48 +0000136 foo *F8 = B1.baz();
137 EXPECT_NE(F8, null_foo);
Zachary Turner2bb94cd2017-04-12 19:59:37 +0000138
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);
144 FP.release();
Gabor Greif1804d302010-07-20 16:51:18 +0000145}
146
147TEST(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);
Gabor Greifb9e69772010-07-22 15:24:48 +0000156 foo *F15 = B1.caz();
157 EXPECT_NE(F15, null_foo);
Zachary Turner2bb94cd2017-04-12 19:59:37 +0000158
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);
Gabor Greifb9e69772010-07-22 15:24:48 +0000162}
163
164TEST(CastingTest, dyn_cast) {
Gabor Greif78595812010-07-22 15:28:30 +0000165 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);
Gabor Greifb9e69772010-07-22 15:24:48 +0000170 EXPECT_NE(F3, null_foo);
Richard Smithc4379c32012-08-21 20:39:25 +0000171 // Can't pass null pointer to dyn_cast<>.
172 // foo *F4 = dyn_cast<foo>(fub());
Gabor Greiffd23a972010-07-22 15:37:20 +0000173 // EXPECT_EQ(F4, null_foo);
174 foo *F5 = B1.daz();
175 EXPECT_NE(F5, null_foo);
176}
177
178TEST(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());
Gabor Greif78595812010-07-22 15:28:30 +0000186 EXPECT_EQ(F4, null_foo);
Gabor Greiffd23a972010-07-22 15:37:20 +0000187 foo *F5 = B1.naz();
188 EXPECT_NE(F5, null_foo);
Gabor Greif1804d302010-07-20 16:51:18 +0000189}
190
Zachary Turner2bb94cd2017-04-12 19:59:37 +0000191std::unique_ptr<derived> newd() { return llvm::make_unique<derived>(); }
192std::unique_ptr<base> newb() { return llvm::make_unique<derived>(); }
193
194TEST(CastingTest, unique_dyn_cast) {
195 derived *OrigD = nullptr;
196 auto D = llvm::make_unique<derived>();
197 OrigD = D.get();
198
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);
204
205 // Converting from D to B is valid, B should have a value and D should be
206 // nullptr.
207 auto B = unique_dyn_cast<base>(NewD);
208 ASSERT_EQ(OrigD, B.get());
209 ASSERT_EQ(nullptr, NewD);
210
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);
216
217 // Converting from B to D is valid, D should have a value and B should be
218 // nullptr;
219 D = unique_dyn_cast<derived>(NewB);
220 ASSERT_EQ(OrigD, D.get());
221 ASSERT_EQ(nullptr, NewB);
222
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());
228
229 // All of the above should also hold for temporaries.
230 auto D2 = unique_dyn_cast<derived>(newd());
231 EXPECT_NE(nullptr, D2);
232
233 auto B2 = unique_dyn_cast<derived>(newb());
234 EXPECT_NE(nullptr, B2);
235
236 auto B3 = unique_dyn_cast<base>(newb());
237 EXPECT_NE(nullptr, B3);
238
239 auto F2 = unique_dyn_cast<foo>(newb());
240 EXPECT_EQ(nullptr, F2);
241}
242
Gabor Greifc4dc3932010-07-20 17:06:28 +0000243// 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);
249
Gabor Greif704524a2010-07-20 16:32:20 +0000250const bar *B2 = &B;
251} // anonymous namespace
Gabor Greifc4dc3932010-07-20 17:06:28 +0000252
Craig Topper66f09ad2014-06-08 22:29:17 +0000253bar *llvm::fub() { return nullptr; }
Sean Silva35dd8772012-10-11 23:30:40 +0000254
255namespace {
256namespace 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)`.
260
261// Note: This test will actually fail to compile without inferred
262// upcasting.
263
264class Base {
265public:
266 // No classof. We are testing that the upcast is inferred.
267 Base() {}
268};
269
270class Derived : public Base {
271public:
272 Derived() {}
273};
274
275// Even with no explicit classof() in Base, we should still be able to cast
276// Derived to its base class.
277TEST(CastingTest, UpcastIsInferred) {
278 Derived D;
279 EXPECT_TRUE(isa<Base>(D));
280 Base *BP = dyn_cast<Base>(&D);
Craig Topper66f09ad2014-06-08 22:29:17 +0000281 EXPECT_TRUE(BP != nullptr);
Sean Silva35dd8772012-10-11 23:30:40 +0000282}
283
284
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.
288class UseInferredUpcast {
289public:
290 int Dummy;
291 static bool classof(const UseInferredUpcast *) {
292 return false;
293 }
294};
295
296TEST(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));
301}
302
303} // end namespace inferred_upcasting
304} // end anonymous namespace
Rafael Espindola9ed17612013-07-18 02:42:40 +0000305// Test that we reject casts of temporaries (and so the illegal cast gets used).
306namespace TemporaryCast {
307struct pod {};
308IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
309}
Duncan P. N. Exon Smith40301182014-11-24 03:13:02 +0000310
311namespace {
312namespace pointer_wrappers {
313
314struct Base {
315 bool IsDerived;
316 Base(bool IsDerived = false) : IsDerived(IsDerived) {}
317};
318
319struct Derived : Base {
320 Derived() : Base(true) {}
321 static bool classof(const Base *B) { return B->IsDerived; }
322};
323
324class PTy {
325 Base *B;
326public:
327 PTy(Base *B) : B(B) {}
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000328 explicit operator bool() const { return get(); }
Duncan P. N. Exon Smith40301182014-11-24 03:13:02 +0000329 Base *get() const { return B; }
330};
331
332} // end namespace pointer_wrappers
333} // end namespace
334
335namespace llvm {
336
337template <> struct simplify_type<pointer_wrappers::PTy> {
338 typedef pointer_wrappers::Base *SimpleType;
339 static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
340 return P.get();
341 }
342};
343template <> struct simplify_type<const pointer_wrappers::PTy> {
344 typedef pointer_wrappers::Base *SimpleType;
345 static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
346 return P.get();
347 }
348};
349
350} // end namespace llvm
351
352namespace {
353namespace pointer_wrappers {
354
355// Some objects.
356pointer_wrappers::Base B;
357pointer_wrappers::Derived D;
358
359// Mutable "smart" pointers.
360pointer_wrappers::PTy MN(nullptr);
361pointer_wrappers::PTy MB(&B);
362pointer_wrappers::PTy MD(&D);
363
364// Const "smart" pointers.
365const pointer_wrappers::PTy CN(nullptr);
366const pointer_wrappers::PTy CB(&B);
367const pointer_wrappers::PTy CD(&D);
368
369TEST(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));
374}
375
376TEST(CastingTest, smart_cast) {
377 EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D);
378 EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D);
379}
380
381TEST(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);
386}
387
388TEST(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);
393}
394
395TEST(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);
402}
403
404} // end namespace pointer_wrappers
405} // end namespace