On 2/28/24 11:26, Ken Matsui wrote:
This patch implements built-in trait for std::rank.
__rank seems too short, maybe __array_rank?
Actually, it occurs to me that perhaps we should have been adding
__builtin to all of these rather than just __ and the library trait
name. I guess it's too late to do that for the GCC 14 traits, but we
could do it for this group?
gcc/cp/ChangeLog:
* cp-trait.def: Define __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 __rank.
* g++.dg/ext/rank.C: New test.
Signed-off-by: Ken Matsui <kmat...@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 23 ++++++++++++++++++++---
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/rank.C | 24 ++++++++++++++++++++++++
5 files changed, 51 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/rank.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 000df847342..23ea66d9c12 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3870,6 +3870,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 2d1cb7c227c..85056c8140b 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, "__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 45dc509855a..7242db75248 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12550,6 +12550,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree
type2)
case CPTK_IS_DEDUCIBLE:
return type_targs_deducible_from (type1, type2);
+ /* __rank is handled in finish_trait_expr. */
+ case CPTK_RANK:
This should have a gcc_unreachable.
+
#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
case CPTK_##CODE:
#include "cp-trait.def"
@@ -12622,7 +12625,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;
@@ -12714,6 +12720,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:
@@ -12745,8 +12752,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 rank = 0;
+ for (; TREE_CODE (type1) == ARRAY_TYPE; type1 = TREE_TYPE (type1))
+ ++rank;
+ val = build_int_cst (size_type_node, 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 3aca273aad6..7f7b27f7aa7 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -179,6 +179,9 @@
#if !__has_builtin (__is_volatile)
# error "__has_builtin (__is_volatile) failed"
#endif
+#if !__has_builtin (__rank)
+# error "__has_builtin (__rank) failed"
+#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) 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 00000000000..28894184387
--- /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(__rank(int) == 0);
+SA(__rank(int[2]) == 1);
+SA(__rank(int[][4]) == 2);
+SA(__rank(int[2][2][4][4][6][6]) == 6);
+SA(__rank(ClassType) == 0);
+SA(__rank(ClassType[2]) == 1);
+SA(__rank(ClassType[][4]) == 2);
+SA(__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(__rank(T)); }
+
+template void g<int>();