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");
}
if (flag_concepts)
{
/* -Wcomma-subscript is enabled by default in C++20. */
SET_OPTION_IF_UNSET (&global_options, &global_options_set,
warn_comma_subscript,
- cxx_dialect >= cxx20 && warn_deprecated);
+ cxx_dialect >= cxx23
+ || (cxx_dialect == cxx20 && warn_deprecated));
/* -Wvolatile is enabled by default in C++20. */
SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
}
}
-/* Subroutine of build_new_op_1: Add to CANDIDATES all candidates for the
+/* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
operator indicated by CODE/CODE2. This function calls itself recursively to
handle C++20 rewritten comparison operator candidates. */
return NULL_TREE;
}
+/* Build a new call to operator[]. This may change ARGS. */
+
+tree
+build_op_subscript (const op_location_t &loc, tree obj,
+ vec<tree, va_gc> **args, tree *overload,
+ tsubst_flags_t complain)
+{
+ struct z_candidate *candidates = 0, *cand;
+ tree fns, first_mem_arg = NULL_TREE;
+ bool any_viable_p;
+ tree result = NULL_TREE;
+ void *p;
+
+ auto_cond_timevar tv (TV_OVERLOAD);
+
+ obj = mark_lvalue_use (obj);
+
+ if (error_operand_p (obj))
+ return error_mark_node;
+
+ tree type = TREE_TYPE (obj);
+
+ obj = prep_operand (obj);
+
+ if (TYPE_BINFO (type))
+ {
+ fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
+ 1, complain);
+ if (fns == error_mark_node)
+ return error_mark_node;
+ }
+ else
+ fns = NULL_TREE;
+
+ if (args != NULL && *args != NULL)
+ {
+ *args = resolve_args (*args, complain);
+ if (*args == NULL)
+ return error_mark_node;
+ }
+
+ /* Get the high-water mark for the CONVERSION_OBSTACK. */
+ p = conversion_obstack_alloc (0);
+
+ if (fns)
+ {
+ first_mem_arg = obj;
+
+ add_candidates (BASELINK_FUNCTIONS (fns),
+ first_mem_arg, *args, NULL_TREE,
+ NULL_TREE, false,
+ BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
+ LOOKUP_NORMAL, &candidates, complain);
+ }
+
+ /* Be strict here because if we choose a bad conversion candidate, the
+ errors we get won't mention the call context. */
+ candidates = splice_viable (candidates, true, &any_viable_p);
+ if (!any_viable_p)
+ {
+ if (complain & tf_error)
+ {
+ auto_diagnostic_group d;
+ error ("no match for call to %<%T::operator[] (%A)%>",
+ TREE_TYPE (obj), build_tree_list_vec (*args));
+ print_z_candidates (loc, candidates);
+ }
+ result = error_mark_node;
+ }
+ else
+ {
+ cand = tourney (candidates, complain);
+ if (cand == 0)
+ {
+ if (complain & tf_error)
+ {
+ auto_diagnostic_group d;
+ error ("call of %<%T::operator[] (%A)%> is ambiguous",
+ TREE_TYPE (obj), build_tree_list_vec (*args));
+ print_z_candidates (loc, candidates);
+ }
+ result = error_mark_node;
+ }
+ else if (TREE_CODE (cand->fn) == FUNCTION_DECL
+ && DECL_OVERLOADED_OPERATOR_P (cand->fn)
+ && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
+ {
+ if (overload)
+ *overload = cand->fn;
+ result = build_over_call (cand, LOOKUP_NORMAL, complain);
+ if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
+ /* There won't be a CALL_EXPR. */;
+ else if (result && result != error_mark_node)
+ {
+ tree call = extract_call_expr (result);
+ CALL_EXPR_OPERATOR_SYNTAX (call) = true;
+
+ /* Specify evaluation order as per P0145R2. */
+ CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
+ }
+ }
+ else
+ gcc_unreachable ();
+ }
+
+ /* Free all the conversions we allocated. */
+ obstack_free (&conversion_obstack, p);
+
+ return result;
+}
+
/* CALL was returned by some call-building function; extract the actual
CALL_EXPR from any bits that have been tacked on, e.g. by
convert_from_reference. */
if (cand->flags & LOOKUP_LIST_INIT_CTOR)
{
tree c = extract_call_expr (call);
- /* build_new_op_1 will clear this when appropriate. */
+ /* build_new_op will clear this when appropriate. */
CALL_EXPR_ORDERED_ARGS (c) = true;
}
if (warned_p)
(bootstrap/91828). */
tree& operator[] (ptrdiff_t i) const { return (*v)[i]; }
- ~releasing_vec() { release_tree_vector (v); }
+ void release () { release_tree_vector (v); v = NULL; }
+
+ ~releasing_vec () { release_tree_vector (v); }
private:
vec_t *v;
};
}
extern tree build_op_call (tree, vec<tree, va_gc> **,
tsubst_flags_t);
+extern tree build_op_subscript (const op_location_t &, tree,
+ vec<tree, va_gc> **, tree *,
+ tsubst_flags_t);
extern bool aligned_allocation_fn_p (tree);
extern tree destroying_delete_p (tree);
extern bool usual_deallocation_fn_p (tree);
extern bool vague_linkage_p (tree);
extern void grokclassfn (tree, tree,
enum overload_flags);
-extern tree grok_array_decl (location_t, tree, tree, bool);
+extern tree grok_array_decl (location_t, tree, tree,
+ vec<tree, va_gc> **, tsubst_flags_t);
extern tree delete_sanity (location_t, tree, tree, bool,
int, tsubst_flags_t);
extern tree check_classfn (tree, tree, tree);
...);
extern tree build_min_non_dep (enum tree_code, tree, ...);
extern tree build_min_non_dep_op_overload (enum tree_code, tree, tree, ...);
+extern tree build_min_non_dep_op_overload (tree, tree, tree,
+ vec<tree, va_gc> *);
extern tree build_min_nt_call_vec (tree, vec<tree, va_gc> *);
extern tree build_min_non_dep_call_vec (tree, tree, vec<tree, va_gc> *);
extern vec<tree, va_gc>* vec_copy_and_insert (vec<tree, va_gc>*, tree, unsigned);
case OVL_OP_FLAG_BINARY:
if (arity != 2)
{
+ if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
+ break;
error_at (loc,
methodp
? G_("%qD must have exactly one argument")
}
/* Create an ARRAY_REF, checking for the user doing things backwards
- along the way. DECLTYPE_P is for N3276, as in the parser. */
+ along the way.
+ If INDEX_EXP is non-NULL, then that is the index expression,
+ otherwise INDEX_EXP_LIST is the list of index expressions. */
tree
grok_array_decl (location_t loc, tree array_expr, tree index_exp,
- bool decltype_p)
+ vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain)
{
tree type;
tree expr;
tree orig_array_expr = array_expr;
tree orig_index_exp = index_exp;
+ vec<tree, va_gc> *orig_index_exp_list
+ = index_exp_list ? *index_exp_list : NULL;
tree overload = NULL_TREE;
if (error_operand_p (array_expr) || error_operand_p (index_exp))
if (processing_template_decl)
{
if (type_dependent_expression_p (array_expr)
- || type_dependent_expression_p (index_exp))
- return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
- NULL_TREE, NULL_TREE);
+ || (index_exp ? type_dependent_expression_p (index_exp)
+ : any_type_dependent_arguments_p (*index_exp_list)))
+ {
+ if (index_exp == NULL)
+ index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
+ *index_exp_list);
+ return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
+ NULL_TREE, NULL_TREE);
+ }
array_expr = build_non_dependent_expr (array_expr);
- index_exp = build_non_dependent_expr (index_exp);
+ if (index_exp)
+ index_exp = build_non_dependent_expr (index_exp);
+ else
+ {
+ orig_index_exp_list = make_tree_vector_copy (*index_exp_list);
+ make_args_non_dependent (*index_exp_list);
+ }
}
type = TREE_TYPE (array_expr);
type = non_reference (type);
/* If they have an `operator[]', use that. */
- if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
- {
- tsubst_flags_t complain = tf_warning_or_error;
- if (decltype_p)
- complain |= tf_decltype;
- expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
- index_exp, NULL_TREE, &overload, complain);
+ if (MAYBE_CLASS_TYPE_P (type)
+ || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
+ || (index_exp == NULL_TREE
+ && !(*index_exp_list)->is_empty ()
+ && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ()))))
+ {
+ if (index_exp)
+ expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
+ index_exp, NULL_TREE, &overload, complain);
+ else if ((*index_exp_list)->is_empty ())
+ expr = build_op_subscript (loc, array_expr, index_exp_list, &overload,
+ complain);
+ else
+ {
+ expr = build_op_subscript (loc, array_expr, index_exp_list,
+ &overload, complain & tf_decltype);
+ if (expr == error_mark_node)
+ {
+ tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
+ tf_none);
+ if (idx != error_mark_node)
+ expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
+ idx, NULL_TREE, &overload,
+ complain & tf_decltype);
+ if (expr == error_mark_node)
+ {
+ overload = NULL_TREE;
+ expr = build_op_subscript (loc, array_expr, index_exp_list,
+ &overload, complain);
+ }
+ else
+ /* If it would be valid albeit deprecated expression in C++20,
+ just pedwarn on it and treat it as if wrapped in (). */
+ pedwarn (loc, OPT_Wcomma_subscript,
+ "top-level comma expression in array subscript "
+ "changed meaning in C++23");
+ }
+ }
}
else
{
else
p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
+ if (index_exp == NULL_TREE)
+ {
+ if ((*index_exp_list)->is_empty ())
+ {
+ error_at (loc, "built-in subscript operator without expression "
+ "list");
+ return error_mark_node;
+ }
+ tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL,
+ tf_none);
+ if (idx != error_mark_node)
+ /* If it would be valid albeit deprecated expression in C++20,
+ just pedwarn on it and treat it as if wrapped in (). */
+ pedwarn (loc, OPT_Wcomma_subscript,
+ "top-level comma expression in array subscript "
+ "changed meaning in C++23");
+ else
+ {
+ error_at (loc, "built-in subscript operator with more than one "
+ "expression in expression list");
+ return error_mark_node;
+ }
+ index_exp = idx;
+ }
+
if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
p2 = index_exp;
else
if (processing_template_decl && expr != error_mark_node)
{
if (overload != NULL_TREE)
- return (build_min_non_dep_op_overload
- (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp));
+ {
+ if (orig_index_exp == NULL_TREE)
+ {
+ expr = build_min_non_dep_op_overload (expr, overload,
+ orig_array_expr,
+ orig_index_exp_list);
+ release_tree_vector (orig_index_exp_list);
+ return expr;
+ }
+ return build_min_non_dep_op_overload (ARRAY_REF, expr, overload,
+ orig_array_expr,
+ orig_index_exp);
+ }
+
+ if (orig_index_exp == NULL_TREE)
+ {
+ orig_index_exp
+ = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF),
+ orig_index_exp_list);
+ release_tree_vector (orig_index_exp_list);
+ }
- return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
- NULL_TREE, NULL_TREE);
+ return build_min_non_dep (ARRAY_REF, expr, orig_array_expr,
+ orig_index_exp, NULL_TREE, NULL_TREE);
}
return expr;
}
return error_mark_node;
}
+/* Helper function for cp_parser_parenthesized_expression_list and
+ cp_parser_postfix_open_square_expression. Parse a single element
+ of parenthesized expression list. */
+
+static cp_expr
+cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
+ bool allow_expansion_p,
+ bool fold_expr_p,
+ bool *non_constant_p)
+{
+ cp_expr expr (NULL_TREE);
+ bool expr_non_constant_p;
+
+ /* Parse the next assignment-expression. */
+ if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ {
+ /* A braced-init-list. */
+ cp_lexer_set_source_position (parser->lexer);
+ maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
+ expr = cp_parser_braced_list (parser, &expr_non_constant_p);
+ if (non_constant_p && expr_non_constant_p)
+ *non_constant_p = true;
+ }
+ else if (non_constant_p)
+ {
+ expr = cp_parser_constant_expression (parser,
+ /*allow_non_constant_p=*/true,
+ &expr_non_constant_p);
+ if (expr_non_constant_p)
+ *non_constant_p = true;
+ }
+ else
+ expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
+
+ if (fold_expr_p)
+ expr = instantiate_non_dependent_expr (expr);
+
+ /* If we have an ellipsis, then this is an expression expansion. */
+ if (allow_expansion_p
+ && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
+ {
+ /* Consume the `...'. */
+ cp_lexer_consume_token (parser->lexer);
+
+ /* Build the argument pack. */
+ expr = make_pack_expansion (expr);
+ }
+ return expr;
+}
+
/* A subroutine of cp_parser_postfix_expression that also gets hijacked
by cp_parser_builtin_offsetof. We're looking for
postfix-expression [ expression ]
postfix-expression [ braced-init-list ] (C++11)
+ postfix-expression [ expression-list[opt] ] (C++23)
FOR_OFFSETOF is set if we're being called in that context, which
changes how we deal with integer constant expressions. */
bool decltype_p)
{
tree index = NULL_TREE;
+ releasing_vec expression_list = NULL;
location_t loc = cp_lexer_peek_token (parser->lexer)->location;
bool saved_greater_than_is_operator_p;
index = cp_parser_constant_expression (parser);
else
{
- if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ if (cxx_dialect >= cxx23
+ && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
+ *&expression_list = make_tree_vector ();
+ else if (cxx_dialect >= cxx23)
+ {
+ while (true)
+ {
+ cp_expr expr
+ = cp_parser_parenthesized_expression_list_elt (parser,
+ /*cast_p=*/
+ false,
+ /*allow_exp_p=*/
+ true,
+ /*fold_expr_p=*/
+ false,
+ /*non_cst_p=*/
+ NULL);
+
+ if (expr == error_mark_node)
+ index = error_mark_node;
+ else if (expression_list.get () == NULL
+ && !PACK_EXPANSION_P (expr.get_value ()))
+ index = expr.get_value ();
+ else
+ vec_safe_push (expression_list, expr.get_value ());
+
+ /* If the next token isn't a `,', then we are done. */
+ if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
+ break;
+
+ if (expression_list.get () == NULL && index != error_mark_node)
+ {
+ *&expression_list = make_tree_vector_single (index);
+ index = NULL_TREE;
+ }
+
+ /* Otherwise, consume the `,' and keep going. */
+ cp_lexer_consume_token (parser->lexer);
+ }
+ if (expression_list.get () && index == error_mark_node)
+ expression_list.release ();
+ }
+ else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
{
bool expr_nonconst_p;
cp_lexer_set_source_position (parser->lexer);
/* Build the ARRAY_REF. */
postfix_expression = grok_array_decl (loc, postfix_expression,
- index, decltype_p);
+ index, &expression_list,
+ tf_warning_or_error
+ | (decltype_p ? tf_decltype : 0));
/* When not doing offsetof, array references are not permitted in
constant-expressions. */
}
else
{
- bool expr_non_constant_p;
-
- /* Parse the next assignment-expression. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
- {
- /* A braced-init-list. */
- cp_lexer_set_source_position (parser->lexer);
- maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
- expr = cp_parser_braced_list (parser, &expr_non_constant_p);
- if (non_constant_p && expr_non_constant_p)
- *non_constant_p = true;
- }
- else if (non_constant_p)
- {
- expr = (cp_parser_constant_expression
- (parser, /*allow_non_constant_p=*/true,
- &expr_non_constant_p));
- if (expr_non_constant_p)
- *non_constant_p = true;
- }
- else
- expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
- cast_p);
-
- if (fold_expr_p)
- expr = instantiate_non_dependent_expr (expr);
-
- /* If we have an ellipsis, then this is an expression
- expansion. */
- if (allow_expansion_p
- && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
- {
- /* Consume the `...'. */
- cp_lexer_consume_token (parser->lexer);
-
- /* Build the argument pack. */
- expr = make_pack_expansion (expr);
- }
+ expr
+ = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
+ allow_expansion_p,
+ fold_expr_p,
+ non_constant_p);
if (wrap_locations_p)
expr.maybe_add_location_wrapper ();
case CPP_DEREF:
/* offsetof-member-designator "->" identifier */
- expr = grok_array_decl (token->location, expr,
- integer_zero_node, false);
+ expr = grok_array_decl (token->location, expr, integer_zero_node,
+ NULL, tf_warning_or_error);
/* FALLTHRU */
case CPP_DOT:
return fold_targs_r (targs, complain);
}
+/* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
+ handling. */
+
+static void
+tsubst_copy_and_build_call_args (tree t, tree args, tsubst_flags_t complain,
+ tree in_decl,
+ bool integral_constant_expression_p,
+ releasing_vec &call_args)
+{
+ unsigned int nargs = call_expr_nargs (t);
+ for (unsigned int i = 0; i < nargs; ++i)
+ {
+ tree arg = CALL_EXPR_ARG (t, i);
+
+ if (!PACK_EXPANSION_P (arg))
+ vec_safe_push (call_args,
+ tsubst_copy_and_build (arg, args, complain, in_decl,
+ /*function_p=*/false,
+ integral_constant_expression_p));
+ else
+ {
+ /* Expand the pack expansion and push each entry onto CALL_ARGS. */
+ arg = tsubst_pack_expansion (arg, args, complain, in_decl);
+ if (TREE_CODE (arg) == TREE_VEC)
+ {
+ unsigned int len, j;
+
+ len = TREE_VEC_LENGTH (arg);
+ for (j = 0; j < len; ++j)
+ {
+ tree value = TREE_VEC_ELT (arg, j);
+ if (value != NULL_TREE)
+ value = convert_from_reference (value);
+ vec_safe_push (call_args, value);
+ }
+ }
+ else
+ /* A partial substitution. Add one entry. */
+ vec_safe_push (call_args, arg);
+ }
+ }
+}
+
/* Like tsubst but deals with expressions and performs semantic
analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
"F<TARGS> (ARGS)". */
case ARRAY_REF:
op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
args, complain, in_decl);
+ if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
+ && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
+ == ovl_op_identifier (ARRAY_REF)))
+ {
+ tree c = TREE_OPERAND (t, 1);
+ releasing_vec index_exp_list;
+ tsubst_copy_and_build_call_args (c, args, complain, in_decl,
+ integral_constant_expression_p,
+ index_exp_list);
+
+ tree r;
+ if (vec_safe_length (index_exp_list) == 1
+ && !PACK_EXPANSION_P (index_exp_list[0]))
+ r = grok_array_decl (EXPR_LOCATION (t), op1,
+ index_exp_list[0], NULL,
+ complain | decltype_flag);
+ else
+ r = grok_array_decl (EXPR_LOCATION (t), op1,
+ NULL_TREE, &index_exp_list,
+ complain | decltype_flag);
+ RETURN (r);
+ }
RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
RECUR (TREE_OPERAND (t, 1)),
complain|decltype_flag));
case CALL_EXPR:
{
tree function;
- unsigned int nargs, i;
+ unsigned int nargs;
bool qualified_p;
bool koenig_p;
tree ret;
nargs = call_expr_nargs (t);
releasing_vec call_args;
- for (i = 0; i < nargs; ++i)
- {
- tree arg = CALL_EXPR_ARG (t, i);
-
- if (!PACK_EXPANSION_P (arg))
- vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
- else
- {
- /* Expand the pack expansion and push each entry onto
- CALL_ARGS. */
- arg = tsubst_pack_expansion (arg, args, complain, in_decl);
- if (TREE_CODE (arg) == TREE_VEC)
- {
- unsigned int len, j;
-
- len = TREE_VEC_LENGTH (arg);
- for (j = 0; j < len; ++j)
- {
- tree value = TREE_VEC_ELT (arg, j);
- if (value != NULL_TREE)
- value = convert_from_reference (value);
- vec_safe_push (call_args, value);
- }
- }
- else
- {
- /* A partial substitution. Add one entry. */
- vec_safe_push (call_args, arg);
- }
- }
- }
+ tsubst_copy_and_build_call_args (t, args, complain, in_decl,
+ integral_constant_expression_p,
+ call_args);
/* Stripped-down processing for a call in a thunk. Specifically, in
the thunk template for a generic lambda. */
OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
- ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
+ ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
+ tf_warning_or_error);
return ret;
}
}
}
else
- gcc_unreachable ();
+ gcc_unreachable ();
va_end (p);
call = build_min_non_dep_call_vec (non_dep, fn, args);
return call;
}
+/* Similar to above build_min_non_dep_op_overload, but arguments
+ are taken from ARGS vector. */
+
+tree
+build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
+ vec<tree, va_gc> *args)
+{
+ 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);
+ nargs--;
+ gcc_assert (vec_safe_length (args) == nargs);
+
+ tree call = build_min_non_dep_call_vec (non_dep, fn, args);
+
+ tree call_expr = extract_call_expr (call);
+ KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
+ CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
+ CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
+ CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
+
+ return call;
+}
+
/* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
vec<tree, va_gc> *
@opindex Wcomma-subscript
@opindex Wno-comma-subscript
Warn about uses of a comma expression within a subscripting expression.
-This usage was deprecated in C++20. However, a comma expression wrapped
-in @code{( )} is not deprecated. Example:
+This usage was deprecated in C++20 and is going to be removed in C++23.
+However, a comma expression wrapped in @code{( )} is not deprecated. Example:
@smallexample
@group
void f(int *a, int b, int c) @{
- a[b,c]; // deprecated
+ a[b,c]; // deprecated in C++20, invalid in C++23
a[(b,c)]; // OK
@}
@end group
@end smallexample
-Enabled by default with @option{-std=c++20}.
+In C++23 it is valid to have comma separated expressions in a subscript
+when an overloaded subscript operator is found and supports the right
+number and types of arguments. G++ will accept the formerly valid syntax
+for code that is not valid in C++23 but used to be valid but deprecated
+in C++20 with a pedantic warning that can be disabled with
+@option{-Wno-comma-subscript}.
+
+Enabled by default with @option{-std=c++20} unless @option{-Wno-deprecated},
+and with @option{-std=c++23} regardless of @option{-Wno-deprecated}.
@item -Wctad-maybe-unsupported @r{(C++ and Objective-C++ only)}
@opindex Wctad-maybe-unsupported
#elif __cpp_if_consteval != 202106
# error "__cpp_if_consteval != 202106"
#endif
+
+#ifndef __cpp_multidimensional_subscript
+# error "__cpp_multidimensional_subscript"
+#elif __cpp_multidimensional_subscript != 202110
+# error "__cpp_multidimensional_subscript != 202110"
+#endif
--- /dev/null
+// P2128R6
+// { dg-do run }
+// { dg-options "-std=c++23" }
+
+extern "C" void abort ();
+
+struct S
+{
+ constexpr S () : a {} {};
+ constexpr S (int x, int y, int z) : a {x, y, z} {};
+ constexpr int &operator[] () { return a[0]; }
+ constexpr int &operator[] (int x) { return a[x]; }
+ constexpr int &operator[] (int x, long y) { return a[x + y * 8]; }
+ int a[64];
+};
+
+struct T
+{
+ operator int () { return 42; };
+};
+
+int buf[64];
+
+struct U
+{
+ operator int * () { return buf; }
+};
+
+static_assert (S ()[1] == 0);
+static_assert (S (1, 2, 42)[2] == 42);
+static_assert (S ()[3, 4] == 0);
+static_assert (S (1, 43, 2)[1, 0] == 43);
+static_assert (S ()[] == 0);
+static_assert (S (44, 1, 2)[] == 44);
+
+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 ();
+ T t;
+ U u;
+ if (&u[t] != &buf[42])
+ abort ();
+ if (&t[u] != &buf[42])
+ abort ();
+}
--- /dev/null
+// P2128R6
+// { dg-do compile }
+// { dg-options "-std=c++23" }
+
+struct S
+{
+ S () : a {} {};
+ int &operator[] () { return a[0]; }
+ int &operator[] (int x) { return a[x]; }
+ int &operator[] (int x, long y) { return a[x + y * 8]; }
+ int a[64];
+};
+
+struct T
+{
+ operator int () { return 42; };
+};
+
+int buf[64];
+
+struct U
+{
+ operator int * () { return buf; }
+};
+
+struct V
+{
+ V () : a {} {};
+ V (int x, int y, int z) : a {x, y, z} {};
+ int &operator[] () { return a[0]; } // { dg-message "candidate" }
+ int &operator[] (int x, long y) { return a[x + y * 8]; } // { dg-message "candidate" }
+ int a[64];
+};
+
+void
+foo ()
+{
+ S s;
+ T t;
+ U u;
+ V v;
+ auto &a = buf[]; // { dg-error "built-in subscript operator without expression list" }
+ auto &b = buf[1, 2]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
+ auto &c = s[1, 2, 3]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
+ auto &d = v[1]; // { dg-error "no match for 'operator\\\[\\\]' in 'v\\\[1\\\]' \\\(operand types are 'V' and 'int'\\\)" }
+ auto &e = v[1, 2, 3]; // { dg-error "no match for call to 'V::operator\\\[\\\] \\\(int, int, int\\\)'" }
+ auto &f = t[42, u]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
+ auto &g = u[42, t]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
+ auto &h = buf[42, 2.5]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
+ // { dg-error "invalid types \[^\n\r]* for array subscript" "" { target *-*-* } .-1 }
+}
--- /dev/null
+// P2128R6
+// { dg-do run }
+// { dg-options "-std=c++23" }
+
+extern "C" void abort ();
+
+struct S
+{
+ constexpr S () : a {} {};
+ constexpr S (int x, int y, int z) : a {x, y, z} {};
+ constexpr int &operator[] () { return a[0]; }
+ constexpr int &operator[] (int x) { return a[x]; }
+ constexpr int &operator[] (int x, long y) { return a[x + y * 8]; }
+ int a[64];
+};
+
+struct T
+{
+ operator int () { return 42; };
+};
+
+int buf[64];
+
+struct U
+{
+ operator int * () { return buf; }
+};
+
+template <int N>
+void
+foo ()
+{
+ static_assert (S ()[1] == 0);
+ static_assert (S (1, 2, 42)[2] == 42);
+ static_assert (S ()[3, 4] == 0);
+ static_assert (S (1, 43, 2)[1, 0] == 43);
+ static_assert (S ()[] == 0);
+ static_assert (S (44, 1, 2)[] == 44);
+ 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 ();
+ T t;
+ U u;
+ if (&u[t] != &buf[42])
+ abort ();
+ if (&t[u] != &buf[42])
+ abort ();
+}
+
+template <typename V, typename W, typename X>
+void
+bar ()
+{
+ static_assert (V ()[1] == 0);
+ static_assert (V (1, 2, 42)[2] == 42);
+ static_assert (V ()[3, 4] == 0);
+ static_assert (V (1, 43, 2)[1, 0] == 43);
+ static_assert (V ()[] == 0);
+ static_assert (V (44, 1, 2)[] == 44);
+ V 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 ();
+ W t;
+ X u;
+ if (&u[t] != &buf[42])
+ abort ();
+ if (&t[u] != &buf[42])
+ abort ();
+}
+
+int
+main ()
+{
+ foo <0> ();
+ bar <S, T, U> ();
+}
--- /dev/null
+// P2128R6
+// { dg-do run }
+// { dg-options "-std=c++23" }
+
+extern "C" void abort ();
+
+struct S
+{
+ constexpr S () : a {} {};
+ constexpr S (int x, int y, int z) : a {x, y, z} {};
+ constexpr int &operator[] () { return a[0]; }
+ constexpr int &operator[] (int x) { return a[x]; }
+ constexpr int &operator[] (int x, long y) { return a[x + y * 8]; }
+ int a[64];
+};
+int buf[26];
+
+template <class ...Ts>
+auto &
+foo (S &s, Ts... args)
+{
+ return s[args...];
+}
+
+template <typename T, class ...Ts>
+auto &
+bar (T &s, Ts... args)
+{
+ return s[args...];
+}
+
+int
+main ()
+{
+ S s;
+ if (&foo (s) != &s.a[0]
+ || &foo (s, 42) != &s.a[42]
+ || &foo (s, 5, 4) != &s.a[37]
+ || &bar (s) != &s.a[0]
+ || &bar (s, 22) != &s.a[22]
+ || &bar (s, 17, 3L) != &s.a[41]
+ || &bar (buf, 5) != &buf[5])
+ abort ();
+}
--- /dev/null
+// P2128R6
+// { dg-do run { target c++11 } }
+
+#include <initializer_list>
+#include <cstdlib>
+
+struct S
+{
+ S () : a {} {};
+ int &operator[] (std::initializer_list<int> l) {
+ int sum = 0;
+ for (auto x : l)
+ sum += x;
+ return a[sum];
+ }
+ int a[64];
+};
+
+int
+main ()
+{
+ S s;
+ if (&s[{0}] != &s.a[0]
+ || &s[{42}] != &s.a[42]
+ || &s[{5, 7, 9}] != &s.a[5 + 7 + 9]
+ || &s[{1, 2, 3, 4}] != &s.a[1 + 2 + 3 + 4])
+ abort ();
+}
--- /dev/null
+// P2128R6
+// { dg-do run }
+// { dg-options "-std=c++23" }
+
+#include <initializer_list>
+#include <cstdlib>
+
+struct S
+{
+ S () : a {} {};
+ int &operator[] (std::initializer_list<int> l, std::initializer_list<int> m) {
+ int sum = 0;
+ for (auto x : l)
+ sum += x;
+ for (auto x : m)
+ sum += x;
+ return a[sum];
+ }
+ int a[64];
+};
+
+int
+main ()
+{
+ S s;
+ if (&s[{0}, {3, 1, 2}] != &s.a[0 + 3 + 1 + 2]
+ || &s[{42}, {11, 1}] != &s.a[42 + 11 + 1]
+ || &s[{5, 7, 9}, {3}] != &s.a[5 + 7 + 9 + 3]
+ || &s[{1, 2, 3, 4}, {3, 5, 8}] != &s.a[1 + 2 + 3 + 4 + 3 + 5 + 8])
+ abort ();
+}
void
fn (int *a, int b, int c)
{
- a[b,c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20 } }
+ a[b,c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(b,c)];
- a[(void) b, c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20 } }
+ a[(void) b, c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, c)];
- a[(void) b, (void) c, (void) b, b]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20 } }
+ a[(void) b, (void) c, (void) b, b]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, (void) c, (void) b, b)];
- a[S(), 10]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20 } }
+ a[S(), 10]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(S(), 10)];
a[int{(1,2)}];
- a[int{(1,2)}, int{}]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20 } }
+ a[int{(1,2)}, int{}]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(int{(1,2)}, int{})];
}
void
fn (int *a, int b, int c)
{
- a[b,c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[b,c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(b,c)];
- a[(void) b, c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[(void) b, c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, c)];
- a[(void) b, (void) c, (void) b, b]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[(void) b, (void) c, (void) b, b]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, (void) c, (void) b, b)];
- a[S(), 10]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[S(), 10]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(S(), 10)];
a[int{(1,2)}];
- a[int{(1,2)}, int{}]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[int{(1,2)}, int{}]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(int{(1,2)}, int{})];
}
fn (int *a, int b, int c)
{
a[b,c]; // { dg-bogus "top-level comma expression in array subscript is deprecated" }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(b,c)];
a[(void) b, c]; // { dg-bogus "top-level comma expression in array subscript is deprecated" }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, c)];
a[(void) b, (void) c, (void) b, b]; // { dg-bogus "top-level comma expression in array subscript is deprecated" }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[((void) b, (void) c, (void) b, b)];
a[S(), 10]; // { dg-bogus "top-level comma expression in array subscript is deprecated" }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(S(), 10)];
a[int{(1,2)}];
a[int{(1,2)}, int{}]; // { dg-bogus "top-level comma expression in array subscript is deprecated" }
+ // { dg-warning "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(int{(1,2)}, int{})];
}
fn (int *a, int b, int c)
{
a[foo<int, int>(1, 2)];
- a[foo<int, int>(1, 2), foo<int, int>(3, 4)]; // { dg-warning "24:top-level comma expression in array subscript is deprecated" }
+ a[foo<int, int>(1, 2), foo<int, int>(3, 4)]; // { dg-warning "24:top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
- a[b < c, b < c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
- a[b < c, b > c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
- a[b > c, b > c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
- a[b > c, b < c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[b < c, b < c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
+ a[b < c, b > c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
+ a[b > c, b > c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
+ a[b > c, b < c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(b < c, b < c)];
a[(b < c, b > c)];
- a[b << c, b << c]; // { dg-warning "top-level comma expression in array subscript is deprecated" }
+ a[b << c, b << c]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_down } }
+ // { dg-error "top-level comma expression in array subscript changed meaning in" "" { target c++23 } .-1 }
a[(b << c, b << c)];
}