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