summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2020-04-08 18:00:13 -0400
committerEric Fiselier <eric@efcs.ca>2020-04-08 18:00:13 -0400
commit601f7631827ae6ac08117a282c83a62b67dedf48 (patch)
treeec067b307d448032231338ec7698a2929aa3a7c8
parentbefc788cfaa996b8399765d8157283d2336750fb (diff)
downloadllvm-601f7631827ae6ac08117a282c83a62b67dedf48.tar.gz
[libcxx] Adds [concept.same]
Patch from Christopher Di Bella (cjdb@google.com) Reviewed as https://reviews.llvm.org/D74291 Adds `std::same_as` to libc++. Since there aren't clang-format rules for //requires-expressions//, I'll need to disable the formatter in certain areas.
-rw-r--r--libcxx/CREDITS.TXT5
-rw-r--r--libcxx/include/CMakeLists.txt1
-rw-r--r--libcxx/include/concepts166
-rw-r--r--libcxx/include/module.modulemap4
-rw-r--r--libcxx/test/libcxx/double_include.sh.cpp1
-rw-r--r--libcxx/test/std/concepts/lang/same_as.pass.cpp295
6 files changed, 472 insertions, 0 deletions
diff --git a/libcxx/CREDITS.TXT b/libcxx/CREDITS.TXT
index f97b9d822772..49f095d29645 100644
--- a/libcxx/CREDITS.TXT
+++ b/libcxx/CREDITS.TXT
@@ -41,6 +41,11 @@ N: Jonathan B Coe
E: jbcoe@me.com
D: Implementation of propagate_const.
+N: Christopher Di Bella
+E: cjdb@google.com
+E: cjdb.ns@gmail.com
+D: Library concepts.
+
N: Glen Joseph Fernandes
E: glenjofe@gmail.com
D: Implementation of to_address.
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 2f375f144774..433bf67fbcb9 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -45,6 +45,7 @@ set(files
compare
complex
complex.h
+ concepts
condition_variable
csetjmp
csignal
diff --git a/libcxx/include/concepts b/libcxx/include/concepts
new file mode 100644
index 000000000000..047e2c290f4e
--- /dev/null
+++ b/libcxx/include/concepts
@@ -0,0 +1,166 @@
+// -*- C++ -*-
+//===-------------------------- concepts ----------------------------------===//
+//
+// 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_CONCEPTS
+#define _LIBCPP_CONCEPTS
+
+/*
+ concepts synopsis
+namespace std {
+ // [concepts.lang], language-related concepts
+ // [concept.same], concept same_as
+ template<class T, class U>
+ concept same_as = see below;
+
+ // [concept.derived], concept derived_from
+ template<class Derived, class Base>
+ concept derived_from = see below;
+
+ // [concept.convertible], concept convertible_to
+ template<class From, class To>
+ concept convertible_to = see below;
+
+ // [concept.commonref], concept common_reference_with
+ template<class T, class U>
+ concept common_reference_with = see below;
+
+ // [concept.common], concept common_with
+ template<class T, class U>
+ concept common_with = see below;
+
+ // [concepts.arithmetic], arithmetic concepts
+ template<class T>
+ concept integral = see below;
+ template<class T>
+ concept signed_integral = see below;
+ template<class T>
+ concept unsigned_integral = see below;
+ template<class T>
+ concept floating_point = see below;
+
+ // [concept.assignable], concept assignable_from
+ template<class LHS, class RHS>
+ concept assignable_from = see below;
+
+ // [concept.swappable], concept swappable
+ namespace ranges {
+ inline namespace unspecified {
+ inline constexpr unspecified swap = unspecified;
+ }
+ }
+ template<class T>
+ concept swappable = see below;
+ template<class T, class U>
+ concept swappable_with = see below;
+
+ // [concept.destructible], concept destructible
+ template<class T>
+ concept destructible = see below;
+
+ // [concept.constructible], concept constructible_from
+ template<class T, class... Args>
+ concept constructible_from = see below;
+
+ // [concept.defaultconstructible], concept default_constructible
+ template<class T>
+ concept default_constructible = see below;
+
+ // [concept.moveconstructible], concept move_constructible
+ template<class T>
+ concept move_constructible = see below;
+
+ // [concept.copyconstructible], concept copy_constructible
+ template<class T>
+ concept copy_constructible = see below;
+
+ // [concepts.compare], comparison concepts
+ // [concept.boolean], concept boolean
+ template<class B>
+ concept boolean = see below;
+
+ // [concept.equalitycomparable], concept equality_comparable
+ template<class T>
+ concept equality_comparable = see below;
+ template<class T, class U>
+ concept equality_comparable_with = see below;
+
+ // [concept.totallyordered], concept totally_ordered
+ template<class T>
+ concept totally_ordered = see below;
+ template<class T, class U>
+ concept totally_ordered_with = see below;
+
+ // [concepts.object], object concepts
+ template<class T>
+ concept movable = see below;
+ template<class T>
+ concept copyable = see below;
+ template<class T>
+ concept semiregular = see below;
+ template<class T>
+ concept regular = see below;
+
+ // [concepts.callable], callable concepts
+ // [concept.invocable], concept invocable
+ template<class F, class... Args>
+ concept invocable = see below;
+
+ // [concept.regularinvocable], concept regular_invocable
+ template<class F, class... Args>
+ concept regular_invocable = see below;
+
+ // [concept.predicate], concept predicate
+ template<class F, class... Args>
+ concept predicate = see below;
+
+ // [concept.relation], concept relation
+ template<class R, class T, class U>
+ concept relation = see below;
+
+ // [concept.equiv], concept equivalence_relation
+ template<class R, class T, class U>
+ concept equivalence_relation = see below;
+
+ // [concept.strictweakorder], concept strict_weak_order
+ template<class R, class T, class U>
+ concept strict_weak_order = see below;
+}
+
+*/
+
+#include <__config>
+#include <type_traits>
+#include <version>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17 && defined(__cpp_concepts) && __cpp_concepts >= 201811L
+
+// [concept.same]
+
+template<class _Tp, class _Up>
+concept __same_as_impl = _VSTD::_IsSame<_Tp, _Up>::value;
+
+template<class _Tp, class _Up>
+concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
+
+#endif //_LIBCPP_STD_VER > 17 && defined(__cpp_concepts) && __cpp_concepts >= 201811L
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP_CONCEPTS
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 535785be8cf2..fb6705c790d2 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -267,6 +267,10 @@ module std [system] {
header "complex"
export *
}
+ module concepts {
+ header "concepts"
+ export *
+ }
module condition_variable {
header "condition_variable"
export *
diff --git a/libcxx/test/libcxx/double_include.sh.cpp b/libcxx/test/libcxx/double_include.sh.cpp
index 17236df3cda7..f56e2ec9fa70 100644
--- a/libcxx/test/libcxx/double_include.sh.cpp
+++ b/libcxx/test/libcxx/double_include.sh.cpp
@@ -49,6 +49,7 @@
#include <compare>
#include <complex>
#include <complex.h>
+#include <concepts>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
diff --git a/libcxx/test/std/concepts/lang/same_as.pass.cpp b/libcxx/test/std/concepts/lang/same_as.pass.cpp
new file mode 100644
index 000000000000..c41d6c47ac82
--- /dev/null
+++ b/libcxx/test/std/concepts/lang/same_as.pass.cpp
@@ -0,0 +1,295 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++98, c++03, c++11, c++14, c++17
+
+// template<class T, class U>
+// concept same_as;
+
+#include <concepts>
+#include <type_traits>
+
+struct S1 {};
+struct S2 {
+ int i;
+
+ int& f();
+ double g(int x) const;
+};
+struct S3 {
+ int& r;
+};
+struct S4 {
+ int&& r;
+};
+struct S5 {
+ int* p;
+};
+
+class C1 {};
+class C2 {
+ [[maybe_unused]] int i;
+};
+
+class C3 {
+public:
+ int i;
+};
+
+template <class T1, class T2 = T1>
+class C4 {
+ int t1;
+ int t2;
+};
+
+template <class T1, class T2 = T1>
+class C5 {
+ [[maybe_unused]] T1 t1;
+
+public:
+ T2 t2;
+};
+
+template <class T1, class T2 = T1>
+class C6 {
+public:
+ [[maybe_unused]] T1 t1;
+ [[maybe_unused]] T2 t2;
+};
+
+template <class T>
+struct identity {
+ using type = T;
+};
+
+template <template <typename> class Modifier = identity>
+void CheckSameAs() {
+ static_assert(
+ std::same_as<typename Modifier<int>::type, typename Modifier<int>::type>);
+ static_assert(
+ std::same_as<typename Modifier<S1>::type, typename Modifier<S1>::type>);
+ static_assert(
+ std::same_as<typename Modifier<S2>::type, typename Modifier<S2>::type>);
+ static_assert(
+ std::same_as<typename Modifier<S3>::type, typename Modifier<S3>::type>);
+ static_assert(
+ std::same_as<typename Modifier<S4>::type, typename Modifier<S4>::type>);
+ static_assert(
+ std::same_as<typename Modifier<S5>::type, typename Modifier<S5>::type>);
+ static_assert(
+ std::same_as<typename Modifier<C1>::type, typename Modifier<C1>::type>);
+ static_assert(
+ std::same_as<typename Modifier<C2>::type, typename Modifier<C2>::type>);
+ static_assert(
+ std::same_as<typename Modifier<C3>::type, typename Modifier<C3>::type>);
+ static_assert(std::same_as<typename Modifier<C4<int> >::type,
+ typename Modifier<C4<int> >::type>);
+ static_assert(std::same_as<typename Modifier<C4<int&> >::type,
+ typename Modifier<C4<int&> >::type>);
+ static_assert(std::same_as<typename Modifier<C4<int&&> >::type,
+ typename Modifier<C4<int&&> >::type>);
+ static_assert(std::same_as<typename Modifier<C5<int> >::type,
+ typename Modifier<C5<int> >::type>);
+ static_assert(std::same_as<typename Modifier<C5<int&> >::type,
+ typename Modifier<C5<int&> >::type>);
+ static_assert(std::same_as<typename Modifier<C5<int&&> >::type,
+ typename Modifier<C5<int&&> >::type>);
+ static_assert(std::same_as<typename Modifier<C6<int> >::type,
+ typename Modifier<C6<int> >::type>);
+ static_assert(std::same_as<typename Modifier<C6<int&> >::type,
+ typename Modifier<C6<int&> >::type>);
+ static_assert(std::same_as<typename Modifier<C6<int&&> >::type,
+ typename Modifier<C6<int&&> >::type>);
+
+ static_assert(std::same_as<typename Modifier<void>::type,
+ typename Modifier<void>::type>);
+}
+
+template <template <typename> class Modifier1,
+ template <typename> class Modifier2>
+void CheckNotSameAs() {
+ static_assert(!std::same_as<typename Modifier1<int>::type,
+ typename Modifier2<int>::type>);
+ static_assert(!std::same_as<typename Modifier1<S1>::type,
+ typename Modifier2<S1>::type>);
+ static_assert(!std::same_as<typename Modifier1<S2>::type,
+ typename Modifier2<S2>::type>);
+ static_assert(!std::same_as<typename Modifier1<S3>::type,
+ typename Modifier2<S3>::type>);
+ static_assert(!std::same_as<typename Modifier1<S4>::type,
+ typename Modifier2<S4>::type>);
+ static_assert(!std::same_as<typename Modifier1<S5>::type,
+ typename Modifier2<S5>::type>);
+ static_assert(!std::same_as<typename Modifier1<C1>::type,
+ typename Modifier2<C1>::type>);
+ static_assert(!std::same_as<typename Modifier1<C2>::type,
+ typename Modifier2<C2>::type>);
+ static_assert(!std::same_as<typename Modifier1<C3>::type,
+ typename Modifier2<C3>::type>);
+ static_assert(!std::same_as<typename Modifier1<C4<int> >::type,
+ typename Modifier2<C4<int> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C4<int&> >::type,
+ typename Modifier2<C4<int&> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C4<int&&> >::type,
+ typename Modifier2<C4<int&&> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C5<int> >::type,
+ typename Modifier2<C5<int> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C5<int&> >::type,
+ typename Modifier2<C5<int&> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C5<int&&> >::type,
+ typename Modifier2<C5<int&&> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C6<int> >::type,
+ typename Modifier2<C6<int> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C6<int&> >::type,
+ typename Modifier2<C6<int&> >::type>);
+ static_assert(!std::same_as<typename Modifier1<C6<int&&> >::type,
+ typename Modifier2<C6<int&&> >::type>);
+}
+
+// Checks subsumption works as intended
+template <class T, class U>
+requires std::same_as<T, U> void SubsumptionTest();
+
+// clang-format off
+template <class T, class U>
+requires std::same_as<U, T> && true // NOLINT(readability-simplify-boolean-expr)
+int SubsumptionTest();
+// clang-format on
+
+static_assert(std::same_as<int, decltype(SubsumptionTest<int, int>())>);
+static_assert(std::same_as<int, decltype(SubsumptionTest<void, void>())>);
+static_assert(
+ std::same_as<int, decltype(SubsumptionTest<int (*)(), int (*)()>())>);
+static_assert(
+ std::same_as<
+ int, decltype(SubsumptionTest<double (&)(int), double (&)(int)>())>);
+static_assert(
+ std::same_as<int, decltype(SubsumptionTest<int S2::*, int S2::*>())>);
+static_assert(
+ std::same_as<int,
+ decltype(SubsumptionTest<int& (S2::*)(), int& (S2::*)()>())>);
+
+int main(int, char**) {
+ { // Checks std::same_as<T, T> is true
+ CheckSameAs();
+
+ // Checks std::same_as<T&, T&> is true
+ CheckSameAs<std::add_lvalue_reference>();
+
+ // Checks std::same_as<T&&, T&&> is true
+ CheckSameAs<std::add_rvalue_reference>();
+
+ // Checks std::same_as<const T, const T> is true
+ CheckSameAs<std::add_const>();
+
+ // Checks std::same_as<volatile T, volatile T> is true
+ CheckSameAs<std::add_volatile>();
+
+ // Checks std::same_as<const volatile T, const volatile T> is true
+ CheckSameAs<std::add_cv>();
+
+ // Checks std::same_as<T*, T*> is true
+ CheckSameAs<std::add_pointer>();
+
+ // Checks concrete types are identical
+ static_assert(std::same_as<void, void>);
+
+ using Void = void;
+ static_assert(std::same_as<void, Void>);
+
+ static_assert(std::same_as<int[1], int[1]>);
+ static_assert(std::same_as<int[2], int[2]>);
+
+ static_assert(std::same_as<int (*)(), int (*)()>);
+ static_assert(std::same_as<void (&)(), void (&)()>);
+ static_assert(std::same_as<S1& (*)(S1), S1& (*)(S1)>);
+ static_assert(std::same_as<C1& (&)(S1, int), C1& (&)(S1, int)>);
+
+ static_assert(std::same_as<int S2::*, int S2::*>);
+ static_assert(std::same_as<double S2::*, double S2::*>);
+
+ static_assert(std::same_as<int& (S2::*)(), int& (S2::*)()>);
+ static_assert(std::same_as<double& (S2::*)(int), double& (S2::*)(int)>);
+ }
+
+ { // Checks that `T` and `T&` are distinct types
+ CheckNotSameAs<identity, std::add_lvalue_reference>();
+ CheckNotSameAs<std::add_lvalue_reference, identity>();
+
+ // Checks that `T` and `T&&` are distinct types
+ CheckNotSameAs<identity, std::add_rvalue_reference>();
+ CheckNotSameAs<std::add_rvalue_reference, identity>();
+
+ // Checks that `T` and `const T` are distinct types
+ CheckNotSameAs<identity, std::add_const>();
+ CheckNotSameAs<std::add_const, identity>();
+
+ // Checks that `T` and `volatile T` are distinct types
+ CheckNotSameAs<identity, std::add_volatile>();
+ CheckNotSameAs<std::add_volatile, identity>();
+
+ // Checks that `T` and `const volatile T` are distinct types
+ CheckNotSameAs<identity, std::add_cv>();
+ CheckNotSameAs<std::add_cv, identity>();
+
+ // Checks that `const T` and `volatile T` are distinct types
+ CheckNotSameAs<std::add_const, std::add_volatile>();
+ CheckNotSameAs<std::add_volatile, std::add_const>();
+
+ // Checks that `const T` and `const volatile T` are distinct types
+ CheckNotSameAs<std::add_const, std::add_cv>();
+ CheckNotSameAs<std::add_cv, std::add_const>();
+
+ // Checks that `volatile T` and `const volatile T` are distinct types
+ CheckNotSameAs<std::add_volatile, std::add_cv>();
+ CheckNotSameAs<std::add_cv, std::add_volatile>();
+
+ // Checks `T&` and `T&&` are distinct types
+ CheckNotSameAs<std::add_lvalue_reference, std::add_rvalue_reference>();
+ CheckNotSameAs<std::add_rvalue_reference, std::add_lvalue_reference>();
+ }
+
+ { // Checks different type names are distinct types
+ static_assert(!std::same_as<S1, C1>);
+ static_assert(!std::same_as<C4<int>, C5<int> >);
+ static_assert(!std::same_as<C4<int>, C5<int> >);
+ static_assert(!std::same_as<C5<int, double>, C5<double, int> >);
+
+ static_assert(!std::same_as<int&, const int&>);
+ static_assert(!std::same_as<int&, volatile int&>);
+ static_assert(!std::same_as<int&, const volatile int&>);
+
+ static_assert(!std::same_as<int&&, const int&>);
+ static_assert(!std::same_as<int&&, volatile int&>);
+ static_assert(!std::same_as<int&&, const volatile int&>);
+
+ static_assert(!std::same_as<int&, const int&&>);
+ static_assert(!std::same_as<int&, volatile int&&>);
+ static_assert(!std::same_as<int&, const volatile int&&>);
+
+ static_assert(!std::same_as<int&&, const int&&>);
+ static_assert(!std::same_as<int&&, volatile int&&>);
+ static_assert(!std::same_as<int&&, const volatile int&&>);
+
+ static_assert(!std::same_as<void, int>);
+
+ static_assert(!std::same_as<int[1], int[2]>);
+ static_assert(!std::same_as<double[1], int[2]>);
+
+ static_assert(!std::same_as<int* (*)(), const int* (*)()>);
+ static_assert(!std::same_as<void (&)(), void (&)(S1)>);
+ static_assert(!std::same_as<S1 (*)(S1), S1& (*)(S1)>);
+ static_assert(!std::same_as<C3 (&)(int), C1& (&)(S1, int)>);
+
+ static_assert(!std::same_as<int S2::*, double S2::*>);
+
+ static_assert(!std::same_as<int& (S2::*)(), double& (S2::*)(int)>);
+ }
+
+ return 0;
+}