2023-01-13 16:56:58 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H
|
|
|
|
#define _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H
|
|
|
|
|
|
|
|
#include <__algorithm/unwrap_iter.h>
|
|
|
|
#include <__algorithm/unwrap_range.h>
|
|
|
|
#include <__config>
|
2024-10-31 02:20:10 +01:00
|
|
|
#include <__cstddef/size_t.h>
|
2023-01-13 16:56:58 -08:00
|
|
|
#include <__iterator/iterator_traits.h>
|
|
|
|
#include <__memory/pointer_traits.h>
|
2023-06-30 13:47:39 -07:00
|
|
|
#include <__string/constexpr_c_functions.h>
|
2023-01-13 16:56:58 -08:00
|
|
|
#include <__type_traits/enable_if.h>
|
|
|
|
#include <__type_traits/is_always_bitcastable.h>
|
|
|
|
#include <__type_traits/is_constant_evaluated.h>
|
[libc++][NFC] Merge is{,_nothrow,_trivially}{,_copy,_move,_default}{_assignable,_constructible} (#85308)
These headers have become very small by using compiler builtins, often
containing only two declarations. This merges these headers, since
there doesn't seem to be much of a benefit keeping them separate.
Specifically, `is_{,_nothrow,_trivially}{assignable,constructible}` are
kept and the `copy`, `move` and `default` versions of these type traits
are moved in to the respective headers.
2024-03-18 08:29:44 +01:00
|
|
|
#include <__type_traits/is_constructible.h>
|
2023-01-13 16:56:58 -08:00
|
|
|
#include <__type_traits/is_trivially_assignable.h>
|
|
|
|
#include <__type_traits/is_volatile.h>
|
|
|
|
#include <__utility/move.h>
|
|
|
|
#include <__utility/pair.h>
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
|
|
# pragma GCC system_header
|
|
|
|
#endif
|
|
|
|
|
[libc++] Fix missing and incorrect push/pop macros (#79204)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
2024-01-25 15:48:46 -05:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2023-01-13 16:56:58 -08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
// Type traits.
|
|
|
|
|
|
|
|
template <class _From, class _To>
|
|
|
|
struct __can_lower_copy_assignment_to_memmove {
|
|
|
|
static const bool value =
|
|
|
|
// If the types are always bitcastable, it's valid to do a bitwise copy between them.
|
|
|
|
__is_always_bitcastable<_From, _To>::value &&
|
|
|
|
// Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays).
|
|
|
|
is_trivially_assignable<_To&, const _From&>::value &&
|
|
|
|
// `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case.
|
|
|
|
!is_volatile<_From>::value && !is_volatile<_To>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _From, class _To>
|
|
|
|
struct __can_lower_move_assignment_to_memmove {
|
|
|
|
static const bool value =
|
|
|
|
__is_always_bitcastable<_From, _To>::value && is_trivially_assignable<_To&, _From&&>::value &&
|
|
|
|
!is_volatile<_From>::value && !is_volatile<_To>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// `memmove` algorithms implementation.
|
|
|
|
|
|
|
|
template <class _In, class _Out>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
|
|
|
|
__copy_trivial_impl(_In* __first, _In* __last, _Out* __result) {
|
|
|
|
const size_t __n = static_cast<size_t>(__last - __first);
|
2023-06-30 13:47:39 -07:00
|
|
|
|
|
|
|
std::__constexpr_memmove(__result, __first, __element_count(__n));
|
2023-01-13 16:56:58 -08:00
|
|
|
|
|
|
|
return std::make_pair(__last, __result + __n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _In, class _Out>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
|
|
|
|
__copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) {
|
|
|
|
const size_t __n = static_cast<size_t>(__last - __first);
|
|
|
|
__result -= __n;
|
|
|
|
|
2023-06-30 13:47:39 -07:00
|
|
|
std::__constexpr_memmove(__result, __first, __element_count(__n));
|
2023-01-13 16:56:58 -08:00
|
|
|
|
|
|
|
return std::make_pair(__last, __result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator unwrapping and dispatching to the correct overload.
|
|
|
|
|
2024-03-27 16:54:50 +01:00
|
|
|
template <class _InIter, class _OutIter>
|
|
|
|
struct __can_rewrap
|
|
|
|
: integral_constant<bool, is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value> {};
|
2023-01-13 16:56:58 -08:00
|
|
|
|
|
|
|
template <class _Algorithm,
|
|
|
|
class _InIter,
|
|
|
|
class _Sent,
|
|
|
|
class _OutIter,
|
2024-03-27 16:54:50 +01:00
|
|
|
__enable_if_t<__can_rewrap<_InIter, _OutIter>::value, int> = 0>
|
2023-01-13 16:56:58 -08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
|
2024-03-27 16:54:50 +01:00
|
|
|
__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {
|
2023-01-13 16:56:58 -08:00
|
|
|
auto __range = std::__unwrap_range(__first, std::move(__last));
|
|
|
|
auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first));
|
|
|
|
return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)),
|
|
|
|
std::__rewrap_iter(std::move(__out_first), std::move(__result.second)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Algorithm,
|
|
|
|
class _InIter,
|
|
|
|
class _Sent,
|
|
|
|
class _OutIter,
|
2024-03-27 16:54:50 +01:00
|
|
|
__enable_if_t<!__can_rewrap<_InIter, _OutIter>::value, int> = 0>
|
2023-01-13 16:56:58 -08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
|
2024-03-27 16:54:50 +01:00
|
|
|
__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {
|
2023-01-13 16:56:58 -08:00
|
|
|
return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first));
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
[libc++] Fix missing and incorrect push/pop macros (#79204)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
2024-01-25 15:48:46 -05:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2023-01-13 16:56:58 -08:00
|
|
|
#endif // _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H
|