1 /* Help friends in C++.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2010, 2011 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)
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/>. */
23 #include "coretypes.h"
30 /* Friend data structures are described in cp-tree.h. */
32 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
35 is_friend (tree type, tree supplicant)
41 if (supplicant == NULL_TREE || type == NULL_TREE)
44 declp = DECL_P (supplicant);
47 /* It's a function decl. */
49 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
50 tree name = DECL_NAME (supplicant);
52 for (; list ; list = TREE_CHAIN (list))
54 if (name == FRIEND_NAME (list))
56 tree friends = FRIEND_DECLS (list);
57 for (; friends ; friends = TREE_CHAIN (friends))
59 tree this_friend = TREE_VALUE (friends);
61 if (this_friend == NULL_TREE)
64 if (supplicant == this_friend)
67 if (is_specialization_of_friend (supplicant, this_friend))
77 if (same_type_p (supplicant, type))
80 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
81 for (; list ; list = TREE_CHAIN (list))
83 tree t = TREE_VALUE (list);
85 if (TREE_CODE (t) == TEMPLATE_DECL ?
86 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
87 same_type_p (supplicant, t))
94 if (DECL_FUNCTION_MEMBER_P (supplicant))
95 context = DECL_CONTEXT (supplicant);
101 if (TYPE_CLASS_SCOPE_P (supplicant))
102 /* Nested classes get the same access as their enclosing types, as
103 per DR 45 (this is a change from the standard). */
104 context = TYPE_CONTEXT (supplicant);
106 /* Local classes have the same access as the enclosing function. */
107 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
110 /* A namespace is not friend to anybody. */
111 if (context && TREE_CODE (context) == NAMESPACE_DECL)
115 return is_friend (type, context);
120 /* Add a new friend to the friends of the aggregate type TYPE.
121 DECL is the FUNCTION_DECL of the friend being added.
123 If COMPLAIN is true, warning about duplicate friend is issued.
124 We want to have this diagnostics during parsing but not
125 when a template is being instantiated. */
128 add_friend (tree type, tree decl, bool complain)
135 if (decl == error_mark_node)
138 typedecl = TYPE_MAIN_DECL (type);
139 list = DECL_FRIENDLIST (typedecl);
140 name = DECL_NAME (decl);
141 type = TREE_TYPE (typedecl);
145 if (name == FRIEND_NAME (list))
147 tree friends = FRIEND_DECLS (list);
148 for (; friends ; friends = TREE_CHAIN (friends))
150 if (decl == TREE_VALUE (friends))
153 warning (0, "%qD is already a friend of class %qT",
159 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
161 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
165 list = TREE_CHAIN (list);
168 ctx = DECL_CONTEXT (decl);
169 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
170 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl);
172 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
174 DECL_FRIENDLIST (typedecl)
175 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
176 DECL_FRIENDLIST (typedecl));
177 if (!uses_template_parms (type))
178 DECL_BEFRIENDING_CLASSES (decl)
179 = tree_cons (NULL_TREE, type,
180 DECL_BEFRIENDING_CLASSES (decl));
183 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
184 been defined, we make all of its member functions friends of
185 TYPE. If not, we make it a pending friend, which can later be added
186 when its definition is seen. If a type is defined, then its TYPE_DECL's
187 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
188 classes that are not defined. If a type has not yet been defined,
189 then the DECL_WAITING_FRIENDS contains a list of types
190 waiting to make it their friend. Note that these two can both
191 be in use at the same time!
193 If COMPLAIN is true, warning about duplicate friend is issued.
194 We want to have this diagnostics during parsing but not
195 when a template is being instantiated. */
198 make_friend_class (tree type, tree friend_type, bool complain)
202 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
203 the enclosing class. FRIEND_DEPTH counts the number of template
204 headers used for this friend declaration. TEMPLATE_MEMBER_P,
205 defined inside the `if' block for TYPENAME_TYPE case, is true if
206 a template header in FRIEND_DEPTH is intended for DECLARATOR.
207 For example, the code
209 template <class T> struct A {
210 template <class U> struct B {
211 template <class V> template <class W>
212 friend class C<V>::D;
216 will eventually give the following results
218 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
219 2. FRIEND_DEPTH equals 2 (for `V' and `W').
220 3. TEMPLATE_MEMBER_P is true (for `W').
222 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
224 int class_template_depth = template_class_depth (type);
225 int friend_depth = processing_template_decl - class_template_depth;
227 if (! MAYBE_CLASS_TYPE_P (friend_type))
229 /* N1791: If the type specifier in a friend declaration designates a
230 (possibly cv-qualified) class type, that class is declared as a
231 friend; otherwise, the friend declaration is ignored.
233 So don't complain in C++0x mode. */
234 if (cxx_dialect < cxx0x)
235 pedwarn (input_location, complain ? 0 : OPT_pedantic,
236 "invalid type %qT declared %<friend%>", friend_type);
240 friend_type = cv_unqualified (friend_type);
243 /* If the TYPE is a template then it makes sense for it to be
244 friends with itself; this means that each instantiation is
245 friends with all other instantiations. */
247 if (CLASS_TYPE_P (friend_type)
248 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
249 && uses_template_parms (friend_type))
252 Friend declarations shall not declare partial
254 error ("partial specialization %qT declared %<friend%>",
259 else if (same_type_p (type, friend_type))
262 warning (0, "class %qT is implicitly friends with itself",
269 A friend of a class or class template can be a function or
270 class template, a specialization of a function template or
271 class template, or an ordinary (nontemplate) function or
275 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
277 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
280 /* template <class U> friend class T::X<U>; */
282 Friend declarations shall not declare partial
284 error ("partial specialization %qT declared %<friend%>",
290 /* We will figure this out later. */
291 bool template_member_p = false;
293 tree ctype = TYPE_CONTEXT (friend_type);
294 tree name = TYPE_IDENTIFIER (friend_type);
297 if (!uses_template_parms_level (ctype, class_template_depth
299 template_member_p = true;
301 if (class_template_depth)
303 /* We rely on tsubst_friend_class to check the
304 validity of the declaration later. */
305 if (template_member_p)
307 = make_unbound_class_template (ctype,
309 current_template_parms,
313 = make_typename_type (ctype, name, class_type, tf_error);
317 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
320 error ("%qT is not a member of %qT", name, ctype);
323 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
325 error ("%qT is not a member class template of %qT",
327 error ("%q+D declared here", decl);
330 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
331 || !CLASS_TYPE_P (TREE_TYPE (decl))))
333 error ("%qT is not a nested class of %qT",
335 error ("%q+D declared here", decl);
339 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
343 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
345 /* template <class T> friend class T; */
346 error ("template parameter type %qT declared %<friend%>", friend_type);
349 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
351 /* template <class T> friend class A; where A is not a template */
352 error ("%q#T is not a template", friend_type);
356 /* template <class T> friend class A; where A is a template */
357 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
359 if (friend_type == error_mark_node)
362 /* See if it is already a friend. */
363 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
365 classes = TREE_CHAIN (classes))
367 tree probe = TREE_VALUE (classes);
369 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
371 if (friend_type == probe)
374 warning (0, "%qD is already a friend of %qT", probe, type);
378 else if (TREE_CODE (probe) != TEMPLATE_DECL)
380 if (same_type_p (probe, friend_type))
383 warning (0, "%qT is already a friend of %qT", probe, type);
391 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
393 CLASSTYPE_FRIEND_CLASSES (type)
394 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
395 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
396 friend_type = TREE_TYPE (friend_type);
397 if (!uses_template_parms (type))
398 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
399 = tree_cons (NULL_TREE, type,
400 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
404 /* Record DECL (a FUNCTION_DECL) as a friend of the
405 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
406 class of which it is a member, as named in the friend declaration.
407 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
408 friend declaration is a definition of the function. FLAGS is as
412 do_friend (tree ctype, tree declarator, tree decl,
413 tree attrlist, enum overload_flags flags,
416 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
417 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
419 /* Every decl that gets here is a friend of something. */
420 DECL_FRIEND_P (decl) = 1;
422 /* Unfortunately, we have to handle attributes here. Normally we would
423 handle them in start_decl_1, but since this is a friend decl start_decl_1
424 never gets to see it. */
426 /* Set attributes here so if duplicate decl, will have proper attributes. */
427 cplus_decl_attributes (&decl, attrlist, 0);
429 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
431 declarator = TREE_OPERAND (declarator, 0);
432 if (is_overloaded_fn (declarator))
433 declarator = DECL_NAME (get_first_fn (declarator));
438 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
439 the enclosing class. FRIEND_DEPTH counts the number of template
440 headers used for this friend declaration. TEMPLATE_MEMBER_P is
441 true if a template header in FRIEND_DEPTH is intended for
442 DECLARATOR. For example, the code
444 template <class T> struct A {
445 template <class U> struct B {
446 template <class V> template <class W>
447 friend void C<V>::f(W);
451 will eventually give the following results
453 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
454 2. FRIEND_DEPTH equals 2 (for `V' and `W').
455 3. TEMPLATE_MEMBER_P is true (for `W'). */
457 int class_template_depth = template_class_depth (current_class_type);
458 int friend_depth = processing_template_decl - class_template_depth;
459 /* We will figure this out later. */
460 bool template_member_p = false;
462 tree cname = TYPE_NAME (ctype);
463 if (TREE_CODE (cname) == TYPE_DECL)
464 cname = DECL_NAME (cname);
466 /* A method friend. */
467 if (flags == NO_SPECIAL && declarator == cname)
468 DECL_CONSTRUCTOR_P (decl) = 1;
470 grokclassfn (ctype, decl, flags);
474 if (!uses_template_parms_level (ctype, class_template_depth
476 template_member_p = true;
479 /* A nested class may declare a member of an enclosing class
480 to be a friend, so we do lookup here even if CTYPE is in
481 the process of being defined. */
482 if (class_template_depth
483 || COMPLETE_TYPE_P (ctype)
484 || (CLASS_TYPE_P (ctype) && TYPE_BEING_DEFINED (ctype)))
486 if (DECL_TEMPLATE_INFO (decl))
487 /* DECL is a template specialization. No need to
488 build a new TEMPLATE_DECL. */
490 else if (class_template_depth)
491 /* We rely on tsubst_friend_function to check the
492 validity of the declaration later. */
493 decl = push_template_decl_real (decl, /*is_friend=*/true);
495 decl = check_classfn (ctype, decl,
497 ? current_template_parms
500 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
501 decl = DECL_TI_TEMPLATE (decl);
504 add_friend (current_class_type, decl, /*complain=*/true);
507 error ("member %qD declared as friend before type %qT defined",
511 @@ or possibly a friend from a base class ?!? */
512 else if (TREE_CODE (decl) == FUNCTION_DECL)
514 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
516 /* Friends must all go through the overload machinery,
517 even though they may not technically be overloaded.
519 Note that because classes all wind up being top-level
520 in their scope, their friend wind up in top-level scope as well. */
522 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
524 if (! DECL_USE_TEMPLATE (decl))
526 /* We must check whether the decl refers to template
527 arguments before push_template_decl_real adds a
528 reference to the containing template class. */
529 int warn = (warn_nontemplate_friend
530 && ! funcdef_flag && ! is_friend_template
531 && current_template_parms
532 && uses_template_parms (decl));
534 if (is_friend_template
535 || template_class_depth (current_class_type) != 0)
536 /* We can't call pushdecl for a template class, since in
537 general, such a declaration depends on template
538 parameters. Instead, we call pushdecl when the class
540 decl = push_template_decl_real (decl, /*is_friend=*/true);
541 else if (current_function_decl)
543 /* This must be a local class. 11.5p11:
545 If a friend declaration appears in a local class (9.8) and
546 the name specified is an unqualified name, a prior
547 declaration is looked up without considering scopes that
548 are outside the innermost enclosing non-class scope. For a
549 friend function declaration, if there is no prior
550 declaration, the program is ill-formed. */
551 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
553 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
556 error ("friend declaration %qD in local class without "
557 "prior declaration", decl);
558 return error_mark_node;
563 /* We can't use pushdecl, as we might be in a template
564 class specialization, and pushdecl will insert an
565 unqualified friend decl into the template parameter
566 scope, rather than the namespace containing it. */
567 tree ns = decl_namespace_context (decl);
569 push_nested_namespace (ns);
570 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
571 pop_nested_namespace (ns);
576 static int explained;
579 warned = warning (OPT_Wnon_template_friend, "friend declaration "
580 "%q#D declares a non-template function", decl);
581 if (! explained && warned)
583 inform (input_location, "(if this is not what you intended, make sure "
584 "the function template has already been declared "
585 "and add <> after the function name here) ");
591 if (decl == error_mark_node)
592 return error_mark_node;
594 add_friend (current_class_type,
595 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
597 DECL_FRIEND_P (decl) = 1;