[llvm] c758708 - [LLVM][Casting.h] Add ForwardToPointerCast trait
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 13 15:48:55 PDT 2022
Author: bzcheeseman
Date: 2022-05-13T18:48:50-04:00
New Revision: c7587080188e1f46ddf8b8274e99d4481361d5dc
URL: https://github.com/llvm/llvm-project/commit/c7587080188e1f46ddf8b8274e99d4481361d5dc
DIFF: https://github.com/llvm/llvm-project/commit/c7587080188e1f46ddf8b8274e99d4481361d5dc.diff
LOG: [LLVM][Casting.h] Add ForwardToPointerCast trait
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.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D125576
Added:
Modified:
llvm/include/llvm/Support/Casting.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h
index da511e94dc98..37f85da868b2 100644
--- a/llvm/include/llvm/Support/Casting.h
+++ b/llvm/include/llvm/Support/Casting.h
@@ -408,6 +408,29 @@ struct ConstStrippingForwardingCast {
}
};
+/// 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
//===----------------------------------------------------------------------===//
More information about the llvm-commits
mailing list