From 866e414678bba1426d1a777eb4a2ae591915b092 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 27 May 2011 00:04:40 -0400 Subject: [PATCH] re PR c++/47721 ([C++0x] Updated friendship rules?) PR c++/47721 * parser.c (cp_parser_member_declaration): Allow friend T. * friend.c (make_friend_class): Ignore non-classes. * pt.c (instantiate_class_template_1): Handle TEMPLATE_TYPE_PARM. From-SVN: r174319 --- gcc/cp/ChangeLog | 5 ++++ gcc/cp/friend.c | 9 ++++++- gcc/cp/parser.c | 17 +++++++----- gcc/cp/pt.c | 3 ++- gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/g++.dg/cpp0x/friend1.C | 22 +++++++++++++++ gcc/testsuite/g++.dg/cpp0x/friend2.C | 40 ++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/friend1.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/friend2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d5ad5010446..ed758c4e050 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,10 @@ 2011-05-26 Jason Merrill + PR c++/47721 + * parser.c (cp_parser_member_declaration): Allow friend T. + * friend.c (make_friend_class): Ignore non-classes. + * pt.c (instantiate_class_template_1): Handle TEMPLATE_TYPE_PARM. + DR 1004 * pt.c (convert_template_argument): Don't complain about using injected-class-name as template template argument. diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c index b61611ad1ad..36fcca4f737 100644 --- a/gcc/cp/friend.c +++ b/gcc/cp/friend.c @@ -226,7 +226,14 @@ make_friend_class (tree type, tree friend_type, bool complain) if (! MAYBE_CLASS_TYPE_P (friend_type)) { - error ("invalid type %qT declared %", friend_type); + /* N1791: If the type specifier in a friend declaration designates a + (possibly cv-qualified) class type, that class is declared as a + friend; otherwise, the friend declaration is ignored. + + So don't complain in C++0x mode. */ + if (cxx_dialect < cxx0x) + pedwarn (input_location, complain ? 0 : OPT_pedantic, + "invalid type %qT declared %", friend_type); return; } diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 004ff05768b..135ab145fb8 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -17758,9 +17758,10 @@ cp_parser_member_declaration (cp_parser* parser) { /* If the `friend' keyword was present, the friend must be introduced with a class-key. */ - if (!declares_class_or_enum) - error_at (decl_spec_token_start->location, - "a class-key must be used when declaring a friend"); + if (!declares_class_or_enum && cxx_dialect < cxx0x) + pedwarn (decl_spec_token_start->location, OPT_pedantic, + "in C++03 a class-key must be used " + "when declaring a friend"); /* In this case: template struct A { @@ -17769,10 +17770,12 @@ cp_parser_member_declaration (cp_parser* parser) A::B will be represented by a TYPENAME_TYPE, and therefore not recognized by check_tag_decl. */ - if (!type - && decl_specifiers.type - && TYPE_P (decl_specifiers.type)) - type = decl_specifiers.type; + if (!type) + { + type = decl_specifiers.type; + if (type && TREE_CODE (type) == TYPE_DECL) + type = TREE_TYPE (type); + } if (!type || !TYPE_P (type)) error_at (decl_spec_token_start->location, "friend declaration does not name a class or " diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 28c82b80f42..767c4f6c584 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -8472,7 +8472,8 @@ instantiate_class_template_1 (tree type) friend_type = TREE_TYPE (friend_type); adjust_processing_template_decl = true; } - else if (TREE_CODE (friend_type) == TYPENAME_TYPE) + else if (TREE_CODE (friend_type) == TYPENAME_TYPE + || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) { /* This could be either diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8703117b5fe..a6ce74b472a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2011-05-26 Jason Merrill + * g++.dg/cpp0x/friend1.C: New. + * g++.dg/cpp0x/friend2.C: New. + * g++.dg/cpp0x/auto7.C: Update. * g++.dg/template/crash50.C: Adjust. * g++.dg/template/static9.C: Adjust. diff --git a/gcc/testsuite/g++.dg/cpp0x/friend1.C b/gcc/testsuite/g++.dg/cpp0x/friend1.C new file mode 100644 index 00000000000..2cf4c3c715a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/friend1.C @@ -0,0 +1,22 @@ +// From N1791 +// { dg-options -std=c++0x } + +class C; +typedef C Ct; +class X1 { + friend C; // OK: class C is a friend +}; + +class X2 +{ + friend Ct; // OK: class C is a friend + friend D; // { dg-error "" } no type-name D in scope + friend class D; // OK: elaborated-type-specifier declares new class +}; + +template class R { + friend T; +}; + +R rc; // class C is a friend of R +R Ri; // OK: "friend int;" is ignored diff --git a/gcc/testsuite/g++.dg/cpp0x/friend2.C b/gcc/testsuite/g++.dg/cpp0x/friend2.C new file mode 100644 index 00000000000..39276a05ac3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/friend2.C @@ -0,0 +1,40 @@ +// PR c++/47721 +// { dg-options -std=c++0x } + +// template type parameter friend: + +template +class Q +{ + static const int I = 2; +public: + friend W; +}; + +struct B +{ + int ar[Q::I]; +}; + +// bonus template template parameter friend: + +template struct A; + +template