Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / cp / friend.c
blob8203dbb3b6f66eab27aae95098e00f37730ea1c6
1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 /* Nested classes are implicitly friends of their enclosing types, as
82 per core issue 45 (this is a change from the standard). */
83 for (context = supplicant;
84 context && TYPE_P (context);
85 context = TYPE_CONTEXT (context))
86 if (type == context)
87 return 1;
89 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
90 for (; list ; list = TREE_CHAIN (list))
92 tree t = TREE_VALUE (list);
94 if (TREE_CODE (t) == TEMPLATE_DECL ?
95 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
96 same_type_p (supplicant, t))
97 return 1;
101 if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
102 context = DECL_CONTEXT (supplicant);
103 else if (! declp)
104 /* Local classes have the same access as the enclosing function. */
105 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
106 else
107 context = NULL_TREE;
109 /* A namespace is not friend to anybody. */
110 if (context && TREE_CODE (context) == NAMESPACE_DECL)
111 context = NULL_TREE;
113 if (context)
114 return is_friend (type, context);
116 return 0;
119 /* Add a new friend to the friends of the aggregate type TYPE.
120 DECL is the FUNCTION_DECL of the friend being added.
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. */
126 void
127 add_friend (tree type, tree decl, bool complain)
129 tree typedecl;
130 tree list;
131 tree name;
132 tree ctx;
134 if (decl == error_mark_node)
135 return;
137 typedecl = TYPE_MAIN_DECL (type);
138 list = DECL_FRIENDLIST (typedecl);
139 name = DECL_NAME (decl);
140 type = TREE_TYPE (typedecl);
142 while (list)
144 if (name == FRIEND_NAME (list))
146 tree friends = FRIEND_DECLS (list);
147 for (; friends ; friends = TREE_CHAIN (friends))
149 if (decl == TREE_VALUE (friends))
151 if (complain)
152 warning ("%qD is already a friend of class %qT",
153 decl, type);
154 return;
158 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
160 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
161 TREE_VALUE (list));
162 return;
164 list = TREE_CHAIN (list);
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);
171 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
173 DECL_FRIENDLIST (typedecl)
174 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
175 DECL_FRIENDLIST (typedecl));
176 if (!uses_template_parms (type))
177 DECL_BEFRIENDING_CLASSES (decl)
178 = tree_cons (NULL_TREE, type,
179 DECL_BEFRIENDING_CLASSES (decl));
182 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
183 been defined, we make all of its member functions friends of
184 TYPE. If not, we make it a pending friend, which can later be added
185 when its definition is seen. If a type is defined, then its TYPE_DECL's
186 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
187 classes that are not defined. If a type has not yet been defined,
188 then the DECL_WAITING_FRIENDS contains a list of types
189 waiting to make it their friend. Note that these two can both
190 be in use at the same time!
192 If COMPLAIN is true, warning about duplicate friend is issued.
193 We want to have this diagnostics during parsing but not
194 when a template is being instantiated. */
196 void
197 make_friend_class (tree type, tree friend_type, bool complain)
199 tree classes;
201 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
202 the enclosing class. FRIEND_DEPTH counts the number of template
203 headers used for this friend declaration. TEMPLATE_MEMBER_P,
204 defined inside the `if' block for TYPENAME_TYPE case, is true if
205 a template header in FRIEND_DEPTH is intended for DECLARATOR.
206 For example, the code
208 template <class T> struct A {
209 template <class U> struct B {
210 template <class V> template <class W>
211 friend class C<V>::D;
215 will eventually give the following results
217 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
218 2. FRIEND_DEPTH equals 2 (for `V' and `W').
219 3. TEMPLATE_MEMBER_P is true (for `W').
221 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
223 int class_template_depth = template_class_depth (type);
224 int friend_depth = processing_template_decl - class_template_depth;
226 if (! IS_AGGR_TYPE (friend_type))
228 error ("invalid type %qT declared %<friend%>", friend_type);
229 return;
232 if (friend_depth)
233 /* If the TYPE is a template then it makes sense for it to be
234 friends with itself; this means that each instantiation is
235 friends with all other instantiations. */
237 if (CLASS_TYPE_P (friend_type)
238 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
239 && uses_template_parms (friend_type))
241 /* [temp.friend]
242 Friend declarations shall not declare partial
243 specializations. */
244 error ("partial specialization %qT declared %<friend%>",
245 friend_type);
246 return;
249 else if (same_type_p (type, friend_type))
251 if (complain)
252 pedwarn ("class %qT is implicitly friends with itself",
253 type);
254 return;
257 /* [temp.friend]
259 A friend of a class or class template can be a function or
260 class template, a specialization of a function template or
261 class template, or an ordinary (nontemplate) function or
262 class. */
263 if (!friend_depth)
264 ;/* ok */
265 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
267 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
268 == TEMPLATE_ID_EXPR)
270 /* template <class U> friend class T::X<U>; */
271 /* [temp.friend]
272 Friend declarations shall not declare partial
273 specializations. */
274 error ("partial specialization %qT declared %<friend%>",
275 friend_type);
276 return;
278 else
280 /* We will figure this out later. */
281 bool template_member_p = false;
283 tree ctype = TYPE_CONTEXT (friend_type);
284 tree name = TYPE_IDENTIFIER (friend_type);
285 tree decl;
287 if (!uses_template_parms_level (ctype, class_template_depth
288 + friend_depth))
289 template_member_p = true;
291 if (class_template_depth)
293 /* We rely on tsubst_friend_class to check the
294 validity of the declaration later. */
295 if (template_member_p)
296 friend_type
297 = make_unbound_class_template (ctype,
298 name,
299 current_template_parms,
300 tf_error);
301 else
302 friend_type
303 = make_typename_type (ctype, name, class_type, tf_error);
305 else
307 decl = lookup_member (ctype, name, 0, true);
308 if (!decl)
310 error ("%qT is not a member of %qT", name, ctype);
311 return;
313 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
315 error ("%qT is not a member class template of %qT",
316 name, ctype);
317 cp_error_at ("%qD declared here", decl);
318 return;
320 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
321 || !CLASS_TYPE_P (TREE_TYPE (decl))))
323 error ("%qT is not a nested class of %qT",
324 name, ctype);
325 cp_error_at ("%qD declared here", decl);
326 return;
329 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
333 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
335 /* template <class T> friend class T; */
336 error ("template parameter type %qT declared %<friend%>", friend_type);
337 return;
339 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
341 /* template <class T> friend class A; where A is not a template */
342 error ("%q#T is not a template", friend_type);
343 return;
345 else
346 /* template <class T> friend class A; where A is a template */
347 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
349 if (friend_type == error_mark_node)
350 return;
352 /* See if it is already a friend. */
353 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
354 classes;
355 classes = TREE_CHAIN (classes))
357 tree probe = TREE_VALUE (classes);
359 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
361 if (friend_type == probe)
363 if (complain)
364 warning ("%qD is already a friend of %qT", probe, type);
365 break;
368 else if (TREE_CODE (probe) != TEMPLATE_DECL)
370 if (same_type_p (probe, friend_type))
372 if (complain)
373 warning ("%qT is already a friend of %qT", probe, type);
374 break;
379 if (!classes)
381 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
383 CLASSTYPE_FRIEND_CLASSES (type)
384 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
385 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
386 friend_type = TREE_TYPE (friend_type);
387 if (!uses_template_parms (type))
388 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
389 = tree_cons (NULL_TREE, type,
390 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
394 /* Main friend processor.
396 CTYPE is the class this friend belongs to.
398 DECLARATOR is the name of the friend.
400 DECL is the FUNCTION_DECL that the friend is.
402 FLAGS is just used for `grokclassfn'.
404 QUALS say what special qualifies should apply to the object
405 pointed to by `this'. */
407 tree
408 do_friend (tree ctype, tree declarator, tree decl,
409 tree attrlist, enum overload_flags flags,
410 cp_cv_quals quals,
411 int funcdef_flag)
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 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
425 if (ctype)
427 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
428 the enclosing class. FRIEND_DEPTH counts the number of template
429 headers used for this friend declaration. TEMPLATE_MEMBER_P is
430 true if a template header in FRIEND_DEPTH is intended for
431 DECLARATOR. For example, the code
433 template <class T> struct A {
434 template <class U> struct B {
435 template <class V> template <class W>
436 friend void C<V>::f(W);
440 will eventually give the following results
442 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
443 2. FRIEND_DEPTH equals 2 (for `V' and `W').
444 3. TEMPLATE_MEMBER_P is true (for `W'). */
446 int class_template_depth = template_class_depth (current_class_type);
447 int friend_depth = processing_template_decl - class_template_depth;
448 /* We will figure this out later. */
449 bool template_member_p = false;
451 tree cname = TYPE_NAME (ctype);
452 if (TREE_CODE (cname) == TYPE_DECL)
453 cname = DECL_NAME (cname);
455 /* A method friend. */
456 if (flags == NO_SPECIAL && declarator == cname)
457 DECL_CONSTRUCTOR_P (decl) = 1;
459 /* This will set up DECL_ARGUMENTS for us. */
460 grokclassfn (ctype, decl, flags, quals);
462 if (friend_depth)
464 if (!uses_template_parms_level (ctype, class_template_depth
465 + friend_depth))
466 template_member_p = true;
469 /* A nested class may declare a member of an enclosing class
470 to be a friend, so we do lookup here even if CTYPE is in
471 the process of being defined. */
472 if (class_template_depth
473 || COMPLETE_TYPE_P (ctype)
474 || TYPE_BEING_DEFINED (ctype))
476 if (DECL_TEMPLATE_INFO (decl))
477 /* DECL is a template specialization. No need to
478 build a new TEMPLATE_DECL. */
480 else if (class_template_depth)
481 /* We rely on tsubst_friend_function to check the
482 validity of the declaration later. */
483 decl = push_template_decl_real (decl, /*is_friend=*/1);
484 else
485 decl = check_classfn (ctype, decl,
486 template_member_p
487 ? current_template_parms
488 : NULL_TREE);
490 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
491 decl = DECL_TI_TEMPLATE (decl);
493 if (decl)
494 add_friend (current_class_type, decl, /*complain=*/true);
496 else
497 error ("member %qD declared as friend before type %qT defined",
498 decl, ctype);
500 /* A global friend.
501 @@ or possibly a friend from a base class ?!? */
502 else if (TREE_CODE (decl) == FUNCTION_DECL)
504 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
506 /* Friends must all go through the overload machinery,
507 even though they may not technically be overloaded.
509 Note that because classes all wind up being top-level
510 in their scope, their friend wind up in top-level scope as well. */
511 if (funcdef_flag)
512 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
514 if (! DECL_USE_TEMPLATE (decl))
516 /* We must check whether the decl refers to template
517 arguments before push_template_decl_real adds a
518 reference to the containing template class. */
519 int warn = (warn_nontemplate_friend
520 && ! funcdef_flag && ! is_friend_template
521 && current_template_parms
522 && uses_template_parms (decl));
524 if (is_friend_template
525 || template_class_depth (current_class_type) != 0)
526 /* We can't call pushdecl for a template class, since in
527 general, such a declaration depends on template
528 parameters. Instead, we call pushdecl when the class
529 is instantiated. */
530 decl = push_template_decl_real (decl, /*is_friend=*/1);
531 else if (current_function_decl)
532 /* This must be a local class, so pushdecl will be ok, and
533 insert an unqualified friend into the local scope
534 (rather than the containing namespace scope, which the
535 next choice will do). */
536 decl = pushdecl (decl);
537 else
539 /* We can't use pushdecl, as we might be in a template
540 class specialization, and pushdecl will insert an
541 unqualified friend decl into the template parameter
542 scope, rather than the namespace containing it. */
543 tree ns = decl_namespace_context (decl);
545 push_nested_namespace (ns);
546 decl = pushdecl_namespace_level (decl);
547 pop_nested_namespace (ns);
550 if (warn)
552 static int explained;
553 warning ("friend declaration %q#D declares a non-template "
554 "function", decl);
555 if (! explained)
557 warning ("(if this is not what you intended, make sure "
558 "the function template has already been declared "
559 "and add <> after the function name here) "
560 "-Wno-non-template-friend disables this warning");
561 explained = 1;
566 if (decl == error_mark_node)
567 return error_mark_node;
569 add_friend (current_class_type,
570 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
571 /*complain=*/true);
572 DECL_FRIEND_P (decl) = 1;
575 /* Unfortunately, we have to handle attributes here. Normally we would
576 handle them in start_decl_1, but since this is a friend decl start_decl_1
577 never gets to see it. */
579 /* Set attributes here so if duplicate decl, will have proper attributes. */
580 cplus_decl_attributes (&decl, attrlist, 0);
582 return decl;