From patchwork Wed Feb 21 08:51:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 1901917 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Dz/QsaXZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tfqms4QW6z23d4 for ; Wed, 21 Feb 2024 19:53:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 16BF23858D28 for ; Wed, 21 Feb 2024 08:53:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 16BF23858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708505590; bh=Hd06F/G5Tq9FkGsvmzXsz2TYO81A6BEBs3E94LvOK1I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Dz/QsaXZ/d0+bCR7BXJ1e3fA2CTCTFiGBepJCSjsQtZOcERzR+raVy0/vrtmZanGM bK0guvThTOrMoBYVdEpNMXSBMZz6n6X/vlkB9x/EsHZJ+8llyf7bc5zdjzeqZBmyeI 29s9l9gPrRUXo3jD7oDxhntbO8zRhPLG7XABuJ/Y= 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 902783858D35; Wed, 21 Feb 2024 08:52:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 902783858D35 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 902783858D35 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=1708505550; cv=none; b=DhokITB0TJzB1l9QtQAoKSiKUgZmmPl4eWNw2zQDCDB7pzpezlSwoHUT3FumBM7krJcqKbX2YfaKp+WLwCacQPcauEPmfGMKSJxxHw+QqwGmfbigttpbup6mJptotK+GFNgQEnlEq+W+az6EcUNJWMhXaY8hXt+0LeycCEEhyDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708505550; c=relaxed/simple; bh=nqJox3hKM+3rvxR1FeBCD6YWAgLx4nyOSWV85bBys2s=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=B9eGgGKJtQtBrtyW2u8E/yHdIGV/6u1UgldIobhKpM+G2Kdk5SnT/qTIwVrvB4CIDhc2z6XUpx0ulkLYsGtX4j1JDy/q6tyNOAcvSqQJgu3Kuq/EUsyqFULPDTNFTP1c4qGykEp0yi7jCdlDFHbrsvk06RxPAt0i1B3tBzRSo58= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247470.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41L8mirU032576; Wed, 21 Feb 2024 08:52:26 GMT Received: from mxout25.s.uw.edu (mxout25.s.uw.edu [140.142.234.175]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3wd56euc13-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Feb 2024 08:52:25 +0000 Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout25.s.uw.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 41L8q4jc011152 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 21 Feb 2024 00:52:04 -0800 X-Auth-Received: from kmatsui-ThinkPad-X1-Carbon-Gen-9.dhcp4.washington.edu ([10.154.74.128]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 41L8q4qB021339 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Feb 2024 00:52:04 -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 v12 25/26] c++: Implement __is_nothrow_invocable built-in trait Date: Wed, 21 Feb 2024 00:51:41 -0800 Message-ID: <20240221085142.3779280-25-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240221062041.3261695-23-kmatsui@gcc.gnu.org> References: <20240221062041.3261695-23-kmatsui@gcc.gnu.org> MIME-Version: 1.0 X-Proofpoint-GUID: 5Rqfnp1WHvUiRV2yoYl3Y_O2wMe2zmVl X-Proofpoint-ORIG-GUID: 5Rqfnp1WHvUiRV2yoYl3Y_O2wMe2zmVl X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-20_06,2024-02-20_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=886 adultscore=0 lowpriorityscore=0 spamscore=0 mlxscore=0 clxscore=1034 impostorscore=0 priorityscore=1501 suspectscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402210068 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org This patch implements built-in trait for std::is_nothrow_invocable. gcc/cp/ChangeLog: * cp-trait.def: Define __is_nothrow_invocable. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_NOTHROW_INVOCABLE. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/is_nothrow_invocable.C: New test. Signed-off-by: Ken Matsui --- gcc/cp/constraint.cc | 6 ++ gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 4 ++ .../g++.dg/ext/is_nothrow_invocable.C | 66 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index c87b126fdb1..43d4f2102d6 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3824,6 +3824,12 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_NOTHROW_CONVERTIBLE: inform (loc, " %qT is not nothrow convertible from %qE", t2, t1); break; + case CPTK_IS_NOTHROW_INVOCABLE: + if (!t2) + inform (loc, " %qT is not nothrow invocable", t1); + else + inform (loc, " %qT is not nothrow invocable by %qE", t1, t2); + break; case CPTK_IS_OBJECT: inform (loc, " %qT is not an object type", t1); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 6cb2b55f4ea..a9714921e94 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -84,6 +84,7 @@ DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1) DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2) DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1) DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2) +DEFTRAIT_EXPR (IS_NOTHROW_INVOCABLE, "__is_nothrow_invocable", -1) DEFTRAIT_EXPR (IS_OBJECT, "__is_object", 1) DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2) DEFTRAIT_EXPR (IS_POD, "__is_pod", 1) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 149c0631d62..dba7b43a109 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12494,6 +12494,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_NOTHROW_CONVERTIBLE: return is_nothrow_convertible (type1, type2); + case CPTK_IS_NOTHROW_INVOCABLE: + return expr_noexcept_p (build_invoke (type1, type2, tf_none), tf_none); + case CPTK_IS_OBJECT: return (type_code1 != FUNCTION_TYPE && type_code1 != REFERENCE_TYPE @@ -12689,6 +12692,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_NOTHROW_ASSIGNABLE: case CPTK_IS_NOTHROW_CONSTRUCTIBLE: case CPTK_IS_NOTHROW_CONVERTIBLE: + case CPTK_IS_NOTHROW_INVOCABLE: case CPTK_IS_TRIVIALLY_ASSIGNABLE: case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE: case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY: diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C b/gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C new file mode 100644 index 00000000000..b95307bb3a3 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C @@ -0,0 +1,66 @@ +// { dg-do compile { target c++11 } } + +#define SA(X) static_assert((X),#X) + +struct T { T(int) { } }; +struct NT { NT(int) noexcept { } }; +struct Ex { explicit Ex(int) noexcept { } }; + +using func_type = void(*)(); +SA( ! __is_nothrow_invocable(func_type) ); + +#if __cpp_noexcept_function_type +using func_type_nt = void(*)() noexcept; +SA( __is_nothrow_invocable(func_type_nt) ); +#endif + +struct X { }; +using mem_type = int X::*; + +SA( ! __is_nothrow_invocable(mem_type) ); +SA( ! __is_nothrow_invocable(mem_type, int) ); +SA( ! __is_nothrow_invocable(mem_type, int&) ); +SA( __is_nothrow_invocable(mem_type, X&) ); + +using memfun_type = int (X::*)(); + +SA( ! __is_nothrow_invocable(memfun_type) ); +SA( ! __is_nothrow_invocable(memfun_type, int) ); +SA( ! __is_nothrow_invocable(memfun_type, int&) ); +SA( ! __is_nothrow_invocable(memfun_type, X&) ); +SA( ! __is_nothrow_invocable(memfun_type, X*) ); + +#if __cpp_noexcept_function_type +using memfun_type_nt = int (X::*)() noexcept; + +SA( ! __is_nothrow_invocable(memfun_type_nt) ); +SA( ! __is_nothrow_invocable(memfun_type_nt, int) ); +SA( ! __is_nothrow_invocable(memfun_type_nt, int&) ); +SA( __is_nothrow_invocable(memfun_type_nt, X&) ); +SA( __is_nothrow_invocable(memfun_type_nt, X*) ); +#endif + +struct F { + int& operator()(); + long& operator()() const noexcept; + short& operator()(int) &&; + char& operator()(int) const& noexcept; +private: + void operator()(int, int) noexcept; +}; +using CF = const F; + +SA( ! __is_nothrow_invocable(F ) ); +SA( __is_nothrow_invocable(CF) ); + +SA( ! __is_nothrow_invocable(F, int) ); +SA( __is_nothrow_invocable(F&, int) ); + +SA( __is_nothrow_invocable(CF, int) ); +SA( __is_nothrow_invocable(CF&, int) ); +SA( ! __is_nothrow_invocable(F, int, int) ); + +struct FX { + X operator()() const noexcept { return {}; } +}; +SA( __is_nothrow_invocable(FX) );