From patchwork Mon Oct 16 00:10:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 153112 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3160324vqb; Sun, 15 Oct 2023 17:27:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXg4Q25SGoBZeAP6mU1bw3EGj0eDbvfNnFB64cJ/8ozYAZDWw4D6xMxp5Tulssopj/y+JI X-Received: by 2002:a05:620a:2446:b0:770:96a1:558b with SMTP id h6-20020a05620a244600b0077096a1558bmr36361880qkn.14.1697416074797; Sun, 15 Oct 2023 17:27:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697416074; cv=pass; d=google.com; s=arc-20160816; b=GS6Ukkkk+Q9LhwupJYCo2KHfbSa+bjAutne1MEpJIa6QXZ8zRZ3DvVCAxl+xpILQ+p aA/kLIpd4AvXt5QhPn7ix7NQyS8yVP6XxtQqSDkkd2ObX+9xDF39OzykcNIjqOE90nmx QEHn2MsTUrAcGJ7nJjBcR1cas/unFq1NjKX82lAuoeEYzd9/fGDsBAYp2aoi/gkz7r1F C1a8kU+iJXc7gssNX6MqFsjZqzJ6Vej+RIJy1RWC7DO+wyhlxGw/qeYJJ29vVwUsg8hV kIoL+2O9qgjir0MbuSzSqOFca6l5MTUVk5kje4fLJ8vQlKF3O7GQInCvIVJkNVWcLo17 /Lzg== 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=f7z37DsjAwyX042EfeSVKHpUntcym/J7MYn5lkdDXBU=; fh=kSSy/oyUXqQD15FLeKdymIgDBFPFITPA1Gj52CNOxRQ=; b=EVZNZ1sOTH6LkHdDNDHmMQiwpEGIQzLzE/KjSfSW+pc2HEX9Na5pbgeYq8s7jfW9QU YGlrwa2+SqGZZKnvlWD6aMQU8oV2tBnQjbWdupxBD/B2zsVGQ1T7YCPUuX2YlWZfzecY wJ7wQ1OfXMkSBwl1CfLmbDT4yvP2geepzBWMyWQxrpVUWEhll0zLaIZ3x+aDR6j7kWCq XIRhSrLYMsZXCx2/AFpfleXkZsn5sZkexZoldexaBiLU29k2eoRGXXv8uDfgXF51m9Yp 5ajAHVhUe/sCR0WU2CBsQQWpo4F5ZZ9zmvStU7gi+l3MomRIzYQ7ONLqPVwQx0iC/Qrr d3MA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wPFx8VSh; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dw17-20020a05620a601100b00773bbc97627si4523191qkb.467.2023.10.15.17.27.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 17:27:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wPFx8VSh; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 81EFC3858C66 for ; Mon, 16 Oct 2023 00:27:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81EFC3858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697416074; bh=f7z37DsjAwyX042EfeSVKHpUntcym/J7MYn5lkdDXBU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=wPFx8VShSz5wjzQGvuO6WkkuA8/3IG09Ioh1iwuZ3f1KvjkgMR74PMuPgaLf8VFfv g+3Dd+4D5eU7vjl822NQ1uNQAmuxpTvyttZLrJ29Rl5jk0/4XH6GGiZ5kp0HixZPkF y/SDaLjlqWrippMLZ10lgf2u6Y++W7Z8Zv6tAPvE= 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 C1C1C3857BA4; Mon, 16 Oct 2023 00:14:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1C1C3857BA4 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 C1C1C3857BA4 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=1697415284; cv=none; b=nYQZOjFAAQZkD0q/9AQNpIVH7Cr99/QDTA5wfTJtViHNQ2A/4S3HfYkiTtbI96YRygl/sR2LAvsyFFq5cGeLofFlwALotGmbMYo49UnqJo8/qLNJZINITeLkiXukHn8r7BTptIEuSy08OXQr2ehdqofCIC3XVQxW8rmwQPhxXVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697415284; c=relaxed/simple; bh=e0naa+kjY6ClTqeN2DAAC63a3CX+tx4Hxum4u+PM4us=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=B4w/2ek27tTP61MxftOTF8IzubMGGuK5qVdz+yBEMorE7/4njHcc4zcxUkGhDEq7V2PN0T0vrOtjcDZ/aCOewIgEGMLhi6ffSuCtink2+GaEkZGwbi+wJ0YA2wNZUnOfQ7/D3cPTka6p6zo61B4J4PAivX2DYws8NN0iJdwOOOs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247471.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39G02GX7021570; Mon, 16 Oct 2023 00:14:41 GMT Received: from mxout21.s.uw.edu (mxout21.s.uw.edu [140.142.32.139]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3tr4dk4c0a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 16 Oct 2023 00:14:41 +0000 Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout21.s.uw.edu (8.14.4+UW20.07/8.14.4+UW22.04) with ESMTP id 39G0EGm6011862 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 15 Oct 2023 17:14:16 -0700 X-Auth-Received: from kmatsui-ThinkPad-X1-Carbon-Gen-9.dhcp4.washington.edu ([10.19.18.191]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW19.10) with ESMTPSA id 39G0CSsL032329 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 15 Oct 2023 17:14:16 -0700 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 v20 31/40] c++: Implement __is_arithmetic built-in trait Date: Sun, 15 Oct 2023 17:10:15 -0700 Message-ID: <20231016001227.2717180-32-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231016001227.2717180-1-kmatsui@gcc.gnu.org> References: <20231013223957.1634024-1-kmatsui@gcc.gnu.org> <20231016001227.2717180-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: QRHNGQPQiv2_E10SdTmKGydpETDAoUKE X-Proofpoint-GUID: QRHNGQPQiv2_E10SdTmKGydpETDAoUKE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-15_09,2023-10-12_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 mlxscore=0 bulkscore=0 priorityscore=1501 spamscore=0 lowpriorityscore=0 mlxlogscore=856 adultscore=0 malwarescore=0 clxscore=1034 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310160000 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, 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 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: 1779677582756408093 X-GMAIL-MSGID: 1779869758471358606 This patch implements built-in trait for std::is_arithmetic. gcc/cp/ChangeLog: * cp-trait.def: Define __is_arithmetic. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic. * g++.dg/ext/is_arithmetic.C: New test. Signed-off-by: Ken Matsui --- 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_arithmetic.C | 33 ++++++++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index c9d627fa782..3a7f968eae8 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_AGGREGATE: inform (loc, " %qT is not an aggregate", t1); break; + case CPTK_IS_ARITHMETIC: + inform (loc, " %qT is not an arithmetic type", t1); + break; case CPTK_IS_ARRAY: inform (loc, " %qT is not an array", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index c60724e869e..b2be7b7bbd7 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1) DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1) DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1) +DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1) DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1) DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2) DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 83ed674b9d4..deab0134509 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12143,6 +12143,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_AGGREGATE: return CP_AGGREGATE_TYPE_P (type1); + case CPTK_IS_ARITHMETIC: + return ARITHMETIC_TYPE_P (type1); + case CPTK_IS_ARRAY: return type_code1 == ARRAY_TYPE; @@ -12406,6 +12409,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) return error_mark_node; break; + case CPTK_IS_ARITHMETIC: case CPTK_IS_ARRAY: case CPTK_IS_BOUNDED_ARRAY: case CPTK_IS_CLASS: diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index efce04fd09d..4bc85f4babb 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -56,6 +56,9 @@ #if !__has_builtin (__is_aggregate) # error "__has_builtin (__is_aggregate) failed" #endif +#if !__has_builtin (__is_arithmetic) +# error "__has_builtin (__is_arithmetic) failed" +#endif #if !__has_builtin (__is_array) # error "__has_builtin (__is_array) failed" #endif diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C new file mode 100644 index 00000000000..fd35831f646 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C @@ -0,0 +1,33 @@ +// { dg-do compile { target c++11 } } + +#include + +using namespace __gnu_test; + +#define SA(X) static_assert((X),#X) +#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \ + SA(TRAIT(TYPE) == EXPECT); \ + SA(TRAIT(const TYPE) == EXPECT); \ + SA(TRAIT(volatile TYPE) == EXPECT); \ + SA(TRAIT(const volatile TYPE) == EXPECT) + +SA_TEST_CATEGORY(__is_arithmetic, void, false); + +SA_TEST_CATEGORY(__is_arithmetic, char, true); +SA_TEST_CATEGORY(__is_arithmetic, signed char, true); +SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true); +SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true); +SA_TEST_CATEGORY(__is_arithmetic, short, true); +SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true); +SA_TEST_CATEGORY(__is_arithmetic, int, true); +SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true); +SA_TEST_CATEGORY(__is_arithmetic, long, true); +SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true); +SA_TEST_CATEGORY(__is_arithmetic, long long, true); +SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true); +SA_TEST_CATEGORY(__is_arithmetic, float, true); +SA_TEST_CATEGORY(__is_arithmetic, double, true); +SA_TEST_CATEGORY(__is_arithmetic, long double, true); + +// Sanity check. +SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);