c++: Implement __is_pointer built-in trait

This patch implements built-in trait for std::is_pointer.

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __is_pointer.
	Arrange the order lexically around __is_pointer.
	* g++.dg/ext/is_pointer.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Reviewed-by: Jason Merrill <jason@redhat.com>
This commit is contained in:
Ken Matsui 2023-07-09 17:49:08 -07:00
parent 9b51b3e79e
commit cb5d904c77
No known key found for this signature in database
5 changed files with 65 additions and 3 deletions

View File

@ -3829,6 +3829,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POD:
inform (loc, " %qT is not a POD type", t1);
break;
case CPTK_IS_POINTER:
inform (loc, " %qT is not a pointer", t1);
break;
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;

View File

@ -82,6 +82,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_OBJECT, "__is_object", 1)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)

View File

@ -12588,6 +12588,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POD:
return pod_type_p (type1);
case CPTK_IS_POINTER:
return TYPE_PTR_P (type1);
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
@ -12827,6 +12830,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_OBJECT:
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:

View File

@ -119,12 +119,15 @@
#if !__has_builtin (__is_object)
# error "__has_builtin (__is_object) failed"
#endif
#if !__has_builtin (__is_pointer_interconvertible_base_of)
# error "__has_builtin (__is_pointer_interconvertible_base_of) failed"
#endif
#if !__has_builtin (__is_pod)
# error "__has_builtin (__is_pod) failed"
#endif
#if !__has_builtin (__is_pointer)
# error "__has_builtin (__is_pointer) failed"
#endif
#if !__has_builtin (__is_pointer_interconvertible_base_of)
# error "__has_builtin (__is_pointer_interconvertible_base_of) failed"
#endif
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif

View File

@ -0,0 +1,51 @@
// { dg-do compile { target c++11 } }
#define SA(X) static_assert((X),#X)
SA(!__is_pointer(int));
SA(__is_pointer(int*));
SA(__is_pointer(int**));
SA(__is_pointer(const int*));
SA(__is_pointer(const int**));
SA(__is_pointer(int* const));
SA(__is_pointer(int** const));
SA(__is_pointer(int* const* const));
SA(__is_pointer(volatile int*));
SA(__is_pointer(volatile int**));
SA(__is_pointer(int* volatile));
SA(__is_pointer(int** volatile));
SA(__is_pointer(int* volatile* volatile));
SA(__is_pointer(const volatile int*));
SA(__is_pointer(const volatile int**));
SA(__is_pointer(const int* volatile));
SA(__is_pointer(volatile int* const));
SA(__is_pointer(int* const volatile));
SA(__is_pointer(const int** volatile));
SA(__is_pointer(volatile int** const));
SA(__is_pointer(int** const volatile));
SA(__is_pointer(int* const* const volatile));
SA(__is_pointer(int* volatile* const volatile));
SA(__is_pointer(int* const volatile* const volatile));
SA(!__is_pointer(int&));
SA(!__is_pointer(const int&));
SA(!__is_pointer(volatile int&));
SA(!__is_pointer(const volatile int&));
SA(!__is_pointer(int&&));
SA(!__is_pointer(const int&&));
SA(!__is_pointer(volatile int&&));
SA(!__is_pointer(const volatile int&&));
SA(!__is_pointer(int[3]));
SA(!__is_pointer(const int[3]));
SA(!__is_pointer(volatile int[3]));
SA(!__is_pointer(const volatile int[3]));
SA(!__is_pointer(int(int)));
SA(__is_pointer(int(*const)(int)));
SA(__is_pointer(int(*volatile)(int)));
SA(__is_pointer(int(*const volatile)(int)));