mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-06 14:41:02 +00:00
Release Cosmopolitan v3.6.0
This release is an atomic upgrade to GCC 14.1.0 with C23 and C++23
This commit is contained in:
parent
62ace3623a
commit
5660ec4741
1585 changed files with 117353 additions and 271644 deletions
88
third_party/libcxx/__iterator/bounded_iter.h
vendored
88
third_party/libcxx/__iterator/bounded_iter.h
vendored
|
@ -23,18 +23,28 @@
|
|||
# pragma GCC system_header
|
||||
#endif
|
||||
|
||||
_LIBCPP_PUSH_MACROS
|
||||
#include <__undef_macros>
|
||||
|
||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||
|
||||
// Iterator wrapper that carries the valid range it is allowed to access.
|
||||
//
|
||||
// This is a simple iterator wrapper for contiguous iterators that points
|
||||
// within a [begin, end) range and carries these bounds with it. The iterator
|
||||
// ensures that it is pointing within that [begin, end) range when it is
|
||||
// dereferenced.
|
||||
// within a [begin, end] range and carries these bounds with it. The iterator
|
||||
// ensures that it is pointing within [begin, end) range when it is
|
||||
// dereferenced. It also ensures that it is never iterated outside of
|
||||
// [begin, end]. This is important for two reasons:
|
||||
//
|
||||
// Arithmetic operations are allowed and the bounds of the resulting iterator
|
||||
// are not checked. Hence, it is possible to create an iterator pointing outside
|
||||
// its range, but it is not possible to dereference it.
|
||||
// 1. It allows `operator*` and `operator++` bounds checks to be `iter != end`.
|
||||
// This is both less for the optimizer to prove, and aligns with how callers
|
||||
// typically use iterators.
|
||||
//
|
||||
// 2. Advancing an iterator out of bounds is undefined behavior (see the table
|
||||
// in [input.iterators]). In particular, when the underlying iterator is a
|
||||
// pointer, it is undefined at the language level (see [expr.add]). If
|
||||
// bounded iterators exhibited this undefined behavior, we risk compiler
|
||||
// optimizations deleting non-redundant bounds checks.
|
||||
template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
|
||||
struct __bounded_iter {
|
||||
using value_type = typename iterator_traits<_Iterator>::value_type;
|
||||
|
@ -48,14 +58,14 @@ struct __bounded_iter {
|
|||
|
||||
// Create a singular iterator.
|
||||
//
|
||||
// Such an iterator does not point to any object and is conceptually out of bounds, so it is
|
||||
// not dereferenceable. Observing operations like comparison and assignment are valid.
|
||||
// Such an iterator points past the end of an empty span, so it is not dereferenceable.
|
||||
// Observing operations like comparison and assignment are valid.
|
||||
_LIBCPP_HIDE_FROM_ABI __bounded_iter() = default;
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;
|
||||
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;
|
||||
|
||||
template <class _OtherIterator, class = __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value > >
|
||||
template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
|
||||
: __current_(__other.__current_),
|
||||
__begin_(__other.__begin_),
|
||||
|
@ -67,18 +77,20 @@ struct __bounded_iter {
|
|||
|
||||
private:
|
||||
// Create an iterator wrapping the given iterator, and whose bounds are described
|
||||
// by the provided [begin, end) range.
|
||||
// by the provided [begin, end] range.
|
||||
//
|
||||
// This constructor does not check whether the resulting iterator is within its bounds.
|
||||
// However, it does check that the provided [begin, end) range is a valid range (that
|
||||
// is, begin <= end).
|
||||
// The constructor does not check whether the resulting iterator is within its bounds. It is a
|
||||
// responsibility of the container to ensure that the given bounds are valid.
|
||||
//
|
||||
// Since it is non-standard for iterators to have this constructor, __bounded_iter must
|
||||
// be created via `std::__make_bounded_iter`.
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __bounded_iter(
|
||||
_Iterator __current, _Iterator __begin, _Iterator __end)
|
||||
_LIBCPP_HIDE_FROM_ABI
|
||||
_LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __bounded_iter(_Iterator __current, _Iterator __begin, _Iterator __end)
|
||||
: __current_(__current), __begin_(__begin), __end_(__end) {
|
||||
_LIBCPP_ASSERT(__begin <= __end, "__bounded_iter(current, begin, end): [begin, end) is not a valid range");
|
||||
_LIBCPP_ASSERT_INTERNAL(
|
||||
__begin <= __current, "__bounded_iter(current, begin, end): current and begin are inconsistent");
|
||||
_LIBCPP_ASSERT_INTERNAL(
|
||||
__current <= __end, "__bounded_iter(current, begin, end): current and end are inconsistent");
|
||||
}
|
||||
|
||||
template <class _It>
|
||||
|
@ -87,30 +99,37 @@ private:
|
|||
public:
|
||||
// Dereference and indexing operations.
|
||||
//
|
||||
// These operations check that the iterator is dereferenceable, that is within [begin, end).
|
||||
// These operations check that the iterator is dereferenceable. Since the class invariant is
|
||||
// that the iterator is always within `[begin, end]`, we only need to check it's not pointing to
|
||||
// `end`. This is easier for the optimizer because it aligns with the `iter != container.end()`
|
||||
// checks that typical callers already use (see
|
||||
// https://github.com/llvm/llvm-project/issues/78829).
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
|
||||
_LIBCPP_ASSERT(
|
||||
__in_bounds(__current_), "__bounded_iter::operator*: Attempt to dereference an out-of-range iterator");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__current_ != __end_, "__bounded_iter::operator*: Attempt to dereference an iterator at the end");
|
||||
return *__current_;
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
|
||||
_LIBCPP_ASSERT(
|
||||
__in_bounds(__current_), "__bounded_iter::operator->: Attempt to dereference an out-of-range iterator");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__current_ != __end_, "__bounded_iter::operator->: Attempt to dereference an iterator at the end");
|
||||
return std::__to_address(__current_);
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
|
||||
_LIBCPP_ASSERT(
|
||||
__in_bounds(__current_ + __n), "__bounded_iter::operator[]: Attempt to index an iterator out-of-range");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n >= __begin_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator past the start");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n < __end_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator at or past the end");
|
||||
return __current_[__n];
|
||||
}
|
||||
|
||||
// Arithmetic operations.
|
||||
//
|
||||
// These operations do not check that the resulting iterator is within the bounds, since that
|
||||
// would make it impossible to create a past-the-end iterator.
|
||||
// These operations check that the iterator remains within `[begin, end]`.
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator++() _NOEXCEPT {
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__current_ != __end_, "__bounded_iter::operator++: Attempt to advance an iterator past the end");
|
||||
++__current_;
|
||||
return *this;
|
||||
}
|
||||
|
@ -121,6 +140,8 @@ public:
|
|||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator--() _NOEXCEPT {
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__current_ != __begin_, "__bounded_iter::operator--: Attempt to rewind an iterator past the start");
|
||||
--__current_;
|
||||
return *this;
|
||||
}
|
||||
|
@ -131,6 +152,10 @@ public:
|
|||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n >= __begin_ - __current_, "__bounded_iter::operator+=: Attempt to rewind an iterator past the start");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n <= __end_ - __current_, "__bounded_iter::operator+=: Attempt to advance an iterator past the end");
|
||||
__current_ += __n;
|
||||
return *this;
|
||||
}
|
||||
|
@ -148,6 +173,10 @@ public:
|
|||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n <= __current_ - __begin_, "__bounded_iter::operator-=: Attempt to rewind an iterator past the start");
|
||||
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
|
||||
__n >= __current_ - __end_, "__bounded_iter::operator-=: Attempt to advance an iterator past the end");
|
||||
__current_ -= __n;
|
||||
return *this;
|
||||
}
|
||||
|
@ -194,15 +223,10 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
// Return whether the given iterator is in the bounds of this __bounded_iter.
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Iterator const& __iter) const {
|
||||
return __iter >= __begin_ && __iter < __end_;
|
||||
}
|
||||
|
||||
template <class>
|
||||
friend struct pointer_traits;
|
||||
_Iterator __current_; // current iterator
|
||||
_Iterator __begin_, __end_; // valid range represented as [begin, end)
|
||||
_Iterator __begin_, __end_; // valid range represented as [begin, end]
|
||||
};
|
||||
|
||||
template <class _It>
|
||||
|
@ -228,4 +252,6 @@ struct pointer_traits<__bounded_iter<_Iterator> > {
|
|||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
_LIBCPP_POP_MACROS
|
||||
|
||||
#endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue