From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00641c01.pphosted.com (mx0a-00641c01.pphosted.com [205.220.165.146]) by sourceware.org (Postfix) with ESMTPS id A4377384AB6E; Thu, 2 May 2024 18:45:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A4377384AB6E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=gcc.gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A4377384AB6E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.165.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714675549; cv=none; b=FxhBZDs+4wY5/Bk91e5kaaW4BpmOQVcHMYsBlhwmGTwmf/0m93pRCBNjcYt3uNmFSaBU8HWi0aauoFtvRfjpeY1Fk37CRK4luYX+im8MeZ5d/mqrZC6/WYp876ou+QXtZhZBNNN67SE19r6TQ4l1SFhsX9L0plxJReJAuFbz1hk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714675549; c=relaxed/simple; bh=dJhKGM8P5YbIe0VeZdL0iO8pFt52Dr7EP4OBtqlxcv0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=qHvSzl7aKirWWL7ynq0jrruytHdKpRwRTzYfTiuqAXIdI2xupz9dYLUYFPkle1jP/zoymCpSfadOEmB/HXxikzcDEaUXosA8IwNUIkZQ0KbpPo2Sm6CXmT/DgtSEqVNOzf6EQsXT2CfCw1l4LUCw7dIVfxLmQ6nCgqheSO8l2NY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247470.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 442INM3R011835; Thu, 2 May 2024 18:45:36 GMT Received: from mxout22.s.uw.edu (mxout22.s.uw.edu [128.95.242.222]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3xvf53gsef-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 May 2024 18:45:36 +0000 (GMT) Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout22.s.uw.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 442IjYtq030596 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 2 May 2024 11:45:34 -0700 X-Auth-Received: from ken-20xw00qgus.dhcp4.washington.edu ([10.154.75.239]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 442IjVww032283 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 2 May 2024 11:45:34 -0700 X-UW-Orig-Sender: kmatsui@smtp.washington.edu From: Ken Matsui To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, jason@redhat.com, Ken Matsui Subject: [PATCH v17 09/26] c++: Implement __add_pointer built-in trait Date: Thu, 2 May 2024 11:42:24 -0700 Message-ID: <20240502184529.97337-10-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240502184529.97337-1-kmatsui@gcc.gnu.org> References: <20240502161614.3705912-1-kmatsui@gcc.gnu.org> <20240502184529.97337-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-ORIG-GUID: xhUfe_lIQgaLkz7RIYOBQvHDF1zGtoNO X-Proofpoint-GUID: xhUfe_lIQgaLkz7RIYOBQvHDF1zGtoNO X-outbound-smtp: 1 x-header: uw-relay-non-domains X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-02_10,2024-05-02_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 priorityscore=1501 adultscore=0 clxscore=1034 malwarescore=0 mlxscore=0 suspectscore=0 spamscore=0 lowpriorityscore=0 impostorscore=0 bulkscore=0 mlxlogscore=734 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2405020125 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NEUTRAL,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This patch implements built-in trait for std::add_pointer. gcc/cp/ChangeLog: * cp-trait.def: Define __add_pointer. * semantics.cc (finish_trait_type): Handle CPTK_ADD_POINTER. (object_type_p): New function. (referenceable_type_p): Likewise. (trait_expr_value): Use object_type_p. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __add_pointer. * g++.dg/ext/add_pointer.C: New test. Signed-off-by: Ken Matsui --- gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 36 ++++++++++++++++++++-- gcc/testsuite/g++.dg/ext/add_pointer.C | 39 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/add_pointer.C diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 00bba91f461..7fdb7347ce6 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_POINTER, "__add_pointer", 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 138b180d9fb..29c1dbe2a85 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12434,6 +12434,16 @@ fold_builtin_is_corresponding_member (location_t loc, int nargs, fold_convert (TREE_TYPE (arg1), arg2))); } +/* [basic.types] 8. True iff TYPE is an object type. */ + +static bool +object_type_p (const_tree type) +{ + return (TREE_CODE (type) != FUNCTION_TYPE + && !TYPE_REF_P (type) + && !VOID_TYPE_P (type)); +} + /* Actually evaluates the trait. */ static bool @@ -12576,9 +12586,7 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) return is_nothrow_convertible (type1, type2); case CPTK_IS_OBJECT: - return (type_code1 != FUNCTION_TYPE - && type_code1 != REFERENCE_TYPE - && type_code1 != VOID_TYPE); + return object_type_p (type1); case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF: return pointer_interconvertible_base_of_p (type1, type2); @@ -12733,6 +12741,18 @@ same_type_ref_bind_p (cp_trait_kind kind, tree type1, tree type2) (non_reference (to), non_reference (from)))); } +/* [defns.referenceable] True iff TYPE is a referenceable type. */ + +static bool +referenceable_type_p (const_tree type) +{ + return (TYPE_REF_P (type) + || object_type_p (type) + || (FUNC_OR_METHOD_TYPE_P (type) + && (type_memfn_quals (type) == TYPE_UNQUALIFIED + && type_memfn_rqual (type) == REF_QUAL_NONE))); +} + /* Process a trait expression. */ tree @@ -12900,6 +12920,16 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2, switch (kind) { + case CPTK_ADD_POINTER: + /* [meta.trans.ptr]. */ + if (VOID_TYPE_P (type1) || referenceable_type_p (type1)) + { + if (TYPE_REF_P (type1)) + type1 = TREE_TYPE (type1); + return build_pointer_type (type1); + } + return type1; + case CPTK_REMOVE_CV: return cv_unqualified (type1); diff --git a/gcc/testsuite/g++.dg/ext/add_pointer.C b/gcc/testsuite/g++.dg/ext/add_pointer.C new file mode 100644 index 00000000000..c405cdd0feb --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/add_pointer.C @@ -0,0 +1,39 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +class ClassType { }; + +SA(__is_same(__add_pointer(int), int*)); +SA(__is_same(__add_pointer(int*), int**)); +SA(__is_same(__add_pointer(const int), const int*)); +SA(__is_same(__add_pointer(int&), int*)); +SA(__is_same(__add_pointer(ClassType*), ClassType**)); +SA(__is_same(__add_pointer(ClassType), ClassType*)); +SA(__is_same(__add_pointer(void), void*)); +SA(__is_same(__add_pointer(const void), const void*)); +SA(__is_same(__add_pointer(volatile void), volatile void*)); +SA(__is_same(__add_pointer(const volatile void), const volatile void*)); + +void f1(); +using f1_type = decltype(f1); +using pf1_type = decltype(&f1); +SA(__is_same(__add_pointer(f1_type), pf1_type)); + +void f2() noexcept; // PR libstdc++/78361 +using f2_type = decltype(f2); +using pf2_type = decltype(&f2); +SA(__is_same(__add_pointer(f2_type), pf2_type)); + +using fn_type = void(); +using pfn_type = void(*)(); +SA(__is_same(__add_pointer(fn_type), pfn_type)); + +SA(__is_same(__add_pointer(void() &), void() &)); +SA(__is_same(__add_pointer(void() & noexcept), void() & noexcept)); +SA(__is_same(__add_pointer(void() const), void() const)); +SA(__is_same(__add_pointer(void(...) &), void(...) &)); +SA(__is_same(__add_pointer(void(...) & noexcept), void(...) & noexcept)); +SA(__is_same(__add_pointer(void(...) const), void(...) const)); + +SA(__is_same(__add_pointer(void() __restrict), void() __restrict)); diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index 068d8187707..c8da22091d3 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_pointer) +# error "__has_builtin (__add_pointer) failed" +#endif #if !__has_builtin (__builtin_addressof) # error "__has_builtin (__builtin_addressof) failed" #endif -- 2.44.0