LCOV - code coverage report
Current view: top level - gcc/cp - friend.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 213 244 87.3 %
Date: 2020-03-28 11:57:23 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

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

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.