1 /* Help friends in C++.
2 Copyright (C) 1997-2023 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
);
134 else if (tree fc
= DECL_FRIEND_CONTEXT (supplicant
))
141 if (TYPE_CLASS_SCOPE_P (supplicant
))
142 /* Nested classes get the same access as their enclosing types, as
143 per DR 45 (this is a change from the standard). */
144 context
= TYPE_CONTEXT (supplicant
);
146 /* Local classes have the same access as the enclosing function. */
147 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
150 /* A namespace is not friend to anybody. */
151 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
155 return is_friend (type
, context
);
160 /* Add a new friend to the friends of the aggregate type TYPE.
161 DECL is the FUNCTION_DECL of the friend being added.
163 If COMPLAIN is true, warning about duplicate friend is issued.
164 We want to have this diagnostics during parsing but not
165 when a template is being instantiated. */
168 add_friend (tree type
, tree decl
, bool complain
)
175 if (decl
== error_mark_node
)
178 typedecl
= TYPE_MAIN_DECL (type
);
179 list
= DECL_FRIENDLIST (typedecl
);
180 name
= DECL_NAME (decl
);
181 type
= TREE_TYPE (typedecl
);
185 if (name
== FRIEND_NAME (list
))
187 tree friends
= FRIEND_DECLS (list
);
188 for (; friends
; friends
= TREE_CHAIN (friends
))
190 if (decl
== TREE_VALUE (friends
))
193 warning (OPT_Wredundant_decls
,
194 "%qD is already a friend of class %qT",
200 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
204 list
= TREE_CHAIN (list
);
207 ctx
= DECL_CONTEXT (decl
);
208 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
209 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
210 tf_warning_or_error
);
212 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
215 DECL_FRIENDLIST (typedecl
)
216 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
217 DECL_FRIENDLIST (typedecl
));
218 if (!uses_template_parms (type
))
219 DECL_BEFRIENDING_CLASSES (decl
)
220 = tree_cons (NULL_TREE
, type
,
221 DECL_BEFRIENDING_CLASSES (decl
));
224 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
225 been defined, we make all of its member functions friends of
226 TYPE. If not, we make it a pending friend, which can later be added
227 when its definition is seen. If a type is defined, then its TYPE_DECL's
228 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
229 classes that are not defined. If a type has not yet been defined,
230 then the DECL_WAITING_FRIENDS contains a list of types
231 waiting to make it their friend. Note that these two can both
232 be in use at the same time!
234 If COMPLAIN is true, warning about duplicate friend is issued.
235 We want to have this diagnostics during parsing but not
236 when a template is being instantiated. */
239 make_friend_class (tree type
, tree friend_type
, bool complain
)
243 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
244 the enclosing class. FRIEND_DEPTH counts the number of template
245 headers used for this friend declaration. TEMPLATE_MEMBER_P,
246 defined inside the `if' block for TYPENAME_TYPE case, is true if
247 a template header in FRIEND_DEPTH is intended for DECLARATOR.
248 For example, the code
250 template <class T> struct A {
251 template <class U> struct B {
252 template <class V> template <class W>
253 friend class C<V>::D;
257 will eventually give the following results
259 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
260 2. FRIEND_DEPTH equals 2 (for `V' and `W').
261 3. TEMPLATE_MEMBER_P is true (for `W').
263 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
265 int class_template_depth
= template_class_depth (type
);
266 int friend_depth
= 0;
267 if (current_template_depth
)
268 /* When processing a friend declaration at parse time, just compare the
269 current depth to that of the class template. */
270 friend_depth
= current_template_depth
- class_template_depth
;
273 /* Otherwise, we got here from instantiate_class_template. Determine
274 the friend depth by looking at the template parameters used within
276 gcc_checking_assert (class_template_depth
== 0);
277 while (uses_template_parms_level (friend_type
, friend_depth
+ 1))
281 if (! MAYBE_CLASS_TYPE_P (friend_type
)
282 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
284 /* N1791: If the type specifier in a friend declaration designates a
285 (possibly cv-qualified) class type, that class is declared as a
286 friend; otherwise, the friend declaration is ignored.
288 So don't complain in C++11 mode. */
289 if (cxx_dialect
< cxx11
)
290 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
291 "invalid type %qT declared %<friend%>", friend_type
);
295 friend_type
= cv_unqualified (friend_type
);
297 if (check_for_bare_parameter_packs (friend_type
))
302 /* [temp.friend] Friend declarations shall not declare partial
304 if (CLASS_TYPE_P (friend_type
)
305 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
306 && uses_template_parms (friend_type
))
308 error ("partial specialization %qT declared %<friend%>",
313 if (TYPE_TEMPLATE_INFO (friend_type
)
314 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type
)))
316 auto_diagnostic_group d
;
317 error ("%qT is not a template", friend_type
);
318 inform (location_of (friend_type
), "previous declaration here");
319 if (TYPE_CLASS_SCOPE_P (friend_type
)
320 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type
))
321 && currently_open_class (TYPE_CONTEXT (friend_type
)))
322 inform (input_location
, "perhaps you need explicit template "
323 "arguments in your nested-name-specifier");
328 /* It makes sense for a template class to be friends with itself,
329 that means the instantiations can be friendly. Other cases are
330 not so meaningful. */
331 if (!friend_depth
&& same_type_p (type
, friend_type
))
334 warning (0, "class %qT is implicitly friends with itself",
341 A friend of a class or class template can be a function or
342 class template, a specialization of a function template or
343 class template, or an ordinary (nontemplate) function or
347 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
349 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
352 /* template <class U> friend class T::X<U>; */
354 Friend declarations shall not declare partial
356 error ("partial specialization %qT declared %<friend%>",
362 /* We will figure this out later. */
363 bool template_member_p
= false;
365 tree ctype
= TYPE_CONTEXT (friend_type
);
366 tree name
= TYPE_IDENTIFIER (friend_type
);
369 /* We need to distinguish a TYPENAME_TYPE for the non-template
371 template<class T> friend class A<T>::B;
372 vs for the class template B in
373 template<class T> template<class U> friend class A<T>::B; */
374 if (current_template_depth
375 && !uses_template_parms_level (ctype
, current_template_depth
))
376 template_member_p
= true;
378 if (class_template_depth
)
380 /* We rely on tsubst_friend_class to check the
381 validity of the declaration later. */
382 if (template_member_p
)
384 = make_unbound_class_template (ctype
,
386 current_template_parms
,
390 = make_typename_type (ctype
, name
, class_type
, tf_error
);
394 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
397 error ("%qT is not a member of %qT", name
, ctype
);
400 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
402 auto_diagnostic_group d
;
403 error ("%qT is not a member class template of %qT",
405 inform (DECL_SOURCE_LOCATION (decl
),
406 "%qD declared here", decl
);
409 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
410 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
412 auto_diagnostic_group d
;
413 error ("%qT is not a nested class of %qT",
415 inform (DECL_SOURCE_LOCATION (decl
),
416 "%qD declared here", decl
);
420 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
424 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
426 /* template <class T> friend class T; */
427 error ("template parameter type %qT declared %<friend%>", friend_type
);
430 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
431 friend_type
= TYPE_NAME (friend_type
);
432 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
434 /* template <class T> friend class A; where A is not a template */
435 error ("%q#T is not a template", friend_type
);
439 /* template <class T> friend class A; where A is a template */
440 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
442 if (friend_type
== error_mark_node
)
445 /* See if it is already a friend. */
446 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
448 classes
= TREE_CHAIN (classes
))
450 tree probe
= TREE_VALUE (classes
);
452 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
454 if (friend_type
== probe
)
457 warning (OPT_Wredundant_decls
,
458 "%qD is already a friend of %qT", probe
, type
);
462 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
464 if (same_type_p (probe
, friend_type
))
467 warning (OPT_Wredundant_decls
,
468 "%qT is already a friend of %qT", probe
, type
);
476 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
478 CLASSTYPE_FRIEND_CLASSES (type
)
479 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
480 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
481 friend_type
= TREE_TYPE (friend_type
);
482 if (!uses_template_parms (type
))
483 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
484 = tree_cons (NULL_TREE
, type
,
485 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
489 /* Record DECL (a FUNCTION_DECL) as a friend of the
490 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
491 class of which it is a member, as named in the friend declaration.
492 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
493 friend declaration is a definition of the function. FLAGS is as
497 do_friend (tree ctype
, tree declarator
, tree decl
,
498 enum overload_flags flags
,
501 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
502 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
504 /* Friend functions are unique, until proved otherwise. */
505 DECL_UNIQUE_FRIEND_P (decl
) = 1;
507 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
508 error ("friend declaration %qD may not have virt-specifiers",
511 tree orig_declarator
= declarator
;
512 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
514 declarator
= TREE_OPERAND (declarator
, 0);
515 if (!identifier_p (declarator
))
516 declarator
= OVL_NAME (declarator
);
519 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
520 the enclosing class. FRIEND_DEPTH counts the number of template
521 headers used for this friend declaration. TEMPLATE_MEMBER_P is
522 true if a template header in FRIEND_DEPTH is intended for
523 DECLARATOR. For example, the code
525 template <class T> struct A {
526 template <class U> struct B {
527 template <class V> template <class W>
528 friend void C<V>::f(W);
532 will eventually give the following results
534 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
535 2. FRIEND_DEPTH equals 2 (for `V' and `W').
536 3. CTYPE_DEPTH equals 1 (for `V').
537 4. TEMPLATE_MEMBER_P is true (for `W'). */
539 int class_template_depth
= template_class_depth (current_class_type
);
540 int friend_depth
= current_template_depth
- class_template_depth
;
541 int ctype_depth
= num_template_headers_for_class (ctype
);
542 bool template_member_p
= friend_depth
> ctype_depth
;
546 tree cname
= TYPE_NAME (ctype
);
547 if (TREE_CODE (cname
) == TYPE_DECL
)
548 cname
= DECL_NAME (cname
);
550 /* A method friend. */
551 if (flags
== NO_SPECIAL
&& declarator
== cname
)
552 DECL_CXX_CONSTRUCTOR_P (decl
) = 1;
554 grokclassfn (ctype
, decl
, flags
);
556 /* A nested class may declare a member of an enclosing class
557 to be a friend, so we do lookup here even if CTYPE is in
558 the process of being defined. */
559 if (class_template_depth
560 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
562 if (DECL_TEMPLATE_INFO (decl
))
563 /* DECL is a template specialization. No need to
564 build a new TEMPLATE_DECL. */
566 else if (class_template_depth
)
567 /* We rely on tsubst_friend_function to check the
568 validity of the declaration later. */
569 decl
= push_template_decl (decl
, /*is_friend=*/true);
571 decl
= check_classfn (ctype
, decl
,
573 ? current_template_parms
576 if ((template_member_p
577 /* Always pull out the TEMPLATE_DECL if we have a friend
578 template in a class template so that it gets tsubsted
579 properly later on (59956). tsubst_friend_function knows
580 how to tell this apart from a member template. */
581 || (class_template_depth
&& friend_depth
))
582 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
583 decl
= DECL_TI_TEMPLATE (decl
);
586 error ("member %qD declared as friend before type %qT defined",
591 /* Namespace-scope friend function. */
594 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
596 if (! DECL_USE_TEMPLATE (decl
))
598 /* We must check whether the decl refers to template
599 arguments before push_template_decl adds a reference to
600 the containing template class. */
601 int warn
= (warn_nontemplate_friend
602 && ! funcdef_flag
&& ! friend_depth
603 && current_template_parms
604 && uses_template_parms (decl
));
606 if (friend_depth
|| class_template_depth
)
607 /* We can't call pushdecl for a template class, since in
608 general, such a declaration depends on template
609 parameters. Instead, we call pushdecl when the class
611 decl
= push_template_decl (decl
, /*is_friend=*/true);
612 else if (current_function_decl
)
613 /* pushdecl will check there's a local decl already. */
614 decl
= pushdecl (decl
, /*hiding=*/true);
617 /* We can't use pushdecl, as we might be in a template
618 class specialization, and pushdecl will insert an
619 unqualified friend decl into the template parameter
620 scope, rather than the namespace containing it. */
621 tree ns
= decl_namespace_context (decl
);
623 push_nested_namespace (ns
);
624 decl
= pushdecl_namespace_level (decl
, /*hiding=*/true);
625 pop_nested_namespace (ns
);
630 static int explained
;
633 auto_diagnostic_group d
;
634 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
635 "%q#D declares a non-template function", decl
);
636 if (! explained
&& warned
)
638 inform (input_location
, "(if this is not what you intended, "
639 "make sure the function template has already been "
640 "declared and add %<<>%> after the function name "
648 if (decl
== error_mark_node
)
649 return error_mark_node
;
651 if (!class_template_depth
&& DECL_IMPLICIT_INSTANTIATION (decl
))
652 /* "[if no non-template match is found,] each remaining function template
653 is replaced with the specialization chosen by deduction from the
654 friend declaration or discarded if deduction fails."
656 set_decl_namespace or check_classfn set DECL_IMPLICIT_INSTANTIATION to
657 indicate that we need a template match, so ask
658 check_explicit_specialization to find one. */
659 decl
= (check_explicit_specialization
660 (orig_declarator
, decl
, ctype_depth
,
661 2 * funcdef_flag
+ 4));
663 add_friend (current_class_type
,
664 (!ctype
&& friend_depth
) ? DECL_TI_TEMPLATE (decl
) : decl
,
670 #include "gt-cp-friend.h"