LCOV - code coverage report
Current view: top level - gcc/cp - search.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 908 945 96.1 %
Date: 2023-07-19 08:18:47 Functions: 67 67 100.0 %

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

Generated by: LCOV version 1.16