blob: e6c35fc21eb7dffc732ed42d9fe511483cd9a97b [file] [log] [blame]
Gabor Greifdd8ba3c2010-07-20 19:35:55 +00001//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
Gabor Greif704524a2010-07-20 16:32:20 +00002//
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 Greif704524a2010-07-20 16:32:20 +000010#include "llvm/Support/Casting.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000011#include "llvm/IR/User.h"
Gabor Greifdd8ba3c2010-07-20 19:35:55 +000012#include "llvm/Support/Debug.h"
13#include "llvm/Support/raw_ostream.h"
Gabor Greif704524a2010-07-20 16:32:20 +000014#include "gtest/gtest.h"
15#include <cstdlib>
16
Gabor Greifc4dc3932010-07-20 17:06:28 +000017namespace llvm {
Rafael Espindola9ed17612013-07-18 02:42:40 +000018// Used to test illegal cast. If a cast doesn't match any of the "real" ones,
19// it will match this one.
20struct IllegalCast;
Craig Topper66f09ad2014-06-08 22:29:17 +000021template <typename T> IllegalCast *cast(...) { return nullptr; }
Gabor Greifc4dc3932010-07-20 17:06:28 +000022
23// set up two example classes
24// with conversion facility
25//
26struct bar {
27 bar() {}
Gabor Greifb9e69772010-07-22 15:24:48 +000028 struct foo *baz();
29 struct foo *caz();
Gabor Greiffd23a972010-07-22 15:37:20 +000030 struct foo *daz();
31 struct foo *naz();
Gabor Greifc4dc3932010-07-20 17:06:28 +000032private:
33 bar(const bar &);
34};
35struct foo {
36 void ext() const;
37 /* static bool classof(const bar *X) {
38 cerr << "Classof: " << X << "\n";
39 return true;
40 }*/
41};
42
Gabor Greiffd23a972010-07-22 15:37:20 +000043template <> struct isa_impl<foo, bar> {
Gabor Greifc4dc3932010-07-20 17:06:28 +000044 static inline bool doit(const bar &Val) {
45 dbgs() << "Classof: " << &Val << "\n";
46 return true;
47 }
48};
49
Gabor Greifb9e69772010-07-22 15:24:48 +000050foo *bar::baz() {
Gabor Greifc4dc3932010-07-20 17:06:28 +000051 return cast<foo>(this);
Gabor Greifb9e69772010-07-22 15:24:48 +000052}
53
54foo *bar::caz() {
55 return cast_or_null<foo>(this);
56}
57
Gabor Greiffd23a972010-07-22 15:37:20 +000058foo *bar::daz() {
Gabor Greifb9e69772010-07-22 15:24:48 +000059 return dyn_cast<foo>(this);
Gabor Greiffd23a972010-07-22 15:37:20 +000060}
61
62foo *bar::naz() {
63 return dyn_cast_or_null<foo>(this);
64}
Gabor Greifc4dc3932010-07-20 17:06:28 +000065
66
67bar *fub();
Rafael Espindola9ed17612013-07-18 02:42:40 +000068
69template <> struct simplify_type<foo> {
70 typedef int SimpleType;
71 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
72};
73
Gabor Greifc4dc3932010-07-20 17:06:28 +000074} // End llvm namespace
75
Gabor Greif704524a2010-07-20 16:32:20 +000076using namespace llvm;
77
Rafael Espindola9ed17612013-07-18 02:42:40 +000078
79// Test the peculiar behavior of Use in simplify_type.
Benjamin Kramerf04ddd02014-03-07 14:42:25 +000080static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
81 "Use doesn't simplify correctly!");
82static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
83 "Use doesn't simplify correctly!");
Rafael Espindola9ed17612013-07-18 02:42:40 +000084
85// Test that a regular class behaves as expected.
Benjamin Kramerf04ddd02014-03-07 14:42:25 +000086static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
87 "Unexpected simplify_type result!");
88static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
89 "Unexpected simplify_type result!");
Rafael Espindola9ed17612013-07-18 02:42:40 +000090
Gabor Greif704524a2010-07-20 16:32:20 +000091namespace {
92
Craig Topper66f09ad2014-06-08 22:29:17 +000093const foo *null_foo = nullptr;
Gabor Greif1804d302010-07-20 16:51:18 +000094
NAKAMURA Takumi31194402012-01-22 12:14:35 +000095bar B;
Gabor Greif704524a2010-07-20 16:32:20 +000096extern bar &B1;
NAKAMURA Takumi31194402012-01-22 12:14:35 +000097bar &B1 = B;
Gabor Greif704524a2010-07-20 16:32:20 +000098extern const bar *B2;
Gabor Greif1804d302010-07-20 16:51:18 +000099// test various configurations of const
100const bar &B3 = B1;
101const bar *const B4 = B2;
Gabor Greif704524a2010-07-20 16:32:20 +0000102
Gabor Greif3eae1b32010-07-20 16:38:12 +0000103TEST(CastingTest, isa) {
Gabor Greif704524a2010-07-20 16:32:20 +0000104 EXPECT_TRUE(isa<foo>(B1));
Gabor Greif3eae1b32010-07-20 16:38:12 +0000105 EXPECT_TRUE(isa<foo>(B2));
106 EXPECT_TRUE(isa<foo>(B3));
107 EXPECT_TRUE(isa<foo>(B4));
Gabor Greif704524a2010-07-20 16:32:20 +0000108}
109
Gabor Greif1804d302010-07-20 16:51:18 +0000110TEST(CastingTest, cast) {
111 foo &F1 = cast<foo>(B1);
112 EXPECT_NE(&F1, null_foo);
113 const foo *F3 = cast<foo>(B2);
114 EXPECT_NE(F3, null_foo);
115 const foo *F4 = cast<foo>(B2);
116 EXPECT_NE(F4, null_foo);
Gabor Greifc4dc3932010-07-20 17:06:28 +0000117 const foo &F5 = cast<foo>(B3);
118 EXPECT_NE(&F5, null_foo);
119 const foo *F6 = cast<foo>(B4);
120 EXPECT_NE(F6, null_foo);
Richard Smithc4379c32012-08-21 20:39:25 +0000121 // Can't pass null pointer to cast<>.
122 // foo *F7 = cast<foo>(fub());
123 // EXPECT_EQ(F7, null_foo);
Gabor Greifb9e69772010-07-22 15:24:48 +0000124 foo *F8 = B1.baz();
125 EXPECT_NE(F8, null_foo);
Gabor Greif1804d302010-07-20 16:51:18 +0000126}
127
128TEST(CastingTest, cast_or_null) {
129 const foo *F11 = cast_or_null<foo>(B2);
130 EXPECT_NE(F11, null_foo);
131 const foo *F12 = cast_or_null<foo>(B2);
132 EXPECT_NE(F12, null_foo);
133 const foo *F13 = cast_or_null<foo>(B4);
134 EXPECT_NE(F13, null_foo);
135 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
136 EXPECT_EQ(F14, null_foo);
Gabor Greifb9e69772010-07-22 15:24:48 +0000137 foo *F15 = B1.caz();
138 EXPECT_NE(F15, null_foo);
139}
140
141TEST(CastingTest, dyn_cast) {
Gabor Greif78595812010-07-22 15:28:30 +0000142 const foo *F1 = dyn_cast<foo>(B2);
143 EXPECT_NE(F1, null_foo);
144 const foo *F2 = dyn_cast<foo>(B2);
145 EXPECT_NE(F2, null_foo);
146 const foo *F3 = dyn_cast<foo>(B4);
Gabor Greifb9e69772010-07-22 15:24:48 +0000147 EXPECT_NE(F3, null_foo);
Richard Smithc4379c32012-08-21 20:39:25 +0000148 // Can't pass null pointer to dyn_cast<>.
149 // foo *F4 = dyn_cast<foo>(fub());
Gabor Greiffd23a972010-07-22 15:37:20 +0000150 // EXPECT_EQ(F4, null_foo);
151 foo *F5 = B1.daz();
152 EXPECT_NE(F5, null_foo);
153}
154
155TEST(CastingTest, dyn_cast_or_null) {
156 const foo *F1 = dyn_cast_or_null<foo>(B2);
157 EXPECT_NE(F1, null_foo);
158 const foo *F2 = dyn_cast_or_null<foo>(B2);
159 EXPECT_NE(F2, null_foo);
160 const foo *F3 = dyn_cast_or_null<foo>(B4);
161 EXPECT_NE(F3, null_foo);
162 foo *F4 = dyn_cast_or_null<foo>(fub());
Gabor Greif78595812010-07-22 15:28:30 +0000163 EXPECT_EQ(F4, null_foo);
Gabor Greiffd23a972010-07-22 15:37:20 +0000164 foo *F5 = B1.naz();
165 EXPECT_NE(F5, null_foo);
Gabor Greif1804d302010-07-20 16:51:18 +0000166}
167
Gabor Greifc4dc3932010-07-20 17:06:28 +0000168// These lines are errors...
169//foo *F20 = cast<foo>(B2); // Yields const foo*
170//foo &F21 = cast<foo>(B3); // Yields const foo&
171//foo *F22 = cast<foo>(B4); // Yields const foo*
172//foo &F23 = cast_or_null<foo>(B1);
173//const foo &F24 = cast_or_null<foo>(B3);
174
Gabor Greif704524a2010-07-20 16:32:20 +0000175const bar *B2 = &B;
176} // anonymous namespace
Gabor Greifc4dc3932010-07-20 17:06:28 +0000177
Craig Topper66f09ad2014-06-08 22:29:17 +0000178bar *llvm::fub() { return nullptr; }
Sean Silva35dd8772012-10-11 23:30:40 +0000179
180namespace {
181namespace inferred_upcasting {
182// This test case verifies correct behavior of inferred upcasts when the
183// types are statically known to be OK to upcast. This is the case when,
184// for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
185
186// Note: This test will actually fail to compile without inferred
187// upcasting.
188
189class Base {
190public:
191 // No classof. We are testing that the upcast is inferred.
192 Base() {}
193};
194
195class Derived : public Base {
196public:
197 Derived() {}
198};
199
200// Even with no explicit classof() in Base, we should still be able to cast
201// Derived to its base class.
202TEST(CastingTest, UpcastIsInferred) {
203 Derived D;
204 EXPECT_TRUE(isa<Base>(D));
205 Base *BP = dyn_cast<Base>(&D);
Craig Topper66f09ad2014-06-08 22:29:17 +0000206 EXPECT_TRUE(BP != nullptr);
Sean Silva35dd8772012-10-11 23:30:40 +0000207}
208
209
210// This test verifies that the inferred upcast takes precedence over an
211// explicitly written one. This is important because it verifies that the
212// dynamic check gets optimized away.
213class UseInferredUpcast {
214public:
215 int Dummy;
216 static bool classof(const UseInferredUpcast *) {
217 return false;
218 }
219};
220
221TEST(CastingTest, InferredUpcastTakesPrecedence) {
222 UseInferredUpcast UIU;
223 // Since the explicit classof() returns false, this will fail if the
224 // explicit one is used.
225 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
226}
227
228} // end namespace inferred_upcasting
229} // end anonymous namespace
Rafael Espindola9ed17612013-07-18 02:42:40 +0000230// Test that we reject casts of temporaries (and so the illegal cast gets used).
231namespace TemporaryCast {
232struct pod {};
233IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
234}
Duncan P. N. Exon Smith40301182014-11-24 03:13:02 +0000235
236namespace {
237namespace pointer_wrappers {
238
239struct Base {
240 bool IsDerived;
241 Base(bool IsDerived = false) : IsDerived(IsDerived) {}
242};
243
244struct Derived : Base {
245 Derived() : Base(true) {}
246 static bool classof(const Base *B) { return B->IsDerived; }
247};
248
249class PTy {
250 Base *B;
251public:
252 PTy(Base *B) : B(B) {}
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000253 explicit operator bool() const { return get(); }
Duncan P. N. Exon Smith40301182014-11-24 03:13:02 +0000254 Base *get() const { return B; }
255};
256
257} // end namespace pointer_wrappers
258} // end namespace
259
260namespace llvm {
261
262template <> struct simplify_type<pointer_wrappers::PTy> {
263 typedef pointer_wrappers::Base *SimpleType;
264 static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
265 return P.get();
266 }
267};
268template <> struct simplify_type<const pointer_wrappers::PTy> {
269 typedef pointer_wrappers::Base *SimpleType;
270 static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
271 return P.get();
272 }
273};
274
275} // end namespace llvm
276
277namespace {
278namespace pointer_wrappers {
279
280// Some objects.
281pointer_wrappers::Base B;
282pointer_wrappers::Derived D;
283
284// Mutable "smart" pointers.
285pointer_wrappers::PTy MN(nullptr);
286pointer_wrappers::PTy MB(&B);
287pointer_wrappers::PTy MD(&D);
288
289// Const "smart" pointers.
290const pointer_wrappers::PTy CN(nullptr);
291const pointer_wrappers::PTy CB(&B);
292const pointer_wrappers::PTy CD(&D);
293
294TEST(CastingTest, smart_isa) {
295 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB));
296 EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB));
297 EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD));
298 EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD));
299}
300
301TEST(CastingTest, smart_cast) {
302 EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D);
303 EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D);
304}
305
306TEST(CastingTest, smart_cast_or_null) {
307 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
308 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
309 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MD) == &D);
310 EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CD) == &D);
311}
312
313TEST(CastingTest, smart_dyn_cast) {
314 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MB) == nullptr);
315 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CB) == nullptr);
316 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MD) == &D);
317 EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CD) == &D);
318}
319
320TEST(CastingTest, smart_dyn_cast_or_null) {
321 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
322 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
323 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MB) == nullptr);
324 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CB) == nullptr);
325 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MD) == &D);
326 EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CD) == &D);
327}
328
329} // end namespace pointer_wrappers
330} // end namespace