From patchwork Wed Jan 10 09:23:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 187015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1015885dyi; Wed, 10 Jan 2024 11:44:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPORyF0R0ik8qrijX2I5aF+XG95X6uiuI+95ZfURhLJXFvveMU0OMP9SyLRweZPbwJyaNr X-Received: by 2002:ad4:596f:0:b0:67f:2dc9:b80d with SMTP id eq15-20020ad4596f000000b0067f2dc9b80dmr1747017qvb.4.1704915896922; Wed, 10 Jan 2024 11:44:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704915896; cv=pass; d=google.com; s=arc-20160816; b=GOCNmIzWLMsmqZd1CyPbBUsGLoVKhxqOsQIip4putEqQwXz+rDIhy7Gw2CL0MR9Fa6 +6S/xvXzABEMV3g0J5u9XSD5LaWrdtxDfUsR4ua4CkCOUcQlsPM5nN4nmUy34NlR4EIm 24KNQC6D4Ud1GQ2FGRyxPtr4VO2kUPmZSkzX7X5t7HMqRojulb2dB4euHDuf2pLLKZ/u JVAUamuohRum/m5QbG2OydZZWitxb0azLL/2MORnnJL+hNvY8GeVzGgEGtQSKdpAyYix MubVCgiTNPaQvqz3tQrLiSryy/v+cUN2oDSriQU9UOMwURpp2/bs2+P72i7V15HHhHFP Ui0A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=HCLXnuySCXA2fhpOOz9Rf2f8NId7W5rLBxVmB5X8s8c=; fh=kSSy/oyUXqQD15FLeKdymIgDBFPFITPA1Gj52CNOxRQ=; b=m9vDby0dtj42xgkOXTyxHzG+Okk5jq7kKaeq+zbqmncl6lKYZbgLJedpcslA0gLEsS RsWQmOT2lwKDNv55yyNUF4osy7VdhgFAZt7gv8x97niCUssYIIK8f9f8a0yB2EZLcQn+ XS/kRdl5CI0WicJwrFFlAIuho8rbyUPu4gKmDetzWIhHQL9JExCP9I2my27M+paaDbEA engihU7Y2AAahYvEaPv3kXB2GbGlTCPDg8jn681eNLybhPx/1tmmtZr8ex8Nb1RBCv/H 1X/cmdc89fNuA6qiCk5qdOz0JVyuyDz/6uiOfX/rDeLFKSGQIpzZdP5P16MnbjkoDxlD b6Hw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dzUEvOdc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q9-20020a0c8cc9000000b0067f30be7df2si4775270qvb.496.2024.01.10.11.44.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 11:44:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dzUEvOdc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51A3A385800C for ; Wed, 10 Jan 2024 19:44:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51A3A385800C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1704915896; bh=HCLXnuySCXA2fhpOOz9Rf2f8NId7W5rLBxVmB5X8s8c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=dzUEvOdc0uLnffl1ZqmYto+IygGqsLXJ2lnT2s7ELslSKt1/B/FFAV5e+RVLhO77r ZHXdbb4ZfH8SeVrlvT390+RdfWMba+JfWBSW/7eUUsSOjBCJCmCfs4N20bcqq3Tl1J 09e4YI8Fv5+ROTH/tcc4K5dzp4c4xN3nxA40cR2Y= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00641c01.pphosted.com (mx0a-00641c01.pphosted.com [205.220.165.146]) by sourceware.org (Postfix) with ESMTPS id 773A938582B3; Wed, 10 Jan 2024 19:41:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 773A938582B3 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 773A938582B3 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=1704915677; cv=none; b=fOuRdsIK71ArDeyExBPLDYg2sQiV6HcbDMVhA/BUX/YZr6QsWgWdGI1zYgRh3iWOI4Im3U066G5ok3KkQOMpmEiDwwGS6pWtqwhV7P8ukX8ZLLM2QR6ED6mxe0/V8pCuDYCfTXdYNfLJN3Lj9udlrZQJpzrqPZ3NYpn0hrChqGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704915677; c=relaxed/simple; bh=/t4dTqddvLqvApC8lgG+0FHj/q9+gyZgFWDJrZXmSXk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Kyq3nzXkT4fozcjG5H3rRgxXUi5pGIdqppIY3FhN4bvrHU/jv6eCxfca0YY+UW1AdwSJK1+tz8zBlj73lbycTwqgEBrgTgl8nwEhkpFWzDa6RZR3dx2tmdSyPo6PLV69ibB6mJM4Wv4NXJpieU02TZC93x9v3FmF8yzCyxbcMMQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247473.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40AJbUos000382; Wed, 10 Jan 2024 19:41:10 GMT Received: from mxout25.s.uw.edu (mxout25.s.uw.edu [140.142.234.175]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3vh9t6ux8x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 19:41:10 +0000 Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.54]) by mxout25.s.uw.edu (8.14.4+UW20.07/8.14.4+UW22.04) with ESMTP id 40AJehbS002144 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 10 Jan 2024 11:40:43 -0800 X-Auth-Received: from kmatsui-ThinkPad-X1-Carbon-Gen-9.dhcp4.washington.edu ([10.154.74.143]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW19.10) with ESMTPSA id 40AJefnK020412 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 11:40:42 -0800 X-UW-Orig-Sender: kmatsui@smtp.washington.edu From: Ken Matsui To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Ken Matsui Subject: [PATCH 11/14] c++: Implement __is_signed built-in trait Date: Wed, 10 Jan 2024 01:23:02 -0800 Message-ID: <20240110194031.2384005-12-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240110194031.2384005-1-kmatsui@gcc.gnu.org> References: <20240110194031.2384005-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 X-Proofpoint-GUID: -6S251iXSJAr1N7qfP5x4GYw-PxYgplo X-Proofpoint-ORIG-GUID: -6S251iXSJAr1N7qfP5x4GYw-PxYgplo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-10_10,2024-01-10_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 clxscore=1034 adultscore=0 impostorscore=0 mlxscore=0 phishscore=0 priorityscore=1501 mlxlogscore=730 lowpriorityscore=0 malwarescore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401100156 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DATE_IN_PAST_06_12, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787733891862436175 X-GMAIL-MSGID: 1787733891862436175 This patch implements built-in trait for std::is_signed. gcc/cp/ChangeLog: * cp-trait.def: Define __is_signed. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SIGNED. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __is_signed. * g++.dg/ext/is_signed.C: New test. Signed-off-by: Ken Matsui --- gcc/cp/constraint.cc | 3 ++ gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 6 ++++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++ gcc/testsuite/g++.dg/ext/is_signed.C | 45 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 7e4b3cd38c4..d2e41aa053d 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3813,6 +3813,9 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_SCOPED_ENUM: inform (loc, " %qT is not a scoped enum", t1); break; + case CPTK_IS_SIGNED: + inform (loc, " %qT is not a signed type", t1); + break; case CPTK_IS_STD_LAYOUT: inform (loc, " %qT is not an standard layout type", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 734522eb5ba..6dac7622a7c 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -88,6 +88,7 @@ DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1) DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1) DEFTRAIT_EXPR (IS_SAME, "__is_same", 2) DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1) +DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1) DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1) DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1) DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 522db3f57e4..c3d6fc2d10f 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12548,6 +12548,11 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_SCOPED_ENUM: return SCOPED_ENUM_P (type1); + case CPTK_IS_SIGNED: + return (integral_type_p (type1) + || floating_point_type_p (type1)) + && TYPE_SIGN (type1) == SIGNED; + case CPTK_IS_STD_LAYOUT: return std_layout_type_p (type1); @@ -12740,6 +12745,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_REFERENCE: case CPTK_IS_SAME: case CPTK_IS_SCOPED_ENUM: + case CPTK_IS_SIGNED: case CPTK_IS_UNION: case CPTK_IS_UNSIGNED: break; diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index 062b06234e6..e3d16add403 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -146,6 +146,9 @@ #if !__has_builtin (__is_scoped_enum) # error "__has_builtin (__is_scoped_enum) failed" #endif +#if !__has_builtin (__is_signed) +# error "__has_builtin (__is_signed) failed" +#endif #if !__has_builtin (__is_standard_layout) # error "__has_builtin (__is_standard_layout) failed" #endif diff --git a/gcc/testsuite/g++.dg/ext/is_signed.C b/gcc/testsuite/g++.dg/ext/is_signed.C new file mode 100644 index 00000000000..a46ba54ac14 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_signed.C @@ -0,0 +1,45 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +#define SA_TEST_CATEGORY(TRAIT, X, expect) \ + SA(TRAIT(X) == expect); \ + SA(TRAIT(const X) == expect); \ + SA(TRAIT(volatile X) == expect); \ + SA(TRAIT(const volatile X) == expect) + +SA_TEST_CATEGORY(__is_signed, void, false); + +SA_TEST_CATEGORY(__is_signed, bool, bool(-1) < bool(0)); +SA_TEST_CATEGORY(__is_signed, char, char(-1) < char(0)); +SA_TEST_CATEGORY(__is_signed, signed char, true); +SA_TEST_CATEGORY(__is_signed, unsigned char, false); +SA_TEST_CATEGORY(__is_signed, wchar_t, wchar_t(-1) < wchar_t(0)); +SA_TEST_CATEGORY(__is_signed, short, true); +SA_TEST_CATEGORY(__is_signed, unsigned short, false); +SA_TEST_CATEGORY(__is_signed, int, true); +SA_TEST_CATEGORY(__is_signed, unsigned int, false); +SA_TEST_CATEGORY(__is_signed, long, true); +SA_TEST_CATEGORY(__is_signed, unsigned long, false); +SA_TEST_CATEGORY(__is_signed, long long, true); +SA_TEST_CATEGORY(__is_signed, unsigned long long, false); + +SA_TEST_CATEGORY(__is_signed, float, true); +SA_TEST_CATEGORY(__is_signed, double, true); +SA_TEST_CATEGORY(__is_signed, long double, true); + +#ifndef __STRICT_ANSI__ +// GNU Extensions. +#ifdef __SIZEOF_INT128__ +SA_TEST_CATEGORY(__is_signed, __int128, true); +SA_TEST_CATEGORY(__is_signed, unsigned __int128, false); +#endif + +#ifdef _GLIBCXX_USE_FLOAT128 +SA_TEST_CATEGORY(__is_signed, __float128, true); +#endif +#endif + +// Sanity check. +class ClassType { }; +SA_TEST_CATEGORY(__is_signed, ClassType, false);