From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31244 invoked by alias); 27 May 2011 03:57:47 -0000 Received: (qmail 31230 invoked by uid 22791); 27 May 2011 03:57:46 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,TW_CX,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 27 May 2011 03:57:30 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4R3vU5W030380 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 26 May 2011 23:57:30 -0400 Received: from [127.0.0.1] (ovpn-113-23.phx2.redhat.com [10.3.113.23]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4R3vTis012632 for ; Thu, 26 May 2011 23:57:30 -0400 Message-ID: <4DDF2129.8010608@redhat.com> Date: Fri, 27 May 2011 07:32:00 -0000 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/47721 (N1791, friend T) Content-Type: multipart/mixed; boundary="------------030607080607070907050302" Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2011-05/txt/msg02125.txt.bz2 This is a multi-part message in MIME format. --------------030607080607070907050302 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-length: 409 The C++0x draft has allowed "friend T;" to make a template type parameter a friend since 2005; we've been getting a few bug reports about not having implemented it yet, so here it is. I notice that friend template template parameters also seem to work now, with the syntax template friend class W; though it's not clear that's valid syntax. Tested x86_64-pc-linux-gnu, applying to trunk. --------------030607080607070907050302 Content-Type: text/x-patch; name="47721.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="47721.patch" Content-length: 4334 commit 89758cb5378bbf92d1f9f72ed5161fd3f89118a8 Author: Jason Merrill Date: Thu May 26 16:31:45 2011 -0400 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. diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c index b61611a..36fcca4 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 004ff05..135ab14 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 28c82b8..767c4f6 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/g++.dg/cpp0x/friend1.C b/gcc/testsuite/g++.dg/cpp0x/friend1.C new file mode 100644 index 0000000..2cf4c3c --- /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 0000000..39276a0 --- /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