* C++ PATCH for c++/47721 (N1791, friend T)
@ 2011-05-27 7:32 Jason Merrill
2011-05-27 7:35 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2011-05-27 7:32 UTC (permalink / raw)
To: gcc-patches List
[-- Attachment #1: Type: text/plain, Size: 409 bytes --]
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 <class T> friend class W;
though it's not clear that's valid syntax.
Tested x86_64-pc-linux-gnu, applying to trunk.
[-- Attachment #2: 47721.patch --]
[-- Type: text/x-patch, Size: 4334 bytes --]
commit 89758cb5378bbf92d1f9f72ed5161fd3f89118a8
Author: Jason Merrill <jason@redhat.com>
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%>", 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%>", 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 <typename T> struct A {
@@ -17769,10 +17770,12 @@ cp_parser_member_declaration (cp_parser* parser)
A<T>::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 <typename T> class R {
+ friend T;
+};
+
+R<C> rc; // class C is a friend of R<C>
+R<int> 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 W>
+class Q
+{
+ static const int I = 2;
+public:
+ friend W;
+};
+
+struct B
+{
+ int ar[Q<B>::I];
+};
+
+// bonus template template parameter friend:
+
+template <class T> struct A;
+
+template<template <class> class W>
+class P
+{
+ static const int I = 2;
+public:
+ // I'm not sure this is well-formed, but I can't find anything
+ // that says otherwise.
+ template <class T> friend class W;
+};
+
+template <class T>
+struct A
+{
+ int ar[P<A>::I];
+};
+
+A<int> a;
+
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: C++ PATCH for c++/47721 (N1791, friend T)
2011-05-27 7:32 C++ PATCH for c++/47721 (N1791, friend T) Jason Merrill
@ 2011-05-27 7:35 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2011-05-27 7:35 UTC (permalink / raw)
To: gcc-patches List
[-- Attachment #1: Type: text/plain, Size: 212 bytes --]
And as a prerequisite for that testcase, I needed to implement DR 1004,
dropping the pedwarn about using an injected class name as a template
template parameter.
Tested x86_64-pc-linux-gnu, applying to trunk.
[-- Attachment #2: dr1004.patch --]
[-- Type: text/x-patch, Size: 871 bytes --]
commit 0407c6ff58d0e7033de64040ce66d60a85b709e5
Author: Jason Merrill <jason@redhat.com>
Date: Thu May 26 15:00:08 2011 -0400
DR 1004
* pt.c (convert_template_argument): Don't complain about using
injected-class-name as template template argument.
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 3b26700..28c82b8 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -5962,7 +5962,9 @@ convert_template_argument (tree parm,
tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
if (TREE_CODE (t) == TEMPLATE_DECL)
{
- if (complain & tf_warning_or_error)
+ if (cxx_dialect >= cxx0x)
+ /* OK under DR 1004. */;
+ else if (complain & tf_warning_or_error)
pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
" used as template template argument", TYPE_NAME (arg));
else if (flag_pedantic_errors)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-05-27 3:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-27 7:32 C++ PATCH for c++/47721 (N1791, friend T) Jason Merrill
2011-05-27 7:35 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).