xref: /netbsd/external/gpl3/gcc/dist/gcc/cp/friend.cc (revision f0fbc68b)
1 /* Help friends in C++.
2    Copyright (C) 1997-2022 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 "cp-tree.h"
24 
25 /* Friend data structures are described in cp-tree.h.  */
26 
27 
28 /* The GLOBAL_FRIEND scope (functions, classes, or templates) is
29    regarded as a friend of every class.  This is only used by libcc1,
30    to enable GDB's code snippets to access private members without
31    disabling access control in general, which could cause different
32    template overload resolution results when accessibility matters
33    (e.g. tests for an accessible member).  */
34 
35 static GTY(()) tree global_friend;
36 
37 /* Set the GLOBAL_FRIEND for this compilation session.  It might be
38    set multiple times, but always to the same scope.  */
39 
40 void
set_global_friend(tree scope)41 set_global_friend (tree scope)
42 {
43   gcc_checking_assert (scope != NULL_TREE);
44   gcc_assert (!global_friend || global_friend == scope);
45   global_friend = scope;
46 }
47 
48 /* Return TRUE if SCOPE is the global friend.  */
49 
50 bool
is_global_friend(tree scope)51 is_global_friend (tree scope)
52 {
53   gcc_checking_assert (scope != NULL_TREE);
54 
55   if (global_friend == scope)
56     return true;
57 
58   if (!global_friend)
59     return false;
60 
61   if (is_specialization_of_friend (global_friend, scope))
62     return true;
63 
64   return false;
65 }
66 
67 /* Returns nonzero if SUPPLICANT is a friend of TYPE.  */
68 
69 int
is_friend(tree type,tree supplicant)70 is_friend (tree type, tree supplicant)
71 {
72   int declp;
73   tree list;
74   tree context;
75 
76   if (supplicant == NULL_TREE || type == NULL_TREE)
77     return 0;
78 
79   if (is_global_friend (supplicant))
80     return 1;
81 
82   declp = DECL_P (supplicant);
83 
84   if (declp)
85     /* It's a function decl.  */
86     {
87       tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
88       tree name = DECL_NAME (supplicant);
89 
90       for (; list ; list = TREE_CHAIN (list))
91 	{
92 	  if (name == FRIEND_NAME (list))
93 	    {
94 	      tree friends = FRIEND_DECLS (list);
95 	      for (; friends ; friends = TREE_CHAIN (friends))
96 		{
97 		  tree this_friend = TREE_VALUE (friends);
98 
99 		  if (this_friend == NULL_TREE)
100 		    continue;
101 
102 		  if (supplicant == this_friend)
103 		    return 1;
104 
105 		  if (is_specialization_of_friend (supplicant, this_friend))
106 		    return 1;
107 		}
108 	      break;
109 	    }
110 	}
111     }
112   else
113     /* It's a type.  */
114     {
115       if (same_type_p (supplicant, type))
116 	return 1;
117 
118       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
119       for (; list ; list = TREE_CHAIN (list))
120 	{
121 	  tree t = TREE_VALUE (list);
122 
123 	  if (TREE_CODE (t) == TEMPLATE_DECL ?
124 	      is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
125 	      same_type_p (supplicant, t))
126 	    return 1;
127 	}
128     }
129 
130   if (declp)
131     {
132       if (DECL_FUNCTION_MEMBER_P (supplicant))
133 	context = DECL_CONTEXT (supplicant);
134       else
135 	context = NULL_TREE;
136     }
137   else
138     {
139       if (TYPE_CLASS_SCOPE_P (supplicant))
140 	/* Nested classes get the same access as their enclosing types, as
141 	   per DR 45 (this is a change from the standard).  */
142 	context = TYPE_CONTEXT (supplicant);
143       else
144 	/* Local classes have the same access as the enclosing function.  */
145 	context = decl_function_context (TYPE_MAIN_DECL (supplicant));
146     }
147 
148   /* A namespace is not friend to anybody.  */
149   if (context && TREE_CODE (context) == NAMESPACE_DECL)
150     context = NULL_TREE;
151 
152   if (context)
153     return is_friend (type, context);
154 
155   return 0;
156 }
157 
158 /* Add a new friend to the friends of the aggregate type TYPE.
159    DECL is the FUNCTION_DECL of the friend being added.
160 
161    If COMPLAIN is true, warning about duplicate friend is issued.
162    We want to have this diagnostics during parsing but not
163    when a template is being instantiated.  */
164 
165 void
add_friend(tree type,tree decl,bool complain)166 add_friend (tree type, tree decl, bool complain)
167 {
168   tree typedecl;
169   tree list;
170   tree name;
171   tree ctx;
172 
173   if (decl == error_mark_node)
174     return;
175 
176   typedecl = TYPE_MAIN_DECL (type);
177   list = DECL_FRIENDLIST (typedecl);
178   name = DECL_NAME (decl);
179   type = TREE_TYPE (typedecl);
180 
181   while (list)
182     {
183       if (name == FRIEND_NAME (list))
184 	{
185 	  tree friends = FRIEND_DECLS (list);
186 	  for (; friends ; friends = TREE_CHAIN (friends))
187 	    {
188 	      if (decl == TREE_VALUE (friends))
189 		{
190 		  if (complain)
191 		    warning (OPT_Wredundant_decls,
192 			     "%qD is already a friend of class %qT",
193 			     decl, type);
194 		  return;
195 		}
196 	    }
197 
198 	  TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
199 					 TREE_VALUE (list));
200 	  break;
201 	}
202       list = TREE_CHAIN (list);
203     }
204 
205   ctx = DECL_CONTEXT (decl);
206   if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
207     perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
208 				   tf_warning_or_error);
209 
210   maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
211 
212   if (!list)
213     DECL_FRIENDLIST (typedecl)
214       = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
215 		   DECL_FRIENDLIST (typedecl));
216   if (!uses_template_parms (type))
217     DECL_BEFRIENDING_CLASSES (decl)
218       = tree_cons (NULL_TREE, type,
219 		   DECL_BEFRIENDING_CLASSES (decl));
220 }
221 
222 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
223    been defined, we make all of its member functions friends of
224    TYPE.  If not, we make it a pending friend, which can later be added
225    when its definition is seen.  If a type is defined, then its TYPE_DECL's
226    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
227    classes that are not defined.  If a type has not yet been defined,
228    then the DECL_WAITING_FRIENDS contains a list of types
229    waiting to make it their friend.  Note that these two can both
230    be in use at the same time!
231 
232    If COMPLAIN is true, warning about duplicate friend is issued.
233    We want to have this diagnostics during parsing but not
234    when a template is being instantiated.  */
235 
236 void
make_friend_class(tree type,tree friend_type,bool complain)237 make_friend_class (tree type, tree friend_type, bool complain)
238 {
239   tree classes;
240 
241   /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
242      the enclosing class.  FRIEND_DEPTH counts the number of template
243      headers used for this friend declaration.  TEMPLATE_MEMBER_P,
244      defined inside the `if' block for TYPENAME_TYPE case, is true if
245      a template header in FRIEND_DEPTH is intended for DECLARATOR.
246      For example, the code
247 
248        template <class T> struct A {
249 	 template <class U> struct B {
250 	   template <class V> template <class W>
251 	     friend class C<V>::D;
252 	 };
253        };
254 
255      will eventually give the following results
256 
257      1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
258      2. FRIEND_DEPTH equals 2 (for `V' and `W').
259      3. TEMPLATE_MEMBER_P is true (for `W').
260 
261      The friend is a template friend iff FRIEND_DEPTH is nonzero.  */
262 
263   int class_template_depth = template_class_depth (type);
264   int friend_depth = 0;
265   if (current_template_depth)
266     /* When processing a friend declaration at parse time, just compare the
267        current depth to that of the class template.  */
268     friend_depth = current_template_depth - class_template_depth;
269   else
270     {
271       /* Otherwise, we got here from instantiate_class_template.  Determine
272 	 the friend depth by looking at the template parameters used within
273 	 FRIEND_TYPE.  */
274       gcc_checking_assert (class_template_depth == 0);
275       while (uses_template_parms_level (friend_type, friend_depth + 1))
276 	++friend_depth;
277     }
278 
279   if (! MAYBE_CLASS_TYPE_P (friend_type)
280       && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
281     {
282       /* N1791: If the type specifier in a friend declaration designates a
283 	 (possibly cv-qualified) class type, that class is declared as a
284 	 friend; otherwise, the friend declaration is ignored.
285 
286          So don't complain in C++11 mode.  */
287       if (cxx_dialect < cxx11)
288 	pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
289 		 "invalid type %qT declared %<friend%>", friend_type);
290       return;
291     }
292 
293   friend_type = cv_unqualified (friend_type);
294 
295   if (check_for_bare_parameter_packs (friend_type))
296     return;
297 
298   if (friend_depth)
299     {
300       /* [temp.friend] Friend declarations shall not declare partial
301 	 specializations.  */
302       if (CLASS_TYPE_P (friend_type)
303 	  && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
304 	  && uses_template_parms (friend_type))
305 	{
306 	  error ("partial specialization %qT declared %<friend%>",
307 		 friend_type);
308 	  return;
309 	}
310 
311       if (TYPE_TEMPLATE_INFO (friend_type)
312 	  && !PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (friend_type)))
313 	{
314 	  auto_diagnostic_group d;
315 	  error ("%qT is not a template", friend_type);
316 	  inform (location_of (friend_type), "previous declaration here");
317 	  if (TYPE_CLASS_SCOPE_P (friend_type)
318 	      && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (friend_type))
319 	      && currently_open_class (TYPE_CONTEXT (friend_type)))
320 	    inform (input_location, "perhaps you need explicit template "
321 		    "arguments in your nested-name-specifier");
322 	  return;
323 	}
324     }
325 
326   /* It makes sense for a template class to be friends with itself,
327      that means the instantiations can be friendly.  Other cases are
328      not so meaningful.  */
329   if (!friend_depth && same_type_p (type, friend_type))
330     {
331       if (complain)
332 	warning (0, "class %qT is implicitly friends with itself",
333 		 type);
334       return;
335     }
336 
337   /* [temp.friend]
338 
339      A friend of a class or class template can be a function or
340      class template, a specialization of a function template or
341      class template, or an ordinary (nontemplate) function or
342      class.  */
343   if (!friend_depth)
344     ;/* ok */
345   else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
346     {
347       if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
348 	  == TEMPLATE_ID_EXPR)
349 	{
350 	  /* template <class U> friend class T::X<U>; */
351 	  /* [temp.friend]
352 	     Friend declarations shall not declare partial
353 	     specializations.  */
354 	  error ("partial specialization %qT declared %<friend%>",
355 		 friend_type);
356 	  return;
357 	}
358       else
359 	{
360 	  /* We will figure this out later.  */
361 	  bool template_member_p = false;
362 
363 	  tree ctype = TYPE_CONTEXT (friend_type);
364 	  tree name = TYPE_IDENTIFIER (friend_type);
365 	  tree decl;
366 
367 	  /* We need to distinguish a TYPENAME_TYPE for the non-template
368 	     class B in
369 	       template<class T> friend class A<T>::B;
370 	     vs for the class template B in
371 	       template<class T> template<class U> friend class A<T>::B;  */
372 	  if (current_template_depth
373 	      && !uses_template_parms_level (ctype, current_template_depth))
374 	    template_member_p = true;
375 
376 	  if (class_template_depth)
377 	    {
378 	      /* We rely on tsubst_friend_class to check the
379 		 validity of the declaration later.  */
380 	      if (template_member_p)
381 		friend_type
382 		  = make_unbound_class_template (ctype,
383 						 name,
384 						 current_template_parms,
385 						 tf_error);
386 	      else
387 		friend_type
388 		  = make_typename_type (ctype, name, class_type, tf_error);
389 	    }
390 	  else
391 	    {
392 	      decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
393 	      if (!decl)
394 		{
395 		  error ("%qT is not a member of %qT", name, ctype);
396 		  return;
397 		}
398 	      if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
399 		{
400 		  auto_diagnostic_group d;
401 		  error ("%qT is not a member class template of %qT",
402 			 name, ctype);
403 		  inform (DECL_SOURCE_LOCATION (decl),
404 			  "%qD declared here", decl);
405 		  return;
406 		}
407 	      if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
408 					 || !CLASS_TYPE_P (TREE_TYPE (decl))))
409 		{
410 		  auto_diagnostic_group d;
411 		  error ("%qT is not a nested class of %qT",
412 			 name, ctype);
413 		  inform (DECL_SOURCE_LOCATION (decl),
414 			  "%qD declared here", decl);
415 		  return;
416 		}
417 
418 	      friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
419 	    }
420 	}
421     }
422   else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
423     {
424       /* template <class T> friend class T; */
425       error ("template parameter type %qT declared %<friend%>", friend_type);
426       return;
427     }
428   else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
429     friend_type = TYPE_NAME (friend_type);
430   else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
431     {
432       /* template <class T> friend class A; where A is not a template */
433       error ("%q#T is not a template", friend_type);
434       return;
435     }
436   else
437     /* template <class T> friend class A; where A is a template */
438     friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
439 
440   if (friend_type == error_mark_node)
441     return;
442 
443   /* See if it is already a friend.  */
444   for (classes = CLASSTYPE_FRIEND_CLASSES (type);
445        classes;
446        classes = TREE_CHAIN (classes))
447     {
448       tree probe = TREE_VALUE (classes);
449 
450       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
451 	{
452 	  if (friend_type == probe)
453 	    {
454 	      if (complain)
455 		warning (OPT_Wredundant_decls,
456 			 "%qD is already a friend of %qT", probe, type);
457 	      break;
458 	    }
459 	}
460       else if (TREE_CODE (probe) != TEMPLATE_DECL)
461 	{
462 	  if (same_type_p (probe, friend_type))
463 	    {
464 	      if (complain)
465 		warning (OPT_Wredundant_decls,
466 			 "%qT is already a friend of %qT", probe, type);
467 	      break;
468 	    }
469 	}
470     }
471 
472   if (!classes)
473     {
474       maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
475 
476       CLASSTYPE_FRIEND_CLASSES (type)
477 	= tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
478       if (TREE_CODE (friend_type) == TEMPLATE_DECL)
479 	friend_type = TREE_TYPE (friend_type);
480       if (!uses_template_parms (type))
481 	CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
482 	  = tree_cons (NULL_TREE, type,
483 		       CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
484     }
485 }
486 
487 /* Record DECL (a FUNCTION_DECL) as a friend of the
488    CURRENT_CLASS_TYPE.  If DECL is a member function, SCOPE is the
489    class of which it is a member, as named in the friend declaration.
490    If the friend declaration was explicitly namespace-qualified, SCOPE
491    is that namespace.
492    DECLARATOR is the name of the friend.  FUNCDEF_FLAG is true if the
493    friend declaration is a definition of the function.  FLAGS is as
494    for grokclass fn.  */
495 
496 tree
do_friend(tree scope,tree declarator,tree decl,enum overload_flags flags,bool funcdef_flag)497 do_friend (tree scope, tree declarator, tree decl,
498 	   enum overload_flags flags,
499 	   bool funcdef_flag)
500 {
501   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
502 
503   tree ctype = NULL_TREE;
504   tree in_namespace = NULL_TREE;
505   if (!scope)
506     ;
507   else if (MAYBE_CLASS_TYPE_P (scope))
508     ctype = scope;
509   else
510     {
511       gcc_checking_assert (TREE_CODE (scope) == NAMESPACE_DECL);
512       in_namespace = scope;
513     }
514 
515   /* Friend functions are unique, until proved otherwise.  */
516   DECL_UNIQUE_FRIEND_P (decl) = 1;
517 
518   if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
519     error ("friend declaration %qD may not have virt-specifiers",
520 	   decl);
521 
522   if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
523     {
524       declarator = TREE_OPERAND (declarator, 0);
525       if (!identifier_p (declarator))
526 	declarator = OVL_NAME (declarator);
527     }
528 
529   if (ctype)
530     {
531       /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
532 	 the enclosing class.  FRIEND_DEPTH counts the number of template
533 	 headers used for this friend declaration.  TEMPLATE_MEMBER_P is
534 	 true if a template header in FRIEND_DEPTH is intended for
535 	 DECLARATOR.  For example, the code
536 
537 	   template <class T> struct A {
538 	     template <class U> struct B {
539 	       template <class V> template <class W>
540 		 friend void C<V>::f(W);
541 	     };
542 	   };
543 
544 	 will eventually give the following results
545 
546 	 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
547 	 2. FRIEND_DEPTH equals 2 (for `V' and `W').
548 	 3. TEMPLATE_MEMBER_P is true (for `W').  */
549 
550       int class_template_depth = template_class_depth (current_class_type);
551       int friend_depth = current_template_depth - class_template_depth;
552       /* We will figure this out later.  */
553       bool template_member_p = false;
554 
555       tree cname = TYPE_NAME (ctype);
556       if (TREE_CODE (cname) == TYPE_DECL)
557 	cname = DECL_NAME (cname);
558 
559       /* A method friend.  */
560       if (flags == NO_SPECIAL && declarator == cname)
561 	DECL_CXX_CONSTRUCTOR_P (decl) = 1;
562 
563       grokclassfn (ctype, decl, flags);
564 
565       if (friend_depth)
566 	{
567 	  if (!uses_template_parms_level (ctype, class_template_depth
568 						 + friend_depth))
569 	    template_member_p = true;
570 	}
571 
572       /* A nested class may declare a member of an enclosing class
573 	 to be a friend, so we do lookup here even if CTYPE is in
574 	 the process of being defined.  */
575       if (class_template_depth
576 	  || COMPLETE_OR_OPEN_TYPE_P (ctype))
577 	{
578 	  if (DECL_TEMPLATE_INFO (decl))
579 	    /* DECL is a template specialization.  No need to
580 	       build a new TEMPLATE_DECL.  */
581 	    ;
582 	  else if (class_template_depth)
583 	    /* We rely on tsubst_friend_function to check the
584 	       validity of the declaration later.  */
585 	    decl = push_template_decl (decl, /*is_friend=*/true);
586 	  else
587 	    decl = check_classfn (ctype, decl,
588 				  template_member_p
589 				  ? current_template_parms
590 				  : NULL_TREE);
591 
592 	  if ((template_member_p
593 	       /* Always pull out the TEMPLATE_DECL if we have a friend
594 		  template in a class template so that it gets tsubsted
595 		  properly later on (59956).  tsubst_friend_function knows
596 		  how to tell this apart from a member template.  */
597 	       || (class_template_depth && friend_depth))
598 	      && decl && TREE_CODE (decl) == FUNCTION_DECL)
599 	    decl = DECL_TI_TEMPLATE (decl);
600 
601 	  if (decl)
602 	    add_friend (current_class_type, decl, /*complain=*/true);
603 	}
604       else
605 	error ("member %qD declared as friend before type %qT defined",
606 		  decl, ctype);
607     }
608   else
609     {
610       /* Namespace-scope friend function.  */
611       int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
612 
613       if (funcdef_flag)
614 	SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
615 
616       if (! DECL_USE_TEMPLATE (decl))
617 	{
618 	  /* We must check whether the decl refers to template
619 	     arguments before push_template_decl adds a reference to
620 	     the containing template class.  */
621 	  int warn = (warn_nontemplate_friend
622 		      && ! funcdef_flag && ! is_friend_template
623 		      && current_template_parms
624 		      && uses_template_parms (decl));
625 
626 	  if (is_friend_template
627 	      || template_class_depth (current_class_type) != 0)
628 	    /* We can't call pushdecl for a template class, since in
629 	       general, such a declaration depends on template
630 	       parameters.  Instead, we call pushdecl when the class
631 	       is instantiated.  */
632 	    decl = push_template_decl (decl, /*is_friend=*/true);
633 	  else if (current_function_decl && !in_namespace)
634 	    /* pushdecl will check there's a local decl already.  */
635 	    decl = pushdecl (decl, /*hiding=*/true);
636 	  else
637 	    {
638 	      /* We can't use pushdecl, as we might be in a template
639 		 class specialization, and pushdecl will insert an
640 		 unqualified friend decl into the template parameter
641 		 scope, rather than the namespace containing it.  */
642 	      tree ns = decl_namespace_context (decl);
643 
644 	      push_nested_namespace (ns);
645 	      decl = pushdecl_namespace_level (decl, /*hiding=*/true);
646 	      pop_nested_namespace (ns);
647 	    }
648 
649 	  if (warn)
650 	    {
651 	      static int explained;
652 	      bool warned;
653 
654 	      auto_diagnostic_group d;
655 	      warned = warning (OPT_Wnon_template_friend, "friend declaration "
656 				"%q#D declares a non-template function", decl);
657 	      if (! explained && warned)
658 		{
659 		  inform (input_location, "(if this is not what you intended, "
660 			  "make sure the function template has already been "
661 			  "declared and add %<<>%> after the function name "
662 			  "here)");
663 		  explained = 1;
664 		}
665 	    }
666 	}
667 
668       if (decl == error_mark_node)
669 	return error_mark_node;
670 
671       add_friend (current_class_type,
672 		  is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
673 		  /*complain=*/true);
674     }
675 
676   return decl;
677 }
678 
679 #include "gt-cp-friend.h"
680