c-common.c (c_common_nodes_and_builtins): Use cxx11 in lieu of cxx0x.
[official-gcc.git] / gcc / cp / friend.c
blob3e018e8e84c138a044ffd9831efc6ec6eec292d4
1 /* Help friends in C++.
2 Copyright (C) 1997-2013 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 (0, "%qD is already a friend of class %qT",
152 decl, type);
153 return;
157 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
159 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
160 TREE_VALUE (list));
161 return;
163 list = TREE_CHAIN (list);
166 ctx = DECL_CONTEXT (decl);
167 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
168 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
169 tf_warning_or_error);
171 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
173 DECL_FRIENDLIST (typedecl)
174 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
175 DECL_FRIENDLIST (typedecl));
176 if (!uses_template_parms (type))
177 DECL_BEFRIENDING_CLASSES (decl)
178 = tree_cons (NULL_TREE, type,
179 DECL_BEFRIENDING_CLASSES (decl));
182 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
183 been defined, we make all of its member functions friends of
184 TYPE. If not, we make it a pending friend, which can later be added
185 when its definition is seen. If a type is defined, then its TYPE_DECL's
186 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
187 classes that are not defined. If a type has not yet been defined,
188 then the DECL_WAITING_FRIENDS contains a list of types
189 waiting to make it their friend. Note that these two can both
190 be in use at the same time!
192 If COMPLAIN is true, warning about duplicate friend is issued.
193 We want to have this diagnostics during parsing but not
194 when a template is being instantiated. */
196 void
197 make_friend_class (tree type, tree friend_type, bool complain)
199 tree classes;
201 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
202 the enclosing class. FRIEND_DEPTH counts the number of template
203 headers used for this friend declaration. TEMPLATE_MEMBER_P,
204 defined inside the `if' block for TYPENAME_TYPE case, is true if
205 a template header in FRIEND_DEPTH is intended for DECLARATOR.
206 For example, the code
208 template <class T> struct A {
209 template <class U> struct B {
210 template <class V> template <class W>
211 friend class C<V>::D;
215 will eventually give the following results
217 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
218 2. FRIEND_DEPTH equals 2 (for `V' and `W').
219 3. TEMPLATE_MEMBER_P is true (for `W').
221 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
223 int class_template_depth = template_class_depth (type);
224 int friend_depth = processing_template_decl - class_template_depth;
226 if (! MAYBE_CLASS_TYPE_P (friend_type)
227 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
229 /* N1791: If the type specifier in a friend declaration designates a
230 (possibly cv-qualified) class type, that class is declared as a
231 friend; otherwise, the friend declaration is ignored.
233 So don't complain in C++11 mode. */
234 if (cxx_dialect < cxx11)
235 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
236 "invalid type %qT declared %<friend%>", friend_type);
237 return;
240 friend_type = cv_unqualified (friend_type);
242 if (check_for_bare_parameter_packs (friend_type))
243 return;
245 if (friend_depth)
246 /* If the TYPE is a template then it makes sense for it to be
247 friends with itself; this means that each instantiation is
248 friends with all other instantiations. */
250 if (CLASS_TYPE_P (friend_type)
251 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
252 && uses_template_parms (friend_type))
254 /* [temp.friend]
255 Friend declarations shall not declare partial
256 specializations. */
257 error ("partial specialization %qT declared %<friend%>",
258 friend_type);
259 return;
262 else if (same_type_p (type, friend_type))
264 if (complain)
265 warning (0, "class %qT is implicitly friends with itself",
266 type);
267 return;
270 /* [temp.friend]
272 A friend of a class or class template can be a function or
273 class template, a specialization of a function template or
274 class template, or an ordinary (nontemplate) function or
275 class. */
276 if (!friend_depth)
277 ;/* ok */
278 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
280 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
281 == TEMPLATE_ID_EXPR)
283 /* template <class U> friend class T::X<U>; */
284 /* [temp.friend]
285 Friend declarations shall not declare partial
286 specializations. */
287 error ("partial specialization %qT declared %<friend%>",
288 friend_type);
289 return;
291 else
293 /* We will figure this out later. */
294 bool template_member_p = false;
296 tree ctype = TYPE_CONTEXT (friend_type);
297 tree name = TYPE_IDENTIFIER (friend_type);
298 tree decl;
300 if (!uses_template_parms_level (ctype, class_template_depth
301 + friend_depth))
302 template_member_p = true;
304 if (class_template_depth)
306 /* We rely on tsubst_friend_class to check the
307 validity of the declaration later. */
308 if (template_member_p)
309 friend_type
310 = make_unbound_class_template (ctype,
311 name,
312 current_template_parms,
313 tf_error);
314 else
315 friend_type
316 = make_typename_type (ctype, name, class_type, tf_error);
318 else
320 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
321 if (!decl)
323 error ("%qT is not a member of %qT", name, ctype);
324 return;
326 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
328 error ("%qT is not a member class template of %qT",
329 name, ctype);
330 error ("%q+D declared here", decl);
331 return;
333 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
334 || !CLASS_TYPE_P (TREE_TYPE (decl))))
336 error ("%qT is not a nested class of %qT",
337 name, ctype);
338 error ("%q+D declared here", decl);
339 return;
342 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
346 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
348 /* template <class T> friend class T; */
349 error ("template parameter type %qT declared %<friend%>", friend_type);
350 return;
352 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
353 friend_type = TYPE_NAME (friend_type);
354 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
356 /* template <class T> friend class A; where A is not a template */
357 error ("%q#T is not a template", friend_type);
358 return;
360 else
361 /* template <class T> friend class A; where A is a template */
362 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
364 if (friend_type == error_mark_node)
365 return;
367 /* See if it is already a friend. */
368 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
369 classes;
370 classes = TREE_CHAIN (classes))
372 tree probe = TREE_VALUE (classes);
374 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
376 if (friend_type == probe)
378 if (complain)
379 warning (0, "%qD is already a friend of %qT", probe, type);
380 break;
383 else if (TREE_CODE (probe) != TEMPLATE_DECL)
385 if (same_type_p (probe, friend_type))
387 if (complain)
388 warning (0, "%qT is already a friend of %qT", probe, type);
389 break;
394 if (!classes)
396 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
398 CLASSTYPE_FRIEND_CLASSES (type)
399 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
400 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
401 friend_type = TREE_TYPE (friend_type);
402 if (!uses_template_parms (type))
403 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
404 = tree_cons (NULL_TREE, type,
405 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
409 /* Record DECL (a FUNCTION_DECL) as a friend of the
410 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
411 class of which it is a member, as named in the friend declaration.
412 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
413 friend declaration is a definition of the function. FLAGS is as
414 for grokclass fn. */
416 tree
417 do_friend (tree ctype, tree declarator, tree decl,
418 tree attrlist, enum overload_flags flags,
419 bool funcdef_flag)
421 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
422 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
424 /* Every decl that gets here is a friend of something. */
425 DECL_FRIEND_P (decl) = 1;
427 /* Unfortunately, we have to handle attributes here. Normally we would
428 handle them in start_decl_1, but since this is a friend decl start_decl_1
429 never gets to see it. */
431 /* Set attributes here so if duplicate decl, will have proper attributes. */
432 cplus_decl_attributes (&decl, attrlist, 0);
434 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
436 declarator = TREE_OPERAND (declarator, 0);
437 if (is_overloaded_fn (declarator))
438 declarator = DECL_NAME (get_first_fn (declarator));
441 if (ctype)
443 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
444 the enclosing class. FRIEND_DEPTH counts the number of template
445 headers used for this friend declaration. TEMPLATE_MEMBER_P is
446 true if a template header in FRIEND_DEPTH is intended for
447 DECLARATOR. For example, the code
449 template <class T> struct A {
450 template <class U> struct B {
451 template <class V> template <class W>
452 friend void C<V>::f(W);
456 will eventually give the following results
458 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
459 2. FRIEND_DEPTH equals 2 (for `V' and `W').
460 3. TEMPLATE_MEMBER_P is true (for `W'). */
462 int class_template_depth = template_class_depth (current_class_type);
463 int friend_depth = processing_template_decl - class_template_depth;
464 /* We will figure this out later. */
465 bool template_member_p = false;
467 tree cname = TYPE_NAME (ctype);
468 if (TREE_CODE (cname) == TYPE_DECL)
469 cname = DECL_NAME (cname);
471 /* A method friend. */
472 if (flags == NO_SPECIAL && declarator == cname)
473 DECL_CONSTRUCTOR_P (decl) = 1;
475 grokclassfn (ctype, decl, flags);
477 if (friend_depth)
479 if (!uses_template_parms_level (ctype, class_template_depth
480 + friend_depth))
481 template_member_p = true;
484 /* A nested class may declare a member of an enclosing class
485 to be a friend, so we do lookup here even if CTYPE is in
486 the process of being defined. */
487 if (class_template_depth
488 || COMPLETE_OR_OPEN_TYPE_P (ctype))
490 if (DECL_TEMPLATE_INFO (decl))
491 /* DECL is a template specialization. No need to
492 build a new TEMPLATE_DECL. */
494 else if (class_template_depth)
495 /* We rely on tsubst_friend_function to check the
496 validity of the declaration later. */
497 decl = push_template_decl_real (decl, /*is_friend=*/true);
498 else
499 decl = check_classfn (ctype, decl,
500 template_member_p
501 ? current_template_parms
502 : NULL_TREE);
504 if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
505 decl = DECL_TI_TEMPLATE (decl);
507 if (decl)
508 add_friend (current_class_type, decl, /*complain=*/true);
510 else
511 error ("member %qD declared as friend before type %qT defined",
512 decl, ctype);
514 /* A global friend.
515 @@ or possibly a friend from a base class ?!? */
516 else if (TREE_CODE (decl) == FUNCTION_DECL)
518 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
520 /* Friends must all go through the overload machinery,
521 even though they may not technically be overloaded.
523 Note that because classes all wind up being top-level
524 in their scope, their friend wind up in top-level scope as well. */
525 if (funcdef_flag)
526 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
528 if (! DECL_USE_TEMPLATE (decl))
530 /* We must check whether the decl refers to template
531 arguments before push_template_decl_real adds a
532 reference to the containing template class. */
533 int warn = (warn_nontemplate_friend
534 && ! funcdef_flag && ! is_friend_template
535 && current_template_parms
536 && uses_template_parms (decl));
538 if (is_friend_template
539 || template_class_depth (current_class_type) != 0)
540 /* We can't call pushdecl for a template class, since in
541 general, such a declaration depends on template
542 parameters. Instead, we call pushdecl when the class
543 is instantiated. */
544 decl = push_template_decl_real (decl, /*is_friend=*/true);
545 else if (current_function_decl)
547 /* This must be a local class. 11.5p11:
549 If a friend declaration appears in a local class (9.8) and
550 the name specified is an unqualified name, a prior
551 declaration is looked up without considering scopes that
552 are outside the innermost enclosing non-class scope. For a
553 friend function declaration, if there is no prior
554 declaration, the program is ill-formed. */
555 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
556 if (t)
557 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
558 else
560 error ("friend declaration %qD in local class without "
561 "prior declaration", decl);
562 return error_mark_node;
565 else
567 /* We can't use pushdecl, as we might be in a template
568 class specialization, and pushdecl will insert an
569 unqualified friend decl into the template parameter
570 scope, rather than the namespace containing it. */
571 tree ns = decl_namespace_context (decl);
573 push_nested_namespace (ns);
574 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
575 pop_nested_namespace (ns);
578 if (warn)
580 static int explained;
581 bool warned;
583 warned = warning (OPT_Wnon_template_friend, "friend declaration "
584 "%q#D declares a non-template function", decl);
585 if (! explained && warned)
587 inform (input_location, "(if this is not what you intended, make sure "
588 "the function template has already been declared "
589 "and add <> after the function name here) ");
590 explained = 1;
595 if (decl == error_mark_node)
596 return error_mark_node;
598 add_friend (current_class_type,
599 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
600 /*complain=*/true);
601 DECL_FRIEND_P (decl) = 1;
604 return decl;