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 | |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame^] | 72 | bar B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 73 | extern bar &B1; |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame^] | 74 | bar &B1 = B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 75 | extern const bar *B2; |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 76 | // test various configurations of const |
| 77 | const bar &B3 = B1; |
| 78 | const bar *const B4 = B2; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 79 | |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 80 | TEST(CastingTest, isa) { |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 81 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 82 | EXPECT_TRUE(isa<foo>(B2)); |
| 83 | EXPECT_TRUE(isa<foo>(B3)); |
| 84 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 87 | TEST(CastingTest, cast) { |
| 88 | foo &F1 = cast<foo>(B1); |
| 89 | EXPECT_NE(&F1, null_foo); |
| 90 | const foo *F3 = cast<foo>(B2); |
| 91 | EXPECT_NE(F3, null_foo); |
| 92 | const foo *F4 = cast<foo>(B2); |
| 93 | EXPECT_NE(F4, null_foo); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 94 | const foo &F5 = cast<foo>(B3); |
| 95 | EXPECT_NE(&F5, null_foo); |
| 96 | const foo *F6 = cast<foo>(B4); |
| 97 | EXPECT_NE(F6, null_foo); |
| 98 | foo *F7 = cast<foo>(fub()); |
| 99 | EXPECT_EQ(F7, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 100 | foo *F8 = B1.baz(); |
| 101 | EXPECT_NE(F8, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(CastingTest, cast_or_null) { |
| 105 | const foo *F11 = cast_or_null<foo>(B2); |
| 106 | EXPECT_NE(F11, null_foo); |
| 107 | const foo *F12 = cast_or_null<foo>(B2); |
| 108 | EXPECT_NE(F12, null_foo); |
| 109 | const foo *F13 = cast_or_null<foo>(B4); |
| 110 | EXPECT_NE(F13, null_foo); |
| 111 | const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. |
| 112 | EXPECT_EQ(F14, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 113 | foo *F15 = B1.caz(); |
| 114 | EXPECT_NE(F15, null_foo); |
| 115 | } |
| 116 | |
| 117 | TEST(CastingTest, dyn_cast) { |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 118 | const foo *F1 = dyn_cast<foo>(B2); |
| 119 | EXPECT_NE(F1, null_foo); |
| 120 | const foo *F2 = dyn_cast<foo>(B2); |
| 121 | EXPECT_NE(F2, null_foo); |
| 122 | const foo *F3 = dyn_cast<foo>(B4); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 123 | EXPECT_NE(F3, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 124 | // foo *F4 = dyn_cast<foo>(fub()); // not permittible |
| 125 | // EXPECT_EQ(F4, null_foo); |
| 126 | foo *F5 = B1.daz(); |
| 127 | EXPECT_NE(F5, null_foo); |
| 128 | } |
| 129 | |
| 130 | TEST(CastingTest, dyn_cast_or_null) { |
| 131 | const foo *F1 = dyn_cast_or_null<foo>(B2); |
| 132 | EXPECT_NE(F1, null_foo); |
| 133 | const foo *F2 = dyn_cast_or_null<foo>(B2); |
| 134 | EXPECT_NE(F2, null_foo); |
| 135 | const foo *F3 = dyn_cast_or_null<foo>(B4); |
| 136 | EXPECT_NE(F3, null_foo); |
| 137 | foo *F4 = dyn_cast_or_null<foo>(fub()); |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 138 | EXPECT_EQ(F4, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 139 | foo *F5 = B1.naz(); |
| 140 | EXPECT_NE(F5, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 143 | // These lines are errors... |
| 144 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 145 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 146 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 147 | //foo &F23 = cast_or_null<foo>(B1); |
| 148 | //const foo &F24 = cast_or_null<foo>(B3); |
| 149 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 150 | const bar *B2 = &B; |
| 151 | } // anonymous namespace |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 152 | |
| 153 | bar *llvm::fub() { return 0; } |