]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement __array_rank built-in trait
authorKen Matsui <kmatsui@gcc.gnu.org>
Thu, 15 Feb 2024 15:17:31 +0000 (07:17 -0800)
committerKen Matsui <kmatsui@gcc.gnu.org>
Sat, 11 May 2024 01:17:44 +0000 (18:17 -0700)
This patch implements built-in trait for std::rank.

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Reviewed-by: Jason Merrill <jason@redhat.com>
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/rank.C [new file with mode: 0644]

index d4cc88504863ebff85cb82382138574ed7fb49bb..c28d7bf428ee95919dcf3cb06f77b8e7de560f88 100644 (file)
@@ -3871,6 +3871,9 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_VOLATILE:
       inform (loc, "  %qT is not a volatile type", t1);
       break;
+    case CPTK_RANK:
+      inform (loc, "  %qT cannot yield a rank", t1);
+      break;
     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
       inform (loc, "  %qT is not a reference that binds to a temporary "
              "object of type %qT (direct-initialization)", t1, t2);
index 2d1cb7c227c0a25ac97acbe9f265b8defd08bcac..b1c875a6e7d445eae03fa51872a2411b3a607da0 100644 (file)
@@ -99,6 +99,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
 DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
 DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
 DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
+DEFTRAIT_EXPR (RANK, "__array_rank", 1)
 DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
 DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
 DEFTRAIT_TYPE (REMOVE_ALL_EXTENTS, "__remove_all_extents", 1)
index d499b858057c5340c1522f672d9c2469b1744319..bde26e4f2bb4b5f722fdc606039be94b9549c325 100644 (file)
@@ -12646,6 +12646,10 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_DEDUCIBLE:
       return type_targs_deducible_from (type1, type2);
 
+    /* __array_rank is handled in finish_trait_expr. */
+    case CPTK_RANK:
+      gcc_unreachable ();
+
 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
     case CPTK_##CODE:
 #include "cp-trait.def"
@@ -12769,7 +12773,10 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
   if (processing_template_decl)
     {
       tree trait_expr = make_node (TRAIT_EXPR);
-      TREE_TYPE (trait_expr) = boolean_type_node;
+      if (kind == CPTK_RANK)
+       TREE_TYPE (trait_expr) = size_type_node;
+      else
+       TREE_TYPE (trait_expr) = boolean_type_node;
       TRAIT_EXPR_TYPE1 (trait_expr) = type1;
       TRAIT_EXPR_TYPE2 (trait_expr) = type2;
       TRAIT_EXPR_KIND (trait_expr) = kind;
@@ -12862,6 +12869,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_UNBOUNDED_ARRAY:
     case CPTK_IS_UNION:
     case CPTK_IS_VOLATILE:
+    case CPTK_RANK:
       break;
 
     case CPTK_IS_LAYOUT_COMPATIBLE:
@@ -12893,8 +12901,18 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
       gcc_unreachable ();
     }
 
-  tree val = (trait_expr_value (kind, type1, type2)
-             ? boolean_true_node : boolean_false_node);
+  tree val;
+  if (kind == CPTK_RANK)
+    {
+      size_t __array_rank = 0;
+      for (; TREE_CODE (type1) == ARRAY_TYPE; type1 = TREE_TYPE (type1))
+       ++__array_rank;
+      val = build_int_cst (size_type_node, __array_rank);
+    }
+  else
+    val = (trait_expr_value (kind, type1, type2)
+          ? boolean_true_node : boolean_false_node);
+
   return maybe_wrap_with_location (val, loc);
 }
 
index d2e6a5b4dcb3cf19c6787ae9ecec2429cf53a1b7..bcaa56a7bc5c003715744f85215a60484342fdf9 100644 (file)
@@ -11,6 +11,9 @@
 #if !__has_builtin (__add_rvalue_reference)
 # error "__has_builtin (__add_rvalue_reference) failed"
 #endif
+#if !__has_builtin (__array_rank)
+# error "__has_builtin (__array_rank) failed"
+#endif
 #if !__has_builtin (__builtin_addressof)
 # error "__has_builtin (__builtin_addressof) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/rank.C b/gcc/testsuite/g++.dg/ext/rank.C
new file mode 100644 (file)
index 0000000..0ab2f23
--- /dev/null
@@ -0,0 +1,24 @@
+// { dg-do compile { target c++11 } }
+
+#include <cstddef>
+
+#define SA(X) static_assert((X),#X)
+
+class ClassType { };
+
+SA(__array_rank(int) == 0);
+SA(__array_rank(int[2]) == 1);
+SA(__array_rank(int[][4]) == 2);
+SA(__array_rank(int[2][2][4][4][6][6]) == 6);
+SA(__array_rank(ClassType) == 0);
+SA(__array_rank(ClassType[2]) == 1);
+SA(__array_rank(ClassType[][4]) == 2);
+SA(__array_rank(ClassType[2][2][4][4][6][6]) == 6);
+
+template<class T> void f(T) = delete;
+void f(size_t);
+
+template<class T>
+void g() { f(__array_rank(T)); }
+
+template void g<int>();