14#ifndef LLVM_SUPPORT_CASTING_H
15#define LLVM_SUPPORT_CASTING_H
63template <
typename To,
typename From,
typename Enabler =
void>
struct isa_impl {
64 static inline bool doit(
const From &Val) {
return To::classof(&Val); }
68template <
typename To,
typename From>
70 static inline bool doit(
const From &) {
return true; }
85template <
typename To,
typename From>
87 static inline bool doit(
const std::unique_ptr<From> &Val) {
88 assert(Val &&
"isa<> used on a null pointer");
95 assert(Val &&
"isa<> used on a null pointer");
102 assert(Val &&
"isa<> used on a null pointer");
109 assert(Val &&
"isa<> used on a null pointer");
114template <
typename To,
typename From>
117 assert(Val &&
"isa<> used on a null pointer");
122template <
typename To,
typename From,
typename SimpleFrom>
133template <
typename To,
typename FromTy>
136 static bool doit(
const FromTy &Val) {
145template <
class To,
class From>
struct cast_retty;
168template <
class To,
class From>
172 using ResultType = std::remove_pointer_t<PointerType>;
214 return *(std::remove_reference_t<typename cast_retty<To, FromTy>::ret_type>
215 *)&
const_cast<FromTy &
>(Val);
219template <
class To,
class FromTy>
234 std::is_same_v<X, typename simplify_type<X>::SimpleType>;
252template <
typename To,
typename From,
typename Enable =
void>
265template <
typename To,
typename From>
267 static inline bool isPossible(
const std::optional<From> &f) {
268 assert(f &&
"CastIsPossible::isPossible called on a nullopt!");
277template <
typename To,
typename From>
308template <
typename To,
typename From,
typename Derived>
311 if (!Derived::isPossible(f))
312 return Derived::castFailed();
313 return Derived::doCast(f);
320template <
typename OptionalDerived,
typename Default>
321using SelfType = std::conditional_t<std::is_same_v<OptionalDerived, void>,
328template <
typename To,
typename From,
typename Derived =
void>
334 detail::SelfType<Derived, ValueFromPointerCast<To, From>>> {
342template <
typename To,
typename From,
typename Derived =
void>
346 std::remove_reference_t<typename cast_retty<To, From>::ret_type>>;
349 return CastResultType((
typename CastResultType::element_type *)f.release());
355 if (!Self::isPossible(f.get()))
357 return doCast(std::move(f));
364template <
typename To,
typename From,
typename Derived =
void>
368 std::optional<To>, From,
369 detail::SelfType<Derived, OptionalValueCast<To, From>>> {
370 static inline std::optional<To>
castFailed() {
return std::optional<To>{}; }
372 static inline std::optional<To>
doCast(
const From &f) {
return To(f); }
387template <
typename To,
typename From,
typename ForwardTo>
390 using DecayedFrom = std::remove_cv_t<std::remove_pointer_t<From>>;
396 return ForwardTo::isPossible(
const_cast<NonConstFrom>(f));
399 static inline decltype(
auto)
castFailed() {
return ForwardTo::castFailed(); }
406 return ForwardTo::doCastIfPossible(
const_cast<NonConstFrom>(f));
422template <
typename To,
typename From,
typename ForwardTo>
425 return ForwardTo::isPossible(&f);
429 return *ForwardTo::doCast(&f);
475template <
typename To,
typename From,
typename Enable =
void>
502template <
typename To,
typename From>
509 return SimplifiedSelf::isPossible(
518 return SimplifiedSelf::castFailed();
522 return SimplifiedSelf::doCastIfPossible(
532template <
typename To,
typename From>
538template <
typename To,
typename From>
547template <
typename To,
typename From>
548[[nodiscard]]
inline bool isa(
const From &Val) {
552template <
typename First,
typename Second,
typename... Rest,
typename From>
553[[nodiscard]]
inline bool isa(
const From &Val) {
554 return isa<First>(Val) ||
isa<Second, Rest...>(Val);
564template <
typename To,
typename From>
565[[nodiscard]]
inline decltype(
auto)
cast(
const From &Val) {
566 assert(isa<To>(Val) &&
"cast<Ty>() argument of incompatible type!");
570template <
typename To,
typename From>
571[[nodiscard]]
inline decltype(
auto)
cast(
From &Val) {
572 assert(isa<To>(Val) &&
"cast<Ty>() argument of incompatible type!");
576template <
typename To,
typename From>
577[[nodiscard]]
inline decltype(
auto)
cast(
From *Val) {
578 assert(isa<To>(Val) &&
"cast<Ty>() argument of incompatible type!");
582template <
typename To,
typename From>
583[[nodiscard]]
inline decltype(
auto)
cast(std::unique_ptr<From> &&Val) {
584 assert(isa<To>(Val) &&
"cast<Ty>() argument of incompatible type!");
594 std::is_pointer_v<T> || std::is_constructible_v<T, std::nullptr_t>;
611 static inline bool isPresent(
const std::optional<T> &t) {
612 return t.has_value();
614 static inline decltype(
auto)
unwrapValue(std::optional<T> &t) {
return *t; }
622 static inline bool isPresent(
const T &t) {
return t !=
T(
nullptr); }
648template <
typename To,
typename From>
654template <
typename To,
typename From>
660template <
typename To,
typename From>
666template <
typename To,
typename From>
667[[nodiscard]]
inline decltype(
auto)
dyn_cast(std::unique_ptr<From> &Val) {
674template <
typename...
X,
class Y>
678 return isa<
X...>(Val);
681template <
typename...
X,
class Y>
688template <
class X,
class Y>
692 assert(isa<X>(Val) &&
"cast_if_present<Ty>() argument of incompatible type!");
699 assert(isa<X>(Val) &&
"cast_if_present<Ty>() argument of incompatible type!");
706 assert(isa<X>(Val) &&
"cast_if_present<Ty>() argument of incompatible type!");
710template <
class X,
class Y>
721 return cast_if_present<X>(Val);
725 return cast_if_present<X>(Val);
729 return cast_if_present<X>(Val);
732template <
class X,
class Y>
auto cast_or_null(std::unique_ptr<Y> &&Val) {
733 return cast_if_present<X>(std::move(Val));
760 return dyn_cast_if_present<X>(Val);
764 return dyn_cast_if_present<X>(Val);
768 return dyn_cast_if_present<X>(Val);
776template <
class X,
class Y>
777[[nodiscard]]
inline typename CastInfo<X, std::unique_ptr<Y>>::CastResultType
781 return cast<X>(std::move(Val));
784template <
class X,
class Y>
786 return unique_dyn_cast<X, Y>(Val);
791template <
class X,
class Y>
792[[nodiscard]]
inline typename CastInfo<X, std::unique_ptr<Y>>::CastResultType
796 return unique_dyn_cast<X, Y>(Val);
799template <
class X,
class Y>
801 return unique_dyn_cast_or_null<X, Y>(Val);
815 template <
typename T> [[nodiscard]]
bool operator()(
const T &Val)
const {
816 return isa<Types...>(Val);
821 template <
typename T> [[nodiscard]]
bool operator()(
const T &Val)
const {
835template <
typename... Types>
846template <
typename... Types>
847inline constexpr detail::IsaAndPresentCheckPredicate<Types...>
BlockVerifier::State From
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::conditional_t< std::is_same_v< OptionalDerived, void >, Default, OptionalDerived > SelfType
A helper to derive the type to use with Self for cast traits, when the provided CRTP derived type is ...
bool isPresent(const T &t)
decltype(auto) unwrapValue(T &t)
This is an optimization pass for GlobalISel generic memory operations.
auto cast_if_present(const Y &Val)
cast_if_present<X> - Functionally identical to cast, except that a null value is accepted.
CastInfo< X, std::unique_ptr< Y > >::CastResultType unique_dyn_cast(std::unique_ptr< Y > &Val)
unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>, taking ownership of the in...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
auto cast_or_null(const Y &Val)
constexpr detail::IsaAndPresentCheckPredicate< Types... > IsaAndPresentPred
Function object wrapper for the llvm::isa_and_present type check.
bool isa_and_nonnull(const Y &Val)
auto dyn_cast_or_null(const Y &Val)
bool isa_and_present(const Y &Val)
isa_and_present<X> - Functionally identical to isa, except that a null value is accepted.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
constexpr bool IsNullable
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
CastInfo< X, std::unique_ptr< Y > >::CastResultType unique_dyn_cast_or_null(std::unique_ptr< Y > &Val)
@ Default
The result values are uniform if and only if all operands are uniform.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Implement std::hash so that hash_code can be used in STL containers.
typename simplify_type< From >::SimpleType SimpleFrom
static decltype(auto) castFailed()
static bool isPossible(From &f)
static decltype(auto) doCast(From &f)
static decltype(auto) doCastIfPossible(From &f)
This struct provides a method for customizing the way a cast is performed.
static CastReturnType castFailed()
static CastReturnType doCast(const From &f)
static CastReturnType doCastIfPossible(const From &f)
typename cast_retty< To, From >::ret_type CastReturnType
static bool isPossible(const From &f)
static bool isPossible(const std::optional< From > &f)
This struct provides a way to check if a given cast is possible.
static bool isPossible(const From &f)
Provides a cast trait that strips const from types to make it easier to implement a const-version of ...
static decltype(auto) castFailed()
static decltype(auto) doCastIfPossible(const From &f)
std::conditional_t< std::is_pointer_v< From >, DecayedFrom *, DecayedFrom & > NonConstFrom
static decltype(auto) doCast(const From &f)
static bool isPossible(const From &f)
std::remove_cv_t< std::remove_pointer_t< From > > DecayedFrom
This cast trait just provides the default implementation of doCastIfPossible to make CastInfo special...
static To doCastIfPossible(From f)
Provides a cast trait that uses a defined pointer to pointer cast as a base for reference-to-referenc...
static bool isPossible(const From &f)
static decltype(auto) doCast(const From &f)
All of these cast traits are meant to be implementations for useful casts that users may want to use ...
This cast trait provides std::optional<T> casting.
static std::optional< To > doCast(const From &f)
static std::optional< To > castFailed()
This cast trait provides std::unique_ptr casting.
std::unique_ptr< std::remove_reference_t< typename cast_retty< To, From >::ret_type > > CastResultType
detail::SelfType< Derived, UniquePtrCast< To, From > > Self
static CastResultType doCast(std::unique_ptr< From > &&f)
static CastResultType doCastIfPossible(std::unique_ptr< From > &f)
static CastResultType castFailed()
This cast trait provides casting for the specific case of casting to a value-typed object from a poin...
static To doCast(From *f)
static decltype(auto) unwrapValue(T &t)
static bool isPresent(const T &t)
static decltype(auto) unwrapValue(std::optional< T > &t)
static bool isPresent(const std::optional< T > &t)
ValueIsPresent provides a way to check if a value is, well, present.
static bool isPresent(const T &t)
static decltype(auto) unwrapValue(T &t)
static cast_retty< To, FromTy >::ret_type doit(const FromTy &Val)
static cast_retty< To, FromTy * >::ret_type doit(const FromTy *Val)
static cast_retty< To, From >::ret_type doit(const From &Val)
std::unique_ptr< ResultType > ret_type
typename cast_retty_impl< To, FromTy >::ret_type ret_type
typename cast_retty< To, SimpleFrom >::ret_type ret_type
typename cast_retty_wrap< To, From, typename simplify_type< From >::SimpleType >::ret_type ret_type
bool operator()(const T &Val) const
bool operator()(const T &Val) const
static bool doit(const From &)
static bool doit(const From *Val)
static bool doit(const From *Val)
static bool doit(const From &Val)
static bool doit(const From *Val)
static bool doit(const From *Val)
static bool doit(const std::unique_ptr< From > &Val)
static bool doit(const From &Val)
static bool doit(const FromTy &Val)
static bool doit(const From &Val)
static bool doit(const From &Val)
typename add_const_past_pointer< NonConstSimpleType >::type SimpleType
typename add_lvalue_reference_if_not_pointer< SimpleType >::type RetType
typename simplify_type< From >::SimpleType NonConstSimpleType
static RetType getSimplifiedValue(const From &Val)
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
static SimpleType & getSimplifiedValue(From &Val)