LCOV - code coverage report
Current view: top level - gcc/cp - search.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.6 % 967 934
Test Date: 2026-06-06 15:39:20 Functions: 100.0 % 68 68
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Breadth-first and depth-first routines for
       2              :    searching multiple-inheritance lattice for GNU C++.
       3              :    Copyright (C) 1987-2026 Free Software Foundation, Inc.
       4              :    Contributed by Michael Tiemann (tiemann@cygnus.com)
       5              : 
       6              : This file is part of GCC.
       7              : 
       8              : GCC is free software; you can redistribute it and/or modify
       9              : it under the terms of the GNU General Public License as published by
      10              : the Free Software Foundation; either version 3, or (at your option)
      11              : any later version.
      12              : 
      13              : GCC is distributed in the hope that it will be useful,
      14              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16              : GNU General Public License for more details.
      17              : 
      18              : You should have received a copy of the GNU General Public License
      19              : along with GCC; see the file COPYING3.  If not see
      20              : <http://www.gnu.org/licenses/>.  */
      21              : 
      22              : /* High-level class interface.  */
      23              : 
      24              : #include "config.h"
      25              : #include "system.h"
      26              : #include "coretypes.h"
      27              : #include "cp-tree.h"
      28              : #include "intl.h"
      29              : #include "toplev.h"
      30              : #include "spellcheck-tree.h"
      31              : #include "stringpool.h"
      32              : #include "attribs.h"
      33              : #include "tree-inline.h"
      34              : #include "contracts.h"
      35              : 
      36              : static int is_subobject_of_p (tree, tree);
      37              : static tree dfs_lookup_base (tree, void *);
      38              : static tree dfs_dcast_hint_pre (tree, void *);
      39              : static tree dfs_dcast_hint_post (tree, void *);
      40              : static tree dfs_debug_mark (tree, void *);
      41              : static int check_hidden_convs (tree, int, int, tree, tree, tree);
      42              : static tree split_conversions (tree, tree, tree, tree);
      43              : static int lookup_conversions_r (tree, int, int, tree, tree, tree *);
      44              : static int look_for_overrides_r (tree, tree);
      45              : static tree lookup_field_r (tree, void *);
      46              : static tree dfs_accessible_post (tree, void *);
      47              : static tree dfs_walk_once_accessible (tree, bool,
      48              :                                       tree (*pre_fn) (tree, void *),
      49              :                                       tree (*post_fn) (tree, void *),
      50              :                                       void *data);
      51              : static tree dfs_access_in_type (tree, void *);
      52              : static access_kind access_in_type (tree, tree);
      53              : static tree dfs_get_pure_virtuals (tree, void *);
      54              : 
      55              : 
      56              : /* Data for lookup_base and its workers.  */
      57              : 
      58              : struct lookup_base_data_s
      59              : {
      60              :   HOST_WIDE_INT offset; /* Offset we want, or -1 if any.  */
      61              :   tree t;               /* type being searched.  */
      62              :   tree base;            /* The base type we're looking for.  */
      63              :   tree binfo;           /* Found binfo.  */
      64              :   bool via_virtual;     /* Found via a virtual path.  */
      65              :   bool ambiguous;       /* Found multiply ambiguous */
      66              :   bool repeated_base;   /* Whether there are repeated bases in the
      67              :                             hierarchy.  */
      68              :   bool want_any;        /* Whether we want any matching binfo.  */
      69              :   bool require_virtual; /* Whether we require a virtual path.  */
      70              : };
      71              : 
      72              : /* Worker function for lookup_base.  See if we've found the desired
      73              :    base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S).  */
      74              : 
      75              : static tree
      76    977569277 : dfs_lookup_base (tree binfo, void *data_)
      77              : {
      78    977569277 :   struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
      79              : 
      80    977569277 :   if (data->offset != -1)
      81              :     {
      82              :       /* We're looking for the type at a particular offset.  */
      83      6687486 :       int comp = compare_tree_int (BINFO_OFFSET (binfo), data->offset);
      84      6687486 :       if (comp > 0)
      85              :         /* Don't bother looking into bases laid out later; even if they
      86              :            do virtually inherit from the base we want, we can get there
      87              :            by another path.  */
      88              :         return dfs_skip_bases;
      89      6544129 :       else if (comp != 0
      90      6550364 :                && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
      91              :         /* Right type, wrong offset.  */
      92              :         return dfs_skip_bases;
      93              :       /* Fall through.  */
      94              :     }
      95              : 
      96    977425917 :   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
      97              :     {
      98    448299454 :       const bool via_virtual
      99    448299454 :         = binfo_via_virtual (binfo, data->t) != NULL_TREE;
     100              : 
     101    448299454 :       if (data->require_virtual && !via_virtual)
     102              :         /* Skip this result if we require virtual inheritance
     103              :            and this is not a virtual base.  */
     104              :         return dfs_skip_bases;
     105              : 
     106    448298778 :       if (!data->binfo)
     107              :         {
     108    448297152 :           data->binfo = binfo;
     109    448297152 :           data->via_virtual = via_virtual;
     110              : 
     111    448297152 :           if (!data->repeated_base)
     112              :             /* If there are no repeated bases, we can stop now.  */
     113              :             return binfo;
     114              : 
     115        12391 :           if (data->want_any && !data->via_virtual)
     116              :             /* If this is a non-virtual base, then we can't do
     117              :                better.  */
     118              :             return binfo;
     119              : 
     120              :           return dfs_skip_bases;
     121              :         }
     122              :       else
     123              :         {
     124         1626 :           gcc_assert (binfo != data->binfo);
     125              : 
     126              :           /* We've found more than one matching binfo.  */
     127         1626 :           if (!data->want_any)
     128              :             {
     129              :               /* This is immediately ambiguous.  */
     130         1530 :               data->binfo = NULL_TREE;
     131         1530 :               data->ambiguous = true;
     132         1530 :               return error_mark_node;
     133              :             }
     134              : 
     135              :           /* Prefer one via a non-virtual path.  */
     136           96 :           if (!via_virtual)
     137              :             {
     138           27 :               data->binfo = binfo;
     139           27 :               data->via_virtual = false;
     140           27 :               return binfo;
     141              :             }
     142              : 
     143              :           /* There must be repeated bases, otherwise we'd have stopped
     144              :              on the first base we found.  */
     145              :           return dfs_skip_bases;
     146              :         }
     147              :     }
     148              : 
     149              :   return NULL_TREE;
     150              : }
     151              : 
     152              : /* This deals with bug PR17314.
     153              : 
     154              :    DECL is a declaration and BINFO represents a class that has attempted (but
     155              :    failed) to access DECL.
     156              : 
     157              :    Examine the parent binfos of BINFO and determine whether any of them had
     158              :    private access to DECL.  If they did, return the parent binfo.  This helps
     159              :    in figuring out the correct error message to show (if the parents had
     160              :    access, it's their fault for not giving sufficient access to BINFO).
     161              : 
     162              :    If no parents had access, return NULL_TREE.  */
     163              : 
     164              : tree
     165         1151 : get_parent_with_private_access (tree decl, tree binfo)
     166              : {
     167              :   /* Only BINFOs should come through here.  */
     168         1151 :   gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
     169              : 
     170         1250 :   tree base_binfo = NULL_TREE;
     171              : 
     172              :   /* Iterate through immediate parent classes.
     173              :      Note that the base list might contain WILDCARD_TYPE_P types, that
     174              :      should be ignored here.  */
     175         1250 :   for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
     176              :     {
     177          281 :       tree base_binfo_type = BINFO_TYPE (base_binfo);
     178              :       /* This parent had private access.  Therefore that's why BINFO can't
     179              :           access DECL.  */
     180          281 :       if (RECORD_OR_UNION_TYPE_P (base_binfo_type)
     181          281 :           && access_in_type (base_binfo_type, decl) == ak_private)
     182              :         return base_binfo;
     183              :     }
     184              : 
     185              :   /* None of the parents had access.  Note: it's impossible for one of the
     186              :      parents to have had public or protected access to DECL, since then
     187              :      BINFO would have been able to access DECL too.  */
     188              :   return NULL_TREE;
     189              : }
     190              : 
     191              : /* Returns true if type BASE is accessible in T.  (BASE is known to be
     192              :    a (possibly non-proper) base class of T.)  If CONSIDER_LOCAL_P is
     193              :    true, consider any special access of the current scope, or access
     194              :    bestowed by friendship.  */
     195              : 
     196              : bool
     197     49481251 : accessible_base_p (tree t, tree base, bool consider_local_p)
     198              : {
     199     49481251 :   tree decl;
     200              : 
     201              :   /* [class.access.base]
     202              : 
     203              :      A base class is said to be accessible if an invented public
     204              :      member of the base class is accessible.
     205              : 
     206              :      If BASE is a non-proper base, this condition is trivially
     207              :      true.  */
     208     49481251 :   if (same_type_p (t, base))
     209              :     return true;
     210              :   /* Rather than inventing a public member, we use the implicit
     211              :      public typedef created in the scope of every class.  */
     212     12358704 :   decl = TYPE_FIELDS (base);
     213    754240063 :   while (!DECL_SELF_REFERENCE_P (decl))
     214    741881359 :     decl = DECL_CHAIN (decl);
     215     12358704 :   while (ANON_AGGR_TYPE_P (t))
     216            0 :     t = TYPE_CONTEXT (t);
     217     12358704 :   return accessible_p (t, decl, consider_local_p);
     218              : }
     219              : 
     220              : /* Lookup BASE in the hierarchy dominated by T.  Do access checking as
     221              :    ACCESS specifies.  Return the binfo we discover.  If KIND_PTR is
     222              :    non-NULL, fill with information about what kind of base we
     223              :    discovered.  If OFFSET is other than -1, only match at that offset.
     224              : 
     225              :    If the base is inaccessible, or ambiguous, then error_mark_node is
     226              :    returned.  If the tf_error bit of COMPLAIN is not set, no error
     227              :    is issued.  */
     228              : 
     229              : tree
     230    848183838 : lookup_base (tree t, tree base, base_access access,
     231              :              base_kind *kind_ptr, tsubst_flags_t complain,
     232              :              HOST_WIDE_INT offset /* = -1 */)
     233              : {
     234    848183838 :   tree binfo;
     235    848183838 :   tree t_binfo;
     236    848183838 :   base_kind bk;
     237              : 
     238              :   /* "Nothing" is definitely not derived from Base.  */
     239    848183838 :   if (t == NULL_TREE)
     240              :     {
     241        11361 :       if (kind_ptr)
     242            0 :         *kind_ptr = bk_not_base;
     243        11361 :       return NULL_TREE;
     244              :     }
     245              : 
     246    848172477 :   if (t == error_mark_node || base == error_mark_node)
     247              :     {
     248            0 :       if (kind_ptr)
     249            0 :         *kind_ptr = bk_not_base;
     250            0 :       return error_mark_node;
     251              :     }
     252    848172477 :   gcc_assert (TYPE_P (base));
     253              : 
     254    848172477 :   if (!TYPE_P (t))
     255              :     {
     256      3560580 :       t_binfo = t;
     257      3560580 :       t = BINFO_TYPE (t);
     258              :     }
     259              :   else
     260              :     {
     261    844611897 :       t = complete_type (TYPE_MAIN_VARIANT (t));
     262    844611897 :       if (dependent_type_p (t))
     263    178377030 :         if (tree open = currently_open_class (t))
     264    844611897 :           t = open;
     265    844611897 :       t_binfo = TYPE_BINFO (t);
     266              :     }
     267              : 
     268    848172477 :   base = TYPE_MAIN_VARIANT (base);
     269              : 
     270              :   /* If BASE is incomplete, it can't be a base of T--and instantiating it
     271              :      might cause an error.  */
     272    848172477 :   if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base))
     273              :     {
     274    831545405 :       struct lookup_base_data_s data;
     275              : 
     276    831545405 :       data.t = t;
     277    831545405 :       data.base = base;
     278    831545405 :       data.binfo = NULL_TREE;
     279    831545405 :       data.ambiguous = data.via_virtual = false;
     280    831545405 :       data.repeated_base = (offset == -1) && CLASSTYPE_REPEATED_BASE_P (t);
     281    831545405 :       data.want_any = access == ba_any;
     282    831545405 :       data.offset = offset;
     283    831545405 :       data.require_virtual = (access & ba_require_virtual);
     284              : 
     285    831545405 :       dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
     286    831545405 :       binfo = data.binfo;
     287              : 
     288    831545405 :       if (!binfo)
     289    383249783 :         bk = data.ambiguous ? bk_ambig : bk_not_base;
     290    448295622 :       else if (binfo == t_binfo)
     291              :         bk = bk_same_type;
     292     78663441 :       else if (data.via_virtual)
     293              :         bk = bk_via_virtual;
     294              :       else
     295     77873939 :         bk = bk_proper_base;
     296              :     }
     297              :   else
     298              :     {
     299              :       binfo = NULL_TREE;
     300              :       bk = bk_not_base;
     301              :     }
     302              : 
     303              :   /* Check that the base is unambiguous and accessible.  */
     304    848172477 :   if (access != ba_any)
     305     17824694 :     switch (bk)
     306              :       {
     307              :       case bk_not_base:
     308              :         break;
     309              : 
     310         1530 :       case bk_ambig:
     311         1530 :         if (complain & tf_error)
     312           97 :           error ("%qT is an ambiguous base of %qT", base, t);
     313         1530 :         binfo = error_mark_node;
     314         1530 :         break;
     315              : 
     316     17813290 :       default:
     317     17813290 :         if ((access & ba_check_bit)
     318              :             /* If BASE is incomplete, then BASE and TYPE are probably
     319              :                the same, in which case BASE is accessible.  If they
     320              :                are not the same, then TYPE is invalid.  In that case,
     321              :                there's no need to issue another error here, and
     322              :                there's no implicit typedef to use in the code that
     323              :                follows, so we skip the check.  */
     324      5645459 :             && COMPLETE_TYPE_P (base)
     325     23458743 :             && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
     326              :           {
     327          851 :             if (complain & tf_error)
     328           48 :               error ("%qT is an inaccessible base of %qT", base, t);
     329          851 :             binfo = error_mark_node;
     330          851 :             bk = bk_inaccessible;
     331              :           }
     332              :         break;
     333              :       }
     334              : 
     335    848172477 :   if (kind_ptr)
     336      4130970 :     *kind_ptr = bk;
     337              : 
     338              :   return binfo;
     339              : }
     340              : 
     341              : /* Data for dcast_base_hint walker.  */
     342              : 
     343              : struct dcast_data_s
     344              : {
     345              :   tree subtype;   /* The base type we're looking for.  */
     346              :   int virt_depth; /* Number of virtual bases encountered from most
     347              :                      derived.  */
     348              :   tree offset;    /* Best hint offset discovered so far.  */
     349              :   bool repeated_base;  /* Whether there are repeated bases in the
     350              :                           hierarchy.  */
     351              : };
     352              : 
     353              : /* Worker for dcast_base_hint.  Search for the base type being cast
     354              :    from.  */
     355              : 
     356              : static tree
     357        11351 : dfs_dcast_hint_pre (tree binfo, void *data_)
     358              : {
     359        11351 :   struct dcast_data_s *data = (struct dcast_data_s *) data_;
     360              : 
     361        11351 :   if (BINFO_VIRTUAL_P (binfo))
     362          300 :     data->virt_depth++;
     363              : 
     364        11351 :   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
     365              :     {
     366         4992 :       if (data->virt_depth)
     367              :         {
     368          239 :           data->offset = ssize_int (-1);
     369          239 :           return data->offset;
     370              :         }
     371         4753 :       if (data->offset)
     372           12 :         data->offset = ssize_int (-3);
     373              :       else
     374         4741 :         data->offset = BINFO_OFFSET (binfo);
     375              : 
     376         4753 :       return data->repeated_base ? dfs_skip_bases : data->offset;
     377              :     }
     378              : 
     379              :   return NULL_TREE;
     380              : }
     381              : 
     382              : /* Worker for dcast_base_hint.  Track the virtual depth.  */
     383              : 
     384              : static tree
     385         1113 : dfs_dcast_hint_post (tree binfo, void *data_)
     386              : {
     387         1113 :   struct dcast_data_s *data = (struct dcast_data_s *) data_;
     388              : 
     389         1113 :   if (BINFO_VIRTUAL_P (binfo))
     390           25 :     data->virt_depth--;
     391              : 
     392         1113 :   return NULL_TREE;
     393              : }
     394              : 
     395              : /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
     396              :    started from is related to the required TARGET type, in order to optimize
     397              :    the inheritance graph search. This information is independent of the
     398              :    current context, and ignores private paths, hence get_base_distance is
     399              :    inappropriate. Return a TREE specifying the base offset, BOFF.
     400              :    BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF,
     401              :       and there are no public virtual SUBTYPE bases.
     402              :    BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases.
     403              :    BOFF == -2, SUBTYPE is not a public base.
     404              :    BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases.  */
     405              : 
     406              : tree
     407         5348 : dcast_base_hint (tree subtype, tree target)
     408              : {
     409         5348 :   struct dcast_data_s data;
     410              : 
     411         5348 :   data.subtype = subtype;
     412         5348 :   data.virt_depth = 0;
     413         5348 :   data.offset = NULL_TREE;
     414         5348 :   data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
     415              : 
     416         5348 :   dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
     417              :                             dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
     418         5348 :   return data.offset ? data.offset : ssize_int (-2);
     419              : }
     420              : 
     421              : /* Search for a member with name NAME in a multiple inheritance
     422              :    lattice specified by TYPE.  If it does not exist, return NULL_TREE.
     423              :    If the member is ambiguously referenced, return `error_mark_node'.
     424              :    Otherwise, return a DECL with the indicated name.  If WANT_TYPE is
     425              :    true, type declarations are preferred.  */
     426              : 
     427              : /* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
     428              :    NAMESPACE_DECL corresponding to the innermost non-block scope.  */
     429              : 
     430              : tree
     431   2787581408 : current_scope (void)
     432              : {
     433              :   /* There are a number of cases we need to be aware of here:
     434              :                          current_class_type     current_function_decl
     435              :      global                     NULL                    NULL
     436              :      fn-local                   NULL                    SET
     437              :      class-local                SET                     NULL
     438              :      class->fn                       SET                     SET
     439              :      fn->class                       SET                     SET
     440              : 
     441              :      Those last two make life interesting.  If we're in a function which is
     442              :      itself inside a class, we need decls to go into the fn's decls (our
     443              :      second case below).  But if we're in a class and the class itself is
     444              :      inside a function, we need decls to go into the decls for the class.  To
     445              :      achieve this last goal, we must see if, when both current_class_ptr and
     446              :      current_function_decl are set, the class was declared inside that
     447              :      function.  If so, we know to put the decls into the class's scope.  */
     448    786363435 :   if (current_function_decl && current_class_type
     449   3319478219 :       && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
     450    518803950 :            && same_type_p (DECL_CONTEXT (current_function_decl),
     451              :                            current_class_type))
     452     44734918 :           || (DECL_FRIEND_CONTEXT (current_function_decl)
     453     17073486 :               && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
     454              :                               current_class_type))))
     455    517710498 :     return current_function_decl;
     456              : 
     457   2269870910 :   if (current_class_type)
     458              :     return current_class_type;
     459              : 
     460   1126677608 :   if (current_function_decl)
     461              :     return current_function_decl;
     462              : 
     463    872210984 :   return current_namespace;
     464              : }
     465              : 
     466              : /* Returns nonzero if we are currently in a function scope.  Note
     467              :    that this function returns zero if we are within a local class, but
     468              :    not within a member function body of the local class.  */
     469              : 
     470              : int
     471    524774888 : at_function_scope_p (void)
     472              : {
     473    524774888 :   tree cs = current_scope ();
     474              :   /* Also check cfun to make sure that we're really compiling
     475              :      this function (as opposed to having set current_function_decl
     476              :      for access checking or some such).  */
     477    524774888 :   return (cs && TREE_CODE (cs) == FUNCTION_DECL
     478    694214235 :           && cfun && cfun->decl == current_function_decl);
     479              : }
     480              : 
     481              : /* Returns true if the innermost active scope is a class scope.  */
     482              : 
     483              : bool
     484    866765116 : at_class_scope_p (void)
     485              : {
     486    866765116 :   tree cs = current_scope ();
     487    866765116 :   return cs && TYPE_P (cs);
     488              : }
     489              : 
     490              : /* Returns true if the innermost active scope is a namespace scope.  */
     491              : 
     492              : bool
     493    598110265 : at_namespace_scope_p (void)
     494              : {
     495    598110265 :   tree cs = current_scope ();
     496    598110265 :   return cs && TREE_CODE (cs) == NAMESPACE_DECL;
     497              : }
     498              : 
     499              : /* Return the scope of DECL, as appropriate when doing name-lookup.  */
     500              : 
     501              : tree
     502   6383244045 : context_for_name_lookup (tree decl)
     503              : {
     504              :   /* [class.union]
     505              : 
     506              :      For the purposes of name lookup, after the anonymous union
     507              :      definition, the members of the anonymous union are considered to
     508              :      have been defined in the scope in which the anonymous union is
     509              :      declared.  */
     510   6383244045 :   tree context = DECL_CONTEXT (decl);
     511              : 
     512  12255117434 :   while (context && TYPE_P (context)
     513   9161506753 :          && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context)))
     514     53044992 :     context = TYPE_CONTEXT (context);
     515   6383244045 :   if (!context)
     516    564415648 :     context = global_namespace;
     517              : 
     518   6383244045 :   return context;
     519              : }
     520              : 
     521              : /* Like the above, but always return a type, because it's simpler for member
     522              :    handling to refer to the anonymous aggr rather than a function.  */
     523              : 
     524              : tree
     525      2367865 : type_context_for_name_lookup (tree decl)
     526              : {
     527      2367865 :   tree context = DECL_P (decl) ? DECL_CONTEXT (decl) : decl;
     528      2367865 :   gcc_checking_assert (CLASS_TYPE_P (context));
     529              : 
     530      2386387 :   while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
     531              :     {
     532        18534 :       tree next = TYPE_CONTEXT (context);
     533        18534 :       if (!TYPE_P (next))
     534              :         break;
     535              :       context = next;
     536              :     }
     537      2367865 :   return context;
     538              : }
     539              : 
     540              : /* Returns true iff DECL is declared in TYPE.  */
     541              : 
     542              : static bool
     543    545975088 : member_declared_in_type (tree decl, tree type)
     544              : {
     545              :   /* A normal declaration obviously counts.  */
     546    545975088 :   if (context_for_name_lookup (decl) == type)
     547              :     return true;
     548              :   /* So does a using or access declaration.  */
     549    165770242 :   if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)
     550    165770242 :       && purpose_member (type, DECL_ACCESS (decl)))
     551              :     return true;
     552              :   return false;
     553              : }
     554              : 
     555              : /* The accessibility routines use BINFO_ACCESS for scratch space
     556              :    during the computation of the accessibility of some declaration.  */
     557              : 
     558              : /* Avoid walking up past a declaration of the member.  */
     559              : 
     560              : static tree
     561    496663155 : dfs_access_in_type_pre (tree binfo, void *data)
     562              : {
     563    496663155 :   tree decl = (tree) data;
     564    496663155 :   tree type = BINFO_TYPE (binfo);
     565    496663155 :   if (member_declared_in_type (decl, type))
     566    415966739 :     return dfs_skip_bases;
     567              :   return NULL_TREE;
     568              : }
     569              : 
     570              : #define BINFO_ACCESS(NODE) \
     571              :   ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
     572              : 
     573              : /* Set the access associated with NODE to ACCESS.  */
     574              : 
     575              : #define SET_BINFO_ACCESS(NODE, ACCESS)                  \
     576              :   ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0),      \
     577              :    (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
     578              : 
     579              : /* Called from access_in_type via dfs_walk.  Calculate the access to
     580              :    DATA (which is really a DECL) in BINFO.  */
     581              : 
     582              : static tree
     583    496663155 : dfs_access_in_type (tree binfo, void *data)
     584              : {
     585    496663155 :   tree decl = (tree) data;
     586    496663155 :   tree type = BINFO_TYPE (binfo);
     587    496663155 :   access_kind access = ak_none;
     588              : 
     589    496663155 :   if (context_for_name_lookup (decl) == type)
     590              :     {
     591              :       /* If we have descended to the scope of DECL, just note the
     592              :          appropriate access.  */
     593    415324710 :       if (TREE_PRIVATE (decl))
     594              :         access = ak_private;
     595    381025740 :       else if (TREE_PROTECTED (decl))
     596              :         access = ak_protected;
     597              :       else
     598    446243539 :         access = ak_public;
     599              :     }
     600              :   else
     601              :     {
     602              :       /* First, check for an access-declaration that gives us more
     603              :          access to the DECL.  */
     604     81338445 :       if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
     605              :         {
     606    120793178 :           tree decl_access = purpose_member (type, DECL_ACCESS (decl));
     607              : 
     608     76677470 :           if (decl_access)
     609              :             {
     610       642029 :               decl_access = TREE_VALUE (decl_access);
     611              : 
     612       642029 :               if (decl_access == access_public_node)
     613              :                 access = ak_public;
     614       236489 :               else if (decl_access == access_protected_node)
     615              :                 access = ak_protected;
     616        42552 :               else if (decl_access == access_private_node)
     617              :                 access = ak_private;
     618              :               else
     619            0 :                 gcc_unreachable ();
     620              :             }
     621              :         }
     622              : 
     623              :       if (!access)
     624              :         {
     625     80696416 :           int i;
     626     80696416 :           tree base_binfo;
     627     80696416 :           vec<tree, va_gc> *accesses;
     628              : 
     629              :           /* Otherwise, scan our baseclasses, and pick the most favorable
     630              :              access.  */
     631     80696416 :           accesses = BINFO_BASE_ACCESSES (binfo);
     632     87176418 :           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
     633              :             {
     634     80086940 :               tree base_access = (*accesses)[i];
     635     80086940 :               access_kind base_access_now = BINFO_ACCESS (base_binfo);
     636              : 
     637     80086940 :               if (base_access_now == ak_none || base_access_now == ak_private)
     638              :                 /* If it was not accessible in the base, or only
     639              :                    accessible as a private member, we can't access it
     640              :                    all.  */
     641              :                 base_access_now = ak_none;
     642     77586937 :               else if (base_access == access_protected_node)
     643              :                 /* Public and protected members in the base become
     644              :                    protected here.  */
     645              :                 base_access_now = ak_protected;
     646     76943958 :               else if (base_access == access_private_node)
     647              :                 /* Public and protected members in the base become
     648              :                    private here.  */
     649              :                 base_access_now = ak_private;
     650              : 
     651              :               /* See if the new access, via this base, gives more
     652              :                  access than our previous best access.  */
     653     75800776 :               if (base_access_now != ak_none
     654     77586937 :                   && (access == ak_none || base_access_now < access))
     655              :                 {
     656     77586516 :                   access = base_access_now;
     657              : 
     658              :                   /* If the new access is public, we can't do better.  */
     659     77586516 :                   if (access == ak_public)
     660              :                     break;
     661              :                 }
     662              :             }
     663              :         }
     664              :     }
     665              : 
     666              :   /* Note the access to DECL in TYPE.  */
     667    496663155 :   SET_BINFO_ACCESS (binfo, access);
     668              : 
     669    496663155 :   return NULL_TREE;
     670              : }
     671              : 
     672              : /* Return the access to DECL in TYPE.  */
     673              : 
     674              : static access_kind
     675    415966306 : access_in_type (tree type, tree decl)
     676              : {
     677    415966306 :   tree binfo = TYPE_BINFO (type);
     678              : 
     679              :   /* We must take into account
     680              : 
     681              :        [class.paths]
     682              : 
     683              :        If a name can be reached by several paths through a multiple
     684              :        inheritance graph, the access is that of the path that gives
     685              :        most access.
     686              : 
     687              :     The algorithm we use is to make a post-order depth-first traversal
     688              :     of the base-class hierarchy.  As we come up the tree, we annotate
     689              :     each node with the most lenient access.  */
     690    415966306 :   dfs_walk_once (binfo, dfs_access_in_type_pre, dfs_access_in_type, decl);
     691              : 
     692    415966306 :   return BINFO_ACCESS (binfo);
     693              : }
     694              : 
     695              : /* Returns nonzero if it is OK to access DECL named in TYPE through an object
     696              :    of OTYPE in the context of DERIVED.  */
     697              : 
     698              : static int
     699      9092184 : protected_accessible_p (tree decl, tree derived, tree type, tree otype)
     700              : {
     701              :   /* We're checking this clause from [class.access.base]
     702              : 
     703              :        m as a member of N is protected, and the reference occurs in a
     704              :        member or friend of class N, or in a member or friend of a
     705              :        class P derived from N, where m as a member of P is public, private
     706              :        or protected.
     707              : 
     708              :     Here DERIVED is a possible P, DECL is m and TYPE is N.  */
     709              : 
     710              :   /* If DERIVED isn't derived from N, then it can't be a P.  */
     711      9092184 :   if (!DERIVED_FROM_P (type, derived))
     712              :     return 0;
     713              : 
     714              :   /* DECL_NONSTATIC_MEMBER_P won't work for USING_DECLs.  */
     715      8988156 :   decl = strip_using_decl (decl);
     716              :   /* We don't expect or support dependent decls.  */
     717      8988156 :   gcc_assert (TREE_CODE (decl) != USING_DECL);
     718              : 
     719              :   /* [class.protected]
     720              : 
     721              :      When a friend or a member function of a derived class references
     722              :      a protected non-static member of a base class, an access check
     723              :      applies in addition to those described earlier in clause
     724              :      _class.access_) Except when forming a pointer to member
     725              :      (_expr.unary.op_), the access must be through a pointer to,
     726              :      reference to, or object of the derived class itself (or any class
     727              :      derived from that class) (_expr.ref_).  If the access is to form
     728              :      a pointer to member, the nested-name-specifier shall name the
     729              :      derived class (or any class derived from that class).  */
     730     14915074 :   if (DECL_NONSTATIC_MEMBER_P (decl)
     731     13430068 :       && !DERIVED_FROM_P (derived, otype))
     732              :     return 0;
     733              : 
     734              :   return 1;
     735              : }
     736              : 
     737              : /* Returns nonzero if SCOPE is a type or a friend of a type which would be able
     738              :    to access DECL through TYPE.  OTYPE is the type of the object.  */
     739              : 
     740              : static int
     741     16011770 : friend_accessible_p (tree scope, tree decl, tree type, tree otype)
     742              : {
     743              :   /* We're checking this clause from [class.access.base]
     744              : 
     745              :        m as a member of N is protected, and the reference occurs in a
     746              :        member or friend of class N, or in a member or friend of a
     747              :        class P derived from N, where m as a member of P is public, private
     748              :        or protected.
     749              : 
     750              :     Here DECL is m and TYPE is N.  SCOPE is the current context,
     751              :     and we check all its possible Ps.  */
     752     16011770 :   tree befriending_classes;
     753     16011770 :   tree t;
     754              : 
     755     16011770 :   if (!scope)
     756              :     return 0;
     757              : 
     758     16011770 :   if (is_global_friend (scope))
     759              :     return 1;
     760              : 
     761              :   /* Is SCOPE itself a suitable P?  */
     762     16011770 :   if (TYPE_P (scope) && protected_accessible_p (decl, scope, type, otype))
     763              :     return 1;
     764              : 
     765      7032816 :   if (DECL_DECLARES_FUNCTION_P (scope))
     766      6950674 :     befriending_classes = DECL_BEFRIENDING_CLASSES (scope);
     767        82142 :   else if (TYPE_P (scope))
     768        80166 :     befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope);
     769              :   else
     770              :     return 0;
     771              : 
     772      7054935 :   for (t = befriending_classes; t; t = TREE_CHAIN (t))
     773        33064 :     if (protected_accessible_p (decl, TREE_VALUE (t), type, otype))
     774              :       return 1;
     775              : 
     776              :   /* Nested classes have the same access as their enclosing types, as
     777              :      per DR 45 (this is a change from C++98).  */
     778      7021871 :   if (TYPE_P (scope))
     779        71737 :     if (friend_accessible_p (TYPE_CONTEXT (scope), decl, type, otype))
     780              :       return 1;
     781              : 
     782      6950968 :   if (DECL_DECLARES_FUNCTION_P (scope))
     783              :     {
     784              :       /* Perhaps this SCOPE is a member of a class which is a
     785              :          friend.  */
     786     13900268 :       if (DECL_CLASS_SCOPE_P (scope)
     787     13899909 :           && friend_accessible_p (DECL_CONTEXT (scope), decl, type, otype))
     788              :         return 1;
     789              :       /* Perhaps SCOPE is a friend function defined inside a class from which
     790              :          DECL is accessible.  */
     791          916 :       if (tree fctx = DECL_FRIEND_CONTEXT (scope))
     792            3 :         if (friend_accessible_p (fctx, decl, type, otype))
     793              :           return 1;
     794              :     }
     795              : 
     796              :   /* Maybe scope's template is a friend.  */
     797         1289 :   if (tree tinfo = get_template_info (scope))
     798              :     {
     799         1033 :       tree tmpl = TI_TEMPLATE (tinfo);
     800         1033 :       if (DECL_CLASS_TEMPLATE_P (tmpl))
     801          773 :         tmpl = TREE_TYPE (tmpl);
     802              :       else
     803          260 :         tmpl = DECL_TEMPLATE_RESULT (tmpl);
     804         1033 :       if (tmpl != scope)
     805              :         {
     806              :           /* Increment processing_template_decl to make sure that
     807              :              dependent_type_p works correctly.  */
     808          889 :           ++processing_template_decl;
     809          889 :           int ret = friend_accessible_p (tmpl, decl, type, otype);
     810          889 :           --processing_template_decl;
     811          889 :           if (ret)
     812              :             return 1;
     813              :         }
     814              :     }
     815              : 
     816              :   /* If is_friend is true, we should have found a befriending class.  */
     817          430 :   gcc_checking_assert (!is_friend (type, scope));
     818              : 
     819              :   return 0;
     820              : }
     821              : 
     822              : struct dfs_accessible_data
     823              : {
     824              :   tree decl;
     825              :   tree object_type;
     826              : };
     827              : 
     828              : /* Avoid walking up past a declaration of the member.  */
     829              : 
     830              : static tree
     831     49311933 : dfs_accessible_pre (tree binfo, void *data)
     832              : {
     833     49311933 :   dfs_accessible_data *d = (dfs_accessible_data *)data;
     834     49311933 :   tree type = BINFO_TYPE (binfo);
     835     49311933 :   if (member_declared_in_type (d->decl, type))
     836     44728649 :     return dfs_skip_bases;
     837              :   return NULL_TREE;
     838              : }
     839              : 
     840              : /* Called via dfs_walk_once_accessible from accessible_p */
     841              : 
     842              : static tree
     843     45248848 : dfs_accessible_post (tree binfo, void *data)
     844              : {
     845              :   /* access_in_type already set BINFO_ACCESS for us.  */
     846     45248848 :   access_kind access = BINFO_ACCESS (binfo);
     847     45248848 :   tree N = BINFO_TYPE (binfo);
     848     45248848 :   dfs_accessible_data *d = (dfs_accessible_data *)data;
     849     45248848 :   tree decl = d->decl;
     850     45248848 :   tree scope = current_nonlambda_scope ();
     851              : 
     852              :   /* A member m is accessible at the point R when named in class N if */
     853     45248848 :   switch (access)
     854              :     {
     855              :     case ak_none:
     856              :       return NULL_TREE;
     857              : 
     858              :     case ak_public:
     859              :       /* m as a member of N is public, or */
     860              :       return binfo;
     861              : 
     862     34341594 :     case ak_private:
     863     34341594 :       {
     864              :         /* m as a member of N is private, and R occurs in a member or friend of
     865              :            class N, or */
     866     34341594 :         if (scope && TREE_CODE (scope) != NAMESPACE_DECL
     867     68681899 :             && is_friend (N, scope))
     868              :           return binfo;
     869              :         return NULL_TREE;
     870              :       }
     871              : 
     872      8989366 :     case ak_protected:
     873      8989366 :       {
     874              :         /* m as a member of N is protected, and R occurs in a member or friend
     875              :            of class N, or in a member or friend of a class P derived from N,
     876              :            where m as a member of P is public, private, or protected  */
     877      8989366 :         if (friend_accessible_p (scope, decl, N, d->object_type))
     878              :           return binfo;
     879              :         return NULL_TREE;
     880              :       }
     881              : 
     882              :     default:
     883              :       gcc_unreachable ();
     884              :     }
     885              : }
     886              : 
     887              : /* Like accessible_p below, but within a template returns true iff DECL is
     888              :    accessible in TYPE to all possible instantiations of the template.  */
     889              : 
     890              : int
     891      4269187 : accessible_in_template_p (tree type, tree decl)
     892              : {
     893      4269187 :   int save_ptd = processing_template_decl;
     894      4269187 :   processing_template_decl = 0;
     895      4269187 :   int val = accessible_p (type, decl, false);
     896      4269187 :   processing_template_decl = save_ptd;
     897      4269187 :   return val;
     898              : }
     899              : 
     900              : /* DECL is a declaration from a base class of TYPE, which was the
     901              :    class used to name DECL.  Return nonzero if, in the current
     902              :    context, DECL is accessible.  If TYPE is actually a BINFO node,
     903              :    then we can tell in what context the access is occurring by looking
     904              :    at the most derived class along the path indicated by BINFO.  If
     905              :    CONSIDER_LOCAL is true, do consider special access the current
     906              :    scope or friendship thereof we might have.  */
     907              : 
     908              : int
     909    415966081 : accessible_p (tree type, tree decl, bool consider_local_p)
     910              : {
     911    415966081 :   tree binfo;
     912    415966081 :   access_kind access;
     913              : 
     914              :   /* If this declaration is in a block or namespace scope, there's no
     915              :      access control.  */
     916    415966081 :   if (!TYPE_P (context_for_name_lookup (decl)))
     917              :     return 1;
     918              : 
     919              :   /* There is no need to perform access checks inside a thunk.  */
     920    415966028 :   if (current_function_decl && DECL_THUNK_P (current_function_decl))
     921              :     return 1;
     922              : 
     923    415966028 :   tree otype = NULL_TREE;
     924    415966028 :   if (!TYPE_P (type))
     925              :     {
     926              :       /* When accessing a non-static member, the most derived type in the
     927              :          binfo chain is the type of the object; remember that type for
     928              :          protected_accessible_p.  */
     929    816637066 :       for (tree b = type; b; b = BINFO_INHERITANCE_CHAIN (b))
     930    417348209 :         otype = BINFO_TYPE (b);
     931    399288857 :       type = BINFO_TYPE (type);
     932              :     }
     933              :   else
     934              :     otype = type;
     935              : 
     936              :   /* Anonymous unions don't have their own access.  */
     937    415966028 :   if (ANON_AGGR_TYPE_P (type))
     938            9 :     type = type_context_for_name_lookup (type);
     939    415966028 :   if (ANON_AGGR_TYPE_P (otype))
     940            9 :     otype = type_context_for_name_lookup (otype);
     941              : 
     942              :   /* [class.access.base]
     943              : 
     944              :      A member m is accessible when named in class N if
     945              : 
     946              :      --m as a member of N is public, or
     947              : 
     948              :      --m as a member of N is private, and the reference occurs in a
     949              :        member or friend of class N, or
     950              : 
     951              :      --m as a member of N is protected, and the reference occurs in a
     952              :        member or friend of class N, or in a member or friend of a
     953              :        class P derived from N, where m as a member of P is public, private or
     954              :        protected, or
     955              : 
     956              :      --there exists a base class B of N that is accessible at the point
     957              :        of reference, and m is accessible when named in class B.
     958              : 
     959              :     We walk the base class hierarchy, checking these conditions.  */
     960              : 
     961              :   /* We walk using TYPE_BINFO (type) because access_in_type will set
     962              :      BINFO_ACCESS on it and its bases.  */
     963    415966028 :   binfo = TYPE_BINFO (type);
     964              : 
     965              :   /* Compute the accessibility of DECL in the class hierarchy
     966              :      dominated by type.  */
     967    415966028 :   access = access_in_type (type, decl);
     968    415966028 :   if (access == ak_public)
     969              :     return 1;
     970              : 
     971              :   /* If we aren't considering the point of reference, only the first bullet
     972              :      applies.  */
     973     44730690 :   if (!consider_local_p)
     974              :     return 0;
     975              : 
     976     44729881 :   dfs_accessible_data d = { decl, otype };
     977              : 
     978              :   /* Walk the hierarchy again, looking for a base class that allows
     979              :      access.  */
     980     44729881 :   return dfs_walk_once_accessible (binfo, /*friends=*/true,
     981              :                                    dfs_accessible_pre,
     982              :                                    dfs_accessible_post, &d)
     983     44729881 :     != NULL_TREE;
     984              : }
     985              : 
     986              : struct lookup_field_info {
     987              :   /* The type in which we're looking.  */
     988              :   tree type;
     989              :   /* The name of the field for which we're looking.  */
     990              :   tree name;
     991              :   /* If non-NULL, the current result of the lookup.  */
     992              :   tree rval;
     993              :   /* The path to RVAL.  */
     994              :   tree rval_binfo;
     995              :   /* If non-NULL, the lookup was ambiguous, and this is a list of the
     996              :      candidates.  */
     997              :   tree ambiguous;
     998              :   /* If nonzero, we are looking for types, not data members.  */
     999              :   int want_type;
    1000              : };
    1001              : 
    1002              : /* True for a class member means that it is shared between all objects
    1003              :    of that class.
    1004              : 
    1005              :    [class.member.lookup]:If the resulting set of declarations are not all
    1006              :    from sub-objects of the same type, or the set has a non-static member
    1007              :    and  includes members from distinct sub-objects, there is an ambiguity
    1008              :    and the program is ill-formed.
    1009              : 
    1010              :    This function checks that T contains no non-static members.  */
    1011              : 
    1012              : bool
    1013     45171356 : shared_member_p (tree t)
    1014              : {
    1015     45171356 :   if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL
    1016     43306642 :       || TREE_CODE (t) == CONST_DECL)
    1017              :     return true;
    1018     43303012 :   if (is_overloaded_fn (t))
    1019              :     {
    1020     69734449 :       for (ovl_iterator iter (get_fns (t)); iter; ++iter)
    1021              :         {
    1022     47067722 :           tree decl = strip_using_decl (*iter);
    1023     47067722 :           if (TREE_CODE (decl) == USING_DECL)
    1024              :             /* Conservatively assume a dependent using-declaration
    1025              :                might resolve to a non-static member.  */
    1026     28878929 :             return false;
    1027     47067719 :           if (DECL_OBJECT_MEMBER_FUNCTION_P (decl))
    1028              :             return false;
    1029              :         }
    1030     14423659 :       return true;
    1031              :     }
    1032              :   return false;
    1033              : }
    1034              : 
    1035              : /* Routine to see if the sub-object denoted by the binfo PARENT can be
    1036              :    found as a base class and sub-object of the object denoted by
    1037              :    BINFO.  */
    1038              : 
    1039              : static int
    1040      4597211 : is_subobject_of_p (tree parent, tree binfo)
    1041              : {
    1042      4597211 :   tree probe;
    1043              : 
    1044     10504441 :   for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
    1045              :     {
    1046      7604947 :       if (probe == binfo)
    1047              :         return 1;
    1048      7564797 :       if (BINFO_VIRTUAL_P (probe))
    1049      1657567 :         return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
    1050      1657567 :                 != NULL_TREE);
    1051              :     }
    1052              :   return 0;
    1053              : }
    1054              : 
    1055              : /* DATA is really a struct lookup_field_info.  Look for a field with
    1056              :    the name indicated there in BINFO.  If this function returns a
    1057              :    non-NULL value it is the result of the lookup.  Called from
    1058              :    lookup_field via breadth_first_search.  */
    1059              : 
    1060              : static tree
    1061   6882447785 : lookup_field_r (tree binfo, void *data)
    1062              : {
    1063   6882447785 :   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
    1064   6882447785 :   tree type = BINFO_TYPE (binfo);
    1065   6882447785 :   tree nval = NULL_TREE;
    1066              : 
    1067              :   /* If this is a dependent base, don't look in it.  */
    1068   6882447785 :   if (BINFO_DEPENDENT_BASE_P (binfo))
    1069              :     return NULL_TREE;
    1070              : 
    1071              :   /* If this base class is hidden by the best-known value so far, we
    1072              :      don't need to look.  */
    1073     71603695 :   if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
    1074   5652191597 :       && !BINFO_VIRTUAL_P (binfo))
    1075              :     return dfs_skip_bases;
    1076              : 
    1077   5528315910 :   nval = get_class_binding (type, lfi->name, lfi->want_type);
    1078              : 
    1079              :   /* If there is no declaration with the indicated name in this type,
    1080              :      then there's nothing to do.  */
    1081   5528315910 :   if (!nval)
    1082   4830664742 :     goto done;
    1083              : 
    1084              :   /* If the lookup already found a match, and the new value doesn't
    1085              :      hide the old one, we might have an ambiguity.  */
    1086    697651168 :   if (lfi->rval_binfo
    1087    697651168 :       && !is_subobject_of_p (lfi->rval_binfo, binfo))
    1088              : 
    1089              :     {
    1090      2278617 :       if (nval == lfi->rval && shared_member_p (nval))
    1091              :         /* The two things are really the same.  */
    1092              :         ;
    1093      2278407 :       else if (is_subobject_of_p (binfo, lfi->rval_binfo))
    1094              :         /* The previous value hides the new one.  */
    1095              :         ;
    1096              :       else
    1097              :         {
    1098              :           /* We have a real ambiguity.  We keep a chain of all the
    1099              :              candidates.  */
    1100       664994 :           if (!lfi->ambiguous && lfi->rval)
    1101              :             {
    1102              :               /* This is the first time we noticed an ambiguity.  Add
    1103              :                  what we previously thought was a reasonable candidate
    1104              :                  to the list.  */
    1105       525807 :               lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE);
    1106       525807 :               TREE_TYPE (lfi->ambiguous) = error_mark_node;
    1107              :             }
    1108              : 
    1109              :           /* Add the new value.  */
    1110       664994 :           if (TREE_CODE (nval) == TREE_LIST)
    1111            7 :             lfi->ambiguous = chainon (nval, lfi->ambiguous);
    1112              :           else
    1113              :             {
    1114       664987 :               lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
    1115       664987 :               TREE_TYPE (lfi->ambiguous) = error_mark_node;
    1116              :             }
    1117              :         }
    1118              :     }
    1119              :   else
    1120              :     {
    1121    695372551 :       if (TREE_CODE (nval) == TREE_LIST)
    1122              :         {
    1123           99 :           lfi->ambiguous = chainon (nval, lfi->ambiguous);
    1124           99 :           lfi->rval = TREE_VALUE (nval);
    1125              :         }
    1126              :       else
    1127    695372452 :         lfi->rval = nval;
    1128    695372551 :       lfi->rval_binfo = binfo;
    1129              :     }
    1130              : 
    1131   5528315910 :  done:
    1132              :   /* Don't look for constructors or destructors in base classes.  */
    1133   5528315910 :   if (IDENTIFIER_CDTOR_P (lfi->name))
    1134              :     return dfs_skip_bases;
    1135              :   return NULL_TREE;
    1136              : }
    1137              : 
    1138              : /* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
    1139              :    BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
    1140              :    FUNCTIONS, and OPTYPE respectively.  */
    1141              : 
    1142              : tree
    1143    220192920 : build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
    1144              : {
    1145    220192920 :   tree baselink;
    1146              : 
    1147    220192920 :   gcc_checking_assert (binfo && access_binfo);
    1148    220192920 :   gcc_assert (OVL_P (functions) || TREE_CODE (functions) == TEMPLATE_ID_EXPR);
    1149    220192920 :   gcc_assert (!optype || TYPE_P (optype));
    1150    220192920 :   gcc_assert (TREE_TYPE (functions));
    1151              : 
    1152    220192920 :   baselink = make_node (BASELINK);
    1153    220192920 :   TREE_TYPE (baselink) = TREE_TYPE (functions);
    1154    220192920 :   BASELINK_BINFO (baselink) = binfo;
    1155    220192920 :   BASELINK_ACCESS_BINFO (baselink) = access_binfo;
    1156    220192920 :   BASELINK_FUNCTIONS (baselink) = functions;
    1157    220192920 :   BASELINK_OPTYPE (baselink) = optype;
    1158              : 
    1159    220192920 :   if (binfo == access_binfo
    1160    429660668 :       && TYPE_BEING_DEFINED (BINFO_TYPE (access_binfo)))
    1161      6319999 :     BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
    1162              : 
    1163    220192920 :   return baselink;
    1164              : }
    1165              : 
    1166              : /* Look for a member named NAME in an inheritance lattice dominated by
    1167              :    XBASETYPE.  If PROTECT is 0 or two, we do not check access.  If it
    1168              :    is 1, we enforce accessibility.  If PROTECT is zero, then, for an
    1169              :    ambiguous lookup, we return NULL.  If PROTECT is 1, we issue error
    1170              :    messages about inaccessible or ambiguous lookup.  If PROTECT is 2,
    1171              :    we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose
    1172              :    TREE_VALUEs are the list of ambiguous candidates.
    1173              : 
    1174              :    WANT_TYPE is 1 when we should only return TYPE_DECLs.
    1175              : 
    1176              :    If nothing can be found return NULL_TREE and do not issue an error.
    1177              : 
    1178              :    If non-NULL, failure information is written back to AFI.  */
    1179              : 
    1180              : tree
    1181   4879617631 : lookup_member (tree xbasetype, tree name, int protect, bool want_type,
    1182              :                tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
    1183              : {
    1184   4879617631 :   tree rval, rval_binfo = NULL_TREE;
    1185   4879617631 :   tree type = NULL_TREE, basetype_path = NULL_TREE;
    1186   4879617631 :   struct lookup_field_info lfi;
    1187              : 
    1188              :   /* rval_binfo is the binfo associated with the found member, note,
    1189              :      this can be set with useful information, even when rval is not
    1190              :      set, because it must deal with ALL members, not just non-function
    1191              :      members.  It is used for ambiguity checking and the hidden
    1192              :      checks.  Whereas rval is only set if a proper (not hidden)
    1193              :      non-function member is found.  */
    1194              : 
    1195   4879617631 :   if (name == error_mark_node
    1196   4879617631 :       || xbasetype == NULL_TREE
    1197   4879617625 :       || xbasetype == error_mark_node)
    1198              :     return NULL_TREE;
    1199              : 
    1200   4879617625 :   gcc_assert (identifier_p (name));
    1201              : 
    1202   4879617625 :   if (TREE_CODE (xbasetype) == TREE_BINFO)
    1203              :     {
    1204    105844558 :       type = BINFO_TYPE (xbasetype);
    1205    105844558 :       basetype_path = xbasetype;
    1206              :     }
    1207              :   else
    1208              :     {
    1209   4773773067 :       if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
    1210              :         return NULL_TREE;
    1211              :       type = xbasetype;
    1212   4879617589 :       xbasetype = NULL_TREE;
    1213              :     }
    1214              : 
    1215   4879617589 :   type = complete_type (type);
    1216              : 
    1217              :   /* Make sure we're looking for a member of the current instantiation in the
    1218              :      right partial specialization.  */
    1219   4879617535 :   if (dependent_type_p (type))
    1220   3115479163 :     if (tree t = currently_open_class (type))
    1221   4879617535 :       type = t;
    1222              : 
    1223   4879617535 :   if (!basetype_path)
    1224   4773772977 :     basetype_path = TYPE_BINFO (type);
    1225              : 
    1226   4773772977 :   if (!basetype_path)
    1227              :     return NULL_TREE;
    1228              : 
    1229   4798988137 :   memset (&lfi, 0, sizeof (lfi));
    1230   4798988137 :   lfi.type = type;
    1231   4798988137 :   lfi.name = name;
    1232   4798988137 :   lfi.want_type = want_type;
    1233   4798988137 :   dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
    1234   4798988137 :   rval = lfi.rval;
    1235   4798988137 :   rval_binfo = lfi.rval_binfo;
    1236   4798988137 :   if (rval_binfo)
    1237    695332364 :     type = BINFO_TYPE (rval_binfo);
    1238              : 
    1239   4798988137 :   if (lfi.ambiguous)
    1240              :     {
    1241       525906 :       if (protect == 0)
    1242              :         return NULL_TREE;
    1243       525906 :       else if (protect == 1)
    1244              :         {
    1245           83 :           if (complain & tf_error)
    1246              :             {
    1247           70 :               auto_diagnostic_group d;
    1248           70 :               error ("request for member %qD is ambiguous", name);
    1249           70 :               print_candidates (input_location, lfi.ambiguous);
    1250           70 :             }
    1251           83 :           return error_mark_node;
    1252              :         }
    1253       525823 :       else if (protect == 2)
    1254              :         return lfi.ambiguous;
    1255              :     }
    1256              : 
    1257   4798462231 :   if (!rval)
    1258              :     return NULL_TREE;
    1259              : 
    1260              :   /* [class.access]
    1261              : 
    1262              :      In the case of overloaded function names, access control is
    1263              :      applied to the function selected by overloaded resolution.
    1264              : 
    1265              :      We cannot check here, even if RVAL is only a single non-static
    1266              :      member function, since we do not know what the "this" pointer
    1267              :      will be.  For:
    1268              : 
    1269              :         class A { protected: void f(); };
    1270              :         class B : public A {
    1271              :           void g(A *p) {
    1272              :             f(); // OK
    1273              :             p->f(); // Not OK.
    1274              :           }
    1275              :         };
    1276              : 
    1277              :     only the first call to "f" is valid.  However, if the function is
    1278              :     static, we can check.  */
    1279    694806458 :   if (protect == 1 && !really_overloaded_fn (rval))
    1280              :     {
    1281    104232633 :       tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval;
    1282    104232633 :       decl = strip_using_decl (decl);
    1283              :       /* A dependent USING_DECL will be checked after tsubsting.  */
    1284    104232633 :       if (TREE_CODE (decl) != USING_DECL
    1285     99121911 :           && !DECL_IOBJ_MEMBER_FUNCTION_P (decl)
    1286    149482411 :           && !perform_or_defer_access_check (basetype_path, decl, decl,
    1287              :                                              complain, afi))
    1288           72 :         return error_mark_node;
    1289              :     }
    1290              : 
    1291    694806386 :   if (is_overloaded_fn (rval)
    1292              :       /* Don't use a BASELINK for class-scope deduction guides since
    1293              :          they're not actually member functions.  */
    1294    694806386 :       && !dguide_name_p (name))
    1295    202930685 :     rval = build_baselink (rval_binfo, basetype_path, rval,
    1296    202930685 :                            (IDENTIFIER_CONV_OP_P (name)
    1297       461094 :                             ? TREE_TYPE (name) : NULL_TREE));
    1298              :   return rval;
    1299              : }
    1300              : 
    1301              : /* Helper class for lookup_member_fuzzy.  */
    1302              : 
    1303          744 : class lookup_field_fuzzy_info
    1304              : {
    1305              :  public:
    1306          744 :   lookup_field_fuzzy_info (bool want_type_p) :
    1307          744 :     m_want_type_p (want_type_p), m_candidates () {}
    1308              : 
    1309              :   void fuzzy_lookup_field (tree type);
    1310              : 
    1311              :   /* If true, we are looking for types, not data members.  */
    1312              :   bool m_want_type_p;
    1313              :   /* The result: a vec of identifiers.  */
    1314              :   auto_vec<tree> m_candidates;
    1315              : };
    1316              : 
    1317              : /* Locate all fields within TYPE, append them to m_candidates.  */
    1318              : 
    1319              : void
    1320          839 : lookup_field_fuzzy_info::fuzzy_lookup_field (tree type)
    1321              : {
    1322          839 :   if (!CLASS_TYPE_P (type))
    1323              :     return;
    1324              : 
    1325         4252 :   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
    1326              :     {
    1327         3419 :       if (m_want_type_p && !DECL_DECLARES_TYPE_P (field))
    1328          146 :         continue;
    1329              : 
    1330         3273 :       if (!DECL_NAME (field))
    1331           72 :         continue;
    1332              : 
    1333         3201 :       if (is_lambda_ignored_entity (field))
    1334           36 :         continue;
    1335              : 
    1336              :       /* Ignore special identifiers with space at the end like cdtor or
    1337              :          conversion op identifiers.  */
    1338         3165 :       if (TREE_CODE (DECL_NAME (field)) == IDENTIFIER_NODE)
    1339         3165 :         if (unsigned int len = IDENTIFIER_LENGTH (DECL_NAME (field)))
    1340         3165 :           if (IDENTIFIER_POINTER (DECL_NAME (field))[len - 1] == ' ')
    1341         1412 :             continue;
    1342              : 
    1343         1753 :       m_candidates.safe_push (DECL_NAME (field));
    1344              :     }
    1345              : }
    1346              : 
    1347              : 
    1348              : /* Helper function for lookup_member_fuzzy, called via dfs_walk_all
    1349              :    DATA is really a lookup_field_fuzzy_info.  Look for a field with
    1350              :    the name indicated there in BINFO.  Gathers pertinent identifiers into
    1351              :    m_candidates.  */
    1352              : 
    1353              : static tree
    1354          839 : lookup_field_fuzzy_r (tree binfo, void *data)
    1355              : {
    1356          839 :   lookup_field_fuzzy_info *lffi = (lookup_field_fuzzy_info *) data;
    1357          839 :   tree type = BINFO_TYPE (binfo);
    1358              : 
    1359          839 :   lffi->fuzzy_lookup_field (type);
    1360              : 
    1361          839 :   return NULL_TREE;
    1362              : }
    1363              : 
    1364              : /* Like lookup_member, but try to find the closest match for NAME,
    1365              :    rather than an exact match, and return an identifier (or NULL_TREE).
    1366              :    Do not complain.  */
    1367              : 
    1368              : tree
    1369          744 : lookup_member_fuzzy (tree xbasetype, tree name, bool want_type_p)
    1370              : {
    1371          744 :   tree type = NULL_TREE, basetype_path = NULL_TREE;
    1372          744 :   class lookup_field_fuzzy_info lffi (want_type_p);
    1373              : 
    1374              :   /* rval_binfo is the binfo associated with the found member, note,
    1375              :      this can be set with useful information, even when rval is not
    1376              :      set, because it must deal with ALL members, not just non-function
    1377              :      members.  It is used for ambiguity checking and the hidden
    1378              :      checks.  Whereas rval is only set if a proper (not hidden)
    1379              :      non-function member is found.  */
    1380              : 
    1381          744 :   if (name == error_mark_node
    1382          744 :       || xbasetype == NULL_TREE
    1383          744 :       || xbasetype == error_mark_node)
    1384              :     return NULL_TREE;
    1385              : 
    1386          744 :   gcc_assert (identifier_p (name));
    1387              : 
    1388          744 :   if (TREE_CODE (xbasetype) == TREE_BINFO)
    1389              :     {
    1390            6 :       type = BINFO_TYPE (xbasetype);
    1391            6 :       basetype_path = xbasetype;
    1392              :     }
    1393              :   else
    1394              :     {
    1395          738 :       if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
    1396              :         return NULL_TREE;
    1397              :       type = xbasetype;
    1398          744 :       xbasetype = NULL_TREE;
    1399              :     }
    1400              : 
    1401          744 :   type = complete_type (type);
    1402              : 
    1403              :   /* Make sure we're looking for a member of the current instantiation in the
    1404              :      right partial specialization.  */
    1405          744 :   if (flag_concepts && dependent_type_p (type))
    1406          131 :     type = currently_open_class (type);
    1407              : 
    1408          744 :   if (!basetype_path)
    1409          738 :     basetype_path = TYPE_BINFO (type);
    1410              : 
    1411          738 :   if (!basetype_path)
    1412              :     return NULL_TREE;
    1413              : 
    1414              :   /* Populate lffi.m_candidates.  */
    1415          738 :   dfs_walk_all (basetype_path, &lookup_field_fuzzy_r, NULL, &lffi);
    1416              : 
    1417          738 :   return find_closest_identifier (name, &lffi.m_candidates);
    1418          744 : }
    1419              : 
    1420              : /* Like lookup_member, except that if we find a function member we
    1421              :    return NULL_TREE.  */
    1422              : 
    1423              : tree
    1424     26268837 : lookup_field (tree xbasetype, tree name, int protect, bool want_type)
    1425              : {
    1426     26268837 :   tree rval = lookup_member (xbasetype, name, protect, want_type,
    1427              :                              tf_warning_or_error);
    1428              : 
    1429              :   /* Ignore functions, but propagate the ambiguity list.  */
    1430     26268837 :   if (!error_operand_p (rval)
    1431     26268837 :       && (rval && BASELINK_P (rval)))
    1432            0 :     return NULL_TREE;
    1433              : 
    1434              :   return rval;
    1435              : }
    1436              : 
    1437              : /* Like lookup_member, except that if we find a non-function member we
    1438              :    return NULL_TREE.  */
    1439              : 
    1440              : tree
    1441    138654734 : lookup_fnfields (tree xbasetype, tree name, int protect,
    1442              :                  tsubst_flags_t complain)
    1443              : {
    1444    138654734 :   tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false,
    1445              :                              complain);
    1446              : 
    1447              :   /* Ignore non-functions, but propagate the ambiguity list.  */
    1448    138654734 :   if (!error_operand_p (rval)
    1449    138654734 :       && (rval && !BASELINK_P (rval)))
    1450            0 :     return NULL_TREE;
    1451              : 
    1452              :   return rval;
    1453              : }
    1454              : 
    1455              : /* DECL is the result of a qualified name lookup.  QUALIFYING_SCOPE is
    1456              :    the class or namespace used to qualify the name.  CONTEXT_CLASS is
    1457              :    the class corresponding to the object in which DECL will be used.
    1458              :    Return a possibly modified version of DECL that takes into account
    1459              :    the CONTEXT_CLASS.
    1460              : 
    1461              :    In particular, consider an expression like `B::m' in the context of
    1462              :    a derived class `D'.  If `B::m' has been resolved to a BASELINK,
    1463              :    then the most derived class indicated by the BASELINK_BINFO will be
    1464              :    `B', not `D'.  This function makes that adjustment.  */
    1465              : 
    1466              : tree
    1467    167800364 : adjust_result_of_qualified_name_lookup (tree decl,
    1468              :                                         tree qualifying_scope,
    1469              :                                         tree context_class)
    1470              : {
    1471    167800364 :   if (!BASELINK_P (decl))
    1472              :     return decl;
    1473              : 
    1474     11971168 :   const bool qualified_p = qualifying_scope != NULL_TREE;
    1475     11971168 :   if (!qualified_p)
    1476          157 :     qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (decl));
    1477              : 
    1478     11971168 :   if (context_class
    1479      8282105 :       && context_class != error_mark_node
    1480      8282096 :       && CLASS_TYPE_P (context_class)
    1481      8282090 :       && CLASS_TYPE_P (qualifying_scope)
    1482     20253258 :       && DERIVED_FROM_P (qualifying_scope, context_class))
    1483              :     {
    1484              :       /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
    1485              :          Because we do not yet know which function will be chosen by
    1486              :          overload resolution, we cannot yet check either accessibility
    1487              :          or ambiguity -- in either case, the choice of a static member
    1488              :          function might make the usage valid.  */
    1489      3385833 :       tree base = lookup_base (context_class, qualifying_scope,
    1490              :                                ba_unique, NULL, tf_none);
    1491      3385833 :       if (base && base != error_mark_node)
    1492              :         {
    1493      3385826 :           BASELINK_ACCESS_BINFO (decl) = base;
    1494      3385826 :           tree decl_binfo
    1495      3385826 :             = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
    1496              :                            ba_unique, NULL, tf_none);
    1497      3385826 :           if (decl_binfo && decl_binfo != error_mark_node)
    1498      3385820 :             BASELINK_BINFO (decl) = decl_binfo;
    1499              :         }
    1500              :     }
    1501              : 
    1502     11971168 :   BASELINK_QUALIFIED_P (decl) = qualified_p;
    1503              : 
    1504     11971168 :   return decl;
    1505              : }
    1506              : 
    1507              : 
    1508              : /* Walk the class hierarchy within BINFO, in a depth-first traversal.
    1509              :    PRE_FN is called in preorder, while POST_FN is called in postorder.
    1510              :    If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
    1511              :    walked.  If PRE_FN or POST_FN returns a different non-NULL value,
    1512              :    that value is immediately returned and the walk is terminated.  One
    1513              :    of PRE_FN and POST_FN can be NULL.  At each node, PRE_FN and
    1514              :    POST_FN are passed the binfo to examine and the caller's DATA
    1515              :    value.  All paths are walked, thus virtual and morally virtual
    1516              :    binfos can be multiply walked.  */
    1517              : 
    1518              : tree
    1519   8458942744 : dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
    1520              :               tree (*post_fn) (tree, void *), void *data)
    1521              : {
    1522   8458942744 :   tree rval;
    1523   8458942744 :   unsigned ix;
    1524   8458942744 :   tree base_binfo;
    1525              : 
    1526              :   /* Call the pre-order walking function.  */
    1527   8458942744 :   if (pre_fn)
    1528              :     {
    1529   8454238635 :       rval = pre_fn (binfo, data);
    1530   8454238635 :       if (rval)
    1531              :         {
    1532   1171833026 :           if (rval == dfs_skip_bases)
    1533    724056460 :             goto skip_bases;
    1534              :           return rval;
    1535              :         }
    1536              :     }
    1537              : 
    1538              :   /* Find the next child binfo to walk.  */
    1539   9546704153 :   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
    1540              :     {
    1541   2343923351 :       rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
    1542   2343923351 :       if (rval)
    1543              :         return rval;
    1544              :     }
    1545              : 
    1546   7926837262 :  skip_bases:
    1547              :   /* Call the post-order walking function.  */
    1548   7926837262 :   if (post_fn)
    1549              :     {
    1550    530844207 :       rval = post_fn (binfo, data);
    1551    530844207 :       gcc_assert (rval != dfs_skip_bases);
    1552              :       return rval;
    1553              :     }
    1554              : 
    1555              :   return NULL_TREE;
    1556              : }
    1557              : 
    1558              : /* Worker for dfs_walk_once.  This behaves as dfs_walk_all, except
    1559              :    that binfos are walked at most once.  */
    1560              : 
    1561              : static tree
    1562      3634218 : dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
    1563              :                  tree (*post_fn) (tree, void *), hash_set<tree> *pset,
    1564              :                  void *data)
    1565              : {
    1566      3634218 :   tree rval;
    1567      3634218 :   unsigned ix;
    1568      3634218 :   tree base_binfo;
    1569              : 
    1570              :   /* Call the pre-order walking function.  */
    1571      3634218 :   if (pre_fn)
    1572              :     {
    1573      3297362 :       rval = pre_fn (binfo, data);
    1574      3297362 :       if (rval)
    1575              :         {
    1576       721437 :           if (rval == dfs_skip_bases)
    1577       206586 :             goto skip_bases;
    1578              : 
    1579              :           return rval;
    1580              :         }
    1581              :     }
    1582              : 
    1583              :   /* Find the next child binfo to walk.  */
    1584      5456875 :   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
    1585              :     {
    1586      3296694 :       if (BINFO_VIRTUAL_P (base_binfo))
    1587      1585615 :         if (pset->add (base_binfo))
    1588       595836 :           continue;
    1589              : 
    1590      2700858 :       rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, pset, data);
    1591      2700858 :       if (rval)
    1592              :         return rval;
    1593              :     }
    1594              : 
    1595      2366767 :  skip_bases:
    1596              :   /* Call the post-order walking function.  */
    1597      2366767 :   if (post_fn)
    1598              :     {
    1599       954461 :       rval = post_fn (binfo, data);
    1600       954461 :       gcc_assert (rval != dfs_skip_bases);
    1601              :       return rval;
    1602              :     }
    1603              : 
    1604              :   return NULL_TREE;
    1605              : }
    1606              : 
    1607              : /* Like dfs_walk_all, except that binfos are not multiply walked.  For
    1608              :    non-diamond shaped hierarchies this is the same as dfs_walk_all.
    1609              :    For diamond shaped hierarchies we must mark the virtual bases, to
    1610              :    avoid multiple walks.  */
    1611              : 
    1612              : tree
    1613   1307176436 : dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
    1614              :                tree (*post_fn) (tree, void *), void *data)
    1615              : {
    1616   1307176436 :   static int active = 0;  /* We must not be called recursively. */
    1617   1307176436 :   tree rval;
    1618              : 
    1619   1307176436 :   gcc_assert (pre_fn || post_fn);
    1620   1307176436 :   gcc_assert (!active);
    1621   1307176436 :   active++;
    1622              : 
    1623   1307176436 :   if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
    1624              :     /* We are not diamond shaped, and therefore cannot encounter the
    1625              :        same binfo twice.  */
    1626   1306243076 :     rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
    1627              :   else
    1628              :     {
    1629       933360 :       hash_set<tree> pset;
    1630       933360 :       rval = dfs_walk_once_r (binfo, pre_fn, post_fn, &pset, data);
    1631       933360 :     }
    1632              : 
    1633   1307176436 :   active--;
    1634              : 
    1635   1307176436 :   return rval;
    1636              : }
    1637              : 
    1638              : /* Worker function for dfs_walk_once_accessible.  Behaves like
    1639              :    dfs_walk_once_r, except (a) FRIENDS_P is true if special
    1640              :    access given by the current context should be considered, (b) ONCE
    1641              :    indicates whether bases should be marked during traversal.  */
    1642              : 
    1643              : static tree
    1644     49323738 : dfs_walk_once_accessible_r (tree binfo, bool friends_p, hash_set<tree> *pset,
    1645              :                             tree (*pre_fn) (tree, void *),
    1646              :                             tree (*post_fn) (tree, void *), void *data)
    1647              : {
    1648     49323738 :   tree rval = NULL_TREE;
    1649     49323738 :   unsigned ix;
    1650     49323738 :   tree base_binfo;
    1651              : 
    1652              :   /* Call the pre-order walking function.  */
    1653     49323738 :   if (pre_fn)
    1654              :     {
    1655     49323738 :       rval = pre_fn (binfo, data);
    1656     49323738 :       if (rval)
    1657              :         {
    1658     44733802 :           if (rval == dfs_skip_bases)
    1659     44728766 :             goto skip_bases;
    1660              : 
    1661              :           return rval;
    1662              :         }
    1663              :     }
    1664              : 
    1665              :   /* Find the next child binfo to walk.  */
    1666      5118987 :   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
    1667              :     {
    1668      4597520 :       bool mark = pset && BINFO_VIRTUAL_P (base_binfo);
    1669              : 
    1670         8458 :       if (mark && pset->contains (base_binfo))
    1671           58 :         continue;
    1672              : 
    1673              :       /* If the base is inherited via private or protected
    1674              :          inheritance, then we can't see it, unless we are a friend of
    1675              :          the current binfo.  */
    1676      4597462 :       if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
    1677              :         {
    1678      1925576 :           tree scope;
    1679      1925576 :           if (!friends_p)
    1680          395 :             continue;
    1681      1925181 :           scope = current_scope ();
    1682      1934151 :           if (!scope
    1683      1925181 :               || TREE_CODE (scope) == NAMESPACE_DECL
    1684      3849714 :               || !is_friend (BINFO_TYPE (binfo), scope))
    1685         8970 :             continue;
    1686              :         }
    1687              : 
    1688      4588097 :       if (mark)
    1689          718 :         pset->add (base_binfo);
    1690              : 
    1691      4588097 :       rval = dfs_walk_once_accessible_r (base_binfo, friends_p, pset,
    1692              :                                          pre_fn, post_fn, data);
    1693      4588097 :       if (rval)
    1694              :         return rval;
    1695              :     }
    1696              : 
    1697     45250233 :  skip_bases:
    1698              :   /* Call the post-order walking function.  */
    1699     45250233 :   if (post_fn)
    1700              :     {
    1701     45249961 :       rval = post_fn (binfo, data);
    1702     45249961 :       gcc_assert (rval != dfs_skip_bases);
    1703              :       return rval;
    1704              :     }
    1705              : 
    1706              :   return NULL_TREE;
    1707              : }
    1708              : 
    1709              : /* Like dfs_walk_once except that only accessible bases are walked.
    1710              :    FRIENDS_P indicates whether friendship of the local context
    1711              :    should be considered when determining accessibility.  */
    1712              : 
    1713              : static tree
    1714     44735641 : dfs_walk_once_accessible (tree binfo, bool friends_p,
    1715              :                             tree (*pre_fn) (tree, void *),
    1716              :                             tree (*post_fn) (tree, void *), void *data)
    1717              : {
    1718     44735641 :   hash_set<tree> *pset = NULL;
    1719     44735641 :   if (CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
    1720         1487 :     pset = new hash_set<tree>;
    1721     44735641 :   tree rval = dfs_walk_once_accessible_r (binfo, friends_p, pset,
    1722              :                                           pre_fn, post_fn, data);
    1723              : 
    1724     44735641 :   if (pset)
    1725         1487 :     delete pset;
    1726     44735641 :   return rval;
    1727              : }
    1728              : 
    1729              : /* Return true iff the code of T is CODE, and it has compatible
    1730              :    type with TYPE.  */
    1731              : 
    1732              : static bool
    1733          448 : matches_code_and_type_p (tree t, enum tree_code code, tree type)
    1734              : {
    1735          448 :   if (TREE_CODE (t) != code)
    1736              :     return false;
    1737          434 :   if (!cxx_types_compatible_p (TREE_TYPE (t), type))
    1738              :     return false;
    1739              :   return true;
    1740              : }
    1741              : 
    1742              : /* Subroutine of direct_accessor_p and reference_accessor_p.
    1743              :    Determine if COMPONENT_REF is a simple field lookup of this->FIELD_DECL.
    1744              :    We expect a tree of the form:
    1745              :              <component_ref:
    1746              :                <indirect_ref:S>
    1747              :                  <nop_expr:P*
    1748              :                    <parm_decl (this)>
    1749              :                  <field_decl (FIELD_DECL)>>>.  */
    1750              : 
    1751              : static bool
    1752          203 : field_access_p (tree component_ref, tree field_decl, tree field_type)
    1753              : {
    1754          203 :   if (!matches_code_and_type_p (component_ref, COMPONENT_REF, field_type))
    1755              :     return false;
    1756              : 
    1757          189 :   tree indirect_ref = TREE_OPERAND (component_ref, 0);
    1758          189 :   if (!INDIRECT_REF_P (indirect_ref))
    1759              :     return false;
    1760              : 
    1761          189 :   tree ptr = STRIP_NOPS (TREE_OPERAND (indirect_ref, 0));
    1762          189 :   if (!is_object_parameter (ptr))
    1763              :     return false;
    1764              : 
    1765              :   /* Must access the correct field.  */
    1766          189 :   if (TREE_OPERAND (component_ref, 1) != field_decl)
    1767              :     return false;
    1768              :   return true;
    1769              : }
    1770              : 
    1771              : /* Subroutine of field_accessor_p.
    1772              : 
    1773              :    Assuming that INIT_EXPR has already had its code and type checked,
    1774              :    determine if it is a simple accessor for FIELD_DECL
    1775              :    (of type FIELD_TYPE).
    1776              : 
    1777              :    Specifically, a simple accessor within struct S of the form:
    1778              :        T get_field () { return m_field; }
    1779              :    should have a constexpr_fn_retval (saved_tree) of the form:
    1780              :          <init_expr:T
    1781              :            <result_decl:T
    1782              :            <nop_expr:T
    1783              :              <component_ref:
    1784              :                <indirect_ref:S>
    1785              :                  <nop_expr:P*
    1786              :                    <parm_decl (this)>
    1787              :                  <field_decl (FIELD_DECL)>>>>>.  */
    1788              : 
    1789              : static bool
    1790          161 : direct_accessor_p (tree init_expr, tree field_decl, tree field_type)
    1791              : {
    1792          161 :   tree result_decl = TREE_OPERAND (init_expr, 0);
    1793          161 :   if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_type))
    1794              :     return false;
    1795              : 
    1796          161 :   tree component_ref = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
    1797          161 :   if (!field_access_p (component_ref, field_decl, field_type))
    1798              :     return false;
    1799              : 
    1800              :   return true;
    1801              : }
    1802              : 
    1803              : /* Subroutine of field_accessor_p.
    1804              : 
    1805              :    Assuming that INIT_EXPR has already had its code and type checked,
    1806              :    determine if it is a "reference" accessor for FIELD_DECL
    1807              :    (of type FIELD_REFERENCE_TYPE).
    1808              : 
    1809              :    Specifically, a simple accessor within struct S of the form:
    1810              :        T& get_field () { return m_field; }
    1811              :    should have a constexpr_fn_retval (saved_tree) of the form:
    1812              :          <init_expr:T&
    1813              :            <result_decl:T&
    1814              :            <nop_expr: T&
    1815              :              <addr_expr: T*
    1816              :                <component_ref:T
    1817              :                  <indirect_ref:S
    1818              :                    <nop_expr
    1819              :                      <parm_decl (this)>>
    1820              :                    <field (FIELD_DECL)>>>>>>.  */
    1821              : static bool
    1822           42 : reference_accessor_p (tree init_expr, tree field_decl, tree field_type,
    1823              :                       tree field_reference_type)
    1824              : {
    1825           42 :   tree result_decl = TREE_OPERAND (init_expr, 0);
    1826           42 :   if (!matches_code_and_type_p (result_decl, RESULT_DECL, field_reference_type))
    1827              :     return false;
    1828              : 
    1829           42 :   tree field_pointer_type = build_pointer_type (field_type);
    1830           42 :   tree addr_expr = STRIP_NOPS (TREE_OPERAND (init_expr, 1));
    1831           42 :   if (!matches_code_and_type_p (addr_expr, ADDR_EXPR, field_pointer_type))
    1832              :     return false;
    1833              : 
    1834           42 :   tree component_ref = STRIP_NOPS (TREE_OPERAND (addr_expr, 0));
    1835              : 
    1836           42 :   if (!field_access_p (component_ref, field_decl, field_type))
    1837              :     return false;
    1838              : 
    1839              :   return true;
    1840              : }
    1841              : 
    1842              : /* Return the class of the `this' or explicit object parameter of FN.  */
    1843              : 
    1844              : static tree
    1845           61 : class_of_object_parm (const_tree fn)
    1846              : {
    1847           61 :   tree fntype = TREE_TYPE (fn);
    1848           61 :   if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
    1849            0 :     return non_reference (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
    1850           61 :   return class_of_this_parm (fntype);
    1851              : }
    1852              : 
    1853              : /* Return true if FN is an accessor method for FIELD_DECL.
    1854              :    i.e. a method of the form { return FIELD; }, with no
    1855              :    conversions.
    1856              : 
    1857              :    If CONST_P, then additionally require that FN be a const
    1858              :    method.  */
    1859              : 
    1860              : static bool
    1861         2417 : field_accessor_p (tree fn, tree field_decl, bool const_p)
    1862              : {
    1863         2417 :   if (TREE_CODE (fn) != FUNCTION_DECL)
    1864              :     return false;
    1865              : 
    1866              :   /* We don't yet support looking up static data, just fields.  */
    1867          648 :   if (TREE_CODE (field_decl) != FIELD_DECL)
    1868              :     return false;
    1869              : 
    1870          639 :   if (!DECL_OBJECT_MEMBER_FUNCTION_P (fn))
    1871              :     return false;
    1872              : 
    1873              :   /* If the field is accessed via a const "this" argument, verify
    1874              :      that the "this" parameter is const.  */
    1875          639 :   if (const_p)
    1876              :     {
    1877           61 :       tree this_class = class_of_object_parm (fn);
    1878           61 :       if (!TYPE_READONLY (this_class))
    1879              :         return false;
    1880              :     }
    1881              : 
    1882          601 :   tree saved_tree = DECL_SAVED_TREE (fn);
    1883              : 
    1884          601 :   if (saved_tree == NULL_TREE)
    1885              :     return false;
    1886              : 
    1887              :   /* Attempt to extract a single return value from the function,
    1888              :      if it has one.  */
    1889          235 :   tree retval = constexpr_fn_retval (saved_tree);
    1890          235 :   if (retval == NULL_TREE || retval == error_mark_node)
    1891              :     return false;
    1892              :   /* Require an INIT_EXPR.  */
    1893          209 :   if (TREE_CODE (retval) != INIT_EXPR)
    1894              :     return false;
    1895          209 :   tree init_expr = retval;
    1896              : 
    1897              :   /* Determine if this is a simple accessor within struct S of the form:
    1898              :        T get_field () { return m_field; }.  */
    1899          209 :   tree field_type = TREE_TYPE (field_decl);
    1900          209 :   if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_type))
    1901          161 :     return direct_accessor_p (init_expr, field_decl, field_type);
    1902              : 
    1903              :   /* Failing that, determine if it is an accessor of the form:
    1904              :        T& get_field () { return m_field; }.  */
    1905           48 :   tree field_reference_type = cp_build_reference_type (field_type, false);
    1906           48 :   if (cxx_types_compatible_p (TREE_TYPE (init_expr), field_reference_type))
    1907           42 :     return reference_accessor_p (init_expr, field_decl, field_type,
    1908           42 :                                  field_reference_type);
    1909              : 
    1910              :   return false;
    1911              : }
    1912              : 
    1913              : /* Callback data for dfs_locate_field_accessor_pre.  */
    1914              : 
    1915              : class locate_field_data
    1916              : {
    1917              : public:
    1918          412 :   locate_field_data (tree field_decl_, bool const_p_)
    1919          412 :   : field_decl (field_decl_), const_p (const_p_) {}
    1920              : 
    1921              :   tree field_decl;
    1922              :   bool const_p;
    1923              : };
    1924              : 
    1925              : /* Return a FUNCTION_DECL that is an "accessor" method for DATA, a FIELD_DECL,
    1926              :    callable via binfo, if one exists, otherwise return NULL_TREE.
    1927              : 
    1928              :    Callback for dfs_walk_once_accessible for use within
    1929              :    locate_field_accessor.  */
    1930              : 
    1931              : static tree
    1932          454 : dfs_locate_field_accessor_pre (tree binfo, void *data)
    1933              : {
    1934          454 :   locate_field_data *lfd = (locate_field_data *)data;
    1935          454 :   tree type = BINFO_TYPE (binfo);
    1936              : 
    1937          454 :   vec<tree, va_gc> *member_vec;
    1938          454 :   tree fn;
    1939          454 :   size_t i;
    1940              : 
    1941          454 :   if (!CLASS_TYPE_P (type))
    1942              :     return NULL_TREE;
    1943              : 
    1944          454 :   member_vec = CLASSTYPE_MEMBER_VEC (type);
    1945          454 :   if (!member_vec)
    1946              :     return NULL_TREE;
    1947              : 
    1948         2594 :   for (i = 0; vec_safe_iterate (member_vec, i, &fn); ++i)
    1949         2417 :     if (fn)
    1950         2417 :       if (field_accessor_p (fn, lfd->field_decl, lfd->const_p))
    1951              :         return fn;
    1952              : 
    1953              :   return NULL_TREE;
    1954              : }
    1955              : 
    1956              : /* Return a FUNCTION_DECL that is an "accessor" method for FIELD_DECL,
    1957              :    callable via BASETYPE_PATH, if one exists, otherwise return NULL_TREE.  */
    1958              : 
    1959              : tree
    1960          412 : locate_field_accessor (tree basetype_path, tree field_decl, bool const_p)
    1961              : {
    1962          412 :   if (TREE_CODE (basetype_path) != TREE_BINFO)
    1963              :     return NULL_TREE;
    1964              : 
    1965              :   /* Walk the hierarchy, looking for a method of some base class that allows
    1966              :      access to the field.  */
    1967          412 :   locate_field_data lfd (field_decl, const_p);
    1968          412 :   return dfs_walk_once_accessible (basetype_path, /*friends=*/true,
    1969              :                                    dfs_locate_field_accessor_pre,
    1970          412 :                                    NULL, &lfd);
    1971              : }
    1972              : 
    1973              : /* Check throw specifier of OVERRIDER is at least as strict as
    1974              :    the one of BASEFN.  This is due to [except.spec]: "If a virtual function
    1975              :    has a non-throwing exception specification, all declarations, including
    1976              :    the definition, of any function that overrides that virtual function in
    1977              :    any derived class shall have a non-throwing exception specification,
    1978              :    unless the overriding function is defined as deleted."  */
    1979              : 
    1980              : bool
    1981      3556378 : maybe_check_overriding_exception_spec (tree overrider, tree basefn)
    1982              : {
    1983      3556378 :   maybe_instantiate_noexcept (basefn);
    1984      3556378 :   maybe_instantiate_noexcept (overrider);
    1985      3556378 :   tree base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn));
    1986      3556378 :   tree over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider));
    1987              : 
    1988      3556378 :   if (DECL_INVALID_OVERRIDER_P (overrider)
    1989              :       /* CWG 1351 added the "unless the overriding function is defined as
    1990              :          deleted" wording.  */
    1991      3556378 :       || DECL_DELETED_FN (overrider))
    1992              :     return true;
    1993              : 
    1994              :   /* Can't check this yet.  Pretend this is fine and let
    1995              :      noexcept_override_late_checks check this later.  */
    1996      2271884 :   if (UNPARSED_NOEXCEPT_SPEC_P (base_throw)
    1997      8100234 :       || UNPARSED_NOEXCEPT_SPEC_P (over_throw))
    1998              :     return true;
    1999              : 
    2000              :   /* We also have to defer checking when we're in a template and couldn't
    2001              :      instantiate & evaluate the noexcept to true/false.  */
    2002      3556363 :   if (processing_template_decl)
    2003            6 :     if ((base_throw
    2004            3 :          && base_throw != noexcept_true_spec
    2005            0 :          && base_throw != noexcept_false_spec)
    2006            6 :         || (over_throw
    2007            6 :             && over_throw != noexcept_true_spec
    2008            6 :             && over_throw != noexcept_false_spec))
    2009              :       return true;
    2010              : 
    2011      3556357 :   if (!comp_except_specs (base_throw, over_throw, ce_derived))
    2012              :     {
    2013           41 :       auto_diagnostic_group d;
    2014           41 :       error ("looser exception specification on overriding virtual function "
    2015              :              "%q+#F", overrider);
    2016           41 :       inform (DECL_SOURCE_LOCATION (basefn),
    2017              :               "overridden function is %q#F", basefn);
    2018           41 :       DECL_INVALID_OVERRIDER_P (overrider) = 1;
    2019           41 :       return false;
    2020           41 :     }
    2021              :   return true;
    2022              : }
    2023              : 
    2024              : /* Check that virtual overrider OVERRIDER is acceptable for base function
    2025              :    BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
    2026              : 
    2027              : static int
    2028      3556423 : check_final_overrider (tree overrider, tree basefn)
    2029              : {
    2030      3556423 :   tree over_type = TREE_TYPE (overrider);
    2031      3556423 :   tree base_type = TREE_TYPE (basefn);
    2032      3556423 :   tree over_return = fndecl_declared_return_type (overrider);
    2033      3556423 :   tree base_return = fndecl_declared_return_type (basefn);
    2034              : 
    2035      3556423 :   int fail = 0;
    2036              : 
    2037      3556423 :   if (DECL_INVALID_OVERRIDER_P (overrider))
    2038              :     return 0;
    2039              : 
    2040      3556417 :   if (same_type_p (base_return, over_return))
    2041              :     /* OK */;
    2042            0 :   else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
    2043          301 :            || (TREE_CODE (base_return) == TREE_CODE (over_return)
    2044          286 :                && INDIRECT_TYPE_P (base_return)))
    2045              :     {
    2046              :       /* Potentially covariant.  */
    2047          286 :       unsigned base_quals, over_quals;
    2048              : 
    2049          286 :       fail = !INDIRECT_TYPE_P (base_return);
    2050          286 :       if (!fail)
    2051              :         {
    2052          286 :           if (cp_type_quals (base_return) != cp_type_quals (over_return))
    2053            0 :             fail = 1;
    2054              : 
    2055          286 :           if (TYPE_REF_P (base_return)
    2056          286 :               && (TYPE_REF_IS_RVALUE (base_return)
    2057           60 :                   != TYPE_REF_IS_RVALUE (over_return)))
    2058              :             fail = 1;
    2059              : 
    2060          286 :           base_return = TREE_TYPE (base_return);
    2061          286 :           over_return = TREE_TYPE (over_return);
    2062              :         }
    2063          286 :       base_quals = cp_type_quals (base_return);
    2064          286 :       over_quals = cp_type_quals (over_return);
    2065              : 
    2066          286 :       if ((base_quals & over_quals) != over_quals)
    2067            3 :         fail = 1;
    2068              : 
    2069          286 :       if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
    2070              :         {
    2071              :           /* Strictly speaking, the standard requires the return type to be
    2072              :              complete even if it only differs in cv-quals, but that seems
    2073              :              like a bug in the wording.  */
    2074          277 :           if (!same_type_ignoring_top_level_qualifiers_p (base_return,
    2075              :                                                           over_return))
    2076              :             {
    2077          267 :               tree binfo = lookup_base (over_return, base_return,
    2078              :                                         ba_check, NULL, tf_none);
    2079              : 
    2080          267 :               if (!binfo || binfo == error_mark_node)
    2081              :                 fail = 1;
    2082              :             }
    2083              :         }
    2084            9 :       else if (can_convert_standard (TREE_TYPE (base_type),
    2085            9 :                                      TREE_TYPE (over_type),
    2086              :                                      tf_warning_or_error))
    2087              :         /* GNU extension, allow trivial pointer conversions such as
    2088              :            converting to void *, or qualification conversion.  */
    2089              :         {
    2090            0 :           auto_diagnostic_group d;
    2091            0 :           if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0,
    2092              :                        "invalid covariant return type for %q#D", overrider))
    2093            0 :             inform (DECL_SOURCE_LOCATION (basefn),
    2094              :                     "overridden function is %q#D", basefn);
    2095            0 :         }
    2096              :       else
    2097              :         fail = 2;
    2098              :     }
    2099              :   else
    2100              :     fail = 2;
    2101          262 :   if (!fail)
    2102              :     /* OK */;
    2103              :   else
    2104              :     {
    2105           45 :       auto_diagnostic_group d;
    2106           45 :       if (fail == 1)
    2107           21 :         error ("invalid covariant return type for %q+#D", overrider);
    2108              :       else
    2109           24 :         error ("conflicting return type specified for %q+#D", overrider);
    2110           45 :       inform (DECL_SOURCE_LOCATION (basefn),
    2111              :               "overridden function is %q#D", basefn);
    2112           45 :       DECL_INVALID_OVERRIDER_P (overrider) = 1;
    2113           45 :       return 0;
    2114           45 :     }
    2115              : 
    2116      3556372 :   if (!maybe_check_overriding_exception_spec (overrider, basefn))
    2117              :     return 0;
    2118              : 
    2119              :   /* Check for conflicting type attributes.  But leave transaction_safe for
    2120              :      set_one_vmethod_tm_attributes.  */
    2121      3556331 :   if (!comp_type_attributes (over_type, base_type)
    2122           63 :       && !tx_safe_fn_type_p (base_type)
    2123      3556338 :       && !tx_safe_fn_type_p (over_type))
    2124              :     {
    2125            0 :       auto_diagnostic_group d;
    2126            0 :       error ("conflicting type attributes specified for %q+#D", overrider);
    2127            0 :       inform (DECL_SOURCE_LOCATION (basefn),
    2128              :               "overridden function is %q#D", basefn);
    2129            0 :       DECL_INVALID_OVERRIDER_P (overrider) = 1;
    2130            0 :       return 0;
    2131            0 :     }
    2132              : 
    2133              :   /* A class with a consteval virtual function that overrides a virtual
    2134              :      function that is not consteval shall have consteval-only type (CWG 3117).
    2135              :      A consteval virtual function shall not be overridden by a virtual
    2136              :      function that is not consteval.  */
    2137      7112662 :   if ((DECL_IMMEDIATE_FUNCTION_P (basefn)
    2138           70 :        && !DECL_IMMEDIATE_FUNCTION_P (overrider))
    2139      3556361 :       || (!DECL_IMMEDIATE_FUNCTION_P (basefn)
    2140      7112592 :           && DECL_IMMEDIATE_FUNCTION_P (overrider)
    2141          402 :           && !consteval_only_p (overrider)))
    2142              :     {
    2143            9 :       auto_diagnostic_group d;
    2144           18 :       if (DECL_IMMEDIATE_FUNCTION_P (overrider))
    2145            4 :         error ("%<consteval%> function %q+D overriding non-%<consteval%> "
    2146              :                "function", overrider);
    2147              :       else
    2148            5 :         error ("non-%<consteval%> function %q+D overriding %<consteval%> "
    2149              :                "function", overrider);
    2150            9 :       inform (DECL_SOURCE_LOCATION (basefn),
    2151              :               "overridden function is %qD", basefn);
    2152            9 :       DECL_INVALID_OVERRIDER_P (overrider) = 1;
    2153            9 :       return 0;
    2154            9 :     }
    2155              : 
    2156              :   /* A function declared transaction_safe_dynamic that overrides a function
    2157              :      declared transaction_safe (but not transaction_safe_dynamic) is
    2158              :      ill-formed.  */
    2159      3556322 :   if (tx_safe_fn_type_p (base_type)
    2160           72 :       && lookup_attribute ("transaction_safe_dynamic",
    2161           72 :                            DECL_ATTRIBUTES (overrider))
    2162      3556335 :       && !lookup_attribute ("transaction_safe_dynamic",
    2163           13 :                             DECL_ATTRIBUTES (basefn)))
    2164              :     {
    2165            1 :       auto_diagnostic_group d;
    2166            1 :       error_at (DECL_SOURCE_LOCATION (overrider),
    2167              :                 "%qD declared %<transaction_safe_dynamic%>", overrider);
    2168            1 :       inform (DECL_SOURCE_LOCATION (basefn),
    2169              :               "overriding %qD declared %<transaction_safe%>", basefn);
    2170            1 :     }
    2171              : 
    2172      3556322 :   if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
    2173              :     {
    2174           15 :       if (DECL_DELETED_FN (overrider))
    2175              :         {
    2176           12 :           auto_diagnostic_group d;
    2177           12 :           error ("deleted function %q+D overriding non-deleted function",
    2178              :                  overrider);
    2179           12 :           inform (DECL_SOURCE_LOCATION (basefn),
    2180              :                   "overridden function is %qD", basefn);
    2181           12 :           maybe_explain_implicit_delete (overrider);
    2182           12 :         }
    2183              :       else
    2184              :         {
    2185            3 :           auto_diagnostic_group d;
    2186            3 :           error ("non-deleted function %q+D overriding deleted function",
    2187              :                  overrider);
    2188            3 :           inform (DECL_SOURCE_LOCATION (basefn),
    2189              :                   "overridden function is %qD", basefn);
    2190            3 :         }
    2191           15 :       return 0;
    2192              :     }
    2193              : 
    2194      3556307 :   if (DECL_FINAL_P (basefn))
    2195              :     {
    2196            6 :       auto_diagnostic_group d;
    2197            6 :       error ("virtual function %q+D overriding final function", overrider);
    2198            6 :       inform (DECL_SOURCE_LOCATION (basefn),
    2199              :               "overridden function is %qD", basefn);
    2200            6 :       return 0;
    2201            6 :     }
    2202              :   return 1;
    2203              : }
    2204              : 
    2205              : /* Given a class TYPE, and a function decl FNDECL, look for
    2206              :    virtual functions in TYPE's hierarchy which FNDECL overrides.
    2207              :    We do not look in TYPE itself, only its bases.
    2208              : 
    2209              :    Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
    2210              :    find that it overrides anything.
    2211              : 
    2212              :    We check that every function which is overridden, is correctly
    2213              :    overridden.  */
    2214              : 
    2215              : int
    2216     17901068 : look_for_overrides (tree type, tree fndecl)
    2217              : {
    2218     17901068 :   tree binfo = TYPE_BINFO (type);
    2219     17901068 :   tree base_binfo;
    2220     17901068 :   int ix;
    2221     17901068 :   int found = 0;
    2222              : 
    2223              :   /* A constructor for a class T does not override a function T
    2224              :      in a base class.  */
    2225     35802136 :   if (DECL_CONSTRUCTOR_P (fndecl))
    2226              :     return 0;
    2227              : 
    2228     26677948 :   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
    2229              :     {
    2230      8776880 :       tree basetype = BINFO_TYPE (base_binfo);
    2231              : 
    2232      8776880 :       if (TYPE_POLYMORPHIC_P (basetype))
    2233      5322219 :         found += look_for_overrides_r (basetype, fndecl);
    2234              :     }
    2235              :   return found;
    2236              : }
    2237              : 
    2238              : /* Look in TYPE for virtual functions with the same signature as
    2239              :    FNDECL.  */
    2240              : 
    2241              : tree
    2242     25628861 : look_for_overrides_here (tree type, tree fndecl)
    2243              : {
    2244     25628861 :   tree ovl = get_class_binding (type, DECL_NAME (fndecl));
    2245              : 
    2246     26480067 :   for (ovl_iterator iter (ovl); iter; ++iter)
    2247              :     {
    2248     19760851 :       tree fn = *iter;
    2249              : 
    2250     19760851 :       if (!DECL_VIRTUAL_P (fn))
    2251              :         /* Not a virtual.  */;
    2252     19669810 :       else if (DECL_CONTEXT (fn) != type)
    2253              :         /* Introduced with a using declaration.  */;
    2254     19669650 :       else if (DECL_STATIC_FUNCTION_P (fndecl)
    2255     19669650 :                || DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
    2256              :         {
    2257           28 :           tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2258           28 :           tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    2259           28 :           dtypes = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl) ? TREE_CHAIN (dtypes)
    2260              :                                                         : dtypes;
    2261           28 :           if (compparms (TREE_CHAIN (btypes), dtypes))
    2262     19288778 :             return fn;
    2263              :         }
    2264     19669622 :       else if (same_signature_p (fndecl, fn))
    2265              :         return fn;
    2266              :     }
    2267              : 
    2268      6340083 :   return NULL_TREE;
    2269              : }
    2270              : 
    2271              : /* Look in TYPE for virtual functions overridden by FNDECL. Check both
    2272              :    TYPE itself and its bases.  */
    2273              : 
    2274              : static int
    2275      5322219 : look_for_overrides_r (tree type, tree fndecl)
    2276              : {
    2277      5322219 :   tree fn = look_for_overrides_here (type, fndecl);
    2278      5322219 :   if (fn)
    2279              :     {
    2280      3556445 :       if (DECL_STATIC_FUNCTION_P (fndecl))
    2281              :         {
    2282              :           /* A static member function cannot match an inherited
    2283              :              virtual member function.  */
    2284            6 :           auto_diagnostic_group d;
    2285            6 :           error ("%q+#D cannot be declared", fndecl);
    2286            6 :           error ("  since %q+#D declared in base class", fn);
    2287            6 :         }
    2288      3556439 :       else if (DECL_XOBJ_MEMBER_FUNCTION_P (fndecl))
    2289              :         {
    2290           16 :           auto_diagnostic_group d;
    2291           16 :           error_at (DECL_SOURCE_LOCATION (fndecl),
    2292              :                     "explicit object member function "
    2293              :                     "overrides virtual function");
    2294           16 :           inform (DECL_SOURCE_LOCATION (fn),
    2295              :                   "virtual function declared here");
    2296           16 :         }
    2297              :       else
    2298              :         {
    2299              :           /* It's definitely virtual, even if not explicitly set.  */
    2300      3556423 :           DECL_VIRTUAL_P (fndecl) = 1;
    2301      3556423 :           check_final_overrider (fndecl, fn);
    2302              :         }
    2303      3556445 :       return 1;
    2304              :     }
    2305              : 
    2306              :   /* We failed to find one declared in this class. Look in its bases.  */
    2307      1765774 :   return look_for_overrides (type, fndecl);
    2308              : }
    2309              : 
    2310              : /* Called via dfs_walk from dfs_get_pure_virtuals.  */
    2311              : 
    2312              : static tree
    2313      5040965 : dfs_get_pure_virtuals (tree binfo, void *data)
    2314              : {
    2315      5040965 :   tree type = (tree) data;
    2316              : 
    2317              :   /* We're not interested in primary base classes; the derived class
    2318              :      of which they are a primary base will contain the information we
    2319              :      need.  */
    2320      5040965 :   if (!BINFO_PRIMARY_P (binfo))
    2321              :     {
    2322      2431353 :       tree virtuals;
    2323              : 
    2324      2431353 :       for (virtuals = BINFO_VIRTUALS (binfo);
    2325     12124395 :            virtuals;
    2326      9693042 :            virtuals = TREE_CHAIN (virtuals))
    2327      9693042 :         if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
    2328       642565 :           vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals));
    2329              :     }
    2330              : 
    2331      5040965 :   return NULL_TREE;
    2332              : }
    2333              : 
    2334              : /* Set CLASSTYPE_PURE_VIRTUALS for TYPE.  */
    2335              : 
    2336              : void
    2337      1572596 : get_pure_virtuals (tree type)
    2338              : {
    2339              :   /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
    2340              :      is going to be overridden.  */
    2341      1572596 :   CLASSTYPE_PURE_VIRTUALS (type) = NULL;
    2342              :   /* Now, run through all the bases which are not primary bases, and
    2343              :      collect the pure virtual functions.  We look at the vtable in
    2344              :      each class to determine what pure virtual functions are present.
    2345              :      (A primary base is not interesting because the derived class of
    2346              :      which it is a primary base will contain vtable entries for the
    2347              :      pure virtuals in the base class.  */
    2348      1572596 :   dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
    2349      1572596 : }
    2350              : 
    2351              : /* Debug info for C++ classes can get very large; try to avoid
    2352              :    emitting it everywhere.
    2353              : 
    2354              :    Note that this optimization wins even when the target supports
    2355              :    BINCL (if only slightly), and reduces the amount of work for the
    2356              :    linker.  */
    2357              : 
    2358              : void
    2359     50238655 : maybe_suppress_debug_info (tree t)
    2360              : {
    2361     50238655 :   if (write_symbols == NO_DEBUG)
    2362              :     return;
    2363              : 
    2364              :   /* We might have set this earlier in cp_finish_decl.  */
    2365     47259547 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
    2366              : 
    2367              :   /* Always emit the information for each class every time. */
    2368     47259547 :   if (flag_emit_class_debug_always)
    2369              :     return;
    2370              : 
    2371              :   /* If we already know how we're handling this class, handle debug info
    2372              :      the same way.  */
    2373     47259547 :   if (CLASSTYPE_INTERFACE_KNOWN (t))
    2374              :     {
    2375            1 :       if (CLASSTYPE_INTERFACE_ONLY (t))
    2376            1 :         TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
    2377              :       /* else don't set it.  */
    2378              :     }
    2379              :   /* If the class has a vtable, write out the debug info along with
    2380              :      the vtable.  */
    2381     47259546 :   else if (TYPE_CONTAINS_VPTR_P (t))
    2382      1675493 :     TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
    2383              : 
    2384              :   /* Otherwise, just emit the debug info normally.  */
    2385              : }
    2386              : 
    2387              : /* Note that we want debugging information for a base class of a class
    2388              :    whose vtable is being emitted.  Normally, this would happen because
    2389              :    calling the constructor for a derived class implies calling the
    2390              :    constructors for all bases, which involve initializing the
    2391              :    appropriate vptr with the vtable for the base class; but in the
    2392              :    presence of optimization, this initialization may be optimized
    2393              :    away, so we tell finish_vtable_vardecl that we want the debugging
    2394              :    information anyway.  */
    2395              : 
    2396              : static tree
    2397       842435 : dfs_debug_mark (tree binfo, void * /*data*/)
    2398              : {
    2399       842435 :   tree t = BINFO_TYPE (binfo);
    2400              : 
    2401       842435 :   if (CLASSTYPE_DEBUG_REQUESTED (t))
    2402              :     return dfs_skip_bases;
    2403              : 
    2404       512870 :   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
    2405              : 
    2406       512870 :   return NULL_TREE;
    2407              : }
    2408              : 
    2409              : /* Write out the debugging information for TYPE, whose vtable is being
    2410              :    emitted.  Also walk through our bases and note that we want to
    2411              :    write out information for them.  This avoids the problem of not
    2412              :    writing any debug info for intermediate basetypes whose
    2413              :    constructors, and thus the references to their vtables, and thus
    2414              :    the vtables themselves, were optimized away.  */
    2415              : 
    2416              : void
    2417       381707 : note_debug_info_needed (tree type)
    2418              : {
    2419       381707 :   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
    2420              :     {
    2421       336863 :       TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0;
    2422       336863 :       rest_of_type_compilation (type, namespace_bindings_p ());
    2423              :     }
    2424              : 
    2425       381707 :   dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
    2426       381707 : }
    2427              : 
    2428              : /* Helper for lookup_conversions_r.  TO_TYPE is the type converted to
    2429              :    by a conversion op in base BINFO.  VIRTUAL_DEPTH is nonzero if
    2430              :    BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
    2431              :    bases have been encountered already in the tree walk.  PARENT_CONVS
    2432              :    is the list of lists of conversion functions that could hide CONV
    2433              :    and OTHER_CONVS is the list of lists of conversion functions that
    2434              :    could hide or be hidden by CONV, should virtualness be involved in
    2435              :    the hierarchy.  Merely checking the conversion op's name is not
    2436              :    enough because two conversion operators to the same type can have
    2437              :    different names.  Return nonzero if we are visible.  */
    2438              : 
    2439              : static int
    2440     39814851 : check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
    2441              :                     tree to_type, tree parent_convs, tree other_convs)
    2442              : {
    2443     39814851 :   tree level, probe;
    2444              : 
    2445              :   /* See if we are hidden by a parent conversion.  */
    2446     39816953 :   for (level = parent_convs; level; level = TREE_CHAIN (level))
    2447        34477 :     for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
    2448        32375 :       if (same_type_p (to_type, TREE_TYPE (probe)))
    2449              :         return 0;
    2450              : 
    2451     39794764 :   if (virtual_depth || virtualness)
    2452              :     {
    2453              :      /* In a virtual hierarchy, we could be hidden, or could hide a
    2454              :         conversion function on the other_convs list.  */
    2455        93442 :       for (level = other_convs; level; level = TREE_CHAIN (level))
    2456              :         {
    2457         1940 :           int we_hide_them;
    2458         1940 :           int they_hide_us;
    2459         1940 :           tree *prev, other;
    2460              : 
    2461         1940 :           if (!(virtual_depth || TREE_STATIC (level)))
    2462              :             /* Neither is morally virtual, so cannot hide each other.  */
    2463            0 :             continue;
    2464              : 
    2465         1940 :           if (!TREE_VALUE (level))
    2466              :             /* They evaporated away already.  */
    2467            0 :             continue;
    2468              : 
    2469         3880 :           they_hide_us = (virtual_depth
    2470         1940 :                           && original_binfo (binfo, TREE_PURPOSE (level)));
    2471            6 :           we_hide_them = (!they_hide_us && TREE_STATIC (level)
    2472            6 :                           && original_binfo (TREE_PURPOSE (level), binfo));
    2473              : 
    2474         1934 :           if (!(we_hide_them || they_hide_us))
    2475              :             /* Neither is within the other, so no hiding can occur.  */
    2476            0 :             continue;
    2477              : 
    2478         1946 :           for (prev = &TREE_VALUE (level), other = *prev; other;)
    2479              :             {
    2480         1940 :               if (same_type_p (to_type, TREE_TYPE (other)))
    2481              :                 {
    2482         1940 :                   if (they_hide_us)
    2483              :                     /* We are hidden.  */
    2484              :                     return 0;
    2485              : 
    2486            6 :                   if (we_hide_them)
    2487              :                     {
    2488              :                       /* We hide the other one.  */
    2489            6 :                       other = TREE_CHAIN (other);
    2490            6 :                       *prev = other;
    2491            6 :                       continue;
    2492              :                     }
    2493              :                 }
    2494            0 :               prev = &TREE_CHAIN (other);
    2495            0 :               other = *prev;
    2496              :             }
    2497              :         }
    2498              :     }
    2499              :   return 1;
    2500              : }
    2501              : 
    2502              : /* Helper for lookup_conversions_r.  PARENT_CONVS is a list of lists
    2503              :    of conversion functions, the first slot will be for the current
    2504              :    binfo, if MY_CONVS is non-NULL.  CHILD_CONVS is the list of lists
    2505              :    of conversion functions from children of the current binfo,
    2506              :    concatenated with conversions from elsewhere in the hierarchy --
    2507              :    that list begins with OTHER_CONVS.  Return a single list of lists
    2508              :    containing only conversions from the current binfo and its
    2509              :    children.  */
    2510              : 
    2511              : static tree
    2512     34126767 : split_conversions (tree my_convs, tree parent_convs,
    2513              :                    tree child_convs, tree other_convs)
    2514              : {
    2515     34126767 :   tree t;
    2516     34126767 :   tree prev;
    2517              : 
    2518              :   /* Remove the original other_convs portion from child_convs.  */
    2519     34126767 :   for (prev = NULL, t = child_convs;
    2520     35080903 :        t != other_convs; prev = t, t = TREE_CHAIN (t))
    2521       954136 :     continue;
    2522              : 
    2523     34126767 :   if (prev)
    2524       954043 :     TREE_CHAIN (prev) = NULL_TREE;
    2525              :   else
    2526              :     child_convs = NULL_TREE;
    2527              : 
    2528              :   /* Attach the child convs to any we had at this level.  */
    2529     34126767 :   if (my_convs)
    2530              :     {
    2531     33152119 :       my_convs = parent_convs;
    2532     33152119 :       TREE_CHAIN (my_convs) = child_convs;
    2533              :     }
    2534              :   else
    2535              :     my_convs = child_convs;
    2536              : 
    2537     34126767 :   return my_convs;
    2538       954136 : }
    2539              : 
    2540              : /* Worker for lookup_conversions.  Lookup conversion functions in
    2541              :    BINFO and its children.  VIRTUAL_DEPTH is nonzero, if BINFO is in a
    2542              :    morally virtual base, and VIRTUALNESS is nonzero, if we've
    2543              :    encountered virtual bases already in the tree walk.  PARENT_CONVS
    2544              :    is a list of conversions within parent binfos.  OTHER_CONVS are
    2545              :    conversions found elsewhere in the tree.  Return the conversions
    2546              :    found within this portion of the graph in CONVS.  Return nonzero if
    2547              :    we encountered virtualness.  We keep template and non-template
    2548              :    conversions separate, to avoid unnecessary type comparisons.
    2549              : 
    2550              :    The located conversion functions are held in lists of lists.  The
    2551              :    TREE_VALUE of the outer list is the list of conversion functions
    2552              :    found in a particular binfo.  The TREE_PURPOSE of both the outer
    2553              :    and inner lists is the binfo at which those conversions were
    2554              :    found.  TREE_STATIC is set for those lists within of morally
    2555              :    virtual binfos.  The TREE_VALUE of the inner list is the conversion
    2556              :    function or overload itself.  The TREE_TYPE of each inner list node
    2557              :    is the converted-to type.  */
    2558              : 
    2559              : static int
    2560     78234894 : lookup_conversions_r (tree binfo, int virtual_depth, int virtualness,
    2561              :                       tree parent_convs, tree other_convs, tree *convs)
    2562              : {
    2563     78234894 :   int my_virtualness = 0;
    2564     78234894 :   tree my_convs = NULL_TREE;
    2565     78234894 :   tree child_convs = NULL_TREE;
    2566              : 
    2567              :   /* If we have no conversion operators, then don't look.  */
    2568     78234894 :   if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
    2569              :     {
    2570     44108127 :       *convs = NULL_TREE;
    2571              : 
    2572     44108127 :       return 0;
    2573              :     }
    2574              : 
    2575     34126767 :   if (BINFO_VIRTUAL_P (binfo))
    2576        93442 :     virtual_depth++;
    2577              : 
    2578              :   /* First, locate the unhidden ones at this level.  */
    2579     34126767 :   if (tree conv = get_class_binding (BINFO_TYPE (binfo), conv_op_identifier))
    2580     82996596 :   for (ovl_iterator iter (conv); iter; ++iter)
    2581              :     {
    2582     39814851 :       tree fn = *iter;
    2583     39814851 :       tree type = DECL_CONV_FN_TYPE (fn);
    2584              : 
    2585     39814851 :       if (TREE_CODE (fn) != TEMPLATE_DECL && type_uses_auto (type))
    2586              :         {
    2587            3 :           mark_used (fn);
    2588            3 :           type = DECL_CONV_FN_TYPE (fn);
    2589              :         }
    2590              : 
    2591     39814851 :       if (check_hidden_convs (binfo, virtual_depth, virtualness,
    2592              :                               type, parent_convs, other_convs))
    2593              :         {
    2594     39792830 :           my_convs = tree_cons (binfo, fn, my_convs);
    2595     39792830 :           TREE_TYPE (my_convs) = type;
    2596     39792830 :           if (virtual_depth)
    2597              :             {
    2598        91496 :               TREE_STATIC (my_convs) = 1;
    2599        91496 :               my_virtualness = 1;
    2600              :             }
    2601              :         }
    2602              :     }
    2603              : 
    2604     33171924 :   if (my_convs)
    2605              :     {
    2606     33152119 :       parent_convs = tree_cons (binfo, my_convs, parent_convs);
    2607     33152119 :       if (virtual_depth)
    2608        91496 :         TREE_STATIC (parent_convs) = 1;
    2609              :     }
    2610              : 
    2611     34126767 :   child_convs = other_convs;
    2612              : 
    2613              :   /* Now iterate over each base, looking for more conversions.  */
    2614     34126767 :   unsigned i;
    2615     34126767 :   tree base_binfo;
    2616     35960410 :   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    2617              :     {
    2618      1833643 :       tree base_convs;
    2619      1833643 :       unsigned base_virtualness;
    2620              : 
    2621      1833643 :       base_virtualness = lookup_conversions_r (base_binfo,
    2622              :                                                virtual_depth, virtualness,
    2623              :                                                parent_convs, child_convs,
    2624              :                                                &base_convs);
    2625      1833643 :       if (base_virtualness)
    2626       107856 :         my_virtualness = virtualness = 1;
    2627      1833643 :       child_convs = chainon (base_convs, child_convs);
    2628              :     }
    2629              : 
    2630     34126767 :   *convs = split_conversions (my_convs, parent_convs,
    2631              :                               child_convs, other_convs);
    2632              : 
    2633     34126767 :   return my_virtualness;
    2634              : }
    2635              : 
    2636              : /* Return a TREE_LIST containing all the non-hidden user-defined
    2637              :    conversion functions for TYPE (and its base-classes).  The
    2638              :    TREE_VALUE of each node is the FUNCTION_DECL of the conversion
    2639              :    function.  The TREE_PURPOSE is the BINFO from which the conversion
    2640              :    functions in this node were selected.  This function is effectively
    2641              :    performing a set of member lookups as lookup_fnfield does, but
    2642              :    using the type being converted to as the unique key, rather than the
    2643              :    field name.  */
    2644              : 
    2645              : tree
    2646     76401536 : lookup_conversions (tree type)
    2647              : {
    2648     76401536 :   tree convs;
    2649              : 
    2650     76401536 :   complete_type (type);
    2651     76401536 :   if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type))
    2652              :     return NULL_TREE;
    2653              : 
    2654     76401251 :   lookup_conversions_r (TYPE_BINFO (type), 0, 0, NULL_TREE, NULL_TREE, &convs);
    2655              : 
    2656     76401251 :   tree list = NULL_TREE;
    2657              : 
    2658              :   /* Flatten the list-of-lists */
    2659    109553370 :   for (; convs; convs = TREE_CHAIN (convs))
    2660              :     {
    2661     33152119 :       tree probe, next;
    2662              : 
    2663     72944943 :       for (probe = TREE_VALUE (convs); probe; probe = next)
    2664              :         {
    2665     39792824 :           next = TREE_CHAIN (probe);
    2666              : 
    2667     39792824 :           TREE_CHAIN (probe) = list;
    2668     39792824 :           list = probe;
    2669              :         }
    2670              :     }
    2671              : 
    2672              :   return list;
    2673              : }
    2674              : 
    2675              : /* Returns the binfo of the first direct or indirect virtual base derived
    2676              :    from BINFO, or NULL if binfo is not via virtual.  */
    2677              : 
    2678              : tree
    2679           78 : binfo_from_vbase (tree binfo)
    2680              : {
    2681          123 :   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
    2682              :     {
    2683          123 :       if (BINFO_VIRTUAL_P (binfo))
    2684              :         return binfo;
    2685              :     }
    2686              :   return NULL_TREE;
    2687              : }
    2688              : 
    2689              : /* Returns the binfo of the first direct or indirect virtual base derived
    2690              :    from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not
    2691              :    via virtual.  */
    2692              : 
    2693              : tree
    2694    449593414 : binfo_via_virtual (tree binfo, tree limit)
    2695              : {
    2696    449593414 :   if (limit && !CLASSTYPE_VBASECLASSES (limit))
    2697              :     /* LIMIT has no virtual bases, so BINFO cannot be via one.  */
    2698              :     return NULL_TREE;
    2699              : 
    2700     13168099 :   for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
    2701      1843792 :        binfo = BINFO_INHERITANCE_CHAIN (binfo))
    2702              :     {
    2703      3928301 :       if (BINFO_VIRTUAL_P (binfo))
    2704              :         return binfo;
    2705              :     }
    2706              :   return NULL_TREE;
    2707              : }
    2708              : 
    2709              : /* BINFO is for a base class in some hierarchy.  Return true iff it is a
    2710              :    direct base.  */
    2711              : 
    2712              : bool
    2713       103065 : binfo_direct_p (tree binfo)
    2714              : {
    2715       103065 :   tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
    2716       103065 :   if (BINFO_INHERITANCE_CHAIN (d_binfo))
    2717              :     /* A second inheritance chain means indirect.  */
    2718              :     return false;
    2719       103059 :   if (!BINFO_VIRTUAL_P (binfo))
    2720              :     /* Non-virtual, so only one inheritance chain means direct.  */
    2721              :     return true;
    2722              :   /* A virtual base looks like a direct base, so we need to look through the
    2723              :      direct bases to see if it's there.  */
    2724              :   tree b_binfo;
    2725           27 :   for (int i = 0; BINFO_BASE_ITERATE (d_binfo, i, b_binfo); ++i)
    2726           24 :     if (b_binfo == binfo)
    2727              :       return true;
    2728              :   return false;
    2729              : }
    2730              : 
    2731              : /* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
    2732              :    Find the equivalent binfo within whatever graph HERE is located.
    2733              :    This is the inverse of original_binfo.  */
    2734              : 
    2735              : tree
    2736     28487428 : copied_binfo (tree binfo, tree here)
    2737              : {
    2738     28487428 :   tree result = NULL_TREE;
    2739              : 
    2740     28487428 :   if (BINFO_VIRTUAL_P (binfo))
    2741              :     {
    2742              :       tree t;
    2743              : 
    2744      6555063 :       for (t = here; BINFO_INHERITANCE_CHAIN (t);
    2745      3404289 :            t = BINFO_INHERITANCE_CHAIN (t))
    2746      3404289 :         continue;
    2747              : 
    2748      3150774 :       result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
    2749      3404289 :     }
    2750     25336654 :   else if (BINFO_INHERITANCE_CHAIN (binfo))
    2751              :     {
    2752     12668327 :       tree cbinfo;
    2753     12668327 :       tree base_binfo;
    2754     12668327 :       int ix;
    2755              : 
    2756     12668327 :       cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
    2757     25362529 :       for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
    2758     12694202 :         if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
    2759              :           {
    2760              :             result = base_binfo;
    2761              :             break;
    2762              :           }
    2763              :     }
    2764              :   else
    2765              :     {
    2766     12668327 :       gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
    2767              :       result = here;
    2768              :     }
    2769              : 
    2770     28487428 :   gcc_assert (result);
    2771     28487428 :   return result;
    2772              : }
    2773              : 
    2774              : tree
    2775      6374304 : binfo_for_vbase (tree base, tree t)
    2776              : {
    2777      6374304 :   unsigned ix;
    2778      6374304 :   tree binfo;
    2779      6374304 :   vec<tree, va_gc> *vbases;
    2780              : 
    2781     67642048 :   for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
    2782     67642048 :        vec_safe_iterate (vbases, ix, &binfo); ix++)
    2783     66166737 :     if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
    2784              :       return binfo;
    2785              :   return NULL;
    2786              : }
    2787              : 
    2788              : /* BINFO is some base binfo of HERE, within some other
    2789              :    hierarchy. Return the equivalent binfo, but in the hierarchy
    2790              :    dominated by HERE.  This is the inverse of copied_binfo.  If BINFO
    2791              :    is not a base binfo of HERE, returns NULL_TREE.  */
    2792              : 
    2793              : tree
    2794         1940 : original_binfo (tree binfo, tree here)
    2795              : {
    2796         1940 :   tree result = NULL;
    2797              : 
    2798         1940 :   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
    2799              :     result = here;
    2800           12 :   else if (BINFO_VIRTUAL_P (binfo))
    2801           12 :     result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
    2802           12 :               ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
    2803              :               : NULL_TREE);
    2804            0 :   else if (BINFO_INHERITANCE_CHAIN (binfo))
    2805              :     {
    2806            0 :       tree base_binfos;
    2807              : 
    2808            0 :       base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
    2809            0 :       if (base_binfos)
    2810              :         {
    2811              :           int ix;
    2812              :           tree base_binfo;
    2813              : 
    2814            0 :           for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
    2815            0 :             if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
    2816              :                                    BINFO_TYPE (binfo)))
    2817              :               {
    2818              :                 result = base_binfo;
    2819              :                 break;
    2820              :               }
    2821              :         }
    2822              :     }
    2823              : 
    2824         1940 :   return result;
    2825              : }
    2826              : 
    2827              : /* True iff TYPE has any dependent bases (and therefore we can't say
    2828              :    definitively that another class is not a base of an instantiation of
    2829              :    TYPE).  */
    2830              : 
    2831              : bool
    2832    161608876 : any_dependent_bases_p (tree type /* = current_nonlambda_class_type () */)
    2833              : {
    2834    161608876 :   if (!type || !CLASS_TYPE_P (type) || !uses_template_parms (type))
    2835    114858423 :     return false;
    2836              : 
    2837              :   /* If we haven't set TYPE_BINFO yet, we don't know anything about the bases.
    2838              :      Return false because in this situation we aren't actually looking up names
    2839              :      in the scope of the class, so it doesn't matter whether it has dependent
    2840              :      bases.  */
    2841     46750453 :   if (!TYPE_BINFO (type))
    2842              :     return false;
    2843              : 
    2844              :   unsigned i;
    2845              :   tree base_binfo;
    2846     48902588 :   FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i, base_binfo)
    2847     31918659 :     if (BINFO_DEPENDENT_BASE_P (base_binfo)
    2848              :         /* Recurse to also consider possibly dependent bases of a base that
    2849              :            is part of the current instantiation.  */
    2850     31918659 :         || any_dependent_bases_p (BINFO_TYPE (base_binfo)))
    2851     29766515 :       return true;
    2852              : 
    2853              :   return false;
    2854              : }
        

Generated by: LCOV version 2.4-beta

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,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.