1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Casting.h"
10 #include "llvm/IR/User.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
17 // Used to test illegal cast. If a cast doesn't match any of the "real" ones,
18 // it will match this one.
20 template <typename T
> IllegalCast
*cast(...) { return nullptr; }
22 // set up two example classes
23 // with conversion facility
36 /* static bool classof(const bar *X) {
37 cerr << "Classof: " << X << "\n";
46 struct derived
: public base
{
47 static bool classof(const base
*B
) { return true; }
50 template <> struct isa_impl
<foo
, bar
> {
51 static inline bool doit(const bar
&Val
) {
52 dbgs() << "Classof: " << &Val
<< "\n";
57 template <typename T
> struct isa_impl
<foo
, T
> {
58 static inline bool doit(const T
&Val
) { return false; }
62 return cast
<foo
>(this);
66 return cast_or_null
<foo
>(this);
70 return dyn_cast
<foo
>(this);
74 return dyn_cast_or_null
<foo
>(this);
80 template <> struct simplify_type
<foo
> {
81 typedef int SimpleType
;
82 static SimpleType
getSimplifiedValue(foo
&Val
) { return 0; }
85 } // End llvm namespace
90 // Test the peculiar behavior of Use in simplify_type.
91 static_assert(std::is_same
<simplify_type
<Use
>::SimpleType
, Value
*>::value
,
92 "Use doesn't simplify correctly!");
93 static_assert(std::is_same
<simplify_type
<Use
*>::SimpleType
, Value
*>::value
,
94 "Use doesn't simplify correctly!");
96 // Test that a regular class behaves as expected.
97 static_assert(std::is_same
<simplify_type
<foo
>::SimpleType
, int>::value
,
98 "Unexpected simplify_type result!");
99 static_assert(std::is_same
<simplify_type
<foo
*>::SimpleType
, foo
*>::value
,
100 "Unexpected simplify_type result!");
104 const foo
*null_foo
= nullptr;
109 extern const bar
*B2
;
110 // test various configurations of const
112 const bar
*const B4
= B2
;
114 TEST(CastingTest
, isa
) {
115 EXPECT_TRUE(isa
<foo
>(B1
));
116 EXPECT_TRUE(isa
<foo
>(B2
));
117 EXPECT_TRUE(isa
<foo
>(B3
));
118 EXPECT_TRUE(isa
<foo
>(B4
));
121 TEST(CastingTest
, cast
) {
122 foo
&F1
= cast
<foo
>(B1
);
123 EXPECT_NE(&F1
, null_foo
);
124 const foo
*F3
= cast
<foo
>(B2
);
125 EXPECT_NE(F3
, null_foo
);
126 const foo
*F4
= cast
<foo
>(B2
);
127 EXPECT_NE(F4
, null_foo
);
128 const foo
&F5
= cast
<foo
>(B3
);
129 EXPECT_NE(&F5
, null_foo
);
130 const foo
*F6
= cast
<foo
>(B4
);
131 EXPECT_NE(F6
, null_foo
);
132 // Can't pass null pointer to cast<>.
133 // foo *F7 = cast<foo>(fub());
134 // EXPECT_EQ(F7, null_foo);
136 EXPECT_NE(F8
, null_foo
);
138 std::unique_ptr
<const bar
> BP(B2
);
139 auto FP
= cast
<foo
>(std::move(BP
));
140 static_assert(std::is_same
<std::unique_ptr
<const foo
>, decltype(FP
)>::value
,
141 "Incorrect deduced return type!");
142 EXPECT_NE(FP
.get(), null_foo
);
146 TEST(CastingTest
, cast_or_null
) {
147 const foo
*F11
= cast_or_null
<foo
>(B2
);
148 EXPECT_NE(F11
, null_foo
);
149 const foo
*F12
= cast_or_null
<foo
>(B2
);
150 EXPECT_NE(F12
, null_foo
);
151 const foo
*F13
= cast_or_null
<foo
>(B4
);
152 EXPECT_NE(F13
, null_foo
);
153 const foo
*F14
= cast_or_null
<foo
>(fub()); // Shouldn't print.
154 EXPECT_EQ(F14
, null_foo
);
156 EXPECT_NE(F15
, null_foo
);
158 std::unique_ptr
<const bar
> BP(fub());
159 auto FP
= cast_or_null
<foo
>(std::move(BP
));
160 EXPECT_EQ(FP
.get(), null_foo
);
163 TEST(CastingTest
, dyn_cast
) {
164 const foo
*F1
= dyn_cast
<foo
>(B2
);
165 EXPECT_NE(F1
, null_foo
);
166 const foo
*F2
= dyn_cast
<foo
>(B2
);
167 EXPECT_NE(F2
, null_foo
);
168 const foo
*F3
= dyn_cast
<foo
>(B4
);
169 EXPECT_NE(F3
, null_foo
);
170 // Can't pass null pointer to dyn_cast<>.
171 // foo *F4 = dyn_cast<foo>(fub());
172 // EXPECT_EQ(F4, null_foo);
174 EXPECT_NE(F5
, null_foo
);
177 TEST(CastingTest
, dyn_cast_or_null
) {
178 const foo
*F1
= dyn_cast_or_null
<foo
>(B2
);
179 EXPECT_NE(F1
, null_foo
);
180 const foo
*F2
= dyn_cast_or_null
<foo
>(B2
);
181 EXPECT_NE(F2
, null_foo
);
182 const foo
*F3
= dyn_cast_or_null
<foo
>(B4
);
183 EXPECT_NE(F3
, null_foo
);
184 foo
*F4
= dyn_cast_or_null
<foo
>(fub());
185 EXPECT_EQ(F4
, null_foo
);
187 EXPECT_NE(F5
, null_foo
);
190 std::unique_ptr
<derived
> newd() { return llvm::make_unique
<derived
>(); }
191 std::unique_ptr
<base
> newb() { return llvm::make_unique
<derived
>(); }
193 TEST(CastingTest
, unique_dyn_cast
) {
194 derived
*OrigD
= nullptr;
195 auto D
= llvm::make_unique
<derived
>();
198 // Converting from D to itself is valid, it should return a new unique_ptr
199 // and the old one should become nullptr.
200 auto NewD
= unique_dyn_cast
<derived
>(D
);
201 ASSERT_EQ(OrigD
, NewD
.get());
202 ASSERT_EQ(nullptr, D
);
204 // Converting from D to B is valid, B should have a value and D should be
206 auto B
= unique_dyn_cast
<base
>(NewD
);
207 ASSERT_EQ(OrigD
, B
.get());
208 ASSERT_EQ(nullptr, NewD
);
210 // Converting from B to itself is valid, it should return a new unique_ptr
211 // and the old one should become nullptr.
212 auto NewB
= unique_dyn_cast
<base
>(B
);
213 ASSERT_EQ(OrigD
, NewB
.get());
214 ASSERT_EQ(nullptr, B
);
216 // Converting from B to D is valid, D should have a value and B should be
218 D
= unique_dyn_cast
<derived
>(NewB
);
219 ASSERT_EQ(OrigD
, D
.get());
220 ASSERT_EQ(nullptr, NewB
);
222 // Converting between unrelated types should fail. The original value should
223 // remain unchanged and it should return nullptr.
224 auto F
= unique_dyn_cast
<foo
>(D
);
225 ASSERT_EQ(nullptr, F
);
226 ASSERT_EQ(OrigD
, D
.get());
228 // All of the above should also hold for temporaries.
229 auto D2
= unique_dyn_cast
<derived
>(newd());
230 EXPECT_NE(nullptr, D2
);
232 auto B2
= unique_dyn_cast
<derived
>(newb());
233 EXPECT_NE(nullptr, B2
);
235 auto B3
= unique_dyn_cast
<base
>(newb());
236 EXPECT_NE(nullptr, B3
);
238 auto F2
= unique_dyn_cast
<foo
>(newb());
239 EXPECT_EQ(nullptr, F2
);
242 // These lines are errors...
243 //foo *F20 = cast<foo>(B2); // Yields const foo*
244 //foo &F21 = cast<foo>(B3); // Yields const foo&
245 //foo *F22 = cast<foo>(B4); // Yields const foo*
246 //foo &F23 = cast_or_null<foo>(B1);
247 //const foo &F24 = cast_or_null<foo>(B3);
250 } // anonymous namespace
252 bar
*llvm::fub() { return nullptr; }
255 namespace inferred_upcasting
{
256 // This test case verifies correct behavior of inferred upcasts when the
257 // types are statically known to be OK to upcast. This is the case when,
258 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
260 // Note: This test will actually fail to compile without inferred
265 // No classof. We are testing that the upcast is inferred.
269 class Derived
: public Base
{
274 // Even with no explicit classof() in Base, we should still be able to cast
275 // Derived to its base class.
276 TEST(CastingTest
, UpcastIsInferred
) {
278 EXPECT_TRUE(isa
<Base
>(D
));
279 Base
*BP
= dyn_cast
<Base
>(&D
);
280 EXPECT_TRUE(BP
!= nullptr);
284 // This test verifies that the inferred upcast takes precedence over an
285 // explicitly written one. This is important because it verifies that the
286 // dynamic check gets optimized away.
287 class UseInferredUpcast
{
290 static bool classof(const UseInferredUpcast
*) {
295 TEST(CastingTest
, InferredUpcastTakesPrecedence
) {
296 UseInferredUpcast UIU
;
297 // Since the explicit classof() returns false, this will fail if the
298 // explicit one is used.
299 EXPECT_TRUE(isa
<UseInferredUpcast
>(&UIU
));
302 } // end namespace inferred_upcasting
303 } // end anonymous namespace
304 // Test that we reject casts of temporaries (and so the illegal cast gets used).
305 namespace TemporaryCast
{
307 IllegalCast
*testIllegalCast() { return cast
<foo
>(pod()); }
311 namespace pointer_wrappers
{
315 Base(bool IsDerived
= false) : IsDerived(IsDerived
) {}
318 struct Derived
: Base
{
319 Derived() : Base(true) {}
320 static bool classof(const Base
*B
) { return B
->IsDerived
; }
326 PTy(Base
*B
) : B(B
) {}
327 explicit operator bool() const { return get(); }
328 Base
*get() const { return B
; }
331 } // end namespace pointer_wrappers
336 template <> struct simplify_type
<pointer_wrappers::PTy
> {
337 typedef pointer_wrappers::Base
*SimpleType
;
338 static SimpleType
getSimplifiedValue(pointer_wrappers::PTy
&P
) {
342 template <> struct simplify_type
<const pointer_wrappers::PTy
> {
343 typedef pointer_wrappers::Base
*SimpleType
;
344 static SimpleType
getSimplifiedValue(const pointer_wrappers::PTy
&P
) {
349 } // end namespace llvm
352 namespace pointer_wrappers
{
355 pointer_wrappers::Base B
;
356 pointer_wrappers::Derived D
;
358 // Mutable "smart" pointers.
359 pointer_wrappers::PTy
MN(nullptr);
360 pointer_wrappers::PTy
MB(&B
);
361 pointer_wrappers::PTy
MD(&D
);
363 // Const "smart" pointers.
364 const pointer_wrappers::PTy
CN(nullptr);
365 const pointer_wrappers::PTy
CB(&B
);
366 const pointer_wrappers::PTy
CD(&D
);
368 TEST(CastingTest
, smart_isa
) {
369 EXPECT_TRUE(!isa
<pointer_wrappers::Derived
>(MB
));
370 EXPECT_TRUE(!isa
<pointer_wrappers::Derived
>(CB
));
371 EXPECT_TRUE(isa
<pointer_wrappers::Derived
>(MD
));
372 EXPECT_TRUE(isa
<pointer_wrappers::Derived
>(CD
));
375 TEST(CastingTest
, smart_cast
) {
376 EXPECT_TRUE(cast
<pointer_wrappers::Derived
>(MD
) == &D
);
377 EXPECT_TRUE(cast
<pointer_wrappers::Derived
>(CD
) == &D
);
380 TEST(CastingTest
, smart_cast_or_null
) {
381 EXPECT_TRUE(cast_or_null
<pointer_wrappers::Derived
>(MN
) == nullptr);
382 EXPECT_TRUE(cast_or_null
<pointer_wrappers::Derived
>(CN
) == nullptr);
383 EXPECT_TRUE(cast_or_null
<pointer_wrappers::Derived
>(MD
) == &D
);
384 EXPECT_TRUE(cast_or_null
<pointer_wrappers::Derived
>(CD
) == &D
);
387 TEST(CastingTest
, smart_dyn_cast
) {
388 EXPECT_TRUE(dyn_cast
<pointer_wrappers::Derived
>(MB
) == nullptr);
389 EXPECT_TRUE(dyn_cast
<pointer_wrappers::Derived
>(CB
) == nullptr);
390 EXPECT_TRUE(dyn_cast
<pointer_wrappers::Derived
>(MD
) == &D
);
391 EXPECT_TRUE(dyn_cast
<pointer_wrappers::Derived
>(CD
) == &D
);
394 TEST(CastingTest
, smart_dyn_cast_or_null
) {
395 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(MN
) == nullptr);
396 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(CN
) == nullptr);
397 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(MB
) == nullptr);
398 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(CB
) == nullptr);
399 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(MD
) == &D
);
400 EXPECT_TRUE(dyn_cast_or_null
<pointer_wrappers::Derived
>(CD
) == &D
);
403 } // end namespace pointer_wrappers