From patchwork Fri Nov 11 07:40:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 18522 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp598569wru; Thu, 10 Nov 2022 23:41:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf4Tt7SvvAtIgOaSK36UvT4BLZ1WvRooRhHmNqVBbqoMVwUxdcrwrK0VPRKKuHh54gZYjnD0 X-Received: by 2002:a05:6402:1241:b0:461:e3e1:bc3b with SMTP id l1-20020a056402124100b00461e3e1bc3bmr439399edw.145.1668152506418; Thu, 10 Nov 2022 23:41:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668152506; cv=none; d=google.com; s=arc-20160816; b=fU3IvxwlgXswWJ4GdHb4O/q8dG/spLXs289X8S22Qw1AgbREvbPoXehUcLeCSvtzV0 Ta0nwOoO6EKXRptaFOPBPOd7q3J5XI50759+S+ljg/4BC0V7QRc2vVO0PqG57VGi9HzA Z7U8CjnVngsGk7J2dJb1IpArEvN7+QQGCrudJIczfEPwU9dfAKvlTFZheiE28GdTaqFs pgFLeLVx+sbAHmm069vRgOzaGzlzkksuMLYnElBcD6Lpew+V8QutIspZAcx6KGsXnyTi VWnhcB5VLY4KdM8Bm+J61G7akBT8MaUrEWiqkfJ2hlX+whOouYwhgcoM+GJ2j4LH7iVI 46Yg== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=52B63pnsbjvw7bnrQ4K0uj3xRVjMQYnGRNMiwqXcV+E=; b=DMNaQ5FChfj2oqoh0863bTELMSbm38OrrmR1qrWb8OfDaAhxIuHZ0rgNTazf4sRlUq IIZEqFXK7Rwk1Cuv3t3d+QWBp8h656I0z+cRgJJVrhVZdRgCGCdYqzp2cY7lVhnIvFdm 8/ksGm+HUdJQjOUUG/UE1U9mi8DqfBkAhB98OL2DE5QRxzSZ3qbLSt2VyBi7YV+gbmVP QDGQ8AgzsHI6W6XdrYeaKBJDsYE2EQYhRg9MAoPHTAlQCmH0ph8GUoqODqmhaxSPcCX2 xzKdMpuLftFZDBu6oifE5xtvkZPHlmhbTvjZAlAdrhBMMGtjUngPWImBMNzDX8GHlKNe ylmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fzsoKvI4; 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 i10-20020a0564020f0a00b004622a17f12bsi1645600eda.259.2022.11.10.23.41.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 23:41:46 -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=fzsoKvI4; 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 B29F3384F031 for ; Fri, 11 Nov 2022 07:41:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B29F3384F031 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668152496; bh=52B63pnsbjvw7bnrQ4K0uj3xRVjMQYnGRNMiwqXcV+E=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=fzsoKvI4vk689nJj2lhS0IsiK3/C2bP1CN5AlwavVKLa6+Hy3zasJ2sqh+tNsbC+e HHOuDL7G/h4YUpduPMEuuSP26gLb9Y0Kyvw+28rAjeGSmkZ6NR9dGwwhhrhx5JTaAH OMOxHbbEO40HunOelg5UJwk8i9P+CGh3An9ZrNaU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E01D23861C4D for ; Fri, 11 Nov 2022 07:40:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E01D23861C4D Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-450-DHAbZJXdPzW6HK_6Vy86TQ-1; Fri, 11 Nov 2022 02:40:48 -0500 X-MC-Unique: DHAbZJXdPzW6HK_6Vy86TQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8D48E1C05EA1 for ; Fri, 11 Nov 2022 07:40:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4A28E49BB69; Fri, 11 Nov 2022 07:40:48 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AB7eiMB3247616 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Nov 2022 08:40:45 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AB7ehEA3247615; Fri, 11 Nov 2022 08:40:43 +0100 Date: Fri, 11 Nov 2022 08:40:43 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Implement C++23 P2589R1 - - static operator[] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1749184682588398371?= X-GMAIL-MSGID: =?utf-8?q?1749184682588398371?= Hi! As stage1 is very close, here is a patch that implements the static operator[] paper. One thing that doesn't work properly is the same problem as I've filed yesterday for static operator() - PR107624 - that side-effects of the postfix-expression on which the call or subscript operator are applied are thrown away, I assume we have to add them into COMPOUND_EXPR somewhere after we find out that the we've chosen a static member function operator. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk provided the paper gets voted into C++23? 2022-11-11 Jakub Jelinek gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Bump C++23 __cpp_multidimensional_subscript macro value to 202211L. gcc/cp/ * decl.cc (grok_op_properties): Implement C++23 P2589R1 - static operator[]. Handle operator[] similarly to operator() - allow static member functions, but pedwarn on it for C++20 and older. Unlike operator(), perform rest of checks on it though for C++20. * call.cc (add_operator_candidates): For operator[] with class typed first parameter, pass that parameter as first_arg and an adjusted arglist without that parameter. gcc/testsuite/ * g++.dg/cpp23/subscript9.C: New test. * g++.dg/cpp23/feat-cxx2b.C: Expect a newer __cpp_multidimensional_subscript value. * g++.old-deja/g++.bugs/900210_10.C: Don't expect an error for C++23 or later. Jakub --- gcc/c-family/c-cppbuiltin.cc.jj 2022-10-14 09:35:56.182990495 +0200 +++ gcc/c-family/c-cppbuiltin.cc 2022-11-10 22:29:12.539832741 +0100 @@ -1075,7 +1075,7 @@ c_cpp_builtins (cpp_reader *pfile) cpp_define (pfile, "__cpp_size_t_suffix=202011L"); cpp_define (pfile, "__cpp_if_consteval=202106L"); cpp_define (pfile, "__cpp_constexpr=202110L"); - cpp_define (pfile, "__cpp_multidimensional_subscript=202110L"); + cpp_define (pfile, "__cpp_multidimensional_subscript=202211L"); cpp_define (pfile, "__cpp_named_character_escapes=202207L"); cpp_define (pfile, "__cpp_static_call_operator=202207L"); cpp_define (pfile, "__cpp_implicit_move=202207L"); --- gcc/cp/decl.cc.jj 2022-11-08 09:54:37.313400209 +0100 +++ gcc/cp/decl.cc 2022-11-10 21:26:06.891359343 +0100 @@ -15377,7 +15377,15 @@ grok_op_properties (tree decl, bool comp an enumeration, or a reference to an enumeration. 13.4.0.6 */ if (! methodp || DECL_STATIC_FUNCTION_P (decl)) { - if (operator_code == CALL_EXPR) + if (operator_code == TYPE_EXPR + || operator_code == COMPONENT_REF + || operator_code == NOP_EXPR) + { + error_at (loc, "%qD must be a non-static member function", decl); + return false; + } + + if (operator_code == CALL_EXPR || operator_code == ARRAY_REF) { if (! DECL_STATIC_FUNCTION_P (decl)) { @@ -15386,52 +15394,41 @@ grok_op_properties (tree decl, bool comp } if (cxx_dialect < cxx23 /* For lambdas we diagnose static lambda specifier elsewhere. */ - && ! LAMBDA_FUNCTION_P (decl) + && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl)) /* For instantiations, we have diagnosed this already. */ && ! DECL_USE_TEMPLATE (decl)) pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member " - "function only with %<-std=c++23%> or %<-std=gnu++23%>", decl); - /* There are no further restrictions on the arguments to an - overloaded "operator ()". */ - return true; - } - if (operator_code == TYPE_EXPR - || operator_code == COMPONENT_REF - || operator_code == ARRAY_REF - || operator_code == NOP_EXPR) - { - error_at (loc, "%qD must be a non-static member function", decl); - return false; + "function only with %<-std=c++23%> or %<-std=gnu++23%>", + decl); } - - if (DECL_STATIC_FUNCTION_P (decl)) + else if (DECL_STATIC_FUNCTION_P (decl)) { error_at (loc, "%qD must be either a non-static member " "function or a non-member function", decl); return false; } - - for (tree arg = argtypes; ; arg = TREE_CHAIN (arg)) - { - if (!arg || arg == void_list_node) - { - if (complain) - error_at(loc, "%qD must have an argument of class or " - "enumerated type", decl); - return false; - } + else + for (tree arg = argtypes; ; arg = TREE_CHAIN (arg)) + { + if (!arg || arg == void_list_node) + { + if (complain) + error_at (loc, "%qD must have an argument of class or " + "enumerated type", decl); + return false; + } - tree type = non_reference (TREE_VALUE (arg)); - if (type == error_mark_node) - return false; - - /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used - because these checks are performed even on template - functions. */ - if (MAYBE_CLASS_TYPE_P (type) - || TREE_CODE (type) == ENUMERAL_TYPE) - break; - } + tree type = non_reference (TREE_VALUE (arg)); + if (type == error_mark_node) + return false; + + /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used + because these checks are performed even on template + functions. */ + if (MAYBE_CLASS_TYPE_P (type) + || TREE_CODE (type) == ENUMERAL_TYPE) + break; + } } if (operator_code == CALL_EXPR) --- gcc/cp/call.cc.jj 2022-11-08 09:54:37.283400616 +0100 +++ gcc/cp/call.cc 2022-11-10 22:26:38.096936491 +0100 @@ -6589,12 +6589,36 @@ add_operator_candidates (z_candidate **c if (fns == error_mark_node) return error_mark_node; if (fns) - add_candidates (BASELINK_FUNCTIONS (fns), - NULL_TREE, arglist, NULL_TREE, - NULL_TREE, false, - BASELINK_BINFO (fns), - BASELINK_ACCESS_BINFO (fns), - flags, candidates, complain); + { + if (code == ARRAY_REF) + { + vec *restlist = make_tree_vector (); + for (unsigned i = 1; i < nargs; ++i) + vec_safe_push (restlist, (*arglist)[i]); + z_candidate *save_cand = *candidates; + add_candidates (BASELINK_FUNCTIONS (fns), + (*arglist)[0], restlist, NULL_TREE, + NULL_TREE, false, + BASELINK_BINFO (fns), + BASELINK_ACCESS_BINFO (fns), + flags, candidates, complain); + /* Release the vec if we didn't add a candidate that uses it. */ + for (z_candidate *c = *candidates; c != save_cand; c = c->next) + if (c->args == restlist) + { + restlist = NULL; + break; + } + release_tree_vector (restlist); + } + else + add_candidates (BASELINK_FUNCTIONS (fns), + NULL_TREE, arglist, NULL_TREE, + NULL_TREE, false, + BASELINK_BINFO (fns), + BASELINK_ACCESS_BINFO (fns), + flags, candidates, complain); + } } /* Per [over.match.oper]3.2, if no operand has a class type, then only non-member functions that have type T1 or reference to --- gcc/testsuite/g++.dg/cpp23/subscript9.C.jj 2022-11-10 21:06:58.155974655 +0100 +++ gcc/testsuite/g++.dg/cpp23/subscript9.C 2022-11-11 08:26:26.502445107 +0100 @@ -0,0 +1,29 @@ +// P2589R1 +// { dg-do run { target c++23 } } + +extern "C" void abort (); + +struct S +{ + S () {}; + static int &operator[] () { return a[0]; } + static int &operator[] (int x) { return a[x]; } + static int &operator[] (int x, long y) { return a[x + y * 8]; } + static int a[64]; +}; +int S::a[64]; + +int +main () +{ + S s; + for (int i = 0; i < 64; i++) + s.a[i] = 64 - i; + if (s[] != 64 || s[3] != 61 || s[4, 5] != 20) + abort (); + s[]++; + s[42]++; + ++s[3, 2]; + if (s.a[0] != 65 || s.a[42] != 23 || s.a[19] != 46) + abort (); +} --- gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C.jj 2022-10-06 08:56:28.785125656 +0200 +++ gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C 2022-11-10 22:31:49.187698955 +0100 @@ -556,8 +556,8 @@ #ifndef __cpp_multidimensional_subscript # error "__cpp_multidimensional_subscript" -#elif __cpp_multidimensional_subscript != 202110 -# error "__cpp_multidimensional_subscript != 202110" +#elif __cpp_multidimensional_subscript != 202211 +# error "__cpp_multidimensional_subscript != 202211" #endif #ifndef __cpp_named_character_escapes --- gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C.jj 2020-01-14 20:02:47.021606269 +0100 +++ gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C 2022-11-11 01:41:13.840971578 +0100 @@ -9,7 +9,7 @@ // keywords: operator[], static function members struct struct0 { - static int operator[] (); /* { dg-error "" } */ + static int operator[] (); /* { dg-error "" "" { target c++20_down } } */ }; int main () { return 0; }