]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/friend.c
2019-02-27 Bernd Edlinger <bernd.edlinger@hotmail.de>
[thirdparty/gcc.git] / gcc / cp / friend.c
CommitLineData
b465397d 1/* Help friends in C++.
fbd26352 2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
b465397d 3
6f0d25a6 4This file is part of GCC.
b465397d 5
6f0d25a6 6GCC is free software; you can redistribute it and/or modify
b465397d 7it under the terms of the GNU General Public License as published by
aa139c3f 8the Free Software Foundation; either version 3, or (at your option)
b465397d 9any later version.
10
6f0d25a6 11GCC is distributed in the hope that it will be useful,
b465397d 12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
aa139c3f 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b465397d 19
20#include "config.h"
b3ef7553 21#include "system.h"
805e22b2 22#include "coretypes.h"
b465397d 23#include "cp-tree.h"
b465397d 24
f3110581 25/* Friend data structures are described in cp-tree.h. */
b465397d 26
37af486a 27
28/* The GLOBAL_FRIEND scope (functions, classes, or templates) is
29 regarded as a friend of every class. This is only used by libcc1,
30 to enable GDB's code snippets to access private members without
31 disabling access control in general, which could cause different
32 template overload resolution results when accessibility matters
33 (e.g. tests for an accessible member). */
34
5407f1e9 35static GTY(()) tree global_friend;
37af486a 36
37/* Set the GLOBAL_FRIEND for this compilation session. It might be
38 set multiple times, but always to the same scope. */
39
40void
41set_global_friend (tree scope)
42{
43 gcc_checking_assert (scope != NULL_TREE);
44 gcc_assert (!global_friend || global_friend == scope);
45 global_friend = scope;
46}
47
48/* Return TRUE if SCOPE is the global friend. */
49
50bool
51is_global_friend (tree scope)
52{
53 gcc_checking_assert (scope != NULL_TREE);
54
55 if (global_friend == scope)
56 return true;
57
58 if (!global_friend)
59 return false;
60
61 if (is_specialization_of_friend (global_friend, scope))
62 return true;
63
64 return false;
65}
66
3160db1d 67/* Returns nonzero if SUPPLICANT is a friend of TYPE. */
60c1a862 68
b465397d 69int
4b8ee8b9 70is_friend (tree type, tree supplicant)
b465397d 71{
72 int declp;
cd16867a 73 tree list;
a4701a9e 74 tree context;
b465397d 75
76 if (supplicant == NULL_TREE || type == NULL_TREE)
77 return 0;
78
37af486a 79 if (is_global_friend (supplicant))
80 return 1;
81
9308e976 82 declp = DECL_P (supplicant);
b465397d 83
84 if (declp)
85 /* It's a function decl. */
86 {
87 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
88 tree name = DECL_NAME (supplicant);
b465397d 89
90 for (; list ; list = TREE_CHAIN (list))
91 {
a731c87f 92 if (name == FRIEND_NAME (list))
b465397d 93 {
a731c87f 94 tree friends = FRIEND_DECLS (list);
b465397d 95 for (; friends ; friends = TREE_CHAIN (friends))
96 {
607a5d68 97 tree this_friend = TREE_VALUE (friends);
f3110581 98
607a5d68 99 if (this_friend == NULL_TREE)
7bdfc61c 100 continue;
34197853 101
607a5d68 102 if (supplicant == this_friend)
34197853 103 return 1;
104
607a5d68 105 if (is_specialization_of_friend (supplicant, this_friend))
b465397d 106 return 1;
107 }
108 break;
109 }
110 }
111 }
112 else
113 /* It's a type. */
114 {
3d5f050a 115 if (same_type_p (supplicant, type))
116 return 1;
9031d10b 117
b465397d 118 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
119 for (; list ; list = TREE_CHAIN (list))
0f0d5a69 120 {
121 tree t = TREE_VALUE (list);
122
9031d10b 123 if (TREE_CODE (t) == TEMPLATE_DECL ?
f95fba26 124 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
daf9ff67 125 same_type_p (supplicant, t))
0f0d5a69 126 return 1;
127 }
9031d10b 128 }
b465397d 129
3d5f050a 130 if (declp)
131 {
132 if (DECL_FUNCTION_MEMBER_P (supplicant))
133 context = DECL_CONTEXT (supplicant);
134 else
135 context = NULL_TREE;
136 }
a4701a9e 137 else
3d5f050a 138 {
139 if (TYPE_CLASS_SCOPE_P (supplicant))
140 /* Nested classes get the same access as their enclosing types, as
141 per DR 45 (this is a change from the standard). */
142 context = TYPE_CONTEXT (supplicant);
143 else
144 /* Local classes have the same access as the enclosing function. */
145 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
146 }
a4701a9e 147
47cd6605 148 /* A namespace is not friend to anybody. */
8417823c 149 if (context && TREE_CODE (context) == NAMESPACE_DECL)
150 context = NULL_TREE;
151
a4701a9e 152 if (context)
153 return is_friend (type, context);
b465397d 154
155 return 0;
156}
157
158/* Add a new friend to the friends of the aggregate type TYPE.
b123b79d 159 DECL is the FUNCTION_DECL of the friend being added.
160
161 If COMPLAIN is true, warning about duplicate friend is issued.
162 We want to have this diagnostics during parsing but not
163 when a template is being instantiated. */
b465397d 164
e207930b 165void
b123b79d 166add_friend (tree type, tree decl, bool complain)
b465397d 167{
d2506df0 168 tree typedecl;
169 tree list;
170 tree name;
c344b0fe 171 tree ctx;
b465397d 172
d2506df0 173 if (decl == error_mark_node)
174 return;
175
176 typedecl = TYPE_MAIN_DECL (type);
177 list = DECL_FRIENDLIST (typedecl);
178 name = DECL_NAME (decl);
a731c87f 179 type = TREE_TYPE (typedecl);
180
b465397d 181 while (list)
182 {
a731c87f 183 if (name == FRIEND_NAME (list))
b465397d 184 {
a731c87f 185 tree friends = FRIEND_DECLS (list);
b465397d 186 for (; friends ; friends = TREE_CHAIN (friends))
187 {
188 if (decl == TREE_VALUE (friends))
189 {
b123b79d 190 if (complain)
d8dbaa4d 191 warning (OPT_Wredundant_decls,
192 "%qD is already a friend of class %qT",
8853c40e 193 decl, type);
b465397d 194 return;
195 }
196 }
7a40f9b7 197
f4da5882 198 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
b465397d 199 TREE_VALUE (list));
70b4d972 200 break;
b465397d 201 }
202 list = TREE_CHAIN (list);
203 }
a731c87f 204
c344b0fe 205 ctx = DECL_CONTEXT (decl);
206 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
eb833cbe 207 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
208 tf_warning_or_error);
f8ae99ca 209
7a40f9b7 210 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
211
70b4d972 212 if (!list)
213 DECL_FRIENDLIST (typedecl)
214 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
215 DECL_FRIENDLIST (typedecl));
e207930b 216 if (!uses_template_parms (type))
9031d10b 217 DECL_BEFRIENDING_CLASSES (decl)
e207930b 218 = tree_cons (NULL_TREE, type,
219 DECL_BEFRIENDING_CLASSES (decl));
b465397d 220}
221
b465397d 222/* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
223 been defined, we make all of its member functions friends of
224 TYPE. If not, we make it a pending friend, which can later be added
225 when its definition is seen. If a type is defined, then its TYPE_DECL's
226 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
227 classes that are not defined. If a type has not yet been defined,
228 then the DECL_WAITING_FRIENDS contains a list of types
229 waiting to make it their friend. Note that these two can both
b123b79d 230 be in use at the same time!
231
232 If COMPLAIN is true, warning about duplicate friend is issued.
233 We want to have this diagnostics during parsing but not
234 when a template is being instantiated. */
b465397d 235
236void
b123b79d 237make_friend_class (tree type, tree friend_type, bool complain)
b465397d 238{
239 tree classes;
f95fba26 240
241 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
242 the enclosing class. FRIEND_DEPTH counts the number of template
243 headers used for this friend declaration. TEMPLATE_MEMBER_P,
244 defined inside the `if' block for TYPENAME_TYPE case, is true if
245 a template header in FRIEND_DEPTH is intended for DECLARATOR.
246 For example, the code
247
248 template <class T> struct A {
249 template <class U> struct B {
250 template <class V> template <class W>
251 friend class C<V>::D;
252 };
253 };
254
255 will eventually give the following results
256
257 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
258 2. FRIEND_DEPTH equals 2 (for `V' and `W').
259 3. TEMPLATE_MEMBER_P is true (for `W').
260
261 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
262
263 int class_template_depth = template_class_depth (type);
264 int friend_depth = processing_template_decl - class_template_depth;
b465397d 265
abcd2504 266 if (! MAYBE_CLASS_TYPE_P (friend_type)
267 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
b465397d 268 {
145c81fd 269 /* N1791: If the type specifier in a friend declaration designates a
270 (possibly cv-qualified) class type, that class is declared as a
271 friend; otherwise, the friend declaration is ignored.
272
60777f69 273 So don't complain in C++11 mode. */
274 if (cxx_dialect < cxx11)
29438999 275 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
145c81fd 276 "invalid type %qT declared %<friend%>", friend_type);
b465397d 277 return;
278 }
34197853 279
834bb7a6 280 friend_type = cv_unqualified (friend_type);
281
93f2ac0b 282 if (check_for_bare_parameter_packs (friend_type))
283 return;
284
f95fba26 285 if (friend_depth)
cb96e388 286 {
ca932088 287 /* [temp.friend] Friend declarations shall not declare partial
288 specializations. */
cb96e388 289 if (CLASS_TYPE_P (friend_type)
290 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
291 && uses_template_parms (friend_type))
292 {
05949fae 293 error ("partial specialization %qT declared %<friend%>",
cb96e388 294 friend_type);
295 return;
296 }
ca932088 297
2b80a2a8 298 if (TYPE_TEMPLATE_INFO (friend_type)
299 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type)))
300 {
bc35ef65 301 auto_diagnostic_group d;
2b80a2a8 302 error ("%qT is not a template", friend_type);
303 inform (location_of (friend_type), "previous declaration here");
304 if (TYPE_CLASS_SCOPE_P (friend_type)
305 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type))
306 && currently_open_class (TYPE_CONTEXT (friend_type)))
307 inform (input_location, "perhaps you need explicit template "
308 "arguments in your nested-name-specifier");
309 return;
310 }
cb96e388 311 }
ca932088 312
313 /* It makes sense for a template class to be friends with itself,
314 that means the instantiations can be friendly. Other cases are
315 not so meaningful. */
316 if (!friend_depth && same_type_p (type, friend_type))
b465397d 317 {
b123b79d 318 if (complain)
561fec9d 319 warning (0, "class %qT is implicitly friends with itself",
b123b79d 320 type);
b465397d 321 return;
322 }
323
918d907f 324 /* [temp.friend]
325
326 A friend of a class or class template can be a function or
327 class template, a specialization of a function template or
328 class template, or an ordinary (nontemplate) function or
47cd6605 329 class. */
f95fba26 330 if (!friend_depth)
918d907f 331 ;/* ok */
332 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
b7b8249a 333 {
f95fba26 334 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
335 == TEMPLATE_ID_EXPR)
336 {
337 /* template <class U> friend class T::X<U>; */
338 /* [temp.friend]
339 Friend declarations shall not declare partial
340 specializations. */
341 error ("partial specialization %qT declared %<friend%>",
342 friend_type);
343 return;
344 }
345 else
346 {
347 /* We will figure this out later. */
348 bool template_member_p = false;
349
350 tree ctype = TYPE_CONTEXT (friend_type);
351 tree name = TYPE_IDENTIFIER (friend_type);
352 tree decl;
353
354 if (!uses_template_parms_level (ctype, class_template_depth
355 + friend_depth))
356 template_member_p = true;
357
358 if (class_template_depth)
359 {
360 /* We rely on tsubst_friend_class to check the
361 validity of the declaration later. */
362 if (template_member_p)
363 friend_type
364 = make_unbound_class_template (ctype,
365 name,
366 current_template_parms,
367 tf_error);
368 else
369 friend_type
e2ae55f2 370 = make_typename_type (ctype, name, class_type, tf_error);
f95fba26 371 }
372 else
373 {
2cbaacd9 374 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
f95fba26 375 if (!decl)
376 {
377 error ("%qT is not a member of %qT", name, ctype);
378 return;
379 }
380 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
381 {
bc35ef65 382 auto_diagnostic_group d;
f95fba26 383 error ("%qT is not a member class template of %qT",
384 name, ctype);
66ed189d 385 inform (DECL_SOURCE_LOCATION (decl),
386 "%qD declared here", decl);
f95fba26 387 return;
388 }
389 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
390 || !CLASS_TYPE_P (TREE_TYPE (decl))))
391 {
bc35ef65 392 auto_diagnostic_group d;
f95fba26 393 error ("%qT is not a nested class of %qT",
394 name, ctype);
66ed189d 395 inform (DECL_SOURCE_LOCATION (decl),
396 "%qD declared here", decl);
f95fba26 397 return;
398 }
399
400 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
401 }
402 }
918d907f 403 }
404 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
405 {
406 /* template <class T> friend class T; */
05949fae 407 error ("template parameter type %qT declared %<friend%>", friend_type);
918d907f 408 return;
409 }
abcd2504 410 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
411 friend_type = TYPE_NAME (friend_type);
918d907f 412 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
413 {
414 /* template <class T> friend class A; where A is not a template */
05949fae 415 error ("%q#T is not a template", friend_type);
b7b8249a 416 return;
417 }
f95fba26 418 else
419 /* template <class T> friend class A; where A is a template */
52c6de7b 420 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
421
f95fba26 422 if (friend_type == error_mark_node)
423 return;
424
e4f430b5 425 /* See if it is already a friend. */
426 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
427 classes;
428 classes = TREE_CHAIN (classes))
429 {
430 tree probe = TREE_VALUE (classes);
431
432 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
433 {
434 if (friend_type == probe)
435 {
b123b79d 436 if (complain)
d8dbaa4d 437 warning (OPT_Wredundant_decls,
438 "%qD is already a friend of %qT", probe, type);
e4f430b5 439 break;
440 }
441 }
442 else if (TREE_CODE (probe) != TEMPLATE_DECL)
443 {
444 if (same_type_p (probe, friend_type))
445 {
b123b79d 446 if (complain)
d8dbaa4d 447 warning (OPT_Wredundant_decls,
448 "%qT is already a friend of %qT", probe, type);
e4f430b5 449 break;
450 }
451 }
452 }
9031d10b 453
454 if (!classes)
b465397d 455 {
7a40f9b7 456 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
457
b465397d 458 CLASSTYPE_FRIEND_CLASSES (type)
459 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
f95fba26 460 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
a731c87f 461 friend_type = TREE_TYPE (friend_type);
e207930b 462 if (!uses_template_parms (type))
463 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
9031d10b 464 = tree_cons (NULL_TREE, type,
465 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
b465397d 466 }
467}
468
2f7a9ec1 469/* Record DECL (a FUNCTION_DECL) as a friend of the
470 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
471 class of which it is a member, as named in the friend declaration.
472 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
473 friend declaration is a definition of the function. FLAGS is as
474 for grokclass fn. */
b465397d 475
476tree
7ef14399 477do_friend (tree ctype, tree declarator, tree decl,
9031d10b 478 tree attrlist, enum overload_flags flags,
7be1bc1f 479 bool funcdef_flag)
b465397d 480{
2f7a9ec1 481 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
95397ff9 482 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
2f7a9ec1 483
b465397d 484 /* Every decl that gets here is a friend of something. */
485 DECL_FRIEND_P (decl) = 1;
486
86c7212c 487 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
488 error ("friend declaration %qD may not have virt-specifiers",
489 decl);
490
230c4e2f 491 /* Unfortunately, we have to handle attributes here. Normally we would
492 handle them in start_decl_1, but since this is a friend decl start_decl_1
493 never gets to see it. */
494
495 /* Set attributes here so if duplicate decl, will have proper attributes. */
496 cplus_decl_attributes (&decl, attrlist, 0);
497
00d2b1b9 498 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
499 {
500 declarator = TREE_OPERAND (declarator, 0);
f990cba0 501 if (!identifier_p (declarator))
502 declarator = OVL_NAME (declarator);
00d2b1b9 503 }
504
b465397d 505 if (ctype)
506 {
7bdfc61c 507 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
508 the enclosing class. FRIEND_DEPTH counts the number of template
509 headers used for this friend declaration. TEMPLATE_MEMBER_P is
510 true if a template header in FRIEND_DEPTH is intended for
511 DECLARATOR. For example, the code
512
513 template <class T> struct A {
514 template <class U> struct B {
515 template <class V> template <class W>
516 friend void C<V>::f(W);
517 };
518 };
519
520 will eventually give the following results
521
522 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
523 2. FRIEND_DEPTH equals 2 (for `V' and `W').
524 3. TEMPLATE_MEMBER_P is true (for `W'). */
525
526 int class_template_depth = template_class_depth (current_class_type);
527 int friend_depth = processing_template_decl - class_template_depth;
528 /* We will figure this out later. */
529 bool template_member_p = false;
530
b465397d 531 tree cname = TYPE_NAME (ctype);
532 if (TREE_CODE (cname) == TYPE_DECL)
533 cname = DECL_NAME (cname);
534
535 /* A method friend. */
7bdfc61c 536 if (flags == NO_SPECIAL && declarator == cname)
c0a09bf1 537 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
607f6131 538
7873fcd6 539 grokclassfn (ctype, decl, flags);
607f6131 540
7bdfc61c 541 if (friend_depth)
542 {
543 if (!uses_template_parms_level (ctype, class_template_depth
544 + friend_depth))
545 template_member_p = true;
546 }
547
607f6131 548 /* A nested class may declare a member of an enclosing class
549 to be a friend, so we do lookup here even if CTYPE is in
550 the process of being defined. */
7bdfc61c 551 if (class_template_depth
2e70f5d0 552 || COMPLETE_OR_OPEN_TYPE_P (ctype))
b465397d 553 {
7bdfc61c 554 if (DECL_TEMPLATE_INFO (decl))
555 /* DECL is a template specialization. No need to
556 build a new TEMPLATE_DECL. */
557 ;
558 else if (class_template_depth)
559 /* We rely on tsubst_friend_function to check the
560 validity of the declaration later. */
c1d4295f 561 decl = push_template_decl_real (decl, /*is_friend=*/true);
7bdfc61c 562 else
9031d10b 563 decl = check_classfn (ctype, decl,
564 template_member_p
0122f52b 565 ? current_template_parms
566 : NULL_TREE);
7bdfc61c 567
35f711cd 568 if ((template_member_p
569 /* Always pull out the TEMPLATE_DECL if we have a friend
570 template in a class template so that it gets tsubsted
571 properly later on (59956). tsubst_friend_function knows
572 how to tell this apart from a member template. */
573 || (class_template_depth && friend_depth))
574 && decl && TREE_CODE (decl) == FUNCTION_DECL)
7bdfc61c 575 decl = DECL_TI_TEMPLATE (decl);
fb868a5d 576
607f6131 577 if (decl)
b123b79d 578 add_friend (current_class_type, decl, /*complain=*/true);
b465397d 579 }
580 else
05949fae 581 error ("member %qD declared as friend before type %qT defined",
607f6131 582 decl, ctype);
b465397d 583 }
b465397d 584 /* A global friend.
585 @@ or possibly a friend from a base class ?!? */
586 else if (TREE_CODE (decl) == FUNCTION_DECL)
587 {
7bdfc61c 588 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
589
b465397d 590 /* Friends must all go through the overload machinery,
591 even though they may not technically be overloaded.
592
593 Note that because classes all wind up being top-level
594 in their scope, their friend wind up in top-level scope as well. */
b465397d 595 if (funcdef_flag)
9ba4048d 596 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
b465397d 597
b1cfe2be 598 if (! DECL_USE_TEMPLATE (decl))
a2a5a510 599 {
8f966d79 600 /* We must check whether the decl refers to template
601 arguments before push_template_decl_real adds a
602 reference to the containing template class. */
603 int warn = (warn_nontemplate_friend
604 && ! funcdef_flag && ! is_friend_template
605 && current_template_parms
606 && uses_template_parms (decl));
607
a81a4baa 608 if (is_friend_template
609 || template_class_depth (current_class_type) != 0)
610 /* We can't call pushdecl for a template class, since in
611 general, such a declaration depends on template
612 parameters. Instead, we call pushdecl when the class
613 is instantiated. */
c1d4295f 614 decl = push_template_decl_real (decl, /*is_friend=*/true);
a81a4baa 615 else if (current_function_decl)
60fadde6 616 /* pushdecl will check there's a local decl already. */
617 decl = pushdecl (decl, /*is_friend=*/true);
a81a4baa 618 else
619 {
620 /* We can't use pushdecl, as we might be in a template
653e5405 621 class specialization, and pushdecl will insert an
622 unqualified friend decl into the template parameter
623 scope, rather than the namespace containing it. */
a81a4baa 624 tree ns = decl_namespace_context (decl);
9031d10b 625
a81a4baa 626 push_nested_namespace (ns);
c1d4295f 627 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
a81a4baa 628 pop_nested_namespace (ns);
629 }
a2a5a510 630
8f966d79 631 if (warn)
a2a5a510 632 {
633 static int explained;
a52d5726 634 bool warned;
635
bc35ef65 636 auto_diagnostic_group d;
a52d5726 637 warned = warning (OPT_Wnon_template_friend, "friend declaration "
638 "%q#D declares a non-template function", decl);
639 if (! explained && warned)
a2a5a510 640 {
5bcc316e 641 inform (input_location, "(if this is not what you intended, make sure "
b02f0a26 642 "the function template has already been declared "
643 "and add <> after the function name here) ");
a2a5a510 644 explained = 1;
645 }
646 }
647 }
b465397d 648
947f430b 649 if (decl == error_mark_node)
650 return error_mark_node;
9031d10b 651
652 add_friend (current_class_type,
b123b79d 653 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
654 /*complain=*/true);
b465397d 655 DECL_FRIEND_P (decl) = 1;
656 }
607f6131 657
b465397d 658 return decl;
659}
5407f1e9 660
661#include "gt-cp-friend.h"