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 | #include "gtest/gtest.h" |
| 14 | #include <cstdlib> |
| 15 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 16 | namespace llvm { |
| 17 | |
| 18 | // set up two example classes |
| 19 | // with conversion facility |
| 20 | // |
| 21 | struct bar { |
| 22 | bar() {} |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 23 | struct foo *baz(); |
| 24 | struct foo *caz(); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 25 | struct foo *daz(); |
| 26 | struct foo *naz(); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 27 | private: |
| 28 | bar(const bar &); |
| 29 | }; |
| 30 | struct foo { |
| 31 | void ext() const; |
| 32 | /* static bool classof(const bar *X) { |
| 33 | cerr << "Classof: " << X << "\n"; |
| 34 | return true; |
| 35 | }*/ |
| 36 | }; |
| 37 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 38 | template <> struct isa_impl<foo, bar> { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 39 | static inline bool doit(const bar &Val) { |
| 40 | dbgs() << "Classof: " << &Val << "\n"; |
| 41 | return true; |
| 42 | } |
| 43 | }; |
| 44 | |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 45 | foo *bar::baz() { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 46 | return cast<foo>(this); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | foo *bar::caz() { |
| 50 | return cast_or_null<foo>(this); |
| 51 | } |
| 52 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 53 | foo *bar::daz() { |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 54 | return dyn_cast<foo>(this); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | foo *bar::naz() { |
| 58 | return dyn_cast_or_null<foo>(this); |
| 59 | } |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | bar *fub(); |
| 63 | } // End llvm namespace |
| 64 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 65 | using namespace llvm; |
| 66 | |
| 67 | namespace { |
| 68 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 69 | const foo *null_foo = NULL; |
| 70 | |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 71 | bar B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 72 | extern bar &B1; |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 73 | bar &B1 = B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 74 | extern const bar *B2; |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 75 | // test various configurations of const |
| 76 | const bar &B3 = B1; |
| 77 | const bar *const B4 = B2; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 78 | |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 79 | TEST(CastingTest, isa) { |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 80 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 81 | EXPECT_TRUE(isa<foo>(B2)); |
| 82 | EXPECT_TRUE(isa<foo>(B3)); |
| 83 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 86 | TEST(CastingTest, cast) { |
| 87 | foo &F1 = cast<foo>(B1); |
| 88 | EXPECT_NE(&F1, null_foo); |
| 89 | const foo *F3 = cast<foo>(B2); |
| 90 | EXPECT_NE(F3, null_foo); |
| 91 | const foo *F4 = cast<foo>(B2); |
| 92 | EXPECT_NE(F4, null_foo); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 93 | const foo &F5 = cast<foo>(B3); |
| 94 | EXPECT_NE(&F5, null_foo); |
| 95 | const foo *F6 = cast<foo>(B4); |
| 96 | EXPECT_NE(F6, null_foo); |
Richard Smith | 79b59a2 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 97 | // Can't pass null pointer to cast<>. |
| 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); |
Richard Smith | 79b59a2 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 124 | // Can't pass null pointer to dyn_cast<>. |
| 125 | // foo *F4 = dyn_cast<foo>(fub()); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 126 | // EXPECT_EQ(F4, null_foo); |
| 127 | foo *F5 = B1.daz(); |
| 128 | EXPECT_NE(F5, null_foo); |
| 129 | } |
| 130 | |
| 131 | TEST(CastingTest, dyn_cast_or_null) { |
| 132 | const foo *F1 = dyn_cast_or_null<foo>(B2); |
| 133 | EXPECT_NE(F1, null_foo); |
| 134 | const foo *F2 = dyn_cast_or_null<foo>(B2); |
| 135 | EXPECT_NE(F2, null_foo); |
| 136 | const foo *F3 = dyn_cast_or_null<foo>(B4); |
| 137 | EXPECT_NE(F3, null_foo); |
| 138 | foo *F4 = dyn_cast_or_null<foo>(fub()); |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 139 | EXPECT_EQ(F4, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 140 | foo *F5 = B1.naz(); |
| 141 | EXPECT_NE(F5, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 144 | // These lines are errors... |
| 145 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 146 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 147 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 148 | //foo &F23 = cast_or_null<foo>(B1); |
| 149 | //const foo &F24 = cast_or_null<foo>(B3); |
| 150 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 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; } |
Sean Silva | 8b8fa7b | 2012-10-11 23:30:40 +0000 | [diff] [blame] | 155 | |
| 156 | namespace { |
| 157 | namespace inferred_upcasting { |
| 158 | // This test case verifies correct behavior of inferred upcasts when the |
| 159 | // types are statically known to be OK to upcast. This is the case when, |
| 160 | // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`. |
| 161 | |
| 162 | // Note: This test will actually fail to compile without inferred |
| 163 | // upcasting. |
| 164 | |
| 165 | class Base { |
| 166 | public: |
| 167 | // No classof. We are testing that the upcast is inferred. |
| 168 | Base() {} |
| 169 | }; |
| 170 | |
| 171 | class Derived : public Base { |
| 172 | public: |
| 173 | Derived() {} |
| 174 | }; |
| 175 | |
| 176 | // Even with no explicit classof() in Base, we should still be able to cast |
| 177 | // Derived to its base class. |
| 178 | TEST(CastingTest, UpcastIsInferred) { |
| 179 | Derived D; |
| 180 | EXPECT_TRUE(isa<Base>(D)); |
| 181 | Base *BP = dyn_cast<Base>(&D); |
| 182 | EXPECT_TRUE(BP != NULL); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // This test verifies that the inferred upcast takes precedence over an |
| 187 | // explicitly written one. This is important because it verifies that the |
| 188 | // dynamic check gets optimized away. |
| 189 | class UseInferredUpcast { |
| 190 | public: |
| 191 | int Dummy; |
| 192 | static bool classof(const UseInferredUpcast *) { |
| 193 | return false; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | TEST(CastingTest, InferredUpcastTakesPrecedence) { |
| 198 | UseInferredUpcast UIU; |
| 199 | // Since the explicit classof() returns false, this will fail if the |
| 200 | // explicit one is used. |
| 201 | EXPECT_TRUE(isa<UseInferredUpcast>(&UIU)); |
| 202 | } |
| 203 | |
| 204 | } // end namespace inferred_upcasting |
| 205 | } // end anonymous namespace |