]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement __is_array built-in trait
authorKen Matsui <kmatsui@gcc.gnu.org>
Thu, 7 Dec 2023 05:32:59 +0000 (21:32 -0800)
committerJason Merrill <jason@redhat.com>
Sun, 10 Dec 2023 18:11:22 +0000 (13:11 -0500)
This patch implements built-in trait for std::is_array.

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
gcc/cp/constraint.cc
gcc/cp/cp-trait.def
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/ext/has-builtin-1.C
gcc/testsuite/g++.dg/ext/is_array.C [new file with mode: 0644]

index 29aa7bb3df842eb51e8c10f42f05d5f9971dd00e..d75132e8e82a1b7cb6ca0e48299cd43b2345e6b2 100644 (file)
@@ -3719,6 +3719,9 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_AGGREGATE:
       inform (loc, "  %qT is not an aggregate", t1);
       break;
+    case CPTK_IS_ARRAY:
+      inform (loc, "  %qT is not an array", t1);
+      break;
     case CPTK_IS_ASSIGNABLE:
       inform (loc, "  %qT is not assignable from %qT", t1, t2);
       break;
index 0e48e64b8dd70928c946fc928c84afb4823f8a49..759f10a35328bc09b87e9eedd56f000fdfd00e8f 100644 (file)
@@ -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_ARRAY, "__is_array", 1)
 DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
 DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
 DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
index 23d1f12e371441f8b2b42e31fd9025f7e16b8b35..399a33a82a81533434b440f24cc096051010d8f5 100644 (file)
@@ -12387,6 +12387,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_ARRAY:
+      return type_code1 == ARRAY_TYPE;
+
     case CPTK_IS_ASSIGNABLE:
       return is_xible (MODIFY_EXPR, type1, type2);
 
@@ -12614,6 +12617,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
        return error_mark_node;
       break;
 
+    case CPTK_IS_ARRAY:
     case CPTK_IS_CLASS:
     case CPTK_IS_ENUM:
     case CPTK_IS_SAME:
index 2223f08a6285c0621e58e6f96a8d767dcde36ae9..6b9437f7c472530ecc2485ab5e02785d9421b996 100644 (file)
@@ -56,6 +56,9 @@
 #if !__has_builtin (__is_aggregate)
 # error "__has_builtin (__is_aggregate) failed"
 #endif
+#if !__has_builtin (__is_array)
+# error "__has_builtin (__is_array) failed"
+#endif
 #if !__has_builtin (__is_assignable)
 # error "__has_builtin (__is_assignable) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
new file mode 100644 (file)
index 0000000..facfed5
--- /dev/null
@@ -0,0 +1,28 @@
+// { 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_array, int[2], true);
+SA_TEST_CATEGORY(__is_array, int[], true);
+SA_TEST_CATEGORY(__is_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_array, int[][3], true);
+SA_TEST_CATEGORY(__is_array, float*[2], true);
+SA_TEST_CATEGORY(__is_array, float*[], true);
+SA_TEST_CATEGORY(__is_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_array, ClassType, false);