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 @@ -456,47 +456,18 @@ using CastReturnType = typename cast_retty::ret_type; - static inline CastReturnType doCast(From &f) { - return cast_convert_val::SimpleType>::doit(f); + static inline CastReturnType doCast(const From &f) { + return cast_convert_val< + To, From, + typename simplify_type::SimpleType>::doit(const_cast(f)); } // This assumes that you can construct the cast return type from `nullptr`. // This is largely to support legacy use cases - if you don't want this // behavior you should specialize CastInfo for your use case. - // - // FIXME: fix legacy use cases to specialize CastInfo so we can remove these - // two - they don't really belong here, as it doesn't make sense to - // have a fallible reference-to-reference cast if the type isn't - // constructible from nullptr, which doesn't really happen in LLVM - // outside of MLIR (which is a separate use case in itself). static inline CastReturnType castFailed() { return CastReturnType(nullptr); } - static inline CastReturnType doCastIfPossible(From &f) { - if (!Self::isPossible(f)) - return castFailed(); - return doCast(f); - } -}; - -/// Provides a CastInfo partial specialization for pointers. This version *does* -/// provide castFailed and doCastIfPossible because for pointers, a fallible -/// cast is trivial - just return nullptr. -template -struct CastInfo::value>> - : public CastIsPossible { - using Self = CastInfo; - - using CastReturnType = typename cast_retty::ret_type; - - static inline CastReturnType doCast(From *f) { - // We know it is a simple type, so we can just do cast_convert_val. - return cast_convert_val::doit(f); - } - - static inline CastReturnType castFailed() { return CastReturnType(nullptr); } - - static inline CastReturnType doCastIfPossible(From *f) { + static inline CastReturnType doCastIfPossible(const From &f) { if (!Self::isPossible(f)) return castFailed(); return doCast(f);