15#ifndef LLVM_ADT_TYPESWITCH_H
16#define LLVM_ADT_TYPESWITCH_H
38 template <
typename CaseT,
typename CaseT2,
typename... CaseTs,
46 DerivedT &derived =
static_cast<DerivedT &
>(*this);
48 .template
Case<CaseT2, CaseTs...>(caseFn);
55 template <
typename CallableT> DerivedT &
Case(CallableT &&caseFn) {
57 using CaseT = std::remove_cv_t<std::remove_pointer_t<
58 std::remove_reference_t<typename Traits::template arg_t<0>>>>;
60 DerivedT &derived =
static_cast<DerivedT &
>(*this);
61 return derived.template
Case<CaseT>(std::forward<CallableT>(caseFn));
66 template <
typename CastT,
typename ValueT>
87template <
typename T,
typename ResultT =
void>
96 template <
typename CaseT,
typename CallableT>
103 result.emplace(caseFn(caseValue));
108 template <
typename CallableT>
109 [[nodiscard]] ResultT
Default(CallableT &&defaultFn) {
111 return std::move(*result);
112 return defaultFn(this->
value);
116 [[nodiscard]] ResultT
Default(ResultT defaultResult) {
118 return std::move(*result);
119 return defaultResult;
123 template <
typename ArgT = ResultT,
125 std::enable_if_t<std::is_constructible_v<ArgT, std::nullptr_t>>>
126 [[nodiscard]] ResultT
Default(std::nullptr_t) {
127 return Default(ResultT(
nullptr));
131 template <
typename ArgT = ResultT,
133 std::enable_if_t<std::is_constructible_v<ArgT, std::nullopt_t>>>
134 [[nodiscard]] ResultT
Default(std::nullopt_t) {
135 return Default(ResultT(std::nullopt));
140 const char *message =
"Fell off the end of a type-switch") {
142 return std::move(*result);
151 std::optional<ResultT> result;
165 template <
typename CaseT,
typename CallableT>
179 template <
typename CallableT>
void Default(CallableT &&defaultFn) {
181 defaultFn(this->
value);
186 const char *message =
"Fell off the end of a type-switch") {
193 bool foundMatch =
false;
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
#define LLVM_ATTRIBUTE_NODEBUG
LLVM_ATTRIBUTE_NO_DEBUG - On compilers where we have a directive to do so, mark a method "no debug" b...
void Default(CallableT &&defaultFn)
As a default, invoke the given callable within the root value.
TypeSwitch(TypeSwitch &&other)=default
TypeSwitch< T, void > & Case(CallableT &&caseFn)
Add a case on the given type.
detail::TypeSwitchBase< TypeSwitch< T, void >, T > BaseT
void DefaultUnreachable(const char *message="Fell off the end of a type-switch")
Declare default as unreachable, making sure that all cases were handled.
detail::TypeSwitchBase< TypeSwitch< T, ResultT >, T > BaseT
ResultT Default(CallableT &&defaultFn)
As a default, invoke the given callable within the root value.
ResultT Default(ResultT defaultResult)
As a default, return the given value.
TypeSwitch(TypeSwitch &&other)=default
TypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
ResultT Default(std::nullptr_t)
Default for pointer-like results types that accept nullptr.
ResultT DefaultUnreachable(const char *message="Fell off the end of a type-switch")
Declare default as unreachable, making sure that all cases were handled.
ResultT Default(std::nullopt_t)
Default for optional results types that accept std::nullopt.
DerivedT & Case(CallableT &&caseFn)
Invoke a case on the derived class, inferring the type of the Case from the first input of the given ...
void operator=(TypeSwitchBase &&other)=delete
void operator=(const TypeSwitchBase &)=delete
~TypeSwitchBase()=default
TypeSwitchBase(TypeSwitchBase &&other)
static decltype(auto) castValue(ValueT &&value)
Attempt to dyn_cast the given value to CastT.
TypeSwitchBase(const TypeSwitchBase &)=delete
TypeSwitchBase is not copyable.
TypeSwitchBase(const T &value)
LLVM_ATTRIBUTE_ALWAYS_INLINE LLVM_ATTRIBUTE_NODEBUG DerivedT & Case(CallableT &&caseFn)
Invoke a case on the derived class with multiple case types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ Default
The result values are uniform if and only if all operands are uniform.
This class provides various trait information about a callable object.