[libcxx][ranges] Add ranges::empty CPO.

Depends on D101079. Refs D101189.

Differential Revision: https://reviews.llvm.org/D101193

NOKEYCHECK=True
GitOrigin-RevId: e5d483f28a3af0972fc9b0df6073e4c14bb39359
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 9648d98..d08d156 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -37,6 +37,7 @@
   __nullptr
   __ranges/access.h
   __ranges/concepts.h
+  __ranges/empty.h
   __ranges/enable_borrowed_range.h
   __ranges/view.h
   __ranges/size.h
diff --git a/include/__ranges/empty.h b/include/__ranges/empty.h
new file mode 100644
index 0000000..65f36cd
--- /dev/null
+++ b/include/__ranges/empty.h
@@ -0,0 +1,83 @@
+// -*- 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_EMPTY_H
+#define _LIBCPP___RANGES_EMPTY_H
+
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__ranges/size.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.empty]
+namespace __empty {
+  template <class _Tp>
+  concept __member_empty = requires(_Tp&& __t) {
+    bool(_VSTD::forward<_Tp>(__t).empty());
+  };
+
+  template<class _Tp>
+  concept __can_invoke_size =
+    !__member_empty<_Tp> &&
+    requires(_Tp&& __t) { ranges::size(_VSTD::forward<_Tp>(__t)); };
+
+  template <class _Tp>
+  concept __can_compare_begin_end =
+    !__can_invoke_size<_Tp> &&
+    requires(_Tp&& __t) {
+      bool(ranges::begin(__t) == ranges::end(__t));
+      { ranges::begin(__t) } -> forward_iterator;
+    };
+
+  struct __fn {
+    template <__member_empty _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(bool(__t.empty()))) {
+      return __t.empty();
+    }
+
+    template <__can_invoke_size _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(ranges::size(_VSTD::forward<_Tp>(__t)))) {
+      return ranges::size(_VSTD::forward<_Tp>(__t)) == 0;
+    }
+
+    template<__can_compare_begin_end _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(bool(ranges::begin(__t) == ranges::end(__t)))) {
+      return ranges::begin(__t) == ranges::end(__t);
+    }
+  };
+}
+
+inline namespace __cpo {
+  inline constexpr auto empty = __empty::__fn{};
+} // namespace __cpo
+} // namespace ranges
+// clang-format off
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_EMPTY_H
diff --git a/include/ranges b/include/ranges
index c2b6bb3..5401add 100644
--- a/include/ranges
+++ b/include/ranges
@@ -78,6 +78,7 @@
 #include <__config>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
+#include <__ranges/empty.h>
 #include <__ranges/enable_borrowed_range.h>
 #include <__ranges/view.h>
 #include <__ranges/size.h>