[gcc r14-6374] c++: Implement __is_member_function_pointer built-in trait
Jason Merrill
jason@gcc.gnu.org
Sun Dec 10 18:12:59 GMT 2023
https://gcc.gnu.org/g:7c62a418f8a14149f28956ba779538000f4ea358
commit r14-6374-g7c62a418f8a14149f28956ba779538000f4ea358
Author: Ken Matsui <kmatsui@gcc.gnu.org>
Date: Wed Dec 6 21:33:07 2023 -0800
c++: Implement __is_member_function_pointer built-in trait
This patch implements built-in trait for std::is_member_function_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_function_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_FUNCTION_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_member_function_pointer.
* g++.dg/ext/is_member_function_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Diff:
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
.../g++.dg/ext/is_member_function_pointer.C | 31 ++++++++++++++++++++++
gcc/cp/cp-trait.def | 1 +
5 files changed, 42 insertions(+)
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index fb150e02ea9..1efc7983039 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3758,6 +3758,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ inform (loc, " %qT is not a member function pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index a462ed35d89..e8224e81933 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12425,6 +12425,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ return TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12630,6 +12633,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 349fae7104e..fb17680d3b0 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -92,6 +92,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_function_pointer)
+# error "__has_builtin (__is_member_function_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
new file mode 100644
index 00000000000..555123e8f07
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(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)
+
+// Positive tests.
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int), true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int) const, true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (float, ...), true);
+SA_TEST_FN(__is_member_function_pointer, ClassType (ClassType::*) (ClassType), true);
+SA_TEST_FN(__is_member_function_pointer, float (ClassType::*) (int, float, int[], int&), true);
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_member_function_pointer, int (ClassType::*), false);
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType (ClassType::*), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType, false);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index e17f5eaeac4..03a5cc28020 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -71,6 +71,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
More information about the Gcc-cvs
mailing list