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