[PATCH v14 37/40] c++, libstdc++: Implement __is_signed built-in trait

Ken Matsui kmatsui@gcc.gnu.org
Fri Sep 15 23:51:23 GMT 2023


This patch implements built-in trait for std::is_signed.

gcc/cp/ChangeLog:

	* cp-trait.def: Define __is_signed.
	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SIGNED.
	* semantics.cc (trait_expr_value): Likewise.
	(finish_trait_expr): Likewise.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __is_signed.
	* g++.dg/ext/is_signed.C: New test.
	* g++.dg/tm/pr46567.C (__is_signed): Rename to ...
	(__is_signed_type): ... this.

libstdc++-v3/ChangeLog:

	* include/ext/numeric_traits.h (__is_signed): Rename to ...
	(__is_signed_type): ... this.
	* include/bits/charconv.h: Use __is_signed_type instead.
	* include/bits/locale_facets.tcc: Likewise.
	* include/bits/uniform_int_dist.h: Likewise.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/cp/constraint.cc                         |  3 ++
 gcc/cp/cp-trait.def                          |  1 +
 gcc/cp/semantics.cc                          |  4 ++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C     |  3 ++
 gcc/testsuite/g++.dg/ext/is_signed.C         | 47 ++++++++++++++++++++
 gcc/testsuite/g++.dg/tm/pr46567.C            | 12 ++---
 libstdc++-v3/include/bits/charconv.h         |  2 +-
 libstdc++-v3/include/bits/locale_facets.tcc  |  6 +--
 libstdc++-v3/include/bits/uniform_int_dist.h |  4 +-
 libstdc++-v3/include/ext/numeric_traits.h    | 18 ++++----
 10 files changed, 79 insertions(+), 21 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c28dad702c3..b161c9b2c9e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_SAME:
       inform (loc, "  %qT is not the same as %qT", t1, t2);
       break;
+    case CPTK_IS_SIGNED:
+      inform (loc, "  %qT is not a signed type", t1);
+      break;
     case CPTK_IS_SCOPED_ENUM:
       inform (loc, "  %qT is not a scoped enum", t1);
       break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 611e84cbbfd..f0b5fe9cb3b 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
 DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
 DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
 DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
 DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
 DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
 DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index b5c6b4992e5..58011a45cc6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_SAME:
       return same_type_p (type1, type2);
 
+    case CPTK_IS_SIGNED:
+      return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
+
     case CPTK_IS_SCOPED_ENUM:
       return SCOPED_ENUM_P (type1);
 
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_POINTER:
     case CPTK_IS_REFERENCE:
     case CPTK_IS_SAME:
+    case CPTK_IS_SIGNED:
     case CPTK_IS_SCOPED_ENUM:
     case CPTK_IS_UNBOUNDED_ARRAY:
     case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 3d380f94b06..aaf7254df4b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
 #if !__has_builtin (__is_same_as)
 # error "__has_builtin (__is_same_as) failed"
 #endif
+#if !__has_builtin (__is_signed)
+# error "__has_builtin (__is_signed) failed"
+#endif
 #if !__has_builtin (__is_scoped_enum)
 # error "__has_builtin (__is_scoped_enum) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_signed.C b/gcc/testsuite/g++.dg/ext/is_signed.C
new file mode 100644
index 00000000000..a04b548105d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_signed.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+  SA(TRAIT(X) == expect);                  \
+  SA(TRAIT(const X) == expect);            \
+  SA(TRAIT(volatile X) == expect);         \
+  SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_signed, void, false);
+
+SA_TEST_CATEGORY(__is_signed, bool, bool(-1) < bool(0));
+SA_TEST_CATEGORY(__is_signed, char, char(-1) < char(0));
+SA_TEST_CATEGORY(__is_signed, signed char, true);
+SA_TEST_CATEGORY(__is_signed, unsigned char, false);
+SA_TEST_CATEGORY(__is_signed, wchar_t, wchar_t(-1) < wchar_t(0));
+SA_TEST_CATEGORY(__is_signed, short, true);
+SA_TEST_CATEGORY(__is_signed, unsigned short, false);
+SA_TEST_CATEGORY(__is_signed, int, true);
+SA_TEST_CATEGORY(__is_signed, unsigned int, false);
+SA_TEST_CATEGORY(__is_signed, long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long, false);
+SA_TEST_CATEGORY(__is_signed, long long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long long, false);
+
+SA_TEST_CATEGORY(__is_signed, float, true);
+SA_TEST_CATEGORY(__is_signed, double, true);
+SA_TEST_CATEGORY(__is_signed, long double, true);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_signed, __int128, true);
+SA_TEST_CATEGORY(__is_signed, unsigned __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_signed, __float128, true);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_signed, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 79d304e0309..c891aff20f4 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -403,7 +403,7 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
     {
       static const _Value __min = (((_Value)(-1) < 0) ? (_Value)1 << (sizeof(_Value) * 8 - ((_Value)(-1) < 0)) : (_Value)0);
       static const _Value __max = (((_Value)(-1) < 0) ? (((((_Value)1 << ((sizeof(_Value) * 8 - ((_Value)(-1) < 0)) - 1)) - 1) << 1) + 1) : ~(_Value)0);
-      static const bool __is_signed = ((_Value)(-1) < 0);
+      static const bool __is_signed_type = ((_Value)(-1) < 0);
       static const int __digits = (sizeof(_Value) * 8 - ((_Value)(-1) < 0));
     };
   template<typename _Value>
@@ -411,21 +411,21 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
   template<typename _Value>
     const _Value __numeric_traits_integer<_Value>::__max;
   template<typename _Value>
-    const bool __numeric_traits_integer<_Value>::__is_signed;
+    const bool __numeric_traits_integer<_Value>::__is_signed_type;
   template<typename _Value>
     const int __numeric_traits_integer<_Value>::__digits;
   template<typename _Value>
     struct __numeric_traits_floating
     {
       static const int __max_digits10 = (2 + (std::__are_same<_Value, float>::__value ? 24 : std::__are_same<_Value, double>::__value ? 53 : 64) * 3010 / 10000);
-      static const bool __is_signed = true;
+      static const bool __is_signed_type = true;
       static const int __digits10 = (std::__are_same<_Value, float>::__value ? 6 : std::__are_same<_Value, double>::__value ? 15 : 18);
       static const int __max_exponent10 = (std::__are_same<_Value, float>::__value ? 38 : std::__are_same<_Value, double>::__value ? 308 : 4932);
     };
   template<typename _Value>
     const int __numeric_traits_floating<_Value>::__max_digits10;
   template<typename _Value>
-    const bool __numeric_traits_floating<_Value>::__is_signed;
+    const bool __numeric_traits_floating<_Value>::__is_signed_type;
   template<typename _Value>
     const int __numeric_traits_floating<_Value>::__digits10;
   template<typename _Value>
@@ -1513,8 +1513,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
       typedef typename iterator_traits<_II2>::value_type _ValueType2;
       const bool __simple =
  (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
-  && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
-  && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
+  && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed_type
+  && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed_type
   && __is_ptr<_II1>::__value
   && __is_ptr<_II2>::__value);
       return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
diff --git a/libstdc++-v3/include/bits/charconv.h b/libstdc++-v3/include/bits/charconv.h
index 20da8303f7a..1acf1e46e4c 100644
--- a/libstdc++-v3/include/bits/charconv.h
+++ b/libstdc++-v3/include/bits/charconv.h
@@ -46,7 +46,7 @@ namespace __detail
   // This accepts 128-bit integers even in strict mode.
   template<typename _Tp>
     constexpr bool __integer_to_chars_is_unsigned
-      = ! __gnu_cxx::__int_traits<_Tp>::__is_signed;
+      = ! __gnu_cxx::__int_traits<_Tp>::__is_signed_type;
 #endif
 
   // Generic implementation for arbitrary bases.
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index 6bfff7d6289..38a6920abe9 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -470,7 +470,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
 	bool __testfail = false;
 	bool __testoverflow = false;
 	const __unsigned_type __max =
-	  (__negative && __num_traits::__is_signed)
+	  (__negative && __num_traits::__is_signed_type)
 	  ? -static_cast<__unsigned_type>(__num_traits::__min)
 	  : __num_traits::__max;
 	const __unsigned_type __smax = __max / __base;
@@ -573,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
 	  }
 	else if (__testoverflow)
 	  {
-	    if (__negative && __num_traits::__is_signed)
+	    if (__negative && __num_traits::__is_signed_type)
 	      __v = __num_traits::__min;
 	    else
 	      __v = __num_traits::__max;
@@ -914,7 +914,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
 	    if (__v >= 0)
 	      {
 		if (bool(__flags & ios_base::showpos)
-		    && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
+		    && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed_type)
 		  *--__cs = __lit[__num_base::_S_oplus], ++__len;
 	      }
 	    else
diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h
index 7ccf930a6d4..73b808e57f3 100644
--- a/libstdc++-v3/include/bits/uniform_int_dist.h
+++ b/libstdc++-v3/include/bits/uniform_int_dist.h
@@ -258,8 +258,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{
 	  using _Up_traits = __gnu_cxx::__int_traits<_Up>;
 	  using _Wp_traits = __gnu_cxx::__int_traits<_Wp>;
-	  static_assert(!_Up_traits::__is_signed, "U must be unsigned");
-	  static_assert(!_Wp_traits::__is_signed, "W must be unsigned");
+	  static_assert(!_Up_traits::__is_signed_type, "U must be unsigned");
+	  static_assert(!_Wp_traits::__is_signed_type, "W must be unsigned");
 	  static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
 			"W must be twice as wide as U");
 
diff --git a/libstdc++-v3/include/ext/numeric_traits.h b/libstdc++-v3/include/ext/numeric_traits.h
index dcbc2d12927..c618f211775 100644
--- a/libstdc++-v3/include/ext/numeric_traits.h
+++ b/libstdc++-v3/include/ext/numeric_traits.h
@@ -67,15 +67,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // NB: these two are also available in std::numeric_limits as compile
       // time constants, but <limits> is big and we can avoid including it.
-      static const bool __is_signed = (_Value)(-1) < 0;
+      static const bool __is_signed_type = (_Value)(-1) < 0;
       static const int __digits
-	= __is_integer_nonstrict<_Value>::__width - __is_signed;
+	= __is_integer_nonstrict<_Value>::__width - __is_signed_type;
 
       // The initializers must be constants so that __max and __min are too.
-      static const _Value __max = __is_signed
+      static const _Value __max = __is_signed_type
 	? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
 	: ~(_Value)0;
-      static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
+      static const _Value __min = __is_signed_type ? -__max - 1 : (_Value)0;
     };
 
   template<typename _Value>
@@ -85,7 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     const _Value __numeric_traits_integer<_Value>::__max;
 
   template<typename _Value>
-    const bool __numeric_traits_integer<_Value>::__is_signed;
+    const bool __numeric_traits_integer<_Value>::__is_signed_type;
 
   template<typename _Value>
     const int __numeric_traits_integer<_Value>::__digits;
@@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static const int __max_digits10 = __glibcxx_max_digits10(_Value);
 
       // See above comment...
-      static const bool __is_signed = true;
+      static const bool __is_signed_type = true;
       static const int __digits10 = __glibcxx_digits10(_Value);
       static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
     };
@@ -170,7 +170,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     const int __numeric_traits_floating<_Value>::__max_digits10;
 
   template<typename _Value>
-    const bool __numeric_traits_floating<_Value>::__is_signed;
+    const bool __numeric_traits_floating<_Value>::__is_signed_type;
 
   template<typename _Value>
     const int __numeric_traits_floating<_Value>::__digits10;
@@ -210,7 +210,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct __numeric_traits_floating<__ibm128>
     {
       static const int __max_digits10 = 33;
-      static const bool __is_signed = true;
+      static const bool __is_signed_type = true;
       static const int __digits10 = 31;
       static const int __max_exponent10 = 308;
     };
@@ -224,7 +224,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct __numeric_traits_floating<__ieee128>
     {
       static const int __max_digits10 = 36;
-      static const bool __is_signed = true;
+      static const bool __is_signed_type = true;
       static const int __digits10 = 33;
       static const int __max_exponent10 = 4932;
     };
-- 
2.42.0



More information about the Gcc-patches mailing list