1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===// 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 10 #include "llvm/Support/Casting.h" 11 #include "llvm/Support/Debug.h" 12 #include "llvm/Support/raw_ostream.h" 13 #include "llvm/IR/User.h" 14 #include "gtest/gtest.h" 15 #include <cstdlib> 16 17 namespace llvm { 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; } 22 23 // set up two example classes 24 // with conversion facility 25 // 26 struct bar { 27 bar() {} 28 struct foo *baz(); 29 struct foo *caz(); 30 struct foo *daz(); 31 struct foo *naz(); 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 43 template <> struct isa_impl<foo, bar> { 44 static inline bool doit(const bar &Val) { 45 dbgs() << "Classof: " << &Val << "\n"; 46 return true; 47 } 48 }; 49 50 foo *bar::baz() { 51 return cast<foo>(this); 52 } 53 54 foo *bar::caz() { 55 return cast_or_null<foo>(this); 56 } 57 58 foo *bar::daz() { 59 return dyn_cast<foo>(this); 60 } 61 62 foo *bar::naz() { 63 return dyn_cast_or_null<foo>(this); 64 } 65 66 67 bar *fub(); 68 69 template <> struct simplify_type<foo> { 70 typedef int SimpleType; 71 static SimpleType getSimplifiedValue(foo &Val) { return 0; } 72 }; 73 74 } // End llvm namespace 75 76 using namespace llvm; 77 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 87 namespace { 88 89 const foo *null_foo = NULL; 90 91 bar B; 92 extern bar &B1; 93 bar &B1 = B; 94 extern const bar *B2; 95 // test various configurations of const 96 const bar &B3 = B1; 97 const bar *const B4 = B2; 98 99 TEST(CastingTest, isa) { 100 EXPECT_TRUE(isa<foo>(B1)); 101 EXPECT_TRUE(isa<foo>(B2)); 102 EXPECT_TRUE(isa<foo>(B3)); 103 EXPECT_TRUE(isa<foo>(B4)); 104 } 105 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); 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); 117 // Can't pass null pointer to cast<>. 118 // foo *F7 = cast<foo>(fub()); 119 // EXPECT_EQ(F7, null_foo); 120 foo *F8 = B1.baz(); 121 EXPECT_NE(F8, null_foo); 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); 133 foo *F15 = B1.caz(); 134 EXPECT_NE(F15, null_foo); 135 } 136 137 TEST(CastingTest, dyn_cast) { 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); 143 EXPECT_NE(F3, null_foo); 144 // Can't pass null pointer to dyn_cast<>. 145 // foo *F4 = dyn_cast<foo>(fub()); 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()); 159 EXPECT_EQ(F4, null_foo); 160 foo *F5 = B1.naz(); 161 EXPECT_NE(F5, null_foo); 162 } 163 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 171 const bar *B2 = &B; 172 } // anonymous namespace 173 174 bar *llvm::fub() { return 0; } 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 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 } 231