Gabor Greif | b3c90d9 | 2010-07-20 19:35:55 +0000 | [diff] [blame] | 1 | //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===// |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 2 | // |
| 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 Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Casting.h" |
Gabor Greif | b3c90d9 | 2010-07-20 19:35:55 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Debug.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | #include <cstdlib> |
| 16 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 17 | namespace llvm { |
| 18 | |
| 19 | // set up two example classes |
| 20 | // with conversion facility |
| 21 | // |
| 22 | struct bar { |
| 23 | bar() {} |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 24 | struct foo *baz(); |
| 25 | struct foo *caz(); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 26 | struct foo *daz(); |
| 27 | struct foo *naz(); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 28 | private: |
| 29 | bar(const bar &); |
| 30 | }; |
| 31 | struct foo { |
| 32 | void ext() const; |
| 33 | /* static bool classof(const bar *X) { |
| 34 | cerr << "Classof: " << X << "\n"; |
| 35 | return true; |
| 36 | }*/ |
| 37 | }; |
| 38 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 39 | template <> struct isa_impl<foo, bar> { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 40 | static inline bool doit(const bar &Val) { |
| 41 | dbgs() << "Classof: " << &Val << "\n"; |
| 42 | return true; |
| 43 | } |
| 44 | }; |
| 45 | |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 46 | foo *bar::baz() { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 47 | return cast<foo>(this); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | foo *bar::caz() { |
| 51 | return cast_or_null<foo>(this); |
| 52 | } |
| 53 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 54 | foo *bar::daz() { |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 55 | return dyn_cast<foo>(this); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | foo *bar::naz() { |
| 59 | return dyn_cast_or_null<foo>(this); |
| 60 | } |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | bar *fub(); |
| 64 | } // End llvm namespace |
| 65 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 66 | using namespace llvm; |
| 67 | |
| 68 | namespace { |
| 69 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 70 | const foo *null_foo = NULL; |
| 71 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 72 | extern bar &B1; |
| 73 | extern const bar *B2; |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 74 | // test various configurations of const |
| 75 | const bar &B3 = B1; |
| 76 | const bar *const B4 = B2; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 77 | |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 78 | TEST(CastingTest, isa) { |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 79 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 80 | EXPECT_TRUE(isa<foo>(B2)); |
| 81 | EXPECT_TRUE(isa<foo>(B3)); |
| 82 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 85 | TEST(CastingTest, cast) { |
| 86 | foo &F1 = cast<foo>(B1); |
| 87 | EXPECT_NE(&F1, null_foo); |
| 88 | const foo *F3 = cast<foo>(B2); |
| 89 | EXPECT_NE(F3, null_foo); |
| 90 | const foo *F4 = cast<foo>(B2); |
| 91 | EXPECT_NE(F4, null_foo); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 92 | const foo &F5 = cast<foo>(B3); |
| 93 | EXPECT_NE(&F5, null_foo); |
| 94 | const foo *F6 = cast<foo>(B4); |
| 95 | EXPECT_NE(F6, null_foo); |
| 96 | foo *F7 = cast<foo>(fub()); |
| 97 | EXPECT_EQ(F7, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 98 | foo *F8 = B1.baz(); |
| 99 | EXPECT_NE(F8, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(CastingTest, cast_or_null) { |
| 103 | const foo *F11 = cast_or_null<foo>(B2); |
| 104 | EXPECT_NE(F11, null_foo); |
| 105 | const foo *F12 = cast_or_null<foo>(B2); |
| 106 | EXPECT_NE(F12, null_foo); |
| 107 | const foo *F13 = cast_or_null<foo>(B4); |
| 108 | EXPECT_NE(F13, null_foo); |
| 109 | const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. |
| 110 | EXPECT_EQ(F14, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 111 | foo *F15 = B1.caz(); |
| 112 | EXPECT_NE(F15, null_foo); |
| 113 | } |
| 114 | |
| 115 | TEST(CastingTest, dyn_cast) { |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 116 | const foo *F1 = dyn_cast<foo>(B2); |
| 117 | EXPECT_NE(F1, null_foo); |
| 118 | const foo *F2 = dyn_cast<foo>(B2); |
| 119 | EXPECT_NE(F2, null_foo); |
| 120 | const foo *F3 = dyn_cast<foo>(B4); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 121 | EXPECT_NE(F3, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 122 | // foo *F4 = dyn_cast<foo>(fub()); // not permittible |
| 123 | // EXPECT_EQ(F4, null_foo); |
| 124 | foo *F5 = B1.daz(); |
| 125 | EXPECT_NE(F5, null_foo); |
| 126 | } |
| 127 | |
| 128 | TEST(CastingTest, dyn_cast_or_null) { |
| 129 | const foo *F1 = dyn_cast_or_null<foo>(B2); |
| 130 | EXPECT_NE(F1, null_foo); |
| 131 | const foo *F2 = dyn_cast_or_null<foo>(B2); |
| 132 | EXPECT_NE(F2, null_foo); |
| 133 | const foo *F3 = dyn_cast_or_null<foo>(B4); |
| 134 | EXPECT_NE(F3, null_foo); |
| 135 | foo *F4 = dyn_cast_or_null<foo>(fub()); |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 136 | EXPECT_EQ(F4, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 137 | foo *F5 = B1.naz(); |
| 138 | EXPECT_NE(F5, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 141 | // These lines are errors... |
| 142 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 143 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 144 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 145 | //foo &F23 = cast_or_null<foo>(B1); |
| 146 | //const foo &F24 = cast_or_null<foo>(B3); |
| 147 | |
| 148 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 149 | bar B; |
| 150 | bar &B1 = B; |
| 151 | const bar *B2 = &B; |
| 152 | } // anonymous namespace |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 153 | |
| 154 | bar *llvm::fub() { return 0; } |