]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/friend.c
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"
34 /* Friend data structures are described in cp-tree.h. */
36 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
39 is_friend (tree type
, tree supplicant
)
45 if (supplicant
== NULL_TREE
|| type
== NULL_TREE
)
48 declp
= DECL_P (supplicant
);
51 /* It's a function decl. */
53 tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
54 tree name
= DECL_NAME (supplicant
);
56 for (; list
; list
= TREE_CHAIN (list
))
58 if (name
== FRIEND_NAME (list
))
60 tree friends
= FRIEND_DECLS (list
);
61 for (; friends
; friends
= TREE_CHAIN (friends
))
63 tree this_friend
= TREE_VALUE (friends
);
65 if (this_friend
== NULL_TREE
)
68 if (supplicant
== this_friend
)
71 if (is_specialization_of_friend (supplicant
, this_friend
))
81 if (same_type_p (supplicant
, type
))
84 list
= CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type
)));
85 for (; list
; list
= TREE_CHAIN (list
))
87 tree t
= TREE_VALUE (list
);
89 if (TREE_CODE (t
) == TEMPLATE_DECL
?
90 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant
), t
) :
91 same_type_p (supplicant
, t
))
98 if (DECL_FUNCTION_MEMBER_P (supplicant
))
99 context
= DECL_CONTEXT (supplicant
);
105 if (TYPE_CLASS_SCOPE_P (supplicant
))
106 /* Nested classes get the same access as their enclosing types, as
107 per DR 45 (this is a change from the standard). */
108 context
= TYPE_CONTEXT (supplicant
);
110 /* Local classes have the same access as the enclosing function. */
111 context
= decl_function_context (TYPE_MAIN_DECL (supplicant
));
114 /* A namespace is not friend to anybody. */
115 if (context
&& TREE_CODE (context
) == NAMESPACE_DECL
)
119 return is_friend (type
, context
);
124 /* Add a new friend to the friends of the aggregate type TYPE.
125 DECL is the FUNCTION_DECL of the friend being added.
127 If COMPLAIN is true, warning about duplicate friend is issued.
128 We want to have this diagnostics during parsing but not
129 when a template is being instantiated. */
132 add_friend (tree type
, tree decl
, bool complain
)
139 if (decl
== error_mark_node
)
142 typedecl
= TYPE_MAIN_DECL (type
);
143 list
= DECL_FRIENDLIST (typedecl
);
144 name
= DECL_NAME (decl
);
145 type
= TREE_TYPE (typedecl
);
149 if (name
== FRIEND_NAME (list
))
151 tree friends
= FRIEND_DECLS (list
);
152 for (; friends
; friends
= TREE_CHAIN (friends
))
154 if (decl
== TREE_VALUE (friends
))
157 warning (OPT_Wredundant_decls
,
158 "%qD is already a friend of class %qT",
164 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
166 TREE_VALUE (list
) = tree_cons (NULL_TREE
, decl
,
170 list
= TREE_CHAIN (list
);
173 ctx
= DECL_CONTEXT (decl
);
174 if (ctx
&& CLASS_TYPE_P (ctx
) && !uses_template_parms (ctx
))
175 perform_or_defer_access_check (TYPE_BINFO (ctx
), decl
, decl
,
176 tf_warning_or_error
);
178 maybe_add_class_template_decl_list (type
, decl
, /*friend_p=*/1);
180 DECL_FRIENDLIST (typedecl
)
181 = tree_cons (DECL_NAME (decl
), build_tree_list (NULL_TREE
, decl
),
182 DECL_FRIENDLIST (typedecl
));
183 if (!uses_template_parms (type
))
184 DECL_BEFRIENDING_CLASSES (decl
)
185 = tree_cons (NULL_TREE
, type
,
186 DECL_BEFRIENDING_CLASSES (decl
));
189 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
190 been defined, we make all of its member functions friends of
191 TYPE. If not, we make it a pending friend, which can later be added
192 when its definition is seen. If a type is defined, then its TYPE_DECL's
193 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
194 classes that are not defined. If a type has not yet been defined,
195 then the DECL_WAITING_FRIENDS contains a list of types
196 waiting to make it their friend. Note that these two can both
197 be in use at the same time!
199 If COMPLAIN is true, warning about duplicate friend is issued.
200 We want to have this diagnostics during parsing but not
201 when a template is being instantiated. */
204 make_friend_class (tree type
, tree friend_type
, bool complain
)
208 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
209 the enclosing class. FRIEND_DEPTH counts the number of template
210 headers used for this friend declaration. TEMPLATE_MEMBER_P,
211 defined inside the `if' block for TYPENAME_TYPE case, is true if
212 a template header in FRIEND_DEPTH is intended for DECLARATOR.
213 For example, the code
215 template <class T> struct A {
216 template <class U> struct B {
217 template <class V> template <class W>
218 friend class C<V>::D;
222 will eventually give the following results
224 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
225 2. FRIEND_DEPTH equals 2 (for `V' and `W').
226 3. TEMPLATE_MEMBER_P is true (for `W').
228 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
230 int class_template_depth
= template_class_depth (type
);
231 int friend_depth
= processing_template_decl
- class_template_depth
;
233 if (! MAYBE_CLASS_TYPE_P (friend_type
)
234 && TREE_CODE (friend_type
) != TEMPLATE_TEMPLATE_PARM
)
236 /* N1791: If the type specifier in a friend declaration designates a
237 (possibly cv-qualified) class type, that class is declared as a
238 friend; otherwise, the friend declaration is ignored.
240 So don't complain in C++11 mode. */
241 if (cxx_dialect
< cxx11
)
242 pedwarn (input_location
, complain
? 0 : OPT_Wpedantic
,
243 "invalid type %qT declared %<friend%>", friend_type
);
247 friend_type
= cv_unqualified (friend_type
);
249 if (check_for_bare_parameter_packs (friend_type
))
253 /* If the TYPE is a template then it makes sense for it to be
254 friends with itself; this means that each instantiation is
255 friends with all other instantiations. */
257 if (CLASS_TYPE_P (friend_type
)
258 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type
)
259 && uses_template_parms (friend_type
))
262 Friend declarations shall not declare partial
264 error ("partial specialization %qT declared %<friend%>",
269 else if (same_type_p (type
, friend_type
))
272 warning (0, "class %qT is implicitly friends with itself",
279 A friend of a class or class template can be a function or
280 class template, a specialization of a function template or
281 class template, or an ordinary (nontemplate) function or
285 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
287 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type
))
290 /* template <class U> friend class T::X<U>; */
292 Friend declarations shall not declare partial
294 error ("partial specialization %qT declared %<friend%>",
300 /* We will figure this out later. */
301 bool template_member_p
= false;
303 tree ctype
= TYPE_CONTEXT (friend_type
);
304 tree name
= TYPE_IDENTIFIER (friend_type
);
307 if (!uses_template_parms_level (ctype
, class_template_depth
309 template_member_p
= true;
311 if (class_template_depth
)
313 /* We rely on tsubst_friend_class to check the
314 validity of the declaration later. */
315 if (template_member_p
)
317 = make_unbound_class_template (ctype
,
319 current_template_parms
,
323 = make_typename_type (ctype
, name
, class_type
, tf_error
);
327 decl
= lookup_member (ctype
, name
, 0, true, tf_warning_or_error
);
330 error ("%qT is not a member of %qT", name
, ctype
);
333 if (template_member_p
&& !DECL_CLASS_TEMPLATE_P (decl
))
335 error ("%qT is not a member class template of %qT",
337 inform (input_location
, "%q+D declared here", decl
);
340 if (!template_member_p
&& (TREE_CODE (decl
) != TYPE_DECL
341 || !CLASS_TYPE_P (TREE_TYPE (decl
))))
343 error ("%qT is not a nested class of %qT",
345 inform (input_location
, "%q+D declared here", decl
);
349 friend_type
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
353 else if (TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
355 /* template <class T> friend class T; */
356 error ("template parameter type %qT declared %<friend%>", friend_type
);
359 else if (TREE_CODE (friend_type
) == TEMPLATE_TEMPLATE_PARM
)
360 friend_type
= TYPE_NAME (friend_type
);
361 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type
))
363 /* template <class T> friend class A; where A is not a template */
364 error ("%q#T is not a template", friend_type
);
368 /* template <class T> friend class A; where A is a template */
369 friend_type
= CLASSTYPE_TI_TEMPLATE (friend_type
);
371 if (friend_type
== error_mark_node
)
374 /* See if it is already a friend. */
375 for (classes
= CLASSTYPE_FRIEND_CLASSES (type
);
377 classes
= TREE_CHAIN (classes
))
379 tree probe
= TREE_VALUE (classes
);
381 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
383 if (friend_type
== probe
)
386 warning (OPT_Wredundant_decls
,
387 "%qD is already a friend of %qT", probe
, type
);
391 else if (TREE_CODE (probe
) != TEMPLATE_DECL
)
393 if (same_type_p (probe
, friend_type
))
396 warning (OPT_Wredundant_decls
,
397 "%qT is already a friend of %qT", probe
, type
);
405 maybe_add_class_template_decl_list (type
, friend_type
, /*friend_p=*/1);
407 CLASSTYPE_FRIEND_CLASSES (type
)
408 = tree_cons (NULL_TREE
, friend_type
, CLASSTYPE_FRIEND_CLASSES (type
));
409 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
410 friend_type
= TREE_TYPE (friend_type
);
411 if (!uses_template_parms (type
))
412 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
)
413 = tree_cons (NULL_TREE
, type
,
414 CLASSTYPE_BEFRIENDING_CLASSES (friend_type
));
418 /* Record DECL (a FUNCTION_DECL) as a friend of the
419 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
420 class of which it is a member, as named in the friend declaration.
421 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
422 friend declaration is a definition of the function. FLAGS is as
426 do_friend (tree ctype
, tree declarator
, tree decl
,
427 tree attrlist
, enum overload_flags flags
,
430 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
431 gcc_assert (!ctype
|| MAYBE_CLASS_TYPE_P (ctype
));
433 /* Every decl that gets here is a friend of something. */
434 DECL_FRIEND_P (decl
) = 1;
436 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
437 error ("friend declaration %qD may not have virt-specifiers",
440 /* Unfortunately, we have to handle attributes here. Normally we would
441 handle them in start_decl_1, but since this is a friend decl start_decl_1
442 never gets to see it. */
444 /* Set attributes here so if duplicate decl, will have proper attributes. */
445 cplus_decl_attributes (&decl
, attrlist
, 0);
447 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
449 declarator
= TREE_OPERAND (declarator
, 0);
450 if (is_overloaded_fn (declarator
))
451 declarator
= DECL_NAME (get_first_fn (declarator
));
456 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
457 the enclosing class. FRIEND_DEPTH counts the number of template
458 headers used for this friend declaration. TEMPLATE_MEMBER_P is
459 true if a template header in FRIEND_DEPTH is intended for
460 DECLARATOR. For example, the code
462 template <class T> struct A {
463 template <class U> struct B {
464 template <class V> template <class W>
465 friend void C<V>::f(W);
469 will eventually give the following results
471 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
472 2. FRIEND_DEPTH equals 2 (for `V' and `W').
473 3. TEMPLATE_MEMBER_P is true (for `W'). */
475 int class_template_depth
= template_class_depth (current_class_type
);
476 int friend_depth
= processing_template_decl
- class_template_depth
;
477 /* We will figure this out later. */
478 bool template_member_p
= false;
480 tree cname
= TYPE_NAME (ctype
);
481 if (TREE_CODE (cname
) == TYPE_DECL
)
482 cname
= DECL_NAME (cname
);
484 /* A method friend. */
485 if (flags
== NO_SPECIAL
&& declarator
== cname
)
486 DECL_CONSTRUCTOR_P (decl
) = 1;
488 grokclassfn (ctype
, decl
, flags
);
492 if (!uses_template_parms_level (ctype
, class_template_depth
494 template_member_p
= true;
497 /* A nested class may declare a member of an enclosing class
498 to be a friend, so we do lookup here even if CTYPE is in
499 the process of being defined. */
500 if (class_template_depth
501 || COMPLETE_OR_OPEN_TYPE_P (ctype
))
503 if (DECL_TEMPLATE_INFO (decl
))
504 /* DECL is a template specialization. No need to
505 build a new TEMPLATE_DECL. */
507 else if (class_template_depth
)
508 /* We rely on tsubst_friend_function to check the
509 validity of the declaration later. */
510 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
512 decl
= check_classfn (ctype
, decl
,
514 ? current_template_parms
517 if ((template_member_p
518 /* Always pull out the TEMPLATE_DECL if we have a friend
519 template in a class template so that it gets tsubsted
520 properly later on (59956). tsubst_friend_function knows
521 how to tell this apart from a member template. */
522 || (class_template_depth
&& friend_depth
))
523 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
524 decl
= DECL_TI_TEMPLATE (decl
);
527 add_friend (current_class_type
, decl
, /*complain=*/true);
530 error ("member %qD declared as friend before type %qT defined",
534 @@ or possibly a friend from a base class ?!? */
535 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
537 int is_friend_template
= PROCESSING_REAL_TEMPLATE_DECL_P ();
539 /* Friends must all go through the overload machinery,
540 even though they may not technically be overloaded.
542 Note that because classes all wind up being top-level
543 in their scope, their friend wind up in top-level scope as well. */
545 SET_DECL_FRIEND_CONTEXT (decl
, current_class_type
);
547 if (! DECL_USE_TEMPLATE (decl
))
549 /* We must check whether the decl refers to template
550 arguments before push_template_decl_real adds a
551 reference to the containing template class. */
552 int warn
= (warn_nontemplate_friend
553 && ! funcdef_flag
&& ! is_friend_template
554 && current_template_parms
555 && uses_template_parms (decl
));
557 if (is_friend_template
558 || template_class_depth (current_class_type
) != 0)
559 /* We can't call pushdecl for a template class, since in
560 general, such a declaration depends on template
561 parameters. Instead, we call pushdecl when the class
563 decl
= push_template_decl_real (decl
, /*is_friend=*/true);
564 else if (current_function_decl
)
566 /* This must be a local class. 11.5p11:
568 If a friend declaration appears in a local class (9.8) and
569 the name specified is an unqualified name, a prior
570 declaration is looked up without considering scopes that
571 are outside the innermost enclosing non-class scope. For a
572 friend function declaration, if there is no prior
573 declaration, the program is ill-formed. */
574 tree t
= lookup_name_innermost_nonclass_level (DECL_NAME (decl
));
576 decl
= pushdecl_maybe_friend (decl
, /*is_friend=*/true);
579 error ("friend declaration %qD in local class without "
580 "prior declaration", decl
);
581 return error_mark_node
;
586 /* We can't use pushdecl, as we might be in a template
587 class specialization, and pushdecl will insert an
588 unqualified friend decl into the template parameter
589 scope, rather than the namespace containing it. */
590 tree ns
= decl_namespace_context (decl
);
592 push_nested_namespace (ns
);
593 decl
= pushdecl_namespace_level (decl
, /*is_friend=*/true);
594 pop_nested_namespace (ns
);
599 static int explained
;
602 warned
= warning (OPT_Wnon_template_friend
, "friend declaration "
603 "%q#D declares a non-template function", decl
);
604 if (! explained
&& warned
)
606 inform (input_location
, "(if this is not what you intended, make sure "
607 "the function template has already been declared "
608 "and add <> after the function name here) ");
614 if (decl
== error_mark_node
)
615 return error_mark_node
;
617 add_friend (current_class_type
,
618 is_friend_template
? DECL_TI_TEMPLATE (decl
) : decl
,
620 DECL_FRIEND_P (decl
) = 1;