aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorKen Matsui <kmatsui@gcc.gnu.org>2024-02-15 07:17:31 -0800
committerKen Matsui <kmatsui@gcc.gnu.org>2024-05-10 18:17:44 -0700
commit37fad797adea340307fb33c3d7a0a17652063a33 (patch)
treee117e834ccdf799115147ee58299da0f4a1c38b0 /gcc/cp
parent142d1d8e057c0e6da7f4e6eba4cc265c556d2dd8 (diff)
downloadgcc-37fad797adea340307fb33c3d7a0a17652063a33.zip
gcc-37fad797adea340307fb33c3d7a0a17652063a33.tar.gz
gcc-37fad797adea340307fb33c3d7a0a17652063a33.tar.bz2
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>
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/constraint.cc3
-rw-r--r--gcc/cp/cp-trait.def1
-rw-r--r--gcc/cp/semantics.cc24
3 files changed, 25 insertions, 3 deletions
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d4cc885..c28d7bf 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 2d1cb7c..b1c875a 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 d499b85..bde26e4 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);
}