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