This patch implements built-in trait for std::rank. gcc/cp/ChangeLog:
* cp-trait.def: Define __rank. * semantics.cc (trait_expr_value): Handle CPTK_RANK. (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/cp-trait.def | 1 + gcc/cp/semantics.cc | 18 ++++++++++++++++-- gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++ gcc/testsuite/g++.dg/ext/rank.C | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/rank.C diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 11270f3ae6b..3d5a7970563 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -95,6 +95,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2) DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1) DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1) DEFTRAIT_EXPR (IS_UNION, "__is_union", 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 256e7ef8166..4f285909b83 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12538,6 +12538,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: + #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ case CPTK_##CODE: #include "cp-trait.def" @@ -12698,6 +12701,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_SAME: case CPTK_IS_SCOPED_ENUM: case CPTK_IS_UNION: + case CPTK_RANK: break; case CPTK_IS_LAYOUT_COMPATIBLE: @@ -12729,8 +12733,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 5b590db1cf6..a00193c1a81 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -167,6 +167,9 @@ #if !__has_builtin (__is_union) # error "__has_builtin (__is_union) 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..bab062d776e --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/rank.C @@ -0,0 +1,14 @@ +// { dg-do compile { target c++11 } } + +#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); -- 2.43.0