jit: document union types
[official-gcc.git] / gcc / cp / friend.c
blob96655f327f1a9aed43117c3532d2a7a64795096e
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)
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 "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
30 /* Friend data structures are described in cp-tree.h. */
32 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
34 int
35 is_friend (tree type, tree supplicant)
37 int declp;
38 tree list;
39 tree context;
41 if (supplicant == NULL_TREE || type == NULL_TREE)
42 return 0;
44 declp = DECL_P (supplicant);
46 if (declp)
47 /* It's a function decl. */
49 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
50 tree name = DECL_NAME (supplicant);
52 for (; list ; list = TREE_CHAIN (list))
54 if (name == FRIEND_NAME (list))
56 tree friends = FRIEND_DECLS (list);
57 for (; friends ; friends = TREE_CHAIN (friends))
59 tree this_friend = TREE_VALUE (friends);
61 if (this_friend == NULL_TREE)
62 continue;
64 if (supplicant == this_friend)
65 return 1;
67 if (is_specialization_of_friend (supplicant, this_friend))
68 return 1;
70 break;
74 else
75 /* It's a type. */
77 if (same_type_p (supplicant, type))
78 return 1;
80 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
81 for (; list ; list = TREE_CHAIN (list))
83 tree t = TREE_VALUE (list);
85 if (TREE_CODE (t) == TEMPLATE_DECL ?
86 is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
87 same_type_p (supplicant, t))
88 return 1;
92 if (declp)
94 if (DECL_FUNCTION_MEMBER_P (supplicant))
95 context = DECL_CONTEXT (supplicant);
96 else
97 context = NULL_TREE;
99 else
101 if (TYPE_CLASS_SCOPE_P (supplicant))
102 /* Nested classes get the same access as their enclosing types, as
103 per DR 45 (this is a change from the standard). */
104 context = TYPE_CONTEXT (supplicant);
105 else
106 /* Local classes have the same access as the enclosing function. */
107 context = decl_function_context (TYPE_MAIN_DECL (supplicant));
110 /* A namespace is not friend to anybody. */
111 if (context && TREE_CODE (context) == NAMESPACE_DECL)
112 context = NULL_TREE;
114 if (context)
115 return is_friend (type, context);
117 return 0;
120 /* Add a new friend to the friends of the aggregate type TYPE.
121 DECL is the FUNCTION_DECL of the friend being added.
123 If COMPLAIN is true, warning about duplicate friend is issued.
124 We want to have this diagnostics during parsing but not
125 when a template is being instantiated. */
127 void
128 add_friend (tree type, tree decl, bool complain)
130 tree typedecl;
131 tree list;
132 tree name;
133 tree ctx;
135 if (decl == error_mark_node)
136 return;
138 typedecl = TYPE_MAIN_DECL (type);
139 list = DECL_FRIENDLIST (typedecl);
140 name = DECL_NAME (decl);
141 type = TREE_TYPE (typedecl);
143 while (list)
145 if (name == FRIEND_NAME (list))
147 tree friends = FRIEND_DECLS (list);
148 for (; friends ; friends = TREE_CHAIN (friends))
150 if (decl == TREE_VALUE (friends))
152 if (complain)
153 warning (OPT_Wredundant_decls,
154 "%qD is already a friend of class %qT",
155 decl, type);
156 return;
160 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
162 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
163 TREE_VALUE (list));
164 return;
166 list = TREE_CHAIN (list);
169 ctx = DECL_CONTEXT (decl);
170 if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
171 perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
172 tf_warning_or_error);
174 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
176 DECL_FRIENDLIST (typedecl)
177 = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
178 DECL_FRIENDLIST (typedecl));
179 if (!uses_template_parms (type))
180 DECL_BEFRIENDING_CLASSES (decl)
181 = tree_cons (NULL_TREE, type,
182 DECL_BEFRIENDING_CLASSES (decl));
185 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
186 been defined, we make all of its member functions friends of
187 TYPE. If not, we make it a pending friend, which can later be added
188 when its definition is seen. If a type is defined, then its TYPE_DECL's
189 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
190 classes that are not defined. If a type has not yet been defined,
191 then the DECL_WAITING_FRIENDS contains a list of types
192 waiting to make it their friend. Note that these two can both
193 be in use at the same time!
195 If COMPLAIN is true, warning about duplicate friend is issued.
196 We want to have this diagnostics during parsing but not
197 when a template is being instantiated. */
199 void
200 make_friend_class (tree type, tree friend_type, bool complain)
202 tree classes;
204 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
205 the enclosing class. FRIEND_DEPTH counts the number of template
206 headers used for this friend declaration. TEMPLATE_MEMBER_P,
207 defined inside the `if' block for TYPENAME_TYPE case, is true if
208 a template header in FRIEND_DEPTH is intended for DECLARATOR.
209 For example, the code
211 template <class T> struct A {
212 template <class U> struct B {
213 template <class V> template <class W>
214 friend class C<V>::D;
218 will eventually give the following results
220 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
221 2. FRIEND_DEPTH equals 2 (for `V' and `W').
222 3. TEMPLATE_MEMBER_P is true (for `W').
224 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
226 int class_template_depth = template_class_depth (type);
227 int friend_depth = processing_template_decl - class_template_depth;
229 if (! MAYBE_CLASS_TYPE_P (friend_type)
230 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
232 /* N1791: If the type specifier in a friend declaration designates a
233 (possibly cv-qualified) class type, that class is declared as a
234 friend; otherwise, the friend declaration is ignored.
236 So don't complain in C++11 mode. */
237 if (cxx_dialect < cxx11)
238 pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
239 "invalid type %qT declared %<friend%>", friend_type);
240 return;
243 friend_type = cv_unqualified (friend_type);
245 if (check_for_bare_parameter_packs (friend_type))
246 return;
248 if (friend_depth)
249 /* If the TYPE is a template then it makes sense for it to be
250 friends with itself; this means that each instantiation is
251 friends with all other instantiations. */
253 if (CLASS_TYPE_P (friend_type)
254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
255 && uses_template_parms (friend_type))
257 /* [temp.friend]
258 Friend declarations shall not declare partial
259 specializations. */
260 error ("partial specialization %qT declared %<friend%>",
261 friend_type);
262 return;
265 else if (same_type_p (type, friend_type))
267 if (complain)
268 warning (0, "class %qT is implicitly friends with itself",
269 type);
270 return;
273 /* [temp.friend]
275 A friend of a class or class template can be a function or
276 class template, a specialization of a function template or
277 class template, or an ordinary (nontemplate) function or
278 class. */
279 if (!friend_depth)
280 ;/* ok */
281 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
283 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
284 == TEMPLATE_ID_EXPR)
286 /* template <class U> friend class T::X<U>; */
287 /* [temp.friend]
288 Friend declarations shall not declare partial
289 specializations. */
290 error ("partial specialization %qT declared %<friend%>",
291 friend_type);
292 return;
294 else
296 /* We will figure this out later. */
297 bool template_member_p = false;
299 tree ctype = TYPE_CONTEXT (friend_type);
300 tree name = TYPE_IDENTIFIER (friend_type);
301 tree decl;
303 if (!uses_template_parms_level (ctype, class_template_depth
304 + friend_depth))
305 template_member_p = true;
307 if (class_template_depth)
309 /* We rely on tsubst_friend_class to check the
310 validity of the declaration later. */
311 if (template_member_p)
312 friend_type
313 = make_unbound_class_template (ctype,
314 name,
315 current_template_parms,
316 tf_error);
317 else
318 friend_type
319 = make_typename_type (ctype, name, class_type, tf_error);
321 else
323 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
324 if (!decl)
326 error ("%qT is not a member of %qT", name, ctype);
327 return;
329 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
331 error ("%qT is not a member class template of %qT",
332 name, ctype);
333 inform (input_location, "%q+D declared here", decl);
334 return;
336 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
337 || !CLASS_TYPE_P (TREE_TYPE (decl))))
339 error ("%qT is not a nested class of %qT",
340 name, ctype);
341 inform (input_location, "%q+D declared here", decl);
342 return;
345 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
349 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
351 /* template <class T> friend class T; */
352 error ("template parameter type %qT declared %<friend%>", friend_type);
353 return;
355 else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
356 friend_type = TYPE_NAME (friend_type);
357 else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
359 /* template <class T> friend class A; where A is not a template */
360 error ("%q#T is not a template", friend_type);
361 return;
363 else
364 /* template <class T> friend class A; where A is a template */
365 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
367 if (friend_type == error_mark_node)
368 return;
370 /* See if it is already a friend. */
371 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
372 classes;
373 classes = TREE_CHAIN (classes))
375 tree probe = TREE_VALUE (classes);
377 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
379 if (friend_type == probe)
381 if (complain)
382 warning (OPT_Wredundant_decls,
383 "%qD is already a friend of %qT", probe, type);
384 break;
387 else if (TREE_CODE (probe) != TEMPLATE_DECL)
389 if (same_type_p (probe, friend_type))
391 if (complain)
392 warning (OPT_Wredundant_decls,
393 "%qT is already a friend of %qT", probe, type);
394 break;
399 if (!classes)
401 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
403 CLASSTYPE_FRIEND_CLASSES (type)
404 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
405 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
406 friend_type = TREE_TYPE (friend_type);
407 if (!uses_template_parms (type))
408 CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
409 = tree_cons (NULL_TREE, type,
410 CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
414 /* Record DECL (a FUNCTION_DECL) as a friend of the
415 CURRENT_CLASS_TYPE. If DECL is a member function, CTYPE is the
416 class of which it is a member, as named in the friend declaration.
417 DECLARATOR is the name of the friend. FUNCDEF_FLAG is true if the
418 friend declaration is a definition of the function. FLAGS is as
419 for grokclass fn. */
421 tree
422 do_friend (tree ctype, tree declarator, tree decl,
423 tree attrlist, enum overload_flags flags,
424 bool funcdef_flag)
426 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
427 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
429 /* Every decl that gets here is a friend of something. */
430 DECL_FRIEND_P (decl) = 1;
432 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
433 error ("friend declaration %qD may not have virt-specifiers",
434 decl);
436 /* Unfortunately, we have to handle attributes here. Normally we would
437 handle them in start_decl_1, but since this is a friend decl start_decl_1
438 never gets to see it. */
440 /* Set attributes here so if duplicate decl, will have proper attributes. */
441 cplus_decl_attributes (&decl, attrlist, 0);
443 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
445 declarator = TREE_OPERAND (declarator, 0);
446 if (is_overloaded_fn (declarator))
447 declarator = DECL_NAME (get_first_fn (declarator));
450 if (ctype)
452 /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
453 the enclosing class. FRIEND_DEPTH counts the number of template
454 headers used for this friend declaration. TEMPLATE_MEMBER_P is
455 true if a template header in FRIEND_DEPTH is intended for
456 DECLARATOR. For example, the code
458 template <class T> struct A {
459 template <class U> struct B {
460 template <class V> template <class W>
461 friend void C<V>::f(W);
465 will eventually give the following results
467 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
468 2. FRIEND_DEPTH equals 2 (for `V' and `W').
469 3. TEMPLATE_MEMBER_P is true (for `W'). */
471 int class_template_depth = template_class_depth (current_class_type);
472 int friend_depth = processing_template_decl - class_template_depth;
473 /* We will figure this out later. */
474 bool template_member_p = false;
476 tree cname = TYPE_NAME (ctype);
477 if (TREE_CODE (cname) == TYPE_DECL)
478 cname = DECL_NAME (cname);
480 /* A method friend. */
481 if (flags == NO_SPECIAL && declarator == cname)
482 DECL_CONSTRUCTOR_P (decl) = 1;
484 grokclassfn (ctype, decl, flags);
486 if (friend_depth)
488 if (!uses_template_parms_level (ctype, class_template_depth
489 + friend_depth))
490 template_member_p = true;
493 /* A nested class may declare a member of an enclosing class
494 to be a friend, so we do lookup here even if CTYPE is in
495 the process of being defined. */
496 if (class_template_depth
497 || COMPLETE_OR_OPEN_TYPE_P (ctype))
499 if (DECL_TEMPLATE_INFO (decl))
500 /* DECL is a template specialization. No need to
501 build a new TEMPLATE_DECL. */
503 else if (class_template_depth)
504 /* We rely on tsubst_friend_function to check the
505 validity of the declaration later. */
506 decl = push_template_decl_real (decl, /*is_friend=*/true);
507 else
508 decl = check_classfn (ctype, decl,
509 template_member_p
510 ? current_template_parms
511 : NULL_TREE);
513 if ((template_member_p
514 /* Always pull out the TEMPLATE_DECL if we have a friend
515 template in a class template so that it gets tsubsted
516 properly later on (59956). tsubst_friend_function knows
517 how to tell this apart from a member template. */
518 || (class_template_depth && friend_depth))
519 && decl && TREE_CODE (decl) == FUNCTION_DECL)
520 decl = DECL_TI_TEMPLATE (decl);
522 if (decl)
523 add_friend (current_class_type, decl, /*complain=*/true);
525 else
526 error ("member %qD declared as friend before type %qT defined",
527 decl, ctype);
529 /* A global friend.
530 @@ or possibly a friend from a base class ?!? */
531 else if (TREE_CODE (decl) == FUNCTION_DECL)
533 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
535 /* Friends must all go through the overload machinery,
536 even though they may not technically be overloaded.
538 Note that because classes all wind up being top-level
539 in their scope, their friend wind up in top-level scope as well. */
540 if (funcdef_flag)
541 SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
543 if (! DECL_USE_TEMPLATE (decl))
545 /* We must check whether the decl refers to template
546 arguments before push_template_decl_real adds a
547 reference to the containing template class. */
548 int warn = (warn_nontemplate_friend
549 && ! funcdef_flag && ! is_friend_template
550 && current_template_parms
551 && uses_template_parms (decl));
553 if (is_friend_template
554 || template_class_depth (current_class_type) != 0)
555 /* We can't call pushdecl for a template class, since in
556 general, such a declaration depends on template
557 parameters. Instead, we call pushdecl when the class
558 is instantiated. */
559 decl = push_template_decl_real (decl, /*is_friend=*/true);
560 else if (current_function_decl)
562 /* This must be a local class. 11.5p11:
564 If a friend declaration appears in a local class (9.8) and
565 the name specified is an unqualified name, a prior
566 declaration is looked up without considering scopes that
567 are outside the innermost enclosing non-class scope. For a
568 friend function declaration, if there is no prior
569 declaration, the program is ill-formed. */
570 tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
571 if (t)
572 decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
573 else
575 error ("friend declaration %qD in local class without "
576 "prior declaration", decl);
577 return error_mark_node;
580 else
582 /* We can't use pushdecl, as we might be in a template
583 class specialization, and pushdecl will insert an
584 unqualified friend decl into the template parameter
585 scope, rather than the namespace containing it. */
586 tree ns = decl_namespace_context (decl);
588 push_nested_namespace (ns);
589 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
590 pop_nested_namespace (ns);
593 if (warn)
595 static int explained;
596 bool warned;
598 warned = warning (OPT_Wnon_template_friend, "friend declaration "
599 "%q#D declares a non-template function", decl);
600 if (! explained && warned)
602 inform (input_location, "(if this is not what you intended, make sure "
603 "the function template has already been declared "
604 "and add <> after the function name here) ");
605 explained = 1;
610 if (decl == error_mark_node)
611 return error_mark_node;
613 add_friend (current_class_type,
614 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
615 /*complain=*/true);
616 DECL_FRIEND_P (decl) = 1;
619 return decl;