From: bzcheeseman Date: Fri, 13 May 2022 22:41:22 +0000 (-0400) Subject: [LLVM][Casting.h] Add ForwardToPointerCast trait X-Git-Tag: upstream/15.0.7~7755 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c7587080188e1f46ddf8b8274e99d4481361d5dc;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h index da511e9..37f85da 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 { ... verbose implementation... }; +/// +/// template <> +/// struct CastInfo +/// : public ForwardToPointerCast> {}; +/// +template +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 //===----------------------------------------------------------------------===//