10Sduke/* Help friends in C++.
213682Sshurailine   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
30Sduke   Free Software Foundation, Inc.
40Sduke
50SdukeThis file is part of GCC.
60Sduke
70SdukeGCC is free software; you can redistribute it and/or modify
80Sdukeit under the terms of the GNU General Public License as published by
90Sdukethe Free Software Foundation; either version 2, or (at your option)
100Sdukeany later version.
110Sduke
120SdukeGCC is distributed in the hope that it will be useful,
130Sdukebut WITHOUT ANY WARRANTY; without even the implied warranty of
140SdukeMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
150SdukeGNU General Public License for more details.
160Sduke
170SdukeYou should have received a copy of the GNU General Public License
180Sdukealong with GCC; see the file COPYING.  If not, write to
192362Sohairthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
202362SohairBoston, MA 02110-1301, USA.  */
212362Sohair
220Sduke#include "config.h"
230Sduke#include "system.h"
240Sduke#include "coretypes.h"
250Sduke#include "tm.h"
260Sduke#include "tree.h"
273303Snaoto#include "rtl.h"
2816226Srgoel#include "expr.h"
290Sduke#include "cp-tree.h"
3016077Snishjain#include "flags.h"
3113682Sshurailine#include "output.h"
3212238Snaoto#include "toplev.h"
330Sduke
340Sduke/* Friend data structures are described in cp-tree.h.  */
350Sduke
360Sduke/* Returns nonzero if SUPPLICANT is a friend of TYPE.  */
370Sduke
380Sdukeint
390Sdukeis_friend (tree type, tree supplicant)
400Sduke{
410Sduke  int declp;
420Sduke  tree list;
430Sduke  tree context;
440Sduke
450Sduke  if (supplicant == NULL_TREE || type == NULL_TREE)
460Sduke    return 0;
470Sduke
480Sduke  declp = DECL_P (supplicant);
490Sduke
500Sduke  if (declp)
510Sduke    /* It's a function decl.  */
520Sduke    {
530Sduke      tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
540Sduke      tree name = DECL_NAME (supplicant);
550Sduke
560Sduke      for (; list ; list = TREE_CHAIN (list))
570Sduke	{
580Sduke	  if (name == FRIEND_NAME (list))
590Sduke	    {
600Sduke	      tree friends = FRIEND_DECLS (list);
610Sduke	      for (; friends ; friends = TREE_CHAIN (friends))
620Sduke		{
630Sduke		  tree friend = TREE_VALUE (friends);
640Sduke
650Sduke		  if (friend == NULL_TREE)
660Sduke		    continue;
6716077Snishjain
6816077Snishjain		  if (supplicant == friend)
6916077Snishjain		    return 1;
7016077Snishjain
7116077Snishjain		  if (is_specialization_of_friend (supplicant, friend))
7216077Snishjain		    return 1;
7316077Snishjain		}
7416077Snishjain	      break;
7516077Snishjain	    }
7616077Snishjain	}
7716077Snishjain    }
789220Snaoto  else
7916077Snishjain    /* It's a type.  */
8016077Snishjain    {
819220Snaoto      if (same_type_p (supplicant, type))
820Sduke	return 1;
830Sduke
840Sduke      list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_MAIN_DECL (type)));
8516077Snishjain      for (; list ; list = TREE_CHAIN (list))
860Sduke	{
870Sduke	  tree t = TREE_VALUE (list);
880Sduke
890Sduke	  if (TREE_CODE (t) == TEMPLATE_DECL ?
900Sduke	      is_specialization_of_friend (TYPE_MAIN_DECL (supplicant), t) :
910Sduke	      same_type_p (supplicant, t))
920Sduke	    return 1;
930Sduke	}
940Sduke    }
950Sduke
960Sduke  if (declp)
970Sduke    {
980Sduke      if (DECL_FUNCTION_MEMBER_P (supplicant))
990Sduke	context = DECL_CONTEXT (supplicant);
1000Sduke      else
1010Sduke	context = NULL_TREE;
1020Sduke    }
1030Sduke  else
1040Sduke    {
1050Sduke      if (TYPE_CLASS_SCOPE_P (supplicant))
1060Sduke	/* Nested classes get the same access as their enclosing types, as
1070Sduke	   per DR 45 (this is a change from the standard).  */
1080Sduke	context = TYPE_CONTEXT (supplicant);
1090Sduke      else
1100Sduke	/* Local classes have the same access as the enclosing function.  */
1110Sduke	context = decl_function_context (TYPE_MAIN_DECL (supplicant));
1120Sduke    }
1130Sduke
1140Sduke  /* A namespace is not friend to anybody.  */
1150Sduke  if (context && TREE_CODE (context) == NAMESPACE_DECL)
1160Sduke    context = NULL_TREE;
1170Sduke
1180Sduke  if (context)
1190Sduke    return is_friend (type, context);
1200Sduke
1210Sduke  return 0;
1220Sduke}
1230Sduke
1240Sduke/* Add a new friend to the friends of the aggregate type TYPE.
1250Sduke   DECL is the FUNCTION_DECL of the friend being added.
1260Sduke
1270Sduke   If COMPLAIN is true, warning about duplicate friend is issued.
1280Sduke   We want to have this diagnostics during parsing but not
1290Sduke   when a template is being instantiated.  */
1300Sduke
1310Sdukevoid
1320Sdukeadd_friend (tree type, tree decl, bool complain)
1330Sduke{
1340Sduke  tree typedecl;
1350Sduke  tree list;
1360Sduke  tree name;
1370Sduke  tree ctx;
1380Sduke
1390Sduke  if (decl == error_mark_node)
1400Sduke    return;
1410Sduke
1420Sduke  typedecl = TYPE_MAIN_DECL (type);
1430Sduke  list = DECL_FRIENDLIST (typedecl);
1440Sduke  name = DECL_NAME (decl);
1450Sduke  type = TREE_TYPE (typedecl);
1460Sduke
1470Sduke  while (list)
1480Sduke    {
1490Sduke      if (name == FRIEND_NAME (list))
1500Sduke	{
1510Sduke	  tree friends = FRIEND_DECLS (list);
1520Sduke	  for (; friends ; friends = TREE_CHAIN (friends))
1530Sduke	    {
1540Sduke	      if (decl == TREE_VALUE (friends))
1550Sduke		{
1560Sduke		  if (complain)
1570Sduke		    warning (0, "%qD is already a friend of class %qT",
1580Sduke			     decl, type);
1590Sduke		  return;
1600Sduke		}
1610Sduke	    }
1620Sduke
1630Sduke	  maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
1640Sduke
1650Sduke	  TREE_VALUE (list) = tree_cons (NULL_TREE, decl,
1660Sduke					 TREE_VALUE (list));
1670Sduke	  return;
1680Sduke	}
1690Sduke      list = TREE_CHAIN (list);
1700Sduke    }
1710Sduke
1720Sduke  ctx = DECL_CONTEXT (decl);
1730Sduke  if (ctx && CLASS_TYPE_P (ctx) && !uses_template_parms (ctx))
1740Sduke    perform_or_defer_access_check (TYPE_BINFO (ctx), decl, decl);
1750Sduke
1760Sduke  maybe_add_class_template_decl_list (type, decl, /*friend_p=*/1);
1770Sduke
1780Sduke  DECL_FRIENDLIST (typedecl)
1790Sduke    = tree_cons (DECL_NAME (decl), build_tree_list (NULL_TREE, decl),
1800Sduke		 DECL_FRIENDLIST (typedecl));
1810Sduke  if (!uses_template_parms (type))
1820Sduke    DECL_BEFRIENDING_CLASSES (decl)
1830Sduke      = tree_cons (NULL_TREE, type,
1840Sduke		   DECL_BEFRIENDING_CLASSES (decl));
1850Sduke}
1860Sduke
1870Sduke/* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
1880Sduke   been defined, we make all of its member functions friends of
1890Sduke   TYPE.  If not, we make it a pending friend, which can later be added
1900Sduke   when its definition is seen.  If a type is defined, then its TYPE_DECL's
1910Sduke   DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
1920Sduke   classes that are not defined.  If a type has not yet been defined,
1930Sduke   then the DECL_WAITING_FRIENDS contains a list of types
1940Sduke   waiting to make it their friend.  Note that these two can both
1950Sduke   be in use at the same time!
1960Sduke
1970Sduke   If COMPLAIN is true, warning about duplicate friend is issued.
1980Sduke   We want to have this diagnostics during parsing but not
19916077Snishjain   when a template is being instantiated.  */
2000Sduke
20116077Snishjainvoid
20216077Snishjainmake_friend_class (tree type, tree friend_type, bool complain)
20316077Snishjain{
2040Sduke  tree classes;
20516077Snishjain
20616077Snishjain  /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
20716077Snishjain     the enclosing class.  FRIEND_DEPTH counts the number of template
2080Sduke     headers used for this friend declaration.  TEMPLATE_MEMBER_P,
20916077Snishjain     defined inside the `if' block for TYPENAME_TYPE case, is true if
21016077Snishjain     a template header in FRIEND_DEPTH is intended for DECLARATOR.
21116077Snishjain     For example, the code
2120Sduke
21316077Snishjain       template <class T> struct A {
21416077Snishjain	 template <class U> struct B {
2150Sduke	   template <class V> template <class W>
2160Sduke	     friend class C<V>::D;
2170Sduke	 };
2180Sduke       };
2190Sduke
2200Sduke     will eventually give the following results
2210Sduke
22216077Snishjain     1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
2230Sduke     2. FRIEND_DEPTH equals 2 (for `V' and `W').
22416077Snishjain     3. TEMPLATE_MEMBER_P is true (for `W').
22516077Snishjain
22616077Snishjain     The friend is a template friend iff FRIEND_DEPTH is nonzero.  */
2270Sduke
22816077Snishjain  int class_template_depth = template_class_depth (type);
22916077Snishjain  int friend_depth = processing_template_decl - class_template_depth;
23016077Snishjain
2310Sduke  if (! IS_AGGR_TYPE (friend_type))
23216077Snishjain    {
2330Sduke      error ("invalid type %qT declared %<friend%>", friend_type);
2340Sduke      return;
2350Sduke    }
2360Sduke
2370Sduke  if (friend_depth)
23816077Snishjain    /* If the TYPE is a template then it makes sense for it to be
2390Sduke       friends with itself; this means that each instantiation is
24016077Snishjain       friends with all other instantiations.  */
2410Sduke    {
2420Sduke      if (CLASS_TYPE_P (friend_type)
2430Sduke	  && CLASSTYPE_TEMPLATE_SPECIALIZATION (friend_type)
2440Sduke	  && uses_template_parms (friend_type))
24516077Snishjain	{
2460Sduke	  /* [temp.friend]
24716077Snishjain	     Friend declarations shall not declare partial
24816077Snishjain	     specializations.  */
24916077Snishjain	  error ("partial specialization %qT declared %<friend%>",
2500Sduke		 friend_type);
25116077Snishjain	  return;
25216077Snishjain	}
2530Sduke    }
2540Sduke  else if (same_type_p (type, friend_type))
2550Sduke    {
2560Sduke      if (complain)
2570Sduke	pedwarn ("class %qT is implicitly friends with itself",
2580Sduke		 type);
2590Sduke      return;
2600Sduke    }
2610Sduke
2620Sduke  /* [temp.friend]
2630Sduke
2640Sduke     A friend of a class or class template can be a function or
2650Sduke     class template, a specialization of a function template or
2660Sduke     class template, or an ordinary (nontemplate) function or
2670Sduke     class.  */
2680Sduke  if (!friend_depth)
26916077Snishjain    ;/* ok */
27016077Snishjain  else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
27116077Snishjain    {
27216077Snishjain      if (TREE_CODE (TYPENAME_TYPE_FULLNAME (friend_type))
27316077Snishjain	  == TEMPLATE_ID_EXPR)
2740Sduke	{
2750Sduke	  /* template <class U> friend class T::X<U>; */
2760Sduke	  /* [temp.friend]
2770Sduke	     Friend declarations shall not declare partial
2780Sduke	     specializations.  */
2790Sduke	  error ("partial specialization %qT declared %<friend%>",
2800Sduke		 friend_type);
2810Sduke	  return;
2820Sduke	}
2830Sduke      else
2840Sduke	{
2850Sduke	  /* We will figure this out later.  */
2860Sduke	  bool template_member_p = false;
2870Sduke
2880Sduke	  tree ctype = TYPE_CONTEXT (friend_type);
2890Sduke	  tree name = TYPE_IDENTIFIER (friend_type);
2900Sduke	  tree decl;
2910Sduke
2920Sduke	  if (!uses_template_parms_level (ctype, class_template_depth
2930Sduke						 + friend_depth))
2940Sduke	    template_member_p = true;
2950Sduke
2960Sduke	  if (class_template_depth)
2970Sduke	    {
2980Sduke	      /* We rely on tsubst_friend_class to check the
2990Sduke		 validity of the declaration later.  */
3000Sduke	      if (template_member_p)
3010Sduke		friend_type
3020Sduke		  = make_unbound_class_template (ctype,
3030Sduke						 name,
3040Sduke						 current_template_parms,
30516077Snishjain						 tf_error);
30616077Snishjain	      else
30716077Snishjain		friend_type
30816077Snishjain		  = make_typename_type (ctype, name, class_type, tf_error);
30916077Snishjain	    }
31016077Snishjain	  else
31116077Snishjain	    {
3120Sduke	      decl = lookup_member (ctype, name, 0, true);
3130Sduke	      if (!decl)
3140Sduke		{
3150Sduke		  error ("%qT is not a member of %qT", name, ctype);
3160Sduke		  return;
31716077Snishjain		}
31816077Snishjain	      if (template_member_p && !DECL_CLASS_TEMPLATE_P (decl))
31916077Snishjain		{
32016077Snishjain		  error ("%qT is not a member class template of %qT",
3210Sduke			 name, ctype);
3220Sduke		  error ("%q+D declared here", decl);
3230Sduke		  return;
3240Sduke		}
3250Sduke	      if (!template_member_p && (TREE_CODE (decl) != TYPE_DECL
3260Sduke					 || !CLASS_TYPE_P (TREE_TYPE (decl))))
32716077Snishjain		{
3280Sduke		  error ("%qT is not a nested class of %qT",
3290Sduke			 name, ctype);
3300Sduke		  error ("%q+D declared here", decl);
3310Sduke		  return;
3320Sduke		}
3330Sduke
33416077Snishjain	      friend_type = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
33516077Snishjain	    }
33616077Snishjain	}
33716077Snishjain    }
3380Sduke  else if (TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
3390Sduke    {
34016077Snishjain      /* template <class T> friend class T; */
3410Sduke      error ("template parameter type %qT declared %<friend%>", friend_type);
34216077Snishjain      return;
34316077Snishjain    }
3440Sduke  else if (!CLASSTYPE_TEMPLATE_INFO (friend_type))
34516077Snishjain    {
3460Sduke      /* template <class T> friend class A; where A is not a template */
3470Sduke      error ("%q#T is not a template", friend_type);
34816077Snishjain      return;
3490Sduke    }
35016077Snishjain  else
35116077Snishjain    /* template <class T> friend class A; where A is a template */
3520Sduke    friend_type = CLASSTYPE_TI_TEMPLATE (friend_type);
35316077Snishjain
3540Sduke  if (friend_type == error_mark_node)
3550Sduke    return;
35616077Snishjain
3570Sduke  /* See if it is already a friend.  */
35816077Snishjain  for (classes = CLASSTYPE_FRIEND_CLASSES (type);
35916077Snishjain       classes;
3600Sduke       classes = TREE_CHAIN (classes))
36116077Snishjain    {
3620Sduke      tree probe = TREE_VALUE (classes);
3630Sduke
36416077Snishjain      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
3650Sduke	{
36616077Snishjain	  if (friend_type == probe)
36716077Snishjain	    {
3680Sduke	      if (complain)
36916077Snishjain		warning (0, "%qD is already a friend of %qT", probe, type);
3700Sduke	      break;
37116077Snishjain	    }
3720Sduke	}
37316077Snishjain      else if (TREE_CODE (probe) != TEMPLATE_DECL)
37416077Snishjain	{
3750Sduke	  if (same_type_p (probe, friend_type))
37616077Snishjain	    {
37716077Snishjain	      if (complain)
3780Sduke		warning (0, "%qT is already a friend of %qT", probe, type);
37916077Snishjain	      break;
38016077Snishjain	    }
3810Sduke	}
38216077Snishjain    }
3830Sduke
3840Sduke  if (!classes)
3850Sduke    {
3860Sduke      maybe_add_class_template_decl_list (type, friend_type, /*friend_p=*/1);
38716077Snishjain
38816077Snishjain      CLASSTYPE_FRIEND_CLASSES (type)
38916077Snishjain	= tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
39016077Snishjain      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
3910Sduke	friend_type = TREE_TYPE (friend_type);
39216077Snishjain      if (!uses_template_parms (type))
3930Sduke	CLASSTYPE_BEFRIENDING_CLASSES (friend_type)
39416077Snishjain	  = tree_cons (NULL_TREE, type,
3950Sduke		       CLASSTYPE_BEFRIENDING_CLASSES (friend_type));
39616077Snishjain    }
3970Sduke}
39816077Snishjain
3990Sduke/* Record DECL (a FUNCTION_DECL) as a friend of the
40016077Snishjain   CURRENT_CLASS_TYPE.  If DECL is a member function, CTYPE is the
4010Sduke   class of which it is a member, as named in the friend declaration.
40216077Snishjain   DECLARATOR is the name of the friend.  FUNCDEF_FLAG is true if the
4030Sduke   friend declaration is a definition of the function.  FLAGS is as
40416077Snishjain   for grokclass fn.  */
4050Sduke
40616077Snishjaintree
4070Sdukedo_friend (tree ctype, tree declarator, tree decl,
4080Sduke	   tree attrlist, enum overload_flags flags,
4090Sduke	   bool funcdef_flag)
4100Sduke{
4110Sduke  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
41216077Snishjain  gcc_assert (!ctype || IS_AGGR_TYPE (ctype));
4130Sduke
41416077Snishjain  /* Every decl that gets here is a friend of something.  */
4150Sduke  DECL_FRIEND_P (decl) = 1;
4160Sduke
4170Sduke  if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
4180Sduke    {
4190Sduke      declarator = TREE_OPERAND (declarator, 0);
4200Sduke      if (is_overloaded_fn (declarator))
4210Sduke	declarator = DECL_NAME (get_first_fn (declarator));
4220Sduke    }
4230Sduke
4240Sduke  if (ctype)
4250Sduke    {
4260Sduke      /* CLASS_TEMPLATE_DEPTH counts the number of template headers for
42716077Snishjain	 the enclosing class.  FRIEND_DEPTH counts the number of template
4280Sduke	 headers used for this friend declaration.  TEMPLATE_MEMBER_P is
4290Sduke	 true if a template header in FRIEND_DEPTH is intended for
43016077Snishjain	 DECLARATOR.  For example, the code
4310Sduke
43216077Snishjain	   template <class T> struct A {
4330Sduke	     template <class U> struct B {
4340Sduke	       template <class V> template <class W>
4350Sduke		 friend void C<V>::f(W);
4360Sduke	     };
43716077Snishjain	   };
4380Sduke
4390Sduke	 will eventually give the following results
44016077Snishjain
4410Sduke	 1. CLASS_TEMPLATE_DEPTH equals 2 (for `T' and `U').
44216077Snishjain	 2. FRIEND_DEPTH equals 2 (for `V' and `W').
4430Sduke	 3. TEMPLATE_MEMBER_P is true (for `W').  */
4440Sduke
4450Sduke      int class_template_depth = template_class_depth (current_class_type);
4465140Syhuang      int friend_depth = processing_template_decl - class_template_depth;
4470Sduke      /* We will figure this out later.  */
4480Sduke      bool template_member_p = false;
4490Sduke
4500Sduke      tree cname = TYPE_NAME (ctype);
4510Sduke      if (TREE_CODE (cname) == TYPE_DECL)
4520Sduke	cname = DECL_NAME (cname);
4530Sduke
4540Sduke      /* A method friend.  */
4550Sduke      if (flags == NO_SPECIAL && declarator == cname)
4560Sduke	DECL_CONSTRUCTOR_P (decl) = 1;
4570Sduke
45816077Snishjain      grokclassfn (ctype, decl, flags);
45916077Snishjain
4600Sduke      if (friend_depth)
46116077Snishjain	{
4620Sduke	  if (!uses_template_parms_level (ctype, class_template_depth
46316077Snishjain						 + friend_depth))
4640Sduke	    template_member_p = true;
4650Sduke	}
46616077Snishjain
46716077Snishjain      /* A nested class may declare a member of an enclosing class
4680Sduke	 to be a friend, so we do lookup here even if CTYPE is in
46916077Snishjain	 the process of being defined.  */
47016077Snishjain      if (class_template_depth
47116077Snishjain	  || COMPLETE_TYPE_P (ctype)
4720Sduke	  || TYPE_BEING_DEFINED (ctype))
47316077Snishjain	{
4740Sduke	  if (DECL_TEMPLATE_INFO (decl))
4750Sduke	    /* DECL is a template specialization.  No need to
4760Sduke	       build a new TEMPLATE_DECL.  */
47716077Snishjain	    ;
4780Sduke	  else if (class_template_depth)
47916077Snishjain	    /* We rely on tsubst_friend_function to check the
48016077Snishjain	       validity of the declaration later.  */
4810Sduke	    decl = push_template_decl_real (decl, /*is_friend=*/true);
48216077Snishjain	  else
48316077Snishjain	    decl = check_classfn (ctype, decl,
4840Sduke				  template_member_p
48516077Snishjain				  ? current_template_parms
4860Sduke				  : NULL_TREE);
4870Sduke
4880Sduke	  if (template_member_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
48916077Snishjain	    decl = DECL_TI_TEMPLATE (decl);
4900Sduke
4910Sduke	  if (decl)
49216226Srgoel	    add_friend (current_class_type, decl, /*complain=*/true);
49316226Srgoel	}
49416077Snishjain      else
4950Sduke	error ("member %qD declared as friend before type %qT defined",
4960Sduke		  decl, ctype);
49716077Snishjain    }
49816077Snishjain  /* A global friend.
4990Sduke     @@ or possibly a friend from a base class ?!?  */
50016077Snishjain  else if (TREE_CODE (decl) == FUNCTION_DECL)
50116077Snishjain    {
50216077Snishjain      int is_friend_template = PROCESSING_REAL_TEMPLATE_DECL_P ();
5030Sduke
50416077Snishjain      /* Friends must all go through the overload machinery,
5050Sduke	 even though they may not technically be overloaded.
5060Sduke
5070Sduke	 Note that because classes all wind up being top-level
50816077Snishjain	 in their scope, their friend wind up in top-level scope as well.  */
5090Sduke      if (funcdef_flag)
51016077Snishjain	SET_DECL_FRIEND_CONTEXT (decl, current_class_type);
51116077Snishjain
5120Sduke      if (! DECL_USE_TEMPLATE (decl))
51316077Snishjain	{
51416077Snishjain	  /* We must check whether the decl refers to template
5150Sduke	     arguments before push_template_decl_real adds a
51616077Snishjain	     reference to the containing template class.  */
5170Sduke	  int warn = (warn_nontemplate_friend
5180Sduke		      && ! funcdef_flag && ! is_friend_template
5190Sduke		      && current_template_parms
5200Sduke		      && uses_template_parms (decl));
5210Sduke
5220Sduke	  if (is_friend_template
5230Sduke	      || template_class_depth (current_class_type) != 0)
5240Sduke	    /* We can't call pushdecl for a template class, since in
5250Sduke	       general, such a declaration depends on template
5260Sduke	       parameters.  Instead, we call pushdecl when the class
5270Sduke	       is instantiated.  */
5280Sduke	    decl = push_template_decl_real (decl, /*is_friend=*/true);
52916077Snishjain	  else if (current_function_decl)
5300Sduke	    /* This must be a local class, so pushdecl will be ok, and
53116077Snishjain	       insert an unqualified friend into the local scope
5320Sduke	       (rather than the containing namespace scope, which the
5330Sduke	       next choice will do).  */
5340Sduke	    decl = pushdecl_maybe_friend (decl, /*is_friend=*/true);
5350Sduke	  else
53616077Snishjain	    {
5370Sduke	      /* We can't use pushdecl, as we might be in a template
53816077Snishjain		 class specialization, and pushdecl will insert an
5390Sduke		 unqualified friend decl into the template parameter
5400Sduke		 scope, rather than the namespace containing it.  */
5410Sduke	      tree ns = decl_namespace_context (decl);
5420Sduke
5430Sduke	      push_nested_namespace (ns);
5440Sduke	      decl = pushdecl_namespace_level (decl, /*is_friend=*/true);
5450Sduke	      pop_nested_namespace (ns);
54616077Snishjain	    }
5470Sduke
54816077Snishjain	  if (warn)
5490Sduke	    {
55016077Snishjain	      static int explained;
5510Sduke	      warning (0, "friend declaration %q#D declares a non-template "
55216077Snishjain		       "function", decl);
5530Sduke	      if (! explained)
5540Sduke		{
5550Sduke		  warning (0, "(if this is not what you intended, make sure "
5560Sduke			   "the function template has already been declared "
5570Sduke			   "and add <> after the function name here) "
5580Sduke			   "-Wno-non-template-friend disables this warning");
5590Sduke		  explained = 1;
5600Sduke		}
5610Sduke	    }
56216077Snishjain	}
5630Sduke
56416077Snishjain      if (decl == error_mark_node)
5650Sduke	return error_mark_node;
5660Sduke
5670Sduke      add_friend (current_class_type,
5680Sduke		  is_friend_template ? DECL_TI_TEMPLATE (decl) : decl,
5690Sduke		  /*complain=*/true);
5700Sduke      DECL_FRIEND_P (decl) = 1;
5710Sduke    }
5720Sduke
5730Sduke  /* Unfortunately, we have to handle attributes here.  Normally we would
5740Sduke     handle them in start_decl_1, but since this is a friend decl start_decl_1
5750Sduke     never gets to see it.  */
5760Sduke
5770Sduke  /* Set attributes here so if duplicate decl, will have proper attributes.  */
5780Sduke  cplus_decl_attributes (&decl, attrlist, 0);
5790Sduke
5800Sduke  return decl;
5810Sduke}
5820Sduke