[libcxx-commits] [libcxx] 9db55b3 - [libcxx][ranges] Add ranges::data CPO.
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri May 21 11:10:15 PDT 2021
Author: zoecarver
Date: 2021-05-21T11:07:23-07:00
New Revision: 9db55b314b5b9e387ed36fe61b829efcc98b7c7b
URL: https://github.com/llvm/llvm-project/commit/9db55b314b5b9e387ed36fe61b829efcc98b7c7b
DIFF: https://github.com/llvm/llvm-project/commit/9db55b314b5b9e387ed36fe61b829efcc98b7c7b.diff
LOG: [libcxx][ranges] Add ranges::data CPO.
This is the second to last one! Based on D101396. Depends on D100255. Refs D101079 and D101193.
Differential Revision: https://reviews.llvm.org/D101476
Added:
libcxx/include/__ranges/data.h
libcxx/test/std/ranges/range.access/range.prim/data.incomplete.verify.cpp
libcxx/test/std/ranges/range.access/range.prim/data.pass.cpp
Modified:
libcxx/include/CMakeLists.txt
libcxx/include/ranges
Removed:
################################################################################
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d08d156a8a0a..4a58a2cfe5b7 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -37,6 +37,7 @@ set(files
__nullptr
__ranges/access.h
__ranges/concepts.h
+ __ranges/data.h
__ranges/empty.h
__ranges/enable_borrowed_range.h
__ranges/view.h
diff --git a/libcxx/include/__ranges/data.h b/libcxx/include/__ranges/data.h
new file mode 100644
index 000000000000..80546670dd00
--- /dev/null
+++ b/libcxx/include/__ranges/data.h
@@ -0,0 +1,83 @@
+// -*- C++ -*-
+//===------------------------ __ranges/data.h ------------------------------===//
+//
+// 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_DATA_H
+#define _LIBCPP___RANGES_DATA_H
+
+#include <__config>
+
+#include <concepts>
+#include <__iterator/concepts.h>
+#include <__iterator/iterator_traits.h>
+#include <__ranges/access.h>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if !defined(_LIBCPP_HAS_NO_RANGES)
+
+// clang-format off
+namespace ranges {
+// [range.prim.data]
+namespace __data {
+ template <class _Tp>
+ concept __ptr_to_object = is_pointer_v<_Tp> && is_object_v<remove_pointer_t<_Tp>>;
+
+ template <class _Tp>
+ concept __member_data =
+ requires(_Tp&& __t) {
+ { _VSTD::forward<_Tp>(__t) } -> __can_borrow;
+ { __t.data() } -> __ptr_to_object;
+ };
+
+ template <class _Tp>
+ concept __ranges_begin_invocable =
+ !__member_data<_Tp> &&
+ requires(_Tp&& __t) {
+ { _VSTD::forward<_Tp>(__t) } -> __can_borrow;
+ { ranges::begin(_VSTD::forward<_Tp>(__t)) } -> contiguous_iterator;
+ };
+
+ struct __fn {
+ template <__member_data _Tp>
+ requires __can_borrow<_Tp>
+ constexpr __ptr_to_object auto operator()(_Tp&& __t) const
+ noexcept(noexcept(__t.data())) {
+ return __t.data();
+ }
+
+ template<__ranges_begin_invocable _Tp>
+ requires __can_borrow<_Tp>
+ constexpr __ptr_to_object auto operator()(_Tp&& __t) const
+ noexcept(noexcept(_VSTD::to_address(ranges::begin(_VSTD::forward<_Tp>(__t))))) {
+ return _VSTD::to_address(ranges::begin(_VSTD::forward<_Tp>(__t)));
+ }
+ };
+} // end namespace __data
+
+inline namespace __cpo {
+ inline constexpr const auto data = __data::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+// clang-format off
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_DATA_H
diff --git a/libcxx/include/ranges b/libcxx/include/ranges
index da443d77a0df..fb140107ca1f 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -85,6 +85,7 @@ namespace std::ranges {
#include <__config>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
+#include <__ranges/data.h>
#include <__ranges/empty.h>
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/view.h>
diff --git a/libcxx/test/std/ranges/range.access/range.prim/data.incomplete.verify.cpp b/libcxx/test/std/ranges/range.access/range.prim/data.incomplete.verify.cpp
new file mode 100644
index 000000000000..f564750443f5
--- /dev/null
+++ b/libcxx/test/std/ranges/range.access/range.prim/data.incomplete.verify.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: clang-10
+// UNSUPPORTED: gcc-10
+
+// std::ranges::data
+
+#include <ranges>
+
+struct Incomplete;
+
+void f(Incomplete arr[]) {
+ // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f(Incomplete(&arr)[]) {
+ // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f(Incomplete(&&arr)[]) {
+ // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f2(Incomplete arr[2]) {
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f(Incomplete(&arr)[2]) {
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f(Incomplete(&&arr)[2]) {
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
+
+void f(Incomplete(&arr)[2][2]) {
+ // expected-error@*:* {{no matching function for call}}
+ std::ranges::data(arr);
+}
diff --git a/libcxx/test/std/ranges/range.access/range.prim/data.pass.cpp b/libcxx/test/std/ranges/range.access/range.prim/data.pass.cpp
new file mode 100644
index 000000000000..042bc4d5e52e
--- /dev/null
+++ b/libcxx/test/std/ranges/range.access/range.prim/data.pass.cpp
@@ -0,0 +1,179 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// std::ranges::data
+
+#include <ranges>
+
+#include <cassert>
+#include "test_macros.h"
+#include "test_iterators.h"
+
+using RangeDataT = decltype(std::ranges::data);
+
+static int globalBuff[2];
+
+struct Incomplete;
+
+static_assert(!std::is_invocable_v<RangeDataT, Incomplete[]>);
+static_assert(!std::is_invocable_v<RangeDataT, Incomplete[2]>);
+static_assert(!std::is_invocable_v<RangeDataT, Incomplete[2][2]>);
+static_assert(!std::is_invocable_v<RangeDataT, int [1]>);
+static_assert(!std::is_invocable_v<RangeDataT, int (&&)[1]>);
+static_assert( std::is_invocable_v<RangeDataT, int (&)[1]>);
+
+struct DataMember {
+ int x;
+ constexpr const int *data() const { return &x; }
+};
+
+static_assert( std::is_invocable_v<RangeDataT, DataMember &>);
+static_assert(!std::is_invocable_v<RangeDataT, DataMember &&>);
+
+struct VoidDataMember {
+ void *data() const;
+};
+static_assert(!std::is_invocable_v<RangeDataT, VoidDataMember const&>);
+
+struct Empty { };
+struct EmptyDataMember {
+ Empty data() const;
+};
+
+static_assert(!std::is_invocable_v<RangeDataT, EmptyDataMember const&>);
+
+struct EmptyPtrDataMember {
+ Empty x;
+ constexpr const Empty *data() const { return &x; }
+};
+
+struct PtrConvertible {
+ operator int*() const;
+};
+struct PtrConvertibleDataMember {
+ PtrConvertible data() const;
+};
+
+static_assert(!std::is_invocable_v<RangeDataT, PtrConvertibleDataMember const&>);
+
+struct NonConstDataMember {
+ int x;
+ constexpr int *data() { return &x; }
+};
+
+struct EnabledBorrowingDataMember {
+ constexpr int *data() { return &globalBuff[0]; }
+};
+
+template<>
+inline constexpr bool std::ranges::enable_borrowed_range<EnabledBorrowingDataMember> = true;
+
+struct DataMemberAndBegin {
+ int x;
+ constexpr const int *data() const { return &x; }
+ constexpr const int *begin() const { return &x; }
+};
+
+constexpr bool testDataMember() {
+ DataMember a;
+ assert(std::ranges::data(a) == &a.x);
+
+ NonConstDataMember b;
+ assert(std::ranges::data(b) == &b.x);
+
+ EnabledBorrowingDataMember c;
+ assert(std::ranges::data(std::move(c)) == &globalBuff[0]);
+
+ DataMemberAndBegin d;
+ assert(std::ranges::data(d) == &d.x);
+
+ return true;
+}
+
+using ContiguousIter = contiguous_iterator<const int*>;
+
+struct BeginMemberContiguousIterator {
+ int buff[8];
+
+ constexpr ContiguousIter begin() const { return ContiguousIter(buff); }
+};
+
+static_assert( std::is_invocable_v<RangeDataT, BeginMemberContiguousIterator &>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberContiguousIterator &&>);
+
+struct BeginMemberRandomAccess {
+ int buff[8];
+
+ random_access_iterator<const int*> begin() const;
+};
+
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberRandomAccess>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberRandomAccess const>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberRandomAccess const&>);
+
+struct BeginFriendContiguousIterator {
+ int buff[8];
+
+ constexpr friend ContiguousIter begin(const BeginFriendContiguousIterator &iter) {
+ return ContiguousIter(iter.buff);
+ }
+};
+
+struct BeginFriendRandomAccess {
+ friend random_access_iterator<const int*> begin(const BeginFriendRandomAccess iter);
+};
+
+static_assert(!std::is_invocable_v<RangeDataT, BeginFriendRandomAccess>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginFriendRandomAccess const>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginFriendRandomAccess const&>);
+
+struct BeginMemberRvalue {
+ int buff[8];
+
+ ContiguousIter begin() &&;
+};
+
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberRvalue&&>);
+static_assert(!std::is_invocable_v<RangeDataT, BeginMemberRvalue const&>);
+
+struct BeginMemberBorrowingEnabled {
+ constexpr contiguous_iterator<int*> begin() { return contiguous_iterator<int*>{&globalBuff[1]}; }
+};
+
+template<>
+inline constexpr bool std::ranges::enable_borrowed_range<BeginMemberBorrowingEnabled> = true;
+
+constexpr bool testViaRangesBegin() {
+ int arr[2];
+ assert(std::ranges::data(arr) == arr + 0);
+
+ BeginMemberContiguousIterator a;
+ assert(std::ranges::data(a) == a.buff);
+
+ const BeginFriendContiguousIterator b {};
+ assert(std::ranges::data(b) == b.buff);
+
+ BeginMemberBorrowingEnabled c;
+ assert(std::ranges::data(std::move(c)) == &globalBuff[1]);
+
+ return true;
+}
+
+int main(int, char**) {
+ testDataMember();
+ static_assert(testDataMember());
+
+ testViaRangesBegin();
+ static_assert(testViaRangesBegin());
+
+ return 0;
+}
More information about the libcxx-commits
mailing list