decl.c (grokdeclarator): Fix typo in pedwarn text.
[official-gcc.git] / gcc / cp / friend.c
blob5e4b2d18bf74dfa8dd25da8457a168d2e3c2f8f5
1 /* Help friends in C++.
2 Copyright (C) 1997-2016 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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "cp-tree.h"
25 /* Friend data structures are described in cp-tree.h. */
27 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
29 int
30 is_friend (tree type, tree supplicant)
32 int declp;
33 tree list;
34 tree context;
36 if (supplicant == NULL_TREE || type == NULL_TREE)
37 return 0;
39 declp = DECL_P (supplicant);
41 if (declp)
42 /* It's a function decl. */
44 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
45 tree name = DECL_NAME (supplicant);
47 for (; list ; list = TREE_CHAIN (list))
49 if (name == FRIEND_NAME (list))
51 tree friends = FRIEND_DECLS (list);
52 for (; friends ; friends = TREE_CHAIN (friends))
54 tree this_friend = TREE_VALUE (friends);
56 if (this_friend == NULL_TREE)
57 continue;
59 if (supplicant == this_friend)
60 return 1;
62 if (is_specialization_of_friend (supplicant, this_friend))
63 return 1;
65 break;
69 else
70 /* It's a type. */
72 if (same_type_p (supplicant, type))
73 return 1;
75 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
76 for (; list ; list = TREE_CHAIN (list))
78 tree t = TREE_VALUE (list);
80 if (TREE_CODE (t) == TEMPLATE_DECL ?
81 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
82 same_type_p (supplicant, t))
83 return 1;
87 if (declp)
89 if (DECL_FUNCTION_MEMBER_P (supplicant))
90 context = DECL_CONTEXT (supplicant);
91 else
92 context = NULL_TREE;
94 else
96 if (TYPE_CLASS_SCOPE_P (supplicant))
97 /* Nested classes get the same access as their enclosing types, as
98 per DR 45 (this is a change from the standard). */
99 context = TYPE_CONTEXT (supplicant);
100 else
101 /* Local classes have the same access as the enclosing function. */
102 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
105 /* A namespace is not friend to anybody. */
106 if (context && TREE_CODE (context) == NAMESPACE_DECL)
107 context = NULL_TREE;
109 if (context)
110 return is_friend (type, context);
112 return 0;
115 /* Add a new friend to the friends of the aggregate type TYPE.
116 DECL is the FUNCTION_DECL of the friend being added.
118 If COMPLAIN is true, warning about duplicate friend is issued.
119 We want to have this diagnostics during parsing but not
120 when a template is being instantiated. */
122 void
123 add_friend (tree type, tree decl, bool complain)
125 tree typedecl;
126 tree list;
127 tree name;
128 tree ctx;
130 if (decl == error_mark_node)
131 return;
133 typedecl = TYPE_MAIN_DECL (type);
134 list = DECL_FRIENDLIST (typedecl);
135 name = DECL_NAME (decl);
136 type = TREE_TYPE (typedecl);
138 while (list)
140 if (name == FRIEND_NAME (list))
142 tree friends = FRIEND_DECLS (list);
143 for (; friends ; friends = TREE_CHAIN (friends))
145 if (decl == TREE_VALUE (friends))
147 if (complain)
148 warning (OPT_Wredundant_decls,
149 "%qD is already a friend of class %qT",
150 decl, type);
151 return;
155 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
156 TREE_VALUE (list));
157 break;
159 list = TREE_CHAIN (list);
162 ctx = DECL_CONTEXT (decl);
163 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
164 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
165 tf_warning_or_error);
167 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
169 if (!list)
170 DECL_FRIENDLIST (typedecl)
171 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
172 DECL_FRIENDLIST (typedecl));
173 if (!uses_template_parms (type))
174 DECL_BEFRIENDING_CLASSES (decl)
175 = tree_cons (NULL_TREE, type,
176 DECL_BEFRIENDING_CLASSES (decl));
179 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
180 been defined, we make all of its member functions friends of
181 TYPE. If not, we make it a pending friend, which can later be added
182 when its definition is seen. If a type is defined, then its TYPE_DECL's
183 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
184 classes that are not defined. If a type has not yet been defined,
185 then the DECL_WAITING_FRIENDS contains a list of types
186 waiting to make it their friend. Note that these two can both
187 be in use at the same time!
189 If COMPLAIN is true, warning about duplicate friend is issued.
190 We want to have this diagnostics during parsing but not
191 when a template is being instantiated. */
193 void
194 make_friend_class (tree type, tree friend_type, bool complain)
196 tree classes;
198 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
199 the enclosing class. FRIEND_DEPTH counts the number of template
200 headers used for this friend declaration. TEMPLATE_MEMBER_P,
201 defined inside the `if' block for TYPENAME_TYPE case, is true if
202 a template header in FRIEND_DEPTH is intended for DECLARATOR.
203 For example, the code
205 template <class T> struct A {
206 template <class U> struct B {
207 template <class V> template <class W>
208 friend class C<V>::D;
212 will eventually give the following results
214 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
215 2. FRIEND_DEPTH equals 2 (for `V' and `W').
216 3. TEMPLATE_MEMBER_P is true (for `W').
218 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
220 int class_template_depth = template_class_depth (type);
221 int friend_depth = processing_template_decl - class_template_depth;
223 if (! MAYBE_CLASS_TYPE_P (friend_type)
224 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
226 /* N1791: If the type specifier in a friend declaration designates a
227 (possibly cv-qualified) class type, that class is declared as a
228 friend; otherwise, the friend declaration is ignored.
230 So don't complain in C++11 mode. */
231 if (cxx_dialect < cxx11)
232 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
233 "invalid type %qT declared %<friend%>", friend_type);
234 return;
237 friend_type = cv_unqualified (friend_type);
239 if (check_for_bare_parameter_packs (friend_type))
240 return;
242 if (friend_depth)
243 /* If the TYPE is a template then it makes sense for it to be
244 friends with itself; this means that each instantiation is
245 friends with all other instantiations. */
247 if (CLASS_TYPE_P (friend_type)
248 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
249 && uses_template_parms (friend_type))
251 /* [temp.friend]
252 Friend declarations shall not declare partial
253 specializations. */
254 error ("partial specialization %qT declared %<friend%>",
255 friend_type);
256 return;
258 if (TYPE_TEMPLATE_INFO (friend_type)
259 && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type)))
261 error ("%qT is not a template", friend_type);
262 inform (location_of (friend_type), "previous declaration here");
263 if (TYPE_CLASS_SCOPE_P (friend_type)
264 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type))
265 && currently_open_class (TYPE_CONTEXT (friend_type)))
266 inform (input_location, "perhaps you need explicit template "
267 "arguments in your nested-name-specifier");
268 return;
271 else if (same_type_p (type, friend_type))
273 if (complain)
274 warning (0, "class %qT is implicitly friends with itself",
275 type);
276 return;
279 /* [temp.friend]
281 A friend of a class or class template can be a function or
282 class template, a specialization of a function template or
283 class template, or an ordinary (nontemplate) function or
284 class. */
285 if (!friend_depth)
286 ;/* ok */
287 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
289 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
290 == TEMPLATE_ID_EXPR)
292 /* template <class U> friend class T::X<U>; */
293 /* [temp.friend]
294 Friend declarations shall not declare partial
295 specializations. */
296 error ("partial specialization %qT declared %<friend%>",
297 friend_type);
298 return;
300 else
302 /* We will figure this out later. */
303 bool template_member_p = false;
305 tree ctype = TYPE_CONTEXT (friend_type);
306 tree name = TYPE_IDENTIFIER (friend_type);
307 tree decl;
309 if (!uses_template_parms_level (ctype, class_template_depth
310 + friend_depth))
311 template_member_p = true;
313 if (class_template_depth)
315 /* We rely on tsubst_friend_class to check the
316 validity of the declaration later. */
317 if (template_member_p)
318 friend_type
319 = make_unbound_class_template (ctype,
320 name,
321 current_template_parms,
322 tf_error);
323 else
324 friend_type
325 = make_typename_type (ctype, name, class_type, tf_error);
327 else
329 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
330 if (!decl)
332 error ("%qT is not a member of %qT", name, ctype);
333 return;
335 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
337 error ("%qT is not a member class template of %qT",
338 name, ctype);
339 inform (DECL_SOURCE_LOCATION (decl),
340 "%qD declared here", decl);
341 return;
343 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
344 || !CLASS_TYPE_P (TREE_TYPE (decl))))
346 error ("%qT is not a nested class of %qT",
347 name, ctype);
348 inform (DECL_SOURCE_LOCATION (decl),
349 "%qD declared here", decl);
350 return;
353 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
357 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
359 /* template <class T> friend class T; */
360 error ("template parameter type %qT declared %<friend%>", friend_type);
361 return;
363 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
364 friend_type = TYPE_NAME (friend_type);
365 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
367 /* template <class T> friend class A; where A is not a template */
368 error ("%q#T is not a template", friend_type);
369 return;
371 else
372 /* template <class T> friend class A; where A is a template */
373 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
375 if (friend_type == error_mark_node)
376 return;
378 /* See if it is already a friend. */
379 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
380 classes;
381 classes = TREE_CHAIN (classes))
383 tree probe = TREE_VALUE (classes);
385 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
387 if (friend_type == probe)
389 if (complain)
390 warning (OPT_Wredundant_decls,
391 "%qD is already a friend of %qT", probe, type);
392 break;
395 else if (TREE_CODE (probe) != TEMPLATE_DECL)
397 if (same_type_p (probe, friend_type))
399 if (complain)
400 warning (OPT_Wredundant_decls,
401 "%qT is already a friend of %qT", probe, type);
402 break;
407 if (!classes)
409 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
411 CLASSTYPE_FRIEND_CLASSES (type)
412 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
413 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
414 friend_type = TREE_TYPE (friend_type);
415 if (!uses_template_parms (type))
416 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
417 = tree_cons (NULL_TREE, type,
418 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
422 /* Record DECL (a FUNCTION_DECL) as a friend of the
423 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
424 class of which it is a member, as named in the friend declaration.
425 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
426 friend declaration is a definition of the function. FLAGS is as
427 for grokclass fn. */
429 tree
430 do_friend (tree ctype, tree declarator, tree decl,
431 tree attrlist, enum overload_flags flags,
432 bool funcdef_flag)
434 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
435 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
437 /* Every decl that gets here is a friend of something. */
438 DECL_FRIEND_P (decl) = 1;
440 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
441 error ("friend declaration %qD may not have virt-specifiers",
442 decl);
444 /* Unfortunately, we have to handle attributes here. Normally we would
445 handle them in start_decl_1, but since this is a friend decl start_decl_1
446 never gets to see it. */
448 /* Set attributes here so if duplicate decl, will have proper attributes. */
449 cplus_decl_attributes (&decl, attrlist, 0);
451 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
453 declarator = TREE_OPERAND (declarator, 0);
454 if (is_overloaded_fn (declarator))
455 declarator = DECL_NAME (get_first_fn (declarator));
458 if (ctype)
460 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
461 the enclosing class. FRIEND_DEPTH counts the number of template
462 headers used for this friend declaration. TEMPLATE_MEMBER_P is
463 true if a template header in FRIEND_DEPTH is intended for
464 DECLARATOR. For example, the code
466 template <class T> struct A {
467 template <class U> struct B {
468 template <class V> template <class W>
469 friend void C<V>::f(W);
473 will eventually give the following results
475 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
476 2. FRIEND_DEPTH equals 2 (for `V' and `W').
477 3. TEMPLATE_MEMBER_P is true (for `W'). */
479 int class_template_depth = template_class_depth (current_class_type);
480 int friend_depth = processing_template_decl - class_template_depth;
481 /* We will figure this out later. */
482 bool template_member_p = false;
484 tree cname = TYPE_NAME (ctype);
485 if (TREE_CODE (cname) == TYPE_DECL)
486 cname = DECL_NAME (cname);
488 /* A method friend. */
489 if (flags == NO_SPECIAL && declarator == cname)
490 DECL_CONSTRUCTOR_P (decl) = 1;
492 grokclassfn (ctype, decl, flags);
494 if (friend_depth)
496 if (!uses_template_parms_level (ctype, class_template_depth
497 + friend_depth))
498 template_member_p = true;
501 /* A nested class may declare a member of an enclosing class
502 to be a friend, so we do lookup here even if CTYPE is in
503 the process of being defined. */
504 if (class_template_depth
505 || COMPLETE_OR_OPEN_TYPE_P (ctype))
507 if (DECL_TEMPLATE_INFO (decl))
508 /* DECL is a template specialization. No need to
509 build a new TEMPLATE_DECL. */
511 else if (class_template_depth)
512 /* We rely on tsubst_friend_function to check the
513 validity of the declaration later. */
514 decl = push_template_decl_real (decl, /*is_friend=*/true);
515 else
516 decl = check_classfn (ctype, decl,
517 template_member_p
518 ? current_template_parms
519 : NULL_TREE);
521 if ((template_member_p
522 /* Always pull out the TEMPLATE_DECL if we have a friend
523 template in a class template so that it gets tsubsted
524 properly later on (59956). tsubst_friend_function knows
525 how to tell this apart from a member template. */
526 || (class_template_depth && friend_depth))
527 && decl && TREE_CODE (decl) == FUNCTION_DECL)
528 decl = DECL_TI_TEMPLATE (decl);
530 if (decl)
531 add_friend (current_class_type, decl, /*complain=*/true);
533 else
534 error ("member %qD declared as friend before type %qT defined",
535 decl, ctype);
537 /* A global friend.
538 @@ or possibly a friend from a base class ?!? */
539 else if (TREE_CODE (decl) == FUNCTION_DECL)
541 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
543 /* Friends must all go through the overload machinery,
544 even though they may not technically be overloaded.
546 Note that because classes all wind up being top-level
547 in their scope, their friend wind up in top-level scope as well. */
548 if (funcdef_flag)
549 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
551 if (! DECL_USE_TEMPLATE (decl))
553 /* We must check whether the decl refers to template
554 arguments before push_template_decl_real adds a
555 reference to the containing template class. */
556 int warn = (warn_nontemplate_friend
557 && ! funcdef_flag && ! is_friend_template
558 && current_template_parms
559 && uses_template_parms (decl));
561 if (is_friend_template
562 || template_class_depth (current_class_type) != 0)
563 /* We can't call pushdecl for a template class, since in
564 general, such a declaration depends on template
565 parameters. Instead, we call pushdecl when the class
566 is instantiated. */
567 decl = push_template_decl_real (decl, /*is_friend=*/true);
568 else if (current_function_decl)
570 /* This must be a local class. 11.5p11:
572 If a friend declaration appears in a local class (9.8) and
573 the name specified is an unqualified name, a prior
574 declaration is looked up without considering scopes that
575 are outside the innermost enclosing non-class scope. For a
576 friend function declaration, if there is no prior
577 declaration, the program is ill-formed. */
578 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
579 if (t)
580 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
581 else
583 error ("friend declaration %qD in local class without "
584 "prior declaration", decl);
585 return error_mark_node;
588 else
590 /* We can't use pushdecl, as we might be in a template
591 class specialization, and pushdecl will insert an
592 unqualified friend decl into the template parameter
593 scope, rather than the namespace containing it. */
594 tree ns = decl_namespace_context (decl);
596 push_nested_namespace (ns);
597 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
598 pop_nested_namespace (ns);
601 if (warn)
603 static int explained;
604 bool warned;
606 warned = warning (OPT_Wnon_template_friend, "friend declaration "
607 "%q#D declares a non-template function", decl);
608 if (! explained && warned)
610 inform (input_location, "(if this is not what you intended, make sure "
611 "the function template has already been declared "
612 "and add <> after the function name here) ");
613 explained = 1;
618 if (decl == error_mark_node)
619 return error_mark_node;
621 add_friend (current_class_type,
622 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
623 /*complain=*/true);
624 DECL_FRIEND_P (decl) = 1;
627 return decl;