-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc] Add invoke #65753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[libc] Add invoke #65753
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//===-- invoke type_traits --------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_H | ||
#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_H | ||
|
||
#include "src/__support/CPP/type_traits/decay.h" | ||
#include "src/__support/CPP/type_traits/invoke_result.h" | ||
#include "src/__support/CPP/type_traits/is_base_of.h" | ||
#include "src/__support/CPP/type_traits/is_function.h" | ||
#include "src/__support/CPP/type_traits/is_member_pointer.h" | ||
#include "src/__support/CPP/type_traits/is_object.h" | ||
#include "src/__support/CPP/utility/forward.h" | ||
|
||
// BEWARE : this implementation is not fully conformant as it doesn't take | ||
// `cpp::reference_wrapper` into account. It also bypasses noexcept detection. | ||
|
||
namespace __llvm_libc::cpp { | ||
|
||
// invoke | ||
namespace detail { | ||
template <class> constexpr bool is_reference_wrapper_v = false; | ||
|
||
// Disable specialization on `cpp::reference_wrapper` as it is not yet | ||
// implemented. | ||
|
||
// template <class U> | ||
// constexpr bool is_reference_wrapper_v<cpp::reference_wrapper<U>> = true; | ||
|
||
template <class C, class Pointed, class T1, class... Args> | ||
constexpr decltype(auto) invoke_memptr(Pointed C::*f, T1 &&t1, Args &&...args) { | ||
if constexpr (cpp::is_function_v<Pointed>) { | ||
if constexpr (cpp::is_base_of_v<C, cpp::decay_t<T1>>) | ||
return (cpp::forward<T1>(t1).*f)(cpp::forward<Args>(args)...); | ||
else if constexpr (is_reference_wrapper_v<cpp::decay_t<T1>>) | ||
return (t1.get().*f)(cpp::forward<Args>(args)...); | ||
else | ||
return ((*cpp::forward<T1>(t1)).*f)(cpp::forward<Args>(args)...); | ||
} else { | ||
static_assert(cpp::is_object_v<Pointed> && sizeof...(args) == 0); | ||
if constexpr (cpp::is_base_of_v<C, cpp::decay_t<T1>>) | ||
return cpp::forward<T1>(t1).*f; | ||
else if constexpr (is_reference_wrapper_v<cpp::decay_t<T1>>) | ||
return t1.get().*f; | ||
else | ||
return (*cpp::forward<T1>(t1)).*f; | ||
} | ||
} | ||
} // namespace detail | ||
|
||
template <class F, class... Args> | ||
constexpr cpp::invoke_result_t<F, Args...> invoke(F &&f, Args &&...args) { | ||
if constexpr (cpp::is_member_pointer_v<cpp::decay_t<F>>) | ||
return detail::invoke_memptr(f, cpp::forward<Args>(args)...); | ||
else | ||
return cpp::forward<F>(f)(cpp::forward<Args>(args)...); | ||
} | ||
} // namespace __llvm_libc::cpp | ||
|
||
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//===-- invoke_result type_traits -------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_RESULT_H | ||
#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_RESULT_H | ||
|
||
#include "src/__support/CPP/type_traits/decay.h" | ||
#include "src/__support/CPP/type_traits/enable_if.h" | ||
#include "src/__support/CPP/type_traits/false_type.h" | ||
#include "src/__support/CPP/type_traits/is_base_of.h" | ||
#include "src/__support/CPP/type_traits/is_function.h" | ||
#include "src/__support/CPP/type_traits/true_type.h" | ||
#include "src/__support/CPP/utility/declval.h" | ||
#include "src/__support/CPP/utility/forward.h" | ||
|
||
// BEWARE : this implementation is not fully conformant as it doesn't take | ||
// `cpp::reference_wrapper` into account. | ||
|
||
namespace __llvm_libc::cpp { | ||
|
||
// invoke_result | ||
|
||
namespace detail { | ||
template <class T> struct is_reference_wrapper : cpp::false_type {}; | ||
|
||
// Disable specialization on `cpp::reference_wrapper` as it is not yet | ||
// implemented. | ||
|
||
// template <class U> struct | ||
// is_reference_wrapper<cpp::reference_wrapper<U>> : cpp::true_type {}; | ||
|
||
template <class T> struct invoke_impl { | ||
template <class F, class... Args> | ||
static auto call(F &&f, Args &&...args) | ||
-> decltype(cpp::forward<F>(f)(cpp::forward<Args>(args)...)); | ||
}; | ||
|
||
template <class B, class MT> struct invoke_impl<MT B::*> { | ||
template <class T, class Td = cpp::decay_t<T>, | ||
class = cpp::enable_if_t<cpp::is_base_of_v<B, Td>>> | ||
static auto get(T &&t) -> T &&; | ||
|
||
template <class T, class Td = cpp::decay_t<T>, | ||
class = cpp::enable_if_t<is_reference_wrapper<Td>::value>> | ||
static auto get(T &&t) -> decltype(t.get()); | ||
|
||
template <class T, class Td = cpp::decay_t<T>, | ||
class = cpp::enable_if_t<!cpp::is_base_of_v<B, Td>>, | ||
class = cpp::enable_if_t<!is_reference_wrapper<Td>::value>> | ||
static auto get(T &&t) -> decltype(*cpp::forward<T>(t)); | ||
|
||
template <class T, class... Args, class MT1, | ||
class = cpp::enable_if_t<cpp::is_function_v<MT1>>> | ||
static auto call(MT1 B::*pmf, T &&t, Args &&...args) | ||
-> decltype((invoke_impl::get(cpp::forward<T>(t)).* | ||
pmf)(cpp::forward<Args>(args)...)); | ||
|
||
template <class T> | ||
static auto call(MT B::*pmd, T &&t) | ||
-> decltype(invoke_impl::get(cpp::forward<T>(t)).*pmd); | ||
}; | ||
|
||
template <class F, class... Args, class Fd = typename cpp::decay_t<F>> | ||
auto INVOKE(F &&f, Args &&...args) | ||
-> decltype(invoke_impl<Fd>::call(cpp::forward<F>(f), | ||
cpp::forward<Args>(args)...)); | ||
|
||
template <typename AlwaysVoid, typename, typename...> struct invoke_result {}; | ||
template <typename F, typename... Args> | ||
struct invoke_result<decltype(void(detail::INVOKE(cpp::declval<F>(), | ||
cpp::declval<Args>()...))), | ||
F, Args...> { | ||
using type = | ||
decltype(detail::INVOKE(cpp::declval<F>(), cpp::declval<Args>()...)); | ||
}; | ||
} // namespace detail | ||
|
||
template <class F, class... ArgTypes> | ||
struct invoke_result : detail::invoke_result<void, F, ArgTypes...> {}; | ||
|
||
template <class F, class... ArgTypes> | ||
using invoke_result_t = typename invoke_result<F, ArgTypes...>::type; | ||
|
||
} // namespace __llvm_libc::cpp | ||
|
||
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_INVOKE_RESULT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===-- is_object type_traits -----------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H | ||
#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H | ||
|
||
#include "src/__support/CPP/type_traits/bool_constant.h" | ||
#include "src/__support/CPP/type_traits/is_array.h" | ||
#include "src/__support/CPP/type_traits/is_class.h" | ||
#include "src/__support/CPP/type_traits/is_scalar.h" | ||
#include "src/__support/CPP/type_traits/is_union.h" | ||
#include "src/__support/macros/attributes.h" | ||
|
||
namespace __llvm_libc::cpp { | ||
|
||
// is_object | ||
template <class T> | ||
struct is_object | ||
: cpp::bool_constant<cpp::is_scalar_v<T> || cpp::is_array_v<T> || | ||
cpp::is_union_v<T> || cpp::is_class_v<T>> {}; | ||
template <class T> | ||
LIBC_INLINE_VAR constexpr bool is_object_v = is_object<T>::value; | ||
|
||
} // namespace __llvm_libc::cpp | ||
|
||
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this all caps?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a concept defined by the standard "23.14.3 [func.require]".
INVOKE
(all caps) serves as a foundation for definingstd::invoke
andstd::invoke_result
.