]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement __decay built-in trait
authorKen Matsui <kmatsui@gcc.gnu.org>
Thu, 15 Feb 2024 12:43:41 +0000 (04:43 -0800)
committerKen Matsui <kmatsui@gcc.gnu.org>
Sat, 11 May 2024 01:17:32 +0000 (18:17 -0700)
This patch implements built-in trait for std::decay.

gcc/cp/ChangeLog:

* cp-trait.def: Define __decay.
* semantics.cc (finish_trait_type): Handle CPTK_DECAY.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cp-trait.def
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/ext/decay.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/has-builtin-1.C

index 173818adf7928222df07e1b5b577664a603e51dd..2d1cb7c227c0a25ac97acbe9f265b8defd08bcac 100644 (file)
@@ -51,6 +51,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_TYPE (DECAY, "__decay", 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)
index f6338f77d239f66522867687fa65aa51344b81ed..d499b858057c5340c1522f672d9c2469b1744319 100644 (file)
@@ -12946,6 +12946,18 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
        return cp_build_reference_type (type1, /*rval=*/true);
       return type1;
 
+    case CPTK_DECAY:
+      if (TYPE_REF_P (type1))
+       type1 = TREE_TYPE (type1);
+
+      if (TREE_CODE (type1) == ARRAY_TYPE)
+       return finish_trait_type (CPTK_ADD_POINTER, TREE_TYPE (type1), type2,
+                                 complain);
+      else if (TREE_CODE (type1) == FUNCTION_TYPE)
+       return finish_trait_type (CPTK_ADD_POINTER, type1, type2, complain);
+      else
+       return cv_unqualified (type1);
+
     case CPTK_REMOVE_ALL_EXTENTS:
       return strip_array_types (type1);
 
diff --git a/gcc/testsuite/g++.dg/ext/decay.C b/gcc/testsuite/g++.dg/ext/decay.C
new file mode 100644 (file)
index 0000000..8adedfe
--- /dev/null
@@ -0,0 +1,22 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+using test1_type = __decay(bool);
+SA(__is_same(test1_type, bool));
+
+// NB: DR 705.
+using test2_type = __decay(const int);
+SA(__is_same(test2_type, int));
+
+using test3_type = __decay(int[4]);
+SA(__is_same(test3_type, __remove_extent(int[4])*));
+
+using fn_type = void ();
+using test4_type = __decay(fn_type);
+SA(__is_same(test4_type, __add_pointer(fn_type)));
+
+using cfn_type = void () const;
+using test5_type = __decay(cfn_type);
+SA(__is_same(test5_type, cfn_type));
index 266cb35a3684646e2b4acefb2303732b3fe2c6db..d2e6a5b4dcb3cf19c6787ae9ecec2429cf53a1b7 100644 (file)
@@ -32,6 +32,9 @@
 #if !__has_builtin (__builtin_source_location)
 # error "__has_builtin (__builtin_source_location) failed"
 #endif
+#if !__has_builtin (__decay)
+# error "__has_builtin (__decay) failed"
+#endif
 #if !__has_builtin (__has_nothrow_assign)
 # error "__has_builtin (__has_nothrow_assign) failed"
 #endif