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