re PR debug/66691 (ICE on valid code at -O3 with -g enabled in simplify_subreg, at...
[gcc.git] / gcc / cp / friend.c
1 /* Help friends in C++.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
29
30 /* Friend data structures are described in cp-tree.h. */
31
32 /* Returns nonzero if SUPPLICANT is a friend of TYPE. */
33
34 int
35 is_friend (tree type, tree supplicant)
36 {
37 int declp;
38 tree list;
39 tree context;
40
41 if (supplicant == NULL_TREE || type == NULL_TREE)
42 return 0;
43
44 declp = DECL_P (supplicant);
45
46 if (declp)
47 /* It's a function decl. */
48 {
49 tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
50 tree name = DECL_NAME (supplicant);
51
52 for (; list ; list = TREE_CHAIN (list))
53 {
54 if (name == FRIEND_NAME (list))
55 {
56 tree friends = FRIEND_DECLS (list);
57 for (; friends ; friends = TREE_CHAIN (friends))
58 {
59 tree this_friend = TREE_VALUE (friends);
60
61 if (this_friend == NULL_TREE)
62 continue;
63
64 if (supplicant == this_friend)
65 return 1;
66
67 if (is_specialization_of_friend (supplicant, this_friend))
68 return 1;
69 }
70 break;
71 }
72 }
73 }
74 else
75 /* It's a type. */
76 {
77 if (same_type_p (supplicant, type))
78 return 1;
79
80 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
81 for (; list ; list = TREE_CHAIN (list))
82 {
83 tree t = TREE_VALUE (list);
84
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;
89 }
90 }
91
92 if (declp)
93 {
94 if (DECL_FUNCTION_MEMBER_P (supplicant))
95 context = DECL_CONTEXT (supplicant);
96 else
97 context = NULL_TREE;
98 }
99 else
100 {
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));
108 }
109
110 /* A namespace is not friend to anybody. */
111 if (context && TREE_CODE (context) == NAMESPACE_DECL)
112 context = NULL_TREE;
113
114 if (context)
115 return is_friend (type, context);
116
117 return 0;
118 }
119
120 /* Add a new friend to the friends of the aggregate type TYPE.
121 DECL is the FUNCTION_DECL of the friend being added.
122
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. */
126
127 void
128 add_friend (tree type, tree decl, bool complain)
129 {
130 tree typedecl;
131 tree list;
132 tree name;
133 tree ctx;
134
135 if (decl == error_mark_node)
136 return;
137
138 typedecl = TYPE_MAIN_DECL (type);
139 list = DECL_FRIENDLIST (typedecl);
140 name = DECL_NAME (decl);
141 type = TREE_TYPE (typedecl);
142
143 while (list)
144 {
145 if (name == FRIEND_NAME (list))
146 {
147 tree friends = FRIEND_DECLS (list);
148 for (; friends ; friends = TREE_CHAIN (friends))
149 {
150 if (decl == TREE_VALUE (friends))
151 {
152 if (complain)
153 warning (OPT_Wredundant_decls,
154 "%qD is already a friend of class %qT",
155 decl, type);
156 return;
157 }
158 }
159
160 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
161
162 TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
163 TREE_VALUE (list));
164 return;
165 }
166 list = TREE_CHAIN (list);
167 }
168
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);
173
174 maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
175
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));
183 }
184
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!
194
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. */
198
199 void
200 make_friend_class (tree type, tree friend_type, bool complain)
201 {
202 tree classes;
203
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
210
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;
215 };
216 };
217
218 will eventually give the following results
219
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').
223
224 The friend is a template friend iff FRIEND_DEPTH is nonzero. */
225
226 int class_template_depth = template_class_depth (type);
227 int friend_depth = processing_template_decl - class_template_depth;
228
229 if (! MAYBE_CLASS_TYPE_P (friend_type)
230 && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
231 {
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.
235
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;
241 }
242
243 friend_type = cv_unqualified (friend_type);
244
245 if (check_for_bare_parameter_packs (friend_type))
246 return;
247
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. */
252 {
253 if (CLASS_TYPE_P (friend_type)
254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
255 && uses_template_parms (friend_type))
256 {
257 /* [temp.friend]
258 Friend declarations shall not declare partial
259 specializations. */
260 error ("partial specialization %qT declared %<friend%>",
261 friend_type);
262 return;
263 }
264 }
265 else if (same_type_p (type, friend_type))
266 {
267 if (complain)
268 warning (0, "class %qT is implicitly friends with itself",
269 type);
270 return;
271 }
272
273 /* [temp.friend]
274
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)
282 {
283 if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
284 == TEMPLATE_ID_EXPR)
285 {
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;
293 }
294 else
295 {
296 /* We will figure this out later. */
297 bool template_member_p = false;
298
299 tree ctype = TYPE_CONTEXT (friend_type);
300 tree name = TYPE_IDENTIFIER (friend_type);
301 tree decl;
302
303 if (!uses_template_parms_level (ctype, class_template_depth
304 + friend_depth))
305 template_member_p = true;
306
307 if (class_template_depth)
308 {
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);
320 }
321 else
322 {
323 decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
324 if (!decl)
325 {
326 error ("%qT is not a member of %qT", name, ctype);
327 return;
328 }
329 if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
330 {
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;
335 }
336 if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
337 || !CLASS_TYPE_P (TREE_TYPE (decl))))
338 {
339 error ("%qT is not a nested class of %qT",
340 name, ctype);
341 inform (input_location, "%q+D declared here", decl);
342 return;
343 }
344
345 friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
346 }
347 }
348 }
349 else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
350 {
351 /* template <class T> friend class T; */
352 error ("template parameter type %qT declared %<friend%>", friend_type);
353 return;
354 }
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))
358 {
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;
362 }
363 else
364 /* template <class T> friend class A; where A is a template */
365 friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
366
367 if (friend_type == error_mark_node)
368 return;
369
370 /* See if it is already a friend. */
371 for (classes = CLASSTYPE_FRIEND_CLASSES (type);
372 classes;
373 classes = TREE_CHAIN (classes))
374 {
375 tree probe = TREE_VALUE (classes);
376
377 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
378 {
379 if (friend_type == probe)
380 {
381 if (complain)
382 warning (OPT_Wredundant_decls,
383 "%qD is already a friend of %qT", probe, type);
384 break;
385 }
386 }
387 else if (TREE_CODE (probe) != TEMPLATE_DECL)
388 {
389 if (same_type_p (probe, friend_type))
390 {
391 if (complain)
392 warning (OPT_Wredundant_decls,
393 "%qT is already a friend of %qT", probe, type);
394 break;
395 }
396 }
397 }
398
399 if (!classes)
400 {
401 maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
402
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));
411 }
412 }
413
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. */
420
421 tree
422 do_friend (tree ctype, tree declarator, tree decl,
423 tree attrlist, enum overload_flags flags,
424 bool funcdef_flag)
425 {
426 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
427 gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
428
429 /* Every decl that gets here is a friend of something. */
430 DECL_FRIEND_P (decl) = 1;
431
432 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
433 error ("friend declaration %qD may not have virt-specifiers",
434 decl);
435
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. */
439
440 /* Set attributes here so if duplicate decl, will have proper attributes. */
441 cplus_decl_attributes (&decl, attrlist, 0);
442
443 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
444 {
445 declarator = TREE_OPERAND (declarator, 0);
446 if (is_overloaded_fn (declarator))
447 declarator = DECL_NAME (get_first_fn (declarator));
448 }
449
450 if (ctype)
451 {
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
457
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);
462 };
463 };
464
465 will eventually give the following results
466
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'). */
470
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;
475
476 tree cname = TYPE_NAME (ctype);
477 if (TREE_CODE (cname) == TYPE_DECL)
478 cname = DECL_NAME (cname);
479
480 /* A method friend. */
481 if (flags == NO_SPECIAL && declarator == cname)
482 DECL_CONSTRUCTOR_P (decl) = 1;
483
484 grokclassfn (ctype, decl, flags);
485
486 if (friend_depth)
487 {
488 if (!uses_template_parms_level (ctype, class_template_depth
489 + friend_depth))
490 template_member_p = true;
491 }
492
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))
498 {
499 if (DECL_TEMPLATE_INFO (decl))
500 /* DECL is a template specialization. No need to
501 build a new TEMPLATE_DECL. */
502 ;
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);
512
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);
521
522 if (decl)
523 add_friend (current_class_type, decl, /*complain=*/true);
524 }
525 else
526 error ("member %qD declared as friend before type %qT defined",
527 decl, ctype);
528 }
529 /* A global friend.
530 @@ or possibly a friend from a base class ?!? */
531 else if (TREE_CODE (decl) == FUNCTION_DECL)
532 {
533 int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
534
535 /* Friends must all go through the overload machinery,
536 even though they may not technically be overloaded.
537
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);
542
543 if (! DECL_USE_TEMPLATE (decl))
544 {
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));
552
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)
561 {
562 /* This must be a local class. 11.5p11:
563
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
574 {
575 error ("friend declaration %qD in local class without "
576 "prior declaration", decl);
577 return error_mark_node;
578 }
579 }
580 else
581 {
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);
587
588 push_nested_namespace (ns);
589 decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
590 pop_nested_namespace (ns);
591 }
592
593 if (warn)
594 {
595 static int explained;
596 bool warned;
597
598 warned = warning (OPT_Wnon_template_friend, "friend declaration "
599 "%q#D declares a non-template function", decl);
600 if (! explained && warned)
601 {
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;
606 }
607 }
608 }
609
610 if (decl == error_mark_node)
611 return error_mark_node;
612
613 add_friend (current_class_type,
614 is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
615 /*complain=*/true);
616 DECL_FRIEND_P (decl) = 1;
617 }
618
619 return decl;
620 }