[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / Casting.cpp
blobbebb36fc61f22d7867fea04adb70c362259fdeab
1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Casting.h"
10 #include "llvm/IR/User.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
14 #include <cstdlib>
16 namespace llvm {
17 // Used to test illegal cast. If a cast doesn't match any of the "real" ones,
18 // it will match this one.
19 struct IllegalCast;
20 template <typename T> IllegalCast *cast(...) { return nullptr; }
22 // set up two example classes
23 // with conversion facility
25 struct bar {
26 bar() {}
27 struct foo *baz();
28 struct foo *caz();
29 struct foo *daz();
30 struct foo *naz();
32 private:
33 bar(const bar &);
35 struct foo {
36 foo(const bar &) {}
37 void ext() const;
40 struct base {
41 virtual ~base() {}
44 struct derived : public base {
45 static bool classof(const base *B) { return true; }
48 template <> struct isa_impl<foo, bar> {
49 static inline bool doit(const bar &Val) {
50 dbgs() << "Classof: " << &Val << "\n";
51 return true;
55 // Note for the future - please don't do this. isa_impl is an internal template
56 // for the implementation of `isa` and should not be exposed this way.
57 // Completely unrelated types *should* result in compiler errors if you try to
58 // cast between them.
59 template <typename T> struct isa_impl<foo, T> {
60 static inline bool doit(const T &Val) { return false; }
63 foo *bar::baz() { return cast<foo>(this); }
65 foo *bar::caz() { return cast_or_null<foo>(this); }
67 foo *bar::daz() { return dyn_cast<foo>(this); }
69 foo *bar::naz() { return dyn_cast_or_null<foo>(this); }
71 bar *fub();
73 template <> struct simplify_type<foo> {
74 typedef int SimpleType;
75 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
78 struct T1 {};
80 struct T2 {
81 T2(const T1 &x) {}
82 static bool classof(const T1 *x) { return true; }
85 template <> struct CastInfo<T2, T1> : public OptionalValueCast<T2, T1> {};
87 struct T3 {
88 T3(const T1 *x) : hasValue(x != nullptr) {}
90 static bool classof(const T1 *x) { return true; }
91 bool hasValue = false;
94 // T3 is convertible from a pointer to T1.
95 template <> struct CastInfo<T3, T1 *> : public ValueFromPointerCast<T3, T1> {};
97 struct T4 {
98 T4() : hasValue(false) {}
99 T4(const T3 &x) : hasValue(true) {}
101 static bool classof(const T3 *x) { return true; }
102 bool hasValue = false;
105 template <> struct ValueIsPresent<T3> {
106 using UnwrappedType = T3;
107 static inline bool isPresent(const T3 &t) { return t.hasValue; }
108 static inline const T3 &unwrapValue(const T3 &t) { return t; }
111 template <> struct CastInfo<T4, T3> {
112 using CastResultType = T4;
113 static inline CastResultType doCast(const T3 &t) { return T4(t); }
114 static inline CastResultType castFailed() { return CastResultType(); }
115 static inline CastResultType doCastIfPossible(const T3 &f) {
116 return doCast(f);
120 } // namespace llvm
122 using namespace llvm;
124 // Test the peculiar behavior of Use in simplify_type.
125 static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
126 "Use doesn't simplify correctly!");
127 static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
128 "Use doesn't simplify correctly!");
130 // Test that a regular class behaves as expected.
131 static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
132 "Unexpected simplify_type result!");
133 static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
134 "Unexpected simplify_type result!");
136 namespace {
138 const foo *null_foo = nullptr;
140 bar B;
141 extern bar &B1;
142 bar &B1 = B;
143 extern const bar *B2;
144 // test various configurations of const
145 const bar &B3 = B1;
146 const bar *const B4 = B2;
148 TEST(CastingTest, isa) {
149 EXPECT_TRUE(isa<foo>(B1));
150 EXPECT_TRUE(isa<foo>(B2));
151 EXPECT_TRUE(isa<foo>(B3));
152 EXPECT_TRUE(isa<foo>(B4));
155 TEST(CastingTest, isa_and_nonnull) {
156 EXPECT_TRUE(isa_and_nonnull<foo>(B2));
157 EXPECT_TRUE(isa_and_nonnull<foo>(B4));
158 EXPECT_FALSE(isa_and_nonnull<foo>(fub()));
161 TEST(CastingTest, cast) {
162 foo &F1 = cast<foo>(B1);
163 EXPECT_NE(&F1, null_foo);
164 const foo *F3 = cast<foo>(B2);
165 EXPECT_NE(F3, null_foo);
166 const foo *F4 = cast<foo>(B2);
167 EXPECT_NE(F4, null_foo);
168 const foo &F5 = cast<foo>(B3);
169 EXPECT_NE(&F5, null_foo);
170 const foo *F6 = cast<foo>(B4);
171 EXPECT_NE(F6, null_foo);
172 // Can't pass null pointer to cast<>.
173 // foo *F7 = cast<foo>(fub());
174 // EXPECT_EQ(F7, null_foo);
175 foo *F8 = B1.baz();
176 EXPECT_NE(F8, null_foo);
178 std::unique_ptr<const bar> BP(B2);
179 auto FP = cast<foo>(std::move(BP));
180 static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value,
181 "Incorrect deduced return type!");
182 EXPECT_NE(FP.get(), null_foo);
183 FP.release();
186 TEST(CastingTest, cast_or_null) {
187 const foo *F11 = cast_or_null<foo>(B2);
188 EXPECT_NE(F11, null_foo);
189 const foo *F12 = cast_or_null<foo>(B2);
190 EXPECT_NE(F12, null_foo);
191 const foo *F13 = cast_or_null<foo>(B4);
192 EXPECT_NE(F13, null_foo);
193 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
194 EXPECT_EQ(F14, null_foo);
195 foo *F15 = B1.caz();
196 EXPECT_NE(F15, null_foo);
198 std::unique_ptr<const bar> BP(fub());
199 auto FP = cast_or_null<foo>(std::move(BP));
200 EXPECT_EQ(FP.get(), null_foo);
203 TEST(CastingTest, dyn_cast) {
204 const foo *F1 = dyn_cast<foo>(B2);
205 EXPECT_NE(F1, null_foo);
206 const foo *F2 = dyn_cast<foo>(B2);
207 EXPECT_NE(F2, null_foo);
208 const foo *F3 = dyn_cast<foo>(B4);
209 EXPECT_NE(F3, null_foo);
210 // Can't pass null pointer to dyn_cast<>.
211 // foo *F4 = dyn_cast<foo>(fub());
212 // EXPECT_EQ(F4, null_foo);
213 foo *F5 = B1.daz();
214 EXPECT_NE(F5, null_foo);
217 // All these tests forward to dyn_cast_if_present, so they also provde an
218 // effective test for its use cases.
219 TEST(CastingTest, dyn_cast_or_null) {
220 const foo *F1 = dyn_cast_or_null<foo>(B2);
221 EXPECT_NE(F1, null_foo);
222 const foo *F2 = dyn_cast_or_null<foo>(B2);
223 EXPECT_NE(F2, null_foo);
224 const foo *F3 = dyn_cast_or_null<foo>(B4);
225 EXPECT_NE(F3, null_foo);
226 foo *F4 = dyn_cast_or_null<foo>(fub());
227 EXPECT_EQ(F4, null_foo);
228 foo *F5 = B1.naz();
229 EXPECT_NE(F5, null_foo);
230 // dyn_cast_if_present should have exactly the same behavior as
231 // dyn_cast_or_null.
232 const foo *F6 = dyn_cast_if_present<foo>(B2);
233 EXPECT_EQ(F6, F2);
236 TEST(CastingTest, dyn_cast_value_types) {
237 T1 t1;
238 Optional<T2> t2 = dyn_cast<T2>(t1);
239 EXPECT_TRUE(t2);
241 T2 *t2ptr = dyn_cast<T2>(&t1);
242 EXPECT_TRUE(t2ptr != nullptr);
244 T3 t3 = dyn_cast<T3>(&t1);
245 EXPECT_TRUE(t3.hasValue);
248 TEST(CastingTest, dyn_cast_if_present) {
249 Optional<T1> empty{};
250 Optional<T2> F1 = dyn_cast_if_present<T2>(empty);
251 EXPECT_FALSE(F1.has_value());
253 T1 t1;
254 Optional<T2> F2 = dyn_cast_if_present<T2>(t1);
255 EXPECT_TRUE(F2.has_value());
257 T1 *t1Null = nullptr;
259 // T3 should have hasValue == false because t1Null is nullptr.
260 T3 t3 = dyn_cast_if_present<T3>(t1Null);
261 EXPECT_FALSE(t3.hasValue);
263 // Now because of that, T4 should receive the castFailed implementation of its
264 // FallibleCastTraits, which default-constructs a T4, which has no value.
265 T4 t4 = dyn_cast_if_present<T4>(t3);
266 EXPECT_FALSE(t4.hasValue);
269 std::unique_ptr<derived> newd() { return std::make_unique<derived>(); }
270 std::unique_ptr<base> newb() { return std::make_unique<derived>(); }
272 TEST(CastingTest, unique_dyn_cast) {
273 derived *OrigD = nullptr;
274 auto D = std::make_unique<derived>();
275 OrigD = D.get();
277 // Converting from D to itself is valid, it should return a new unique_ptr
278 // and the old one should become nullptr.
279 auto NewD = unique_dyn_cast<derived>(D);
280 ASSERT_EQ(OrigD, NewD.get());
281 ASSERT_EQ(nullptr, D);
283 // Converting from D to B is valid, B should have a value and D should be
284 // nullptr.
285 auto B = unique_dyn_cast<base>(NewD);
286 ASSERT_EQ(OrigD, B.get());
287 ASSERT_EQ(nullptr, NewD);
289 // Converting from B to itself is valid, it should return a new unique_ptr
290 // and the old one should become nullptr.
291 auto NewB = unique_dyn_cast<base>(B);
292 ASSERT_EQ(OrigD, NewB.get());
293 ASSERT_EQ(nullptr, B);
295 // Converting from B to D is valid, D should have a value and B should be
296 // nullptr;
297 D = unique_dyn_cast<derived>(NewB);
298 ASSERT_EQ(OrigD, D.get());
299 ASSERT_EQ(nullptr, NewB);
301 // This is a very contrived test, casting between completely unrelated types
302 // should generally fail to compile. See the classof shenanigans we have in
303 // the definition of `foo` above.
304 auto F = unique_dyn_cast<foo>(D);
305 ASSERT_EQ(nullptr, F);
306 ASSERT_EQ(OrigD, D.get());
308 // All of the above should also hold for temporaries.
309 auto D2 = unique_dyn_cast<derived>(newd());
310 EXPECT_NE(nullptr, D2);
312 auto B2 = unique_dyn_cast<derived>(newb());
313 EXPECT_NE(nullptr, B2);
315 auto B3 = unique_dyn_cast<base>(newb());
316 EXPECT_NE(nullptr, B3);
318 // This is a very contrived test, casting between completely unrelated types
319 // should generally fail to compile. See the classof shenanigans we have in
320 // the definition of `foo` above.
321 auto F2 = unique_dyn_cast<foo>(newb());
322 EXPECT_EQ(nullptr, F2);
325 // These lines are errors...
326 // foo *F20 = cast<foo>(B2); // Yields const foo*
327 // foo &F21 = cast<foo>(B3); // Yields const foo&
328 // foo *F22 = cast<foo>(B4); // Yields const foo*
329 // foo &F23 = cast_or_null<foo>(B1);
330 // const foo &F24 = cast_or_null<foo>(B3);
332 const bar *B2 = &B;
333 } // anonymous namespace
335 bar *llvm::fub() { return nullptr; }
337 namespace {
338 namespace inferred_upcasting {
339 // This test case verifies correct behavior of inferred upcasts when the
340 // types are statically known to be OK to upcast. This is the case when,
341 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
343 // Note: This test will actually fail to compile without inferred
344 // upcasting.
346 class Base {
347 public:
348 // No classof. We are testing that the upcast is inferred.
349 Base() {}
352 class Derived : public Base {
353 public:
354 Derived() {}
357 // Even with no explicit classof() in Base, we should still be able to cast
358 // Derived to its base class.
359 TEST(CastingTest, UpcastIsInferred) {
360 Derived D;
361 EXPECT_TRUE(isa<Base>(D));
362 Base *BP = dyn_cast<Base>(&D);
363 EXPECT_NE(BP, nullptr);
366 // This test verifies that the inferred upcast takes precedence over an
367 // explicitly written one. This is important because it verifies that the
368 // dynamic check gets optimized away.
369 class UseInferredUpcast {
370 public:
371 int Dummy;
372 static bool classof(const UseInferredUpcast *) { return false; }
375 TEST(CastingTest, InferredUpcastTakesPrecedence) {
376 UseInferredUpcast UIU;
377 // Since the explicit classof() returns false, this will fail if the
378 // explicit one is used.
379 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
382 } // end namespace inferred_upcasting
383 } // end anonymous namespace
385 namespace {
386 namespace pointer_wrappers {
388 struct Base {
389 bool IsDerived;
390 Base(bool IsDerived = false) : IsDerived(IsDerived) {}
393 struct Derived : Base {
394 Derived() : Base(true) {}
395 static bool classof(const Base *B) { return B->IsDerived; }
398 class PTy {
399 Base *B;
401 public:
402 PTy(Base *B) : B(B) {}
403 explicit operator bool() const { return get(); }
404 Base *get() const { return B; }
407 } // end namespace pointer_wrappers
408 } // end namespace
410 namespace llvm {
412 template <> struct ValueIsPresent<pointer_wrappers::PTy> {
413 using UnwrappedType = pointer_wrappers::PTy;
414 static inline bool isPresent(const pointer_wrappers::PTy &P) {
415 return P.get() != nullptr;
417 static UnwrappedType &unwrapValue(pointer_wrappers::PTy &P) { return P; }
420 template <> struct ValueIsPresent<const pointer_wrappers::PTy> {
421 using UnwrappedType = pointer_wrappers::PTy;
422 static inline bool isPresent(const pointer_wrappers::PTy &P) {
423 return P.get() != nullptr;
426 static UnwrappedType &unwrapValue(const pointer_wrappers::PTy &P) {
427 return const_cast<UnwrappedType &>(P);
431 template <> struct simplify_type<pointer_wrappers::PTy> {
432 typedef pointer_wrappers::Base *SimpleType;
433 static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
434 return P.get();
437 template <> struct simplify_type<const pointer_wrappers::PTy> {
438 typedef pointer_wrappers::Base *SimpleType;
439 static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
440 return P.get();
444 } // end namespace llvm
446 namespace {
447 namespace pointer_wrappers {
449 // Some objects.
450 pointer_wrappers::Base B;
451 pointer_wrappers::Derived D;
453 // Mutable "smart" pointers.
454 pointer_wrappers::PTy MN(nullptr);
455 pointer_wrappers::PTy MB(&B);
456 pointer_wrappers::PTy MD(&D);
458 // Const "smart" pointers.
459 const pointer_wrappers::PTy CN(nullptr);
460 const pointer_wrappers::PTy CB(&B);
461 const pointer_wrappers::PTy CD(&D);
463 TEST(CastingTest, smart_isa) {
464 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB));
465 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB));
466 EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD));
467 EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD));
470 TEST(CastingTest, smart_cast) {
471 EXPECT_EQ(cast<pointer_wrappers::Derived>(MD), &D);
472 EXPECT_EQ(cast<pointer_wrappers::Derived>(CD), &D);
475 TEST(CastingTest, smart_cast_or_null) {
476 EXPECT_EQ(cast_or_null<pointer_wrappers::Derived>(MN), nullptr);
477 EXPECT_EQ(cast_or_null<pointer_wrappers::Derived>(CN), nullptr);
478 EXPECT_EQ(cast_or_null<pointer_wrappers::Derived>(MD), &D);
479 EXPECT_EQ(cast_or_null<pointer_wrappers::Derived>(CD), &D);
482 TEST(CastingTest, smart_dyn_cast) {
483 EXPECT_EQ(dyn_cast<pointer_wrappers::Derived>(MB), nullptr);
484 EXPECT_EQ(dyn_cast<pointer_wrappers::Derived>(CB), nullptr);
485 EXPECT_EQ(dyn_cast<pointer_wrappers::Derived>(MD), &D);
486 EXPECT_EQ(dyn_cast<pointer_wrappers::Derived>(CD), &D);
489 TEST(CastingTest, smart_dyn_cast_or_null) {
490 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(MN), nullptr);
491 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(CN), nullptr);
492 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(MB), nullptr);
493 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(CB), nullptr);
494 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(MD), &D);
495 EXPECT_EQ(dyn_cast_or_null<pointer_wrappers::Derived>(CD), &D);
498 } // end namespace pointer_wrappers
500 #ifndef NDEBUG
501 namespace assertion_checks {
502 struct Base {
503 virtual ~Base() {}
506 struct Derived : public Base {
507 static bool classof(const Base *B) { return false; }
510 TEST(CastingTest, assertion_check_const_ref) {
511 const Base B;
512 EXPECT_DEATH((void)cast<Derived>(B), "argument of incompatible type")
513 << "Invalid cast of const ref did not cause an abort()";
516 TEST(CastingTest, assertion_check_ref) {
517 Base B;
518 EXPECT_DEATH((void)cast<Derived>(B), "argument of incompatible type")
519 << "Invalid cast of const ref did not cause an abort()";
522 TEST(CastingTest, assertion_check_ptr) {
523 Base B;
524 EXPECT_DEATH((void)cast<Derived>(&B), "argument of incompatible type")
525 << "Invalid cast of const ref did not cause an abort()";
528 TEST(CastingTest, assertion_check_unique_ptr) {
529 auto B = std::make_unique<Base>();
530 EXPECT_DEATH((void)cast<Derived>(std::move(B)),
531 "argument of incompatible type")
532 << "Invalid cast of const ref did not cause an abort()";
535 } // end namespace assertion_checks
536 #endif
537 } // end namespace