[v19,27/40] c++: Implement __remove_pointer built-in trait

Message ID 20231013223957.1634024-28-kmatsui@gcc.gnu.org
State Unresolved
Headers
Series Optimize type traits performance |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Ken Matsui Oct. 13, 2023, 10:37 p.m. UTC
  This patch implements built-in trait for std::remove_pointer.

gcc/cp/ChangeLog:

	* cp-trait.def: Define __remove_pointer.
	* cp-trait.gperf: Reflect cp-trait.def change.
	* cp-trait.h: Likewise.
	* semantics.cc (finish_trait_type): Handle CPTK_REMOVE_POINTER.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/cp/cp-trait.def                       |  1 +
 gcc/cp/cp-trait.gperf                     |  1 +
 gcc/cp/cp-trait.h                         | 32 +++++++-------
 gcc/cp/semantics.cc                       |  5 +++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C  |  3 ++
 gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 +++++++++++++++++++++++
 6 files changed, 78 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
  

Patch

diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index fa79bc0c68c..2add97ae749 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -97,6 +97,7 @@  DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_tempo
 DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
 DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
 DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
+DEFTRAIT_TYPE (REMOVE_POINTER, "__remove_pointer", 1)
 DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
 DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
 DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
diff --git a/gcc/cp/cp-trait.gperf b/gcc/cp/cp-trait.gperf
index 771242a7f45..8fbd67788d5 100644
--- a/gcc/cp/cp-trait.gperf
+++ b/gcc/cp/cp-trait.gperf
@@ -77,6 +77,7 @@  struct cp_trait {
 "__reference_converts_from_temporary", CPTK_REF_CONVERTS_FROM_TEMPORARY, 2, false
 "__remove_cv", CPTK_REMOVE_CV, 1, true
 "__remove_cvref", CPTK_REMOVE_CVREF, 1, true
+"__remove_pointer", CPTK_REMOVE_POINTER, 1, true
 "__remove_reference", CPTK_REMOVE_REFERENCE, 1, true
 "__type_pack_element", CPTK_TYPE_PACK_ELEMENT, -1, true
 "__underlying_type", CPTK_UNDERLYING_TYPE, 1, true
diff --git a/gcc/cp/cp-trait.h b/gcc/cp/cp-trait.h
index b6db58e93c9..ad2c2a2d250 100644
--- a/gcc/cp/cp-trait.h
+++ b/gcc/cp/cp-trait.h
@@ -116,7 +116,7 @@  cp_trait_lookup::find (const char *str, size_t len)
 {
   enum
     {
-      TOTAL_KEYWORDS = 56,
+      TOTAL_KEYWORDS = 57,
       MIN_WORD_LENGTH = 7,
       MAX_WORD_LENGTH = 37,
       MIN_HASH_VALUE = 7,
@@ -125,7 +125,7 @@  cp_trait_lookup::find (const char *str, size_t len)
 
   static const struct cp_trait wordlist[] =
     {
-#line 84 "../../gcc/cp/cp-trait.gperf"
+#line 85 "../../gcc/cp/cp-trait.gperf"
       {"__bases", CPTK_BASES, 1, true},
 #line 51 "../../gcc/cp/cp-trait.gperf"
       {"__is_enum", CPTK_IS_ENUM, 1, false},
@@ -137,17 +137,19 @@  cp_trait_lookup::find (const char *str, size_t len)
       {"__remove_cvref", CPTK_REMOVE_CVREF, 1, true},
 #line 50 "../../gcc/cp/cp-trait.gperf"
       {"__is_empty", CPTK_IS_EMPTY, 1, false},
+#line 80 "../../gcc/cp/cp-trait.gperf"
+      {"__remove_pointer", CPTK_REMOVE_POINTER, 1, true},
 #line 69 "../../gcc/cp/cp-trait.gperf"
       {"__is_trivial", CPTK_IS_TRIVIAL, 1, false},
-#line 80 "../../gcc/cp/cp-trait.gperf"
+#line 81 "../../gcc/cp/cp-trait.gperf"
       {"__remove_reference", CPTK_REMOVE_REFERENCE, 1, true},
-#line 85 "../../gcc/cp/cp-trait.gperf"
+#line 86 "../../gcc/cp/cp-trait.gperf"
       {"__direct_bases", CPTK_DIRECT_BASES, 1, true},
-#line 82 "../../gcc/cp/cp-trait.gperf"
+#line 83 "../../gcc/cp/cp-trait.gperf"
       {"__underlying_type", CPTK_UNDERLYING_TYPE, 1, true},
 #line 45 "../../gcc/cp/cp-trait.gperf"
       {"__is_bounded_array", CPTK_IS_BOUNDED_ARRAY, 1, false},
-#line 81 "../../gcc/cp/cp-trait.gperf"
+#line 82 "../../gcc/cp/cp-trait.gperf"
       {"__type_pack_element", CPTK_TYPE_PACK_ELEMENT, -1, true},
 #line 73 "../../gcc/cp/cp-trait.gperf"
       {"__is_unbounded_array", CPTK_IS_UNBOUNDED_ARRAY, 1, false},
@@ -235,21 +237,21 @@  cp_trait_lookup::find (const char *str, size_t len)
       {"__is_final", CPTK_IS_FINAL, 1, false},
 #line 53 "../../gcc/cp/cp-trait.gperf"
       {"__is_function", CPTK_IS_FUNCTION, 1, false},
-#line 83 "../../gcc/cp/cp-trait.gperf"
+#line 84 "../../gcc/cp/cp-trait.gperf"
       {"__is_deducible ", CPTK_IS_DEDUCIBLE, 2, false}
     };
 
   static const signed char lookup[] =
     {
       -1, -1, -1, -1, -1, -1, -1,  0, -1,  1,  2,  3, -1, -1,
-       4,  5, -1,  6,  7,  8, -1, -1,  9, 10, 11, 12, 13, 14,
-      15, -1, 16, 17, 18, 19, -1, 20, -1, 21, 22, -1, 23, -1,
-      24, 25, 26, 27, -1, 28, 29, 30, 31, -1, 32, -1, 33, 34,
-      -1, -1, 35, 36, 37, 38, -1, 39, 40, -1, -1, -1, 41, 42,
-      43, -1, -1, -1, -1, 44, 45, -1, 46, 47, 48, -1, -1, -1,
-      -1, 49, 50, -1, 51, -1, 52, -1, -1, -1, -1, 53, -1, -1,
-      54, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-      -1, -1, -1, 55
+       4,  5,  6,  7,  8,  9, -1, -1, 10, 11, 12, 13, 14, 15,
+      16, -1, 17, 18, 19, 20, -1, 21, -1, 22, 23, -1, 24, -1,
+      25, 26, 27, 28, -1, 29, 30, 31, 32, -1, 33, -1, 34, 35,
+      -1, -1, 36, 37, 38, 39, -1, 40, 41, -1, -1, -1, 42, 43,
+      44, -1, -1, -1, -1, 45, 46, -1, 47, 48, 49, -1, -1, -1,
+      -1, 50, 51, -1, 52, -1, 53, -1, -1, -1, -1, 54, -1, -1,
+      55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+      -1, -1, -1, 56
     };
 
   if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 4b8e80f3e62..168411f6700 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12488,6 +12488,11 @@  finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
 	type1 = TREE_TYPE (type1);
       return cv_unqualified (type1);
 
+    case CPTK_REMOVE_POINTER:
+      if (TYPE_PTR_P (type1))
+    type1 = TREE_TYPE (type1);
+      return type1;
+
     case CPTK_REMOVE_REFERENCE:
       if (TYPE_REF_P (type1))
 	type1 = TREE_TYPE (type1);
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4d3947572a4..bcab0599d1a 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -173,6 +173,9 @@ 
 #if !__has_builtin (__remove_cvref)
 # error "__has_builtin (__remove_cvref) failed"
 #endif
+#if !__has_builtin (__remove_pointer)
+# error "__has_builtin (__remove_pointer) failed"
+#endif
 #if !__has_builtin (__remove_reference)
 # error "__has_builtin (__remove_reference) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/remove_pointer.C b/gcc/testsuite/g++.dg/ext/remove_pointer.C
new file mode 100644
index 00000000000..7b13db93950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/remove_pointer.C
@@ -0,0 +1,51 @@ 
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_pointer(int), int));
+SA(__is_same(__remove_pointer(int*), int));
+SA(__is_same(__remove_pointer(int**), int*));
+
+SA(__is_same(__remove_pointer(const int*), const int));
+SA(__is_same(__remove_pointer(const int**), const int*));
+SA(__is_same(__remove_pointer(int* const), int));
+SA(__is_same(__remove_pointer(int** const), int*));
+SA(__is_same(__remove_pointer(int* const* const), int* const));
+
+SA(__is_same(__remove_pointer(volatile int*), volatile int));
+SA(__is_same(__remove_pointer(volatile int**), volatile int*));
+SA(__is_same(__remove_pointer(int* volatile), int));
+SA(__is_same(__remove_pointer(int** volatile), int*));
+SA(__is_same(__remove_pointer(int* volatile* volatile), int* volatile));
+
+SA(__is_same(__remove_pointer(const volatile int*), const volatile int));
+SA(__is_same(__remove_pointer(const volatile int**), const volatile int*));
+SA(__is_same(__remove_pointer(const int* volatile), const int));
+SA(__is_same(__remove_pointer(volatile int* const), volatile int));
+SA(__is_same(__remove_pointer(int* const volatile), int));
+SA(__is_same(__remove_pointer(const int** volatile), const int*));
+SA(__is_same(__remove_pointer(volatile int** const), volatile int*));
+SA(__is_same(__remove_pointer(int** const volatile), int*));
+SA(__is_same(__remove_pointer(int* const* const volatile), int* const));
+SA(__is_same(__remove_pointer(int* volatile* const volatile), int* volatile));
+SA(__is_same(__remove_pointer(int* const volatile* const volatile), int* const volatile));
+
+SA(__is_same(__remove_pointer(int&), int&));
+SA(__is_same(__remove_pointer(const int&), const int&));
+SA(__is_same(__remove_pointer(volatile int&), volatile int&));
+SA(__is_same(__remove_pointer(const volatile int&), const volatile int&));
+
+SA(__is_same(__remove_pointer(int&&), int&&));
+SA(__is_same(__remove_pointer(const int&&), const int&&));
+SA(__is_same(__remove_pointer(volatile int&&), volatile int&&));
+SA(__is_same(__remove_pointer(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__remove_pointer(int[3]), int[3]));
+SA(__is_same(__remove_pointer(const int[3]), const int[3]));
+SA(__is_same(__remove_pointer(volatile int[3]), volatile int[3]));
+SA(__is_same(__remove_pointer(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__remove_pointer(int(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*volatile)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const volatile)(int)), int(int)));