public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-5266] c++: Fix up handling of non-dependent subscript with static operator[] [PR108437]
@ 2023-01-19 22:31 Jakub Jelinek
0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-01-19 22:31 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:86caab6c5d1e26e1c54c3dceacc873d6e27bfc09
commit r13-5266-g86caab6c5d1e26e1c54c3dceacc873d6e27bfc09
Author: Jakub Jelinek <jakub@redhat.com>
Date: Thu Jan 19 23:31:15 2023 +0100
c++: Fix up handling of non-dependent subscript with static operator[] [PR108437]
As the following testcases shows, when adding static operator[]
support I've missed that the 2 build_min_non_dep_op_overload functions
need to be adjusted. The first one we only use for the single index
case, but as cp_tree_code_length (ARRAY_REF) is 2, we were running
into an assertion there which compared nargs and expected_nargs.
For ARRAY_REF, the operator[] is either a non-static member or newly
static member, never out of class and for the static member case
if user uses single index the operator[] needs to have a single
argument as well, but the function is called with 2 - the object
it is invoked on and the index. We need to evaluate side-effects
of the object and use just a single argument in the call - the index.
The other build_min_non_dep_op_overload overload has been added
solely for ARRAY_REF - CALL_EXPR is the other operator that accepts
variable number of operands but that one goes through different
routines. There we asserted it is a METHOD_TYPE, so again
we shouldn't assert that but handle the case when it is not one
by making sure object's side-effects are evaluated if needed and
passing all the index arguments to the static operator[].
2023-01-19 Jakub Jelinek <jakub@redhat.com>
PR c++/108437
* cp-tree.h (keep_unused_object_arg): Declare.
* call.cc (keep_unused_object_arg): No longer static.
* tree.cc (build_min_non_dep_op_overload): Handle ARRAY_REF
with overload being static member function.
* g++.dg/cpp23/subscript12.C: New test.
* g++.dg/cpp23/subscript13.C: New test.
Diff:
---
gcc/cp/call.cc | 2 +-
gcc/cp/cp-tree.h | 1 +
gcc/cp/tree.cc | 30 ++++++++++++++++------
gcc/testsuite/g++.dg/cpp23/subscript12.C | 34 +++++++++++++++++++++++++
gcc/testsuite/g++.dg/cpp23/subscript13.C | 43 ++++++++++++++++++++++++++++++++
5 files changed, 102 insertions(+), 8 deletions(-)
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 0780b5840a3..991730713e6 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -5187,7 +5187,7 @@ build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
or static operator(), in which cases the source expression
would be `obj[...]' or `obj(...)'. */
-static tree
+tree
keep_unused_object_arg (tree result, tree obj, tree fn)
{
if (result == NULL_TREE
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index d3058c093e1..9f188724cf2 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6599,6 +6599,7 @@ inline tree build_new_op (const op_location_t &loc, enum tree_code code,
return build_new_op (loc, code, flags, arg1, arg2, NULL_TREE, NULL_TREE,
NULL, complain);
}
+extern tree keep_unused_object_arg (tree, tree, tree);
extern tree build_op_call (tree, vec<tree, va_gc> **,
tsubst_flags_t);
extern tree build_op_subscript (const op_location_t &, tree,
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 8060d39d216..c1da868732b 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3693,14 +3693,20 @@ build_min_non_dep_op_overload (enum tree_code op,
{
va_list p;
int nargs, expected_nargs;
- tree fn, call;
+ tree fn, call, obj = NULL_TREE;
non_dep = extract_call_expr (non_dep);
nargs = call_expr_nargs (non_dep);
expected_nargs = cp_tree_code_length (op);
- if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
+ if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE
+ /* For ARRAY_REF, operator[] is either a non-static member or newly
+ static member, never out of class and for the static member case
+ if user uses single index the operator[] needs to have a single
+ argument as well, but the function is called with 2 - the object
+ it is invoked on and the index. */
+ || op == ARRAY_REF)
expected_nargs -= 1;
if ((op == POSTINCREMENT_EXPR
|| op == POSTDECREMENT_EXPR)
@@ -3715,6 +3721,8 @@ build_min_non_dep_op_overload (enum tree_code op,
if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
{
fn = overload;
+ if (op == ARRAY_REF)
+ obj = va_arg (p, tree);
for (int i = 0; i < nargs; i++)
{
tree arg = va_arg (p, tree);
@@ -3746,6 +3754,8 @@ build_min_non_dep_op_overload (enum tree_code op,
CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
+ if (obj)
+ return keep_unused_object_arg (call, obj, overload);
return call;
}
@@ -3759,11 +3769,15 @@ build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
non_dep = extract_call_expr (non_dep);
unsigned int nargs = call_expr_nargs (non_dep);
- gcc_assert (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE);
- tree binfo = TYPE_BINFO (TREE_TYPE (object));
- tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
- tree fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
- object, method, NULL_TREE);
+ tree fn = overload;
+ if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
+ {
+ tree binfo = TYPE_BINFO (TREE_TYPE (object));
+ tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
+ fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
+ object, method, NULL_TREE);
+ object = NULL_TREE;
+ }
gcc_assert (vec_safe_length (args) == nargs);
tree call = build_min_non_dep_call_vec (non_dep, fn, args);
@@ -3774,6 +3788,8 @@ build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
+ if (object)
+ return keep_unused_object_arg (call, object, overload);
return call;
}
diff --git a/gcc/testsuite/g++.dg/cpp23/subscript12.C b/gcc/testsuite/g++.dg/cpp23/subscript12.C
new file mode 100644
index 00000000000..49cd8a6ac5b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/subscript12.C
@@ -0,0 +1,34 @@
+// PR c++/108437
+// { dg-do run { target c++23 } }
+
+struct S { static int &operator[] (int x) { static int a[2]; return a[x]; } };
+struct U { static int &operator[] (int x, int y, int z) { static int a[2]; return a[x + y + z]; } };
+struct V { static int &operator[] () { static int a; return a; } };
+
+template <class T, class W, class X> void
+foo ()
+{
+ S s;
+ s[0]++;
+ T t;
+ t[0]++;
+ U u;
+ u[0, 0, 0]++;
+ V v;
+ v[]++;
+ W w;
+ w[0, 0, 0]++;
+ X x;
+ x[]++;
+}
+
+int
+main ()
+{
+ S::operator[] (0) = 1;
+ U::operator[] (0, 0, 0) = 2;
+ V::operator[] () = 3;
+ foo <S, U, V> ();
+ if (S::operator[] (0) != 3 || U::operator[] (0, 0, 0) != 4 || V::operator[] () != 5)
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/cpp23/subscript13.C b/gcc/testsuite/g++.dg/cpp23/subscript13.C
new file mode 100644
index 00000000000..3fb3b4975b1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/subscript13.C
@@ -0,0 +1,43 @@
+// PR c++/108437
+// { dg-do run { target c++23 } }
+
+struct S { static int &operator[] (int x) { static int a[2]; return a[x]; } };
+struct U { static int &operator[] (int x, int y, int z) { static int a[2]; return a[x + y + z]; } };
+struct V { static int &operator[] () { static int a; return a; } };
+int cnt;
+
+template <typename T>
+T &
+bar (T &x)
+{
+ ++cnt;
+ return x;
+}
+
+template <class T, class W, class X> void
+foo ()
+{
+ S s;
+ bar (s)[0]++;
+ T t;
+ bar (t)[0]++;
+ U u;
+ bar (u)[0, 0, 0]++;
+ V v;
+ bar (v)[]++;
+ W w;
+ bar (w)[0, 0, 0]++;
+ X x;
+ bar (x)[]++;
+}
+
+int
+main ()
+{
+ S::operator[] (0) = 1;
+ U::operator[] (0, 0, 0) = 2;
+ V::operator[] () = 3;
+ foo <S, U, V> ();
+ if (S::operator[] (0) != 3 || U::operator[] (0, 0, 0) != 4 || V::operator[] () != 5 || cnt != 6)
+ __builtin_abort ();
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-01-19 22:31 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-19 22:31 [gcc r13-5266] c++: Fix up handling of non-dependent subscript with static operator[] [PR108437] Jakub Jelinek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).