[PATCH] D125576: [LLVM][Casting.h] Add ForwardToPointerCast trait
Aman LaChapelle via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri May 13 11:53:04 PDT 2022
bzcheeseman created this revision.
Herald added subscribers: Chia-hungDuan, rriddle.
Herald added a project: All.
bzcheeseman requested review of this revision.
Herald added subscribers: llvm-commits, stephenneuendorffer.
Herald added a project: LLVM.
Addresses use cases in Clang/MLIR that need pointer-to-pointer, reference-to-reference, and value-to-value casts from/to the same types. This should reduce boilerplate by allowing the user to simply specify the pointer cast and forward the reference cast directly to the pointer cast.
This cast trait DOES NOT implement `castFailed` and `doCastIfPossible` because in the general case doing so could result in a nullptr dereference. Users can use `NullableValueCastFailed` and `DefaultDoCastIfPossible` as desired for those cases where `nullptr` is acceptable.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D125576
Files:
llvm/include/llvm/Support/Casting.h
Index: llvm/include/llvm/Support/Casting.h
===================================================================
--- llvm/include/llvm/Support/Casting.h
+++ llvm/include/llvm/Support/Casting.h
@@ -408,6 +408,29 @@
}
};
+/// Provides a cast trait that uses a defined pointer to pointer cast as a base
+/// for reference-to-reference casts. Note that it does not provide castFailed
+/// and doCastIfPossible because a pointer-to-pointer cast would likely just
+/// return `nullptr` which could cause nullptr dereference. You can use it like
+/// this:
+///
+/// template <> struct CastInfo<foo, bar *> { ... verbose implementation... };
+///
+/// template <>
+/// struct CastInfo<foo, bar>
+/// : public ForwardToPointerCast<foo, bar, CastInfo<foo, bar *>> {};
+///
+template <typename To, typename From, typename ForwardTo>
+struct ForwardToPointerCast {
+ static inline bool isPossible(const From &f) {
+ return ForwardTo::isPossible(&f);
+ }
+
+ static inline decltype(auto) doCast(const From &f) {
+ return *ForwardTo::doCast(&f);
+ }
+};
+
//===----------------------------------------------------------------------===//
// CastInfo
//===----------------------------------------------------------------------===//
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125576.429318.patch
Type: text/x-patch
Size: 1246 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220513/469efcdd/attachment.bin>
More information about the llvm-commits
mailing list