Skip to content

Commit

Permalink
c++: Implement __is_bounded_array built-in trait
Browse files Browse the repository at this point in the history
This patch implements built-in trait for std::is_bounded_array.

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
  • Loading branch information
ken-matsui authored and ouuleilei-bot committed Sep 17, 2023
1 parent cd0d216 commit 2d5449b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gcc/cp/constraint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3736,6 +3736,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
case CPTK_IS_BOUNDED_ARRAY:
inform (loc, " %qT is not a bounded array", t1);
break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
Expand Down
1 change: 1 addition & 0 deletions gcc/cp/cp-trait.def
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
Expand Down
4 changes: 4 additions & 0 deletions gcc/cp/semantics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12019,6 +12019,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
|| DERIVED_FROM_P (type1, type2)));

case CPTK_IS_BOUNDED_ARRAY:
return type_code1 == ARRAY_TYPE && TYPE_DOMAIN (type1);

case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);

Expand Down Expand Up @@ -12248,6 +12251,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;

case CPTK_IS_ARRAY:
case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
Expand Down
3 changes: 3 additions & 0 deletions gcc/testsuite/g++.dg/ext/has-builtin-1.C
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
#if !__has_builtin (__is_bounded_array)
# error "__has_builtin (__is_bounded_array) failed"
#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
Expand Down
38 changes: 38 additions & 0 deletions gcc/testsuite/g++.dg/ext/is_bounded_array.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// { dg-do compile { target c++11 } }

#include <testsuite_tr1.h>

using namespace __gnu_test;

#define SA(X) static_assert((X),#X)

#define SA_TEST_CONST(TRAIT, TYPE, EXPECT) \
SA(TRAIT(TYPE) == EXPECT); \
SA(TRAIT(const TYPE) == EXPECT)

#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
SA(TRAIT(TYPE) == EXPECT); \
SA(TRAIT(const TYPE) == EXPECT); \
SA(TRAIT(volatile TYPE) == EXPECT); \
SA(TRAIT(const volatile TYPE) == EXPECT)

SA_TEST_CATEGORY(__is_bounded_array, int[2], true);
SA_TEST_CATEGORY(__is_bounded_array, int[], false);
SA_TEST_CATEGORY(__is_bounded_array, int[2][3], true);
SA_TEST_CATEGORY(__is_bounded_array, int[][3], false);
SA_TEST_CATEGORY(__is_bounded_array, float*[2], true);
SA_TEST_CATEGORY(__is_bounded_array, float*[], false);
SA_TEST_CATEGORY(__is_bounded_array, float*[2][3], true);
SA_TEST_CATEGORY(__is_bounded_array, float*[][3], false);
SA_TEST_CATEGORY(__is_bounded_array, ClassType[2], true);
SA_TEST_CATEGORY(__is_bounded_array, ClassType[], false);
SA_TEST_CATEGORY(__is_bounded_array, ClassType[2][3], true);
SA_TEST_CATEGORY(__is_bounded_array, ClassType[][3], false);
SA_TEST_CATEGORY(__is_bounded_array, int(*)[2], false);
SA_TEST_CATEGORY(__is_bounded_array, int(*)[], false);
SA_TEST_CATEGORY(__is_bounded_array, int(&)[2], false);
SA_TEST_CONST(__is_bounded_array, int(&)[], false);

// Sanity check.
SA_TEST_CATEGORY(__is_bounded_array, ClassType, false);
SA_TEST_CONST(__is_bounded_array, void(), false);

0 comments on commit 2d5449b

Please sign in to comment.