]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Define std::byte for C++17 (P0298R3)
authorJonathan Wakely <jwakely@redhat.com>
Thu, 9 Mar 2017 15:42:02 +0000 (15:42 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 9 Mar 2017 15:42:02 +0000 (15:42 +0000)
* doc/xml/manual/status_cxx2017.xml: Document std::byte support.
* include/c_global/cstddef (std::byte): Define for C++17.
* testsuite/18_support/byte/global_neg.cc: New test.
* testsuite/18_support/byte/ops.cc: New test.
* testsuite/18_support/byte/requirements.cc: New test.

From-SVN: r246005

libstdc++-v3/ChangeLog
libstdc++-v3/doc/xml/manual/status_cxx2017.xml
libstdc++-v3/include/c_global/cstddef
libstdc++-v3/testsuite/18_support/byte/global_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/18_support/byte/ops.cc [new file with mode: 0644]
libstdc++-v3/testsuite/18_support/byte/requirements.cc [new file with mode: 0644]

index ef48d8767cffbf71d246c8d7201e281390658a52..dabac4ad1280b9e0031cc1c7e855c4471868f4a6 100644 (file)
@@ -1,3 +1,11 @@
+2017-03-09  Jonathan Wakely  <jwakely@redhat.com>
+
+       * doc/xml/manual/status_cxx2017.xml: Document std::byte support.
+       * include/c_global/cstddef (std::byte): Define for C++17.
+       * testsuite/18_support/byte/global_neg.cc: New test.
+       * testsuite/18_support/byte/ops.cc: New test.
+       * testsuite/18_support/byte/requirements.cc: New test.
+
 2017-03-05  Jonathan Wakely  <jwakely@redhat.com>
 
        * doc/xml/manual/status_cxx2017.xml: Document P0156R2 status.
index 1053f2dd84bad904ef2f5f76c299498b3182848f..e0e4e8e7c8005e4d645cdb7beeef3d12e921e3cd 100644 (file)
@@ -459,18 +459,6 @@ Feature-testing recommendations for C++</link>.
       <entry><code> __cpp_lib_has_unique_object_representations >= 201606 </code></entry>
     </row>
 
-    <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
-      <entry> Ordered by default </entry>
-      <entry>
-       <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0181r1.html">
-       P0181R1
-       </link>
-      </entry>
-      <entry align="center"> No </entry>
-      <entry><code> __cpp_lib_default_order >= 201606</code></entry>
-    </row>
-
     <row>
       <entry> Polishing <code>&lt;chrono&gt;</code> </entry>
       <entry>
@@ -729,6 +717,19 @@ Feature-testing recommendations for C++</link>.
       <entry><code> __cpp_lib_scoped_lock >= 201703 </code></entry>
     </row>
 
+    <row>
+      <?dbhtml bgcolor="#C8B0B0" ?>
+      <entry>  </entry> byte type definition
+      <entry>
+       <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0298r3.pdf">
+       P0298R3
+       </link>
+      </entry>
+      <entry align="center"> 7 </entry>
+      <entry><code> ??? </code></entry>
+    </row>
+
+
   </tbody>
 </tgroup>
 </table>
index a571e80c2f6e9e8bde26455e28075569944528f4..09754ee45da45cb90ea637aea72424e1104b46fc 100644 (file)
@@ -57,4 +57,133 @@ namespace std
 }
 #endif
 
+#if __cplusplus > 201402L
+namespace std
+{
+  /// std::byte
+  enum class byte : unsigned char {};
+
+  template<typename _IntegerType> struct __byte_operand;
+  template<> struct __byte_operand<bool> { using __type = byte; };
+  template<> struct __byte_operand<char> { using __type = byte; };
+  template<> struct __byte_operand<signed char> { using __type = byte; };
+  template<> struct __byte_operand<unsigned char> { using __type = byte; };
+#ifdef _GLIBCXX_USE_WCHAR_T
+  template<> struct __byte_operand<wchar_t> { using __type = byte; };
+#endif
+  template<> struct __byte_operand<char16_t> { using __type = byte; };
+  template<> struct __byte_operand<char32_t> { using __type = byte; };
+  template<> struct __byte_operand<short> { using __type = byte; };
+  template<> struct __byte_operand<unsigned short> { using __type = byte; };
+  template<> struct __byte_operand<int> { using __type = byte; };
+  template<> struct __byte_operand<unsigned int> { using __type = byte; };
+  template<> struct __byte_operand<long> { using __type = byte; };
+  template<> struct __byte_operand<unsigned long> { using __type = byte; };
+  template<> struct __byte_operand<long long> { using __type = byte; };
+  template<> struct __byte_operand<unsigned long long> { using __type = byte; };
+#if defined(__GLIBCXX_TYPE_INT_N_0)
+  template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_0>
+  { using __type = byte; };
+  template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_0>
+  { using __type = byte; };
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_1)
+  template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_1>
+  { using __type = byte; };
+  template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_1>
+  { using __type = byte; };
+#endif
+#if defined(__GLIBCXX_TYPE_INT_N_2)
+  template<> struct __byte_operand<__GLIBCXX_TYPE_INT_N_2>
+  { using __type = byte; };
+  template<> struct __byte_operand<unsigned __GLIBCXX_TYPE_INT_N_2>
+  { using __type = byte; };
+#endif
+  template<typename _IntegerType>
+    struct __byte_operand<const _IntegerType>
+    : __byte_operand<_IntegerType> { };
+  template<typename _IntegerType>
+    struct __byte_operand<volatile _IntegerType>
+    : __byte_operand<_IntegerType> { };
+  template<typename _IntegerType>
+    struct __byte_operand<const volatile _IntegerType>
+    : __byte_operand<_IntegerType> { };
+
+  template<typename _IntegerType>
+    using __byte_op_t = typename __byte_operand<_IntegerType>::__type;
+
+  template<typename _IntegerType>
+    constexpr __byte_op_t<_IntegerType>&
+    operator<<=(byte& __b, _IntegerType __shift) noexcept
+    { return __b = byte(static_cast<unsigned char>(__b) << __shift); }
+
+  template<typename _IntegerType>
+    constexpr __byte_op_t<_IntegerType>
+    operator<<(byte __b, _IntegerType __shift) noexcept
+    { return byte(static_cast<unsigned char>(__b) << __shift); }
+
+  template<typename _IntegerType>
+    constexpr __byte_op_t<_IntegerType>&
+    operator>>=(byte& __b, _IntegerType __shift) noexcept
+    { return __b = byte(static_cast<unsigned char>(__b) >> __shift); }
+
+  template<typename _IntegerType>
+    constexpr __byte_op_t<_IntegerType>
+    operator>>(byte __b, _IntegerType __shift) noexcept
+    { return byte(static_cast<unsigned char>(__b) >> __shift); }
+
+  constexpr byte&
+  operator|=(byte& __l, byte __r) noexcept
+  {
+    return __l =
+      byte(static_cast<unsigned char>(__l) | static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte
+  operator|(byte __l, byte __r) noexcept
+  {
+    return
+      byte(static_cast<unsigned char>(__l) | static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte&
+  operator&=(byte& __l, byte __r) noexcept
+  {
+   return __l =
+     byte(static_cast<unsigned char>(__l) & static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte
+  operator&(byte __l, byte __r) noexcept
+  {
+    return
+      byte(static_cast<unsigned char>(__l) & static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte&
+  operator^=(byte& __l, byte __r) noexcept
+  {
+    return __l =
+      byte(static_cast<unsigned char>(__l) ^ static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte
+  operator^(byte __l, byte __r) noexcept
+  {
+    return
+      byte(static_cast<unsigned char>(__l) ^ static_cast<unsigned char>(__r));
+  }
+
+  constexpr byte
+  operator~(byte __b) noexcept
+  { return byte(~static_cast<unsigned char>(__b)); }
+
+  template<typename _IntegerType>
+    constexpr _IntegerType
+    to_integer(__byte_op_t<_IntegerType> __b) noexcept
+    { return _IntegerType(__b); }
+
+} // namespace std
+#endif
+
 #endif // _GLIBCXX_CSTDDEF
diff --git a/libstdc++-v3/testsuite/18_support/byte/global_neg.cc b/libstdc++-v3/testsuite/18_support/byte/global_neg.cc
new file mode 100644 (file)
index 0000000..722f658
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <stddef.h>
+
+byte b;                        // { dg-error "does not name a type" }
+using std::byte;       // { dg-error "has not been declared" }
diff --git a/libstdc++-v3/testsuite/18_support/byte/ops.cc b/libstdc++-v3/testsuite/18_support/byte/ops.cc
new file mode 100644 (file)
index 0000000..6573bd9
--- /dev/null
@@ -0,0 +1,224 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <cstddef>
+
+constexpr bool is_byte(std::byte) { return true; }
+template<typename T> constexpr bool is_byte(T) { return false; }
+
+template<typename T>
+constexpr bool test_lshift_assign(unsigned char c, T t)
+{
+  std::byte b{c};
+  b <<= t;
+  return b == std::byte(c << t);
+}
+
+static_assert( test_lshift_assign(0, 1) );
+static_assert( test_lshift_assign(0, 1u) );
+static_assert( test_lshift_assign(0, 1ll) );
+static_assert( test_lshift_assign(4, 1) );
+static_assert( test_lshift_assign(9, 2u) );
+static_assert( test_lshift_assign(16, 3ll) );
+static_assert( test_lshift_assign(127, 1) );
+static_assert( test_lshift_assign(255, 2u) );
+static_assert( test_lshift_assign(63, 3ll) );
+
+template<typename T>
+constexpr bool test_lshift(unsigned char c, T t)
+{
+  const std::byte b{c};
+  const std::byte b2 = b << t;
+  return b2 == std::byte(c << t);
+}
+
+static_assert( test_lshift(0, 1) );
+static_assert( test_lshift(0, 1u) );
+static_assert( test_lshift(0, 1ll) );
+static_assert( test_lshift(4, 1) );
+static_assert( test_lshift(9, 2u) );
+static_assert( test_lshift(16, 3ll) );
+static_assert( test_lshift(127, 1) );
+static_assert( test_lshift(255, 2u) );
+static_assert( test_lshift(63, 3ll) );
+
+template<typename T>
+constexpr bool test_rshift_assign(unsigned char c, T t)
+{
+  std::byte b{c};
+  b >>= t;
+  return b == std::byte(c >> t);
+}
+
+static_assert( test_rshift_assign(0, 1) );
+static_assert( test_rshift_assign(0, 1u) );
+static_assert( test_rshift_assign(0, 1ll) );
+static_assert( test_rshift_assign(4, 1) );
+static_assert( test_rshift_assign(9, 2u) );
+static_assert( test_rshift_assign(16, 3ll) );
+static_assert( test_rshift_assign(127, 1) );
+static_assert( test_rshift_assign(255, 2u) );
+static_assert( test_rshift_assign(63, 3ll) );
+
+template<typename T>
+constexpr bool test_rshift(unsigned char c, T t)
+{
+  const std::byte b{c};
+  const std::byte b2 = b >> t;
+  return b2 == std::byte(c >> t);
+}
+
+static_assert( test_rshift(0, 1) );
+static_assert( test_rshift(0, 1u) );
+static_assert( test_rshift(0, 1ll) );
+static_assert( test_rshift(4, 1) );
+static_assert( test_rshift(9, 2u) );
+static_assert( test_rshift(16, 3ll) );
+static_assert( test_rshift(127, 1) );
+static_assert( test_rshift(255, 2u) );
+static_assert( test_rshift(63, 3ll) );
+
+constexpr bool test_or_assign(unsigned char l, unsigned char r)
+{
+  std::byte b{l};
+  b |= std::byte{r};
+  return b == std::byte(l | r);
+}
+
+static_assert( test_or_assign(0, 1) );
+static_assert( test_or_assign(4, 1) );
+static_assert( test_or_assign(9, 2) );
+static_assert( test_or_assign(16, 3) );
+static_assert( test_or_assign(63, 3) );
+static_assert( test_or_assign(127, 1) );
+static_assert( test_or_assign(255, 2) );
+
+constexpr bool test_or(unsigned char l, unsigned char r)
+{
+  const std::byte b1{l};
+  const std::byte b2{r};
+  return (b1 | b2) == std::byte(l | r);
+}
+
+static_assert( test_or(0, 1) );
+static_assert( test_or(0, 1u) );
+static_assert( test_or(0, 1ll) );
+static_assert( test_or(4, 1) );
+static_assert( test_or(9, 2u) );
+static_assert( test_or(16, 3ll) );
+static_assert( test_or(127, 1) );
+static_assert( test_or(255, 2u) );
+static_assert( test_or(63, 3ll) );
+
+constexpr bool test_and_assign(unsigned char l, unsigned char r)
+{
+  std::byte b{l};
+  b &= std::byte{r};
+  return b == std::byte(l & r);
+}
+
+static_assert( test_and_assign(0, 1) );
+static_assert( test_and_assign(0, 1u) );
+static_assert( test_and_assign(0, 1ll) );
+static_assert( test_and_assign(4, 1) );
+static_assert( test_and_assign(9, 2u) );
+static_assert( test_and_assign(16, 3ll) );
+static_assert( test_and_assign(127, 1) );
+static_assert( test_and_assign(255, 2u) );
+static_assert( test_and_assign(63, 3ll) );
+
+constexpr bool test_and(unsigned char l, unsigned char r)
+{
+  const std::byte b1{l};
+  const std::byte b2{r};
+  return (b1 & b2) == std::byte(l & r);
+}
+
+static_assert( test_and(0, 1) );
+static_assert( test_and(0, 1u) );
+static_assert( test_and(0, 1ll) );
+static_assert( test_and(4, 1) );
+static_assert( test_and(9, 2u) );
+static_assert( test_and(16, 3ll) );
+static_assert( test_and(127, 1) );
+static_assert( test_and(255, 2u) );
+static_assert( test_and(63, 3ll) );
+
+constexpr bool test_xor_assign(unsigned char l, unsigned char r)
+{
+  std::byte b{l};
+  b ^= std::byte{r};
+  return b == std::byte(l ^ r);
+}
+
+static_assert( test_xor_assign(0, 1) );
+static_assert( test_xor_assign(0, 1u) );
+static_assert( test_xor_assign(0, 1ll) );
+static_assert( test_xor_assign(4, 1) );
+static_assert( test_xor_assign(9, 2u) );
+static_assert( test_xor_assign(16, 3ll) );
+static_assert( test_xor_assign(127, 1) );
+static_assert( test_xor_assign(255, 2u) );
+static_assert( test_xor_assign(63, 3ll) );
+
+constexpr bool test_xor(unsigned char l, unsigned char r)
+{
+  const std::byte b1{l};
+  const std::byte b2{r};
+  return (b1 ^ b2) == std::byte(l ^ r);
+}
+
+static_assert( test_xor(0, 1) );
+static_assert( test_xor(0, 1u) );
+static_assert( test_xor(0, 1ll) );
+static_assert( test_xor(4, 1) );
+static_assert( test_xor(9, 2u) );
+static_assert( test_xor(16, 3ll) );
+static_assert( test_xor(127, 1) );
+static_assert( test_xor(255, 2u) );
+static_assert( test_xor(63, 3ll) );
+
+constexpr bool test_complement(unsigned char c)
+{
+  const std::byte b{c};
+  return ~b == std::byte(~c);
+}
+
+static_assert( test_complement(0) );
+static_assert( test_complement(4) );
+static_assert( test_complement(9) );
+static_assert( test_complement(16) );
+static_assert( test_complement(63) );
+static_assert( test_complement(127) );
+static_assert( test_complement(255) );
+
+template<typename T>
+constexpr bool test_to_integer(unsigned char c)
+{
+  std::byte b{c};
+  return std::to_integer<T>(b) == T(c);
+}
+
+static_assert( test_to_integer<int>(0) );
+static_assert( test_to_integer<int>(255) );
+static_assert( test_to_integer<signed char>(255) );
+static_assert( test_to_integer<unsigned>(0) );
+static_assert( test_to_integer<unsigned>(255) );
+
diff --git a/libstdc++-v3/testsuite/18_support/byte/requirements.cc b/libstdc++-v3/testsuite/18_support/byte/requirements.cc
new file mode 100644 (file)
index 0000000..da5fc91
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++1z } }
+
+#include <cstddef>
+
+static_assert( sizeof(std::byte) == sizeof(unsigned char) );
+static_assert( alignof(std::byte) == alignof(unsigned char) );
+
+// Use the built-in to avoid depending on <type_traits>
+__underlying_type(std::byte)* p = (unsigned char*)nullptr;