Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / cp / friend.c
blobffb0baaa60031902c633c65edd43920486cd7db4
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "expr.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "toplev.h"
33 /* Friend data structures are described in cp-tree.h. */
35 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
37 int
38 is_friend (tree type, tree supplicant)
40 int declp;
41 tree list;
42 tree context;
44 if (supplicant == NULL_TREE || type == NULL_TREE)
45 return 0;
47 declp = DECL_P (supplicant);
49 if (declp)
50 /* It's a function decl. */
52 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
53 tree name = DECL_NAME (supplicant);
55 for (; list ; list = TREE_CHAIN (list))
57 if (name == FRIEND_NAME (list))
59 tree friends = FRIEND_DECLS (list);
60 for (; friends ; friends = TREE_CHAIN (friends))
62 tree friend = TREE_VALUE (friends);
64 if (friend == NULL_TREE)
65 continue;
67 if (supplicant == friend)
68 return 1;
70 if (is_specialization_of_friend (supplicant, friend))
71 return 1;
73 break;
77 else
78 /* It's a type. */
80 if (same_type_p (supplicant, type))
81 return 1;
83 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
84 for (; list ; list = TREE_CHAIN (list))
86 tree t = TREE_VALUE (list);
88 if (TREE_CODE (t) == TEMPLATE_DECL ?
89 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
90 same_type_p (supplicant, t))
91 return 1;
95 if (declp)
97 if (DECL_FUNCTION_MEMBER_P (supplicant))
98 context = DECL_CONTEXT (supplicant);
99 else
100 context = NULL_TREE;
102 else
104 if (TYPE_CLASS_SCOPE_P (supplicant))
105 /* Nested classes get the same access as their enclosing types, as
106 per DR 45 (this is a change from the standard). */
107 context = TYPE_CONTEXT (supplicant);
108 else
109 /* Local classes have the same access as the enclosing function. */
110 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
113 /* A namespace is not friend to anybody. */
114 if (context && TREE_CODE (context) == NAMESPACE_DECL)
115 context = NULL_TREE;
117 if (context)
118 return is_friend (type, context);
120 return 0;
123 /* Add a new friend to the friends of the aggregate type TYPE.
124 DECL is the FUNCTION_DECL of the friend being added.
126 If COMPLAIN is true, warning about duplicate friend is issued.
127 We want to have this diagnostics during parsing but not
128 when a template is being instantiated. */
130 void
131 add_friend (tree type, tree decl, bool complain)
133 tree typedecl;
134 tree list;
135 tree name;
136 tree ctx;
138 if (decl == error_mark_node)
139 return;
141 typedecl = TYPE_MAIN_DECL (type);
142 list = DECL_FRIENDLIST (typedecl);
143 name = DECL_NAME (decl);
144 type = TREE_TYPE (typedecl);
146 while (list)
148 if (name == FRIEND_NAME (list))
150 tree friends = FRIEND_DECLS (list);
151 for (; friends ; friends = TREE_CHAIN (friends))
153 if (decl == TREE_VALUE (friends))
155 if (complain)
156 warning (0, "%qD is already a friend of class %qT",
157 decl, type);
158 return;
162 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
164 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
165 TREE_VALUE (list));
166 return;
168 list = TREE_CHAIN (list);
171 ctx = DECL_CONTEXT (decl);
172 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
173 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl);
175 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
177 DECL_FRIENDLIST (typedecl)
178 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
179 DECL_FRIENDLIST (typedecl));
180 if (!uses_template_parms (type))
181 DECL_BEFRIENDING_CLASSES (decl)
182 = tree_cons (NULL_TREE, type,
183 DECL_BEFRIENDING_CLASSES (decl));
186 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
187 been defined, we make all of its member functions friends of
188 TYPE. If not, we make it a pending friend, which can later be added
189 when its definition is seen. If a type is defined, then its TYPE_DECL's
190 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
191 classes that are not defined. If a type has not yet been defined,
192 then the DECL_WAITING_FRIENDS contains a list of types
193 waiting to make it their friend. Note that these two can both
194 be in use at the same time!
196 If COMPLAIN is true, warning about duplicate friend is issued.
197 We want to have this diagnostics during parsing but not
198 when a template is being instantiated. */
200 void
201 make_friend_class (tree type, tree friend_type, bool complain)
203 tree classes;
205 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
206 the enclosing class. FRIEND_DEPTH counts the number of template
207 headers used for this friend declaration. TEMPLATE_MEMBER_P,
208 defined inside the `if' block for TYPENAME_TYPE case, is true if
209 a template header in FRIEND_DEPTH is intended for DECLARATOR.
210 For example, the code
212 template <class T> struct A {
213 template <class U> struct B {
214 template <class V> template <class W>
215 friend class C<V>::D;
219 will eventually give the following results
221 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
222 2. FRIEND_DEPTH equals 2 (for `V' and `W').
223 3. TEMPLATE_MEMBER_P is true (for `W').
225 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
227 int class_template_depth = template_class_depth (type);
228 int friend_depth = processing_template_decl - class_template_depth;
230 if (! IS_AGGR_TYPE (friend_type))
232 error ("invalid type %qT declared %<friend%>", friend_type);
233 return;
236 if (friend_depth)
237 /* If the TYPE is a template then it makes sense for it to be
238 friends with itself; this means that each instantiation is
239 friends with all other instantiations. */
241 if (CLASS_TYPE_P (friend_type)
242 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
243 && uses_template_parms (friend_type))
245 /* [temp.friend]
246 Friend declarations shall not declare partial
247 specializations. */
248 error ("partial specialization %qT declared %<friend%>",
249 friend_type);
250 return;
253 else if (same_type_p (type, friend_type))
255 if (complain)
256 pedwarn ("class %qT is implicitly friends with itself",
257 type);
258 return;
261 /* [temp.friend]
263 A friend of a class or class template can be a function or
264 class template, a specialization of a function template or
265 class template, or an ordinary (nontemplate) function or
266 class. */
267 if (!friend_depth)
268 ;/* ok */
269 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
271 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
272 == TEMPLATE_ID_EXPR)
274 /* template <class U> friend class T::X<U>; */
275 /* [temp.friend]
276 Friend declarations shall not declare partial
277 specializations. */
278 error ("partial specialization %qT declared %<friend%>",
279 friend_type);
280 return;
282 else
284 /* We will figure this out later. */
285 bool template_member_p = false;
287 tree ctype = TYPE_CONTEXT (friend_type);
288 tree name = TYPE_IDENTIFIER (friend_type);
289 tree decl;
291 if (!uses_template_parms_level (ctype, class_template_depth
292 + friend_depth))
293 template_member_p = true;
295 if (class_template_depth)
297 /* We rely on tsubst_friend_class to check the
298 validity of the declaration later. */
299 if (template_member_p)
300 friend_type
301 = make_unbound_class_template (ctype,
302 name,
303 current_template_parms,
304 tf_error);
305 else
306 friend_type
307 = make_typename_type (ctype, name, class_type, tf_error);
309 else
311 decl = lookup_member (ctype, name, 0, true);
312 if (!decl)
314 error ("%qT is not a member of %qT", name, ctype);
315 return;
317 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
319 error ("%qT is not a member class template of %qT",
320 name, ctype);
321 error ("%q+D declared here", decl);
322 return;
324 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
325 || !CLASS_TYPE_P (TREE_TYPE (decl))))
327 error ("%qT is not a nested class of %qT",
328 name, ctype);
329 error ("%q+D declared here", decl);
330 return;
333 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
337 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
339 /* template <class T> friend class T; */
340 error ("template parameter type %qT declared %<friend%>", friend_type);
341 return;
343 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
345 /* template <class T> friend class A; where A is not a template */
346 error ("%q#T is not a template", friend_type);
347 return;
349 else
350 /* template <class T> friend class A; where A is a template */
351 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
353 if (friend_type == error_mark_node)
354 return;
356 /* See if it is already a friend. */
357 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
358 classes;
359 classes = TREE_CHAIN (classes))
361 tree probe = TREE_VALUE (classes);
363 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
365 if (friend_type == probe)
367 if (complain)
368 warning (0, "%qD is already a friend of %qT", probe, type);
369 break;
372 else if (TREE_CODE (probe) != TEMPLATE_DECL)
374 if (same_type_p (probe, friend_type))
376 if (complain)
377 warning (0, "%qT is already a friend of %qT", probe, type);
378 break;
383 if (!classes)
385 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
387 CLASSTYPE_FRIEND_CLASSES (type)
388 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
389 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
390 friend_type = TREE_TYPE (friend_type);
391 if (!uses_template_parms (type))
392 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
393 = tree_cons (NULL_TREE, type,
394 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
398 /* Record DECL (a FUNCTION_DECL) as a friend of the
399 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
400 class of which it is a member, as named in the friend declaration.
401 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
402 friend declaration is a definition of the function. FLAGS is as
403 for grokclass fn. */
405 tree
406 do_friend (tree ctype, tree declarator, tree decl,
407 tree attrlist, enum overload_flags flags,
408 bool funcdef_flag)
410 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
411 gcc_assert (!ctype || IS_AGGR_TYPE (ctype));
413 /* Every decl that gets here is a friend of something. */
414 DECL_FRIEND_P (decl) = 1;
416 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
418 declarator = TREE_OPERAND (declarator, 0);
419 if (is_overloaded_fn (declarator))
420 declarator = DECL_NAME (get_first_fn (declarator));
423 if (ctype)
425 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
426 the enclosing class. FRIEND_DEPTH counts the number of template
427 headers used for this friend declaration. TEMPLATE_MEMBER_P is
428 true if a template header in FRIEND_DEPTH is intended for
429 DECLARATOR. For example, the code
431 template <class T> struct A {
432 template <class U> struct B {
433 template <class V> template <class W>
434 friend void C<V>::f(W);
438 will eventually give the following results
440 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
441 2. FRIEND_DEPTH equals 2 (for `V' and `W').
442 3. TEMPLATE_MEMBER_P is true (for `W'). */
444 int class_template_depth = template_class_depth (current_class_type);
445 int friend_depth = processing_template_decl - class_template_depth;
446 /* We will figure this out later. */
447 bool template_member_p = false;
449 tree cname = TYPE_NAME (ctype);
450 if (TREE_CODE (cname) == TYPE_DECL)
451 cname = DECL_NAME (cname);
453 /* A method friend. */
454 if (flags == NO_SPECIAL && declarator == cname)
455 DECL_CONSTRUCTOR_P (decl) = 1;
457 grokclassfn (ctype, decl, flags);
459 if (friend_depth)
461 if (!uses_template_parms_level (ctype, class_template_depth
462 + friend_depth))
463 template_member_p = true;
466 /* A nested class may declare a member of an enclosing class
467 to be a friend, so we do lookup here even if CTYPE is in
468 the process of being defined. */
469 if (class_template_depth
470 || COMPLETE_TYPE_P (ctype)
471 || (CLASS_TYPE_P (ctype) && TYPE_BEING_DEFINED (ctype)))
473 if (DECL_TEMPLATE_INFO (decl))
474 /* DECL is a template specialization. No need to
475 build a new TEMPLATE_DECL. */
477 else if (class_template_depth)
478 /* We rely on tsubst_friend_function to check the
479 validity of the declaration later. */
480 decl = push_template_decl_real (decl, /*is_friend=*/true);
481 else
482 decl = check_classfn (ctype, decl,
483 template_member_p
484 ? current_template_parms
485 : NULL_TREE);
487 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
488 decl = DECL_TI_TEMPLATE (decl);
490 if (decl)
491 add_friend (current_class_type, decl, /*complain=*/true);
493 else
494 error ("member %qD declared as friend before type %qT defined",
495 decl, ctype);
497 /* A global friend.
498 @@ or possibly a friend from a base class ?!? */
499 else if (TREE_CODE (decl) == FUNCTION_DECL)
501 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
503 /* Friends must all go through the overload machinery,
504 even though they may not technically be overloaded.
506 Note that because classes all wind up being top-level
507 in their scope, their friend wind up in top-level scope as well. */
508 if (funcdef_flag)
509 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
511 if (! DECL_USE_TEMPLATE (decl))
513 /* We must check whether the decl refers to template
514 arguments before push_template_decl_real adds a
515 reference to the containing template class. */
516 int warn = (warn_nontemplate_friend
517 && ! funcdef_flag && ! is_friend_template
518 && current_template_parms
519 && uses_template_parms (decl));
521 if (is_friend_template
522 || template_class_depth (current_class_type) != 0)
523 /* We can't call pushdecl for a template class, since in
524 general, such a declaration depends on template
525 parameters. Instead, we call pushdecl when the class
526 is instantiated. */
527 decl = push_template_decl_real (decl, /*is_friend=*/true);
528 else if (current_function_decl)
530 /* This must be a local class. 11.5p11:
532 If a friend declaration appears in a local class (9.8) and
533 the name specified is an unqualified name, a prior
534 declaration is looked up without considering scopes that
535 are outside the innermost enclosing non-class scope. For a
536 friend function declaration, if there is no prior
537 declaration, the program is ill-formed. */
538 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
539 if (t)
540 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
541 else
543 error ("friend declaration %qD in local class without "
544 "prior declaration", decl);
545 return error_mark_node;
548 else
550 /* We can't use pushdecl, as we might be in a template
551 class specialization, and pushdecl will insert an
552 unqualified friend decl into the template parameter
553 scope, rather than the namespace containing it. */
554 tree ns = decl_namespace_context (decl);
556 push_nested_namespace (ns);
557 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
558 pop_nested_namespace (ns);
561 if (warn)
563 static int explained;
564 warning (OPT_Wnon_template_friend, "friend declaration "
565 "%q#D declares a non-template function", decl);
566 if (! explained)
568 inform ("(if this is not what you intended, make sure "
569 "the function template has already been declared "
570 "and add <> after the function name here) ");
571 explained = 1;
576 if (decl == error_mark_node)
577 return error_mark_node;
579 add_friend (current_class_type,
580 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
581 /*complain=*/true);
582 DECL_FRIEND_P (decl) = 1;
585 /* Unfortunately, we have to handle attributes here. Normally we would
586 handle them in start_decl_1, but since this is a friend decl start_decl_1
587 never gets to see it. */
589 /* Set attributes here so if duplicate decl, will have proper attributes. */
590 cplus_decl_attributes (&decl, attrlist, 0);
592 return decl;