1/* Help friends in C++.
2   Copyright (C) 1997-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along 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 "hash-set.h"
25#include "machmode.h"
26#include "vec.h"
27#include "double-int.h"
28#include "input.h"
29#include "alias.h"
30#include "symtab.h"
31#include "wide-int.h"
32#include "inchash.h"
33#include "tree.h"
34#include "cp-tree.h"
35#include "flags.h"
36
37/* Friend data structures are described in cp-tree.h.  */
38
39/* Returns nonzero if SUPPLICANT is a friend of TYPE.  */
40
41int
42is_friend (tree type, tree supplicant)
43{
44  int declp;
45  tree list;
46  tree context;
47
48  if (supplicant == NULL_TREE || type == NULL_TREE)
49    return 0;
50
51  declp = DECL_P (supplicant);
52
53  if (declp)
54    /* It's a function decl.  */
55    {
56      tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
57      tree name = DECL_NAME (supplicant);
58
59      for (; list ; list = TREE_CHAIN (list))
60	{
61	  if (name == FRIEND_NAME (list))
62	    {
63	      tree friends = FRIEND_DECLS (list);
64	      for (; friends ; friends = TREE_CHAIN (friends))
65		{
66		  tree this_friend = TREE_VALUE (friends);
67
68		  if (this_friend == NULL_TREE)
69		    continue;
70
71		  if (supplicant == this_friend)
72		    return 1;
73
74		  if (is_specialization_of_friend (supplicant, this_friend))
75		    return 1;
76		}
77	      break;
78	    }
79	}
80    }
81  else
82    /* It's a type.  */
83    {
84      if (same_type_p (supplicant, type))
85	return 1;
86
87      list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
88      for (; list ; list = TREE_CHAIN (list))
89	{
90	  tree t = TREE_VALUE (list);
91
92	  if (TREE_CODE (t) == TEMPLATE_DECL ?
93	      is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
94	      same_type_p (supplicant, t))
95	    return 1;
96	}
97    }
98
99  if (declp)
100    {
101      if (DECL_FUNCTION_MEMBER_P (supplicant))
102	context = DECL_CONTEXT (supplicant);
103      else
104	context = NULL_TREE;
105    }
106  else
107    {
108      if (TYPE_CLASS_SCOPE_P (supplicant))
109	/* Nested classes get the same access as their enclosing types, as
110	   per DR 45 (this is a change from the standard).  */
111	context = TYPE_CONTEXT (supplicant);
112      else
113	/* Local classes have the same access as the enclosing function.  */
114	context = decl_function_context (TYPE_MAIN_DECL (supplicant));
115    }
116
117  /* A namespace is not friend to anybody.  */
118  if (context && TREE_CODE (context) == NAMESPACE_DECL)
119    context = NULL_TREE;
120
121  if (context)
122    return is_friend (type, context);
123
124  return 0;
125}
126
127/* Add a new friend to the friends of the aggregate type TYPE.
128   DECL is the FUNCTION_DECL of the friend being added.
129
130   If COMPLAIN is true, warning about duplicate friend is issued.
131   We want to have this diagnostics during parsing but not
132   when a template is being instantiated.  */
133
134void
135add_friend (tree type, tree decl, bool complain)
136{
137  tree typedecl;
138  tree list;
139  tree name;
140  tree ctx;
141
142  if (decl == error_mark_node)
143    return;
144
145  typedecl = TYPE_MAIN_DECL (type);
146  list = DECL_FRIENDLIST (typedecl);
147  name = DECL_NAME (decl);
148  type = TREE_TYPE (typedecl);
149
150  while (list)
151    {
152      if (name == FRIEND_NAME (list))
153	{
154	  tree friends = FRIEND_DECLS (list);
155	  for (; friends ; friends = TREE_CHAIN (friends))
156	    {
157	      if (decl == TREE_VALUE (friends))
158		{
159		  if (complain)
160		    warning (OPT_Wredundant_decls,
161			     "%qD is already a friend of class %qT",
162			     decl, type);
163		  return;
164		}
165	    }
166
167	  maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
168
169	  TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
170					 TREE_VALUE (list));
171	  return;
172	}
173      list = TREE_CHAIN (list);
174    }
175
176  ctx = DECL_CONTEXT (decl);
177  if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
178    perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl,
179				   tf_warning_or_error);
180
181  maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
182
183  DECL_FRIENDLIST (typedecl)
184    = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
185		 DECL_FRIENDLIST (typedecl));
186  if (!uses_template_parms (type))
187    DECL_BEFRIENDING_CLASSES (decl)
188      = tree_cons (NULL_TREE, type,
189		   DECL_BEFRIENDING_CLASSES (decl));
190}
191
192/* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
193   been defined, we make all of its member functions friends of
194   TYPE.  If not, we make it a pending friend, which can later be added
195   when its definition is seen.  If a type is defined, then its TYPE_DECL's
196   DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
197   classes that are not defined.  If a type has not yet been defined,
198   then the DECL_WAITING_FRIENDS contains a list of types
199   waiting to make it their friend.  Note that these two can both
200   be in use at the same time!
201
202   If COMPLAIN is true, warning about duplicate friend is issued.
203   We want to have this diagnostics during parsing but not
204   when a template is being instantiated.  */
205
206void
207make_friend_class (tree type, tree friend_type, bool complain)
208{
209  tree classes;
210
211  /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
212     the enclosing class.  FRIEND_DEPTH counts the number of template
213     headers used for this friend declaration.  TEMPLATE_MEMBER_P,
214     defined inside the `if' block for TYPENAME_TYPE case, is true if
215     a template header in FRIEND_DEPTH is intended for DECLARATOR.
216     For example, the code
217
218       template <class T> struct A {
219	 template <class U> struct B {
220	   template <class V> template <class W>
221	     friend class C<V>::D;
222	 };
223       };
224
225     will eventually give the following results
226
227     1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
228     2. FRIEND_DEPTH equals 2 (for `V' and `W').
229     3. TEMPLATE_MEMBER_P is true (for `W').
230
231     The friend is a template friend iff FRIEND_DEPTH is nonzero.  */
232
233  int class_template_depth = template_class_depth (type);
234  int friend_depth = processing_template_decl - class_template_depth;
235
236  if (! MAYBE_CLASS_TYPE_P (friend_type)
237      && TREE_CODE (friend_type) != TEMPLATE_TEMPLATE_PARM)
238    {
239      /* N1791: If the type specifier in a friend declaration designates a
240	 (possibly cv-qualified) class type, that class is declared as a
241	 friend; otherwise, the friend declaration is ignored.
242
243         So don't complain in C++11 mode.  */
244      if (cxx_dialect < cxx11)
245	pedwarn (input_location, complain ? 0 : OPT_Wpedantic,
246		 "invalid type %qT declared %<friend%>", friend_type);
247      return;
248    }
249
250  friend_type = cv_unqualified (friend_type);
251
252  if (check_for_bare_parameter_packs (friend_type))
253    return;
254
255  if (friend_depth)
256    /* If the TYPE is a template then it makes sense for it to be
257       friends with itself; this means that each instantiation is
258       friends with all other instantiations.  */
259    {
260      if (CLASS_TYPE_P (friend_type)
261	  && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
262	  && uses_template_parms (friend_type))
263	{
264	  /* [temp.friend]
265	     Friend declarations shall not declare partial
266	     specializations.  */
267	  error ("partial specialization %qT declared %<friend%>",
268		 friend_type);
269	  return;
270	}
271    }
272  else if (same_type_p (type, friend_type))
273    {
274      if (complain)
275	warning (0, "class %qT is implicitly friends with itself",
276		 type);
277      return;
278    }
279
280  /* [temp.friend]
281
282     A friend of a class or class template can be a function or
283     class template, a specialization of a function template or
284     class template, or an ordinary (nontemplate) function or
285     class.  */
286  if (!friend_depth)
287    ;/* ok */
288  else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
289    {
290      if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
291	  == TEMPLATE_ID_EXPR)
292	{
293	  /* template <class U> friend class T::X<U>; */
294	  /* [temp.friend]
295	     Friend declarations shall not declare partial
296	     specializations.  */
297	  error ("partial specialization %qT declared %<friend%>",
298		 friend_type);
299	  return;
300	}
301      else
302	{
303	  /* We will figure this out later.  */
304	  bool template_member_p = false;
305
306	  tree ctype = TYPE_CONTEXT (friend_type);
307	  tree name = TYPE_IDENTIFIER (friend_type);
308	  tree decl;
309
310	  if (!uses_template_parms_level (ctype, class_template_depth
311						 + friend_depth))
312	    template_member_p = true;
313
314	  if (class_template_depth)
315	    {
316	      /* We rely on tsubst_friend_class to check the
317		 validity of the declaration later.  */
318	      if (template_member_p)
319		friend_type
320		  = make_unbound_class_template (ctype,
321						 name,
322						 current_template_parms,
323						 tf_error);
324	      else
325		friend_type
326		  = make_typename_type (ctype, name, class_type, tf_error);
327	    }
328	  else
329	    {
330	      decl = lookup_member (ctype, name, 0, true, tf_warning_or_error);
331	      if (!decl)
332		{
333		  error ("%qT is not a member of %qT", name, ctype);
334		  return;
335		}
336	      if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
337		{
338		  error ("%qT is not a member class template of %qT",
339			 name, ctype);
340		  inform (input_location, "%q+D declared here", decl);
341		  return;
342		}
343	      if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
344					 || !CLASS_TYPE_P (TREE_TYPE (decl))))
345		{
346		  error ("%qT is not a nested class of %qT",
347			 name, ctype);
348		  inform (input_location, "%q+D declared here", decl);
349		  return;
350		}
351
352	      friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
353	    }
354	}
355    }
356  else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
357    {
358      /* template <class T> friend class T; */
359      error ("template parameter type %qT declared %<friend%>", friend_type);
360      return;
361    }
362  else if (TREE_CODE (friend_type) == TEMPLATE_TEMPLATE_PARM)
363    friend_type = TYPE_NAME (friend_type);
364  else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
365    {
366      /* template <class T> friend class A; where A is not a template */
367      error ("%q#T is not a template", friend_type);
368      return;
369    }
370  else
371    /* template <class T> friend class A; where A is a template */
372    friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
373
374  if (friend_type == error_mark_node)
375    return;
376
377  /* See if it is already a friend.  */
378  for (classes = CLASSTYPE_FRIEND_CLASSES (type);
379       classes;
380       classes = TREE_CHAIN (classes))
381    {
382      tree probe = TREE_VALUE (classes);
383
384      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
385	{
386	  if (friend_type == probe)
387	    {
388	      if (complain)
389		warning (OPT_Wredundant_decls,
390			 "%qD is already a friend of %qT", probe, type);
391	      break;
392	    }
393	}
394      else if (TREE_CODE (probe) != TEMPLATE_DECL)
395	{
396	  if (same_type_p (probe, friend_type))
397	    {
398	      if (complain)
399		warning (OPT_Wredundant_decls,
400			 "%qT is already a friend of %qT", probe, type);
401	      break;
402	    }
403	}
404    }
405
406  if (!classes)
407    {
408      maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
409
410      CLASSTYPE_FRIEND_CLASSES (type)
411	= tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
412      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
413	friend_type = TREE_TYPE (friend_type);
414      if (!uses_template_parms (type))
415	CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
416	  = tree_cons (NULL_TREE, type,
417		       CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
418    }
419}
420
421/* Record DECL (a FUNCTION_DECL) as a friend of the
422   CURRENT_CLASS_TYPE.  If DECL is a member function, CTYPE is the
423   class of which it is a member, as named in the friend declaration.
424   DECLARATOR is the name of the friend.  FUNCDEF_FLAG is true if the
425   friend declaration is a definition of the function.  FLAGS is as
426   for grokclass fn.  */
427
428tree
429do_friend (tree ctype, tree declarator, tree decl,
430	   tree attrlist, enum overload_flags flags,
431	   bool funcdef_flag)
432{
433  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
434  gcc_assert (!ctype || MAYBE_CLASS_TYPE_P (ctype));
435
436  /* Every decl that gets here is a friend of something.  */
437  DECL_FRIEND_P (decl) = 1;
438
439  if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
440    error ("friend declaration %qD may not have virt-specifiers",
441	   decl);
442
443  /* Unfortunately, we have to handle attributes here.  Normally we would
444     handle them in start_decl_1, but since this is a friend decl start_decl_1
445     never gets to see it.  */
446
447  /* Set attributes here so if duplicate decl, will have proper attributes.  */
448  cplus_decl_attributes (&decl, attrlist, 0);
449
450  if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
451    {
452      declarator = TREE_OPERAND (declarator, 0);
453      if (is_overloaded_fn (declarator))
454	declarator = DECL_NAME (get_first_fn (declarator));
455    }
456
457  if (ctype)
458    {
459      /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
460	 the enclosing class.  FRIEND_DEPTH counts the number of template
461	 headers used for this friend declaration.  TEMPLATE_MEMBER_P is
462	 true if a template header in FRIEND_DEPTH is intended for
463	 DECLARATOR.  For example, the code
464
465	   template <class T> struct A {
466	     template <class U> struct B {
467	       template <class V> template <class W>
468		 friend void C<V>::f(W);
469	     };
470	   };
471
472	 will eventually give the following results
473
474	 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
475	 2. FRIEND_DEPTH equals 2 (for `V' and `W').
476	 3. TEMPLATE_MEMBER_P is true (for `W').  */
477
478      int class_template_depth = template_class_depth (current_class_type);
479      int friend_depth = processing_template_decl - class_template_depth;
480      /* We will figure this out later.  */
481      bool template_member_p = false;
482
483      tree cname = TYPE_NAME (ctype);
484      if (TREE_CODE (cname) == TYPE_DECL)
485	cname = DECL_NAME (cname);
486
487      /* A method friend.  */
488      if (flags == NO_SPECIAL && declarator == cname)
489	DECL_CONSTRUCTOR_P (decl) = 1;
490
491      grokclassfn (ctype, decl, flags);
492
493      if (friend_depth)
494	{
495	  if (!uses_template_parms_level (ctype, class_template_depth
496						 + friend_depth))
497	    template_member_p = true;
498	}
499
500      /* A nested class may declare a member of an enclosing class
501	 to be a friend, so we do lookup here even if CTYPE is in
502	 the process of being defined.  */
503      if (class_template_depth
504	  || COMPLETE_OR_OPEN_TYPE_P (ctype))
505	{
506	  if (DECL_TEMPLATE_INFO (decl))
507	    /* DECL is a template specialization.  No need to
508	       build a new TEMPLATE_DECL.  */
509	    ;
510	  else if (class_template_depth)
511	    /* We rely on tsubst_friend_function to check the
512	       validity of the declaration later.  */
513	    decl = push_template_decl_real (decl, /*is_friend=*/true);
514	  else
515	    decl = check_classfn (ctype, decl,
516				  template_member_p
517				  ? current_template_parms
518				  : NULL_TREE);
519
520	  if ((template_member_p
521	       /* Always pull out the TEMPLATE_DECL if we have a friend
522		  template in a class template so that it gets tsubsted
523		  properly later on (59956).  tsubst_friend_function knows
524		  how to tell this apart from a member template.  */
525	       || (class_template_depth && friend_depth))
526	      && decl && TREE_CODE (decl) == FUNCTION_DECL)
527	    decl = DECL_TI_TEMPLATE (decl);
528
529	  if (decl)
530	    add_friend (current_class_type, decl, /*complain=*/true);
531	}
532      else
533	error ("member %qD declared as friend before type %qT defined",
534		  decl, ctype);
535    }
536  /* A global friend.
537     @@ or possibly a friend from a base class ?!?  */
538  else if (TREE_CODE (decl) == FUNCTION_DECL)
539    {
540      int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
541
542      /* Friends must all go through the overload machinery,
543	 even though they may not technically be overloaded.
544
545	 Note that because classes all wind up being top-level
546	 in their scope, their friend wind up in top-level scope as well.  */
547      if (funcdef_flag)
548	SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
549
550      if (! DECL_USE_TEMPLATE (decl))
551	{
552	  /* We must check whether the decl refers to template
553	     arguments before push_template_decl_real adds a
554	     reference to the containing template class.  */
555	  int warn = (warn_nontemplate_friend
556		      && ! funcdef_flag && ! is_friend_template
557		      && current_template_parms
558		      && uses_template_parms (decl));
559
560	  if (is_friend_template
561	      || template_class_depth (current_class_type) != 0)
562	    /* We can't call pushdecl for a template class, since in
563	       general, such a declaration depends on template
564	       parameters.  Instead, we call pushdecl when the class
565	       is instantiated.  */
566	    decl = push_template_decl_real (decl, /*is_friend=*/true);
567	  else if (current_function_decl)
568	    {
569	      /* This must be a local class.  11.5p11:
570
571		 If a friend declaration appears in a local class (9.8) and
572		 the name specified is an unqualified name, a prior
573		 declaration is looked up without considering scopes that
574		 are outside the innermost enclosing non-class scope. For a
575		 friend function declaration, if there is no prior
576		 declaration, the program is ill-formed.  */
577	      tree t = lookup_name_innermost_nonclass_level (DECL_NAME (decl));
578	      if (t)
579		decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
580	      else
581		{
582		  error ("friend declaration %qD in local class without "
583			 "prior declaration", decl);
584		  return error_mark_node;
585		}
586	    }
587	  else
588	    {
589	      /* We can't use pushdecl, as we might be in a template
590		 class specialization, and pushdecl will insert an
591		 unqualified friend decl into the template parameter
592		 scope, rather than the namespace containing it.  */
593	      tree ns = decl_namespace_context (decl);
594
595	      push_nested_namespace (ns);
596	      decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
597	      pop_nested_namespace (ns);
598	    }
599
600	  if (warn)
601	    {
602	      static int explained;
603	      bool warned;
604
605	      warned = warning (OPT_Wnon_template_friend, "friend declaration "
606				"%q#D declares a non-template function", decl);
607	      if (! explained && warned)
608		{
609		  inform (input_location, "(if this is not what you intended, make sure "
610			  "the function template has already been declared "
611			  "and add <> after the function name here) ");
612		  explained = 1;
613		}
614	    }
615	}
616
617      if (decl == error_mark_node)
618	return error_mark_node;
619
620      add_friend (current_class_type,
621		  is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
622		  /*complain=*/true);
623      DECL_FRIEND_P (decl) = 1;
624    }
625
626  return decl;
627}
628