[05/14] c++: Implement __is_arithmetic built-in trait

Message ID 20240110194031.2384005-6-kmatsui@gcc.gnu.org
State New
Headers
Series Optimize integral-related type traits |

Commit Message

Ken Matsui Jan. 10, 2024, 9:22 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                      | 10 +++++---
 gcc/testsuite/g++.dg/ext/has-builtin-1.C |  3 +++
 gcc/testsuite/g++.dg/ext/is_arithmetic.C | 31 ++++++++++++++++++++++++
 5 files changed, 45 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
  

Patch

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index aca0b91711f..d25c3109789 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3722,6 +3722,9 @@  diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_ARRAY:
       inform (loc, "  %qT is not an array", t1);
       break;
+    case CPTK_IS_ARITHMETIC:
+      inform (loc, "  %qT is not an arithmetic type", t1);
+      break;
     case CPTK_IS_ASSIGNABLE:
       inform (loc, "  %qT is not assignable from %qT", t1, t2);
       break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index acf668d48ee..8df3ed8fedf 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -60,6 +60,7 @@  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_ARRAY, "__is_array", 1)
+DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
 DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
 DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
 DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index c052e47c204..882e0924ea4 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -2818,7 +2818,7 @@  perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
 
   if (fn && template_id && fn != error_mark_node)
     fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
-  
+
   return cp_expr (fn, loc);
 }
 
@@ -9325,7 +9325,7 @@  finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
 	      && DECL_P (t))
 	    bitmap_clear_bit (&aligned_head, DECL_UID (t));
-	    
+
 	  if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
 	    share_name = "threadprivate";
 	  else switch (cxx_omp_predetermined_sharing_1 (t))
@@ -12013,7 +12013,7 @@  pointer_interconvertible_base_of_p (tree base, tree derived)
   if (!NON_UNION_CLASS_TYPE_P (base)
       || !NON_UNION_CLASS_TYPE_P (derived))
     return false;
-    
+
   if (same_type_p (base, derived))
     return true;
 
@@ -12460,6 +12460,9 @@  trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_ARRAY:
       return type_code1 == ARRAY_TYPE;
 
+    case CPTK_IS_ARITHMETIC:
+      return integral_type_p (type1) || floating_point_type_p (type1);
+
     case CPTK_IS_ASSIGNABLE:
       return is_xible (MODIFY_EXPR, type1, type2);
 
@@ -12720,6 +12723,7 @@  finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
       break;
 
     case CPTK_IS_ARRAY:
+    case CPTK_IS_ARITHMETIC:
     case CPTK_IS_BOUNDED_ARRAY:
     case CPTK_IS_CLASS:
     case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 5dc70a19e79..a64a6a85bc2 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -59,6 +59,9 @@ 
 #if !__has_builtin (__is_array)
 # error "__has_builtin (__is_array) failed"
 #endif
+#if !__has_builtin (__is_arithmetic)
+# error "__has_builtin (__is_arithmetic) failed"
+#endif
 #if !__has_builtin (__is_assignable)
 # error "__has_builtin (__is_assignable) 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..719e976fbdf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
@@ -0,0 +1,31 @@ 
+// { dg-do compile { target c++11 } }
+
+#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.
+class ClassType { };
+SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);