From patchwork Wed Jan 10 09:23:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 187012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp1015235dyi; Wed, 10 Jan 2024 11:43:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpLtjjeYAUPRfpoe25MYmAilAfhh7O1LU5fy5vfLaerCknd0i5Gbxybi4OyKtZOoEYol3b X-Received: by 2002:ae9:e70a:0:b0:783:37fa:cd06 with SMTP id m10-20020ae9e70a000000b0078337facd06mr153736qka.92.1704915816974; Wed, 10 Jan 2024 11:43:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704915816; cv=pass; d=google.com; s=arc-20160816; b=ouyVcQRetgrTIihWF3x5Oru+9H4KJGZB4WKsn24orQpJqgPwC/jsFKVo26/qANuE/v P2SawElRaVCJvhb7CdhvlRZdr2CONV2SqSTrZtdVPvPQLkd9a6sg8EyHAMQwgDiGjKzx 3kmNAf/S/9R3tA9pMn2IWymhnuJBRKxkH+xJTDsT7D4KVjNCXgisCJRD8fSk+iljVH7h Oc76iYGbP+PnuO3K8VSzz53SGhWexnh9MHjaZ2vmwGU4w4HmZLTemN9r+OEPooyq8w0l DqIMeAJV6i6fZJYccY5Bz6gBnwnfose/8OMdEZL+5HndhGmYf78HarB8+npkyfICuuHJ cIUg== 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=lSo4fgKhtSQ3jYBeyRtBGt9w75fr6TiFX96c5VEekgU=; fh=kSSy/oyUXqQD15FLeKdymIgDBFPFITPA1Gj52CNOxRQ=; b=xseAUDChLWwyU5gYvssEIlFc8GrRopfhoRaDOFY8cD3Ql4ai+zGzOK8JiK041mYpuJ GWD1x5dPRGhEV3VoIilrj/fUaOlZrTlQQ2hVqDANZGV8sMFX75dpaQEHXVMExFbixD0h rS2MOsLcjjfAjm3PeQ6spi0wu+znjBxX4H0sfzA7P+JVAjvjh9tp51hpw7BZgJZfMnem SJ9OupBKLyytikfKmMmxuzrZOmwM9BVIr/GQiJtJp9jOP2wig9KXwZTWy3zc2YHcafGF SlX0rPpNOmOyIhQNcniPu0zyr4EHj5ypkGIYcpHrisQeQKtwDP2zfQYQ0XidqdB1jFMh uTzw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="YdHX0/aK"; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id az9-20020a05620a170900b0078335234933si2184011qkb.291.2024.01.10.11.43.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 11:43:36 -0800 (PST) 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="YdHX0/aK"; 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 9E65B3857B88 for ; Wed, 10 Jan 2024 19:43:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E65B3857B88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1704915816; bh=lSo4fgKhtSQ3jYBeyRtBGt9w75fr6TiFX96c5VEekgU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=YdHX0/aKy2R+Na4syZjy62muAtUHt93Ix+DJhAe3DGhZISkzimjCJDFiT72L8l1Zn fFTumkso8v7z02pcYt3GjSE/n/+U/gLR/X+ALLjz/tVVZIdnd8G07Ozn9svFA/HCbu M3JuxuDEJ98J+nMF5cK0BDK86nXFwBuY1AyYuNdY= 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 CBC5C3858C41; Wed, 10 Jan 2024 19:40:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBC5C3858C41 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 CBC5C3858C41 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=1704915654; cv=none; b=JyNCH35Yl16/irNA6PtYj2s6Tf870zurvEvItdwkC+sOl00zo7kO+fZybLIG7fo2BvclSQAM8pGWkyDuM2MWY7nXFgvNHObBwvtgTcIVmqjdOt3dAcI7gxfn2rANYiOQNnZp/Nq2ChgCPsablqelWgF+yA+G4brQpTw2nTpTZW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704915654; c=relaxed/simple; bh=x7oWPkWI842vO/4miEPfAYIUA+9xXIORB1Dov6nfIF4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=gv7LdhyOpHzQkTRgQjdsFFrSrVijC1Z34aeu4ytMB5f4WaorFrWAttY830tecT0/bBPg32x2EXiwRhgluPP3P9E1ZlWR+i7BvWCIo96eN5pYI8yiCOWippRA6o1gnKqOn4bdA/JgqFyJQ0AcZJSSuriVvhSSSoG3S2I0vxZKoeM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247480.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40AJckxU010557; Wed, 10 Jan 2024 19:40:52 GMT Received: from mxout21.s.uw.edu (mxout21.s.uw.edu [140.142.32.139]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3vj1010eeg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 19:40:51 +0000 Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.54]) by mxout21.s.uw.edu (8.14.4+UW20.07/8.14.4+UW22.04) with ESMTP id 40AJehoi015830 (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 40AJefnM020412 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 10 Jan 2024 11:40:43 -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 13/14] c++: Implement __is_scalar built-in trait Date: Wed, 10 Jan 2024 01:23:04 -0800 Message-ID: <20240110194031.2384005-14-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-ORIG-GUID: e0Offga51Jtim6lJFHQ_oTgjuEe1o2b1 X-Proofpoint-GUID: e0Offga51Jtim6lJFHQ_oTgjuEe1o2b1 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 impostorscore=0 lowpriorityscore=0 spamscore=0 bulkscore=0 mlxscore=0 suspectscore=0 mlxlogscore=633 clxscore=1034 priorityscore=1501 adultscore=0 phishscore=0 malwarescore=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_H2, 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: 1787733808027320578 X-GMAIL-MSGID: 1787733808027320578 This patch implements built-in trait for std::is_scalar. gcc/cp/ChangeLog: * cp-trait.def: Define __is_scalar. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCALAR. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of __is_scalar. * g++.dg/ext/is_scalar.C: New test. Signed-off-by: Ken Matsui --- gcc/cp/constraint.cc | 3 +++ gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 10 +++++++++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++ gcc/testsuite/g++.dg/ext/is_scalar.C | 28 ++++++++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index d2e41aa053d..7293f33c676 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3810,6 +3810,9 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_SAME: inform (loc, " %qT is not the same as %qT", t1, t2); break; + case CPTK_IS_SCALAR: + inform (loc, " %qT is not a scalar type", t1); + break; case CPTK_IS_SCOPED_ENUM: inform (loc, " %qT is not a scoped enum", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 6dac7622a7c..48e195b4938 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -87,6 +87,7 @@ DEFTRAIT_EXPR (IS_POD, "__is_pod", 1) DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1) DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1) DEFTRAIT_EXPR (IS_SAME, "__is_same", 2) +DEFTRAIT_EXPR (IS_SCALAR, "__is_scalar", 1) 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) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index c3d6fc2d10f..2426ba629d9 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12545,6 +12545,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_SAME: return same_type_p (type1, type2); + case CPTK_IS_SCALAR: + return (TYPE_PTRDATAMEM_P (type1) + || TREE_CODE (type1) == ENUMERAL_TYPE + || integral_type_p (type1) + || floating_point_type_p (type1) + || TYPE_PTR_P (type1) + || TYPE_PTRMEMFUNC_P (type1) + || NULLPTR_TYPE_P (type1)); + case CPTK_IS_SCOPED_ENUM: return SCOPED_ENUM_P (type1); @@ -12744,6 +12753,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_OBJECT: case CPTK_IS_REFERENCE: case CPTK_IS_SAME: + case CPTK_IS_SCALAR: case CPTK_IS_SCOPED_ENUM: case CPTK_IS_SIGNED: case CPTK_IS_UNION: diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index e3d16add403..c860f7e12ca 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -143,6 +143,9 @@ #if !__has_builtin (__is_same_as) # error "__has_builtin (__is_same_as) failed" #endif +#if !__has_builtin (__is_scalar) +# error "__has_builtin (__is_scalar) failed" +#endif #if !__has_builtin (__is_scoped_enum) # error "__has_builtin (__is_scoped_enum) failed" #endif diff --git a/gcc/testsuite/g++.dg/ext/is_scalar.C b/gcc/testsuite/g++.dg/ext/is_scalar.C new file mode 100644 index 00000000000..ad4c2d7ea05 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_scalar.C @@ -0,0 +1,28 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \ + SA(TRAIT(TYPE) == EXPECT); \ + SA(TRAIT(const TYPE) == EXPECT) + +#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) + +class ClassType { }; +enum EnumType { e0 }; + +SA_TEST_CATEGORY(__is_scalar, int, true); +SA_TEST_CATEGORY(__is_scalar, float, true); +SA_TEST_CATEGORY(__is_scalar, EnumType, true); +SA_TEST_CATEGORY(__is_scalar, int*, true); +SA_TEST_FN(__is_scalar, int(*)(int), true); +SA_TEST_CATEGORY(__is_scalar, int (ClassType::*), true); +SA_TEST_FN(__is_scalar, int (ClassType::*) (int), true); +SA_TEST_CATEGORY(__is_scalar, decltype(nullptr), true); + +// Sanity check. +SA_TEST_CATEGORY(__is_scalar, ClassType, false);