From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id D0D1F382EF36; Tue, 15 Nov 2022 07:18:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0D1F382EF36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668496710; bh=Rxvnt2sxdhujCYYdm0wDL2WNHsmbrNvj3jTrRwgymBU=; h=From:To:Subject:Date:From; b=GtmVO+qT7xuxcm6JwaLuMO8L27+Ypj+MEpLqwuwuCFdnVAx1YjbIxj+axSLfhl/pl w7+xFyMN0mIoqiw1mh8zfpw+RmGLNHmQswfj3WNZE0f0O+5GF6i2bOMRxjsVRWUpGx ynga+L7m8LFFYvz+phEu1btTlwRElxVtdThvSTv8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4046] c++: Implement C++23 P2589R1 - - static operator[] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: e0f4fcf9dfb8794a11d4ee63899b820aa9257f50 X-Git-Newrev: 6492cec069bccf817ac5e984fb3eca407056a566 Message-Id: <20221115071830.D0D1F382EF36@sourceware.org> Date: Tue, 15 Nov 2022 07:18:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6492cec069bccf817ac5e984fb3eca407056a566 commit r13-4046-g6492cec069bccf817ac5e984fb3eca407056a566 Author: Jakub Jelinek Date: Tue Nov 15 08:00:21 2022 +0100 c++: Implement C++23 P2589R1 - - static operator[] 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. 2022-11-15 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. Diff: --- gcc/c-family/c-cppbuiltin.cc | 2 +- gcc/cp/call.cc | 36 ++++++++++--- gcc/cp/decl.cc | 71 ++++++++++++------------- gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C | 4 +- gcc/testsuite/g++.dg/cpp23/subscript9.C | 29 ++++++++++ gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C | 2 +- 6 files changed, 97 insertions(+), 47 deletions(-) diff --git a/gcc/c-family/c-cppbuiltin.cc b/gcc/c-family/c-cppbuiltin.cc index cdb658f6ac9..f0be0598190 100644 --- a/gcc/c-family/c-cppbuiltin.cc +++ b/gcc/c-family/c-cppbuiltin.cc @@ -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"); diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index ef618d5c485..dbb01ec6ee5 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -6589,12 +6589,36 @@ add_operator_candidates (z_candidate **candidates, 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 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 890cfcabd35..c83fdac4984 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -15377,7 +15377,15 @@ grok_op_properties (tree decl, bool complain) 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 complain) } 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; + "function only with %<-std=c++23%> or %<-std=gnu++23%>", + decl); } - 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; - } - - 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) diff --git a/gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C b/gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C index efe97703a98..85e66871fd1 100644 --- a/gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C +++ b/gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C @@ -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 diff --git a/gcc/testsuite/g++.dg/cpp23/subscript9.C b/gcc/testsuite/g++.dg/cpp23/subscript9.C new file mode 100644 index 00000000000..72cea4b8cab --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/subscript9.C @@ -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 (); +} diff --git a/gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C b/gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C index b1b1067276d..7fbee3f5c43 100644 --- a/gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C +++ b/gcc/testsuite/g++.old-deja/g++.bugs/900210_10.C @@ -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; }