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/IR/User.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.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;
cast(...)21 template <typename T> IllegalCast *cast(...) { return nullptr; }
22
23 // set up two example classes
24 // with conversion facility
25 //
26 struct bar {
barllvm::bar27 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 struct base {
~basellvm::base44 virtual ~base() {}
45 };
46
47 struct derived : public base {
classofllvm::derived48 static bool classof(const base *B) { return true; }
49 };
50
51 template <> struct isa_impl<foo, bar> {
doitllvm::isa_impl52 static inline bool doit(const bar &Val) {
53 dbgs() << "Classof: " << &Val << "\n";
54 return true;
55 }
56 };
57
58 template <typename T> struct isa_impl<foo, T> {
doitllvm::isa_impl59 static inline bool doit(const T &Val) { return false; }
60 };
61
baz()62 foo *bar::baz() {
63 return cast<foo>(this);
64 }
65
caz()66 foo *bar::caz() {
67 return cast_or_null<foo>(this);
68 }
69
daz()70 foo *bar::daz() {
71 return dyn_cast<foo>(this);
72 }
73
naz()74 foo *bar::naz() {
75 return dyn_cast_or_null<foo>(this);
76 }
77
78
79 bar *fub();
80
81 template <> struct simplify_type<foo> {
82 typedef int SimpleType;
getSimplifiedValuellvm::simplify_type83 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
84 };
85
86 } // End llvm namespace
87
88 using namespace llvm;
89
90
91 // Test the peculiar behavior of Use in simplify_type.
92 static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
93 "Use doesn't simplify correctly!");
94 static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
95 "Use doesn't simplify correctly!");
96
97 // Test that a regular class behaves as expected.
98 static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
99 "Unexpected simplify_type result!");
100 static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
101 "Unexpected simplify_type result!");
102
103 namespace {
104
105 const foo *null_foo = nullptr;
106
107 bar B;
108 extern bar &B1;
109 bar &B1 = B;
110 extern const bar *B2;
111 // test various configurations of const
112 const bar &B3 = B1;
113 const bar *const B4 = B2;
114
TEST(CastingTest,isa)115 TEST(CastingTest, isa) {
116 EXPECT_TRUE(isa<foo>(B1));
117 EXPECT_TRUE(isa<foo>(B2));
118 EXPECT_TRUE(isa<foo>(B3));
119 EXPECT_TRUE(isa<foo>(B4));
120 }
121
TEST(CastingTest,cast)122 TEST(CastingTest, cast) {
123 foo &F1 = cast<foo>(B1);
124 EXPECT_NE(&F1, null_foo);
125 const foo *F3 = cast<foo>(B2);
126 EXPECT_NE(F3, null_foo);
127 const foo *F4 = cast<foo>(B2);
128 EXPECT_NE(F4, null_foo);
129 const foo &F5 = cast<foo>(B3);
130 EXPECT_NE(&F5, null_foo);
131 const foo *F6 = cast<foo>(B4);
132 EXPECT_NE(F6, null_foo);
133 // Can't pass null pointer to cast<>.
134 // foo *F7 = cast<foo>(fub());
135 // EXPECT_EQ(F7, null_foo);
136 foo *F8 = B1.baz();
137 EXPECT_NE(F8, null_foo);
138
139 std::unique_ptr<const bar> BP(B2);
140 auto FP = cast<foo>(std::move(BP));
141 static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value,
142 "Incorrect deduced return type!");
143 EXPECT_NE(FP.get(), null_foo);
144 FP.release();
145 }
146
TEST(CastingTest,cast_or_null)147 TEST(CastingTest, cast_or_null) {
148 const foo *F11 = cast_or_null<foo>(B2);
149 EXPECT_NE(F11, null_foo);
150 const foo *F12 = cast_or_null<foo>(B2);
151 EXPECT_NE(F12, null_foo);
152 const foo *F13 = cast_or_null<foo>(B4);
153 EXPECT_NE(F13, null_foo);
154 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
155 EXPECT_EQ(F14, null_foo);
156 foo *F15 = B1.caz();
157 EXPECT_NE(F15, null_foo);
158
159 std::unique_ptr<const bar> BP(fub());
160 auto FP = cast_or_null<foo>(std::move(BP));
161 EXPECT_EQ(FP.get(), null_foo);
162 }
163
TEST(CastingTest,dyn_cast)164 TEST(CastingTest, dyn_cast) {
165 const foo *F1 = dyn_cast<foo>(B2);
166 EXPECT_NE(F1, null_foo);
167 const foo *F2 = dyn_cast<foo>(B2);
168 EXPECT_NE(F2, null_foo);
169 const foo *F3 = dyn_cast<foo>(B4);
170 EXPECT_NE(F3, null_foo);
171 // Can't pass null pointer to dyn_cast<>.
172 // foo *F4 = dyn_cast<foo>(fub());
173 // EXPECT_EQ(F4, null_foo);
174 foo *F5 = B1.daz();
175 EXPECT_NE(F5, null_foo);
176 }
177
TEST(CastingTest,dyn_cast_or_null)178 TEST(CastingTest, dyn_cast_or_null) {
179 const foo *F1 = dyn_cast_or_null<foo>(B2);
180 EXPECT_NE(F1, null_foo);
181 const foo *F2 = dyn_cast_or_null<foo>(B2);
182 EXPECT_NE(F2, null_foo);
183 const foo *F3 = dyn_cast_or_null<foo>(B4);
184 EXPECT_NE(F3, null_foo);
185 foo *F4 = dyn_cast_or_null<foo>(fub());
186 EXPECT_EQ(F4, null_foo);
187 foo *F5 = B1.naz();
188 EXPECT_NE(F5, null_foo);
189 }
190
newd()191 std::unique_ptr<derived> newd() { return llvm::make_unique<derived>(); }
newb()192 std::unique_ptr<base> newb() { return llvm::make_unique<derived>(); }
193
TEST(CastingTest,unique_dyn_cast)194 TEST(CastingTest, unique_dyn_cast) {
195 derived *OrigD = nullptr;
196 auto D = llvm::make_unique<derived>();
197 OrigD = D.get();
198
199 // Converting from D to itself is valid, it should return a new unique_ptr
200 // and the old one should become nullptr.
201 auto NewD = unique_dyn_cast<derived>(D);
202 ASSERT_EQ(OrigD, NewD.get());
203 ASSERT_EQ(nullptr, D);
204
205 // Converting from D to B is valid, B should have a value and D should be
206 // nullptr.
207 auto B = unique_dyn_cast<base>(NewD);
208 ASSERT_EQ(OrigD, B.get());
209 ASSERT_EQ(nullptr, NewD);
210
211 // Converting from B to itself is valid, it should return a new unique_ptr
212 // and the old one should become nullptr.
213 auto NewB = unique_dyn_cast<base>(B);
214 ASSERT_EQ(OrigD, NewB.get());
215 ASSERT_EQ(nullptr, B);
216
217 // Converting from B to D is valid, D should have a value and B should be
218 // nullptr;
219 D = unique_dyn_cast<derived>(NewB);
220 ASSERT_EQ(OrigD, D.get());
221 ASSERT_EQ(nullptr, NewB);
222
223 // Converting between unrelated types should fail. The original value should
224 // remain unchanged and it should return nullptr.
225 auto F = unique_dyn_cast<foo>(D);
226 ASSERT_EQ(nullptr, F);
227 ASSERT_EQ(OrigD, D.get());
228
229 // All of the above should also hold for temporaries.
230 auto D2 = unique_dyn_cast<derived>(newd());
231 EXPECT_NE(nullptr, D2);
232
233 auto B2 = unique_dyn_cast<derived>(newb());
234 EXPECT_NE(nullptr, B2);
235
236 auto B3 = unique_dyn_cast<base>(newb());
237 EXPECT_NE(nullptr, B3);
238
239 auto F2 = unique_dyn_cast<foo>(newb());
240 EXPECT_EQ(nullptr, F2);
241 }
242
243 // These lines are errors...
244 //foo *F20 = cast<foo>(B2); // Yields const foo*
245 //foo &F21 = cast<foo>(B3); // Yields const foo&
246 //foo *F22 = cast<foo>(B4); // Yields const foo*
247 //foo &F23 = cast_or_null<foo>(B1);
248 //const foo &F24 = cast_or_null<foo>(B3);
249
250 const bar *B2 = &B;
251 } // anonymous namespace
252
fub()253 bar *llvm::fub() { return nullptr; }
254
255 namespace {
256 namespace inferred_upcasting {
257 // This test case verifies correct behavior of inferred upcasts when the
258 // types are statically known to be OK to upcast. This is the case when,
259 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
260
261 // Note: This test will actually fail to compile without inferred
262 // upcasting.
263
264 class Base {
265 public:
266 // No classof. We are testing that the upcast is inferred.
Base()267 Base() {}
268 };
269
270 class Derived : public Base {
271 public:
Derived()272 Derived() {}
273 };
274
275 // Even with no explicit classof() in Base, we should still be able to cast
276 // Derived to its base class.
TEST(CastingTest,UpcastIsInferred)277 TEST(CastingTest, UpcastIsInferred) {
278 Derived D;
279 EXPECT_TRUE(isa<Base>(D));
280 Base *BP = dyn_cast<Base>(&D);
281 EXPECT_TRUE(BP != nullptr);
282 }
283
284
285 // This test verifies that the inferred upcast takes precedence over an
286 // explicitly written one. This is important because it verifies that the
287 // dynamic check gets optimized away.
288 class UseInferredUpcast {
289 public:
290 int Dummy;
classof(const UseInferredUpcast *)291 static bool classof(const UseInferredUpcast *) {
292 return false;
293 }
294 };
295
TEST(CastingTest,InferredUpcastTakesPrecedence)296 TEST(CastingTest, InferredUpcastTakesPrecedence) {
297 UseInferredUpcast UIU;
298 // Since the explicit classof() returns false, this will fail if the
299 // explicit one is used.
300 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
301 }
302
303 } // end namespace inferred_upcasting
304 } // end anonymous namespace
305 // Test that we reject casts of temporaries (and so the illegal cast gets used).
306 namespace TemporaryCast {
307 struct pod {};
testIllegalCast()308 IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
309 }
310
311 namespace {
312 namespace pointer_wrappers {
313
314 struct Base {
315 bool IsDerived;
Base__anonfc5f95040311::pointer_wrappers::Base316 Base(bool IsDerived = false) : IsDerived(IsDerived) {}
317 };
318
319 struct Derived : Base {
Derived__anonfc5f95040311::pointer_wrappers::Derived320 Derived() : Base(true) {}
classof__anonfc5f95040311::pointer_wrappers::Derived321 static bool classof(const Base *B) { return B->IsDerived; }
322 };
323
324 class PTy {
325 Base *B;
326 public:
PTy(Base * B)327 PTy(Base *B) : B(B) {}
operator bool() const328 explicit operator bool() const { return get(); }
get() const329 Base *get() const { return B; }
330 };
331
332 } // end namespace pointer_wrappers
333 } // end namespace
334
335 namespace llvm {
336
337 template <> struct simplify_type<pointer_wrappers::PTy> {
338 typedef pointer_wrappers::Base *SimpleType;
getSimplifiedValuellvm::simplify_type339 static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
340 return P.get();
341 }
342 };
343 template <> struct simplify_type<const pointer_wrappers::PTy> {
344 typedef pointer_wrappers::Base *SimpleType;
getSimplifiedValuellvm::simplify_type345 static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
346 return P.get();
347 }
348 };
349
350 } // end namespace llvm
351
352 namespace {
353 namespace pointer_wrappers {
354
355 // Some objects.
356 pointer_wrappers::Base B;
357 pointer_wrappers::Derived D;
358
359 // Mutable "smart" pointers.
360 pointer_wrappers::PTy MN(nullptr);
361 pointer_wrappers::PTy MB(&B);
362 pointer_wrappers::PTy MD(&D);
363
364 // Const "smart" pointers.
365 const pointer_wrappers::PTy CN(nullptr);
366 const pointer_wrappers::PTy CB(&B);
367 const pointer_wrappers::PTy CD(&D);
368
TEST(CastingTest,smart_isa)369 TEST(CastingTest, smart_isa) {
370 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB));
371 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB));
372 EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD));
373 EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD));
374 }
375
TEST(CastingTest,smart_cast)376 TEST(CastingTest, smart_cast) {
377 EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D);
378 EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D);
379 }
380
TEST(CastingTest,smart_cast_or_null)381 TEST(CastingTest, smart_cast_or_null) {
382 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
383 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
384 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MD) == &D);
385 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CD) == &D);
386 }
387
TEST(CastingTest,smart_dyn_cast)388 TEST(CastingTest, smart_dyn_cast) {
389 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MB) == nullptr);
390 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CB) == nullptr);
391 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MD) == &D);
392 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CD) == &D);
393 }
394
TEST(CastingTest,smart_dyn_cast_or_null)395 TEST(CastingTest, smart_dyn_cast_or_null) {
396 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
397 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
398 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MB) == nullptr);
399 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CB) == nullptr);
400 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MD) == &D);
401 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CD) == &D);
402 }
403
404 } // end namespace pointer_wrappers
405 } // end namespace
406