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" |
Rafael Espindola | 60f18ad | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 13 | #include "llvm/IR/User.h" |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | #include <cstdlib> |
| 16 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 17 | namespace llvm { |
Rafael Espindola | 60f18ad | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 18 | // Used to test illegal cast. If a cast doesn't match any of the "real" ones, |
| 19 | // it will match this one. |
| 20 | struct IllegalCast; |
| 21 | template <typename T> IllegalCast *cast(...) { return 0; } |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 22 | |
| 23 | // set up two example classes |
| 24 | // with conversion facility |
| 25 | // |
| 26 | struct bar { |
| 27 | bar() {} |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 28 | struct foo *baz(); |
| 29 | struct foo *caz(); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 30 | struct foo *daz(); |
| 31 | struct foo *naz(); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 32 | private: |
| 33 | bar(const bar &); |
| 34 | }; |
| 35 | struct foo { |
| 36 | void ext() const; |
| 37 | /* static bool classof(const bar *X) { |
| 38 | cerr << "Classof: " << X << "\n"; |
| 39 | return true; |
| 40 | }*/ |
| 41 | }; |
| 42 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 43 | template <> struct isa_impl<foo, bar> { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 44 | static inline bool doit(const bar &Val) { |
| 45 | dbgs() << "Classof: " << &Val << "\n"; |
| 46 | return true; |
| 47 | } |
| 48 | }; |
| 49 | |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 50 | foo *bar::baz() { |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 51 | return cast<foo>(this); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | foo *bar::caz() { |
| 55 | return cast_or_null<foo>(this); |
| 56 | } |
| 57 | |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 58 | foo *bar::daz() { |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 59 | return dyn_cast<foo>(this); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | foo *bar::naz() { |
| 63 | return dyn_cast_or_null<foo>(this); |
| 64 | } |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | bar *fub(); |
Rafael Espindola | 60f18ad | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 68 | |
| 69 | template <> struct simplify_type<foo> { |
| 70 | typedef int SimpleType; |
| 71 | static SimpleType getSimplifiedValue(foo &Val) { return 0; } |
| 72 | }; |
| 73 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 74 | } // End llvm namespace |
| 75 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 76 | using namespace llvm; |
| 77 | |
Rafael Espindola | 60f18ad | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 78 | |
| 79 | // Test the peculiar behavior of Use in simplify_type. |
| 80 | int Check1[is_same<simplify_type<Use>::SimpleType, Value *>::value ? 1 : -1]; |
| 81 | int Check2[is_same<simplify_type<Use *>::SimpleType, Value *>::value ? 1 : -1]; |
| 82 | |
| 83 | // Test that a regular class behaves as expected. |
| 84 | int Check3[is_same<simplify_type<foo>::SimpleType, int>::value ? 1 : -1]; |
| 85 | int Check4[is_same<simplify_type<foo *>::SimpleType, foo *>::value ? 1 : -1]; |
| 86 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 87 | namespace { |
| 88 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 89 | const foo *null_foo = NULL; |
| 90 | |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 91 | bar B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 92 | extern bar &B1; |
NAKAMURA Takumi | 2a53577 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 93 | bar &B1 = B; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 94 | extern const bar *B2; |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 95 | // test various configurations of const |
| 96 | const bar &B3 = B1; |
| 97 | const bar *const B4 = B2; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 98 | |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 99 | TEST(CastingTest, isa) { |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 100 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 101 | EXPECT_TRUE(isa<foo>(B2)); |
| 102 | EXPECT_TRUE(isa<foo>(B3)); |
| 103 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 106 | TEST(CastingTest, cast) { |
| 107 | foo &F1 = cast<foo>(B1); |
| 108 | EXPECT_NE(&F1, null_foo); |
| 109 | const foo *F3 = cast<foo>(B2); |
| 110 | EXPECT_NE(F3, null_foo); |
| 111 | const foo *F4 = cast<foo>(B2); |
| 112 | EXPECT_NE(F4, null_foo); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 113 | const foo &F5 = cast<foo>(B3); |
| 114 | EXPECT_NE(&F5, null_foo); |
| 115 | const foo *F6 = cast<foo>(B4); |
| 116 | EXPECT_NE(F6, null_foo); |
Richard Smith | 79b59a2 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 117 | // Can't pass null pointer to cast<>. |
| 118 | // foo *F7 = cast<foo>(fub()); |
| 119 | // EXPECT_EQ(F7, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 120 | foo *F8 = B1.baz(); |
| 121 | EXPECT_NE(F8, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST(CastingTest, cast_or_null) { |
| 125 | const foo *F11 = cast_or_null<foo>(B2); |
| 126 | EXPECT_NE(F11, null_foo); |
| 127 | const foo *F12 = cast_or_null<foo>(B2); |
| 128 | EXPECT_NE(F12, null_foo); |
| 129 | const foo *F13 = cast_or_null<foo>(B4); |
| 130 | EXPECT_NE(F13, null_foo); |
| 131 | const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. |
| 132 | EXPECT_EQ(F14, null_foo); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 133 | foo *F15 = B1.caz(); |
| 134 | EXPECT_NE(F15, null_foo); |
| 135 | } |
| 136 | |
| 137 | TEST(CastingTest, dyn_cast) { |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 138 | const foo *F1 = dyn_cast<foo>(B2); |
| 139 | EXPECT_NE(F1, null_foo); |
| 140 | const foo *F2 = dyn_cast<foo>(B2); |
| 141 | EXPECT_NE(F2, null_foo); |
| 142 | const foo *F3 = dyn_cast<foo>(B4); |
Gabor Greif | d159467 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 143 | EXPECT_NE(F3, null_foo); |
Richard Smith | 79b59a2 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 144 | // Can't pass null pointer to dyn_cast<>. |
| 145 | // foo *F4 = dyn_cast<foo>(fub()); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 146 | // EXPECT_EQ(F4, null_foo); |
| 147 | foo *F5 = B1.daz(); |
| 148 | EXPECT_NE(F5, null_foo); |
| 149 | } |
| 150 | |
| 151 | TEST(CastingTest, dyn_cast_or_null) { |
| 152 | const foo *F1 = dyn_cast_or_null<foo>(B2); |
| 153 | EXPECT_NE(F1, null_foo); |
| 154 | const foo *F2 = dyn_cast_or_null<foo>(B2); |
| 155 | EXPECT_NE(F2, null_foo); |
| 156 | const foo *F3 = dyn_cast_or_null<foo>(B4); |
| 157 | EXPECT_NE(F3, null_foo); |
| 158 | foo *F4 = dyn_cast_or_null<foo>(fub()); |
Gabor Greif | 46a3501 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 159 | EXPECT_EQ(F4, null_foo); |
Gabor Greif | f06eb37 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 160 | foo *F5 = B1.naz(); |
| 161 | EXPECT_NE(F5, null_foo); |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 164 | // These lines are errors... |
| 165 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 166 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 167 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 168 | //foo &F23 = cast_or_null<foo>(B1); |
| 169 | //const foo &F24 = cast_or_null<foo>(B3); |
| 170 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 171 | const bar *B2 = &B; |
| 172 | } // anonymous namespace |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 173 | |
| 174 | bar *llvm::fub() { return 0; } |
Sean Silva | 8b8fa7b | 2012-10-11 23:30:40 +0000 | [diff] [blame] | 175 | |
| 176 | namespace { |
| 177 | namespace inferred_upcasting { |
| 178 | // This test case verifies correct behavior of inferred upcasts when the |
| 179 | // types are statically known to be OK to upcast. This is the case when, |
| 180 | // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`. |
| 181 | |
| 182 | // Note: This test will actually fail to compile without inferred |
| 183 | // upcasting. |
| 184 | |
| 185 | class Base { |
| 186 | public: |
| 187 | // No classof. We are testing that the upcast is inferred. |
| 188 | Base() {} |
| 189 | }; |
| 190 | |
| 191 | class Derived : public Base { |
| 192 | public: |
| 193 | Derived() {} |
| 194 | }; |
| 195 | |
| 196 | // Even with no explicit classof() in Base, we should still be able to cast |
| 197 | // Derived to its base class. |
| 198 | TEST(CastingTest, UpcastIsInferred) { |
| 199 | Derived D; |
| 200 | EXPECT_TRUE(isa<Base>(D)); |
| 201 | Base *BP = dyn_cast<Base>(&D); |
| 202 | EXPECT_TRUE(BP != NULL); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | // This test verifies that the inferred upcast takes precedence over an |
| 207 | // explicitly written one. This is important because it verifies that the |
| 208 | // dynamic check gets optimized away. |
| 209 | class UseInferredUpcast { |
| 210 | public: |
| 211 | int Dummy; |
| 212 | static bool classof(const UseInferredUpcast *) { |
| 213 | return false; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | TEST(CastingTest, InferredUpcastTakesPrecedence) { |
| 218 | UseInferredUpcast UIU; |
| 219 | // Since the explicit classof() returns false, this will fail if the |
| 220 | // explicit one is used. |
| 221 | EXPECT_TRUE(isa<UseInferredUpcast>(&UIU)); |
| 222 | } |
| 223 | |
| 224 | } // end namespace inferred_upcasting |
| 225 | } // end anonymous namespace |
Rafael Espindola | 60f18ad | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 226 | // Test that we reject casts of temporaries (and so the illegal cast gets used). |
| 227 | namespace TemporaryCast { |
| 228 | struct pod {}; |
| 229 | IllegalCast *testIllegalCast() { return cast<foo>(pod()); } |
| 230 | } |