PR c++/29020
[official-gcc.git] / gcc / cp / friend.c
blobd92df5f6eba3df85ad1519eba905cbcd3a777c61
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "output.h"
32 #include "toplev.h"
34 /* Friend data structures are described in cp-tree.h. */
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
38 int
39 is_friend (tree type, tree supplicant)
41 int declp;
42 tree list;
43 tree context;
45 if (supplicant == NULL_TREE || type == NULL_TREE)
46 return 0;
48 declp = DECL_P (supplicant);
50 if (declp)
51 /* It's a function decl. */
53 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
54 tree name = DECL_NAME (supplicant);
56 for (; list ; list = TREE_CHAIN (list))
58 if (name == FRIEND_NAME (list))
60 tree friends = FRIEND_DECLS (list);
61 for (; friends ; friends = TREE_CHAIN (friends))
63 tree friend = TREE_VALUE (friends);
65 if (friend == NULL_TREE)
66 continue;
68 if (supplicant == friend)
69 return 1;
71 if (is_specialization_of_friend (supplicant, friend))
72 return 1;
74 break;
78 else
79 /* It's a type. */
81 if (same_type_p (supplicant, type))
82 return 1;
84 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
85 for (; list ; list = TREE_CHAIN (list))
87 tree t = TREE_VALUE (list);
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;
96 if (declp)
98 if (DECL_FUNCTION_MEMBER_P (supplicant))
99 context = DECL_CONTEXT (supplicant);
100 else
101 context = NULL_TREE;
103 else
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));
114 /* A namespace is not friend to anybody. */
115 if (context && TREE_CODE (context) == NAMESPACE_DECL)
116 context = NULL_TREE;
118 if (context)
119 return is_friend (type, context);
121 return 0;
124 /* Add a new friend to the friends of the aggregate type TYPE.
125 DECL is the FUNCTION_DECL of the friend being added.
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. */
131 void
132 add_friend (tree type, tree decl, bool complain)
134 tree typedecl;
135 tree list;
136 tree name;
137 tree ctx;
139 if (decl == error_mark_node)
140 return;
142 typedecl = TYPE_MAIN_DECL (type);
143 list = DECL_FRIENDLIST (typedecl);
144 name = DECL_NAME (decl);
145 type = TREE_TYPE (typedecl);
147 while (list)
149 if (name == FRIEND_NAME (list))
151 tree friends = FRIEND_DECLS (list);
152 for (; friends ; friends = TREE_CHAIN (friends))
154 if (decl == TREE_VALUE (friends))
156 if (complain)
157 warning (0, "%qD is already a friend of class %qT",
158 decl, type);
159 return;
163 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
165 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
166 TREE_VALUE (list));
167 return;
169 list = TREE_CHAIN (list);
172 ctx = DECL_CONTEXT (decl);
173 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
174 perform_or_defer_access_check (TYPE_BINFO (ctx), decl);
176 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
178 DECL_FRIENDLIST (typedecl)
179 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
180 DECL_FRIENDLIST (typedecl));
181 if (!uses_template_parms (type))
182 DECL_BEFRIENDING_CLASSES (decl)
183 = tree_cons (NULL_TREE, type,
184 DECL_BEFRIENDING_CLASSES (decl));
187 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
188 been defined, we make all of its member functions friends of
189 TYPE. If not, we make it a pending friend, which can later be added
190 when its definition is seen. If a type is defined, then its TYPE_DECL's
191 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
192 classes that are not defined. If a type has not yet been defined,
193 then the DECL_WAITING_FRIENDS contains a list of types
194 waiting to make it their friend. Note that these two can both
195 be in use at the same time!
197 If COMPLAIN is true, warning about duplicate friend is issued.
198 We want to have this diagnostics during parsing but not
199 when a template is being instantiated. */
201 void
202 make_friend_class (tree type, tree friend_type, bool complain)
204 tree classes;
206 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
207 the enclosing class. FRIEND_DEPTH counts the number of template
208 headers used for this friend declaration. TEMPLATE_MEMBER_P,
209 defined inside the `if' block for TYPENAME_TYPE case, is true if
210 a template header in FRIEND_DEPTH is intended for DECLARATOR.
211 For example, the code
213 template <class T> struct A {
214 template <class U> struct B {
215 template <class V> template <class W>
216 friend class C<V>::D;
220 will eventually give the following results
222 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
223 2. FRIEND_DEPTH equals 2 (for `V' and `W').
224 3. TEMPLATE_MEMBER_P is true (for `W').
226 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
228 int class_template_depth = template_class_depth (type);
229 int friend_depth = processing_template_decl - class_template_depth;
231 if (! IS_AGGR_TYPE (friend_type))
233 error ("invalid type %qT declared %<friend%>", friend_type);
234 return;
237 if (friend_depth)
238 /* If the TYPE is a template then it makes sense for it to be
239 friends with itself; this means that each instantiation is
240 friends with all other instantiations. */
242 if (CLASS_TYPE_P (friend_type)
243 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
244 && uses_template_parms (friend_type))
246 /* [temp.friend]
247 Friend declarations shall not declare partial
248 specializations. */
249 error ("partial specialization %qT declared %<friend%>",
250 friend_type);
251 return;
254 else if (same_type_p (type, friend_type))
256 if (complain)
257 pedwarn ("class %qT is implicitly friends with itself",
258 type);
259 return;
262 /* [temp.friend]
264 A friend of a class or class template can be a function or
265 class template, a specialization of a function template or
266 class template, or an ordinary (nontemplate) function or
267 class. */
268 if (!friend_depth)
269 ;/* ok */
270 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
272 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
273 == TEMPLATE_ID_EXPR)
275 /* template <class U> friend class T::X<U>; */
276 /* [temp.friend]
277 Friend declarations shall not declare partial
278 specializations. */
279 error ("partial specialization %qT declared %<friend%>",
280 friend_type);
281 return;
283 else
285 /* We will figure this out later. */
286 bool template_member_p = false;
288 tree ctype = TYPE_CONTEXT (friend_type);
289 tree name = TYPE_IDENTIFIER (friend_type);
290 tree decl;
292 if (!uses_template_parms_level (ctype, class_template_depth
293 + friend_depth))
294 template_member_p = true;
296 if (class_template_depth)
298 /* We rely on tsubst_friend_class to check the
299 validity of the declaration later. */
300 if (template_member_p)
301 friend_type
302 = make_unbound_class_template (ctype,
303 name,
304 current_template_parms,
305 tf_error);
306 else
307 friend_type
308 = make_typename_type (ctype, name, class_type, tf_error);
310 else
312 decl = lookup_member (ctype, name, 0, true);
313 if (!decl)
315 error ("%qT is not a member of %qT", name, ctype);
316 return;
318 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
320 error ("%qT is not a member class template of %qT",
321 name, ctype);
322 error ("%q+D declared here", decl);
323 return;
325 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
326 || !CLASS_TYPE_P (TREE_TYPE (decl))))
328 error ("%qT is not a nested class of %qT",
329 name, ctype);
330 error ("%q+D declared here", decl);
331 return;
334 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
338 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
340 /* template <class T> friend class T; */
341 error ("template parameter type %qT declared %<friend%>", friend_type);
342 return;
344 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
346 /* template <class T> friend class A; where A is not a template */
347 error ("%q#T is not a template", friend_type);
348 return;
350 else
351 /* template <class T> friend class A; where A is a template */
352 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
354 if (friend_type == error_mark_node)
355 return;
357 /* See if it is already a friend. */
358 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
359 classes;
360 classes = TREE_CHAIN (classes))
362 tree probe = TREE_VALUE (classes);
364 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
366 if (friend_type == probe)
368 if (complain)
369 warning (0, "%qD is already a friend of %qT", probe, type);
370 break;
373 else if (TREE_CODE (probe) != TEMPLATE_DECL)
375 if (same_type_p (probe, friend_type))
377 if (complain)
378 warning (0, "%qT is already a friend of %qT", probe, type);
379 break;
384 if (!classes)
386 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
388 CLASSTYPE_FRIEND_CLASSES (type)
389 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
390 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
391 friend_type = TREE_TYPE (friend_type);
392 if (!uses_template_parms (type))
393 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
394 = tree_cons (NULL_TREE, type,
395 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
399 /* Record DECL (a FUNCTION_DECL) as a friend of the
400 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
401 class of which it is a member, as named in the friend declaration.
402 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
403 friend declaration is a definition of the function. FLAGS is as
404 for grokclass fn. */
406 tree
407 do_friend (tree ctype, tree declarator, tree decl,
408 tree attrlist, enum overload_flags flags,
409 bool funcdef_flag)
411 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
412 gcc_assert (!ctype || IS_AGGR_TYPE (ctype));
414 /* Every decl that gets here is a friend of something. */
415 DECL_FRIEND_P (decl) = 1;
417 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
419 declarator = TREE_OPERAND (declarator, 0);
420 if (is_overloaded_fn (declarator))
421 declarator = DECL_NAME (get_first_fn (declarator));
424 if (ctype)
426 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
427 the enclosing class. FRIEND_DEPTH counts the number of template
428 headers used for this friend declaration. TEMPLATE_MEMBER_P is
429 true if a template header in FRIEND_DEPTH is intended for
430 DECLARATOR. For example, the code
432 template <class T> struct A {
433 template <class U> struct B {
434 template <class V> template <class W>
435 friend void C<V>::f(W);
439 will eventually give the following results
441 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
442 2. FRIEND_DEPTH equals 2 (for `V' and `W').
443 3. TEMPLATE_MEMBER_P is true (for `W'). */
445 int class_template_depth = template_class_depth (current_class_type);
446 int friend_depth = processing_template_decl - class_template_depth;
447 /* We will figure this out later. */
448 bool template_member_p = false;
450 tree cname = TYPE_NAME (ctype);
451 if (TREE_CODE (cname) == TYPE_DECL)
452 cname = DECL_NAME (cname);
454 /* A method friend. */
455 if (flags == NO_SPECIAL && declarator == cname)
456 DECL_CONSTRUCTOR_P (decl) = 1;
458 grokclassfn (ctype, decl, flags);
460 if (friend_depth)
462 if (!uses_template_parms_level (ctype, class_template_depth
463 + friend_depth))
464 template_member_p = true;
467 /* A nested class may declare a member of an enclosing class
468 to be a friend, so we do lookup here even if CTYPE is in
469 the process of being defined. */
470 if (class_template_depth
471 || COMPLETE_TYPE_P (ctype)
472 || TYPE_BEING_DEFINED (ctype))
474 if (DECL_TEMPLATE_INFO (decl))
475 /* DECL is a template specialization. No need to
476 build a new TEMPLATE_DECL. */
478 else if (class_template_depth)
479 /* We rely on tsubst_friend_function to check the
480 validity of the declaration later. */
481 decl = push_template_decl_real (decl, /*is_friend=*/true);
482 else
483 decl = check_classfn (ctype, decl,
484 template_member_p
485 ? current_template_parms
486 : NULL_TREE);
488 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
489 decl = DECL_TI_TEMPLATE (decl);
491 if (decl)
492 add_friend (current_class_type, decl, /*complain=*/true);
494 else
495 error ("member %qD declared as friend before type %qT defined",
496 decl, ctype);
498 /* A global friend.
499 @@ or possibly a friend from a base class ?!? */
500 else if (TREE_CODE (decl) == FUNCTION_DECL)
502 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
504 /* Friends must all go through the overload machinery,
505 even though they may not technically be overloaded.
507 Note that because classes all wind up being top-level
508 in their scope, their friend wind up in top-level scope as well. */
509 if (funcdef_flag)
510 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
512 if (! DECL_USE_TEMPLATE (decl))
514 /* We must check whether the decl refers to template
515 arguments before push_template_decl_real adds a
516 reference to the containing template class. */
517 int warn = (warn_nontemplate_friend
518 && ! funcdef_flag && ! is_friend_template
519 && current_template_parms
520 && uses_template_parms (decl));
522 if (is_friend_template
523 || template_class_depth (current_class_type) != 0)
524 /* We can't call pushdecl for a template class, since in
525 general, such a declaration depends on template
526 parameters. Instead, we call pushdecl when the class
527 is instantiated. */
528 decl = push_template_decl_real (decl, /*is_friend=*/true);
529 else if (current_function_decl)
530 /* This must be a local class, so pushdecl will be ok, and
531 insert an unqualified friend into the local scope
532 (rather than the containing namespace scope, which the
533 next choice will do). */
534 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
535 else
537 /* We can't use pushdecl, as we might be in a template
538 class specialization, and pushdecl will insert an
539 unqualified friend decl into the template parameter
540 scope, rather than the namespace containing it. */
541 tree ns = decl_namespace_context (decl);
543 push_nested_namespace (ns);
544 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
545 pop_nested_namespace (ns);
548 if (warn)
550 static int explained;
551 warning (0, "friend declaration %q#D declares a non-template "
552 "function", decl);
553 if (! explained)
555 warning (0, "(if this is not what you intended, make sure "
556 "the function template has already been declared "
557 "and add <> after the function name here) "
558 "-Wno-non-template-friend disables this warning");
559 explained = 1;
564 if (decl == error_mark_node)
565 return error_mark_node;
567 add_friend (current_class_type,
568 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
569 /*complain=*/true);
570 DECL_FRIEND_P (decl) = 1;
573 /* Unfortunately, we have to handle attributes here. Normally we would
574 handle them in start_decl_1, but since this is a friend decl start_decl_1
575 never gets to see it. */
577 /* Set attributes here so if duplicate decl, will have proper attributes. */
578 cplus_decl_attributes (&decl, attrlist, 0);
580 return decl;