[v20,31/40] c++: Implement __is_arithmetic built-in trait

Message ID 20231016001227.2717180-32-kmatsui@gcc.gnu.org
State Unresolved
Headers
Series Optimize type traits performance |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Ken Matsui Oct. 16, 2023, 12:10 a.m. UTC
  This patch implements built-in trait for std::is_arithmetic.

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
	* g++.dg/ext/is_arithmetic.C: New test.

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_arithmetic.C | 33 ++++++++++++++++++++++++
 5 files changed, 44 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
  

Comments

Patrick Palka Oct. 16, 2023, 5:16 p.m. UTC | #1
On Sun, 15 Oct 2023, Ken Matsui wrote:

> This patch implements built-in trait for std::is_arithmetic.
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-trait.def: Define __is_arithmetic.
> 	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC.
> 	* semantics.cc (trait_expr_value): Likewise.
> 	(finish_trait_expr): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
> 	* g++.dg/ext/is_arithmetic.C: New test.
> 
> 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_arithmetic.C | 33 ++++++++++++++++++++++++
>  5 files changed, 44 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
> 
> diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> index c9d627fa782..3a7f968eae8 100644
> --- a/gcc/cp/constraint.cc
> +++ b/gcc/cp/constraint.cc
> @@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
>      case CPTK_IS_AGGREGATE:
>        inform (loc, "  %qT is not an aggregate", t1);
>        break;
> +    case CPTK_IS_ARITHMETIC:
> +      inform (loc, "  %qT is not an arithmetic type", t1);
> +      break;
>      case CPTK_IS_ARRAY:
>        inform (loc, "  %qT is not an array", t1);
>        break;
> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> index c60724e869e..b2be7b7bbd7 100644
> --- a/gcc/cp/cp-trait.def
> +++ b/gcc/cp/cp-trait.def
> @@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
>  DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
>  DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
>  DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
> +DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
>  DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
>  DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
>  DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 83ed674b9d4..deab0134509 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12143,6 +12143,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
>      case CPTK_IS_AGGREGATE:
>        return CP_AGGREGATE_TYPE_P (type1);
>  
> +    case CPTK_IS_ARITHMETIC:
> +      return ARITHMETIC_TYPE_P (type1);
> +

For built-ins corresponding to is_arithmetic and other standard traits
defined in terms of it (e.g.  is_scalar, is_unsigned, is_signed,
is_fundamental) we need to make sure we preserve their behavior for
__int128, which IIUC is currently recognized as an integral type
(according to std::is_integral) only in GNU mode.

This'll probably be subtle to get right, so if you don't mind let's
split out the work for those built-in traits into a separate patch
series in order to ease review of the main patch series.

>      case CPTK_IS_ARRAY:
>        return type_code1 == ARRAY_TYPE;
>  
> @@ -12406,6 +12409,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
>  	return error_mark_node;
>        break;
>  
> +    case CPTK_IS_ARITHMETIC:
>      case CPTK_IS_ARRAY:
>      case CPTK_IS_BOUNDED_ARRAY:
>      case CPTK_IS_CLASS:
> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> index efce04fd09d..4bc85f4babb 100644
> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> @@ -56,6 +56,9 @@
>  #if !__has_builtin (__is_aggregate)
>  # error "__has_builtin (__is_aggregate) failed"
>  #endif
> +#if !__has_builtin (__is_arithmetic)
> +# error "__has_builtin (__is_arithmetic) failed"
> +#endif
>  #if !__has_builtin (__is_array)
>  # error "__has_builtin (__is_array) failed"
>  #endif
> diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
> new file mode 100644
> index 00000000000..fd35831f646
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
> @@ -0,0 +1,33 @@
> +// { 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, TYPE, EXPECT)	\
> +  SA(TRAIT(TYPE) == EXPECT);					\
> +  SA(TRAIT(const TYPE) == EXPECT);				\
> +  SA(TRAIT(volatile TYPE) == EXPECT);			\
> +  SA(TRAIT(const volatile TYPE) == EXPECT)
> +
> +SA_TEST_CATEGORY(__is_arithmetic, void, false);
> +
> +SA_TEST_CATEGORY(__is_arithmetic, char, true);
> +SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
> +SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
> +SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
> +SA_TEST_CATEGORY(__is_arithmetic, short, true);
> +SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
> +SA_TEST_CATEGORY(__is_arithmetic, int, true);
> +SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
> +SA_TEST_CATEGORY(__is_arithmetic, long, true);
> +SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
> +SA_TEST_CATEGORY(__is_arithmetic, long long, true);
> +SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
> +SA_TEST_CATEGORY(__is_arithmetic, float, true);
> +SA_TEST_CATEGORY(__is_arithmetic, double, true);
> +SA_TEST_CATEGORY(__is_arithmetic, long double, true);
> +
> +// Sanity check.
> +SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);
> -- 
> 2.42.0
> 
>
  
Ken Matsui Oct. 16, 2023, 8:25 p.m. UTC | #2
On Mon, Oct 16, 2023 at 10:16 AM Patrick Palka <ppalka@redhat.com> wrote:
>
> On Sun, 15 Oct 2023, Ken Matsui wrote:
>
> > This patch implements built-in trait for std::is_arithmetic.
> >
> > gcc/cp/ChangeLog:
> >
> >       * cp-trait.def: Define __is_arithmetic.
> >       * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC.
> >       * semantics.cc (trait_expr_value): Likewise.
> >       (finish_trait_expr): Likewise.
> >
> > gcc/testsuite/ChangeLog:
> >
> >       * g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
> >       * g++.dg/ext/is_arithmetic.C: New test.
> >
> > 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_arithmetic.C | 33 ++++++++++++++++++++++++
> >  5 files changed, 44 insertions(+)
> >  create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
> >
> > diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> > index c9d627fa782..3a7f968eae8 100644
> > --- a/gcc/cp/constraint.cc
> > +++ b/gcc/cp/constraint.cc
> > @@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
> >      case CPTK_IS_AGGREGATE:
> >        inform (loc, "  %qT is not an aggregate", t1);
> >        break;
> > +    case CPTK_IS_ARITHMETIC:
> > +      inform (loc, "  %qT is not an arithmetic type", t1);
> > +      break;
> >      case CPTK_IS_ARRAY:
> >        inform (loc, "  %qT is not an array", t1);
> >        break;
> > diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> > index c60724e869e..b2be7b7bbd7 100644
> > --- a/gcc/cp/cp-trait.def
> > +++ b/gcc/cp/cp-trait.def
> > @@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
> >  DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
> >  DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
> >  DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
> > +DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
> >  DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
> >  DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
> >  DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
> > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> > index 83ed674b9d4..deab0134509 100644
> > --- a/gcc/cp/semantics.cc
> > +++ b/gcc/cp/semantics.cc
> > @@ -12143,6 +12143,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
> >      case CPTK_IS_AGGREGATE:
> >        return CP_AGGREGATE_TYPE_P (type1);
> >
> > +    case CPTK_IS_ARITHMETIC:
> > +      return ARITHMETIC_TYPE_P (type1);
> > +
>
> For built-ins corresponding to is_arithmetic and other standard traits
> defined in terms of it (e.g.  is_scalar, is_unsigned, is_signed,
> is_fundamental) we need to make sure we preserve their behavior for
> __int128, which IIUC is currently recognized as an integral type
> (according to std::is_integral) only in GNU mode.
>
> This'll probably be subtle to get right, so if you don't mind let's
> split out the work for those built-in traits into a separate patch
> series in order to ease review of the main patch series.
>

I agree. I am postponing work on integral-related traits, so isolating
non-ready-for-review patches makes a lot of sense.

> >      case CPTK_IS_ARRAY:
> >        return type_code1 == ARRAY_TYPE;
> >
> > @@ -12406,6 +12409,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
> >       return error_mark_node;
> >        break;
> >
> > +    case CPTK_IS_ARITHMETIC:
> >      case CPTK_IS_ARRAY:
> >      case CPTK_IS_BOUNDED_ARRAY:
> >      case CPTK_IS_CLASS:
> > diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > index efce04fd09d..4bc85f4babb 100644
> > --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > @@ -56,6 +56,9 @@
> >  #if !__has_builtin (__is_aggregate)
> >  # error "__has_builtin (__is_aggregate) failed"
> >  #endif
> > +#if !__has_builtin (__is_arithmetic)
> > +# error "__has_builtin (__is_arithmetic) failed"
> > +#endif
> >  #if !__has_builtin (__is_array)
> >  # error "__has_builtin (__is_array) failed"
> >  #endif
> > diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
> > new file mode 100644
> > index 00000000000..fd35831f646
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
> > @@ -0,0 +1,33 @@
> > +// { 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, TYPE, EXPECT)        \
> > +  SA(TRAIT(TYPE) == EXPECT);                                 \
> > +  SA(TRAIT(const TYPE) == EXPECT);                           \
> > +  SA(TRAIT(volatile TYPE) == EXPECT);                        \
> > +  SA(TRAIT(const volatile TYPE) == EXPECT)
> > +
> > +SA_TEST_CATEGORY(__is_arithmetic, void, false);
> > +
> > +SA_TEST_CATEGORY(__is_arithmetic, char, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, short, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, int, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, long, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, long long, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, float, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, double, true);
> > +SA_TEST_CATEGORY(__is_arithmetic, long double, true);
> > +
> > +// Sanity check.
> > +SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);
> > --
> > 2.42.0
> >
> >
>
  

Patch

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9d627fa782..3a7f968eae8 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@  diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_AGGREGATE:
       inform (loc, "  %qT is not an aggregate", t1);
       break;
+    case CPTK_IS_ARITHMETIC:
+      inform (loc, "  %qT is not an arithmetic type", t1);
+      break;
     case CPTK_IS_ARRAY:
       inform (loc, "  %qT is not an array", t1);
       break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index c60724e869e..b2be7b7bbd7 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@  DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
 DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
 DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
 DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
 DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
 DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
 DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 83ed674b9d4..deab0134509 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12143,6 +12143,9 @@  trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_AGGREGATE:
       return CP_AGGREGATE_TYPE_P (type1);
 
+    case CPTK_IS_ARITHMETIC:
+      return ARITHMETIC_TYPE_P (type1);
+
     case CPTK_IS_ARRAY:
       return type_code1 == ARRAY_TYPE;
 
@@ -12406,6 +12409,7 @@  finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
 	return error_mark_node;
       break;
 
+    case CPTK_IS_ARITHMETIC:
     case CPTK_IS_ARRAY:
     case CPTK_IS_BOUNDED_ARRAY:
     case CPTK_IS_CLASS:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index efce04fd09d..4bc85f4babb 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@ 
 #if !__has_builtin (__is_aggregate)
 # error "__has_builtin (__is_aggregate) failed"
 #endif
+#if !__has_builtin (__is_arithmetic)
+# error "__has_builtin (__is_arithmetic) failed"
+#endif
 #if !__has_builtin (__is_array)
 # error "__has_builtin (__is_array) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
new file mode 100644
index 00000000000..fd35831f646
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
@@ -0,0 +1,33 @@ 
+// { 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, TYPE, EXPECT)	\
+  SA(TRAIT(TYPE) == EXPECT);					\
+  SA(TRAIT(const TYPE) == EXPECT);				\
+  SA(TRAIT(volatile TYPE) == EXPECT);			\
+  SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_arithmetic, void, false);
+
+SA_TEST_CATEGORY(__is_arithmetic, char, true);
+SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
+SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
+SA_TEST_CATEGORY(__is_arithmetic, short, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
+SA_TEST_CATEGORY(__is_arithmetic, int, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
+SA_TEST_CATEGORY(__is_arithmetic, long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
+SA_TEST_CATEGORY(__is_arithmetic, long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, float, true);
+SA_TEST_CATEGORY(__is_arithmetic, double, true);
+SA_TEST_CATEGORY(__is_arithmetic, long double, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);