2014-04-16 Paul Pluzhnikov <ppluzhnikov@google.com>
[official-gcc.git] / gcc / cp / friend.c
bloba30918c0006986149dd6eb4445f5ee2e403935ad
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)
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 "tm.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "flags.h"
28 /* Friend data structures are described in cp-tree.h. */
30 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
32 int
33 is_friend (tree type, tree supplicant)
35 int declp;
36 tree list;
37 tree context;
39 if (supplicant == NULL_TREE || type == NULL_TREE)
40 return 0;
42 declp = DECL_P (supplicant);
44 if (declp)
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)
60 continue;
62 if (supplicant == this_friend)
63 return 1;
65 if (is_specialization_of_friend (supplicant, this_friend))
66 return 1;
68 break;
72 else
73 /* It's a type. */
75 if (same_type_p (supplicant, type))
76 return 1;
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))
86 return 1;
90 if (declp)
92 if (DECL_FUNCTION_MEMBER_P (supplicant))
93 context = DECL_CONTEXT (supplicant);
94 else
95 context = NULL_TREE;
97 else
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);
103 else
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)
110 context = NULL_TREE;
112 if (context)
113 return is_friend (type, context);
115 return 0;
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. */
125 void
126 add_friend (tree type, tree decl, bool complain)
128 tree typedecl;
129 tree list;
130 tree name;
131 tree ctx;
133 if (decl == error_mark_node)
134 return;
136 typedecl = TYPE_MAIN_DECL (type);
137 list = DECL_FRIENDLIST (typedecl);
138 name = DECL_NAME (decl);
139 type = TREE_TYPE (typedecl);
141 while (list)
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))
150 if (complain)
151 warning (OPT_Wredundant_decls,
152 "%qD is already a friend of class %qT",
153 decl, type);
154 return;
158 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
160 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
161 TREE_VALUE (list));
162 return;
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. */
197 void
198 make_friend_class (tree type, tree friend_type, bool complain)
200 tree classes;
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);
238 return;
241 friend_type = cv_unqualified (friend_type);
243 if (check_for_bare_parameter_packs (friend_type))
244 return;
246 if (friend_depth)
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))
255 /* [temp.friend]
256 Friend declarations shall not declare partial
257 specializations. */
258 error ("partial specialization %qT declared %<friend%>",
259 friend_type);
260 return;
263 else if (same_type_p (type, friend_type))
265 if (complain)
266 warning (0, "class %qT is implicitly friends with itself",
267 type);
268 return;
271 /* [temp.friend]
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
276 class. */
277 if (!friend_depth)
278 ;/* ok */
279 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
281 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
282 == TEMPLATE_ID_EXPR)
284 /* template <class U> friend class T::X<U>; */
285 /* [temp.friend]
286 Friend declarations shall not declare partial
287 specializations. */
288 error ("partial specialization %qT declared %<friend%>",
289 friend_type);
290 return;
292 else
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);
299 tree decl;
301 if (!uses_template_parms_level (ctype, class_template_depth
302 + friend_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)
310 friend_type
311 = make_unbound_class_template (ctype,
312 name,
313 current_template_parms,
314 tf_error);
315 else
316 friend_type
317 = make_typename_type (ctype, name, class_type, tf_error);
319 else
321 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
322 if (!decl)
324 error ("%qT is not a member of %qT", name, ctype);
325 return;
327 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
329 error ("%qT is not a member class template of %qT",
330 name, ctype);
331 inform (input_location, "%q+D declared here", decl);
332 return;
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",
338 name, ctype);
339 inform (input_location, "%q+D declared here", decl);
340 return;
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);
351 return;
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);
359 return;
361 else
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)
366 return;
368 /* See if it is already a friend. */
369 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
370 classes;
371 classes = TREE_CHAIN (classes))
373 tree probe = TREE_VALUE (classes);
375 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
377 if (friend_type == probe)
379 if (complain)
380 warning (OPT_Wredundant_decls,
381 "%qD is already a friend of %qT", probe, type);
382 break;
385 else if (TREE_CODE (probe) != TEMPLATE_DECL)
387 if (same_type_p (probe, friend_type))
389 if (complain)
390 warning (OPT_Wredundant_decls,
391 "%qT is already a friend of %qT", probe, type);
392 break;
397 if (!classes)
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
417 for grokclass fn. */
419 tree
420 do_friend (tree ctype, tree declarator, tree decl,
421 tree attrlist, enum overload_flags flags,
422 bool funcdef_flag)
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 /* Unfortunately, we have to handle attributes here. Normally we would
431 handle them in start_decl_1, but since this is a friend decl start_decl_1
432 never gets to see it. */
434 /* Set attributes here so if duplicate decl, will have proper attributes. */
435 cplus_decl_attributes (&decl, attrlist, 0);
437 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
439 declarator = TREE_OPERAND (declarator, 0);
440 if (is_overloaded_fn (declarator))
441 declarator = DECL_NAME (get_first_fn (declarator));
444 if (ctype)
446 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
447 the enclosing class. FRIEND_DEPTH counts the number of template
448 headers used for this friend declaration. TEMPLATE_MEMBER_P is
449 true if a template header in FRIEND_DEPTH is intended for
450 DECLARATOR. For example, the code
452 template <class T> struct A {
453 template <class U> struct B {
454 template <class V> template <class W>
455 friend void C<V>::f(W);
459 will eventually give the following results
461 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
462 2. FRIEND_DEPTH equals 2 (for `V' and `W').
463 3. TEMPLATE_MEMBER_P is true (for `W'). */
465 int class_template_depth = template_class_depth (current_class_type);
466 int friend_depth = processing_template_decl - class_template_depth;
467 /* We will figure this out later. */
468 bool template_member_p = false;
470 tree cname = TYPE_NAME (ctype);
471 if (TREE_CODE (cname) == TYPE_DECL)
472 cname = DECL_NAME (cname);
474 /* A method friend. */
475 if (flags == NO_SPECIAL && declarator == cname)
476 DECL_CONSTRUCTOR_P (decl) = 1;
478 grokclassfn (ctype, decl, flags);
480 if (friend_depth)
482 if (!uses_template_parms_level (ctype, class_template_depth
483 + friend_depth))
484 template_member_p = true;
487 /* A nested class may declare a member of an enclosing class
488 to be a friend, so we do lookup here even if CTYPE is in
489 the process of being defined. */
490 if (class_template_depth
491 || COMPLETE_OR_OPEN_TYPE_P (ctype))
493 if (DECL_TEMPLATE_INFO (decl))
494 /* DECL is a template specialization. No need to
495 build a new TEMPLATE_DECL. */
497 else if (class_template_depth)
498 /* We rely on tsubst_friend_function to check the
499 validity of the declaration later. */
500 decl = push_template_decl_real (decl, /*is_friend=*/true);
501 else
502 decl = check_classfn (ctype, decl,
503 template_member_p
504 ? current_template_parms
505 : NULL_TREE);
507 if ((template_member_p
508 /* Always pull out the TEMPLATE_DECL if we have a friend
509 template in a class template so that it gets tsubsted
510 properly later on (59956). tsubst_friend_function knows
511 how to tell this apart from a member template. */
512 || (class_template_depth && friend_depth))
513 && decl && TREE_CODE (decl) == FUNCTION_DECL)
514 decl = DECL_TI_TEMPLATE (decl);
516 if (decl)
517 add_friend (current_class_type, decl, /*complain=*/true);
519 else
520 error ("member %qD declared as friend before type %qT defined",
521 decl, ctype);
523 /* A global friend.
524 @@ or possibly a friend from a base class ?!? */
525 else if (TREE_CODE (decl) == FUNCTION_DECL)
527 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
529 /* Friends must all go through the overload machinery,
530 even though they may not technically be overloaded.
532 Note that because classes all wind up being top-level
533 in their scope, their friend wind up in top-level scope as well. */
534 if (funcdef_flag)
535 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
537 if (! DECL_USE_TEMPLATE (decl))
539 /* We must check whether the decl refers to template
540 arguments before push_template_decl_real adds a
541 reference to the containing template class. */
542 int warn = (warn_nontemplate_friend
543 && ! funcdef_flag && ! is_friend_template
544 && current_template_parms
545 && uses_template_parms (decl));
547 if (is_friend_template
548 || template_class_depth (current_class_type) != 0)
549 /* We can't call pushdecl for a template class, since in
550 general, such a declaration depends on template
551 parameters. Instead, we call pushdecl when the class
552 is instantiated. */
553 decl = push_template_decl_real (decl, /*is_friend=*/true);
554 else if (current_function_decl)
556 /* This must be a local class. 11.5p11:
558 If a friend declaration appears in a local class (9.8) and
559 the name specified is an unqualified name, a prior
560 declaration is looked up without considering scopes that
561 are outside the innermost enclosing non-class scope. For a
562 friend function declaration, if there is no prior
563 declaration, the program is ill-formed. */
564 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
565 if (t)
566 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
567 else
569 error ("friend declaration %qD in local class without "
570 "prior declaration", decl);
571 return error_mark_node;
574 else
576 /* We can't use pushdecl, as we might be in a template
577 class specialization, and pushdecl will insert an
578 unqualified friend decl into the template parameter
579 scope, rather than the namespace containing it. */
580 tree ns = decl_namespace_context (decl);
582 push_nested_namespace (ns);
583 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
584 pop_nested_namespace (ns);
587 if (warn)
589 static int explained;
590 bool warned;
592 warned = warning (OPT_Wnon_template_friend, "friend declaration "
593 "%q#D declares a non-template function", decl);
594 if (! explained && warned)
596 inform (input_location, "(if this is not what you intended, make sure "
597 "the function template has already been declared "
598 "and add <> after the function name here) ");
599 explained = 1;
604 if (decl == error_mark_node)
605 return error_mark_node;
607 add_friend (current_class_type,
608 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
609 /*complain=*/true);
610 DECL_FRIEND_P (decl) = 1;
613 return decl;