static tree cp_parser_parameter_declaration_clause
(cp_parser *, cp_parser_flags);
static tree cp_parser_parameter_declaration_list
- (cp_parser *, cp_parser_flags);
+ (cp_parser *, cp_parser_flags, auto_vec<tree> *);
static cp_parameter_declarator *cp_parser_parameter_declaration
(cp_parser *, cp_parser_flags, bool, bool *);
static tree cp_parser_default_argument
return explicit_void_list_node;
}
+ /* A vector of parameters that haven't been pushed yet. */
+ auto_vec<tree> pending_decls;
+
/* Parse the parameter-declaration-list. */
- parameters = cp_parser_parameter_declaration_list (parser, flags);
+ parameters = cp_parser_parameter_declaration_list (parser, flags,
+ &pending_decls);
/* If a parse error occurred while parsing the
parameter-declaration-list, then the entire
parameter-declaration-clause is erroneous. */
else
ellipsis_p = false;
+ /* A valid parameter-declaration-clause can only be followed by a ')'.
+ So it's time to push all the parameters we have seen now that we
+ know we have a valid declaration. Note that here we may not have
+ committed yet, nor should we. Pushing here will detect the error
+ of redefining a parameter. */
+ if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
+ for (tree p : pending_decls)
+ pushdecl (p);
+
/* Finish the parameter list. */
if (!ellipsis_p)
parameters = chainon (parameters, void_list_node);
parameter-declaration-list , parameter-declaration
The parser flags FLAGS is used to control type-specifier parsing.
+ PENDING_DECLS is a vector of parameters that haven't been pushed yet.
Returns a representation of the parameter-declaration-list, as for
cp_parser_parameter_declaration_clause. However, the
`void_list_node' is never appended to the list. */
static tree
-cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
+cp_parser_parameter_declaration_list (cp_parser* parser,
+ cp_parser_flags flags,
+ auto_vec<tree> *pending_decls)
{
tree parameters = NULL_TREE;
tree *tail = ¶meters;
parameter->decl_specifiers.attributes,
0);
if (DECL_NAME (decl))
- decl = pushdecl (decl);
+ {
+ /* We cannot always pushdecl while parsing tentatively because
+ it may have side effects and we can't be sure yet if we're
+ parsing a declaration, e.g.:
+
+ S foo(int(x), int(x), int{x});
+
+ where it's not clear if we're dealing with a constructor call
+ or a function declaration until we've seen the last argument
+ which breaks it up.
+ It's safe to pushdecl so long as it doesn't result in a clash
+ with an already-pushed parameter. But we don't delay pushing
+ different parameters to handle
+
+ S foo(int(i), decltype(i) j = 42);
+
+ which is valid. */
+ if (pending_decls
+ && cp_parser_uncommitted_to_tentative_parse_p (parser)
+ /* See if PARAMETERS already contains a parameter with the same
+ DECL_NAME as DECL. */
+ && [parameters, decl] {
+ for (tree p = parameters; p; p = TREE_CHAIN (p))
+ if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
+ return true;
+ return false;
+ }())
+ pending_decls->safe_push (decl);
+ else
+ decl = pushdecl (decl);
+ }
if (decl != error_mark_node)
{
cp_lexer_consume_token (parser->lexer);
begin_scope (sk_function_parms, NULL_TREE);
tree t = cp_parser_parameter_declaration_list
- (parser, CP_PARSER_FLAGS_NONE);
+ (parser, CP_PARSER_FLAGS_NONE,
+ /*pending_decls*/nullptr);
if (t == error_mark_node)
error = true;
pop_bindings_and_leave_scope ();
--- /dev/null
+// PR c++/64679
+// { dg-do run { target c++11 } }
+
+struct Bar {
+ int a, b, c;
+ Bar(int a, int b, int c) : a(a), b(b), c(c) { }
+};
+
+void
+f ()
+{
+ Bar fn1(int(a), int(b), int c = sizeof(a));
+ Bar fn2(int(x), int(y), int(z)); // { dg-warning "function declaration" }
+ Bar fn3(int(x), int(y), int);
+ Bar fn4(int (*p)(int(x), int(y))); // { dg-warning "function declaration" }
+ Bar fn5(int (x), int (*p)(int(x), int(y)), int);
+}
+
+int
+main ()
+{
+ int x = 1;
+ // This ain't a decl.
+ Bar v1(int(x), int(x), int{x});
+ if (v1.a != 1 || v1.b != v1.a || v1.c != v1.a)
+ __builtin_abort ();
+ Bar v2(int(x), int(x), 1);
+ if (v2.a != 1 || v2.b != v2.a || v2.c != 1)
+ __builtin_abort ();
+ Bar v3(int(x), int(x), int(1));
+ if (v3.a != 1 || v3.b != v3.a || v3.c != 1)
+ __builtin_abort ();
+ Bar v4(int(1), int(x), int{x});
+ if (v4.a != 1 || v4.b != 1 || v4.c != 1)
+ __builtin_abort ();
+ Bar v5(int{x}, int(x), int{x});
+ if (v5.a != 1 || v5.b != v5.a || v5.c != v5.a)
+ __builtin_abort ();
+}