diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h --- a/llvm/include/llvm/Support/Casting.h +++ b/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 { ... 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 //===----------------------------------------------------------------------===//