Skip to content

Commit

Permalink
[libc++][ranges] Implement rbegin, rend, crbegin and crend.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D119057
  • Loading branch information
var-const committed Feb 14, 2022
1 parent 01e0486 commit 55bd22f
Show file tree
Hide file tree
Showing 12 changed files with 1,379 additions and 6 deletions.
10 changes: 5 additions & 5 deletions libcxx/docs/Status/RangesPaper.csv
Expand Up @@ -100,15 +100,15 @@ Section,Description,Dependencies,Assignee,Complete
| `ranges::end <https://llvm.org/D100255>`_
| `range::cbegin <https://llvm.org/D100255>`_
| `ranges::cend <https://llvm.org/D100255>`_
| ranges::rbegin
| ranges::rend
| ranges::crbegin
| ranges::crend
| `ranges::rbegin <https://llvm.org/D119057>`_
| `ranges::rend <https://llvm.org/D119057>`_
| `ranges::crbegin <https://llvm.org/D119057>`_
| `ranges::crend <https://llvm.org/D119057>`_
| `ranges::size <https://llvm.org/D101079>`_
| `ranges::ssize <https://llvm.org/D101189>`_
| `ranges::empty <https://llvm.org/D101193>`_
| `ranges::data <https://llvm.org/D101476>`_
| `ranges::cdata <https://llvm.org/D117044>`_",[iterator.concepts],Christopher Di Bella and Zoe Carver,In progress
| `ranges::cdata <https://llvm.org/D117044>`_",[iterator.concepts],Various,✅
`[range.range] <https://wg21.link/range.range>`_,"| `ranges::range <https://llvm.org/D100269>`_
| `ranges::borrowed_range <https://llvm.org/D102426>`_
| `ranges::enable_borrowed_range <https://llvm.org/D90999>`_
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -346,7 +346,9 @@ set(files
__ranges/non_propagating_cache.h
__ranges/owning_view.h
__ranges/range_adaptor.h
__ranges/rbegin.h
__ranges/ref_view.h
__ranges/rend.h
__ranges/reverse_view.h
__ranges/single_view.h
__ranges/size.h
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__concepts/class_or_enum.h
Expand Up @@ -26,6 +26,7 @@ template<class _Tp>
concept __class_or_enum = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;

// Work around Clang bug https://llvm.org/PR52970
// TODO: remove this workaround once libc++ no longer has to support Clang 13 (it was fixed in Clang 14).
template<class _Tp>
concept __workaround_52970 = is_class_v<__uncvref_t<_Tp>> || is_union_v<__uncvref_t<_Tp>>;

Expand Down
1 change: 0 additions & 1 deletion libcxx/include/__ranges/access.h
Expand Up @@ -15,7 +15,6 @@
#include <__iterator/readable_traits.h>
#include <__ranges/enable_borrowed_range.h>
#include <__utility/auto_cast.h>
#include <concepts>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down
130 changes: 130 additions & 0 deletions libcxx/include/__ranges/rbegin.h
@@ -0,0 +1,130 @@
// -*- 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 _LIBCPP___RANGES_RBEGIN_H
#define _LIBCPP___RANGES_RBEGIN_H

#include <__concepts/class_or_enum.h>
#include <__concepts/same_as.h>
#include <__config>
#include <__iterator/concepts.h>
#include <__iterator/readable_traits.h>
#include <__iterator/reverse_iterator.h>
#include <__ranges/access.h>
#include <__utility/auto_cast.h>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if !defined(_LIBCPP_HAS_NO_CONCEPTS)

// [ranges.access.rbegin]

namespace ranges {
namespace __rbegin {
template <class _Tp>
concept __member_rbegin =
__can_borrow<_Tp> &&
__workaround_52970<_Tp> &&
requires(_Tp&& __t) {
{ _LIBCPP_AUTO_CAST(__t.rbegin()) } -> input_or_output_iterator;
};

void rbegin(auto&) = delete;
void rbegin(const auto&) = delete;

template <class _Tp>
concept __unqualified_rbegin =
!__member_rbegin<_Tp> &&
__can_borrow<_Tp> &&
__class_or_enum<remove_cvref_t<_Tp>> &&
requires(_Tp&& __t) {
{ _LIBCPP_AUTO_CAST(rbegin(__t)) } -> input_or_output_iterator;
};

template <class _Tp>
concept __can_reverse =
__can_borrow<_Tp> &&
!__member_rbegin<_Tp> &&
!__unqualified_rbegin<_Tp> &&
requires(_Tp&& __t) {
{ ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
{ ranges::begin(__t) } -> bidirectional_iterator;
};

struct __fn {
template <class _Tp>
requires __member_rbegin<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rbegin())))
{
return _LIBCPP_AUTO_CAST(__t.rbegin());
}

template <class _Tp>
requires __unqualified_rbegin<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(_LIBCPP_AUTO_CAST(rbegin(__t))))
{
return _LIBCPP_AUTO_CAST(rbegin(__t));
}

template <class _Tp>
requires __can_reverse<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::end(__t)))
{
return std::make_reverse_iterator(ranges::end(__t));
}

void operator()(auto&&) const = delete;
};
} // namespace __rbegin

inline namespace __cpo {
inline constexpr auto rbegin = __rbegin::__fn{};
} // namespace __cpo
} // namespace ranges

// [range.access.crbegin]

namespace ranges {
namespace __crbegin {
struct __fn {
template <class _Tp>
requires is_lvalue_reference_v<_Tp&&>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t))))
-> decltype( ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t)))
{ return ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t)); }

template <class _Tp>
requires is_rvalue_reference_v<_Tp&&>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::rbegin(static_cast<const _Tp&&>(__t))))
-> decltype( ranges::rbegin(static_cast<const _Tp&&>(__t)))
{ return ranges::rbegin(static_cast<const _Tp&&>(__t)); }
};
} // namespace __crbegin

inline namespace __cpo {
inline constexpr auto crbegin = __crbegin::__fn{};
} // namespace __cpo
} // namespace ranges

#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___RANGES_RBEGIN_H
134 changes: 134 additions & 0 deletions libcxx/include/__ranges/rend.h
@@ -0,0 +1,134 @@
// -*- 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 _LIBCPP___RANGES_REND_H
#define _LIBCPP___RANGES_REND_H

#include <__concepts/class_or_enum.h>
#include <__concepts/same_as.h>
#include <__config>
#include <__iterator/concepts.h>
#include <__iterator/readable_traits.h>
#include <__iterator/reverse_iterator.h>
#include <__ranges/access.h>
#include <__ranges/rbegin.h>
#include <__utility/auto_cast.h>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if !defined(_LIBCPP_HAS_NO_CONCEPTS)

// [range.access.rend]

namespace ranges {
namespace __rend {
template <class _Tp>
concept __member_rend =
__can_borrow<_Tp> &&
__workaround_52970<_Tp> &&
requires(_Tp&& __t) {
ranges::rbegin(__t);
{ _LIBCPP_AUTO_CAST(__t.rend()) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
};

void rend(auto&) = delete;
void rend(const auto&) = delete;

template <class _Tp>
concept __unqualified_rend =
!__member_rend<_Tp> &&
__can_borrow<_Tp> &&
__class_or_enum<remove_cvref_t<_Tp>> &&
requires(_Tp&& __t) {
ranges::rbegin(__t);
{ _LIBCPP_AUTO_CAST(rend(__t)) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
};

template <class _Tp>
concept __can_reverse =
__can_borrow<_Tp> &&
!__member_rend<_Tp> &&
!__unqualified_rend<_Tp> &&
requires(_Tp&& __t) {
{ ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
{ ranges::begin(__t) } -> bidirectional_iterator;
};

class __fn {
public:
template <class _Tp>
requires __member_rend<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rend())))
{
return _LIBCPP_AUTO_CAST(__t.rend());
}

template <class _Tp>
requires __unqualified_rend<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(_LIBCPP_AUTO_CAST(rend(__t))))
{
return _LIBCPP_AUTO_CAST(rend(__t));
}

template <class _Tp>
requires __can_reverse<_Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::begin(__t)))
{
return std::make_reverse_iterator(ranges::begin(__t));
}

void operator()(auto&&) const = delete;
};
} // namespace __rend

inline namespace __cpo {
inline constexpr auto rend = __rend::__fn{};
} // namespace __cpo
} // namespace ranges

// [range.access.crend]

namespace ranges {
namespace __crend {
struct __fn {
template <class _Tp>
requires is_lvalue_reference_v<_Tp&&>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))))
-> decltype( ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)))
{ return ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)); }

template <class _Tp>
requires is_rvalue_reference_v<_Tp&&>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(ranges::rend(static_cast<const _Tp&&>(__t))))
-> decltype( ranges::rend(static_cast<const _Tp&&>(__t)))
{ return ranges::rend(static_cast<const _Tp&&>(__t)); }
};
} // namespace __crend

inline namespace __cpo {
inline constexpr auto crend = __crend::__fn{};
} // namespace __cpo
} // namespace ranges

#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___RANGES_REND_H
2 changes: 2 additions & 0 deletions libcxx/include/module.modulemap
Expand Up @@ -815,7 +815,9 @@ module std [system] {
module non_propagating_cache { private header "__ranges/non_propagating_cache.h" }
module owning_view { private header "__ranges/owning_view.h" }
module range_adaptor { private header "__ranges/range_adaptor.h" }
module rbegin { private header "__ranges/rbegin.h" }
module ref_view { private header "__ranges/ref_view.h" }
module rend { private header "__ranges/rend.h" }
module reverse_view { private header "__ranges/reverse_view.h" }
module single_view { private header "__ranges/single_view.h" }
module size { private header "__ranges/size.h" }
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/ranges
Expand Up @@ -250,7 +250,9 @@ namespace std {
#include <__ranges/enable_view.h>
#include <__ranges/iota_view.h>
#include <__ranges/join_view.h>
#include <__ranges/rbegin.h>
#include <__ranges/ref_view.h>
#include <__ranges/rend.h>
#include <__ranges/reverse_view.h>
#include <__ranges/single_view.h>
#include <__ranges/size.h>
Expand Down
@@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// REQUIRES: modules-build

// WARNING: This test was generated by 'generate_private_header_tests.py'
// and should not be edited manually.

// expected-error@*:* {{use of private header from outside its module: '__ranges/rbegin.h'}}
#include <__ranges/rbegin.h>
@@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// REQUIRES: modules-build

// WARNING: This test was generated by 'generate_private_header_tests.py'
// and should not be edited manually.

// expected-error@*:* {{use of private header from outside its module: '__ranges/rend.h'}}
#include <__ranges/rend.h>

0 comments on commit 55bd22f

Please sign in to comment.