[PATCH] compiler built in is_scalar, use built-in is_scalar in libstdc++ std::is_scalar

2023-03-15 Thread Berke Yavas via Gcc-patches
From: Berke 

Signed-off-by: Berke Yavas 
---
 gcc/cp/constraint.cc|  3 +++
 gcc/cp/cp-trait.def |  1 +
 gcc/cp/cxx-pretty-print.cc  |  3 ++-
 gcc/cp/parser.cc|  1 +
 gcc/cp/semantics.cc |  4 
 gcc/testsuite/g++.dg/ext/has-builtin-1.C|  4 
 gcc/testsuite/g++.dg/ext/is_scalar.C| 21 +
 gcc/testsuite/g++.dg/tm/pr46567.C   | 10 +-
 gcc/testsuite/g++.dg/torture/pr57107.C  |  4 ++--
 libstdc++-v3/include/bits/cpp_type_traits.h |  8 
 libstdc++-v3/include/bits/stl_algobase.h|  8 
 libstdc++-v3/include/bits/valarray_array.h  |  2 +-
 libstdc++-v3/include/std/type_traits|  7 +++
 13 files changed, 59 insertions(+), 17 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 9374327008b..f799ba2efd5 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3747,6 +3747,9 @@ diagnose_trait_expr (tree expr, tree args)
 case CPTK_IS_UNION:
   inform (loc, "  %qT is not a union", t1);
   break;
+case CPTK_IS_SCALAR:
+  inform(loc, "  %qT is not a scalar type", t1);
+  break;
 case CPTK_IS_AGGREGATE:
   inform (loc, "  %qT is not an aggregate", t1);
   break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 823899a26c5..8dace289e88 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -82,6 +82,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, 
"__is_trivially_assignable", 2)
 DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
 DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
 DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_SCALAR, "__is_scalar", 1)
 DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, 
"__reference_constructs_from_temporary", 2)
 DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, 
"__reference_converts_from_temporary", 2)
 
diff --git a/gcc/cp/cxx-pretty-print.cc b/gcc/cp/cxx-pretty-print.cc
index bea52a608f1..a79359430a5 100644
--- a/gcc/cp/cxx-pretty-print.cc
+++ b/gcc/cp/cxx-pretty-print.cc
@@ -437,7 +437,8 @@ pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t)
  __is_polymorphic ( type-id )
  __is_std_layout ( type-id )
  __is_trivial ( type-id )
- __is_union ( type-id )  */
+ __is_union ( type-id )
+ __is_scalar ( type-id )  */
 
 void
 cxx_pretty_printer::primary_expression (tree t)
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index b00a6cd5b8b..8465d4fbcdb 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -5577,6 +5577,7 @@ cp_parser_fold_expression (cp_parser *parser, tree expr1)
  __is_std_layout ( type-id )
  __is_trivial ( type-id )
  __is_union ( type-id )
+ __is_scalar ( type-id )
 
Objective-C++ Extension:
 
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index db982d594e6..81b13d1ae5c 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12025,6 +12025,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree 
type2)
 case CPTK_IS_UNION:
   return type_code1 == UNION_TYPE;
 
+case CPTK_IS_SCALAR:
+  return SCALAR_TYPE_P (type1);
+
 case CPTK_IS_ASSIGNABLE:
   return is_xible (MODIFY_EXPR, type1, type2);
 
@@ -12190,6 +12193,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, 
tree type1, tree type2)
 case CPTK_IS_CLASS:
 case CPTK_IS_ENUM:
 case CPTK_IS_UNION:
+case CPTK_IS_SCALAR:
 case CPTK_IS_SAME:
   break;
 
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C 
b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index f343e153e56..4bf06a7a438 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -146,3 +146,7 @@
 #if !__has_builtin (__remove_cvref)
 # error "__has_builtin (__remove_cvref) failed"
 #endif
+#if !__has_builtin (__is_scalar)
+# error "__has_builtin (__is_scalar) failed"
+#endif
+
diff --git a/gcc/testsuite/g++.dg/ext/is_scalar.C 
b/gcc/testsuite/g++.dg/ext/is_scalar.C
new file mode 100644
index 000..1d58f6ec475
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scalar.C
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+struct S { int m; };
+
+enum class E {
+  e
+};
+
+int main() {
+  char* pNull= nullptr;
+
+  SA(__is_scalar(decltype(pNull)));
+  SA(__is_scalar(int));
+  SA(__is_scalar(double));
+  SA(__is_scalar(E));
+  SA(__is_scalar(char const *));
+  SA(!__is_scalar(struct S));
+  return 0;
+}
\ No newline at end of file
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C 
b/gcc/testsuite/g++.dg/tm/pr46567.C
index 6d791484448..760c7fd6cab 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -225,7 +225,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
 

[PATCH] c++, libstdc++: new compiler built in is_scalar, use built-in is_scalar in libstdc++ std::is_scalar

2023-03-15 Thread Berke Yavas via Gcc-patches
Have implemented a new compiler built-in for the scalar types(is_scalar). 
Changed the libstdc++
std::is_scalar implementation to use this new compiler built_in when available.

tested on x86_64-redhat-linux. Compared test results and configured with 
--enable-bootstrap

gcc/cp/ChangeLog:

* constraint.cc (diagnose_trait_expr): Add note for CPTK_IS_SCALAR
failing.
* cp-trait.def (IS_SCALAR): Define IS_SCALAR trait.
* cxx-pretty-print.cc (pp_cxx_userdef_literal): Add __is_scalar to
comment.
* parser.cc (cp_parser_fold_expression): Likewise.
* semantics.cc (trait_expr_value): Implement built-in CPTK_IS_SCALAR.
(finish_trait_expr): Add CPTK_IS_SCALAR.

libstdc++-v3/ChangeLog:

* include/bits/cpp_type_traits.h (__is_scalar): Rename to
__is_arith_or_ptr.
* include/bits/stl_algobase.h (__fill_a1, __fill_n_a1): Adjust.
* include/bits/valarray_array.h: Likewise..
* include/std/type_traits (is_scalar): Use built-in __is_scalar if
available.

gcc/testsuite/ChangeLog:

* g++.dg/ext/has-builtin-1.C: Check __is_scalar.
* g++.dg/tm/pr46567.C: Rename __is_scalar to __is_arith_or_ptr to
avoid collision with built-in.
* g++.dg/torture/pr57107.C: Likewise.
* g++.dg/ext/is_scalar.C: New test.

Signed-off-by: Berke Yavas 
---
 gcc/cp/constraint.cc|  3 +++
 gcc/cp/cp-trait.def |  1 +
 gcc/cp/cxx-pretty-print.cc  |  3 ++-
 gcc/cp/parser.cc|  1 +
 gcc/cp/semantics.cc |  4 
 gcc/testsuite/g++.dg/ext/has-builtin-1.C|  4 
 gcc/testsuite/g++.dg/ext/is_scalar.C| 21 +
 gcc/testsuite/g++.dg/tm/pr46567.C   | 10 +-
 gcc/testsuite/g++.dg/torture/pr57107.C  |  4 ++--
 libstdc++-v3/include/bits/cpp_type_traits.h |  4 ++--
 libstdc++-v3/include/bits/stl_algobase.h|  8 
 libstdc++-v3/include/bits/valarray_array.h  |  2 +-
 libstdc++-v3/include/std/type_traits|  7 +++
 13 files changed, 57 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 9374327008b..f799ba2efd5 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3747,6 +3747,9 @@ diagnose_trait_expr (tree expr, tree args)
 case CPTK_IS_UNION:
   inform (loc, "  %qT is not a union", t1);
   break;
+case CPTK_IS_SCALAR:
+  inform(loc, "  %qT is not a scalar type", t1);
+  break;
 case CPTK_IS_AGGREGATE:
   inform (loc, "  %qT is not an aggregate", t1);
   break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 823899a26c5..8dace289e88 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -82,6 +82,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, 
"__is_trivially_assignable", 2)
 DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
 DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
 DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_SCALAR, "__is_scalar", 1)
 DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, 
"__reference_constructs_from_temporary", 2)
 DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, 
"__reference_converts_from_temporary", 2)
 
diff --git a/gcc/cp/cxx-pretty-print.cc b/gcc/cp/cxx-pretty-print.cc
index bea52a608f1..a79359430a5 100644
--- a/gcc/cp/cxx-pretty-print.cc
+++ b/gcc/cp/cxx-pretty-print.cc
@@ -437,7 +437,8 @@ pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t)
  __is_polymorphic ( type-id )
  __is_std_layout ( type-id )
  __is_trivial ( type-id )
- __is_union ( type-id )  */
+ __is_union ( type-id )
+ __is_scalar ( type-id )  */
 
 void
 cxx_pretty_printer::primary_expression (tree t)
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index b00a6cd5b8b..8465d4fbcdb 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -5577,6 +5577,7 @@ cp_parser_fold_expression (cp_parser *parser, tree expr1)
  __is_std_layout ( type-id )
  __is_trivial ( type-id )
  __is_union ( type-id )
+ __is_scalar ( type-id )
 
Objective-C++ Extension:
 
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index db982d594e6..81b13d1ae5c 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12025,6 +12025,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree 
type2)
 case CPTK_IS_UNION:
   return type_code1 == UNION_TYPE;
 
+case CPTK_IS_SCALAR:
+  return SCALAR_TYPE_P (type1);
+
 case CPTK_IS_ASSIGNABLE:
   return is_xible (MODIFY_EXPR, type1, type2);
 
@@ -12190,6 +12193,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, 
tree type1, tree type2)
 case CPTK_IS_CLASS:
 case CPTK_IS_ENUM:
 case CPTK_IS_UNION:
+case CPTK_IS_SCALAR:
 case CPTK_IS_SAME:
   break;
 
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C 
b/gcc/te