From 7651c656858e4c80febb67e1742c0aa0d716e1a1 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Wed, 29 Jan 2014 15:44:50 -0500 Subject: [PATCH] =?UTF-8?q?re=20PR=20c++/59956=20(internal=20compiler=20er?= =?UTF-8?q?ror:=20unexpected=20expression=20=E2=80=98P=5FS=E2=80=99=20of?= =?UTF-8?q?=20kind=20template=5Fparm=5Findex)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR c++/59956 * friend.c (do_friend): Pass the TEMPLATE_DECL to add_friend if we have a friend template in a class template. * pt.c (tsubst_friend_function): Look through it. (push_template_decl_real): A friend member template is primary. From-SVN: r207281 --- gcc/cp/ChangeLog | 9 +++++++++ gcc/cp/friend.c | 8 +++++++- gcc/cp/pt.c | 14 +++++++++++--- gcc/testsuite/g++.dg/template/friend55.C | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/friend55.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3215a43d0ee..723ce954e06 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2014-01-29 Jason Merrill + + PR c++/59956 + * friend.c (do_friend): Pass the TEMPLATE_DECL to add_friend if we + have a friend template in a class template. + * pt.c (tsubst_friend_function): Look through it. + (push_template_decl_real): A friend member template is + primary. + 2014-01-29 Paolo Carlini PR c++/58846 diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c index 4fd6ffacbdf..150b392b62a 100644 --- a/gcc/cp/friend.c +++ b/gcc/cp/friend.c @@ -501,7 +501,13 @@ do_friend (tree ctype, tree declarator, tree decl, ? current_template_parms : NULL_TREE); - if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL) + if ((template_member_p + /* Always pull out the TEMPLATE_DECL if we have a friend + template in a class template so that it gets tsubsted + properly later on (59956). tsubst_friend_function knows + how to tell this apart from a member template. */ + || (class_template_depth && friend_depth)) + && decl && TREE_CODE (decl) == FUNCTION_DECL) decl = DECL_TI_TEMPLATE (decl); if (decl) diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index ce679f6d18e..943255d0da7 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -4615,7 +4615,8 @@ push_template_decl_real (tree decl, bool is_friend) DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); /* See if this is a primary template. */ - if (is_friend && ctx) + if (is_friend && ctx + && uses_template_parms_level (ctx, processing_template_decl)) /* A friend template that specifies a class context, i.e. template friend void A::f(); is not primary. */ @@ -8424,10 +8425,17 @@ tsubst_friend_function (tree decl, tree args) if (COMPLETE_TYPE_P (context)) { + tree fn = new_friend; + /* do_friend adds the TEMPLATE_DECL for any member friend + template even if it isn't a member template, i.e. + template friend A::f(); + Look through it in that case. */ + if (TREE_CODE (fn) == TEMPLATE_DECL + && !PRIMARY_TEMPLATE_P (fn)) + fn = DECL_TEMPLATE_RESULT (fn); /* Check to see that the declaration is really present, and, possibly obtain an improved declaration. */ - tree fn = check_classfn (context, - new_friend, NULL_TREE); + fn = check_classfn (context, fn, NULL_TREE); if (fn) new_friend = fn; diff --git a/gcc/testsuite/g++.dg/template/friend55.C b/gcc/testsuite/g++.dg/template/friend55.C new file mode 100644 index 00000000000..4abe6ce6a23 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/friend55.C @@ -0,0 +1,18 @@ +// PR c++/59956 + +template struct A; +template class B { + int i; + template friend void A::impl(); +}; + +B<0> b1; +templatestruct A { void impl(); }; +B<1> b2; + +template void A::impl() { ++b1.i; ++b2.i; } + +int main() +{ + A<0>().impl(); +}