2010-07-21 03:35:55 +08:00
|
|
|
//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
|
2010-07-21 00:32:20 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-07-21 00:32:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Casting.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/IR/User.h"
|
2010-07-21 03:35:55 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-07-21 00:32:20 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2010-07-21 01:06:28 +08:00
|
|
|
namespace llvm {
|
2013-07-18 10:42:40 +08:00
|
|
|
// Used to test illegal cast. If a cast doesn't match any of the "real" ones,
|
|
|
|
// it will match this one.
|
|
|
|
struct IllegalCast;
|
2014-06-09 06:29:17 +08:00
|
|
|
template <typename T> IllegalCast *cast(...) { return nullptr; }
|
2010-07-21 01:06:28 +08:00
|
|
|
|
|
|
|
// set up two example classes
|
|
|
|
// with conversion facility
|
|
|
|
//
|
|
|
|
struct bar {
|
|
|
|
bar() {}
|
2010-07-22 23:24:48 +08:00
|
|
|
struct foo *baz();
|
|
|
|
struct foo *caz();
|
2010-07-22 23:37:20 +08:00
|
|
|
struct foo *daz();
|
|
|
|
struct foo *naz();
|
2010-07-21 01:06:28 +08:00
|
|
|
private:
|
|
|
|
bar(const bar &);
|
|
|
|
};
|
|
|
|
struct foo {
|
|
|
|
void ext() const;
|
|
|
|
/* static bool classof(const bar *X) {
|
|
|
|
cerr << "Classof: " << X << "\n";
|
|
|
|
return true;
|
|
|
|
}*/
|
|
|
|
};
|
|
|
|
|
2017-04-13 03:59:37 +08:00
|
|
|
struct base {
|
|
|
|
virtual ~base() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct derived : public base {
|
|
|
|
static bool classof(const base *B) { return true; }
|
|
|
|
};
|
|
|
|
|
2010-07-22 23:37:20 +08:00
|
|
|
template <> struct isa_impl<foo, bar> {
|
2010-07-21 01:06:28 +08:00
|
|
|
static inline bool doit(const bar &Val) {
|
|
|
|
dbgs() << "Classof: " << &Val << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-13 03:59:37 +08:00
|
|
|
template <typename T> struct isa_impl<foo, T> {
|
|
|
|
static inline bool doit(const T &Val) { return false; }
|
|
|
|
};
|
|
|
|
|
2010-07-22 23:24:48 +08:00
|
|
|
foo *bar::baz() {
|
2010-07-21 01:06:28 +08:00
|
|
|
return cast<foo>(this);
|
2010-07-22 23:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foo *bar::caz() {
|
|
|
|
return cast_or_null<foo>(this);
|
|
|
|
}
|
|
|
|
|
2010-07-22 23:37:20 +08:00
|
|
|
foo *bar::daz() {
|
2010-07-22 23:24:48 +08:00
|
|
|
return dyn_cast<foo>(this);
|
2010-07-22 23:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foo *bar::naz() {
|
|
|
|
return dyn_cast_or_null<foo>(this);
|
|
|
|
}
|
2010-07-21 01:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
bar *fub();
|
2013-07-18 10:42:40 +08:00
|
|
|
|
|
|
|
template <> struct simplify_type<foo> {
|
|
|
|
typedef int SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(foo &Val) { return 0; }
|
|
|
|
};
|
|
|
|
|
2010-07-21 01:06:28 +08:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2010-07-21 00:32:20 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-07-18 10:42:40 +08:00
|
|
|
|
|
|
|
// Test the peculiar behavior of Use in simplify_type.
|
2014-03-07 22:42:25 +08:00
|
|
|
static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value,
|
|
|
|
"Use doesn't simplify correctly!");
|
|
|
|
static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value,
|
|
|
|
"Use doesn't simplify correctly!");
|
2013-07-18 10:42:40 +08:00
|
|
|
|
|
|
|
// Test that a regular class behaves as expected.
|
2014-03-07 22:42:25 +08:00
|
|
|
static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value,
|
|
|
|
"Unexpected simplify_type result!");
|
|
|
|
static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value,
|
|
|
|
"Unexpected simplify_type result!");
|
2013-07-18 10:42:40 +08:00
|
|
|
|
2010-07-21 00:32:20 +08:00
|
|
|
namespace {
|
|
|
|
|
2014-06-09 06:29:17 +08:00
|
|
|
const foo *null_foo = nullptr;
|
2010-07-21 00:51:18 +08:00
|
|
|
|
2012-01-22 20:14:35 +08:00
|
|
|
bar B;
|
2010-07-21 00:32:20 +08:00
|
|
|
extern bar &B1;
|
2012-01-22 20:14:35 +08:00
|
|
|
bar &B1 = B;
|
2010-07-21 00:32:20 +08:00
|
|
|
extern const bar *B2;
|
2010-07-21 00:51:18 +08:00
|
|
|
// test various configurations of const
|
|
|
|
const bar &B3 = B1;
|
|
|
|
const bar *const B4 = B2;
|
2010-07-21 00:32:20 +08:00
|
|
|
|
2010-07-21 00:38:12 +08:00
|
|
|
TEST(CastingTest, isa) {
|
2010-07-21 00:32:20 +08:00
|
|
|
EXPECT_TRUE(isa<foo>(B1));
|
2010-07-21 00:38:12 +08:00
|
|
|
EXPECT_TRUE(isa<foo>(B2));
|
|
|
|
EXPECT_TRUE(isa<foo>(B3));
|
|
|
|
EXPECT_TRUE(isa<foo>(B4));
|
2010-07-21 00:32:20 +08:00
|
|
|
}
|
|
|
|
|
2010-07-21 00:51:18 +08:00
|
|
|
TEST(CastingTest, cast) {
|
|
|
|
foo &F1 = cast<foo>(B1);
|
|
|
|
EXPECT_NE(&F1, null_foo);
|
|
|
|
const foo *F3 = cast<foo>(B2);
|
|
|
|
EXPECT_NE(F3, null_foo);
|
|
|
|
const foo *F4 = cast<foo>(B2);
|
|
|
|
EXPECT_NE(F4, null_foo);
|
2010-07-21 01:06:28 +08:00
|
|
|
const foo &F5 = cast<foo>(B3);
|
|
|
|
EXPECT_NE(&F5, null_foo);
|
|
|
|
const foo *F6 = cast<foo>(B4);
|
|
|
|
EXPECT_NE(F6, null_foo);
|
2012-08-22 04:39:25 +08:00
|
|
|
// Can't pass null pointer to cast<>.
|
|
|
|
// foo *F7 = cast<foo>(fub());
|
|
|
|
// EXPECT_EQ(F7, null_foo);
|
2010-07-22 23:24:48 +08:00
|
|
|
foo *F8 = B1.baz();
|
|
|
|
EXPECT_NE(F8, null_foo);
|
2017-04-13 03:59:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const bar> BP(B2);
|
|
|
|
auto FP = cast<foo>(std::move(BP));
|
|
|
|
static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value,
|
|
|
|
"Incorrect deduced return type!");
|
|
|
|
EXPECT_NE(FP.get(), null_foo);
|
|
|
|
FP.release();
|
2010-07-21 00:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, cast_or_null) {
|
|
|
|
const foo *F11 = cast_or_null<foo>(B2);
|
|
|
|
EXPECT_NE(F11, null_foo);
|
|
|
|
const foo *F12 = cast_or_null<foo>(B2);
|
|
|
|
EXPECT_NE(F12, null_foo);
|
|
|
|
const foo *F13 = cast_or_null<foo>(B4);
|
|
|
|
EXPECT_NE(F13, null_foo);
|
|
|
|
const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
|
|
|
|
EXPECT_EQ(F14, null_foo);
|
2010-07-22 23:24:48 +08:00
|
|
|
foo *F15 = B1.caz();
|
|
|
|
EXPECT_NE(F15, null_foo);
|
2017-04-13 03:59:37 +08:00
|
|
|
|
|
|
|
std::unique_ptr<const bar> BP(fub());
|
|
|
|
auto FP = cast_or_null<foo>(std::move(BP));
|
|
|
|
EXPECT_EQ(FP.get(), null_foo);
|
2010-07-22 23:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, dyn_cast) {
|
2010-07-22 23:28:30 +08:00
|
|
|
const foo *F1 = dyn_cast<foo>(B2);
|
|
|
|
EXPECT_NE(F1, null_foo);
|
|
|
|
const foo *F2 = dyn_cast<foo>(B2);
|
|
|
|
EXPECT_NE(F2, null_foo);
|
|
|
|
const foo *F3 = dyn_cast<foo>(B4);
|
2010-07-22 23:24:48 +08:00
|
|
|
EXPECT_NE(F3, null_foo);
|
2012-08-22 04:39:25 +08:00
|
|
|
// Can't pass null pointer to dyn_cast<>.
|
|
|
|
// foo *F4 = dyn_cast<foo>(fub());
|
2010-07-22 23:37:20 +08:00
|
|
|
// EXPECT_EQ(F4, null_foo);
|
|
|
|
foo *F5 = B1.daz();
|
|
|
|
EXPECT_NE(F5, null_foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, dyn_cast_or_null) {
|
|
|
|
const foo *F1 = dyn_cast_or_null<foo>(B2);
|
|
|
|
EXPECT_NE(F1, null_foo);
|
|
|
|
const foo *F2 = dyn_cast_or_null<foo>(B2);
|
|
|
|
EXPECT_NE(F2, null_foo);
|
|
|
|
const foo *F3 = dyn_cast_or_null<foo>(B4);
|
|
|
|
EXPECT_NE(F3, null_foo);
|
|
|
|
foo *F4 = dyn_cast_or_null<foo>(fub());
|
2010-07-22 23:28:30 +08:00
|
|
|
EXPECT_EQ(F4, null_foo);
|
2010-07-22 23:37:20 +08:00
|
|
|
foo *F5 = B1.naz();
|
|
|
|
EXPECT_NE(F5, null_foo);
|
2010-07-21 00:51:18 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 03:59:37 +08:00
|
|
|
std::unique_ptr<derived> newd() { return llvm::make_unique<derived>(); }
|
|
|
|
std::unique_ptr<base> newb() { return llvm::make_unique<derived>(); }
|
|
|
|
|
|
|
|
TEST(CastingTest, unique_dyn_cast) {
|
|
|
|
derived *OrigD = nullptr;
|
|
|
|
auto D = llvm::make_unique<derived>();
|
|
|
|
OrigD = D.get();
|
|
|
|
|
|
|
|
// Converting from D to itself is valid, it should return a new unique_ptr
|
|
|
|
// and the old one should become nullptr.
|
|
|
|
auto NewD = unique_dyn_cast<derived>(D);
|
|
|
|
ASSERT_EQ(OrigD, NewD.get());
|
|
|
|
ASSERT_EQ(nullptr, D);
|
|
|
|
|
|
|
|
// Converting from D to B is valid, B should have a value and D should be
|
|
|
|
// nullptr.
|
|
|
|
auto B = unique_dyn_cast<base>(NewD);
|
|
|
|
ASSERT_EQ(OrigD, B.get());
|
|
|
|
ASSERT_EQ(nullptr, NewD);
|
|
|
|
|
|
|
|
// Converting from B to itself is valid, it should return a new unique_ptr
|
|
|
|
// and the old one should become nullptr.
|
|
|
|
auto NewB = unique_dyn_cast<base>(B);
|
|
|
|
ASSERT_EQ(OrigD, NewB.get());
|
|
|
|
ASSERT_EQ(nullptr, B);
|
|
|
|
|
|
|
|
// Converting from B to D is valid, D should have a value and B should be
|
|
|
|
// nullptr;
|
|
|
|
D = unique_dyn_cast<derived>(NewB);
|
|
|
|
ASSERT_EQ(OrigD, D.get());
|
|
|
|
ASSERT_EQ(nullptr, NewB);
|
|
|
|
|
|
|
|
// Converting between unrelated types should fail. The original value should
|
|
|
|
// remain unchanged and it should return nullptr.
|
|
|
|
auto F = unique_dyn_cast<foo>(D);
|
|
|
|
ASSERT_EQ(nullptr, F);
|
|
|
|
ASSERT_EQ(OrigD, D.get());
|
|
|
|
|
|
|
|
// All of the above should also hold for temporaries.
|
|
|
|
auto D2 = unique_dyn_cast<derived>(newd());
|
|
|
|
EXPECT_NE(nullptr, D2);
|
|
|
|
|
|
|
|
auto B2 = unique_dyn_cast<derived>(newb());
|
|
|
|
EXPECT_NE(nullptr, B2);
|
|
|
|
|
|
|
|
auto B3 = unique_dyn_cast<base>(newb());
|
|
|
|
EXPECT_NE(nullptr, B3);
|
|
|
|
|
|
|
|
auto F2 = unique_dyn_cast<foo>(newb());
|
|
|
|
EXPECT_EQ(nullptr, F2);
|
|
|
|
}
|
|
|
|
|
2010-07-21 01:06:28 +08:00
|
|
|
// These lines are errors...
|
|
|
|
//foo *F20 = cast<foo>(B2); // Yields const foo*
|
|
|
|
//foo &F21 = cast<foo>(B3); // Yields const foo&
|
|
|
|
//foo *F22 = cast<foo>(B4); // Yields const foo*
|
|
|
|
//foo &F23 = cast_or_null<foo>(B1);
|
|
|
|
//const foo &F24 = cast_or_null<foo>(B3);
|
|
|
|
|
2010-07-21 00:32:20 +08:00
|
|
|
const bar *B2 = &B;
|
|
|
|
} // anonymous namespace
|
2010-07-21 01:06:28 +08:00
|
|
|
|
2014-06-09 06:29:17 +08:00
|
|
|
bar *llvm::fub() { return nullptr; }
|
2012-10-12 07:30:40 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
namespace inferred_upcasting {
|
|
|
|
// This test case verifies correct behavior of inferred upcasts when the
|
|
|
|
// types are statically known to be OK to upcast. This is the case when,
|
|
|
|
// for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
|
|
|
|
|
|
|
|
// Note: This test will actually fail to compile without inferred
|
|
|
|
// upcasting.
|
|
|
|
|
|
|
|
class Base {
|
|
|
|
public:
|
|
|
|
// No classof. We are testing that the upcast is inferred.
|
|
|
|
Base() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Derived : public Base {
|
|
|
|
public:
|
|
|
|
Derived() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Even with no explicit classof() in Base, we should still be able to cast
|
|
|
|
// Derived to its base class.
|
|
|
|
TEST(CastingTest, UpcastIsInferred) {
|
|
|
|
Derived D;
|
|
|
|
EXPECT_TRUE(isa<Base>(D));
|
|
|
|
Base *BP = dyn_cast<Base>(&D);
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(BP != nullptr);
|
2012-10-12 07:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This test verifies that the inferred upcast takes precedence over an
|
|
|
|
// explicitly written one. This is important because it verifies that the
|
|
|
|
// dynamic check gets optimized away.
|
|
|
|
class UseInferredUpcast {
|
|
|
|
public:
|
|
|
|
int Dummy;
|
|
|
|
static bool classof(const UseInferredUpcast *) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(CastingTest, InferredUpcastTakesPrecedence) {
|
|
|
|
UseInferredUpcast UIU;
|
|
|
|
// Since the explicit classof() returns false, this will fail if the
|
|
|
|
// explicit one is used.
|
|
|
|
EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace inferred_upcasting
|
|
|
|
} // end anonymous namespace
|
2013-07-18 10:42:40 +08:00
|
|
|
// Test that we reject casts of temporaries (and so the illegal cast gets used).
|
|
|
|
namespace TemporaryCast {
|
|
|
|
struct pod {};
|
|
|
|
IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
|
|
|
|
}
|
2014-11-24 11:13:02 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
namespace pointer_wrappers {
|
|
|
|
|
|
|
|
struct Base {
|
|
|
|
bool IsDerived;
|
|
|
|
Base(bool IsDerived = false) : IsDerived(IsDerived) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived : Base {
|
|
|
|
Derived() : Base(true) {}
|
|
|
|
static bool classof(const Base *B) { return B->IsDerived; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class PTy {
|
|
|
|
Base *B;
|
|
|
|
public:
|
|
|
|
PTy(Base *B) : B(B) {}
|
2015-02-16 06:00:20 +08:00
|
|
|
explicit operator bool() const { return get(); }
|
2014-11-24 11:13:02 +08:00
|
|
|
Base *get() const { return B; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace pointer_wrappers
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template <> struct simplify_type<pointer_wrappers::PTy> {
|
|
|
|
typedef pointer_wrappers::Base *SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) {
|
|
|
|
return P.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <> struct simplify_type<const pointer_wrappers::PTy> {
|
|
|
|
typedef pointer_wrappers::Base *SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) {
|
|
|
|
return P.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
namespace pointer_wrappers {
|
|
|
|
|
|
|
|
// Some objects.
|
|
|
|
pointer_wrappers::Base B;
|
|
|
|
pointer_wrappers::Derived D;
|
|
|
|
|
|
|
|
// Mutable "smart" pointers.
|
|
|
|
pointer_wrappers::PTy MN(nullptr);
|
|
|
|
pointer_wrappers::PTy MB(&B);
|
|
|
|
pointer_wrappers::PTy MD(&D);
|
|
|
|
|
|
|
|
// Const "smart" pointers.
|
|
|
|
const pointer_wrappers::PTy CN(nullptr);
|
|
|
|
const pointer_wrappers::PTy CB(&B);
|
|
|
|
const pointer_wrappers::PTy CD(&D);
|
|
|
|
|
|
|
|
TEST(CastingTest, smart_isa) {
|
|
|
|
EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB));
|
|
|
|
EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB));
|
|
|
|
EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD));
|
|
|
|
EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, smart_cast) {
|
|
|
|
EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D);
|
|
|
|
EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, smart_cast_or_null) {
|
|
|
|
EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
|
|
|
|
EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
|
|
|
|
EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MD) == &D);
|
|
|
|
EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CD) == &D);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, smart_dyn_cast) {
|
|
|
|
EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MB) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CB) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MD) == &D);
|
|
|
|
EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CD) == &D);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CastingTest, smart_dyn_cast_or_null) {
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MN) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CN) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MB) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CB) == nullptr);
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MD) == &D);
|
|
|
|
EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CD) == &D);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace pointer_wrappers
|
|
|
|
} // end namespace
|