]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: implement __remove_cv, __remove_reference and __remove_cvref
authorPatrick Palka <ppalka@redhat.com>
Thu, 29 Sep 2022 13:18:40 +0000 (09:18 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 29 Sep 2022 13:18:40 +0000 (09:18 -0400)
This implements builtins for std::remove_cv, std::remove_reference and
std::remove_cvref using TRAIT_TYPE from the previous patch.

gcc/c-family/ChangeLog:

* c-common.cc (c_common_reswords): Add __remove_cv,
__remove_reference and __remove_cvref.
* c-common.h (enum rid): Add RID_REMOVE_CV, RID_REMOVE_REFERENCE
and RID_REMOVE_CVREF.

gcc/cp/ChangeLog:

* constraint.cc (diagnose_trait_expr): Handle CPTK_REMOVE_CV,
CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
* cp-objcp-common.cc (names_builtin_p): Likewise.
* cp-tree.h (enum cp_trait_kind): Add CPTK_REMOVE_CV,
CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
* cxx-pretty-print.cc (pp_cxx_trait): Handle CPTK_REMOVE_CV,
CPTK_REMOVE_REFERENCE and CPTK_REMOVE_CVREF.
* parser.cc (cp_keyword_starts_decl_specifier_p): Return true
for RID_REMOVE_CV, RID_REMOVE_REFERENCE and RID_REMOVE_CVREF.
(cp_parser_trait): Handle RID_REMOVE_CV, RID_REMOVE_REFERENCE
and RID_REMOVE_CVREF.
(cp_parser_simple_type_specifier): Likewise.
* semantics.cc (finish_trait_type): Likewise.

libstdc++-v3/ChangeLog:

* include/bits/unique_ptr.h (unique_ptr<_Tp[], _Dp>): Remove
__remove_cv and use __remove_cv_t instead.

gcc/testsuite/ChangeLog:

* g++.dg/ext/has-builtin-1.C: Test existence of __remove_cv,
__remove_reference and __remove_cvref.
* g++.dg/ext/remove_cv.C: New test.
* g++.dg/ext/remove_reference.C: New test.
* g++.dg/ext/remove_cvref.C: New test.

13 files changed:
gcc/c-family/c-common.cc
gcc/c-family/c-common.h
gcc/cp/constraint.cc
gcc/cp/cp-objcp-common.cc
gcc/cp/cp-tree.h
gcc/cp/cxx-pretty-print.cc
gcc/cp/parser.cc
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/ext/has-builtin-1.C
gcc/testsuite/g++.dg/ext/remove_cv.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/remove_cvref.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/remove_reference.C [new file with mode: 0644]
libstdc++-v3/include/bits/unique_ptr.h

index cda6910e8c586bb3714e701e5726b84f045883c5..6e0af863a493223246eb5e32041795e198c432ce 100644 (file)
@@ -547,6 +547,9 @@ const struct c_common_resword c_common_reswords[] =
                                        D_CXXONLY },
   { "__reference_converts_from_temporary", RID_REF_CONVERTS_FROM_TEMPORARY,
                                        D_CXXONLY },
+  { "__remove_cv", RID_REMOVE_CV, D_CXXONLY },
+  { "__remove_reference", RID_REMOVE_REFERENCE, D_CXXONLY },
+  { "__remove_cvref", RID_REMOVE_CVREF, D_CXXONLY },
 
   /* C++ transactional memory.  */
   { "synchronized",    RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
index 50a4691cda6577ee808c1878e935301395d74da7..d5c98d306ced973510c7c97acc9c690cede0920b 100644 (file)
@@ -187,6 +187,7 @@ enum rid
   RID_IS_CONVERTIBLE,          RID_IS_NOTHROW_CONVERTIBLE,
   RID_REF_CONSTRUCTS_FROM_TEMPORARY,
   RID_REF_CONVERTS_FROM_TEMPORARY,
+  RID_REMOVE_CV, RID_REMOVE_REFERENCE, RID_REMOVE_CVREF,
 
   /* C++11 */
   RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR, RID_STATIC_ASSERT,
index 266ec581a2026157718bfb6a891d882770130689..ca73aff3f38c21fc3a82cf5313b34dd1566154ae 100644 (file)
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_BASES:
     case CPTK_DIRECT_BASES:
     case CPTK_UNDERLYING_TYPE:
+    case CPTK_REMOVE_CV:
+    case CPTK_REMOVE_REFERENCE:
+    case CPTK_REMOVE_CVREF:
       /* We shouldn't see these non-expression traits.  */
       gcc_unreachable ();
     /* We deliberately omit the default case so that when adding a new
index 380f288a7f1556d2a629453e601fba09b5c07566..2d3f206b530869a79f137d82d84b2824d718b85c 100644 (file)
@@ -467,6 +467,9 @@ names_builtin_p (const char *name)
     case RID_IS_NOTHROW_CONVERTIBLE:
     case RID_REF_CONSTRUCTS_FROM_TEMPORARY:
     case RID_REF_CONVERTS_FROM_TEMPORARY:
+    case RID_REMOVE_CV:
+    case RID_REMOVE_REFERENCE:
+    case RID_REMOVE_CVREF:
       return true;
     default:
       break;
index ff9913ca1eeea95e830ebbe26a0f939d4a7ff0ff..3cbcdf726caf7174a2fd80fabf4c3e4aedbd03c7 100644 (file)
@@ -1412,7 +1412,10 @@ enum cp_trait_kind
   CPTK_IS_CONVERTIBLE,
   CPTK_IS_NOTHROW_CONVERTIBLE,
   CPTK_REF_CONSTRUCTS_FROM_TEMPORARY,
-  CPTK_REF_CONVERTS_FROM_TEMPORARY
+  CPTK_REF_CONVERTS_FROM_TEMPORARY,
+  CPTK_REMOVE_CV,
+  CPTK_REMOVE_REFERENCE,
+  CPTK_REMOVE_CVREF,
 };
 
 /* The types that we are processing.  */
index 752ce3f8dd99aedcb7ef7c9a99c4153714a49d16..b91615439e40158e19ae0811bfe2f1c7ef2c3659 100644 (file)
@@ -2728,6 +2728,15 @@ pp_cxx_trait (cxx_pretty_printer *pp, tree t)
     case CPTK_UNDERLYING_TYPE:
       pp_cxx_ws_string (pp, "__underlying_type");
       break;
+    case CPTK_REMOVE_CV:
+      pp_cxx_ws_string (pp, "__remove_cv");
+      break;
+    case CPTK_REMOVE_REFERENCE:
+      pp_cxx_ws_string (pp, "__remove_reference");
+      break;
+    case CPTK_REMOVE_CVREF:
+      pp_cxx_ws_string (pp, "__remove_cvref");
+      break;
     default:
       gcc_unreachable ();
     }
index 9f5e2c292b334dbd915d9598d42b73aa5a48cd78..d592d783250ed154af3d44ad561182696f608dc1 100644 (file)
@@ -1147,6 +1147,9 @@ cp_keyword_starts_decl_specifier_p (enum rid keyword)
       /* C++11 extensions.  */
     case RID_DECLTYPE:
     case RID_UNDERLYING_TYPE:
+    case RID_REMOVE_CV:
+    case RID_REMOVE_REFERENCE:
+    case RID_REMOVE_CVREF:
     case RID_CONSTEXPR:
       /* C++20 extensions.  */
     case RID_CONSTINIT:
@@ -11027,6 +11030,18 @@ cp_parser_trait (cp_parser* parser, enum rid keyword)
       kind = CPTK_REF_CONVERTS_FROM_TEMPORARY;
       binary = true;
       break;
+    case RID_REMOVE_CV:
+      kind = CPTK_REMOVE_CV;
+      type = true;
+      break;
+    case RID_REMOVE_REFERENCE:
+      kind = CPTK_REMOVE_REFERENCE;
+      type = true;
+      break;
+    case RID_REMOVE_CVREF:
+      kind = CPTK_REMOVE_CVREF;
+      type = true;
+      break;
     default:
       gcc_unreachable ();
     }
@@ -19867,6 +19882,9 @@ cp_parser_simple_type_specifier (cp_parser* parser,
       return type;
 
     case RID_UNDERLYING_TYPE:
+    case RID_REMOVE_CV:
+    case RID_REMOVE_REFERENCE:
+    case RID_REMOVE_CVREF:
       type = cp_parser_trait (parser, token->keyword);
       if (decl_specs)
        cp_parser_set_decl_spec_type (decl_specs, type,
index aae492de5299bbe135e047b0eb6bf598751fbd7d..66ee2186a84e206de72445795eb7214ffa804340 100644 (file)
@@ -12240,6 +12240,16 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
     {
     case CPTK_UNDERLYING_TYPE:
       return finish_underlying_type (type1);
+    case CPTK_REMOVE_CV:
+      return cv_unqualified (type1);
+    case CPTK_REMOVE_REFERENCE:
+      if (TYPE_REF_P (type1))
+       type1 = TREE_TYPE (type1);
+      return type1;
+    case CPTK_REMOVE_CVREF:
+      if (TYPE_REF_P (type1))
+       type1 = TREE_TYPE (type1);
+      return cv_unqualified (type1);
     default:
       gcc_unreachable ();
     }
index 17dabf648cff4b213fe42aabc2ded306598c67b3..f343e153e56c444af0e1cf776adb27a479ff2186 100644 (file)
 #if !__has_builtin (__is_nothrow_convertible)
 # error "__has_builtin (__is_nothrow_convertible) failed"
 #endif
+#if !__has_builtin (__remove_cv)
+# error "__has_builtin (__remove_cv) failed"
+#endif
+#if !__has_builtin (__remove_reference)
+# error "__has_builtin (__remove_reference) failed"
+#endif
+#if !__has_builtin (__remove_cvref)
+# error "__has_builtin (__remove_cvref) failed"
+#endif
diff --git a/gcc/testsuite/g++.dg/ext/remove_cv.C b/gcc/testsuite/g++.dg/ext/remove_cv.C
new file mode 100644 (file)
index 0000000..cff7050
--- /dev/null
@@ -0,0 +1,32 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_cv(void), void));
+SA(__is_same(__remove_cv(int*), int*));
+
+SA(__is_same(__remove_cv(int&), int&));
+SA(__is_same(__remove_cv(const int&), const int&));
+SA(__is_same(__remove_cv(volatile int&), volatile int&));
+SA(__is_same(__remove_cv(const volatile int&), const volatile int&));
+
+SA(__is_same(__remove_cv(int&&), int&&));
+SA(__is_same(__remove_cv(const int&&), const int&&));
+SA(__is_same(__remove_cv(volatile int&&), volatile int&&));
+SA(__is_same(__remove_cv(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__remove_cv(int[3]), int[3]));
+SA(__is_same(__remove_cv(const int[3]), int[3]));
+SA(__is_same(__remove_cv(volatile int[3]), int[3]));
+SA(__is_same(__remove_cv(const volatile int[3]), int[3]));
+
+SA(__is_same(__remove_cv(int(int)), int(int)));
+SA(__is_same(__remove_cv(int(*const)(int)), int(*)(int)));
+SA(__is_same(__remove_cv(int(*volatile)(int)), int(*)(int)));
+SA(__is_same(__remove_cv(int(*const volatile)(int)), int(*)(int)));
+
+template<class T> using const_non_volatile_t = const __remove_cv(T);
+SA(__is_same(const_non_volatile_t<int>, const int));
+SA(__is_same(const_non_volatile_t<volatile int>, const int));
+SA(__is_same(const_non_volatile_t<int&>, int&));
+SA(__is_same(const_non_volatile_t<const int&>, const int&));
diff --git a/gcc/testsuite/g++.dg/ext/remove_cvref.C b/gcc/testsuite/g++.dg/ext/remove_cvref.C
new file mode 100644 (file)
index 0000000..109d54f
--- /dev/null
@@ -0,0 +1,32 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_cvref(void), void));
+SA(__is_same(__remove_cvref(int*), int*));
+
+SA(__is_same(__remove_cvref(int&), int));
+SA(__is_same(__remove_cvref(const int&), int));
+SA(__is_same(__remove_cvref(volatile int&), int));
+SA(__is_same(__remove_cvref(const volatile int&), int));
+
+SA(__is_same(__remove_cvref(int&&), int));
+SA(__is_same(__remove_cvref(const int&&), int));
+SA(__is_same(__remove_cvref(volatile int&&), int));
+SA(__is_same(__remove_cvref(const volatile int&&), int));
+
+SA(__is_same(__remove_cvref(int[3]), int[3]));
+SA(__is_same(__remove_cvref(const int[3]), int[3]));
+SA(__is_same(__remove_cvref(volatile int[3]), int[3]));
+SA(__is_same(__remove_cvref(const volatile int[3]), int[3]));
+
+SA(__is_same(__remove_cvref(int(int)), int(int)));
+SA(__is_same(__remove_cvref(int(*const)(int)), int(*)(int)));
+SA(__is_same(__remove_cvref(int(*volatile)(int)), int(*)(int)));
+SA(__is_same(__remove_cvref(int(*const volatile)(int)), int(*)(int)));
+
+template<class T> using const_non_volatile_non_ref_t = const __remove_cvref(T);
+SA(__is_same(const_non_volatile_non_ref_t<int>, const int));
+SA(__is_same(const_non_volatile_non_ref_t<volatile int&>, const int));
+SA(__is_same(const_non_volatile_non_ref_t<int&>, const int));
+SA(__is_same(const_non_volatile_non_ref_t<const int&>, const int));
diff --git a/gcc/testsuite/g++.dg/ext/remove_reference.C b/gcc/testsuite/g++.dg/ext/remove_reference.C
new file mode 100644 (file)
index 0000000..1a2580b
--- /dev/null
@@ -0,0 +1,32 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_reference(void), void));
+SA(__is_same(__remove_reference(int*), int*));
+
+SA(__is_same(__remove_reference(int&), int));
+SA(__is_same(__remove_reference(const int&), const int));
+SA(__is_same(__remove_reference(volatile int&), volatile int));
+SA(__is_same(__remove_reference(const volatile int&), const volatile int));
+
+SA(__is_same(__remove_reference(int&&), int));
+SA(__is_same(__remove_reference(const int&&), const int));
+SA(__is_same(__remove_reference(volatile int&&), volatile int));
+SA(__is_same(__remove_reference(const volatile int&&), const volatile int));
+
+SA(__is_same(__remove_reference(int[3]), int[3]));
+SA(__is_same(__remove_reference(const int[3]), const int[3]));
+SA(__is_same(__remove_reference(volatile int[3]), volatile int[3]));
+SA(__is_same(__remove_reference(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__remove_reference(int(int)), int(int)));
+SA(__is_same(__remove_reference(int(*const)(int)), int(*const)(int)));
+SA(__is_same(__remove_reference(int(*volatile)(int)), int(*volatile)(int)));
+SA(__is_same(__remove_reference(int(*const volatile)(int)), int(*const volatile)(int)));
+
+template<class T> using const_non_ref_t = const __remove_reference(T);
+SA(__is_same(const_non_ref_t<int>, const int));
+SA(__is_same(const_non_ref_t<volatile int&>, const volatile int));
+SA(__is_same(const_non_ref_t<int&>, const int));
+SA(__is_same(const_non_ref_t<const int&>, const int));
index 1086f408374cd1688ab9ab7348ff743850726ada..34c3a766179d77064b807b6e21b73437f8739786 100644 (file)
@@ -541,14 +541,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       __uniq_ptr_data<_Tp, _Dp> _M_t;
 
-      template<typename _Up>
-       using __remove_cv = typename remove_cv<_Up>::type;
-
       // like is_base_of<_Tp, _Up> but false if unqualified types are the same
       template<typename _Up>
        using __is_derived_Tp
          = __and_< is_base_of<_Tp, _Up>,
-                   __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
+                   __not_<is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up>>> >;
 
     public:
       using pointer      = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;