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