public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r15-375] c++: Implement __array_rank built-in trait
@ 2024-05-11 1:19 Ken Matsui
0 siblings, 0 replies; only message in thread
From: Ken Matsui @ 2024-05-11 1:19 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:37fad797adea340307fb33c3d7a0a17652063a33
commit r15-375-g37fad797adea340307fb33c3d7a0a17652063a33
Author: Ken Matsui <kmatsui@gcc.gnu.org>
Date: Thu Feb 15 07:17:31 2024 -0800
c++: Implement __array_rank built-in trait
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>
Diff:
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 24 +++++++++++++++++++++---
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/rank.C | 24 ++++++++++++++++++++++++
5 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d4cc88504863..c28d7bf428ee 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -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);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 2d1cb7c227c0..b1c875a6e7d4 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -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)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index d499b858057c..bde26e4f2bb4 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -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);
}
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index d2e6a5b4dcb3..bcaa56a7bc5c 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -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
index 000000000000..0ab2f235036b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/rank.C
@@ -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>();
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-05-11 1:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-11 1:19 [gcc r15-375] c++: Implement __array_rank built-in trait Ken Matsui
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).