1 /* Help friends in C++.
2 Copyright (C) 1997-2014 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"
28 /* Friend data structures are described in cp-tree.h. */
30 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
33 is_friend (tree type
, tree supplicant
)
39 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
42 declp
= DECL_P (supplicant
);
45 /* It's a function decl. */
47 tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
48 tree name
= DECL_NAME (supplicant
);
50 for (; list
; list
= TREE_CHAIN (list
))
52 if (name
== FRIEND_NAME (list
))
54 tree friends
= FRIEND_DECLS (list
);
55 for (; friends
; friends
= TREE_CHAIN (friends
))
57 tree this_friend
= TREE_VALUE (friends
);
59 if (this_friend
== NULL_TREE
)
62 if (supplicant
== this_friend
)
65 if (is_specialization_of_friend (supplicant
, this_friend
))
75 if (same_type_p (supplicant
, type
))
78 list
= CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type
)));
79 for (; list
; list
= TREE_CHAIN (list
))
81 tree t
= TREE_VALUE (list
);
83 if (TREE_CODE (t
) == TEMPLATE_DECL
?
84 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant
), t
) :
85 same_type_p (supplicant
, t
))
92 if (DECL_FUNCTION_MEMBER_P (supplicant
))
93 context
= DECL_CONTEXT (supplicant
);
99 if (TYPE_CLASS_SCOPE_P (supplicant
))
100 /* Nested classes get the same access as their enclosing types, as
101 per DR 45 (this is a change from the standard). */
102 context
= TYPE_CONTEXT (supplicant
);
104 /* Local classes have the same access as the enclosing function. */
105 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
108 /* A namespace is not friend to anybody. */
109 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
113 return is_friend (type
, context
);
118 /* Add a new friend to the friends of the aggregate type TYPE.
119 DECL is the FUNCTION_DECL of the friend being added.
121 If COMPLAIN is true, warning about duplicate friend is issued.
122 We want to have this diagnostics during parsing but not
123 when a template is being instantiated. */
126 add_friend (tree type
, tree decl
, bool complain
)
133 if (decl
== error_mark_node
)
136 typedecl
= TYPE_MAIN_DECL (type
);
137 list
= DECL_FRIENDLIST (typedecl
);
138 name
= DECL_NAME (decl
);
139 type
= TREE_TYPE (typedecl
);
143 if (name
== FRIEND_NAME (list
))
145 tree friends
= FRIEND_DECLS (list
);
146 for (; friends
; friends
= TREE_CHAIN (friends
))
148 if (decl
== TREE_VALUE (friends
))
151 warning (OPT_Wredundant_decls
,
152 "%qD is already a friend of class %qT",
158 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
160 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
164 list
= TREE_CHAIN (list
);
167 ctx
= DECL_CONTEXT (decl
);
168 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
169 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
170 tf_warning_or_error
);
172 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
174 DECL_FRIENDLIST (typedecl
)
175 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
176 DECL_FRIENDLIST (typedecl
));
177 if (!uses_template_parms (type
))
178 DECL_BEFRIENDING_CLASSES (decl
)
179 = tree_cons (NULL_TREE
, type
,
180 DECL_BEFRIENDING_CLASSES (decl
));
183 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
184 been defined, we make all of its member functions friends of
185 TYPE. If not, we make it a pending friend, which can later be added
186 when its definition is seen. If a type is defined, then its TYPE_DECL's
187 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
188 classes that are not defined. If a type has not yet been defined,
189 then the DECL_WAITING_FRIENDS contains a list of types
190 waiting to make it their friend. Note that these two can both
191 be in use at the same time!
193 If COMPLAIN is true, warning about duplicate friend is issued.
194 We want to have this diagnostics during parsing but not
195 when a template is being instantiated. */
198 make_friend_class (tree type
, tree friend_type
, bool complain
)
202 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
203 the enclosing class. FRIEND_DEPTH counts the number of template
204 headers used for this friend declaration. TEMPLATE_MEMBER_P,
205 defined inside the `if' block for TYPENAME_TYPE case, is true if
206 a template header in FRIEND_DEPTH is intended for DECLARATOR.
207 For example, the code
209 template <class T> struct A {
210 template <class U> struct B {
211 template <class V> template <class W>
212 friend class C<V>::D;
216 will eventually give the following results
218 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
219 2. FRIEND_DEPTH equals 2 (for `V' and `W').
220 3. TEMPLATE_MEMBER_P is true (for `W').
222 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
224 int class_template_depth
= template_class_depth (type
);
225 int friend_depth
= processing_template_decl
- class_template_depth
;
227 if (! MAYBE_CLASS_TYPE_P (friend_type
)
228 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
230 /* N1791: If the type specifier in a friend declaration designates a
231 (possibly cv-qualified) class type, that class is declared as a
232 friend; otherwise, the friend declaration is ignored.
234 So don't complain in C++11 mode. */
235 if (cxx_dialect
< cxx11
)
236 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
237 "invalid type %qT declared %<friend%>", friend_type
);
241 friend_type
= cv_unqualified (friend_type
);
243 if (check_for_bare_parameter_packs (friend_type
))
247 /* If the TYPE is a template then it makes sense for it to be
248 friends with itself; this means that each instantiation is
249 friends with all other instantiations. */
251 if (CLASS_TYPE_P (friend_type
)
252 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
253 && uses_template_parms (friend_type
))
256 Friend declarations shall not declare partial
258 error ("partial specialization %qT declared %<friend%>",
263 else if (same_type_p (type
, friend_type
))
266 warning (0, "class %qT is implicitly friends with itself",
273 A friend of a class or class template can be a function or
274 class template, a specialization of a function template or
275 class template, or an ordinary (nontemplate) function or
279 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
281 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
284 /* template <class U> friend class T::X<U>; */
286 Friend declarations shall not declare partial
288 error ("partial specialization %qT declared %<friend%>",
294 /* We will figure this out later. */
295 bool template_member_p
= false;
297 tree ctype
= TYPE_CONTEXT (friend_type
);
298 tree name
= TYPE_IDENTIFIER (friend_type
);
301 if (!uses_template_parms_level (ctype
, class_template_depth
303 template_member_p
= true;
305 if (class_template_depth
)
307 /* We rely on tsubst_friend_class to check the
308 validity of the declaration later. */
309 if (template_member_p
)
311 = make_unbound_class_template (ctype
,
313 current_template_parms
,
317 = make_typename_type (ctype
, name
, class_type
, tf_error
);
321 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
324 error ("%qT is not a member of %qT", name
, ctype
);
327 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
329 error ("%qT is not a member class template of %qT",
331 inform (input_location
, "%q+D declared here", decl
);
334 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
335 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
337 error ("%qT is not a nested class of %qT",
339 inform (input_location
, "%q+D declared here", decl
);
343 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
347 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
349 /* template <class T> friend class T; */
350 error ("template parameter type %qT declared %<friend%>", friend_type
);
353 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
354 friend_type
= TYPE_NAME (friend_type
);
355 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
357 /* template <class T> friend class A; where A is not a template */
358 error ("%q#T is not a template", friend_type
);
362 /* template <class T> friend class A; where A is a template */
363 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
365 if (friend_type
== error_mark_node
)
368 /* See if it is already a friend. */
369 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
371 classes
= TREE_CHAIN (classes
))
373 tree probe
= TREE_VALUE (classes
);
375 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
377 if (friend_type
== probe
)
380 warning (OPT_Wredundant_decls
,
381 "%qD is already a friend of %qT", probe
, type
);
385 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
387 if (same_type_p (probe
, friend_type
))
390 warning (OPT_Wredundant_decls
,
391 "%qT is already a friend of %qT", probe
, type
);
399 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
401 CLASSTYPE_FRIEND_CLASSES (type
)
402 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
403 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
404 friend_type
= TREE_TYPE (friend_type
);
405 if (!uses_template_parms (type
))
406 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
407 = tree_cons (NULL_TREE
, type
,
408 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
412 /* Record DECL (a FUNCTION_DECL) as a friend of the
413 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
414 class of which it is a member, as named in the friend declaration.
415 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
416 friend declaration is a definition of the function. FLAGS is as
420 do_friend (tree ctype
, tree declarator
, tree decl
,
421 tree attrlist
, enum overload_flags flags
,
424 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
425 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
427 /* Every decl that gets here is a friend of something. */
428 DECL_FRIEND_P (decl
) = 1;
430 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
431 error ("friend declaration %qD may not have virt-specifiers",
434 /* Unfortunately, we have to handle attributes here. Normally we would
435 handle them in start_decl_1, but since this is a friend decl start_decl_1
436 never gets to see it. */
438 /* Set attributes here so if duplicate decl, will have proper attributes. */
439 cplus_decl_attributes (&decl
, attrlist
, 0);
441 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
443 declarator
= TREE_OPERAND (declarator
, 0);
444 if (is_overloaded_fn (declarator
))
445 declarator
= DECL_NAME (get_first_fn (declarator
));
450 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
451 the enclosing class. FRIEND_DEPTH counts the number of template
452 headers used for this friend declaration. TEMPLATE_MEMBER_P is
453 true if a template header in FRIEND_DEPTH is intended for
454 DECLARATOR. For example, the code
456 template <class T> struct A {
457 template <class U> struct B {
458 template <class V> template <class W>
459 friend void C<V>::f(W);
463 will eventually give the following results
465 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
466 2. FRIEND_DEPTH equals 2 (for `V' and `W').
467 3. TEMPLATE_MEMBER_P is true (for `W'). */
469 int class_template_depth
= template_class_depth (current_class_type
);
470 int friend_depth
= processing_template_decl
- class_template_depth
;
471 /* We will figure this out later. */
472 bool template_member_p
= false;
474 tree cname
= TYPE_NAME (ctype
);
475 if (TREE_CODE (cname
) == TYPE_DECL
)
476 cname
= DECL_NAME (cname
);
478 /* A method friend. */
479 if (flags
== NO_SPECIAL
&& declarator
== cname
)
480 DECL_CONSTRUCTOR_P (decl
) = 1;
482 grokclassfn (ctype
, decl
, flags
);
486 if (!uses_template_parms_level (ctype
, class_template_depth
488 template_member_p
= true;
491 /* A nested class may declare a member of an enclosing class
492 to be a friend, so we do lookup here even if CTYPE is in
493 the process of being defined. */
494 if (class_template_depth
495 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
497 if (DECL_TEMPLATE_INFO (decl
))
498 /* DECL is a template specialization. No need to
499 build a new TEMPLATE_DECL. */
501 else if (class_template_depth
)
502 /* We rely on tsubst_friend_function to check the
503 validity of the declaration later. */
504 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
506 decl
= check_classfn (ctype
, decl
,
508 ? current_template_parms
511 if ((template_member_p
512 /* Always pull out the TEMPLATE_DECL if we have a friend
513 template in a class template so that it gets tsubsted
514 properly later on (59956). tsubst_friend_function knows
515 how to tell this apart from a member template. */
516 || (class_template_depth
&& friend_depth
))
517 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
518 decl
= DECL_TI_TEMPLATE (decl
);
521 add_friend (current_class_type
, decl
, /*complain=*/true);
524 error ("member %qD declared as friend before type %qT defined",
528 @@ or possibly a friend from a base class ?!? */
529 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
531 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
533 /* Friends must all go through the overload machinery,
534 even though they may not technically be overloaded.
536 Note that because classes all wind up being top-level
537 in their scope, their friend wind up in top-level scope as well. */
539 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
541 if (! DECL_USE_TEMPLATE (decl
))
543 /* We must check whether the decl refers to template
544 arguments before push_template_decl_real adds a
545 reference to the containing template class. */
546 int warn
= (warn_nontemplate_friend
547 && ! funcdef_flag
&& ! is_friend_template
548 && current_template_parms
549 && uses_template_parms (decl
));
551 if (is_friend_template
552 || template_class_depth (current_class_type
) != 0)
553 /* We can't call pushdecl for a template class, since in
554 general, such a declaration depends on template
555 parameters. Instead, we call pushdecl when the class
557 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
558 else if (current_function_decl
)
560 /* This must be a local class. 11.5p11:
562 If a friend declaration appears in a local class (9.8) and
563 the name specified is an unqualified name, a prior
564 declaration is looked up without considering scopes that
565 are outside the innermost enclosing non-class scope. For a
566 friend function declaration, if there is no prior
567 declaration, the program is ill-formed. */
568 tree t
= lookup_name_innermost_nonclass_level (DECL_NAME (decl
));
570 decl
= pushdecl_maybe_friend (decl
, /*is_friend=*/true);
573 error ("friend declaration %qD in local class without "
574 "prior declaration", decl
);
575 return error_mark_node
;
580 /* We can't use pushdecl, as we might be in a template
581 class specialization, and pushdecl will insert an
582 unqualified friend decl into the template parameter
583 scope, rather than the namespace containing it. */
584 tree ns
= decl_namespace_context (decl
);
586 push_nested_namespace (ns
);
587 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
588 pop_nested_namespace (ns
);
593 static int explained
;
596 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
597 "%q#D declares a non-template function", decl
);
598 if (! explained
&& warned
)
600 inform (input_location
, "(if this is not what you intended, make sure "
601 "the function template has already been declared "
602 "and add <> after the function name here) ");
608 if (decl
== error_mark_node
)
609 return error_mark_node
;
611 add_friend (current_class_type
,
612 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
614 DECL_FRIEND_P (decl
) = 1;