From patchwork Thu Mar 23 00:41:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 73729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2649878wrt; Wed, 22 Mar 2023 17:43:13 -0700 (PDT) X-Google-Smtp-Source: AK7set8U7e2iZkJTjflr/NefQWpYg2hWVxSOibweUdVfsLGUJh4sPS5RG4pHRQY9efnfKzN/VaoI X-Received: by 2002:a05:6402:742:b0:4fb:3054:232e with SMTP id p2-20020a056402074200b004fb3054232emr8351701edy.26.1679532193250; Wed, 22 Mar 2023 17:43:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679532193; cv=none; d=google.com; s=arc-20160816; b=pxtwozSlLUndC3q6SxS2xpTmw4uFO12T/kcPkQY0oN9Om3jhJZUj8xu/Lh7uXV3wjM 3BfBShl8HVRRl5+jLl5c1SmlRX0f6YNsvvlEN4CHw5VemsQ7bTHYpVgJevt4Cvu2Z7od j9FyBmemlV1bqaNnhRgbrjILW7Gj9adWqvxp+kHTV7iWInPOK2MSDmME2gMia7LQ7H2E mXh6mwSSRmIeq8mqi+ChW7xtI43e19YWtiQGDhz5RUuFKKnjPGQmMuNmfPxmHUyQadJc O7SUPpiFE5xSGlmbWI/As/zltjHXVaoj+8cjQ49V02Md7dq9Ddda1alF4DW+255NRt3G bhNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from: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:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=Oe0/ttpNBHUqfSbonxb/ICW/f51MDzU8uOJ9Xu3cSAk=; b=sKgYwnEgNKtDqPXhbQRSJwie+dugjsGs11ft9tbU4iz6qyXSEIp21pIxJONnUCmHL7 Lp6m4xXp0YatoCxAfEZuiJRVGIbfFbG4N0fenpuomUu7PX9kBSSkLs6RFRK3Zgl/F1FJ G6KvDwkfljcyfUJVraYUX+8Kmk+//tF3R0IwS0r/8Ygvgr4dlxsRe4ljjwJWK1ssy+gu lnYQWOZml6U3GlGL5UNdlmkCJMkRhu1Edjf6SSNVVgANAv6qMAvs0ax05WEh85+8boIz r4+c3xqaZbv+GqfuwKisc3t9P2YaKvbNuz8XJCTjZJQ5Fl1YViAYY4o4F8YqP8NYBmm/ 8x2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="bG/5Rg6b"; 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g22-20020aa7d1d6000000b004bf976a1b56si17164975edp.627.2023.03.22.17.43.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Mar 2023 17:43:13 -0700 (PDT) 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="bG/5Rg6b"; 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 4F453384D14C for ; Thu, 23 Mar 2023 00:42:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F453384D14C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679532174; bh=Oe0/ttpNBHUqfSbonxb/ICW/f51MDzU8uOJ9Xu3cSAk=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=bG/5Rg6bMt15P6RR4Jlx/PgcA39NhbkePM+6ahIuZQH7kLDq6MgPutVuFSH2F7viV szDZgdnTMzxLnSSS5Lv/EiOUDnMp97sbeeEVCub4qIJ9m2lq6EPU/M3Fg9mwk0LtJI gfBJarn8chkrgPGo4SbiMm6q/f8wwXNk3VpHxuiE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00641c01.pphosted.com (mx0b-00641c01.pphosted.com [205.220.177.146]) by sourceware.org (Postfix) with ESMTPS id 6B5F1385843D; Thu, 23 Mar 2023 00:42:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B5F1385843D Received: from pps.filterd (m0247481.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32N0WKAT026955; Thu, 23 Mar 2023 00:42:05 GMT Received: from mxout25.s.uw.edu (mxout25.s.uw.edu [140.142.234.175]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3pg9teh0hk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 23 Mar 2023 00:42:05 +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 32N0faDU027411 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 22 Mar 2023 17:41:37 -0700 X-Auth-Received: from localhost.localdomain ([10.154.74.245]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW19.10) with ESMTPSA id 32N0faYE019455 (version=TLSv1.2 cipher=DHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 22 Mar 2023 17:41:36 -0700 X-UW-Orig-Sender: kmatsui@smtp.washington.edu To: jwakely@redhat.com Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org, Ken Matsui Subject: [PATCH v2 1/2] c++: implement __is_unsigned built-in trait Date: Wed, 22 Mar 2023 17:41:21 -0700 Message-Id: <20230323004122.34600-1-kmatsui@cs.washington.edu> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: i5LrGARwpQfwkLFf3YJhTZ6tQOpfniI1 X-Proofpoint-GUID: i5LrGARwpQfwkLFf3YJhTZ6tQOpfniI1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-22_20,2023-03-22_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 mlxscore=0 clxscore=1011 bulkscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 priorityscore=1501 spamscore=0 suspectscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303150002 definitions=main-2303230002 X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ken Matsui via Gcc-patches From: Ken Matsui Reply-To: Ken Matsui Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760996230567308816?= X-GMAIL-MSGID: =?utf-8?q?1761117148799986795?= This patch implements built-in trait for std::is_unsigned. gcc/cp/ChangeLog: * cp-trait.def: Define __is_unsigned. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNSIGNED. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __is_unsigned. * g++.dg/ext/is_unsigned.C: New test. --- gcc/cp/constraint.cc | 3 ++ gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 4 ++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++ gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 273d15ab097..bc4c3d3ec57 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3747,6 +3747,9 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_UNION: inform (loc, " %qT is not a union", t1); break; + case CPTK_IS_UNSIGNED: + inform (loc, " %qT is not an unsigned type", t1); + break; case CPTK_IS_AGGREGATE: inform (loc, " %qT is not an aggregate", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index bac593c0094..9f1fff9043e 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -82,6 +82,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2) DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1) DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1) DEFTRAIT_EXPR (IS_UNION, "__is_union", 1) +DEFTRAIT_EXPR (IS_UNSIGNED, "__is_unsigned", 1) DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2) DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2) /* FIXME Added space to avoid direct usage in GCC 13. */ diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 87c2e8a7111..d43e2543490 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12031,6 +12031,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_UNION: return type_code1 == UNION_TYPE; + case CPTK_IS_UNSIGNED: + return TYPE_UNSIGNED (type1); + case CPTK_IS_ASSIGNABLE: return is_xible (MODIFY_EXPR, type1, type2); @@ -12200,6 +12203,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_ENUM: case CPTK_IS_UNION: case CPTK_IS_SAME: + case CPTK_IS_UNSIGNED: break; case CPTK_IS_LAYOUT_COMPATIBLE: diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index f343e153e56..20bf8e6cad5 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -146,3 +146,6 @@ #if !__has_builtin (__remove_cvref) # error "__has_builtin (__remove_cvref) failed" #endif +#if !__has_builtin (__is_unsigned) +# error "__has_builtin (__is_unsigned) failed" +#endif diff --git a/gcc/testsuite/g++.dg/ext/is_unsigned.C b/gcc/testsuite/g++.dg/ext/is_unsigned.C new file mode 100644 index 00000000000..2bb45d209a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_unsigned.C @@ -0,0 +1,47 @@ +// { dg-do compile { target c++11 } } + +#include + +using namespace __gnu_test; + +#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_unsigned, void, false); + +SA_TEST_CATEGORY(__is_unsigned, bool, (bool(-1) > bool(0))); +SA_TEST_CATEGORY(__is_unsigned, char, (char(-1) > char(0))); +SA_TEST_CATEGORY(__is_unsigned, signed char, false); +SA_TEST_CATEGORY(__is_unsigned, unsigned char, true); +SA_TEST_CATEGORY(__is_unsigned, wchar_t, (wchar_t(-1) > wchar_t(0))); +SA_TEST_CATEGORY(__is_unsigned, short, false); +SA_TEST_CATEGORY(__is_unsigned, unsigned short, true); +SA_TEST_CATEGORY(__is_unsigned, int, false); +SA_TEST_CATEGORY(__is_unsigned, unsigned int, true); +SA_TEST_CATEGORY(__is_unsigned, long, false); +SA_TEST_CATEGORY(__is_unsigned, unsigned long, true); +SA_TEST_CATEGORY(__is_unsigned, long long, false); +SA_TEST_CATEGORY(__is_unsigned, unsigned long long, true); + +SA_TEST_CATEGORY(__is_unsigned, float, false); +SA_TEST_CATEGORY(__is_unsigned, double, false); +SA_TEST_CATEGORY(__is_unsigned, long double, false); + +#ifndef __STRICT_ANSI__ +// GNU Extensions. +#ifdef __SIZEOF_INT128__ +SA_TEST_CATEGORY(__is_unsigned, unsigned __int128, true); +SA_TEST_CATEGORY(__is_unsigned, __int128, false); +#endif + +#ifdef _GLIBCXX_USE_FLOAT128 +SA_TEST_CATEGORY(__is_unsigned, __float128, false); +#endif +#endif + +// Sanity check. +SA_TEST_CATEGORY(__is_unsigned, ClassType, false);