2021-06-17 11:30:11 -04: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_UNWRAP_ITER_H
|
|
|
|
#define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
|
|
|
|
|
|
|
|
#include <__config>
|
2022-06-10 19:53:10 +02:00
|
|
|
#include <__iterator/iterator_traits.h>
|
2021-06-17 11:30:11 -04:00
|
|
|
#include <__memory/pointer_traits.h>
|
2022-12-21 00:07:17 +01:00
|
|
|
#include <__type_traits/enable_if.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>
|
2022-12-21 00:07:17 +01:00
|
|
|
#include <__utility/declval.h>
|
2022-07-12 15:29:07 +02:00
|
|
|
#include <__utility/move.h>
|
2021-06-17 11:30:11 -04:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2021-06-17 11:30:11 -04:00
|
|
|
#endif
|
|
|
|
|
2023-07-06 17:12:05 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2021-06-17 11:30:11 -04:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
// TODO: Change the name of __unwrap_iter_impl to something more appropriate
|
|
|
|
// The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
|
|
|
|
// to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
|
2021-06-17 11:30:11 -04:00
|
|
|
//
|
|
|
|
// Some algorithms (e.g. std::copy, but not std::sort) need to convert an
|
2022-07-12 15:29:07 +02:00
|
|
|
// "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
|
2021-06-17 11:30:11 -04:00
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
// Default case - we can't unwrap anything
|
2023-05-17 10:34:51 -07:00
|
|
|
template <class _Iter, bool = __libcpp_is_contiguous_iterator<_Iter>::value>
|
2021-06-17 11:30:11 -04:00
|
|
|
struct __unwrap_iter_impl {
|
2022-07-12 15:29:07 +02:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
|
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
|
2021-06-17 11:30:11 -04:00
|
|
|
};
|
|
|
|
|
2023-06-29 14:49:45 -07:00
|
|
|
// TODO(hardening): make sure that the following unwrapping doesn't unexpectedly turn hardened iterators into raw
|
|
|
|
// pointers.
|
2021-06-17 11:30:11 -04:00
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
// It's a contiguous iterator, so we can use a raw pointer instead
|
2021-06-17 11:30:11 -04:00
|
|
|
template <class _Iter>
|
|
|
|
struct __unwrap_iter_impl<_Iter, true> {
|
2022-07-12 15:29:07 +02:00
|
|
|
using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>()));
|
2021-06-17 11:30:11 -04:00
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) {
|
|
|
|
return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter));
|
2022-06-17 15:59:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
|
|
|
|
return std::__to_address(__i);
|
2022-06-17 15:59:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
template <class _Iter,
|
|
|
|
class _Impl = __unwrap_iter_impl<_Iter>,
|
|
|
|
__enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
|
2022-08-19 13:08:01 +02:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 decltype(_Impl::__unwrap(std::declval<_Iter>()))
|
2022-07-12 15:29:07 +02:00
|
|
|
__unwrap_iter(_Iter __i) _NOEXCEPT {
|
|
|
|
return _Impl::__unwrap(__i);
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
|
|
|
|
2022-12-07 18:51:28 +01:00
|
|
|
// Allow input_iterators to be passed to __unwrap_iter (but not __rewrap_iter)
|
|
|
|
#if _LIBCPP_STD_VER >= 20
|
|
|
|
template <class _Iter, __enable_if_t<!is_copy_constructible<_Iter>::value, int> = 0>
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI constexpr _Iter __unwrap_iter(_Iter __i) noexcept {
|
|
|
|
return __i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-12 15:29:07 +02:00
|
|
|
template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
|
|
|
|
return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_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-07-06 17:12:05 +00:00
|
|
|
|
2021-06-17 11:30:11 -04:00
|
|
|
#endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H
|