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"
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 (OPT_Wredundant_decls
,
154 "%qD is already a friend of class %qT",
160 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
162 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
166 list
= TREE_CHAIN (list
);
169 ctx
= DECL_CONTEXT (decl
);
170 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
171 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
172 tf_warning_or_error
);
174 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
176 DECL_FRIENDLIST (typedecl
)
177 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
178 DECL_FRIENDLIST (typedecl
));
179 if (!uses_template_parms (type
))
180 DECL_BEFRIENDING_CLASSES (decl
)
181 = tree_cons (NULL_TREE
, type
,
182 DECL_BEFRIENDING_CLASSES (decl
));
185 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
186 been defined, we make all of its member functions friends of
187 TYPE. If not, we make it a pending friend, which can later be added
188 when its definition is seen. If a type is defined, then its TYPE_DECL's
189 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
190 classes that are not defined. If a type has not yet been defined,
191 then the DECL_WAITING_FRIENDS contains a list of types
192 waiting to make it their friend. Note that these two can both
193 be in use at the same time!
195 If COMPLAIN is true, warning about duplicate friend is issued.
196 We want to have this diagnostics during parsing but not
197 when a template is being instantiated. */
200 make_friend_class (tree type
, tree friend_type
, bool complain
)
204 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
205 the enclosing class. FRIEND_DEPTH counts the number of template
206 headers used for this friend declaration. TEMPLATE_MEMBER_P,
207 defined inside the `if' block for TYPENAME_TYPE case, is true if
208 a template header in FRIEND_DEPTH is intended for DECLARATOR.
209 For example, the code
211 template <class T> struct A {
212 template <class U> struct B {
213 template <class V> template <class W>
214 friend class C<V>::D;
218 will eventually give the following results
220 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
221 2. FRIEND_DEPTH equals 2 (for `V' and `W').
222 3. TEMPLATE_MEMBER_P is true (for `W').
224 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
226 int class_template_depth
= template_class_depth (type
);
227 int friend_depth
= processing_template_decl
- class_template_depth
;
229 if (! MAYBE_CLASS_TYPE_P (friend_type
)
230 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
232 /* N1791: If the type specifier in a friend declaration designates a
233 (possibly cv-qualified) class type, that class is declared as a
234 friend; otherwise, the friend declaration is ignored.
236 So don't complain in C++11 mode. */
237 if (cxx_dialect
< cxx11
)
238 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
239 "invalid type %qT declared %<friend%>", friend_type
);
243 friend_type
= cv_unqualified (friend_type
);
245 if (check_for_bare_parameter_packs (friend_type
))
249 /* If the TYPE is a template then it makes sense for it to be
250 friends with itself; this means that each instantiation is
251 friends with all other instantiations. */
253 if (CLASS_TYPE_P (friend_type
)
254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
255 && uses_template_parms (friend_type
))
258 Friend declarations shall not declare partial
260 error ("partial specialization %qT declared %<friend%>",
265 else if (same_type_p (type
, friend_type
))
268 warning (0, "class %qT is implicitly friends with itself",
275 A friend of a class or class template can be a function or
276 class template, a specialization of a function template or
277 class template, or an ordinary (nontemplate) function or
281 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
283 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
286 /* template <class U> friend class T::X<U>; */
288 Friend declarations shall not declare partial
290 error ("partial specialization %qT declared %<friend%>",
296 /* We will figure this out later. */
297 bool template_member_p
= false;
299 tree ctype
= TYPE_CONTEXT (friend_type
);
300 tree name
= TYPE_IDENTIFIER (friend_type
);
303 if (!uses_template_parms_level (ctype
, class_template_depth
305 template_member_p
= true;
307 if (class_template_depth
)
309 /* We rely on tsubst_friend_class to check the
310 validity of the declaration later. */
311 if (template_member_p
)
313 = make_unbound_class_template (ctype
,
315 current_template_parms
,
319 = make_typename_type (ctype
, name
, class_type
, tf_error
);
323 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
326 error ("%qT is not a member of %qT", name
, ctype
);
329 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
331 error ("%qT is not a member class template of %qT",
333 inform (input_location
, "%q+D declared here", decl
);
336 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
337 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
339 error ("%qT is not a nested class of %qT",
341 inform (input_location
, "%q+D declared here", decl
);
345 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
349 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
351 /* template <class T> friend class T; */
352 error ("template parameter type %qT declared %<friend%>", friend_type
);
355 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
356 friend_type
= TYPE_NAME (friend_type
);
357 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
359 /* template <class T> friend class A; where A is not a template */
360 error ("%q#T is not a template", friend_type
);
364 /* template <class T> friend class A; where A is a template */
365 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
367 if (friend_type
== error_mark_node
)
370 /* See if it is already a friend. */
371 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
373 classes
= TREE_CHAIN (classes
))
375 tree probe
= TREE_VALUE (classes
);
377 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
379 if (friend_type
== probe
)
382 warning (OPT_Wredundant_decls
,
383 "%qD is already a friend of %qT", probe
, type
);
387 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
389 if (same_type_p (probe
, friend_type
))
392 warning (OPT_Wredundant_decls
,
393 "%qT is already a friend of %qT", probe
, type
);
401 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
403 CLASSTYPE_FRIEND_CLASSES (type
)
404 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
405 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
406 friend_type
= TREE_TYPE (friend_type
);
407 if (!uses_template_parms (type
))
408 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
409 = tree_cons (NULL_TREE
, type
,
410 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
414 /* Record DECL (a FUNCTION_DECL) as a friend of the
415 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
416 class of which it is a member, as named in the friend declaration.
417 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
418 friend declaration is a definition of the function. FLAGS is as
422 do_friend (tree ctype
, tree declarator
, tree decl
,
423 tree attrlist
, enum overload_flags flags
,
426 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
427 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
429 /* Every decl that gets here is a friend of something. */
430 DECL_FRIEND_P (decl
) = 1;
432 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
433 error ("friend declaration %qD may not have virt-specifiers",
436 /* Unfortunately, we have to handle attributes here. Normally we would
437 handle them in start_decl_1, but since this is a friend decl start_decl_1
438 never gets to see it. */
440 /* Set attributes here so if duplicate decl, will have proper attributes. */
441 cplus_decl_attributes (&decl
, attrlist
, 0);
443 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
445 declarator
= TREE_OPERAND (declarator
, 0);
446 if (is_overloaded_fn (declarator
))
447 declarator
= DECL_NAME (get_first_fn (declarator
));
452 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
453 the enclosing class. FRIEND_DEPTH counts the number of template
454 headers used for this friend declaration. TEMPLATE_MEMBER_P is
455 true if a template header in FRIEND_DEPTH is intended for
456 DECLARATOR. For example, the code
458 template <class T> struct A {
459 template <class U> struct B {
460 template <class V> template <class W>
461 friend void C<V>::f(W);
465 will eventually give the following results
467 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
468 2. FRIEND_DEPTH equals 2 (for `V' and `W').
469 3. TEMPLATE_MEMBER_P is true (for `W'). */
471 int class_template_depth
= template_class_depth (current_class_type
);
472 int friend_depth
= processing_template_decl
- class_template_depth
;
473 /* We will figure this out later. */
474 bool template_member_p
= false;
476 tree cname
= TYPE_NAME (ctype
);
477 if (TREE_CODE (cname
) == TYPE_DECL
)
478 cname
= DECL_NAME (cname
);
480 /* A method friend. */
481 if (flags
== NO_SPECIAL
&& declarator
== cname
)
482 DECL_CONSTRUCTOR_P (decl
) = 1;
484 grokclassfn (ctype
, decl
, flags
);
488 if (!uses_template_parms_level (ctype
, class_template_depth
490 template_member_p
= true;
493 /* A nested class may declare a member of an enclosing class
494 to be a friend, so we do lookup here even if CTYPE is in
495 the process of being defined. */
496 if (class_template_depth
497 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
499 if (DECL_TEMPLATE_INFO (decl
))
500 /* DECL is a template specialization. No need to
501 build a new TEMPLATE_DECL. */
503 else if (class_template_depth
)
504 /* We rely on tsubst_friend_function to check the
505 validity of the declaration later. */
506 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
508 decl
= check_classfn (ctype
, decl
,
510 ? current_template_parms
513 if ((template_member_p
514 /* Always pull out the TEMPLATE_DECL if we have a friend
515 template in a class template so that it gets tsubsted
516 properly later on (59956). tsubst_friend_function knows
517 how to tell this apart from a member template. */
518 || (class_template_depth
&& friend_depth
))
519 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
520 decl
= DECL_TI_TEMPLATE (decl
);
523 add_friend (current_class_type
, decl
, /*complain=*/true);
526 error ("member %qD declared as friend before type %qT defined",
530 @@ or possibly a friend from a base class ?!? */
531 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
533 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
535 /* Friends must all go through the overload machinery,
536 even though they may not technically be overloaded.
538 Note that because classes all wind up being top-level
539 in their scope, their friend wind up in top-level scope as well. */
541 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
543 if (! DECL_USE_TEMPLATE (decl
))
545 /* We must check whether the decl refers to template
546 arguments before push_template_decl_real adds a
547 reference to the containing template class. */
548 int warn
= (warn_nontemplate_friend
549 && ! funcdef_flag
&& ! is_friend_template
550 && current_template_parms
551 && uses_template_parms (decl
));
553 if (is_friend_template
554 || template_class_depth (current_class_type
) != 0)
555 /* We can't call pushdecl for a template class, since in
556 general, such a declaration depends on template
557 parameters. Instead, we call pushdecl when the class
559 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
560 else if (current_function_decl
)
562 /* This must be a local class. 11.5p11:
564 If a friend declaration appears in a local class (9.8) and
565 the name specified is an unqualified name, a prior
566 declaration is looked up without considering scopes that
567 are outside the innermost enclosing non-class scope. For a
568 friend function declaration, if there is no prior
569 declaration, the program is ill-formed. */
570 tree t
= lookup_name_innermost_nonclass_level (DECL_NAME (decl
));
572 decl
= pushdecl_maybe_friend (decl
, /*is_friend=*/true);
575 error ("friend declaration %qD in local class without "
576 "prior declaration", decl
);
577 return error_mark_node
;
582 /* We can't use pushdecl, as we might be in a template
583 class specialization, and pushdecl will insert an
584 unqualified friend decl into the template parameter
585 scope, rather than the namespace containing it. */
586 tree ns
= decl_namespace_context (decl
);
588 push_nested_namespace (ns
);
589 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
590 pop_nested_namespace (ns
);
595 static int explained
;
598 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
599 "%q#D declares a non-template function", decl
);
600 if (! explained
&& warned
)
602 inform (input_location
, "(if this is not what you intended, make sure "
603 "the function template has already been declared "
604 "and add <> after the function name here) ");
610 if (decl
== error_mark_node
)
611 return error_mark_node
;
613 add_friend (current_class_type
,
614 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
616 DECL_FRIEND_P (decl
) = 1;