PR libstdc++/85951 for make_signed/make_unsigned for character types
authorJonathan Wakely <jwakely@redhat.com>
Thu, 31 May 2018 12:18:19 +0000 (13:18 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 31 May 2018 12:18:19 +0000 (13:18 +0100)
Because the wide character types are neither signed integer types nor
unsigned integer types they need to be transformed to an integral type
of the correct size and the lowest rank (which is not necessarily the
underlying type). Reuse the helpers for enumeration types to select the
correct integer.

The refactoring of __make_unsigned_selector and __make_signed_selector
slightly reduces the number of template instantiations and so reduces
memory usage.

PR libstdc++/85951
* include/std/type_traits [_GLIBCXX_USE_C99_STDINT_TR1]: Do not define
uint_least16_t and uint_least32_t.
(__make_unsigned<wchar_t>): Define unconditionally.
(__make_unsigned_selector<_Tp, true, false>): Remove intermediate
typedefs.
(__make_unsigned_selector_base): New type to provide helper templates.
(__make_unsigned_selector<_Tp, false, true>): Reimplement using
__make_unsigned_selector_base helpers.
(__make_unsigned<char16_t>, __make_unsigned<char32_t>): Define.
(__make_signed_selector<_Tp, true, false>): Remove intermediate
typedefs.
(__make_signed<wchar_t>, __make_signed<char16_t>)
(__make_signed<char32_t>)): Define unconditionally.
* testsuite/20_util/make_signed/requirements/typedefs-3.cc: Check
wchar_t, char16_t and char32_t are transformed correctly.
* testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust
dg-error lineno.
* testsuite/20_util/make_unsigned/requirements/typedefs-3.cc: Check
wchar_t, char16_t and char32_t are transformed correctly.
* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: Adjust
dg-error lineno.

From-SVN: r261023

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs-3.cc
libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc
libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs-3.cc
libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc

index 762de9a763d6aff8f16de843f42e11d0566b0aa7..8735cacf94105e418f23dc9eb1859001f5eb02be 100644 (file)
@@ -1,3 +1,28 @@
+2018-05-31  Jonathan Wakely  <jwakely@redhat.com>
+
+       PR libstdc++/85951
+       * include/std/type_traits [_GLIBCXX_USE_C99_STDINT_TR1]: Do not define
+       uint_least16_t and uint_least32_t.
+       (__make_unsigned<wchar_t>): Define unconditionally.
+       (__make_unsigned_selector<_Tp, true, false>): Remove intermediate
+       typedefs.
+       (__make_unsigned_selector_base): New type to provide helper templates.
+       (__make_unsigned_selector<_Tp, false, true>): Reimplement using
+       __make_unsigned_selector_base helpers.
+       (__make_unsigned<char16_t>, __make_unsigned<char32_t>): Define.
+       (__make_signed_selector<_Tp, true, false>): Remove intermediate
+       typedefs.
+       (__make_signed<wchar_t>, __make_signed<char16_t>)
+       (__make_signed<char32_t>)): Define unconditionally.
+       * testsuite/20_util/make_signed/requirements/typedefs-3.cc: Check
+       wchar_t, char16_t and char32_t are transformed correctly.
+       * testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust
+       dg-error lineno.
+       * testsuite/20_util/make_unsigned/requirements/typedefs-3.cc: Check
+       wchar_t, char16_t and char32_t are transformed correctly.
+       * testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: Adjust
+       dg-error lineno.
+
 2018-05-29  Jonathan Wakely  <jwakely@redhat.com>
 
        * include/std/variant (__erased_dtor): Qualify call to __get.
index 7c0ba727511055d248431645e476da3ac0d5c4fd..4397c484f208180b5e7a63d0bb0089953ac4c4fd 100644 (file)
 
 #include <bits/c++config.h>
 
-#ifdef _GLIBCXX_USE_C99_STDINT_TR1
-# if defined (__UINT_LEAST16_TYPE__) && defined(__UINT_LEAST32_TYPE__)
-namespace std
-{
-  typedef __UINT_LEAST16_TYPE__ uint_least16_t;
-  typedef __UINT_LEAST32_TYPE__ uint_least32_t;
-}
-# else
-#  include <cstdint>
-# endif
-#endif
-
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -1576,12 +1564,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct __make_unsigned<long long>
     { typedef unsigned long long __type; };
 
-#if defined(_GLIBCXX_USE_WCHAR_T) && !defined(__WCHAR_UNSIGNED__)
-  template<>
-    struct __make_unsigned<wchar_t> : __make_unsigned<__WCHAR_TYPE__>
-    { };
-#endif
-
 #if defined(__GLIBCXX_TYPE_INT_N_0)
   template<>
     struct __make_unsigned<__GLIBCXX_TYPE_INT_N_0>
@@ -1612,36 +1594,77 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     class __make_unsigned_selector<_Tp, true, false>
     {
-      typedef __make_unsigned<typename remove_cv<_Tp>::type> __unsignedt;
-      typedef typename __unsignedt::__type __unsigned_type;
-      typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned;
+      using __unsigned_type
+       = typename __make_unsigned<typename remove_cv<_Tp>::type>::__type;
 
     public:
-      typedef typename __cv_unsigned::__type __type;
+      using __type
+       = typename __match_cv_qualifiers<_Tp, __unsigned_type>::__type;
     };
 
+  class __make_unsigned_selector_base
+  {
+  protected:
+    template<typename...> struct _List { };
+
+    template<typename _Tp, typename... _Up>
+      struct _List<_Tp, _Up...> : _List<_Up...>
+      { static constexpr size_t __size = sizeof(_Tp); };
+
+    template<size_t _Sz, typename _Tp, bool = (_Sz <= _Tp::__size)>
+      struct __select;
+
+    template<size_t _Sz, typename _Uint, typename... _UInts>
+      struct __select<_Sz, _List<_Uint, _UInts...>, true>
+      { using __type = _Uint; };
+
+    template<size_t _Sz, typename _Uint, typename... _UInts>
+      struct __select<_Sz, _List<_Uint, _UInts...>, false>
+      : __select<_Sz, _List<_UInts...>>
+      { };
+  };
+
+  // Choose unsigned integer type with the smallest rank and same size as _Tp
   template<typename _Tp>
     class __make_unsigned_selector<_Tp, false, true>
+    : __make_unsigned_selector_base
     {
       // With -fshort-enums, an enum may be as small as a char.
-      typedef unsigned char __smallest;
-      static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest);
-      static const bool __b1 = sizeof(_Tp) <= sizeof(unsigned short);
-      static const bool __b2 = sizeof(_Tp) <= sizeof(unsigned int);
-      static const bool __b3 = sizeof(_Tp) <= sizeof(unsigned long);
-      typedef conditional<__b3, unsigned long, unsigned long long> __cond3;
-      typedef typename __cond3::type __cond3_type;
-      typedef conditional<__b2, unsigned int, __cond3_type> __cond2;
-      typedef typename __cond2::type __cond2_type;
-      typedef conditional<__b1, unsigned short, __cond2_type> __cond1;
-      typedef typename __cond1::type __cond1_type;
-
-      typedef typename conditional<__b0, __smallest, __cond1_type>::type
-       __unsigned_type;
-      typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned;
+      using _UInts = _List<unsigned char, unsigned short, unsigned int,
+                          unsigned long, unsigned long long>;
+
+      using __unsigned_type = typename __select<sizeof(_Tp), _UInts>::__type;
 
     public:
-      typedef typename __cv_unsigned::__type __type;
+      using __type
+       = typename __match_cv_qualifiers<_Tp, __unsigned_type>::__type;
+    };
+
+  // wchar_t, char16_t and char32_t are integral types but are neither
+  // signed integer types not unsigned integer types, so must be
+  // transformed to the unsigned integer type with the smallest rank.
+  // Use the partial specialization for enumeration types to do that.
+#if defined(_GLIBCXX_USE_WCHAR_T)
+  template<>
+    struct __make_unsigned<wchar_t>
+    {
+      using __type
+       = typename __make_unsigned_selector<wchar_t, false, true>::__type;
+    };
+#endif
+
+  template<>
+    struct __make_unsigned<char16_t>
+    {
+      using __type
+       = typename __make_unsigned_selector<char16_t, false, true>::__type;
+    };
+
+  template<>
+    struct __make_unsigned<char32_t>
+    {
+      using __type
+       = typename __make_unsigned_selector<char32_t, false, true>::__type;
     };
 
   // Given an integral/enum type, return the corresponding unsigned
@@ -1686,21 +1709,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct __make_signed<unsigned long long>
     { typedef signed long long __type; };
 
-#if defined(_GLIBCXX_USE_WCHAR_T) && defined(__WCHAR_UNSIGNED__)
-  template<>
-    struct __make_signed<wchar_t> : __make_signed<__WCHAR_TYPE__>
-    { };
-#endif
-
-#ifdef _GLIBCXX_USE_C99_STDINT_TR1
-  template<>
-    struct __make_signed<char16_t> : __make_signed<uint_least16_t>
-    { };
-  template<>
-    struct __make_signed<char32_t> : __make_signed<uint_least32_t>
-    { };
-#endif
-
 #if defined(__GLIBCXX_TYPE_INT_N_0)
   template<>
     struct __make_signed<unsigned __GLIBCXX_TYPE_INT_N_0>
@@ -1731,14 +1739,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     class __make_signed_selector<_Tp, true, false>
     {
-      typedef __make_signed<typename remove_cv<_Tp>::type> __signedt;
-      typedef typename __signedt::__type __signed_type;
-      typedef __match_cv_qualifiers<_Tp, __signed_type> __cv_signed;
+      using __signed_type
+       = typename __make_signed<typename remove_cv<_Tp>::type>::__type;
 
     public:
-      typedef typename __cv_signed::__type __type;
+      using __type
+       = typename __match_cv_qualifiers<_Tp, __signed_type>::__type;
     };
 
+  // Choose signed integer type with the smallest rank and same size as _Tp
   template<typename _Tp>
     class __make_signed_selector<_Tp, false, true>
     {
@@ -1748,6 +1757,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef typename __make_signed_selector<__unsigned_type>::__type __type;
     };
 
+  // wchar_t, char16_t and char32_t are integral types but are neither
+  // signed integer types not unsigned integer types, so must be
+  // transformed to the signed integer type with the smallest rank.
+  // Use the partial specialization for enumeration types to do that.
+#if defined(_GLIBCXX_USE_WCHAR_T)
+  template<>
+    struct __make_signed<wchar_t>
+    {
+      using __type
+       = typename __make_signed_selector<wchar_t, false, true>::__type;
+    };
+#endif
+
+  template<>
+    struct __make_signed<char16_t>
+    {
+      using __type
+       = typename __make_signed_selector<char16_t, false, true>::__type;
+    };
+
+  template<>
+    struct __make_signed<char32_t>
+    {
+      using __type
+       = typename __make_signed_selector<char32_t, false, true>::__type;
+    };
+
   // Given an integral/enum type, return the corresponding signed
   // integer type.
   // Primary template.
index 3f50952fa3f39185649ded6687c1ef6f12db81ad..79e0a700d0977d517d8b147138be856b48066322 100644 (file)
@@ -59,8 +59,21 @@ using I4 = smallest_rank_t<E4>;
 static_assert(is_same<make_signed<E4>::type,       I4>::value, "");
 static_assert(is_same<make_signed<E4 const>::type, I4 const>::value, "");
 
-// PI libstdc++/60333
+// PR libstdc++/60333
 enum E5 : long long { };
 using I5 = smallest_rank_t<E5>;
 static_assert(is_same<make_signed<E5>::type, I5>::value, "");
 static_assert(is_same<make_signed<E5 const>::type, I5 const>::value, "");
+
+// PR libstdc++/85951
+using I6 = smallest_rank_t<char16_t>;
+static_assert(is_same<make_signed<char16_t>::type, I6>::value, "");
+static_assert(is_same<make_signed<char16_t const>::type, I6 const>::value, "");
+using I7 = smallest_rank_t<char32_t>;
+static_assert(is_same<make_signed<char32_t>::type, I7>::value, "");
+static_assert(is_same<make_signed<char32_t const>::type, I7 const>::value, "");
+#ifdef _GLIBCXX_USE_WCHAR_T
+using I8 = smallest_rank_t<wchar_t>;
+static_assert(is_same<make_signed<wchar_t>::type, I8>::value, "");
+static_assert(is_same<make_signed<wchar_t const>::type, I8 const>::value, "");
+#endif
index 8d46405165eb4e8ec979a963e204e5a92acda328..13bca19c7e041b2c18b02cba8cd2e80019be1d2d 100644 (file)
@@ -28,7 +28,7 @@ void test01()
 {
   using std::make_signed;
 
-  // Negative  tests.
+  // Negative tests.
   typedef make_signed<bool>::type      test1_type;
 
   typedef make_signed<__gnu_test::pod_uint>::type      test2_type;
@@ -47,4 +47,4 @@ void test01()
 // { dg-error "required from here" "" { target *-*-* } 39 }
 // { dg-error "required from here" "" { target *-*-* } 41 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1757 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1793 }
index c901b0e08b127da1dde9eec0069c10c38bccafa2..895aa1c59b9d6701eb138d8c4a4e638b69ddc3c1 100644 (file)
@@ -59,8 +59,21 @@ using I4 = smallest_rank_t<E4>;
 static_assert(is_same<make_unsigned<E4>::type, I4>::value, "");
 static_assert(is_same<make_unsigned<E4 const>::type, I4 const>::value, "");
 
-// PI libstdc++/60333
+// PR libstdc++/60333
 enum E5 : long long { };
 using I5 = smallest_rank_t<E5>;
 static_assert(is_same<make_unsigned<E5>::type, I5>::value, "");
 static_assert(is_same<make_unsigned<E5 const>::type, I5 const>::value, "");
+
+// PR libstdc++/85951
+using I6 = smallest_rank_t<char16_t>;
+static_assert(is_same<make_unsigned<char16_t>::type, I6>::value, "");
+static_assert(is_same<make_unsigned<char16_t const>::type, I6 const>::value, "");
+using I7 = smallest_rank_t<char32_t>;
+static_assert(is_same<make_unsigned<char32_t>::type, I7>::value, "");
+static_assert(is_same<make_unsigned<char32_t const>::type, I7 const>::value, "");
+#ifdef _GLIBCXX_USE_WCHAR_T
+using I8 = smallest_rank_t<wchar_t>;
+static_assert(is_same<make_unsigned<wchar_t>::type, I8>::value, "");
+static_assert(is_same<make_unsigned<wchar_t const>::type, I8 const>::value, "");
+#endif
index 93ae15723cf913965175379b60ee81061b0be153..9df01550ea3dba88dd41cf00e14ea1b95763ddf9 100644 (file)
@@ -28,7 +28,7 @@ void test01()
 {
   using std::make_unsigned;
 
-  // Negative  tests.
+  // Negative tests.
   typedef make_unsigned<bool>::type            test1_type;
 
   typedef make_unsigned<__gnu_test::pod_uint>::type            test2_type;
@@ -47,5 +47,5 @@ void test01()
 // { dg-error "required from here" "" { target *-*-* } 39 }
 // { dg-error "required from here" "" { target *-*-* } 41 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1653 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1676 }