From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00641c01.pphosted.com (mx0b-00641c01.pphosted.com [205.220.177.146]) by sourceware.org (Postfix) with ESMTPS id EB753385842D; Thu, 2 May 2024 16:16:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB753385842D 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 EB753385842D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.177.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714666597; cv=none; b=hfpMkEmGuUVVek1if7mJKwKuVu6DKDxigyG2Z+1Rafzr/RWN0YzubvoaknUyqn7a9ToEWbEOkFycx6w1Qxt++GDdJ8jGg8PfG1PEWSGKebWtJ1Xb0JPXeYz7t7R2XM8ccaHcdO8cgadZQcirSjP06oMBJFbx1aNxrcV3RKK5k5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714666597; c=relaxed/simple; bh=V6i+ATSAq3aJd2gyvlmLtx291cI18vYPXLJR2yGhYnY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=QQOl4xq9/EhpKwaoZBlmCjHeLNMIeTKxi+O3877GK10dCpa0fD42hQnpFS1FJwcgfDLBJdFYwmJxVVhcmKezOJCbf709APOowvR0FS9NS8/XAW/fe1w9mzV4aB2HVAIxaV3G4H8SUjlIhrZvGBU5aq/LvwrdyjOSTrOVB9yh1+U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247477.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 442GAsUi028310; Thu, 2 May 2024 16:16:33 GMT Received: from mxout22.s.uw.edu (mxout22.s.uw.edu [128.95.242.222]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3xv4e5kd3g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 02 May 2024 16:16:32 +0000 (GMT) Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.54]) by mxout22.s.uw.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 442GGGpu003506 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 2 May 2024 09:16:16 -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 442GGFom023119 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 2 May 2024 09:16:16 -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 v16 09/26] c++: Implement __builtin_add_pointer trait Date: Thu, 2 May 2024 09:13:35 -0700 Message-ID: <20240502161614.3705912-10-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240502161614.3705912-1-kmatsui@gcc.gnu.org> References: <20240228192843.188979-1-kmatsui@gcc.gnu.org> <20240502161614.3705912-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: hSipzt1qQ0HdTWnXqqSqKjDympEZqufP X-Proofpoint-ORIG-GUID: hSipzt1qQ0HdTWnXqqSqKjDympEZqufP 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_05,2024-05-02_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=732 spamscore=0 priorityscore=1501 suspectscore=0 phishscore=0 adultscore=0 impostorscore=0 bulkscore=0 malwarescore=0 lowpriorityscore=0 clxscore=1034 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2405020106 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_DNSWL_LOW,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 __builtin_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 __builtin_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 7a3cbbebde6..7847ce748f6 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, "__builtin_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..1b9a318e7d9 --- /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(__builtin_add_pointer(int), int*)); +SA(__is_same(__builtin_add_pointer(int*), int**)); +SA(__is_same(__builtin_add_pointer(const int), const int*)); +SA(__is_same(__builtin_add_pointer(int&), int*)); +SA(__is_same(__builtin_add_pointer(ClassType*), ClassType**)); +SA(__is_same(__builtin_add_pointer(ClassType), ClassType*)); +SA(__is_same(__builtin_add_pointer(void), void*)); +SA(__is_same(__builtin_add_pointer(const void), const void*)); +SA(__is_same(__builtin_add_pointer(volatile void), volatile void*)); +SA(__is_same(__builtin_add_pointer(const volatile void), const volatile void*)); + +void f1(); +using f1_type = decltype(f1); +using pf1_type = decltype(&f1); +SA(__is_same(__builtin_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(__builtin_add_pointer(f2_type), pf2_type)); + +using fn_type = void(); +using pfn_type = void(*)(); +SA(__is_same(__builtin_add_pointer(fn_type), pfn_type)); + +SA(__is_same(__builtin_add_pointer(void() &), void() &)); +SA(__is_same(__builtin_add_pointer(void() & noexcept), void() & noexcept)); +SA(__is_same(__builtin_add_pointer(void() const), void() const)); +SA(__is_same(__builtin_add_pointer(void(...) &), void(...) &)); +SA(__is_same(__builtin_add_pointer(void(...) & noexcept), void(...) & noexcept)); +SA(__is_same(__builtin_add_pointer(void(...) const), void(...) const)); + +SA(__is_same(__builtin_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 7eb341a8068..509cd4294f6 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -5,6 +5,9 @@ #if !__has_builtin (__builtin_addressof) # error "__has_builtin (__builtin_addressof) failed" #endif +#if !__has_builtin (__builtin_add_pointer) +# error "__has_builtin (__builtin_add_pointer) failed" +#endif #if !__has_builtin (__builtin_bit_cast) # error "__has_builtin (__builtin_bit_cast) failed" #endif -- 2.44.0