File indexing completed on 2026-05-10 08:43:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef LLVM_ADT_STLFUNCTIONALEXTRAS_H
0016 #define LLVM_ADT_STLFUNCTIONALEXTRAS_H
0017
0018 #include "llvm/ADT/STLForwardCompat.h"
0019 #include "llvm/Support/Compiler.h"
0020
0021 #include <cstdint>
0022 #include <type_traits>
0023 #include <utility>
0024
0025 namespace llvm {
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template<typename Fn> class function_ref;
0038
0039 template <typename Ret, typename... Params>
0040 class LLVM_GSL_POINTER function_ref<Ret(Params...)> {
0041 Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
0042 intptr_t callable;
0043
0044 template<typename Callable>
0045 static Ret callback_fn(intptr_t callable, Params ...params) {
0046 return (*reinterpret_cast<Callable*>(callable))(
0047 std::forward<Params>(params)...);
0048 }
0049
0050 public:
0051 function_ref() = default;
0052 function_ref(std::nullptr_t) {}
0053
0054 template <typename Callable>
0055 function_ref(
0056 Callable &&callable LLVM_LIFETIME_BOUND,
0057
0058 std::enable_if_t<!std::is_same<remove_cvref_t<Callable>,
0059 function_ref>::value> * = nullptr,
0060
0061 std::enable_if_t<std::is_void<Ret>::value ||
0062 std::is_convertible<decltype(std::declval<Callable>()(
0063 std::declval<Params>()...)),
0064 Ret>::value> * = nullptr)
0065 : callback(callback_fn<std::remove_reference_t<Callable>>),
0066 callable(reinterpret_cast<intptr_t>(&callable)) {}
0067
0068 Ret operator()(Params ...params) const {
0069 return callback(callable, std::forward<Params>(params)...);
0070 }
0071
0072 explicit operator bool() const { return callback; }
0073
0074 bool operator==(const function_ref<Ret(Params...)> &Other) const {
0075 return callable == Other.callable;
0076 }
0077 };
0078
0079 }
0080
0081 #endif