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);
115 [[nodiscard]] ResultT
Default(ResultT defaultResult) {
117 return std::move(*result);
118 return defaultResult;
123 const char *message =
"Fell off the end of a type-switch") {
125 return std::move(*result);
134 std::optional<ResultT> result;
148 template <
typename CaseT,
typename CallableT>
162 template <
typename CallableT>
void Default(CallableT &&defaultFn) {
164 defaultFn(this->
value);
169 const char *message =
"Fell off the end of a type-switch") {
176 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 DefaultUnreachable(const char *message="Fell off the end of a type-switch")
Declare default as unreachable, making sure that all cases were handled.
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.
These are wrappers over isa* function that allow them to be used in generic algorithms such as llvm:a...
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.
This class provides various trait information about a callable object.