1 /* Help friends in C++.
2 Copyright (C) 1997-2017 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 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 typedecl
= TYPE_MAIN_DECL (type
);
177 list
= DECL_FRIENDLIST (typedecl
);
178 name
= DECL_NAME (decl
);
179 type
= TREE_TYPE (typedecl
);
183 if (name
== FRIEND_NAME (list
))
185 tree friends
= FRIEND_DECLS (list
);
186 for (; friends
; friends
= TREE_CHAIN (friends
))
188 if (decl
== TREE_VALUE (friends
))
191 warning (OPT_Wredundant_decls
,
192 "%qD is already a friend of class %qT",
198 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
202 list
= TREE_CHAIN (list
);
205 ctx
= DECL_CONTEXT (decl
);
206 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
207 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
208 tf_warning_or_error
);
210 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
213 DECL_FRIENDLIST (typedecl
)
214 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
215 DECL_FRIENDLIST (typedecl
));
216 if (!uses_template_parms (type
))
217 DECL_BEFRIENDING_CLASSES (decl
)
218 = tree_cons (NULL_TREE
, type
,
219 DECL_BEFRIENDING_CLASSES (decl
));
222 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
223 been defined, we make all of its member functions friends of
224 TYPE. If not, we make it a pending friend, which can later be added
225 when its definition is seen. If a type is defined, then its TYPE_DECL's
226 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
227 classes that are not defined. If a type has not yet been defined,
228 then the DECL_WAITING_FRIENDS contains a list of types
229 waiting to make it their friend. Note that these two can both
230 be in use at the same time!
232 If COMPLAIN is true, warning about duplicate friend is issued.
233 We want to have this diagnostics during parsing but not
234 when a template is being instantiated. */
237 make_friend_class (tree type
, tree friend_type
, bool complain
)
241 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
242 the enclosing class. FRIEND_DEPTH counts the number of template
243 headers used for this friend declaration. TEMPLATE_MEMBER_P,
244 defined inside the `if' block for TYPENAME_TYPE case, is true if
245 a template header in FRIEND_DEPTH is intended for DECLARATOR.
246 For example, the code
248 template <class T> struct A {
249 template <class U> struct B {
250 template <class V> template <class W>
251 friend class C<V>::D;
255 will eventually give the following results
257 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
258 2. FRIEND_DEPTH equals 2 (for `V' and `W').
259 3. TEMPLATE_MEMBER_P is true (for `W').
261 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
263 int class_template_depth
= template_class_depth (type
);
264 int friend_depth
= processing_template_decl
- class_template_depth
;
266 if (! MAYBE_CLASS_TYPE_P (friend_type
)
267 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
269 /* N1791: If the type specifier in a friend declaration designates a
270 (possibly cv-qualified) class type, that class is declared as a
271 friend; otherwise, the friend declaration is ignored.
273 So don't complain in C++11 mode. */
274 if (cxx_dialect
< cxx11
)
275 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
276 "invalid type %qT declared %<friend%>", friend_type
);
280 friend_type
= cv_unqualified (friend_type
);
282 if (check_for_bare_parameter_packs (friend_type
))
286 /* If the TYPE is a template then it makes sense for it to be
287 friends with itself; this means that each instantiation is
288 friends with all other instantiations. */
290 if (CLASS_TYPE_P (friend_type
)
291 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
292 && uses_template_parms (friend_type
))
295 Friend declarations shall not declare partial
297 error ("partial specialization %qT declared %<friend%>",
301 if (TYPE_TEMPLATE_INFO (friend_type
)
302 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type
)))
304 error ("%qT is not a template", friend_type
);
305 inform (location_of (friend_type
), "previous declaration here");
306 if (TYPE_CLASS_SCOPE_P (friend_type
)
307 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type
))
308 && currently_open_class (TYPE_CONTEXT (friend_type
)))
309 inform (input_location
, "perhaps you need explicit template "
310 "arguments in your nested-name-specifier");
314 else if (same_type_p (type
, friend_type
))
317 warning (0, "class %qT is implicitly friends with itself",
324 A friend of a class or class template can be a function or
325 class template, a specialization of a function template or
326 class template, or an ordinary (nontemplate) function or
330 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
332 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
335 /* template <class U> friend class T::X<U>; */
337 Friend declarations shall not declare partial
339 error ("partial specialization %qT declared %<friend%>",
345 /* We will figure this out later. */
346 bool template_member_p
= false;
348 tree ctype
= TYPE_CONTEXT (friend_type
);
349 tree name
= TYPE_IDENTIFIER (friend_type
);
352 if (!uses_template_parms_level (ctype
, class_template_depth
354 template_member_p
= true;
356 if (class_template_depth
)
358 /* We rely on tsubst_friend_class to check the
359 validity of the declaration later. */
360 if (template_member_p
)
362 = make_unbound_class_template (ctype
,
364 current_template_parms
,
368 = make_typename_type (ctype
, name
, class_type
, tf_error
);
372 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
375 error ("%qT is not a member of %qT", name
, ctype
);
378 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
380 error ("%qT is not a member class template of %qT",
382 inform (DECL_SOURCE_LOCATION (decl
),
383 "%qD declared here", decl
);
386 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
387 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
389 error ("%qT is not a nested class of %qT",
391 inform (DECL_SOURCE_LOCATION (decl
),
392 "%qD declared here", decl
);
396 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
400 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
402 /* template <class T> friend class T; */
403 error ("template parameter type %qT declared %<friend%>", friend_type
);
406 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
407 friend_type
= TYPE_NAME (friend_type
);
408 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
410 /* template <class T> friend class A; where A is not a template */
411 error ("%q#T is not a template", friend_type
);
415 /* template <class T> friend class A; where A is a template */
416 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
418 if (friend_type
== error_mark_node
)
421 /* See if it is already a friend. */
422 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
424 classes
= TREE_CHAIN (classes
))
426 tree probe
= TREE_VALUE (classes
);
428 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
430 if (friend_type
== probe
)
433 warning (OPT_Wredundant_decls
,
434 "%qD is already a friend of %qT", probe
, type
);
438 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
440 if (same_type_p (probe
, friend_type
))
443 warning (OPT_Wredundant_decls
,
444 "%qT is already a friend of %qT", probe
, type
);
452 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
454 CLASSTYPE_FRIEND_CLASSES (type
)
455 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
456 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
457 friend_type
= TREE_TYPE (friend_type
);
458 if (!uses_template_parms (type
))
459 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
460 = tree_cons (NULL_TREE
, type
,
461 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
465 /* Record DECL (a FUNCTION_DECL) as a friend of the
466 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
467 class of which it is a member, as named in the friend declaration.
468 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
469 friend declaration is a definition of the function. FLAGS is as
473 do_friend (tree ctype
, tree declarator
, tree decl
,
474 tree attrlist
, enum overload_flags flags
,
477 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
478 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
480 /* Every decl that gets here is a friend of something. */
481 DECL_FRIEND_P (decl
) = 1;
483 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
484 error ("friend declaration %qD may not have virt-specifiers",
487 /* Unfortunately, we have to handle attributes here. Normally we would
488 handle them in start_decl_1, but since this is a friend decl start_decl_1
489 never gets to see it. */
491 /* Set attributes here so if duplicate decl, will have proper attributes. */
492 cplus_decl_attributes (&decl
, attrlist
, 0);
494 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
496 declarator
= TREE_OPERAND (declarator
, 0);
497 if (is_overloaded_fn (declarator
))
498 declarator
= DECL_NAME (get_first_fn (declarator
));
503 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
504 the enclosing class. FRIEND_DEPTH counts the number of template
505 headers used for this friend declaration. TEMPLATE_MEMBER_P is
506 true if a template header in FRIEND_DEPTH is intended for
507 DECLARATOR. For example, the code
509 template <class T> struct A {
510 template <class U> struct B {
511 template <class V> template <class W>
512 friend void C<V>::f(W);
516 will eventually give the following results
518 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
519 2. FRIEND_DEPTH equals 2 (for `V' and `W').
520 3. TEMPLATE_MEMBER_P is true (for `W'). */
522 int class_template_depth
= template_class_depth (current_class_type
);
523 int friend_depth
= processing_template_decl
- class_template_depth
;
524 /* We will figure this out later. */
525 bool template_member_p
= false;
527 tree cname
= TYPE_NAME (ctype
);
528 if (TREE_CODE (cname
) == TYPE_DECL
)
529 cname
= DECL_NAME (cname
);
531 /* A method friend. */
532 if (flags
== NO_SPECIAL
&& declarator
== cname
)
533 DECL_CONSTRUCTOR_P (decl
) = 1;
535 grokclassfn (ctype
, decl
, flags
);
539 if (!uses_template_parms_level (ctype
, class_template_depth
541 template_member_p
= true;
544 /* A nested class may declare a member of an enclosing class
545 to be a friend, so we do lookup here even if CTYPE is in
546 the process of being defined. */
547 if (class_template_depth
548 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
550 if (DECL_TEMPLATE_INFO (decl
))
551 /* DECL is a template specialization. No need to
552 build a new TEMPLATE_DECL. */
554 else if (class_template_depth
)
555 /* We rely on tsubst_friend_function to check the
556 validity of the declaration later. */
557 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
559 decl
= check_classfn (ctype
, decl
,
561 ? current_template_parms
564 if ((template_member_p
565 /* Always pull out the TEMPLATE_DECL if we have a friend
566 template in a class template so that it gets tsubsted
567 properly later on (59956). tsubst_friend_function knows
568 how to tell this apart from a member template. */
569 || (class_template_depth
&& friend_depth
))
570 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
571 decl
= DECL_TI_TEMPLATE (decl
);
574 add_friend (current_class_type
, decl
, /*complain=*/true);
577 error ("member %qD declared as friend before type %qT defined",
581 @@ or possibly a friend from a base class ?!? */
582 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
584 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
586 /* Friends must all go through the overload machinery,
587 even though they may not technically be overloaded.
589 Note that because classes all wind up being top-level
590 in their scope, their friend wind up in top-level scope as well. */
592 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
594 if (! DECL_USE_TEMPLATE (decl
))
596 /* We must check whether the decl refers to template
597 arguments before push_template_decl_real adds a
598 reference to the containing template class. */
599 int warn
= (warn_nontemplate_friend
600 && ! funcdef_flag
&& ! is_friend_template
601 && current_template_parms
602 && uses_template_parms (decl
));
604 if (is_friend_template
605 || template_class_depth (current_class_type
) != 0)
606 /* We can't call pushdecl for a template class, since in
607 general, such a declaration depends on template
608 parameters. Instead, we call pushdecl when the class
610 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
611 else if (current_function_decl
)
613 /* This must be a local class. 11.5p11:
615 If a friend declaration appears in a local class (9.8) and
616 the name specified is an unqualified name, a prior
617 declaration is looked up without considering scopes that
618 are outside the innermost enclosing non-class scope. For a
619 friend function declaration, if there is no prior
620 declaration, the program is ill-formed. */
621 tree t
= lookup_name_innermost_nonclass_level (DECL_NAME (decl
));
623 decl
= pushdecl_maybe_friend (decl
, /*is_friend=*/true);
626 error ("friend declaration %qD in local class without "
627 "prior declaration", decl
);
628 return error_mark_node
;
633 /* We can't use pushdecl, as we might be in a template
634 class specialization, and pushdecl will insert an
635 unqualified friend decl into the template parameter
636 scope, rather than the namespace containing it. */
637 tree ns
= decl_namespace_context (decl
);
639 push_nested_namespace (ns
);
640 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
641 pop_nested_namespace (ns
);
646 static int explained
;
649 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
650 "%q#D declares a non-template function", decl
);
651 if (! explained
&& warned
)
653 inform (input_location
, "(if this is not what you intended, make sure "
654 "the function template has already been declared "
655 "and add <> after the function name here) ");
661 if (decl
== error_mark_node
)
662 return error_mark_node
;
664 add_friend (current_class_type
,
665 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
667 DECL_FRIEND_P (decl
) = 1;