Commit 5d9607f0 by Ville Voutilainen Committed by Jason Merrill

re PR c++/62101 (deleted definitions of friend functions are rejected)

	PR c++/62101
	* decl.c (grokdeclarator): Move the check for friend initializers..
	* decl2.c (grokfield) ..here. Postpone early return for friends
	until after the initializer check.

From-SVN: r213974
parent a62dbaa4
2014-08-14 Ville Voutilainen <ville.voutilainen@gmail.com>
PR c++/62101
* decl.c (grokdeclarator): Move the check for friend initializers..
* decl2.c (grokfield) ..here. Postpone early return for friends
until after the initializer check.
2014-08-14 Paolo Carlini <paolo.carlini@oracle.com> 2014-08-14 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/54377 PR c++/54377
......
...@@ -9765,8 +9765,6 @@ grokdeclarator (const cp_declarator *declarator, ...@@ -9765,8 +9765,6 @@ grokdeclarator (const cp_declarator *declarator,
} }
else if (friendp) else if (friendp)
{ {
if (initialized)
error ("can%'t initialize friend function %qs", name);
if (virtualp) if (virtualp)
{ {
/* Cannot be both friend and virtual. */ /* Cannot be both friend and virtual. */
......
...@@ -870,11 +870,6 @@ grokfield (const cp_declarator *declarator, ...@@ -870,11 +870,6 @@ grokfield (const cp_declarator *declarator,
if (value == void_type_node) if (value == void_type_node)
return value; return value;
/* Pass friend decls back. */
if ((TREE_CODE (value) == FUNCTION_DECL
|| TREE_CODE (value) == TEMPLATE_DECL)
&& DECL_CONTEXT (value) != current_class_type)
return value;
name = DECL_NAME (value); name = DECL_NAME (value);
...@@ -926,7 +921,9 @@ grokfield (const cp_declarator *declarator, ...@@ -926,7 +921,9 @@ grokfield (const cp_declarator *declarator,
return value; return value;
} }
if (DECL_IN_AGGR_P (value)) int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
if (!friendp && DECL_IN_AGGR_P (value))
{ {
error ("%qD is already defined in %qT", value, DECL_CONTEXT (value)); error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
return void_type_node; return void_type_node;
...@@ -939,8 +936,6 @@ grokfield (const cp_declarator *declarator, ...@@ -939,8 +936,6 @@ grokfield (const cp_declarator *declarator,
{ {
if (TREE_CODE (value) == FUNCTION_DECL) if (TREE_CODE (value) == FUNCTION_DECL)
{ {
/* Initializers for functions are rejected early in the parser.
If we get here, it must be a pure specifier for a method. */
if (init == ridpointers[(int)RID_DELETE]) if (init == ridpointers[(int)RID_DELETE])
{ {
DECL_DELETED_FN (value) = 1; DECL_DELETED_FN (value) = 1;
...@@ -971,8 +966,12 @@ grokfield (const cp_declarator *declarator, ...@@ -971,8 +966,12 @@ grokfield (const cp_declarator *declarator,
else else
{ {
gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE); gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
error ("initializer specified for static member function %qD", if (friendp)
value); error ("initializer specified for friend function %qD",
value);
else
error ("initializer specified for static member function %qD",
value);
} }
} }
else if (TREE_CODE (value) == FIELD_DECL) else if (TREE_CODE (value) == FIELD_DECL)
...@@ -981,6 +980,12 @@ grokfield (const cp_declarator *declarator, ...@@ -981,6 +980,12 @@ grokfield (const cp_declarator *declarator,
gcc_unreachable (); gcc_unreachable ();
} }
/* Pass friend decls back. */
if ((TREE_CODE (value) == FUNCTION_DECL
|| TREE_CODE (value) == TEMPLATE_DECL)
&& DECL_CONTEXT (value) != current_class_type)
return value;
if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value)) if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
{ {
value = push_template_decl (value); value = push_template_decl (value);
......
// PR c++/62101
// { dg-do compile { target c++11 } }
struct X
{
friend void g(X, int) = 0; // { dg-error "initializer specified for friend function" }
friend void g(X, int) = default; // { dg-error "cannot be defaulted" }
// { dg-prune-output "note" }
friend void f(X, int) = delete;
friend void f(X, double) {}
};
struct Y;
void g(Y, int);
void g(Y, double);
struct Y
{
// { dg-prune-output "note" }
friend void g(Y, int) = delete;
friend void g(Y, double) {}
};
int main()
{
X x;
f(x, 5.0);
f(x, 5); // { dg-error "use of deleted function" }
Y y;
g(y, 5.0);
g(y, 5); // { dg-error "use of deleted function" }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment