Skip to content

Commit 13a9098

Browse files
committed
Specialize ValueIsPresent for PointerUnion
1 parent f833dff commit 13a9098

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

llvm/include/llvm/ADT/PointerUnion.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ class PointerUnion
198198
}
199199
};
200200

201-
template <typename... PTs>
202-
bool operator==(PointerUnion<PTs...> LHS, PointerUnion<PTs...> RHS) {
203-
return (!LHS && !RHS) || LHS.getOpaqueValue() == RHS.getOpaqueValue();
201+
template <typename ...PTs>
202+
bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
203+
return lhs.getOpaqueValue() == rhs.getOpaqueValue();
204204
}
205205

206-
template <typename... PTs>
207-
bool operator!=(PointerUnion<PTs...> LHS, PointerUnion<PTs...> RHS) {
208-
return !operator==(LHS, RHS);
206+
template <typename ...PTs>
207+
bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
208+
return lhs.getOpaqueValue() != rhs.getOpaqueValue();
209209
}
210210

211211
template <typename ...PTs>
@@ -259,6 +259,17 @@ struct CastInfo<To, const PointerUnion<PTs...>>
259259
CastInfo<To, PointerUnion<PTs...>>> {
260260
};
261261

262+
// The default implementation of isPresent() for nullable types returns true
263+
// if the active member is not the first one, even if its value is nullptr.
264+
// Override the default behavior to return false for all possible null values.
265+
template <typename... PTs>
266+
struct ValueIsPresent<PointerUnion<PTs...>,
267+
std::enable_if_t<IsNullable<PointerUnion<PTs...>>>> {
268+
using Union = PointerUnion<PTs...>;
269+
static bool isPresent(const Union &V) { return static_cast<bool>(V); }
270+
static decltype(auto) unwrapValue(Union &V) { return V; }
271+
};
272+
262273
// Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
263274
// # low bits available = min(PT1bits,PT2bits)-1.
264275
template <typename ...PTs>

llvm/unittests/ADT/PointerUnionTest.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,9 @@ TEST_F(PointerUnionTest, Comparison) {
5353
EXPECT_TRUE(i4 != l4);
5454
EXPECT_TRUE(f4 != l4);
5555
EXPECT_TRUE(l4 != d4);
56-
EXPECT_TRUE(i4null == f4null);
57-
EXPECT_FALSE(i4null != f4null);
58-
EXPECT_TRUE(i4null == l4null);
59-
EXPECT_FALSE(i4null != l4null);
60-
EXPECT_TRUE(i4null == d4null);
61-
EXPECT_FALSE(i4null != d4null);
62-
EXPECT_FALSE(i4null == i4);
63-
EXPECT_TRUE(i4null != i4);
64-
EXPECT_FALSE(i4null == f4);
65-
EXPECT_TRUE(i4null != f4);
56+
EXPECT_TRUE(i4null != f4null);
57+
EXPECT_TRUE(i4null != l4null);
58+
EXPECT_TRUE(i4null != d4null);
6659
}
6760

6861
TEST_F(PointerUnionTest, Null) {
@@ -215,6 +208,11 @@ TEST_F(PointerUnionTest, NewCastInfra) {
215208
EXPECT_FALSE(isa<float *>(d4null));
216209
EXPECT_FALSE(isa<long long *>(d4null));
217210

211+
EXPECT_FALSE(isa_and_present<int *>(i4null));
212+
EXPECT_FALSE(isa_and_present<float *>(f4null));
213+
EXPECT_FALSE(isa_and_present<long long *>(l4null));
214+
EXPECT_FALSE(isa_and_present<double *>(d4null));
215+
218216
// test cast<>
219217
EXPECT_EQ(cast<float *>(a), &f);
220218
EXPECT_EQ(cast<int *>(b), &i);

0 commit comments

Comments
 (0)