diff --git a/llvm/include/llvm/ADT/PointerIntPair.h b/llvm/include/llvm/ADT/PointerIntPair.h index 119285087957f..9278ccdb47887 100644 --- a/llvm/include/llvm/ADT/PointerIntPair.h +++ b/llvm/include/llvm/ADT/PointerIntPair.h @@ -227,6 +227,32 @@ struct PointerLikeTypeTraits< PtrTraits::NumLowBitsAvailable - IntBits; }; +// Allow structured bindings on PointerIntPair. +template +decltype(auto) +get(const PointerIntPair &Pair) { + static_assert(I < 2); + if constexpr (I == 0) + return Pair.getPointer(); + else + return Pair.getInt(); +} + } // end namespace llvm +namespace std { +template +struct tuple_size< + llvm::PointerIntPair> + : std::integral_constant {}; + +template +struct tuple_element< + I, llvm::PointerIntPair> + : std::conditional {}; +} // namespace std + #endif // LLVM_ADT_POINTERINTPAIR_H diff --git a/llvm/unittests/ADT/PointerIntPairTest.cpp b/llvm/unittests/ADT/PointerIntPairTest.cpp index 9e5e0ee1614be..b2790050a242b 100644 --- a/llvm/unittests/ADT/PointerIntPairTest.cpp +++ b/llvm/unittests/ADT/PointerIntPairTest.cpp @@ -62,6 +62,10 @@ TEST(PointerIntPairTest, GetSet) { EXPECT_EQ(&s, Pair2.getPointer()); EXPECT_EQ(E::Case3, Pair2.getInt()); + auto [Pointer2, Int2] = Pair2; + EXPECT_EQ(Pair2.getPointer(), Pointer2); + EXPECT_EQ(Pair2.getInt(), Int2); + static_assert(std::is_trivially_copyable_v>, "trivially copyable"); }