Re: [PATCH] c++: Implement C++23 P2589R1 - - static operator[]

2022-11-14 Thread Jason Merrill via Gcc-patches

On 11/10/22 21:40, Jakub Jelinek wrote:

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.


Indeed.  The code in build_new_method_call for this case has the comment

  /* In an expression of the form `a->f()' where `f' turns 

 out to be a static member function, `a' is 


 none-the-less evaluated.  */


Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
provided the paper gets voted into C++23?


OK.


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.

--- gcc/c-family/c-cppbuiltin.cc.jj 2022-10-14 09:35:56.182990495 +0200
+++ gcc/c-family/c-cppbuiltin.cc2022-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

[PATCH] c++: Implement C++23 P2589R1 - - static operator[]

2022-11-10 Thread Jakub Jelinek via Gcc-patches
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.

--- gcc/c-family/c-cppbuiltin.cc.jj 2022-10-14 09:35:56.182990495 +0200
+++ gcc/c-family/c-cppbuiltin.cc2022-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);
+