[llvm] [ADT] Simplify CastInfo<To, PointerUnion<PTs...>> (NFC) (PR #156274)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 31 20:57:40 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
This patch simplifies CastInfo<To, PointerUnion<PTs...>> by "inlining"
CastInfoPointerUnionImpl into the CastInfo specialization.
Here is a brief background:
https://reviews.llvm.org/D125609
added support for CastInfo<To, PointerUnion<PTs...>> along with helper
struct CastInfoPointerUnionImpl. During the review, we did discuss
the idea of implementing the CastInfo specialization without the
helper struct, but the suggested solution did not work.
This patch attempts to simplify the CastInfo specialization again by
making CastInfo a friend of PointerUnion:
template <typename To, typename From, typename Enable>
friend struct CastInfo;
This gives CastInfo more access to PointerUnion than strictly
necessary, but the ability to simplify the CastInfo specialization
outweighs the risk.
---
Full diff: https://github.com/llvm/llvm-project/pull/156274.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/PointerUnion.h (+11-30)
``````````diff
diff --git a/llvm/include/llvm/ADT/PointerUnion.h b/llvm/include/llvm/ADT/PointerUnion.h
index cdbd76d7f505b..24caa960c718d 100644
--- a/llvm/include/llvm/ADT/PointerUnion.h
+++ b/llvm/include/llvm/ADT/PointerUnion.h
@@ -126,10 +126,12 @@ class PointerUnion
using First = TypeAtIndex<0, PTs...>;
using Base = typename PointerUnion::PointerUnionMembers;
- /// This is needed to give the CastInfo implementation below access
- /// to protected members.
- /// Refer to its definition for further details.
- friend struct CastInfoPointerUnionImpl<PTs...>;
+ // Give the CastInfo specialization below access to protected members.
+ //
+ // This makes all of CastInfo a friend, which is more than strictly
+ // necessary. It's a workaround for C++'s inability to friend a
+ // partial template specialization.
+ template <typename To, typename From, typename Enable> friend struct CastInfo;
public:
PointerUnion() = default;
@@ -219,42 +221,21 @@ bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) {
return lhs.getOpaqueValue() < rhs.getOpaqueValue();
}
-/// We can't (at least, at this moment with C++14) declare CastInfo
-/// as a friend of PointerUnion like this:
-/// ```
-/// template<typename To>
-/// friend struct CastInfo<To, PointerUnion<PTs...>>;
-/// ```
-/// The compiler complains 'Partial specialization cannot be declared as a
-/// friend'.
-/// So we define this struct to be a bridge between CastInfo and
-/// PointerUnion.
-template <typename... PTs> struct CastInfoPointerUnionImpl {
- using From = PointerUnion<PTs...>;
-
- template <typename To> static inline bool isPossible(From &F) {
- return F.Val.getInt() == FirstIndexOfType<To, PTs...>::value;
- }
-
- template <typename To> static To doCast(From &F) {
- assert(isPossible<To>(F) && "cast to an incompatible type!");
- return PointerLikeTypeTraits<To>::getFromVoidPointer(F.Val.getPointer());
- }
-};
-
// Specialization of CastInfo for PointerUnion
template <typename To, typename... PTs>
struct CastInfo<To, PointerUnion<PTs...>>
: public DefaultDoCastIfPossible<To, PointerUnion<PTs...>,
CastInfo<To, PointerUnion<PTs...>>> {
using From = PointerUnion<PTs...>;
- using Impl = CastInfoPointerUnionImpl<PTs...>;
static inline bool isPossible(From &f) {
- return Impl::template isPossible<To>(f);
+ return f.Val.getInt() == FirstIndexOfType<To, PTs...>::value;
}
- static To doCast(From &f) { return Impl::template doCast<To>(f); }
+ static To doCast(From &f) {
+ assert(isPossible(f) && "cast to an incompatible type!");
+ return PointerLikeTypeTraits<To>::getFromVoidPointer(f.Val.getPointer());
+ }
static inline To castFailed() { return To(); }
};
``````````
</details>
https://github.com/llvm/llvm-project/pull/156274
More information about the llvm-commits
mailing list