1 /* Help friends in C++.
2 Copyright (C) 1997-2018 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"
25 /* Friend data structures are described in cp-tree.h. */
28 /* The GLOBAL_FRIEND scope (functions, classes, or templates) is
29 regarded as a friend of every class. This is only used by libcc1,
30 to enable GDB's code snippets to access private members without
31 disabling access control in general, which could cause different
32 template overload resolution results when accessibility matters
33 (e.g. tests for an accessible member). */
35 static GTY(()) tree global_friend
;
37 /* Set the GLOBAL_FRIEND for this compilation session. It might be
38 set multiple times, but always to the same scope. */
41 set_global_friend (tree scope
)
43 gcc_checking_assert (scope
!= NULL_TREE
);
44 gcc_assert (!global_friend
|| global_friend
== scope
);
45 global_friend
= scope
;
48 /* Return TRUE if SCOPE is the global friend. */
51 is_global_friend (tree scope
)
53 gcc_checking_assert (scope
!= NULL_TREE
);
55 if (global_friend
== scope
)
61 if (is_specialization_of_friend (global_friend
, scope
))
67 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
70 is_friend (tree type
, tree supplicant
)
76 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
79 if (is_global_friend (supplicant
))
82 declp
= DECL_P (supplicant
);
85 /* It's a function decl. */
87 tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
88 tree name
= DECL_NAME (supplicant
);
90 for (; list
; list
= TREE_CHAIN (list
))
92 if (name
== FRIEND_NAME (list
))
94 tree friends
= FRIEND_DECLS (list
);
95 for (; friends
; friends
= TREE_CHAIN (friends
))
97 tree this_friend
= TREE_VALUE (friends
);
99 if (this_friend
== NULL_TREE
)
102 if (supplicant
== this_friend
)
105 if (is_specialization_of_friend (supplicant
, this_friend
))
115 if (same_type_p (supplicant
, type
))
118 list
= CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type
)));
119 for (; list
; list
= TREE_CHAIN (list
))
121 tree t
= TREE_VALUE (list
);
123 if (TREE_CODE (t
) == TEMPLATE_DECL
?
124 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant
), t
) :
125 same_type_p (supplicant
, t
))
132 if (DECL_FUNCTION_MEMBER_P (supplicant
))
133 context
= DECL_CONTEXT (supplicant
);
139 if (TYPE_CLASS_SCOPE_P (supplicant
))
140 /* Nested classes get the same access as their enclosing types, as
141 per DR 45 (this is a change from the standard). */
142 context
= TYPE_CONTEXT (supplicant
);
144 /* Local classes have the same access as the enclosing function. */
145 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
148 /* A namespace is not friend to anybody. */
149 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
153 return is_friend (type
, context
);
158 /* Add a new friend to the friends of the aggregate type TYPE.
159 DECL is the FUNCTION_DECL of the friend being added.
161 If COMPLAIN is true, warning about duplicate friend is issued.
162 We want to have this diagnostics during parsing but not
163 when a template is being instantiated. */
166 add_friend (tree type
, tree decl
, bool complain
)
173 if (decl
== error_mark_node
)
176 if (TREE_CODE (decl
) == FUNCTION_DECL
177 && DECL_TEMPLATE_INSTANTIATION (decl
))
178 /* We'll have parsed this as a declaration, and therefore not
179 marked the lookup set for keeping. Do that now. */
180 lookup_keep (DECL_TI_TEMPLATE (decl
));
182 typedecl
= TYPE_MAIN_DECL (type
);
183 list
= DECL_FRIENDLIST (typedecl
);
184 name
= DECL_NAME (decl
);
185 type
= TREE_TYPE (typedecl
);
189 if (name
== FRIEND_NAME (list
))
191 tree friends
= FRIEND_DECLS (list
);
192 for (; friends
; friends
= TREE_CHAIN (friends
))
194 if (decl
== TREE_VALUE (friends
))
197 warning (OPT_Wredundant_decls
,
198 "%qD is already a friend of class %qT",
204 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
208 list
= TREE_CHAIN (list
);
211 ctx
= DECL_CONTEXT (decl
);
212 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
213 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
214 tf_warning_or_error
);
216 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
219 DECL_FRIENDLIST (typedecl
)
220 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
221 DECL_FRIENDLIST (typedecl
));
222 if (!uses_template_parms (type
))
223 DECL_BEFRIENDING_CLASSES (decl
)
224 = tree_cons (NULL_TREE
, type
,
225 DECL_BEFRIENDING_CLASSES (decl
));
228 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
229 been defined, we make all of its member functions friends of
230 TYPE. If not, we make it a pending friend, which can later be added
231 when its definition is seen. If a type is defined, then its TYPE_DECL's
232 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
233 classes that are not defined. If a type has not yet been defined,
234 then the DECL_WAITING_FRIENDS contains a list of types
235 waiting to make it their friend. Note that these two can both
236 be in use at the same time!
238 If COMPLAIN is true, warning about duplicate friend is issued.
239 We want to have this diagnostics during parsing but not
240 when a template is being instantiated. */
243 make_friend_class (tree type
, tree friend_type
, bool complain
)
247 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
248 the enclosing class. FRIEND_DEPTH counts the number of template
249 headers used for this friend declaration. TEMPLATE_MEMBER_P,
250 defined inside the `if' block for TYPENAME_TYPE case, is true if
251 a template header in FRIEND_DEPTH is intended for DECLARATOR.
252 For example, the code
254 template <class T> struct A {
255 template <class U> struct B {
256 template <class V> template <class W>
257 friend class C<V>::D;
261 will eventually give the following results
263 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
264 2. FRIEND_DEPTH equals 2 (for `V' and `W').
265 3. TEMPLATE_MEMBER_P is true (for `W').
267 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
269 int class_template_depth
= template_class_depth (type
);
270 int friend_depth
= processing_template_decl
- class_template_depth
;
272 if (! MAYBE_CLASS_TYPE_P (friend_type
)
273 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
275 /* N1791: If the type specifier in a friend declaration designates a
276 (possibly cv-qualified) class type, that class is declared as a
277 friend; otherwise, the friend declaration is ignored.
279 So don't complain in C++11 mode. */
280 if (cxx_dialect
< cxx11
)
281 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
282 "invalid type %qT declared %<friend%>", friend_type
);
286 friend_type
= cv_unqualified (friend_type
);
288 if (check_for_bare_parameter_packs (friend_type
))
293 /* [temp.friend] Friend declarations shall not declare partial
295 if (CLASS_TYPE_P (friend_type
)
296 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
297 && uses_template_parms (friend_type
))
299 error ("partial specialization %qT declared %<friend%>",
304 if (TYPE_TEMPLATE_INFO (friend_type
)
305 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type
)))
307 error ("%qT is not a template", friend_type
);
308 inform (location_of (friend_type
), "previous declaration here");
309 if (TYPE_CLASS_SCOPE_P (friend_type
)
310 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type
))
311 && currently_open_class (TYPE_CONTEXT (friend_type
)))
312 inform (input_location
, "perhaps you need explicit template "
313 "arguments in your nested-name-specifier");
318 /* It makes sense for a template class to be friends with itself,
319 that means the instantiations can be friendly. Other cases are
320 not so meaningful. */
321 if (!friend_depth
&& same_type_p (type
, friend_type
))
324 warning (0, "class %qT is implicitly friends with itself",
331 A friend of a class or class template can be a function or
332 class template, a specialization of a function template or
333 class template, or an ordinary (nontemplate) function or
337 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
339 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
342 /* template <class U> friend class T::X<U>; */
344 Friend declarations shall not declare partial
346 error ("partial specialization %qT declared %<friend%>",
352 /* We will figure this out later. */
353 bool template_member_p
= false;
355 tree ctype
= TYPE_CONTEXT (friend_type
);
356 tree name
= TYPE_IDENTIFIER (friend_type
);
359 if (!uses_template_parms_level (ctype
, class_template_depth
361 template_member_p
= true;
363 if (class_template_depth
)
365 /* We rely on tsubst_friend_class to check the
366 validity of the declaration later. */
367 if (template_member_p
)
369 = make_unbound_class_template (ctype
,
371 current_template_parms
,
375 = make_typename_type (ctype
, name
, class_type
, tf_error
);
379 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
382 error ("%qT is not a member of %qT", name
, ctype
);
385 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
387 error ("%qT is not a member class template of %qT",
389 inform (DECL_SOURCE_LOCATION (decl
),
390 "%qD declared here", decl
);
393 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
394 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
396 error ("%qT is not a nested class of %qT",
398 inform (DECL_SOURCE_LOCATION (decl
),
399 "%qD declared here", decl
);
403 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
407 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
409 /* template <class T> friend class T; */
410 error ("template parameter type %qT declared %<friend%>", friend_type
);
413 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
414 friend_type
= TYPE_NAME (friend_type
);
415 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
417 /* template <class T> friend class A; where A is not a template */
418 error ("%q#T is not a template", friend_type
);
422 /* template <class T> friend class A; where A is a template */
423 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
425 if (friend_type
== error_mark_node
)
428 /* See if it is already a friend. */
429 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
431 classes
= TREE_CHAIN (classes
))
433 tree probe
= TREE_VALUE (classes
);
435 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
437 if (friend_type
== probe
)
440 warning (OPT_Wredundant_decls
,
441 "%qD is already a friend of %qT", probe
, type
);
445 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
447 if (same_type_p (probe
, friend_type
))
450 warning (OPT_Wredundant_decls
,
451 "%qT is already a friend of %qT", probe
, type
);
459 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
461 CLASSTYPE_FRIEND_CLASSES (type
)
462 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
463 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
464 friend_type
= TREE_TYPE (friend_type
);
465 if (!uses_template_parms (type
))
466 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
467 = tree_cons (NULL_TREE
, type
,
468 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
472 /* Record DECL (a FUNCTION_DECL) as a friend of the
473 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
474 class of which it is a member, as named in the friend declaration.
475 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
476 friend declaration is a definition of the function. FLAGS is as
480 do_friend (tree ctype
, tree declarator
, tree decl
,
481 tree attrlist
, enum overload_flags flags
,
484 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
485 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
487 /* Every decl that gets here is a friend of something. */
488 DECL_FRIEND_P (decl
) = 1;
490 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
491 error ("friend declaration %qD may not have virt-specifiers",
494 /* Unfortunately, we have to handle attributes here. Normally we would
495 handle them in start_decl_1, but since this is a friend decl start_decl_1
496 never gets to see it. */
498 /* Set attributes here so if duplicate decl, will have proper attributes. */
499 cplus_decl_attributes (&decl
, attrlist
, 0);
501 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
503 declarator
= TREE_OPERAND (declarator
, 0);
504 if (!identifier_p (declarator
))
505 declarator
= OVL_NAME (declarator
);
510 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
511 the enclosing class. FRIEND_DEPTH counts the number of template
512 headers used for this friend declaration. TEMPLATE_MEMBER_P is
513 true if a template header in FRIEND_DEPTH is intended for
514 DECLARATOR. For example, the code
516 template <class T> struct A {
517 template <class U> struct B {
518 template <class V> template <class W>
519 friend void C<V>::f(W);
523 will eventually give the following results
525 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
526 2. FRIEND_DEPTH equals 2 (for `V' and `W').
527 3. TEMPLATE_MEMBER_P is true (for `W'). */
529 int class_template_depth
= template_class_depth (current_class_type
);
530 int friend_depth
= processing_template_decl
- class_template_depth
;
531 /* We will figure this out later. */
532 bool template_member_p
= false;
534 tree cname
= TYPE_NAME (ctype
);
535 if (TREE_CODE (cname
) == TYPE_DECL
)
536 cname
= DECL_NAME (cname
);
538 /* A method friend. */
539 if (flags
== NO_SPECIAL
&& declarator
== cname
)
540 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
542 grokclassfn (ctype
, decl
, flags
);
546 if (!uses_template_parms_level (ctype
, class_template_depth
548 template_member_p
= true;
551 /* A nested class may declare a member of an enclosing class
552 to be a friend, so we do lookup here even if CTYPE is in
553 the process of being defined. */
554 if (class_template_depth
555 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
557 if (DECL_TEMPLATE_INFO (decl
))
558 /* DECL is a template specialization. No need to
559 build a new TEMPLATE_DECL. */
561 else if (class_template_depth
)
562 /* We rely on tsubst_friend_function to check the
563 validity of the declaration later. */
564 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
566 decl
= check_classfn (ctype
, decl
,
568 ? current_template_parms
571 if ((template_member_p
572 /* Always pull out the TEMPLATE_DECL if we have a friend
573 template in a class template so that it gets tsubsted
574 properly later on (59956). tsubst_friend_function knows
575 how to tell this apart from a member template. */
576 || (class_template_depth
&& friend_depth
))
577 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
578 decl
= DECL_TI_TEMPLATE (decl
);
581 add_friend (current_class_type
, decl
, /*complain=*/true);
584 error ("member %qD declared as friend before type %qT defined",
588 @@ or possibly a friend from a base class ?!? */
589 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
591 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
593 /* Friends must all go through the overload machinery,
594 even though they may not technically be overloaded.
596 Note that because classes all wind up being top-level
597 in their scope, their friend wind up in top-level scope as well. */
599 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
601 if (! DECL_USE_TEMPLATE (decl
))
603 /* We must check whether the decl refers to template
604 arguments before push_template_decl_real adds a
605 reference to the containing template class. */
606 int warn
= (warn_nontemplate_friend
607 && ! funcdef_flag
&& ! is_friend_template
608 && current_template_parms
609 && uses_template_parms (decl
));
611 if (is_friend_template
612 || template_class_depth (current_class_type
) != 0)
613 /* We can't call pushdecl for a template class, since in
614 general, such a declaration depends on template
615 parameters. Instead, we call pushdecl when the class
617 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
618 else if (current_function_decl
)
619 /* pushdecl will check there's a local decl already. */
620 decl
= pushdecl (decl
, /*is_friend=*/true);
623 /* We can't use pushdecl, as we might be in a template
624 class specialization, and pushdecl will insert an
625 unqualified friend decl into the template parameter
626 scope, rather than the namespace containing it. */
627 tree ns
= decl_namespace_context (decl
);
629 push_nested_namespace (ns
);
630 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
631 pop_nested_namespace (ns
);
636 static int explained
;
639 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
640 "%q#D declares a non-template function", decl
);
641 if (! explained
&& warned
)
643 inform (input_location
, "(if this is not what you intended, make sure "
644 "the function template has already been declared "
645 "and add <> after the function name here) ");
651 if (decl
== error_mark_node
)
652 return error_mark_node
;
654 add_friend (current_class_type
,
655 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
657 DECL_FRIEND_P (decl
) = 1;
663 #include "gt-cp-friend.h"