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)
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. */
24 #include "coretypes.h"
34 /* Friend data structures are described in cp-tree.h. */
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
39 is_friend (tree type
, tree supplicant
)
45 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
48 declp
= DECL_P (supplicant
);
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
)
68 if (supplicant
== friend)
71 if (is_specialization_of_friend (supplicant
, friend))
81 if (same_type_p (supplicant
, type
))
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
))
98 if (DECL_FUNCTION_MEMBER_P (supplicant
))
99 context
= DECL_CONTEXT (supplicant
);
105 if (TYPE_CONTEXT (supplicant
)
106 && TYPE_P (TYPE_CONTEXT (supplicant
)))
107 /* Nested classes get the same access as their enclosing types, as
108 per DR 45 (this is a change from the standard). */
109 context
= TYPE_CONTEXT (supplicant
);
111 /* Local classes have the same access as the enclosing function. */
112 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
115 /* A namespace is not friend to anybody. */
116 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
120 return is_friend (type
, context
);
125 /* Add a new friend to the friends of the aggregate type TYPE.
126 DECL is the FUNCTION_DECL of the friend being added.
128 If COMPLAIN is true, warning about duplicate friend is issued.
129 We want to have this diagnostics during parsing but not
130 when a template is being instantiated. */
133 add_friend (tree type
, tree decl
, bool complain
)
140 if (decl
== error_mark_node
)
143 typedecl
= TYPE_MAIN_DECL (type
);
144 list
= DECL_FRIENDLIST (typedecl
);
145 name
= DECL_NAME (decl
);
146 type
= TREE_TYPE (typedecl
);
150 if (name
== FRIEND_NAME (list
))
152 tree friends
= FRIEND_DECLS (list
);
153 for (; friends
; friends
= TREE_CHAIN (friends
))
155 if (decl
== TREE_VALUE (friends
))
158 warning (0, "%qD is already a friend of class %qT",
164 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
166 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
170 list
= TREE_CHAIN (list
);
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
);
177 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
179 DECL_FRIENDLIST (typedecl
)
180 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
181 DECL_FRIENDLIST (typedecl
));
182 if (!uses_template_parms (type
))
183 DECL_BEFRIENDING_CLASSES (decl
)
184 = tree_cons (NULL_TREE
, type
,
185 DECL_BEFRIENDING_CLASSES (decl
));
188 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
189 been defined, we make all of its member functions friends of
190 TYPE. If not, we make it a pending friend, which can later be added
191 when its definition is seen. If a type is defined, then its TYPE_DECL's
192 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
193 classes that are not defined. If a type has not yet been defined,
194 then the DECL_WAITING_FRIENDS contains a list of types
195 waiting to make it their friend. Note that these two can both
196 be in use at the same time!
198 If COMPLAIN is true, warning about duplicate friend is issued.
199 We want to have this diagnostics during parsing but not
200 when a template is being instantiated. */
203 make_friend_class (tree type
, tree friend_type
, bool complain
)
207 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
208 the enclosing class. FRIEND_DEPTH counts the number of template
209 headers used for this friend declaration. TEMPLATE_MEMBER_P,
210 defined inside the `if' block for TYPENAME_TYPE case, is true if
211 a template header in FRIEND_DEPTH is intended for DECLARATOR.
212 For example, the code
214 template <class T> struct A {
215 template <class U> struct B {
216 template <class V> template <class W>
217 friend class C<V>::D;
221 will eventually give the following results
223 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
224 2. FRIEND_DEPTH equals 2 (for `V' and `W').
225 3. TEMPLATE_MEMBER_P is true (for `W').
227 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
229 int class_template_depth
= template_class_depth (type
);
230 int friend_depth
= processing_template_decl
- class_template_depth
;
232 if (! IS_AGGR_TYPE (friend_type
))
234 error ("invalid type %qT declared %<friend%>", friend_type
);
239 /* If the TYPE is a template then it makes sense for it to be
240 friends with itself; this means that each instantiation is
241 friends with all other instantiations. */
243 if (CLASS_TYPE_P (friend_type
)
244 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
245 && uses_template_parms (friend_type
))
248 Friend declarations shall not declare partial
250 error ("partial specialization %qT declared %<friend%>",
255 else if (same_type_p (type
, friend_type
))
258 pedwarn ("class %qT is implicitly friends with itself",
265 A friend of a class or class template can be a function or
266 class template, a specialization of a function template or
267 class template, or an ordinary (nontemplate) function or
271 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
273 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
276 /* template <class U> friend class T::X<U>; */
278 Friend declarations shall not declare partial
280 error ("partial specialization %qT declared %<friend%>",
286 /* We will figure this out later. */
287 bool template_member_p
= false;
289 tree ctype
= TYPE_CONTEXT (friend_type
);
290 tree name
= TYPE_IDENTIFIER (friend_type
);
293 if (!uses_template_parms_level (ctype
, class_template_depth
295 template_member_p
= true;
297 if (class_template_depth
)
299 /* We rely on tsubst_friend_class to check the
300 validity of the declaration later. */
301 if (template_member_p
)
303 = make_unbound_class_template (ctype
,
305 current_template_parms
,
309 = make_typename_type (ctype
, name
, class_type
, tf_error
);
313 decl
= lookup_member (ctype
, name
, 0, true);
316 error ("%qT is not a member of %qT", name
, ctype
);
319 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
321 error ("%qT is not a member class template of %qT",
323 error ("%q+D declared here", decl
);
326 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
327 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
329 error ("%qT is not a nested class of %qT",
331 error ("%q+D declared here", decl
);
335 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
339 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
341 /* template <class T> friend class T; */
342 error ("template parameter type %qT declared %<friend%>", friend_type
);
345 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
347 /* template <class T> friend class A; where A is not a template */
348 error ("%q#T is not a template", friend_type
);
352 /* template <class T> friend class A; where A is a template */
353 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
355 if (friend_type
== error_mark_node
)
358 /* See if it is already a friend. */
359 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
361 classes
= TREE_CHAIN (classes
))
363 tree probe
= TREE_VALUE (classes
);
365 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
367 if (friend_type
== probe
)
370 warning (0, "%qD is already a friend of %qT", probe
, type
);
374 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
376 if (same_type_p (probe
, friend_type
))
379 warning (0, "%qT is already a friend of %qT", probe
, type
);
387 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
389 CLASSTYPE_FRIEND_CLASSES (type
)
390 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
391 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
392 friend_type
= TREE_TYPE (friend_type
);
393 if (!uses_template_parms (type
))
394 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
395 = tree_cons (NULL_TREE
, type
,
396 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
400 /* Main friend processor.
402 CTYPE is the class this friend belongs to.
404 DECLARATOR is the name of the friend.
406 DECL is the FUNCTION_DECL that the friend is.
408 FLAGS is just used for `grokclassfn'.
410 QUALS say what special qualifies should apply to the object
411 pointed to by `this'. */
414 do_friend (tree ctype
, tree declarator
, tree decl
,
415 tree attrlist
, enum overload_flags flags
,
419 /* Every decl that gets here is a friend of something. */
420 DECL_FRIEND_P (decl
) = 1;
422 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
424 declarator
= TREE_OPERAND (declarator
, 0);
425 if (is_overloaded_fn (declarator
))
426 declarator
= DECL_NAME (get_first_fn (declarator
));
429 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
433 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
434 the enclosing class. FRIEND_DEPTH counts the number of template
435 headers used for this friend declaration. TEMPLATE_MEMBER_P is
436 true if a template header in FRIEND_DEPTH is intended for
437 DECLARATOR. For example, the code
439 template <class T> struct A {
440 template <class U> struct B {
441 template <class V> template <class W>
442 friend void C<V>::f(W);
446 will eventually give the following results
448 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
449 2. FRIEND_DEPTH equals 2 (for `V' and `W').
450 3. TEMPLATE_MEMBER_P is true (for `W'). */
452 int class_template_depth
= template_class_depth (current_class_type
);
453 int friend_depth
= processing_template_decl
- class_template_depth
;
454 /* We will figure this out later. */
455 bool template_member_p
= false;
457 tree cname
= TYPE_NAME (ctype
);
458 if (TREE_CODE (cname
) == TYPE_DECL
)
459 cname
= DECL_NAME (cname
);
461 /* A method friend. */
462 if (flags
== NO_SPECIAL
&& declarator
== cname
)
463 DECL_CONSTRUCTOR_P (decl
) = 1;
465 /* This will set up DECL_ARGUMENTS for us. */
466 grokclassfn (ctype
, decl
, flags
, quals
);
470 if (!uses_template_parms_level (ctype
, class_template_depth
472 template_member_p
= true;
475 /* A nested class may declare a member of an enclosing class
476 to be a friend, so we do lookup here even if CTYPE is in
477 the process of being defined. */
478 if (class_template_depth
479 || COMPLETE_TYPE_P (ctype
)
480 || TYPE_BEING_DEFINED (ctype
))
482 if (DECL_TEMPLATE_INFO (decl
))
483 /* DECL is a template specialization. No need to
484 build a new TEMPLATE_DECL. */
486 else if (class_template_depth
)
487 /* We rely on tsubst_friend_function to check the
488 validity of the declaration later. */
489 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
491 decl
= check_classfn (ctype
, decl
,
493 ? current_template_parms
496 if (template_member_p
&& decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
497 decl
= DECL_TI_TEMPLATE (decl
);
500 add_friend (current_class_type
, decl
, /*complain=*/true);
503 error ("member %qD declared as friend before type %qT defined",
507 @@ or possibly a friend from a base class ?!? */
508 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
510 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
512 /* Friends must all go through the overload machinery,
513 even though they may not technically be overloaded.
515 Note that because classes all wind up being top-level
516 in their scope, their friend wind up in top-level scope as well. */
518 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
520 if (! DECL_USE_TEMPLATE (decl
))
522 /* We must check whether the decl refers to template
523 arguments before push_template_decl_real adds a
524 reference to the containing template class. */
525 int warn
= (warn_nontemplate_friend
526 && ! funcdef_flag
&& ! is_friend_template
527 && current_template_parms
528 && uses_template_parms (decl
));
530 if (is_friend_template
531 || template_class_depth (current_class_type
) != 0)
532 /* We can't call pushdecl for a template class, since in
533 general, such a declaration depends on template
534 parameters. Instead, we call pushdecl when the class
536 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
537 else if (current_function_decl
)
538 /* This must be a local class, so pushdecl will be ok, and
539 insert an unqualified friend into the local scope
540 (rather than the containing namespace scope, which the
541 next choice will do). */
542 decl
= pushdecl_maybe_friend (decl
, /*is_friend=*/true);
545 /* We can't use pushdecl, as we might be in a template
546 class specialization, and pushdecl will insert an
547 unqualified friend decl into the template parameter
548 scope, rather than the namespace containing it. */
549 tree ns
= decl_namespace_context (decl
);
551 push_nested_namespace (ns
);
552 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
553 pop_nested_namespace (ns
);
558 static int explained
;
559 warning (0, "friend declaration %q#D declares a non-template "
563 warning (0, "(if this is not what you intended, make sure "
564 "the function template has already been declared "
565 "and add <> after the function name here) "
566 "-Wno-non-template-friend disables this warning");
572 if (decl
== error_mark_node
)
573 return error_mark_node
;
575 add_friend (current_class_type
,
576 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
578 DECL_FRIEND_P (decl
) = 1;
581 /* Unfortunately, we have to handle attributes here. Normally we would
582 handle them in start_decl_1, but since this is a friend decl start_decl_1
583 never gets to see it. */
585 /* Set attributes here so if duplicate decl, will have proper attributes. */
586 cplus_decl_attributes (&decl
, attrlist
, 0);