1 /* Help friends in C++.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
37 /* Friend data structures are described in cp-tree.h. */
39 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
42 is_friend (tree type, tree supplicant)
48 if (supplicant == NULL_TREE || type == NULL_TREE)
51 declp = DECL_P (supplicant);
54 /* It's a function decl. */
56 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
57 tree name = DECL_NAME (supplicant);
59 for (; list ; list = TREE_CHAIN (list))
61 if (name == FRIEND_NAME (list))
63 tree friends = FRIEND_DECLS (list);
64 for (; friends ; friends = TREE_CHAIN (friends))
66 tree this_friend = TREE_VALUE (friends);
68 if (this_friend == NULL_TREE)
71 if (supplicant == this_friend)
74 if (is_specialization_of_friend (supplicant, this_friend))
84 if (same_type_p (supplicant, type))
87 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
88 for (; list ; list = TREE_CHAIN (list))
90 tree t = TREE_VALUE (list);
92 if (TREE_CODE (t) == TEMPLATE_DECL ?
93 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
94 same_type_p (supplicant, t))
101 if (DECL_FUNCTION_MEMBER_P (supplicant))
102 context = DECL_CONTEXT (supplicant);
108 if (TYPE_CLASS_SCOPE_P (supplicant))
109 /* Nested classes get the same access as their enclosing types, as
110 per DR 45 (this is a change from the standard). */
111 context = TYPE_CONTEXT (supplicant);
113 /* Local classes have the same access as the enclosing function. */
114 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
117 /* A namespace is not friend to anybody. */
118 if (context && TREE_CODE (context) == NAMESPACE_DECL)
122 return is_friend (type, context);
127 /* Add a new friend to the friends of the aggregate type TYPE.
128 DECL is the FUNCTION_DECL of the friend being added.
130 If COMPLAIN is true, warning about duplicate friend is issued.
131 We want to have this diagnostics during parsing but not
132 when a template is being instantiated. */
135 add_friend (tree type, tree decl, bool complain)
142 if (decl == error_mark_node)
145 typedecl = TYPE_MAIN_DECL (type);
146 list = DECL_FRIENDLIST (typedecl);
147 name = DECL_NAME (decl);
148 type = TREE_TYPE (typedecl);
152 if (name == FRIEND_NAME (list))
154 tree friends = FRIEND_DECLS (list);
155 for (; friends ; friends = TREE_CHAIN (friends))
157 if (decl == TREE_VALUE (friends))
160 warning (OPT_Wredundant_decls,
161 "%qD is already a friend of class %qT",
167 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
169 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
173 list = TREE_CHAIN (list);
176 ctx = DECL_CONTEXT (decl);
177 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
178 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
179 tf_warning_or_error);
181 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
183 DECL_FRIENDLIST (typedecl)
184 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
185 DECL_FRIENDLIST (typedecl));
186 if (!uses_template_parms (type))
187 DECL_BEFRIENDING_CLASSES (decl)
188 = tree_cons (NULL_TREE, type,
189 DECL_BEFRIENDING_CLASSES (decl));
192 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
193 been defined, we make all of its member functions friends of
194 TYPE. If not, we make it a pending friend, which can later be added
195 when its definition is seen. If a type is defined, then its TYPE_DECL's
196 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
197 classes that are not defined. If a type has not yet been defined,
198 then the DECL_WAITING_FRIENDS contains a list of types
199 waiting to make it their friend. Note that these two can both
200 be in use at the same time!
202 If COMPLAIN is true, warning about duplicate friend is issued.
203 We want to have this diagnostics during parsing but not
204 when a template is being instantiated. */
207 make_friend_class (tree type, tree friend_type, bool complain)
211 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
212 the enclosing class. FRIEND_DEPTH counts the number of template
213 headers used for this friend declaration. TEMPLATE_MEMBER_P,
214 defined inside the `if' block for TYPENAME_TYPE case, is true if
215 a template header in FRIEND_DEPTH is intended for DECLARATOR.
216 For example, the code
218 template <class T> struct A {
219 template <class U> struct B {
220 template <class V> template <class W>
221 friend class C<V>::D;
225 will eventually give the following results
227 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
228 2. FRIEND_DEPTH equals 2 (for `V' and `W').
229 3. TEMPLATE_MEMBER_P is true (for `W').
231 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
233 int class_template_depth = template_class_depth (type);
234 int friend_depth = processing_template_decl - class_template_depth;
236 if (! MAYBE_CLASS_TYPE_P (friend_type)
237 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
239 /* N1791: If the type specifier in a friend declaration designates a
240 (possibly cv-qualified) class type, that class is declared as a
241 friend; otherwise, the friend declaration is ignored.
243 So don't complain in C++11 mode. */
244 if (cxx_dialect < cxx11)
245 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
246 "invalid type %qT declared %<friend%>", friend_type);
250 friend_type = cv_unqualified (friend_type);
252 if (check_for_bare_parameter_packs (friend_type))
256 /* If the TYPE is a template then it makes sense for it to be
257 friends with itself; this means that each instantiation is
258 friends with all other instantiations. */
260 if (CLASS_TYPE_P (friend_type)
261 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
262 && uses_template_parms (friend_type))
265 Friend declarations shall not declare partial
267 error ("partial specialization %qT declared %<friend%>",
272 else if (same_type_p (type, friend_type))
275 warning (0, "class %qT is implicitly friends with itself",
282 A friend of a class or class template can be a function or
283 class template, a specialization of a function template or
284 class template, or an ordinary (nontemplate) function or
288 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
290 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
293 /* template <class U> friend class T::X<U>; */
295 Friend declarations shall not declare partial
297 error ("partial specialization %qT declared %<friend%>",
303 /* We will figure this out later. */
304 bool template_member_p = false;
306 tree ctype = TYPE_CONTEXT (friend_type);
307 tree name = TYPE_IDENTIFIER (friend_type);
310 if (!uses_template_parms_level (ctype, class_template_depth
312 template_member_p = true;
314 if (class_template_depth)
316 /* We rely on tsubst_friend_class to check the
317 validity of the declaration later. */
318 if (template_member_p)
320 = make_unbound_class_template (ctype,
322 current_template_parms,
326 = make_typename_type (ctype, name, class_type, tf_error);
330 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
333 error ("%qT is not a member of %qT", name, ctype);
336 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
338 error ("%qT is not a member class template of %qT",
340 inform (input_location, "%q+D declared here", decl);
343 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
344 || !CLASS_TYPE_P (TREE_TYPE (decl))))
346 error ("%qT is not a nested class of %qT",
348 inform (input_location, "%q+D declared here", decl);
352 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
356 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
358 /* template <class T> friend class T; */
359 error ("template parameter type %qT declared %<friend%>", friend_type);
362 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
363 friend_type = TYPE_NAME (friend_type);
364 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
366 /* template <class T> friend class A; where A is not a template */
367 error ("%q#T is not a template", friend_type);
371 /* template <class T> friend class A; where A is a template */
372 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
374 if (friend_type == error_mark_node)
377 /* See if it is already a friend. */
378 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
380 classes = TREE_CHAIN (classes))
382 tree probe = TREE_VALUE (classes);
384 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
386 if (friend_type == probe)
389 warning (OPT_Wredundant_decls,
390 "%qD is already a friend of %qT", probe, type);
394 else if (TREE_CODE (probe) != TEMPLATE_DECL)
396 if (same_type_p (probe, friend_type))
399 warning (OPT_Wredundant_decls,
400 "%qT is already a friend of %qT", probe, type);
408 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
410 CLASSTYPE_FRIEND_CLASSES (type)
411 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
412 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
413 friend_type = TREE_TYPE (friend_type);
414 if (!uses_template_parms (type))
415 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
416 = tree_cons (NULL_TREE, type,
417 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
421 /* Record DECL (a FUNCTION_DECL) as a friend of the
422 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
423 class of which it is a member, as named in the friend declaration.
424 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
425 friend declaration is a definition of the function. FLAGS is as
429 do_friend (tree ctype, tree declarator, tree decl,
430 tree attrlist, enum overload_flags flags,
433 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
434 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
436 /* Every decl that gets here is a friend of something. */
437 DECL_FRIEND_P (decl) = 1;
439 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
440 error ("friend declaration %qD may not have virt-specifiers",
443 /* Unfortunately, we have to handle attributes here. Normally we would
444 handle them in start_decl_1, but since this is a friend decl start_decl_1
445 never gets to see it. */
447 /* Set attributes here so if duplicate decl, will have proper attributes. */
448 cplus_decl_attributes (&decl, attrlist, 0);
450 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
452 declarator = TREE_OPERAND (declarator, 0);
453 if (is_overloaded_fn (declarator))
454 declarator = DECL_NAME (get_first_fn (declarator));
459 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
460 the enclosing class. FRIEND_DEPTH counts the number of template
461 headers used for this friend declaration. TEMPLATE_MEMBER_P is
462 true if a template header in FRIEND_DEPTH is intended for
463 DECLARATOR. For example, the code
465 template <class T> struct A {
466 template <class U> struct B {
467 template <class V> template <class W>
468 friend void C<V>::f(W);
472 will eventually give the following results
474 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
475 2. FRIEND_DEPTH equals 2 (for `V' and `W').
476 3. TEMPLATE_MEMBER_P is true (for `W'). */
478 int class_template_depth = template_class_depth (current_class_type);
479 int friend_depth = processing_template_decl - class_template_depth;
480 /* We will figure this out later. */
481 bool template_member_p = false;
483 tree cname = TYPE_NAME (ctype);
484 if (TREE_CODE (cname) == TYPE_DECL)
485 cname = DECL_NAME (cname);
487 /* A method friend. */
488 if (flags == NO_SPECIAL && declarator == cname)
489 DECL_CONSTRUCTOR_P (decl) = 1;
491 grokclassfn (ctype, decl, flags);
495 if (!uses_template_parms_level (ctype, class_template_depth
497 template_member_p = true;
500 /* A nested class may declare a member of an enclosing class
501 to be a friend, so we do lookup here even if CTYPE is in
502 the process of being defined. */
503 if (class_template_depth
504 || COMPLETE_OR_OPEN_TYPE_P (ctype))
506 if (DECL_TEMPLATE_INFO (decl))
507 /* DECL is a template specialization. No need to
508 build a new TEMPLATE_DECL. */
510 else if (class_template_depth)
511 /* We rely on tsubst_friend_function to check the
512 validity of the declaration later. */
513 decl = push_template_decl_real (decl, /*is_friend=*/true);
515 decl = check_classfn (ctype, decl,
517 ? current_template_parms
520 if ((template_member_p
521 /* Always pull out the TEMPLATE_DECL if we have a friend
522 template in a class template so that it gets tsubsted
523 properly later on (59956). tsubst_friend_function knows
524 how to tell this apart from a member template. */
525 || (class_template_depth && friend_depth))
526 && decl && TREE_CODE (decl) == FUNCTION_DECL)
527 decl = DECL_TI_TEMPLATE (decl);
530 add_friend (current_class_type, decl, /*complain=*/true);
533 error ("member %qD declared as friend before type %qT defined",
537 @@ or possibly a friend from a base class ?!? */
538 else if (TREE_CODE (decl) == FUNCTION_DECL)
540 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
542 /* Friends must all go through the overload machinery,
543 even though they may not technically be overloaded.
545 Note that because classes all wind up being top-level
546 in their scope, their friend wind up in top-level scope as well. */
548 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
550 if (! DECL_USE_TEMPLATE (decl))
552 /* We must check whether the decl refers to template
553 arguments before push_template_decl_real adds a
554 reference to the containing template class. */
555 int warn = (warn_nontemplate_friend
556 && ! funcdef_flag && ! is_friend_template
557 && current_template_parms
558 && uses_template_parms (decl));
560 if (is_friend_template
561 || template_class_depth (current_class_type) != 0)
562 /* We can't call pushdecl for a template class, since in
563 general, such a declaration depends on template
564 parameters. Instead, we call pushdecl when the class
566 decl = push_template_decl_real (decl, /*is_friend=*/true);
567 else if (current_function_decl)
569 /* This must be a local class. 11.5p11:
571 If a friend declaration appears in a local class (9.8) and
572 the name specified is an unqualified name, a prior
573 declaration is looked up without considering scopes that
574 are outside the innermost enclosing non-class scope. For a
575 friend function declaration, if there is no prior
576 declaration, the program is ill-formed. */
577 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
579 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
582 error ("friend declaration %qD in local class without "
583 "prior declaration", decl);
584 return error_mark_node;
589 /* We can't use pushdecl, as we might be in a template
590 class specialization, and pushdecl will insert an
591 unqualified friend decl into the template parameter
592 scope, rather than the namespace containing it. */
593 tree ns = decl_namespace_context (decl);
595 push_nested_namespace (ns);
596 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
597 pop_nested_namespace (ns);
602 static int explained;
605 warned = warning (OPT_Wnon_template_friend, "friend declaration "
606 "%q#D declares a non-template function", decl);
607 if (! explained && warned)
609 inform (input_location, "(if this is not what you intended, make sure "
610 "the function template has already been declared "
611 "and add <> after the function name here) ");
617 if (decl == error_mark_node)
618 return error_mark_node;
620 add_friend (current_class_type,
621 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
623 DECL_FRIEND_P (decl) = 1;