diff mbox series

[v14,17/26] c++: Implement __add_rvalue_reference built-in trait

Message ID 20240228192843.188979-18-kmatsui@gcc.gnu.org
State New
Headers show
Series Optimize more type traits | expand

Commit Message

Ken Matsui Feb. 28, 2024, 7:26 p.m. UTC
This patch implements built-in trait for std::add_rvalue_reference.

gcc/cp/ChangeLog:

	* cp-trait.def: Define __add_rvalue_reference.
	* semantics.cc (finish_trait_type): Handle
	CPTK_ADD_RVALUE_REFERENCE.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/cp/cp-trait.def                           |  1 +
 gcc/cp/semantics.cc                           |  8 ++++++++
 .../g++.dg/ext/add_rvalue_reference.C         | 20 +++++++++++++++++++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C      |  3 +++
 4 files changed, 32 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/ext/add_rvalue_reference.C

Comments

Jason Merrill April 30, 2024, 9:02 p.m. UTC | #1
On 2/28/24 11:26, Ken Matsui wrote:
> This patch implements built-in trait for std::add_rvalue_reference.
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-trait.def: Define __add_rvalue_reference.
> 	* semantics.cc (finish_trait_type): Handle
> 	CPTK_ADD_RVALUE_REFERENCE.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/has-builtin-1.C: Test existence of
> 	__add_rvalue_reference.
> 	* g++.dg/ext/add_rvalue_reference.C: New test.
> 
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
>   gcc/cp/cp-trait.def                           |  1 +
>   gcc/cp/semantics.cc                           |  8 ++++++++
>   .../g++.dg/ext/add_rvalue_reference.C         | 20 +++++++++++++++++++
>   gcc/testsuite/g++.dg/ext/has-builtin-1.C      |  3 +++
>   4 files changed, 32 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/ext/add_rvalue_reference.C
> 
> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> index 9a27dca4ea3..173818adf79 100644
> --- a/gcc/cp/cp-trait.def
> +++ b/gcc/cp/cp-trait.def
> @@ -50,6 +50,7 @@
>   
>   DEFTRAIT_TYPE (ADD_LVALUE_REFERENCE, "__add_lvalue_reference", 1)
>   DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
> +DEFTRAIT_TYPE (ADD_RVALUE_REFERENCE, "__add_rvalue_reference", 1)
>   DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
>   DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
>   DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 05f5b62f9df..19d6f87a9ea 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -12793,6 +12793,14 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
>   	type1 = TREE_TYPE (type1);
>         return build_pointer_type (type1);
>   
> +    case CPTK_ADD_RVALUE_REFERENCE:
> +      if (VOID_TYPE_P (type1)
> +	  || (FUNC_OR_METHOD_TYPE_P (type1)
> +	      && (type_memfn_quals (type1) != TYPE_UNQUALIFIED
> +		  || type_memfn_rqual (type1) != REF_QUAL_NONE)))

Actually, let's factor this bit out of this and the other traits into a 
referenceable_type_p function since it's getting checked so much.

> +	return type1;
> +      return cp_build_reference_type (type1, /*rval=*/true);
> +
>       case CPTK_REMOVE_ALL_EXTENTS:
>         return strip_array_types (type1);
>   
> diff --git a/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C b/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C
> new file mode 100644
> index 00000000000..c92fe6bfa17
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C
> @@ -0,0 +1,20 @@
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(X) static_assert((X),#X)
> +
> +class ClassType { };
> +
> +SA(__is_same(__add_rvalue_reference(int), int&&));
> +SA(__is_same(__add_rvalue_reference(int&&), int&&));
> +SA(__is_same(__add_rvalue_reference(int&), int&));
> +SA(__is_same(__add_rvalue_reference(const int), const int&&));
> +SA(__is_same(__add_rvalue_reference(int*), int*&&));
> +SA(__is_same(__add_rvalue_reference(ClassType&&), ClassType&&));
> +SA(__is_same(__add_rvalue_reference(ClassType), ClassType&&));
> +SA(__is_same(__add_rvalue_reference(int(int)), int(&&)(int)));
> +SA(__is_same(__add_rvalue_reference(void), void));
> +SA(__is_same(__add_rvalue_reference(const void), const void));
> +SA(__is_same(__add_rvalue_reference(bool(int) const), bool(int) const));
> +SA(__is_same(__add_rvalue_reference(bool(int) &), bool(int) &));
> +SA(__is_same(__add_rvalue_reference(bool(int) const &&), bool(int) const &&));
> +SA(__is_same(__add_rvalue_reference(bool(int)), bool(&&)(int)));
> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> index 3fca9cfabcc..c2503c5d82b 100644
> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> @@ -8,6 +8,9 @@
>   #if !__has_builtin (__add_pointer)
>   # error "__has_builtin (__add_pointer) failed"
>   #endif
> +#if !__has_builtin (__add_rvalue_reference)
> +# error "__has_builtin (__add_rvalue_reference) failed"
> +#endif
>   #if !__has_builtin (__builtin_addressof)
>   # error "__has_builtin (__builtin_addressof) failed"
>   #endif
diff mbox series

Patch

diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 9a27dca4ea3..173818adf79 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -50,6 +50,7 @@ 
 
 DEFTRAIT_TYPE (ADD_LVALUE_REFERENCE, "__add_lvalue_reference", 1)
 DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
+DEFTRAIT_TYPE (ADD_RVALUE_REFERENCE, "__add_rvalue_reference", 1)
 DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
 DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
 DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 05f5b62f9df..19d6f87a9ea 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12793,6 +12793,14 @@  finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
 	type1 = TREE_TYPE (type1);
       return build_pointer_type (type1);
 
+    case CPTK_ADD_RVALUE_REFERENCE:
+      if (VOID_TYPE_P (type1)
+	  || (FUNC_OR_METHOD_TYPE_P (type1)
+	      && (type_memfn_quals (type1) != TYPE_UNQUALIFIED
+		  || type_memfn_rqual (type1) != REF_QUAL_NONE)))
+	return type1;
+      return cp_build_reference_type (type1, /*rval=*/true);
+
     case CPTK_REMOVE_ALL_EXTENTS:
       return strip_array_types (type1);
 
diff --git a/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C b/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C
new file mode 100644
index 00000000000..c92fe6bfa17
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/add_rvalue_reference.C
@@ -0,0 +1,20 @@ 
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+class ClassType { };
+
+SA(__is_same(__add_rvalue_reference(int), int&&));
+SA(__is_same(__add_rvalue_reference(int&&), int&&));
+SA(__is_same(__add_rvalue_reference(int&), int&));
+SA(__is_same(__add_rvalue_reference(const int), const int&&));
+SA(__is_same(__add_rvalue_reference(int*), int*&&));
+SA(__is_same(__add_rvalue_reference(ClassType&&), ClassType&&));
+SA(__is_same(__add_rvalue_reference(ClassType), ClassType&&));
+SA(__is_same(__add_rvalue_reference(int(int)), int(&&)(int)));
+SA(__is_same(__add_rvalue_reference(void), void));
+SA(__is_same(__add_rvalue_reference(const void), const void));
+SA(__is_same(__add_rvalue_reference(bool(int) const), bool(int) const));
+SA(__is_same(__add_rvalue_reference(bool(int) &), bool(int) &));
+SA(__is_same(__add_rvalue_reference(bool(int) const &&), bool(int) const &&));
+SA(__is_same(__add_rvalue_reference(bool(int)), bool(&&)(int)));
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 3fca9cfabcc..c2503c5d82b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -8,6 +8,9 @@ 
 #if !__has_builtin (__add_pointer)
 # error "__has_builtin (__add_pointer) failed"
 #endif
+#if !__has_builtin (__add_rvalue_reference)
+# error "__has_builtin (__add_rvalue_reference) failed"
+#endif
 #if !__has_builtin (__builtin_addressof)
 # error "__has_builtin (__builtin_addressof) failed"
 #endif