This patch implements built-in trait for std::decay. gcc/cp/ChangeLog:
* cp-trait.def: Define __builtin_decay. * semantics.cc (finish_trait_type): Handle CPTK_DECAY. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __builtin_decay. * g++.dg/ext/decay.C: New test. Signed-off-by: Ken Matsui <kmat...@gcc.gnu.org> --- gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 12 ++++++++++++ gcc/testsuite/g++.dg/ext/decay.C | 22 ++++++++++++++++++++++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++ 4 files changed, 38 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/decay.C diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index c6fb2aa5f1e..90cd5ff99cc 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -51,6 +51,7 @@ DEFTRAIT_TYPE (ADD_LVALUE_REFERENCE, "__builtin_add_lvalue_reference", 1) DEFTRAIT_TYPE (ADD_POINTER, "__builtin_add_pointer", 1) DEFTRAIT_TYPE (ADD_RVALUE_REFERENCE, "__builtin_add_rvalue_reference", 1) +DEFTRAIT_TYPE (DECAY, "__builtin_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) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index fd6d9fc1dc5..c498191a3c8 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12942,6 +12942,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 index 00000000000..f8b161a97c7 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/decay.C @@ -0,0 +1,22 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +// Positive tests. +using test1_type = __builtin_decay(bool); +SA(__is_same(test1_type, bool)); + +// NB: DR 705. +using test2_type = __builtin_decay(const int); +SA(__is_same(test2_type, int)); + +using test3_type = __builtin_decay(int[4]); +SA(__is_same(test3_type, __builtin_remove_extent(int[4])*)); + +using fn_type = void (); +using test4_type = __builtin_decay(fn_type); +SA(__is_same(test4_type, __builtin_add_pointer(fn_type))); + +using cfn_type = void () const; +using test5_type = __builtin_decay(cfn_type); +SA(__is_same(test5_type, cfn_type)); diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index fc66c816887..0ea96d0ea87 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -17,6 +17,9 @@ #if !__has_builtin (__builtin_bit_cast) # error "__has_builtin (__builtin_bit_cast) failed" #endif +#if !__has_builtin (__builtin_decay) +# error "__has_builtin (__builtin_decay) failed" +#endif #if !__has_builtin (__builtin_is_const) # error "__has_builtin (__builtin_is_const) failed" #endif -- 2.44.0