https://gcc.gnu.org/g:c08d1afbf539a672f28100640d964565ac575ff5

commit r15-372-gc08d1afbf539a672f28100640d964565ac575ff5
Author: Ken Matsui <kmat...@gcc.gnu.org>
Date:   Wed Feb 14 23:21:35 2024 -0800

    c++: Implement __add_lvalue_reference built-in trait
    
    This patch implements built-in trait for std::add_lvalue_reference.
    
    gcc/cp/ChangeLog:
    
            * cp-trait.def: Define __add_lvalue_reference.
            * semantics.cc (finish_trait_type): Handle
            CPTK_ADD_LVALUE_REFERENCE.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/ext/has-builtin-1.C: Test existence of
            __add_lvalue_reference.
            * g++.dg/ext/add_lvalue_reference.C: New test.
    
    Signed-off-by: Ken Matsui <kmat...@gcc.gnu.org>
    Reviewed-by: Jason Merrill <ja...@redhat.com>

Diff:
---
 gcc/cp/cp-trait.def                             |  1 +
 gcc/cp/semantics.cc                             |  6 ++++++
 gcc/testsuite/g++.dg/ext/add_lvalue_reference.C | 21 +++++++++++++++++++++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C        |  3 +++
 4 files changed, 31 insertions(+)

diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 933c8bcbe68e..9a27dca4ea35 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -48,6 +48,7 @@
 #define DEFTRAIT_TYPE_DEFAULTED
 #endif
 
+DEFTRAIT_TYPE (ADD_LVALUE_REFERENCE, "__add_lvalue_reference", 1)
 DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
 DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
 DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index c2ab08c56052..a6f795d6336b 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12924,6 +12924,12 @@ finish_trait_type (cp_trait_kind kind, tree type1, 
tree type2,
 
   switch (kind)
     {
+    case CPTK_ADD_LVALUE_REFERENCE:
+      /* [meta.trans.ref].  */
+      if (referenceable_type_p (type1))
+       return cp_build_reference_type (type1, /*rval=*/false);
+      return type1;
+
     case CPTK_ADD_POINTER:
       /* [meta.trans.ptr].  */
       if (VOID_TYPE_P (type1) || referenceable_type_p (type1))
diff --git a/gcc/testsuite/g++.dg/ext/add_lvalue_reference.C 
b/gcc/testsuite/g++.dg/ext/add_lvalue_reference.C
new file mode 100644
index 000000000000..8fe1e0300e5a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/add_lvalue_reference.C
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+class ClassType { };
+
+SA(__is_same(__add_lvalue_reference(int), int&));
+SA(__is_same(__add_lvalue_reference(int&), int&));
+SA(__is_same(__add_lvalue_reference(const int), const int&));
+SA(__is_same(__add_lvalue_reference(int*), int*&));
+SA(__is_same(__add_lvalue_reference(ClassType&), ClassType&));
+SA(__is_same(__add_lvalue_reference(ClassType), ClassType&));
+SA(__is_same(__add_lvalue_reference(int(int)), int(&)(int)));
+SA(__is_same(__add_lvalue_reference(int&&), int&));
+SA(__is_same(__add_lvalue_reference(ClassType&&), ClassType&));
+SA(__is_same(__add_lvalue_reference(void), void));
+SA(__is_same(__add_lvalue_reference(const void), const void));
+SA(__is_same(__add_lvalue_reference(bool(int) const), bool(int) const));
+SA(__is_same(__add_lvalue_reference(bool(int) &), bool(int) &));
+SA(__is_same(__add_lvalue_reference(bool(int) const &&), bool(int) const &&));
+SA(__is_same(__add_lvalue_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 f0b165e28916..a8b2f0fcfd28 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -2,6 +2,9 @@
 // { dg-do compile }
 // Verify that __has_builtin gives the correct answer for C++ built-ins.
 
+#if !__has_builtin (__add_lvalue_reference)
+# error "__has_builtin (__add_lvalue_reference) failed"
+#endif
 #if !__has_builtin (__add_pointer)
 # error "__has_builtin (__add_pointer) failed"
 #endif

Reply via email to