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,
{
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)
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);
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;
}
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);
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;
}
--- /dev/null
+// 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 ();
+}
--- /dev/null
+// 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 ();
+}