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_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);
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)
119 return is_friend (type, context);
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. */
132 add_friend (tree type, tree decl, bool complain)
139 if (decl == error_mark_node)
142 typedecl = TYPE_MAIN_DECL (type);
143 list = DECL_FRIENDLIST (typedecl);
144 name = DECL_NAME (decl);
145 type = TREE_TYPE (typedecl);
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))
157 warning (0, "%qD is already a friend of class %qT",
163 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
165 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
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. */
202 make_friend_class (tree type, tree friend_type, bool complain)
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);
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))
247 Friend declarations shall not declare partial
249 error ("partial specialization %qT declared %<friend%>",
254 else if (same_type_p (type, friend_type))
257 pedwarn ("class %qT is implicitly friends with itself",
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
270 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
272 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
275 /* template <class U> friend class T::X<U>; */
277 Friend declarations shall not declare partial
279 error ("partial specialization %qT declared %<friend%>",
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);
292 if (!uses_template_parms_level (ctype, class_template_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)
302 = make_unbound_class_template (ctype,
304 current_template_parms,
308 = make_typename_type (ctype, name, class_type, tf_error);
312 decl = lookup_member (ctype, name, 0, true);
315 error ("%qT is not a member of %qT", name, ctype);
318 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
320 error ("%qT is not a member class template of %qT",
322 error ("%q+D declared here", decl);
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",
330 error ("%q+D declared here", decl);
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);
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);
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)
357 /* See if it is already a friend. */
358 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
360 classes = TREE_CHAIN (classes))
362 tree probe = TREE_VALUE (classes);
364 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
366 if (friend_type == probe)
369 warning (0, "%qD is already a friend of %qT", probe, type);
373 else if (TREE_CODE (probe) != TEMPLATE_DECL)
375 if (same_type_p (probe, friend_type))
378 warning (0, "%qT is already a friend of %qT", probe, type);
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 /* Main friend processor.
401 CTYPE is the class this friend belongs to.
403 DECLARATOR is the name of the friend.
405 DECL is the FUNCTION_DECL that the friend is.
407 FLAGS is just used for `grokclassfn'. */
410 do_friend (tree ctype, tree declarator, tree decl,
411 tree attrlist, enum overload_flags flags,
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 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
428 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
429 the enclosing class. FRIEND_DEPTH counts the number of template
430 headers used for this friend declaration. TEMPLATE_MEMBER_P is
431 true if a template header in FRIEND_DEPTH is intended for
432 DECLARATOR. For example, the code
434 template <class T> struct A {
435 template <class U> struct B {
436 template <class V> template <class W>
437 friend void C<V>::f(W);
441 will eventually give the following results
443 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
444 2. FRIEND_DEPTH equals 2 (for `V' and `W').
445 3. TEMPLATE_MEMBER_P is true (for `W'). */
447 int class_template_depth = template_class_depth (current_class_type);
448 int friend_depth = processing_template_decl - class_template_depth;
449 /* We will figure this out later. */
450 bool template_member_p = false;
452 tree cname = TYPE_NAME (ctype);
453 if (TREE_CODE (cname) == TYPE_DECL)
454 cname = DECL_NAME (cname);
456 /* A method friend. */
457 if (flags == NO_SPECIAL && declarator == cname)
458 DECL_CONSTRUCTOR_P (decl) = 1;
460 grokclassfn (ctype, decl, flags);
464 if (!uses_template_parms_level (ctype, class_template_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=*/true);
485 decl = check_classfn (ctype, decl,
487 ? current_template_parms
490 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
491 decl = DECL_TI_TEMPLATE (decl);
494 add_friend (current_class_type, decl, /*complain=*/true);
497 error ("member %qD declared as friend before type %qT defined",
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. */
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
530 decl = push_template_decl_real (decl, /*is_friend=*/true);
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_maybe_friend (decl, /*is_friend=*/true);
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, /*is_friend=*/true);
547 pop_nested_namespace (ns);
552 static int explained;
553 warning (0, "friend declaration %q#D declares a non-template "
557 warning (0, "(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");
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,
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);