LCOV - code coverage report
Current view: top level - gcc/cp - name-lookup.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.6 % 4237 4008
Test Date: 2026-04-20 14:57:17 Functions: 96.2 % 237 228
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Definitions for C++ name lookup routines.
       2              :    Copyright (C) 2003-2026 Free Software Foundation, Inc.
       3              :    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "cp-tree.h"
      25              : #include "timevar.h"
      26              : #include "stringpool.h"
      27              : #include "print-tree.h"
      28              : #include "attribs.h"
      29              : #include "debug.h"
      30              : #include "c-family/c-pragma.h"
      31              : #include "gcc-rich-location.h"
      32              : #include "spellcheck-tree.h"
      33              : #include "parser.h"
      34              : #include "c-family/name-hint.h"
      35              : #include "c-family/known-headers.h"
      36              : #include "c-family/c-spellcheck.h"
      37              : #include "bitmap.h"
      38              : 
      39              : static cxx_binding *cxx_binding_make (tree value, tree type);
      40              : static cp_binding_level *innermost_nonclass_level (void);
      41              : static void set_identifier_type_value_with_scope (tree id, tree decl,
      42              :                                                   cp_binding_level *b);
      43              : static name_hint maybe_suggest_missing_std_header (location_t location,
      44              :                                                    tree name);
      45              : static name_hint suggest_alternatives_for_1 (location_t location, tree name,
      46              :                                              bool suggest_misspellings);
      47              : 
      48              : /* Slots in BINDING_VECTOR.  */
      49              : enum binding_slots
      50              : {
      51              :  BINDING_SLOT_CURRENT,  /* Slot for current TU.  */
      52              :  BINDING_SLOT_GLOBAL,   /* Slot for merged global module. */
      53              :  BINDING_SLOT_PARTITION, /* Slot for merged partition entities or
      54              :                             imported friends.  */
      55              : 
      56              :  /* Number of always-allocated slots.  */
      57              :  BINDING_SLOTS_FIXED = BINDING_SLOT_GLOBAL + 1
      58              : };
      59              : 
      60              : /* Create a STAT_HACK node with DECL as the value binding and TYPE as
      61              :    the type binding.  */
      62              : 
      63              : static tree
      64       398973 : stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
      65              : {
      66       398973 :   tree result = make_node (OVERLOAD);
      67              : 
      68              :   /* Mark this as a lookup, so we can tell this is a stat hack.  */
      69       398973 :   OVL_LOOKUP_P (result) = true;
      70       398973 :   STAT_DECL (result) = decl;
      71       398973 :   STAT_TYPE (result) = type;
      72       398973 :   return result;
      73              : }
      74              : 
      75              : /* Create a local binding level for NAME.  */
      76              : 
      77              : static cxx_binding *
      78    914899824 : create_local_binding (cp_binding_level *level, tree name)
      79              : {
      80    914899824 :   cxx_binding *binding = cxx_binding_make (NULL, NULL);
      81              : 
      82    914899824 :   LOCAL_BINDING_P (binding) = true;
      83    914899824 :   binding->scope = level;
      84    914899824 :   binding->previous = IDENTIFIER_BINDING (name);
      85              : 
      86    914899824 :   IDENTIFIER_BINDING (name) = binding;
      87              : 
      88    914899824 :   return binding;
      89              : }
      90              : 
      91              : /* Find the binding for NAME in namespace NS.  If CREATE_P is true,
      92              :    make an empty binding if there wasn't one.  */
      93              : 
      94              : static tree *
      95  11587826997 : find_namespace_slot (tree ns, tree name, bool create_p = false)
      96              : {
      97  11587826997 :   tree *slot = DECL_NAMESPACE_BINDINGS (ns)
      98  22781073369 :     ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
      99              :                            create_p ? INSERT : NO_INSERT);
     100  11587826997 :   return slot;
     101              : }
     102              : 
     103              : static tree
     104        60717 : find_namespace_value (tree ns, tree name)
     105              : {
     106        60717 :   tree *b = find_namespace_slot (ns, name);
     107              : 
     108        60717 :   return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
     109              : }
     110              : 
     111              : /* Look in *SLOT for a the binding of NAME in imported module IX.
     112              :    Returns pointer to binding's slot, or NULL if not found.  Does a
     113              :    binary search, as this is mainly used for random access during
     114              :    importing.  Do not use for the fixed slots.  */
     115              : 
     116              : static binding_slot *
     117       108316 : search_imported_binding_slot (tree *slot, unsigned ix)
     118              : {
     119       108316 :   gcc_assert (ix);
     120              : 
     121       108316 :   if (!*slot)
     122              :     return NULL;
     123              : 
     124       108316 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
     125              :     return NULL;
     126              : 
     127       108316 :   unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     128       108316 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
     129              : 
     130       108316 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     131              :     {
     132       108316 :       clusters--;
     133       108316 :       cluster++;
     134              :     }
     135              : 
     136       219709 :   while (clusters > 1)
     137              :     {
     138         3077 :       unsigned half = clusters / 2;
     139         3077 :       gcc_checking_assert (cluster[half].indices[0].span);
     140         3077 :       if (cluster[half].indices[0].base > ix)
     141              :         clusters = half;
     142              :       else
     143              :         {
     144          820 :           clusters -= half;
     145          820 :           cluster += half;
     146              :         }
     147              :     }
     148              : 
     149       108316 :   if (clusters)
     150              :     /* Is it in this cluster?  */
     151       215818 :     for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     152              :       {
     153       215818 :         if (!cluster->indices[off].span)
     154              :           break;
     155       215818 :         if (cluster->indices[off].base > ix)
     156              :           break;
     157              : 
     158       215818 :         if (cluster->indices[off].base + cluster->indices[off].span > ix)
     159       108316 :           return &cluster->slots[off];
     160              :       }
     161              : 
     162              :   return NULL;
     163              : }
     164              : 
     165              : static void
     166        97289 : init_global_partition (binding_cluster *cluster, tree decl)
     167              : {
     168        97289 :   bool named = true;
     169              : 
     170        97289 :   if (header_module_p ())
     171              :     named = false;
     172        97211 :   else if (TREE_PUBLIC (decl)
     173        54010 :            && TREE_CODE (decl) == NAMESPACE_DECL
     174        98059 :            && !DECL_NAMESPACE_ALIAS (decl))
     175              :     named = false;
     176        96370 :   else if (!get_originating_module (decl))
     177              :     named = false;
     178              : 
     179            0 :   binding_slot *mslot;
     180            0 :   if (named)
     181            0 :     mslot = &cluster[BINDING_SLOT_PARTITION
     182              :                      / BINDING_VECTOR_SLOTS_PER_CLUSTER]
     183              :       .slots[BINDING_SLOT_PARTITION
     184              :              % BINDING_VECTOR_SLOTS_PER_CLUSTER];
     185              :   else
     186        97289 :     mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
     187              : 
     188        97289 :   if (*mslot)
     189        20629 :     decl = ovl_make (decl, *mslot);
     190        97289 :   *mslot = decl;
     191              : 
     192        97289 :   if (TREE_CODE (decl) == CONST_DECL)
     193              :     {
     194         1857 :       tree type = TREE_TYPE (decl);
     195         1857 :       if (TREE_CODE (type) == ENUMERAL_TYPE
     196         1857 :           && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type)))
     197         2541 :           && decl == TREE_VALUE (TYPE_VALUES (type)))
     198              :         /* Anonymous enums are keyed by their first enumerator, put
     199              :            the TYPE_DECL here too.  */
     200          143 :         *mslot = ovl_make (TYPE_NAME (type), *mslot);
     201              :     }
     202        97289 : }
     203              : 
     204              : /* Get the fixed binding slot IX.  Creating the vector if CREATE is
     205              :    non-zero.  If CREATE is < 0, make sure there is at least 1 spare
     206              :    slot for an import.  (It is an error for CREATE < 0 and the slot to
     207              :    already exist.)  */
     208              : 
     209              : static tree *
     210    390691545 : get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
     211              : {
     212    390691545 :   gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
     213              : 
     214              :   /* An assumption is that the fixed slots all reside in one cluster.  */
     215    390691545 :   gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
     216              : 
     217    390691545 :   if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
     218              :     {
     219    390552841 :       if (ix == BINDING_SLOT_CURRENT)
     220              :         /* The current TU can just use slot directly.  */
     221              :         return slot;
     222              : 
     223       195161 :       if (!create)
     224              :         return NULL;
     225              : 
     226              :       /* The partition slot is always needed, in case we have imported
     227              :          temploid friends with attachment different from the module we
     228              :          imported them from.  */
     229       195161 :       bool partition_slot = true;
     230       195161 :       unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
     231              :                         + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
     232              :                        / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     233       195161 :       tree new_vec = make_binding_vec (name, want);
     234       195161 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
     235       195161 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
     236              : 
     237              :       /* Initialize the fixed slots.  */
     238       585483 :       for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
     239              :         {
     240       390322 :           cluster[0].indices[jx].base = 0;
     241       390322 :           cluster[0].indices[jx].span = 1;
     242       390322 :           cluster[0].slots[jx] = NULL_TREE;
     243              :         }
     244              : 
     245       195161 :       if (partition_slot)
     246              :         {
     247       195161 :           unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     248       195161 :           unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
     249       195161 :           cluster[ind].indices[off].base = 0;
     250       195161 :           cluster[ind].indices[off].span = 1;
     251       195161 :           cluster[ind].slots[off] = NULL_TREE;
     252              :         }
     253              : 
     254       195161 :       if (tree orig = *slot)
     255              :         {
     256              :           /* Propagate existing value to current slot.  */
     257              : 
     258              :           /* Propagate global & module entities to the global and
     259              :              partition slots.  */
     260        76660 :           if (tree type = strip_using_decl (MAYBE_STAT_TYPE (orig)))
     261           17 :             init_global_partition (cluster, type);
     262              : 
     263       173932 :           for (ovl_iterator iter (strip_using_decl (MAYBE_STAT_DECL (orig)));
     264       173932 :                iter; ++iter)
     265              :             {
     266        97272 :               tree decl = *iter;
     267              : 
     268              :               /* Internal linkage entities are in deduplicateable.  */
     269        97272 :               init_global_partition (cluster, decl);
     270              :             }
     271              : 
     272        76660 :           if (cluster[0].slots[BINDING_SLOT_GLOBAL]
     273        76660 :               && !(TREE_CODE (orig) == NAMESPACE_DECL
     274        77522 :                    && !DECL_NAMESPACE_ALIAS (orig)))
     275              :             {
     276              :               /* Note that we had some GMF entries.  */
     277        75805 :               if (!STAT_HACK_P (orig))
     278        75778 :                 orig = stat_hack (orig);
     279              : 
     280        75805 :               MODULE_BINDING_GLOBAL_P (orig) = true;
     281              :             }
     282              : 
     283        76660 :           cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
     284              :         }
     285              : 
     286       195161 :       *slot = new_vec;
     287       195161 :     }
     288              :   else
     289       138704 :     gcc_checking_assert (create >= 0);
     290              : 
     291       333865 :   unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     292       333865 :   binding_cluster &cluster
     293       333865 :     = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     294              : 
     295              :   /* There must always be slots for these indices  */
     296       333865 :   gcc_checking_assert (cluster.indices[off].span == 1
     297              :                        && !cluster.indices[off].base
     298              :                        && !cluster.slots[off].is_lazy ());
     299              : 
     300       333865 :   return reinterpret_cast<tree *> (&cluster.slots[off]);
     301              : }
     302              : 
     303              : /* *SLOT is a namespace binding slot.  Append a slot for imported
     304              :    module IX.  */
     305              : 
     306              : static binding_slot *
     307       192779 : append_imported_binding_slot (tree *slot, tree name, unsigned ix)
     308              : {
     309       192779 :   gcc_checking_assert (ix);
     310              : 
     311       192779 :   if (!*slot ||  TREE_CODE (*slot) != BINDING_VECTOR)
     312              :     /* Make an initial module vector.  */
     313       179134 :     get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
     314        13645 :   else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
     315        13645 :            ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
     316              :     /* There is space in the last cluster.  */;
     317        11905 :   else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
     318        11905 :            != BINDING_VECTOR_ALLOC_CLUSTERS (*slot))
     319              :     /* There is space in the vector.  */
     320            0 :     BINDING_VECTOR_NUM_CLUSTERS (*slot)++;
     321              :   else
     322              :     {
     323              :       /* Extend the vector.  */
     324        11905 :       unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     325        11905 :       unsigned want = (have * 3 + 1) / 2;
     326              : 
     327        11905 :       if (want > (unsigned short)~0)
     328              :         want = (unsigned short)~0;
     329              : 
     330        11905 :       tree new_vec = make_binding_vec (name, want);
     331        11905 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
     332        23810 :       BINDING_VECTOR_INTERNAL_DECLS (new_vec)
     333        11905 :         = BINDING_VECTOR_INTERNAL_DECLS (*slot);
     334        23810 :       BINDING_VECTOR_GLOBAL_DUPS_P (new_vec)
     335        11905 :         = BINDING_VECTOR_GLOBAL_DUPS_P (*slot);
     336        23810 :       BINDING_VECTOR_PARTITION_DUPS_P (new_vec)
     337        11905 :         = BINDING_VECTOR_PARTITION_DUPS_P (*slot);
     338        23810 :       memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
     339        23810 :               BINDING_VECTOR_CLUSTER_BASE (*slot),
     340        11905 :               have * sizeof (binding_cluster));
     341        11905 :       *slot = new_vec;
     342              :     }
     343              : 
     344       192779 :   binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
     345       373653 :   for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     346       373653 :     if (!last->indices[off].span)
     347              :       {
     348              :         /* Fill the free slot of the cluster.  */
     349       192779 :         last->indices[off].base = ix;
     350       192779 :         last->indices[off].span = 1;
     351       192779 :         last->slots[off] = NULL_TREE;
     352              :         /* Check monotonicity.  */
     353       192779 :         gcc_checking_assert (last[off ? 0 : -1]
     354              :                              .indices[off ? off - 1
     355              :                                       : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
     356              :                              .base < ix);
     357       192779 :         return &last->slots[off];
     358              :       }
     359              : 
     360            0 :   gcc_unreachable ();
     361              : }
     362              : 
     363              : /* Add DECL to the list of things declared in binding level B.  */
     364              : 
     365              : static void
     366   1326814361 : add_decl_to_level (cp_binding_level *b, tree decl)
     367              : {
     368   1326814361 :   gcc_assert (b->kind != sk_class);
     369              : 
     370              :   /* Make sure we don't create a circular list.  xref_tag can end
     371              :      up pushing the same artificial decl more than once.  We
     372              :      should have already detected that in update_binding.  (This isn't a
     373              :      complete verification of non-circularity.)  */
     374   1326814361 :   gcc_assert (b->names != decl);
     375              : 
     376              :   /* We build up the list in reverse order, and reverse it later if
     377              :      necessary.  */
     378   1326814361 :   TREE_CHAIN (decl) = b->names;
     379   1326814361 :   b->names = decl;
     380              : 
     381              :   /* If appropriate, add decl to separate list of statics.  We include
     382              :      extern variables because they might turn out to be static later.
     383              :      It's OK for this list to contain a few false positives.  */
     384   1326814361 :   if (b->kind == sk_namespace
     385   1326814361 :       && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     386    369464569 :           || (TREE_CODE (decl) == FUNCTION_DECL
     387    303482881 :               && (!TREE_PUBLIC (decl)
     388    303365682 :                   || decl_internal_context_p (decl)
     389    303364506 :                   || DECL_DECLARED_INLINE_P (decl)))))
     390     17286062 :     vec_safe_push (static_decls, decl);
     391   1326814361 : }
     392              : 
     393              : /* Find the binding for NAME in the local binding level B.  */
     394              : 
     395              : static cxx_binding *
     396    940998374 : find_local_binding (cp_binding_level *b, tree name)
     397              : {
     398    940998374 :   if (cxx_binding *binding = IDENTIFIER_BINDING (name))
     399            0 :     for (;; b = b->level_chain)
     400              :       {
     401      3832211 :         if (binding->scope == b)
     402              :           return binding;
     403              : 
     404              :         /* Cleanup contours are transparent to the language.  */
     405      3829041 :         if (b->kind != sk_cleanup)
     406              :           break;
     407              :       }
     408              :   return NULL;
     409              : }
     410              : 
     411              : class name_lookup
     412              : {
     413              : public:
     414              :   typedef std::pair<tree, tree> using_pair;
     415              :   typedef auto_vec<using_pair, 16> using_queue;
     416              : 
     417              : public:
     418              :   tree name;    /* The identifier being looked for.  */
     419              : 
     420              :   /* Usually we just add things to the VALUE binding, but we record
     421              :      (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
     422              :      using-decl resolution.  */
     423              :   tree value;   /* A (possibly ambiguous) set of things found.  */
     424              :   tree type;    /* A type that has been found.  */
     425              : 
     426              :   LOOK_want want;  /* What kind of entity we want.  */
     427              : 
     428              :   bool tentative;  /* The lookup is just to find additional decl-reachable
     429              :                       entities in this TU during modules streaming.  */
     430              :   bool deduping; /* Full deduping is needed because using declarations
     431              :                     are in play.  */
     432              :   vec<tree, va_heap, vl_embed> *scopes;
     433              :   name_lookup *previous; /* Previously active lookup.  */
     434              : 
     435              : protected:
     436              :   /* Marked scope stack for outermost name lookup.  */
     437              :   static vec<tree, va_heap, vl_embed> *shared_scopes;
     438              :   /* Currently active lookup.  */
     439              :   static name_lookup *active;
     440              : 
     441              : public:
     442   1831619542 :   name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
     443   1831619542 :   : name (n), value (NULL_TREE), type (NULL_TREE),
     444   1831619542 :     want (w), tentative (false),
     445   1831619542 :     deduping (false), scopes (NULL), previous (NULL)
     446              :   {
     447   1831619542 :     preserve_state ();
     448              :   }
     449   1831619542 :   ~name_lookup ()
     450              :   {
     451   1831619542 :     gcc_checking_assert (!deduping);
     452   1831619542 :     restore_state ();
     453   1831619542 :   }
     454              : 
     455              : private: /* Uncopyable, unmovable, unassignable. I am a rock. */
     456              :   name_lookup (const name_lookup &);
     457              :   name_lookup &operator= (const name_lookup &);
     458              : 
     459              :  public:
     460              :   /* Turn on or off deduping mode.  */
     461   1879103970 :   void dedup (bool state)
     462              :   {
     463   1879103970 :     if (deduping != state)
     464              :       {
     465    116966614 :         deduping = state;
     466    116966614 :         lookup_mark (value, state);
     467              :       }
     468   1879103970 :   }
     469              : 
     470              : protected:
     471  23683016304 :   static bool seen_p (tree scope)
     472              :   {
     473  23683016304 :     return LOOKUP_SEEN_P (scope);
     474              :   }
     475     58958066 :   static bool found_p (tree scope)
     476              :   {
     477     58958066 :     return LOOKUP_FOUND_P (scope);
     478              :   }
     479              : 
     480              :   void mark_seen (tree scope); /* Mark and add to scope vector. */
     481    479053546 :   static void mark_found (tree scope)
     482              :   {
     483    479053546 :     gcc_checking_assert (seen_p (scope));
     484    479053546 :     LOOKUP_FOUND_P (scope) = true;
     485    479053546 :   }
     486  11532364607 :   bool see_and_mark (tree scope)
     487              :   {
     488  11532364607 :     bool ret = seen_p (scope);
     489  11532364607 :     if (!ret)
     490   2473455420 :       mark_seen (scope);
     491  11246220041 :     return ret;
     492              :   }
     493              :   bool find_and_mark (tree scope);
     494              : 
     495              : private:
     496              :   void preserve_state ();
     497              :   void restore_state ();
     498              : 
     499              : public:
     500              :   static tree ambiguous (tree thing, tree current);
     501              :   void add_value (tree new_val);
     502              : private:
     503              :   void add_overload (tree fns);
     504              :   void add_type (tree new_type);
     505              :   bool process_binding (tree val_bind, tree type_bind);
     506              :   unsigned process_module_binding (tree val_bind, tree type_bind, unsigned);
     507              :   /* Look in only namespace.  */
     508              :   bool search_namespace_only (tree scope);
     509              :   /* Look in namespace and its (recursive) inlines. Ignore using
     510              :      directives.  Return true if something found (inc dups). */
     511              :   bool search_namespace (tree scope);
     512              :   /* Look in the using directives of namespace + inlines using
     513              :      qualified lookup rules.  */
     514              :   bool search_usings (tree scope);
     515              : 
     516              : private:
     517              :   void queue_namespace (using_queue& queue, int depth, tree scope);
     518              :   void queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings);
     519              : 
     520              : private:
     521              :   void add_fns (tree);
     522              : 
     523              :  private:
     524              :   void adl_expr (tree);
     525              :   void adl_type (tree);
     526              :   void adl_template_arg (tree);
     527              :   void adl_class (tree);
     528              :   void adl_enum (tree);
     529              :   void adl_bases (tree);
     530              :   void adl_class_only (tree);
     531              :   void adl_namespace (tree);
     532              :   void adl_class_fns (tree);
     533              :   void adl_namespace_fns (tree, bitmap, bitmap, bitmap);
     534              : 
     535              : public:
     536              :   /* Search namespace + inlines + maybe usings as qualified lookup.  */
     537              :   bool search_qualified (tree scope, bool usings = true);
     538              : 
     539              :   /* Search namespace + inlines + usings as unqualified lookup.  */
     540              :   bool search_unqualified (tree scope, cp_binding_level *);
     541              : 
     542              :   /* ADL lookup of ARGS.  */
     543              :   tree search_adl (tree fns, vec<tree, va_gc> *args);
     544              : };
     545              : 
     546              : /* Scope stack shared by all outermost lookups.  This avoids us
     547              :    allocating and freeing on every single lookup.  */
     548              : vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
     549              : 
     550              : /* Currently active lookup.  */
     551              : name_lookup *name_lookup::active;
     552              : 
     553              : /* Name lookup is recursive, becase ADL can cause template
     554              :    instatiation.  This is of course a rare event, so we optimize for
     555              :    it not happening.  When we discover an active name-lookup, which
     556              :    must be an ADL lookup,  we need to unmark the marked scopes and also
     557              :    unmark the lookup we might have been accumulating.  */
     558              : 
     559              : void
     560   1831619542 : name_lookup::preserve_state ()
     561              : {
     562   1831619542 :   previous = active;
     563   1831619542 :   if (previous)
     564              :     {
     565         1658 :       unsigned length = vec_safe_length (previous->scopes);
     566         1658 :       vec_safe_reserve (previous->scopes, length);
     567         7901 :       for (unsigned ix = length; ix--;)
     568              :         {
     569         6243 :           tree decl = (*previous->scopes)[ix];
     570              : 
     571         6243 :           gcc_checking_assert (LOOKUP_SEEN_P (decl));
     572         6243 :           LOOKUP_SEEN_P (decl) = false;
     573              : 
     574              :           /* Preserve the FOUND_P state on the interrupted lookup's
     575              :              stack.  */
     576         6243 :           if (LOOKUP_FOUND_P (decl))
     577              :             {
     578          949 :               LOOKUP_FOUND_P (decl) = false;
     579          949 :               previous->scopes->quick_push (decl);
     580              :             }
     581              :         }
     582              : 
     583         1658 :       tentative = previous->tentative;
     584              : 
     585              :       /* Unmark the outer partial lookup.  */
     586         1658 :       if (previous->deduping)
     587           12 :         lookup_mark (previous->value, false);
     588              :     }
     589              :   else
     590   1831617884 :     scopes = shared_scopes;
     591   1831619542 :   active = this;
     592   1831619542 : }
     593              : 
     594              : /* Restore the marking state of a lookup we interrupted.  */
     595              : 
     596              : void
     597   1831619542 : name_lookup::restore_state ()
     598              : {
     599   1831619542 :   gcc_checking_assert (!deduping);
     600              : 
     601              :   /* Unmark and empty this lookup's scope stack.  */
     602  14909459125 :   for (unsigned ix = vec_safe_length (scopes); ix--;)
     603              :     {
     604  11246220041 :       tree decl = scopes->pop ();
     605  11246220041 :       gcc_checking_assert (LOOKUP_SEEN_P (decl));
     606  11246220041 :       LOOKUP_SEEN_P (decl) = false;
     607  11246220041 :       LOOKUP_FOUND_P (decl) = false;
     608              :     }
     609              : 
     610   1831619542 :   active = previous;
     611   1831619542 :   if (previous)
     612              :     {
     613         1658 :       free (scopes);
     614              : 
     615         1658 :       unsigned length = vec_safe_length (previous->scopes);
     616         7901 :       for (unsigned ix = 0; ix != length; ix++)
     617              :         {
     618         6965 :           tree decl = (*previous->scopes)[ix];
     619         6965 :           if (LOOKUP_SEEN_P (decl))
     620              :             {
     621              :               /* The remainder of the scope stack must be recording
     622              :                  FOUND_P decls, which we want to pop off.  */
     623          949 :               do
     624              :                 {
     625          949 :                   tree decl = previous->scopes->pop ();
     626          949 :                   gcc_checking_assert (LOOKUP_SEEN_P (decl)
     627              :                                        && !LOOKUP_FOUND_P (decl));
     628          949 :                   LOOKUP_FOUND_P (decl) = true;
     629              :                 }
     630          949 :               while (++ix != length);
     631              :               break;
     632              :             }
     633              : 
     634         6243 :           gcc_checking_assert (!LOOKUP_FOUND_P (decl));
     635         6243 :           LOOKUP_SEEN_P (decl) = true;
     636              :         }
     637              : 
     638              :       /* Remark the outer partial lookup.  */
     639         1658 :       if (previous->deduping)
     640           12 :         lookup_mark (previous->value, true);
     641              :     }
     642              :   else
     643   1831617884 :     shared_scopes = scopes;
     644   1831619542 : }
     645              : 
     646              : void
     647  11246220041 : name_lookup::mark_seen (tree scope)
     648              : {
     649  11246220041 :   gcc_checking_assert (!seen_p (scope));
     650  11246220041 :   LOOKUP_SEEN_P (scope) = true;
     651  11246220041 :   vec_safe_push (scopes, scope);
     652  11246220041 : }
     653              : 
     654              : bool
     655            0 : name_lookup::find_and_mark (tree scope)
     656              : {
     657            0 :   bool result = LOOKUP_FOUND_P (scope);
     658            0 :   if (!result)
     659              :     {
     660            0 :       LOOKUP_FOUND_P (scope) = true;
     661            0 :       if (!LOOKUP_SEEN_P (scope))
     662            0 :         vec_safe_push (scopes, scope);
     663              :     }
     664              : 
     665            0 :   return result;
     666              : }
     667              : 
     668              : /* THING and CURRENT are ambiguous, concatenate them.  */
     669              : 
     670              : tree
     671          696 : name_lookup::ambiguous (tree thing, tree current)
     672              : {
     673          696 :   if (TREE_CODE (current) != TREE_LIST)
     674              :     {
     675          527 :       current = build_tree_list (NULL_TREE, current);
     676          527 :       TREE_TYPE (current) = error_mark_node;
     677              :     }
     678          696 :   current = tree_cons (NULL_TREE, thing, current);
     679          696 :   TREE_TYPE (current) = error_mark_node;
     680              : 
     681          696 :   return current;
     682              : }
     683              : 
     684              : /* FNS is a new overload set to add to the exising set.  */
     685              : 
     686              : void
     687    655239572 : name_lookup::add_overload (tree fns)
     688              : {
     689    655239572 :   if (!deduping && TREE_CODE (fns) == OVERLOAD)
     690              :     {
     691    459553182 :       tree probe = fns;
     692    459553182 :       if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     693    459353774 :         probe = ovl_skip_hidden (probe);
     694    459553182 :       if (probe && TREE_CODE (probe) == OVERLOAD
     695    919106364 :           && OVL_DEDUP_P (probe))
     696              :         /* We're about to add something found by multiple paths, so need to
     697              :            engage deduping mode.  */
     698     30921010 :         dedup (true);
     699              :     }
     700              : 
     701    655239572 :   value = lookup_maybe_add (fns, value, deduping);
     702    655239572 : }
     703              : 
     704              : /* Add a NEW_VAL, a found value binding into the current value binding.  */
     705              : 
     706              : void
     707   1484122220 : name_lookup::add_value (tree new_val)
     708              : {
     709   1484122220 :   if (OVL_P (new_val) && (!value || OVL_P (value)))
     710    598611847 :     add_overload (new_val);
     711    885510373 :   else if (!value)
     712    885274202 :     value = new_val;
     713       236171 :   else if (value == new_val)
     714              :     ;
     715        13726 :   else if ((TREE_CODE (value) == TYPE_DECL
     716        13561 :             && TREE_CODE (new_val) == TYPE_DECL
     717        27284 :             && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
     718              :     /* Typedefs to the same type. */;
     719          260 :   else if (TREE_CODE (value) == NAMESPACE_DECL
     720           63 :            && TREE_CODE (new_val) == NAMESPACE_DECL
     721          323 :            && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
     722              :     /* Namespace (possibly aliased) to the same namespace.  Locate
     723              :        the namespace*/
     724           34 :     value = ORIGINAL_NAMESPACE (value);
     725              :   else
     726              :     {
     727              :       /* Disengage deduping mode.  */
     728          226 :       dedup (false);
     729          226 :       value = ambiguous (new_val, value);
     730              :     }
     731   1484122220 : }
     732              : 
     733              : /* Add a NEW_TYPE, a found type binding into the current type binding.  */
     734              : 
     735              : void
     736       421832 : name_lookup::add_type (tree new_type)
     737              : {
     738       421832 :   if (!type)
     739       421829 :     type = new_type;
     740            3 :   else if (TREE_CODE (type) == TREE_LIST
     741            3 :            || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
     742            3 :     type = ambiguous (new_type, type);
     743       421832 : }
     744              : 
     745              : /* Process a found binding containing NEW_VAL and NEW_TYPE.  Returns
     746              :    true if we actually found something noteworthy.  Hiddenness has
     747              :    already been handled in the caller.  */
     748              : 
     749              : bool
     750   1505861245 : name_lookup::process_binding (tree new_val, tree new_type)
     751              : {
     752              :   /* Did we really see a type? */
     753   1505861245 :   if (new_type
     754   1505861245 :       && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
     755              :     new_type = NULL_TREE;
     756              : 
     757   1505861245 :   new_val = strip_using_decl (new_val);
     758   1505861245 :   new_type = strip_using_decl (new_type);
     759              : 
     760              :   /* Do we really see a value? */
     761   1505861245 :   if (new_val)
     762   1484172093 :     switch (TREE_CODE (new_val))
     763              :       {
     764    265426650 :       case TEMPLATE_DECL:
     765              :         /* If we expect types or namespaces, and not templates,
     766              :            or this is not a template class.  */
     767    265426650 :         if (bool (want & LOOK_want::TYPE_NAMESPACE)
     768    265426650 :             && !DECL_TYPE_TEMPLATE_P (new_val))
     769              :           new_val = NULL_TREE;
     770              :         break;
     771    260991549 :       case TYPE_DECL:
     772    260991549 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
     773    260991549 :             || (new_type && bool (want & LOOK_want::TYPE)))
     774              :           new_val = NULL_TREE;
     775              :         break;
     776    266659703 :       case NAMESPACE_DECL:
     777    266659703 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
     778              :           new_val = NULL_TREE;
     779              :         break;
     780    691094191 :       default:
     781    691094191 :         if (bool (want & LOOK_want::TYPE_NAMESPACE))
     782              :           new_val = NULL_TREE;
     783              :       }
     784              : 
     785              :   if (!new_val)
     786              :     {
     787     21787979 :       new_val = new_type;
     788     21787979 :       new_type = NULL_TREE;
     789              :     }
     790              : 
     791              :   /* Merge into the lookup  */
     792     21787979 :   if (new_val)
     793   1484122220 :     add_value (new_val);
     794   1505861245 :   if (new_type)
     795       421832 :     add_type (new_type);
     796              : 
     797   1505861245 :   return new_val != NULL_TREE;
     798              : }
     799              : 
     800              : /* If we're importing a module containing this binding, add it to the
     801              :    lookup set.  The trickiness is with namespaces, we only want to
     802              :    find it once.  */
     803              : 
     804              : unsigned
     805        86288 : name_lookup::process_module_binding (tree new_val, tree new_type,
     806              :                                      unsigned marker)
     807              : {
     808              :   /* Optimize for (re-)finding a public namespace.  We only need to
     809              :      look once.  */
     810        86288 :   if (new_val && !new_type
     811        81705 :       && TREE_CODE (new_val) == NAMESPACE_DECL
     812        32062 :       && TREE_PUBLIC (new_val)
     813       118344 :       && !DECL_NAMESPACE_ALIAS (new_val))
     814              :     {
     815        32026 :       if (marker & 2)
     816              :         return marker;
     817        16605 :       marker |= 2;
     818              :     }
     819              : 
     820        70867 :   if (new_type || new_val)
     821        66359 :     marker |= process_binding (new_val, new_type);
     822              : 
     823              :   return marker;
     824              : }
     825              : 
     826              : /* Look in exactly namespace SCOPE.  */
     827              : 
     828              : bool
     829  10949462078 : name_lookup::search_namespace_only (tree scope)
     830              : {
     831  10949462078 :   bool found = false;
     832  10949462078 :   if (tree *binding = find_namespace_slot (scope, name))
     833              :     {
     834   1505850163 :       tree val = *binding;
     835   1505850163 :       if (TREE_CODE (val) == BINDING_VECTOR)
     836              :         {
     837              :           /* I presume the binding list is going to be sparser than
     838              :              the import bitmap.  Hence iterate over the former
     839              :              checking for bits set in the bitmap.  */
     840        55277 :           bitmap imports = get_import_bitmap ();
     841              :           /* FIXME: For instantiations, we also want to include any
     842              :              declarations visible at the point it was defined, even
     843              :              if not visible from the current TU; we approximate
     844              :              this here, but a proper solution would involve caching
     845              :              phase 1 lookup results (PR c++/122609).  */
     846        55277 :           unsigned orig_mod = 0;
     847        55277 :           bitmap orig_imp = visible_from_instantiation_origination (&orig_mod);
     848              : 
     849        55277 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
     850        55277 :           int marker = 0;
     851        55277 :           int dup_detect = 0;
     852              : 
     853        55277 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
     854              :             {
     855        30947 :               if (!deduping)
     856              :                 {
     857        30498 :                   if (named_module_purview_p ())
     858              :                     {
     859        12278 :                       dup_detect |= 2;
     860              : 
     861        12278 :                       if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
     862              :                         dup_detect |= 1;
     863              :                     }
     864              :                   else
     865              :                     dup_detect |= 1;
     866              :                 }
     867        30947 :               tree type = NULL_TREE;
     868        30947 :               tree value = bind;
     869              : 
     870        30947 :               if (STAT_HACK_P (bind))
     871              :                 {
     872        12805 :                   type = STAT_TYPE (bind);
     873        12805 :                   value = STAT_DECL (bind);
     874              : 
     875        12805 :                   if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     876              :                     {
     877        12738 :                       if (STAT_TYPE_HIDDEN_P (bind))
     878            0 :                         type = NULL_TREE;
     879        12738 :                       if (STAT_DECL_HIDDEN_P (bind))
     880              :                         value = NULL_TREE;
     881              :                       else
     882        12738 :                         value = ovl_skip_hidden (value);
     883              :                     }
     884              :                 }
     885        18142 :               else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     886        18136 :                 value = ovl_skip_hidden (value);
     887              : 
     888        30947 :               marker = process_module_binding (value, type, marker);
     889              :             }
     890              : 
     891              :           /* Scan the imported bindings.  */
     892        55277 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
     893        55277 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     894              :             {
     895        55277 :               ix--;
     896        55277 :               cluster++;
     897              :             }
     898              : 
     899              :           /* Do this in forward order, so we load modules in an order
     900              :              the user expects.  */
     901       113478 :           for (; ix--; cluster++)
     902       174603 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
     903              :               {
     904              :                 /* Are we importing this module?  */
     905       116402 :                 if (unsigned base = cluster->indices[jx].base)
     906        56414 :                   if (unsigned span = cluster->indices[jx].span)
     907        57383 :                     do
     908        57383 :                       if (bool (want & LOOK_want::ANY_REACHABLE)
     909        57292 :                           || bitmap_bit_p (imports, base)
     910        60577 :                           || (orig_imp && bitmap_bit_p (orig_imp, base)))
     911        55341 :                         goto found;
     912         2042 :                     while (++base, --span);
     913        61061 :                 continue;
     914              : 
     915        55341 :               found:;
     916              :                 /* Is it loaded?  */
     917        55341 :                 unsigned mod = cluster->indices[jx].base;
     918        55341 :                 if (cluster->slots[jx].is_lazy ())
     919              :                   {
     920         5711 :                     gcc_assert (cluster->indices[jx].span == 1);
     921         5711 :                     lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
     922              :                   }
     923        55341 :                 tree bind = cluster->slots[jx];
     924        55341 :                 if (!bind)
     925              :                   /* Load errors could mean there's nothing here.  */
     926            0 :                   continue;
     927              : 
     928              :                 /* Extract what we can see from here.  If there's no
     929              :                    stat_hack, then everything was exported.  */
     930        55341 :                 tree type = NULL_TREE;
     931              : 
     932              :                 /* If STAT_HACK_P is false, everything is visible, and
     933              :                    there's no duplication possibilities.  */
     934        55341 :                 if (STAT_HACK_P (bind))
     935              :                   {
     936        32339 :                     if (!deduping)
     937              :                       {
     938              :                         /* Do we need to engage deduplication?  */
     939        29791 :                         int dup = 0;
     940        29791 :                         if (MODULE_BINDING_GLOBAL_P (bind))
     941        27387 :                           dup |= 1;
     942        29791 :                         if (MODULE_BINDING_PARTITION_P (bind))
     943         1827 :                           dup |= 2;
     944        29791 :                         if (unsigned hit = dup_detect & dup)
     945              :                           {
     946        13361 :                             if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
     947        16156 :                                 || (hit & 2
     948          162 :                                     && BINDING_VECTOR_PARTITION_DUPS_P (val)))
     949        10845 :                               dedup (true);
     950              :                           }
     951        29791 :                         dup_detect |= dup;
     952              :                       }
     953              : 
     954        32339 :                     if (bool (want & LOOK_want::ANY_REACHABLE)
     955        32339 :                         || mod == orig_mod)
     956              :                       {
     957         7509 :                         type = STAT_TYPE (bind);
     958         7509 :                         bind = STAT_DECL (bind);
     959              :                       }
     960              :                     else
     961              :                       {
     962        24830 :                         if (STAT_TYPE_VISIBLE_P (bind))
     963         1857 :                           type = STAT_TYPE (bind);
     964        24830 :                         bind = STAT_VISIBLE (bind);
     965              :                       }
     966              :                   }
     967              : 
     968              :                 /* And process it.  */
     969        55341 :                 marker = process_module_binding (bind, type, marker);
     970        61061 :               }
     971        55277 :           found |= marker & 1;
     972              :         }
     973              :       else
     974              :         {
     975              :           /* Only a current module binding, visible from the current module.  */
     976   1505794886 :           tree bind = *binding;
     977   1505794886 :           tree value = bind, type = NULL_TREE;
     978              : 
     979   1505794886 :           if (STAT_HACK_P (bind))
     980              :             {
     981       471406 :               type = STAT_TYPE (bind);
     982       471406 :               value = STAT_DECL (bind);
     983              : 
     984       471406 :               if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     985              :                 {
     986       471152 :                   if (STAT_TYPE_HIDDEN_P (bind))
     987            3 :                     type = NULL_TREE;
     988       471152 :                   if (STAT_DECL_HIDDEN_P (bind))
     989              :                     value = NULL_TREE;
     990              :                   else
     991       470714 :                     value = ovl_skip_hidden (value);
     992              :                 }
     993              :             }
     994   1505323480 :           else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     995   1504669625 :             value = ovl_skip_hidden (value);
     996              : 
     997   1505794886 :           found |= process_binding (value, type);
     998              :         }
     999              :     }
    1000              : 
    1001  10949462078 :   return found;
    1002              : }
    1003              : 
    1004              : /* Conditionally look in namespace SCOPE and inline children.  */
    1005              : 
    1006              : bool
    1007   2200256341 : name_lookup::search_namespace (tree scope)
    1008              : {
    1009   2200256341 :   if (see_and_mark (scope))
    1010              :     /* We've visited this scope before.  Return what we found then.  */
    1011            0 :     return found_p (scope);
    1012              : 
    1013              :   /* Look in exactly namespace. */
    1014   2200256341 :   bool found = search_namespace_only (scope);
    1015              : 
    1016              :   /* Don't look into inline children, if we're looking for an
    1017              :      anonymous name -- it must be in the current scope, if anywhere.  */
    1018   2200256341 :   if (name)
    1019              :     /* Recursively look in its inline children.  */
    1020   2200254484 :     if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1021   2325109718 :       for (unsigned ix = inlinees->length (); ix--;)
    1022   1774878231 :         found |= search_namespace ((*inlinees)[ix]);
    1023              : 
    1024   2200256341 :   if (found)
    1025    442604106 :     mark_found (scope);
    1026              : 
    1027              :   return found;
    1028              : }
    1029              : 
    1030              : /* Recursively follow using directives of SCOPE & its inline children.
    1031              :    Such following is essentially a flood-fill algorithm.  */
    1032              : 
    1033              : bool
    1034     12307620 : name_lookup::search_usings (tree scope)
    1035              : {
    1036              :   /* We do not check seen_p here, as that was already set during the
    1037              :      namespace_only walk.  */
    1038     12307620 :   if (found_p (scope))
    1039              :     return true;
    1040              : 
    1041     12307620 :   bool found = false;
    1042     12307620 :   if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
    1043      3364578 :     for (unsigned ix = usings->length (); ix--;)
    1044      1682674 :       found |= search_qualified (strip_using_decl ((*usings)[ix]), true);
    1045              : 
    1046              :   /* Look in its inline children.  */
    1047     12307620 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1048      5090390 :     for (unsigned ix = inlinees->length (); ix--;)
    1049      2611945 :       found |= search_usings ((*inlinees)[ix]);
    1050              : 
    1051     12307620 :   if (found)
    1052        21592 :     mark_found (scope);
    1053              : 
    1054              :   return found;
    1055              : }
    1056              : 
    1057              : /* Qualified namespace lookup in SCOPE.
    1058              :    1) Look in SCOPE (+inlines).  If found, we're done.
    1059              :    2) Otherwise, if USINGS is true,
    1060              :       recurse for every using directive of SCOPE (+inlines).
    1061              : 
    1062              :    Trickiness is (a) loops and (b) multiple paths to same namespace.
    1063              :    In both cases we want to not repeat any lookups, and know whether
    1064              :    to stop the caller's step #2.  Do this via the FOUND_P marker.  */
    1065              : 
    1066              : bool
    1067    425378110 : name_lookup::search_qualified (tree scope, bool usings)
    1068              : {
    1069    425378110 :   bool found = false;
    1070              : 
    1071    425378110 :   if (seen_p (scope))
    1072            0 :     found = found_p (scope);
    1073              :   else
    1074              :     {
    1075    425378110 :       found = search_namespace (scope);
    1076    425378110 :       if (!found && usings)
    1077      9695675 :         found = search_usings (scope);
    1078              :     }
    1079              : 
    1080    425378110 :   dedup (false);
    1081              : 
    1082    425378110 :   return found;
    1083              : }
    1084              : 
    1085              : /* Add SCOPE to the unqualified search queue, recursively add its
    1086              :    inlines and those via using directives.  */
    1087              : 
    1088              : void
    1089   8859970025 : name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
    1090              : {
    1091   8859970025 :   if (see_and_mark (scope))
    1092   8859970025 :     return;
    1093              : 
    1094              :   /* Record it.  */
    1095              :   tree common = scope;
    1096  17505976342 :   while (SCOPE_DEPTH (common) > depth)
    1097   8733211721 :     common = CP_DECL_CONTEXT (common);
    1098   8772764621 :   queue.safe_push (using_pair (common, scope));
    1099              : 
    1100              :   /* Queue its inline children.  */
    1101   8772764621 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1102   8669176647 :     for (unsigned ix = inlinees->length (); ix--;)
    1103   6614226960 :       queue_namespace (queue, depth, (*inlinees)[ix]);
    1104              : 
    1105              :   /* Queue its using targets.  */
    1106   8772764621 :   queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
    1107              : }
    1108              : 
    1109              : /* Add the namespaces in USINGS to the unqualified search queue.  */
    1110              : 
    1111              : void
    1112  13333372119 : name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
    1113              : {
    1114  13333372119 :   if (usings)
    1115    107890150 :     for (unsigned ix = usings->length (); ix--;)
    1116     54185515 :       queue_namespace (queue, depth, strip_using_decl ((*usings)[ix]));
    1117  13333372119 : }
    1118              : 
    1119              : /* Unqualified namespace lookup in SCOPE.
    1120              :    1) add scope+inlins to worklist.
    1121              :    2) recursively add target of every using directive
    1122              :    3) for each worklist item where SCOPE is common ancestor, search it
    1123              :    4) if nothing find, scope=parent, goto 1.  */
    1124              : 
    1125              : bool
    1126   1344010306 : name_lookup::search_unqualified (tree scope, cp_binding_level *level)
    1127              : {
    1128   1344010306 :   using_queue queue;
    1129   1344010306 :   bool found = false;
    1130              : 
    1131              :   /* Queue local using-directives.  */
    1132   5904617804 :   for (; level->kind != sk_namespace; level = level->level_chain)
    1133   4560607498 :     queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
    1134              : 
    1135   4492823680 :   for (; !found; scope = CP_DECL_CONTEXT (scope))
    1136              :     {
    1137   2191557550 :       gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
    1138   2191557550 :       int depth = SCOPE_DEPTH (scope);
    1139              : 
    1140              :       /* Queue namespaces reachable from SCOPE. */
    1141   2191557550 :       queue_namespace (queue, depth, scope);
    1142              : 
    1143              :       /* Search every queued namespace where SCOPE is the common
    1144              :          ancestor.  Adjust the others.  */
    1145   2191557550 :       unsigned ix = 0;
    1146   2238625184 :       do
    1147              :         {
    1148   2238625184 :           using_pair &pair = queue[ix];
    1149  11036255736 :           while (pair.first == scope)
    1150              :             {
    1151   8749205737 :               found |= search_namespace_only (pair.second);
    1152   8749205737 :               pair = queue.pop ();
    1153   8749205737 :               if (ix == queue.length ())
    1154   2190200369 :                 goto done;
    1155              :             }
    1156              :           /* The depth is the same as SCOPE, find the parent scope.  */
    1157     48424815 :           if (SCOPE_DEPTH (pair.first) == depth)
    1158     48420423 :             pair.first = CP_DECL_CONTEXT (pair.first);
    1159     48424815 :           ix++;
    1160              :         }
    1161     96849630 :       while (ix < queue.length ());
    1162      1357181 :     done:;
    1163   2191557550 :       if (scope == global_namespace)
    1164              :         break;
    1165              : 
    1166              :       /* If looking for hidden friends, we only look in the innermost
    1167              :          namespace scope.  [namespace.memdef]/3 If a friend
    1168              :          declaration in a non-local class first declares a class,
    1169              :          function, class template or function template the friend is a
    1170              :          member of the innermost enclosing namespace.  See also
    1171              :          [basic.lookup.unqual]/7 */
    1172   1574822806 :       if (bool (want & LOOK_want::HIDDEN_FRIEND))
    1173              :         break;
    1174              :     }
    1175              : 
    1176   1344010306 :   dedup (false);
    1177              : 
    1178   1344010306 :   return found;
    1179   1344010306 : }
    1180              : 
    1181              : /* FNS is a value binding.  If it is a (set of overloaded) functions,
    1182              :    add them into the current value.  */
    1183              : 
    1184              : void
    1185     62734572 : name_lookup::add_fns (tree fns)
    1186              : {
    1187     62734572 :   if (!fns)
    1188              :     return;
    1189     56645477 :   else if (TREE_CODE (fns) == OVERLOAD)
    1190              :     {
    1191     27107875 :       if (TREE_TYPE (fns) != unknown_type_node)
    1192        86424 :         fns = OVL_FUNCTION (fns);
    1193              :     }
    1194     29537602 :   else if (!DECL_DECLARES_FUNCTION_P (fns))
    1195              :     return;
    1196              : 
    1197     56627710 :   add_overload (fns);
    1198              : }
    1199              : 
    1200              : /* Add the overloaded fns of SCOPE.  IMPORTS is the list of visible modules
    1201              :    for this lookup. INST_PATH for dependent (2nd phase) ADL is the list of
    1202              :    modules on the instantiation context for this lookup, or otherwise NULL.
    1203              :    ASSOCS is the list of modules where this namespace shares an innermost
    1204              :    non-inline namespace with an associated entity attached to said module,
    1205              :    or NULL if there are none.  */
    1206              : 
    1207              : void
    1208    209966393 : name_lookup::adl_namespace_fns (tree scope, bitmap imports,
    1209              :                                 bitmap inst_path, bitmap assocs)
    1210              : {
    1211    209966393 :   if (tree *binding = find_namespace_slot (scope, name))
    1212              :     {
    1213     41778583 :       tree val = *binding;
    1214     41778583 :       if (TREE_CODE (val) != BINDING_VECTOR)
    1215     41754154 :         add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
    1216              :       else
    1217              :         {
    1218              :           /* I presume the binding list is going to be sparser than
    1219              :              the import bitmap.  Hence iterate over the former
    1220              :              checking for bits set in the bitmap.  */
    1221        24429 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
    1222        24429 :           int dup_detect = 0;
    1223              : 
    1224        24429 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    1225              :             {
    1226              :               /* The current TU's bindings must be visible, we don't
    1227              :                  need to check the bitmaps.  */
    1228              : 
    1229        15264 :               if (!deduping)
    1230              :                 {
    1231         5452 :                   if (named_module_purview_p ())
    1232              :                     {
    1233           10 :                       dup_detect |= 2;
    1234              : 
    1235           10 :                       if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
    1236              :                         dup_detect |= 1;
    1237              :                     }
    1238              :                   else
    1239              :                     dup_detect |= 1;
    1240              :                 }
    1241              : 
    1242        15264 :               add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
    1243              :             }
    1244              : 
    1245              :           /* When doing tentative name lookup we only care about entities
    1246              :              in the current TU.  */
    1247        24429 :           if (tentative)
    1248              :             return;
    1249              : 
    1250              :           /* Scan the imported bindings.  */
    1251        24428 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
    1252        24428 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    1253              :             {
    1254        24428 :               ix--;
    1255        24428 :               cluster++;
    1256              :             }
    1257              : 
    1258              :           /* Do this in forward order, so we load modules in an order
    1259              :              the user expects.  */
    1260        50361 :           for (; ix--; cluster++)
    1261        77799 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    1262              :               {
    1263        51866 :                 int mod = cluster->indices[jx].base;
    1264              : 
    1265              :                 /* Functions are never on merged slots.  */
    1266        51866 :                 if (!mod || cluster->indices[jx].span != 1)
    1267        32979 :                   continue;
    1268              : 
    1269              :                 /* Is this slot accessible here?  */
    1270        18887 :                 bool visible = bitmap_bit_p (imports, mod);
    1271        18887 :                 bool on_inst_path = inst_path && bitmap_bit_p (inst_path, mod);
    1272         9475 :                 if (!visible && !on_inst_path
    1273         9472 :                     && !(assocs && bitmap_bit_p (assocs, mod)))
    1274            3 :                   continue;
    1275              : 
    1276              :                 /* Is it loaded?  */
    1277        18884 :                 if (cluster->slots[jx].is_lazy ())
    1278           87 :                   lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
    1279              : 
    1280        18884 :                 tree bind = cluster->slots[jx];
    1281        18884 :                 if (!bind)
    1282              :                   /* Load errors could mean there's nothing here.  */
    1283            0 :                   continue;
    1284              : 
    1285        18884 :                 if (STAT_HACK_P (bind))
    1286              :                   {
    1287        18791 :                     if (!deduping)
    1288              :                       {
    1289              :                         /* Do we need to engage deduplication?  */
    1290         9349 :                         int dup = 0;
    1291         9349 :                         if (MODULE_BINDING_GLOBAL_P (bind))
    1292         9286 :                           dup |= 1;
    1293         9349 :                         if (MODULE_BINDING_PARTITION_P (bind))
    1294            3 :                           dup |= 2;
    1295         9349 :                         if (unsigned hit = dup_detect & dup)
    1296         4610 :                           if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
    1297         5683 :                               || (hit & 2
    1298            3 :                                   && BINDING_VECTOR_PARTITION_DUPS_P (val)))
    1299         3540 :                             dedup (true);
    1300         9349 :                         dup_detect |= dup;
    1301              :                       }
    1302              : 
    1303              :                     /* For lookups on the instantiation path we can see any
    1304              :                        declarations visible at any point on the path;
    1305              :                        otherwise we should only see exported decls.  */
    1306        18791 :                     if (on_inst_path)
    1307              :                       {
    1308              :                         /* If there are any internal functions visible, naming
    1309              :                            them outside that module is ill-formed.  */
    1310         9412 :                         auto_diagnostic_group d;
    1311         9412 :                         if (MODULE_BINDING_INTERNAL_DECLS_P (bind)
    1312         9412 :                             && pedwarn (input_location, OPT_Wexternal_tu_local,
    1313              :                                         "overload set for argument-dependent "
    1314              :                                         "lookup of %<%D::%D%> in module %qs "
    1315              :                                         "contains TU-local entities",
    1316              :                                         scope, name, module_name (mod, false)))
    1317              :                           {
    1318            6 :                             tree *tu_locals
    1319            6 :                               = BINDING_VECTOR_INTERNAL_DECLS (val)->get (mod);
    1320            6 :                             gcc_checking_assert (tu_locals && *tu_locals);
    1321           12 :                             for (tree t = *tu_locals; t; t = TREE_CHAIN (t))
    1322              :                               {
    1323            6 :                                 tree decl = TREE_VALUE (t);
    1324            6 :                                 inform (TU_LOCAL_ENTITY_LOCATION (decl),
    1325              :                                         "ignoring %qD declared here "
    1326              :                                         "with internal linkage",
    1327            6 :                                         TU_LOCAL_ENTITY_NAME (decl));
    1328              :                               }
    1329              :                           }
    1330         9412 :                         bind = STAT_DECL (bind);
    1331         9412 :                       }
    1332              :                     else
    1333         9379 :                       bind = STAT_VISIBLE (bind);
    1334              :                   }
    1335              : 
    1336        18884 :                 bind = ovl_skip_hidden (bind);
    1337        18884 :                 if (on_inst_path || visible)
    1338        18848 :                   add_fns (bind);
    1339              :                 else
    1340              :                   {
    1341              :                     /* We're only accessible because we're the same module as
    1342              :                        an associated entity with module attachment: only add
    1343              :                        functions actually attached to this module.  */
    1344           78 :                     for (tree fn : ovl_range (bind))
    1345            6 :                       if (DECL_DECLARES_FUNCTION_P (fn)
    1346           24 :                           && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
    1347           54 :                           && DECL_MODULE_ATTACH_P (STRIP_TEMPLATE (fn)))
    1348           15 :                         add_overload (fn);
    1349              :                   }
    1350              :               }
    1351              :         }
    1352              :     }
    1353              : }
    1354              : 
    1355              : /* Add the hidden friends of SCOPE.  */
    1356              : 
    1357              : void
    1358     48962418 : name_lookup::adl_class_fns (tree type)
    1359              : {
    1360              :   /* Add friends.  */
    1361     48962418 :   for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
    1362    149689377 :        list; list = TREE_CHAIN (list))
    1363    100726959 :     if (name == FRIEND_NAME (list))
    1364              :       {
    1365     10385383 :         tree context = NULL_TREE; /* Lazily computed.  */
    1366     31335549 :         for (tree friends = FRIEND_DECLS (list); friends;
    1367     20950166 :              friends = TREE_CHAIN (friends))
    1368              :           {
    1369     20950166 :             tree fn = TREE_VALUE (friends);
    1370              : 
    1371              :             /* Before C++20, ADL just makes hidden friends visible, so we
    1372              :                only include functions in the same namespace.  After C++20,
    1373              :                include all namespace-scope functions.  */
    1374     20950166 :             if (!context)
    1375     10385383 :               context = decl_namespace_context (type);
    1376        21187 :             if (cxx_dialect < cxx20
    1377     20950166 :                 ? CP_DECL_CONTEXT (fn) != context
    1378     20928979 :                 : !DECL_NAMESPACE_SCOPE_P (fn))
    1379          424 :               continue;
    1380              : 
    1381     20949742 :             dedup (true);
    1382              : 
    1383              :             /* Template specializations are never found by name lookup.
    1384              :                (Templates themselves can be found, but not template
    1385              :                specializations.)  */
    1386     20949742 :             if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
    1387         3436 :               continue;
    1388              : 
    1389     20946306 :             add_fns (fn);
    1390              :           }
    1391              :       }
    1392     48962418 : }
    1393              : 
    1394              : /* Find the containing non-inlined namespace, add it and all its
    1395              :    inlinees.  */
    1396              : 
    1397              : void
    1398    229629632 : name_lookup::adl_namespace (tree scope)
    1399              : {
    1400    397905494 :   if (see_and_mark (scope))
    1401              :     return;
    1402              : 
    1403              :   /* Look down into inline namespaces.  */
    1404    209966393 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1405    220610229 :     for (unsigned ix = inlinees->length (); ix--;)
    1406    168275862 :       adl_namespace ((*inlinees)[ix]);
    1407              : 
    1408    209966393 :   if (DECL_NAMESPACE_INLINE_P (scope))
    1409              :     /* Mark parent.  */
    1410    168275862 :     adl_namespace (CP_DECL_CONTEXT (scope));
    1411              : }
    1412              : 
    1413              : /* Adds the class and its friends to the lookup structure.  */
    1414              : 
    1415              : void
    1416     49128287 : name_lookup::adl_class_only (tree type)
    1417              : {
    1418              :   /* Backend-built structures, such as __builtin_va_list, aren't
    1419              :      affected by all this.  */
    1420     49128287 :   if (!CLASS_TYPE_P (type))
    1421              :     return;
    1422              : 
    1423     49128287 :   type = TYPE_MAIN_VARIANT (type);
    1424              : 
    1425     49128287 :   if (see_and_mark (type))
    1426              :     return;
    1427              : 
    1428     48967177 :   tree context = decl_namespace_context (type);
    1429     48967177 :   adl_namespace (context);
    1430              : }
    1431              : 
    1432              : /* Adds the class and its bases to the lookup structure.
    1433              :    Returns true on error.  */
    1434              : 
    1435              : void
    1436     45422419 : name_lookup::adl_bases (tree type)
    1437              : {
    1438     45422419 :   adl_class_only (type);
    1439              : 
    1440              :   /* Process baseclasses.  */
    1441     45422419 :   if (tree binfo = TYPE_BINFO (type))
    1442              :     {
    1443              :       tree base_binfo;
    1444              :       int i;
    1445              : 
    1446     53624504 :       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    1447      8994571 :         adl_bases (BINFO_TYPE (base_binfo));
    1448              :     }
    1449     45422419 : }
    1450              : 
    1451              : /* Adds everything associated with a class argument type to the lookup
    1452              :    structure.
    1453              : 
    1454              :    If T is a class type (including unions), its associated classes are: the
    1455              :    class itself; the class of which it is a member, if any; and its direct
    1456              :    and indirect base classes. Its associated namespaces are the namespaces
    1457              :    of which its associated classes are members. Furthermore, if T is a
    1458              :    class template specialization, its associated namespaces and classes
    1459              :    also include: the namespaces and classes associated with the types of
    1460              :    the template arguments provided for template type parameters (excluding
    1461              :    template template parameters); the namespaces of which any template
    1462              :    template arguments are members; and the classes of which any member
    1463              :    templates used as template template arguments are members. [ Note:
    1464              :    non-type template arguments do not contribute to the set of associated
    1465              :    namespaces.  --end note] */
    1466              : 
    1467              : void
    1468     46734271 : name_lookup::adl_class (tree type)
    1469              : {
    1470              :   /* Backend build structures, such as __builtin_va_list, aren't
    1471              :      affected by all this.  */
    1472     46734271 :   if (!CLASS_TYPE_P (type))
    1473              :     return;
    1474              : 
    1475     46650446 :   type = TYPE_MAIN_VARIANT (type);
    1476              : 
    1477              :   /* We don't set found here because we have to have set seen first,
    1478              :      which is done in the adl_bases walk.  */
    1479     46650446 :   if (found_p (type))
    1480              :     return;
    1481              : 
    1482              :   /* Don't instantiate if we don't have to so we don't unnecessarily error
    1483              :      on incomplete types during modules streaming.  This does however mean
    1484              :      we incorrectly miss some decl-reachable entities (PR c++/123235).  */
    1485     36427848 :   if (!tentative)
    1486     36423879 :     complete_type (type);
    1487              : 
    1488     36427848 :   adl_bases (type);
    1489     36427848 :   mark_found (type);
    1490              : 
    1491     36427848 :   if (TYPE_CLASS_SCOPE_P (type))
    1492      1776583 :     adl_class_only (TYPE_CONTEXT (type));
    1493              : 
    1494              :   /* Process template arguments.  */
    1495     36427848 :   if (CLASSTYPE_TEMPLATE_INFO (type)
    1496     36427848 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
    1497              :     {
    1498     21475620 :       tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1499     60664552 :       for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
    1500     39188932 :         adl_template_arg (TREE_VEC_ELT (list, i));
    1501              :     }
    1502              : }
    1503              : 
    1504              : void
    1505     25104460 : name_lookup::adl_enum (tree type)
    1506              : {
    1507     25104460 :   type = TYPE_MAIN_VARIANT (type);
    1508     25104460 :   if (see_and_mark (type))
    1509              :     return;
    1510              : 
    1511     14265509 :   if (TYPE_CLASS_SCOPE_P (type))
    1512      1929279 :     adl_class_only (TYPE_CONTEXT (type));
    1513              :   else
    1514     12336230 :     adl_namespace (decl_namespace_context (type));
    1515              : }
    1516              : 
    1517              : void
    1518     88320465 : name_lookup::adl_expr (tree expr)
    1519              : {
    1520     88320465 :   if (!expr)
    1521              :     return;
    1522              : 
    1523     88320465 :   gcc_assert (!TYPE_P (expr));
    1524              : 
    1525     88320465 :   if (TREE_TYPE (expr) != unknown_type_node)
    1526              :     {
    1527     88312315 :       adl_type (unlowered_expr_type (expr));
    1528     88312315 :       return;
    1529              :     }
    1530              : 
    1531         8150 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1532          502 :     expr = TREE_OPERAND (expr, 0);
    1533         8150 :   if (TREE_CODE (expr) == COMPONENT_REF
    1534         8150 :       || TREE_CODE (expr) == OFFSET_REF)
    1535          466 :     expr = TREE_OPERAND (expr, 1);
    1536         8150 :   expr = MAYBE_BASELINK_FUNCTIONS (expr);
    1537              : 
    1538         8150 :   if (OVL_P (expr))
    1539        15782 :     for (lkp_iterator iter (expr); iter; ++iter)
    1540         8047 :       adl_type (TREE_TYPE (*iter));
    1541          415 :   else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
    1542              :     {
    1543              :       /* The working paper doesn't currently say how to handle
    1544              :          template-id arguments.  The sensible thing would seem to be
    1545              :          to handle the list of template candidates like a normal
    1546              :          overload set, and handle the template arguments like we do
    1547              :          for class template specializations.  */
    1548              : 
    1549              :       /* First the templates.  */
    1550          415 :       adl_expr (TREE_OPERAND (expr, 0));
    1551              : 
    1552              :       /* Now the arguments.  */
    1553          415 :       if (tree args = TREE_OPERAND (expr, 1))
    1554          839 :         for (int ix = TREE_VEC_LENGTH (args); ix--;)
    1555          424 :           adl_template_arg (TREE_VEC_ELT (args, ix));
    1556              :     }
    1557              : }
    1558              : 
    1559              : void
    1560    119894210 : name_lookup::adl_type (tree type)
    1561              : {
    1562    131048219 :   if (!type)
    1563              :     return;
    1564              : 
    1565    131048219 :   if (TYPE_PTRDATAMEM_P (type))
    1566              :     {
    1567              :       /* Pointer to member: associate class type and value type.  */
    1568         1624 :       adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
    1569         1624 :       adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
    1570         1624 :       return;
    1571              :     }
    1572    131046595 :   else if (REFLECTION_TYPE_P (type))
    1573              :     {
    1574              :       /* The namespace std::meta is an associated namespace of
    1575              :          std::meta::info.  */
    1576        44420 :       adl_namespace (std_meta_node);
    1577        44420 :       return;
    1578              :     }
    1579              : 
    1580    131002175 :   switch (TREE_CODE (type))
    1581              :     {
    1582     45563064 :     case RECORD_TYPE:
    1583     45563064 :       if (TYPE_PTRMEMFUNC_P (type))
    1584              :         {
    1585        46243 :           adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
    1586        46243 :           return;
    1587              :         }
    1588              :       /* FALLTHRU */
    1589     46734271 :     case UNION_TYPE:
    1590     46734271 :       adl_class (type);
    1591     46734271 :       return;
    1592              : 
    1593       122745 :     case METHOD_TYPE:
    1594              :       /* The basetype is referenced in the first arg type, so just
    1595              :          fall through.  */
    1596       122745 :     case FUNCTION_TYPE:
    1597              :       /* Associate the parameter types.  */
    1598       557920 :       for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
    1599       435175 :         adl_type (TREE_VALUE (args));
    1600              :       /* FALLTHROUGH */
    1601              : 
    1602     11106130 :     case POINTER_TYPE:
    1603     11106130 :     case REFERENCE_TYPE:
    1604     11106130 :     case ARRAY_TYPE:
    1605     11106130 :       adl_type (TREE_TYPE (type));
    1606     11106130 :       return;
    1607              : 
    1608     25104460 :     case ENUMERAL_TYPE:
    1609     25104460 :       adl_enum (type);
    1610     25104460 :       return;
    1611              : 
    1612         4118 :     case LANG_TYPE:
    1613         4118 :       gcc_assert (type == unknown_type_node
    1614              :                   || type == init_list_type_node);
    1615              :       return;
    1616              : 
    1617           12 :     case TYPE_PACK_EXPANSION:
    1618           12 :       adl_type (PACK_EXPANSION_PATTERN (type));
    1619           12 :       return;
    1620              : 
    1621              :     default:
    1622              :       break;
    1623              :     }
    1624              : }
    1625              : 
    1626              : /* Adds everything associated with a template argument to the lookup
    1627              :    structure.  */
    1628              : 
    1629              : void
    1630     39392661 : name_lookup::adl_template_arg (tree arg)
    1631              : {
    1632              :   /* [basic.lookup.koenig]
    1633              : 
    1634              :      If T is a template-id, its associated namespaces and classes are
    1635              :      ... the namespaces and classes associated with the types of the
    1636              :      template arguments provided for template type parameters
    1637              :      (excluding template template parameters); the namespaces in which
    1638              :      any template template arguments are defined; and the classes in
    1639              :      which any member templates used as template template arguments
    1640              :      are defined.  [Note: non-type template arguments do not
    1641              :      contribute to the set of associated namespaces.  ]  */
    1642              : 
    1643              :   /* Consider first template template arguments.  */
    1644     39392661 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    1645     39392661 :       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
    1646              :     ;
    1647     39392661 :   else if (TREE_CODE (arg) == TEMPLATE_DECL)
    1648              :     {
    1649         5949 :       tree ctx = CP_DECL_CONTEXT (arg);
    1650              : 
    1651              :       /* It's not a member template.  */
    1652         5949 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
    1653         5943 :         adl_namespace (ctx);
    1654              :       /* Otherwise, it must be member template.  */
    1655              :       else
    1656            6 :         adl_class_only (ctx);
    1657              :     }
    1658              :   /* It's an argument pack; handle it recursively.  */
    1659     39386712 :   else if (ARGUMENT_PACK_P (arg))
    1660              :     {
    1661        59215 :       tree args = ARGUMENT_PACK_ARGS (arg);
    1662        59215 :       int i, len = TREE_VEC_LENGTH (args);
    1663       262520 :       for (i = 0; i < len; ++i)
    1664       203305 :         adl_template_arg (TREE_VEC_ELT (args, i));
    1665              :     }
    1666              :   /* It's not a template template argument, but it is a type template
    1667              :      argument.  */
    1668     39327497 :   else if (TYPE_P (arg))
    1669     31136969 :     adl_type (arg);
    1670     39392661 : }
    1671              : 
    1672              : /* Perform ADL lookup.  FNS is the existing lookup result and ARGS are
    1673              :    the call arguments.  */
    1674              : 
    1675              : tree
    1676     48218699 : name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
    1677              : {
    1678     48218699 :   gcc_checking_assert (!vec_safe_length (scopes));
    1679              : 
    1680              :   /* Gather each associated entity onto the lookup's scope list.  */
    1681     48218699 :   unsigned ix;
    1682     48218699 :   tree arg;
    1683              : 
    1684    136910784 :   FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
    1685              :     /* OMP reduction operators put an ADL-significant type as the
    1686              :        first arg. */
    1687     88692085 :     if (TYPE_P (arg))
    1688           80 :       adl_type (arg);
    1689              :     /* When processing a module CMI we might get a type-dependent
    1690              :        argument: treat as a placeholder with no associated namespace
    1691              :        or entities.  */
    1692     88692005 :     else if (!tentative || !type_dependent_expression_p (arg))
    1693     88320050 :       adl_expr (arg);
    1694              : 
    1695     48218699 :   if (vec_safe_length (scopes))
    1696              :     {
    1697              :       /* Now do the lookups.  */
    1698     33769899 :       value = fns;
    1699     33769899 :       if (fns)
    1700     24060292 :         dedup (true);
    1701              : 
    1702              :       /* First get the attached modules for each innermost non-inline
    1703              :          namespace of an associated entity.  This isn't needed for
    1704              :          tentative lookup, as we're only interested in the current TU.  */
    1705     33769899 :       bitmap_obstack_initialize (NULL);
    1706     33769899 :       hash_map<tree, bitmap> ns_mod_assocs;
    1707     33769899 :       if (modules_p () && !tentative)
    1708              :         {
    1709      1086911 :           for (tree scope : scopes)
    1710       775886 :             if (TYPE_P (scope))
    1711              :               {
    1712       193177 :                 int mod = get_originating_module (TYPE_NAME (scope),
    1713              :                                                   /*global_m1=*/true);
    1714       193177 :                 if (mod > 0)
    1715              :                   {
    1716          345 :                     tree ctx = decl_namespace_context (scope);
    1717          357 :                     while (DECL_NAMESPACE_INLINE_P (ctx))
    1718           12 :                       ctx = CP_DECL_CONTEXT (ctx);
    1719              : 
    1720          345 :                     bool existed = false;
    1721          345 :                     bitmap &b = ns_mod_assocs.get_or_insert (ctx, &existed);
    1722          345 :                     if (!existed)
    1723          296 :                       b = BITMAP_ALLOC (NULL);
    1724          345 :                     bitmap_set_bit (b, mod);
    1725              :                   }
    1726              :               }
    1727              :         }
    1728              : 
    1729              :       /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL.  */
    1730     33769899 :       bitmap inst_path = NULL;
    1731              :       /* VISIBLE is the regular import bitmap.  */
    1732     33769899 :       bitmap visible = visible_instantiation_path (&inst_path);
    1733              : 
    1734    306968978 :       for (unsigned ix = scopes->length (); ix--;)
    1735              :         {
    1736    273199079 :           tree scope = (*scopes)[ix];
    1737    273199079 :           if (TREE_CODE (scope) == NAMESPACE_DECL)
    1738              :             {
    1739    209966393 :               tree ctx = scope;
    1740    440987586 :               while (DECL_NAMESPACE_INLINE_P (ctx))
    1741    231021193 :                 ctx = CP_DECL_CONTEXT (ctx);
    1742    209966393 :               bitmap *assocs = ns_mod_assocs.get (ctx);
    1743    209966393 :               adl_namespace_fns (scope, visible, inst_path,
    1744              :                                  assocs ? *assocs : NULL);
    1745              :             }
    1746     63232686 :           else if (RECORD_OR_UNION_TYPE_P (scope) && !tentative)
    1747              :             /* We don't need to look at friends when searching for
    1748              :                new decl-reachable entities as they will already be
    1749              :                considered reachable by importers.  */
    1750     48962418 :             adl_class_fns (scope);
    1751              :         }
    1752              : 
    1753     33770195 :       for (auto refs : ns_mod_assocs)
    1754          296 :         BITMAP_FREE (refs.second);
    1755     33769899 :       bitmap_obstack_release (NULL);
    1756              : 
    1757     33769899 :       fns = value;
    1758     33769899 :       dedup (false);
    1759     33769899 :     }
    1760              : 
    1761     48218699 :   return fns;
    1762              : }
    1763              : 
    1764              : static bool qualified_namespace_lookup (tree, name_lookup *);
    1765              : static void consider_binding_level (tree name,
    1766              :                                     best_match <tree, const char *> &bm,
    1767              :                                     cp_binding_level *lvl,
    1768              :                                     bool look_within_fields,
    1769              :                                     enum lookup_name_fuzzy_kind kind);
    1770              : 
    1771              : /* ADL lookup of NAME.  FNS is the result of regular lookup, and we
    1772              :    don't add duplicates to it.  ARGS is the vector of call
    1773              :    arguments (which will not be empty).  TENTATIVE is true when
    1774              :    this is early lookup only for the purpose of finding more
    1775              :    decl-reachable declarations.  */
    1776              : 
    1777              : tree
    1778     48218699 : lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args,
    1779              :                       bool tentative/*=false*/)
    1780              : {
    1781     48218699 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    1782     48218699 :   name_lookup lookup (name);
    1783     48218699 :   lookup.tentative = tentative;
    1784     48218699 :   return lookup.search_adl (fns, args);
    1785     48218699 : }
    1786              : 
    1787              : /* FNS is an overload set of conversion functions.  Return the
    1788              :    overloads converting to TYPE.  */
    1789              : 
    1790              : static tree
    1791       671886 : extract_conversion_operator (tree fns, tree type)
    1792              : {
    1793       671886 :   tree convs = NULL_TREE;
    1794       671886 :   tree tpls = NULL_TREE;
    1795              : 
    1796      1918021 :   for (ovl_iterator iter (fns); iter; ++iter)
    1797              :     {
    1798       863318 :       if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
    1799       553689 :         convs = lookup_add (*iter, convs);
    1800              : 
    1801       863318 :       if (TREE_CODE (*iter) == TEMPLATE_DECL)
    1802       191509 :         tpls = lookup_add (*iter, tpls);
    1803              :     }
    1804              : 
    1805       671886 :   if (!convs)
    1806       256562 :     convs = tpls;
    1807              : 
    1808       671886 :   return convs;
    1809              : }
    1810              : 
    1811              : /* Binary search of (ordered) MEMBER_VEC for NAME.  */
    1812              : 
    1813              : static tree
    1814   3104167498 : member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
    1815              : {
    1816  18319716871 :   for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
    1817              :     {
    1818  12938496037 :       unsigned mid = (lo + hi) / 2;
    1819  12938496037 :       tree binding = (*member_vec)[mid];
    1820  25876992074 :       tree binding_name = OVL_NAME (binding);
    1821              : 
    1822  12938496037 :       if (binding_name > name)
    1823              :         hi = mid;
    1824   6443340858 :       else if (binding_name < name)
    1825   5616226696 :         lo = mid + 1;
    1826              :       else
    1827              :         return binding;
    1828              :     }
    1829              : 
    1830              :   return NULL_TREE;
    1831              : }
    1832              : 
    1833              : /* Linear search of (unordered) MEMBER_VEC for NAME.  */
    1834              : 
    1835              : static tree
    1836    851183416 : member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
    1837              : {
    1838   7466835035 :   for (int ix = member_vec->length (); ix--;)
    1839   6679603522 :     if (tree binding = (*member_vec)[ix])
    1840   6679603522 :       if (OVL_NAME (binding) == name)
    1841              :         return binding;
    1842              : 
    1843              :   return NULL_TREE;
    1844              : }
    1845              : 
    1846              : /* Linear search of (partially ordered) fields of KLASS for NAME.  */
    1847              : 
    1848              : static tree
    1849   3135837144 : fields_linear_search (tree klass, tree name, bool want_type)
    1850              : {
    1851  40413159348 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1852              :     {
    1853  37450056378 :       tree decl = fields;
    1854              : 
    1855  37450056378 :       if (TREE_CODE (decl) == FIELD_DECL
    1856  37450056378 :           && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1857              :         {
    1858     36091862 :           if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
    1859              :             return temp;
    1860              :         }
    1861              : 
    1862  37450043809 :       if (DECL_NAME (decl) != name)
    1863  37230165415 :         continue;
    1864              : 
    1865    219878394 :       if (TREE_CODE (decl) == USING_DECL)
    1866              :         {
    1867       916503 :           decl = strip_using_decl (decl);
    1868       916503 :           if (is_overloaded_fn (decl))
    1869       408387 :             continue;
    1870              :         }
    1871              : 
    1872    219470007 :       if (TYPE_DECL_WAS_UNNAMED (decl))
    1873              :         /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    1874              :            purposes.  */
    1875           22 :         continue;
    1876              : 
    1877    219469985 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1878              :         /* Functions are found separately.  */
    1879     46279437 :         continue;
    1880              : 
    1881    173190548 :       if (!want_type || DECL_DECLARES_TYPE_P (decl))
    1882              :         return decl;
    1883              :     }
    1884              : 
    1885              :   return NULL_TREE;
    1886              : }
    1887              : 
    1888              : /* Like fields_linear_search, but specific for "_" name.  There can be multiple
    1889              :    name-independent non-static data members and in that case a TREE_LIST with the
    1890              :    ambiguous decls should be returned.  */
    1891              : 
    1892              : static tree
    1893          685 : name_independent_linear_search (tree val, tree klass, tree name)
    1894              : {
    1895         3292 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1896              :     {
    1897         2607 :       tree decl = fields;
    1898              : 
    1899         2607 :       if (TREE_CODE (decl) == FIELD_DECL
    1900         2607 :           && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1901              :         {
    1902            0 :           if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, false))
    1903              :             {
    1904            0 :               decl = temp;
    1905            0 :               goto add;
    1906              :             }
    1907              :         }
    1908              : 
    1909         2607 :       if (DECL_NAME (decl) != name)
    1910         2549 :         continue;
    1911              : 
    1912           58 :       if (TREE_CODE (decl) == USING_DECL)
    1913              :         {
    1914            0 :           decl = strip_using_decl (decl);
    1915            0 :           if (is_overloaded_fn (decl))
    1916            0 :             continue;
    1917              :         }
    1918              : 
    1919           58 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1920              :         /* Functions are found separately.  */
    1921            0 :         continue;
    1922              : 
    1923           58 :     add:
    1924           58 :       if (val == NULL_TREE)
    1925              :         val = decl;
    1926              :       else
    1927              :         {
    1928            7 :           if (TREE_CODE (val) != TREE_LIST)
    1929              :             {
    1930            5 :               if (TREE_CODE (val) == OVERLOAD
    1931            0 :                   && OVL_DEDUP_P (val)
    1932            5 :                   && TREE_CODE (decl) == USING_DECL)
    1933              :                 {
    1934            0 :                   val = ovl_make (decl, val);
    1935            0 :                   continue;
    1936              :                 }
    1937            5 :               val = tree_cons (NULL_TREE, val, NULL_TREE);
    1938            5 :               TREE_TYPE (val) = error_mark_node;
    1939              :             }
    1940            7 :           if (TREE_CODE (decl) == TREE_LIST)
    1941            0 :             val = chainon (decl, val);
    1942              :           else
    1943              :             {
    1944            7 :               val = tree_cons (NULL_TREE, decl, val);
    1945            7 :               TREE_TYPE (val) = error_mark_node;
    1946              :             }
    1947              :         }
    1948              :     }
    1949              : 
    1950          685 :   return val;
    1951              : }
    1952              : 
    1953              : /* Look for NAME member inside of anonymous aggregate ANON.  Although
    1954              :    such things should only contain FIELD_DECLs, we check that too
    1955              :    late, and would give very confusing errors if we weren't
    1956              :    permissive here.  */
    1957              : 
    1958              : tree
    1959     36091862 : search_anon_aggr (tree anon, tree name, bool want_type)
    1960              : {
    1961     36091862 :   gcc_assert (COMPLETE_TYPE_P (anon));
    1962     36091862 :   tree ret = get_class_binding_direct (anon, name, want_type);
    1963     36091862 :   return ret;
    1964              : }
    1965              : 
    1966              : /* Look for NAME as an immediate member of KLASS (including
    1967              :    anon-members or unscoped enum member).  TYPE_OR_FNS is zero for
    1968              :    regular search.  >0 to get a type binding (if there is one) and <0
    1969              :    if you want (just) the member function binding.
    1970              : 
    1971              :    Use this if you do not want lazy member creation.  */
    1972              : 
    1973              : tree
    1974   6303469601 : get_class_binding_direct (tree klass, tree name, bool want_type)
    1975              : {
    1976   6303469601 :   gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
    1977              : 
    1978              :   /* Conversion operators can only be found by the marker conversion
    1979              :      operator name.  */
    1980   6303469601 :   bool conv_op = IDENTIFIER_CONV_OP_P (name);
    1981   6303469601 :   tree lookup = conv_op ? conv_op_identifier : name;
    1982   6303469601 :   tree val = NULL_TREE;
    1983   6303469601 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    1984              : 
    1985   6303469601 :   if (COMPLETE_TYPE_P (klass) && member_vec)
    1986              :     {
    1987   3104007068 :       val = member_vec_binary_search (member_vec, lookup);
    1988   3104007068 :       if (!val)
    1989              :         ;
    1990    826954145 :       else if (TREE_CODE (val) == OVERLOAD
    1991    826954145 :                && OVL_NAME_INDEPENDENT_DECL_P (val))
    1992              :         {
    1993          161 :           if (want_type)
    1994              :             {
    1995          120 :               while (TREE_CODE (val) == OVERLOAD
    1996          120 :                      && OVL_NAME_INDEPENDENT_DECL_P (val))
    1997           63 :                 val = OVL_CHAIN (val);
    1998           57 :               if (STAT_HACK_P (val))
    1999           12 :                 val = STAT_TYPE (val);
    2000           45 :               else if (!DECL_DECLARES_TYPE_P (val))
    2001              :                 val = NULL_TREE;
    2002              :             }
    2003              :           else
    2004              :             {
    2005              :               /* OVERLOAD with a special OVL_NAME_INDEPENDENT_DECL_P
    2006              :                  flag is used under the hood to represent lookup
    2007              :                  results which include name-independent declarations,
    2008              :                  and get_class_binding_direct is turning that into
    2009              :                  TREE_LIST representation (which the callers expect for
    2010              :                  ambiguous lookups) instead.
    2011              :                  There are 2 reasons for that:
    2012              :                  1) in order to keep the member_vec binary search fast, I
    2013              :                  think it is better to keep OVL_NAME usable on all elements
    2014              :                  because having to special case TREE_LIST would slow
    2015              :                  everything down;
    2016              :                  2) the callers need to be able to chain the results anyway
    2017              :                  and so need an unshared TREE_LIST they can tweak/destroy.  */
    2018              :               tree ovl = val;
    2019              :               val = NULL_TREE;
    2020          220 :               while (TREE_CODE (ovl) == OVERLOAD
    2021          220 :                      && OVL_NAME_INDEPENDENT_DECL_P (ovl))
    2022              :                 {
    2023          116 :                   val = tree_cons (NULL_TREE, OVL_FUNCTION (ovl), val);
    2024          116 :                   TREE_TYPE (val) = error_mark_node;
    2025          116 :                   ovl = OVL_CHAIN (ovl);
    2026              :                 }
    2027          104 :               if (STAT_HACK_P (ovl))
    2028            6 :                 val = tree_cons (NULL_TREE, STAT_DECL (ovl), val);
    2029              :               else
    2030           98 :                 val = tree_cons (NULL_TREE, ovl, val);
    2031          104 :               TREE_TYPE (val) = error_mark_node;
    2032              :             }
    2033              :         }
    2034    826953984 :       else if (STAT_HACK_P (val))
    2035          201 :         val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
    2036    826953783 :       else if (want_type && !DECL_DECLARES_TYPE_P (val))
    2037              :         val = NULL_TREE;
    2038              :     }
    2039              :   else
    2040              :     {
    2041   3199462533 :       if (member_vec && !want_type)
    2042    851183416 :         val = member_vec_linear_search (member_vec, lookup);
    2043              : 
    2044   3199462533 :       if (id_equal (lookup, "_") && !want_type)
    2045          685 :         val = name_independent_linear_search (val, klass, lookup);
    2046   3199461848 :       else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
    2047              :         /* Dependent using declarations are a 'field', make sure we
    2048              :            return that even if we saw an overload already.  */
    2049   3135835680 :         if (tree field_val = fields_linear_search (klass, lookup, want_type))
    2050              :           {
    2051    172732710 :             if (!val)
    2052              :               val = field_val;
    2053            0 :             else if (TREE_CODE (field_val) == USING_DECL)
    2054            0 :               val = ovl_make (field_val, val);
    2055              :           }
    2056              :     }
    2057              : 
    2058              :   /* Extract the conversion operators asked for, unless the general
    2059              :      conversion operator was requested.   */
    2060   6303469601 :   if (val && conv_op)
    2061              :     {
    2062     33883849 :       gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
    2063     33883849 :       val = OVL_CHAIN (val);
    2064     33883849 :       if (tree type = TREE_TYPE (name))
    2065       671886 :         val = extract_conversion_operator (val, type);
    2066              :     }
    2067              : 
    2068   6303469601 :   return val;
    2069              : }
    2070              : 
    2071              : /* We're about to lookup NAME in KLASS.  Make sure any lazily declared
    2072              :    members are now declared.  */
    2073              : 
    2074              : static void
    2075   3414093876 : maybe_lazily_declare (tree klass, tree name)
    2076              : {
    2077              :   /* See big comment about module_state::write_pendings regarding adding
    2078              :      a check bit.  */
    2079   3414093876 :   if (modules_p ())
    2080      9962250 :     lazy_load_pendings (TYPE_NAME (klass));
    2081              : 
    2082              :   /* Lazily declare functions, if we're going to search these.  */
    2083   3414093876 :   if (IDENTIFIER_CTOR_P (name))
    2084              :     {
    2085     86375599 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
    2086      3641122 :         lazily_declare_fn (sfk_constructor, klass);
    2087     86375599 :       if (CLASSTYPE_LAZY_COPY_CTOR (klass))
    2088      5817926 :         lazily_declare_fn (sfk_copy_constructor, klass);
    2089     86375599 :       if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
    2090      4761400 :         lazily_declare_fn (sfk_move_constructor, klass);
    2091              :     }
    2092   3327718277 :   else if (IDENTIFIER_DTOR_P (name))
    2093              :     {
    2094     79333717 :       if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
    2095      6738683 :         lazily_declare_fn (sfk_destructor, klass);
    2096              :     }
    2097   3248384560 :   else if (name == assign_op_identifier)
    2098              :     {
    2099     13884984 :       if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
    2100      1587524 :         lazily_declare_fn (sfk_copy_assignment, klass);
    2101     13884984 :       if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
    2102      1068786 :         lazily_declare_fn (sfk_move_assignment, klass);
    2103              :     }
    2104   3414093876 : }
    2105              : 
    2106              : /* Look for NAME's binding in exactly KLASS.  See
    2107              :    get_class_binding_direct for argument description.  Does lazy
    2108              :    special function creation as necessary.  */
    2109              : 
    2110              : tree
    2111   5760590731 : get_class_binding (tree klass, tree name, bool want_type /*=false*/)
    2112              : {
    2113   5760590731 :   klass = complete_type (klass);
    2114              : 
    2115   5760590731 :   if (COMPLETE_TYPE_P (klass))
    2116   3414093876 :     maybe_lazily_declare (klass, name);
    2117              : 
    2118   5760590731 :   return get_class_binding_direct (klass, name, want_type);
    2119              : }
    2120              : 
    2121              : /* Find the slot containing overloads called 'NAME'.  If there is no
    2122              :    such slot and the class is complete, create an empty one, at the
    2123              :    correct point in the sorted member vector.  Otherwise return NULL.
    2124              :    Deals with conv_op marker handling.  */
    2125              : 
    2126              : tree *
    2127    344481851 : find_member_slot (tree klass, tree name)
    2128              : {
    2129    344481851 :   bool complete_p = COMPLETE_TYPE_P (klass);
    2130              : 
    2131    344481851 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2132    344481851 :   if (!member_vec)
    2133              :     {
    2134     24736952 :       vec_alloc (member_vec, 8);
    2135     24736952 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2136     24736952 :       if (complete_p)
    2137              :         /* If the class is complete but had no member_vec, we need to
    2138              :            add the TYPE_FIELDS into it.  We're also most likely to be
    2139              :            adding ctors & dtors, so ask for 6 spare slots (the
    2140              :            abstract cdtors and their clones).  */
    2141      2164030 :         member_vec = set_class_bindings (klass, 6);
    2142              :     }
    2143              : 
    2144    344481851 :   if (IDENTIFIER_CONV_OP_P (name))
    2145      2672130 :     name = conv_op_identifier;
    2146              : 
    2147    344481851 :   unsigned ix, length = member_vec->length ();
    2148   4153207147 :   for (ix = 0; ix < length; ix++)
    2149              :     {
    2150   3976972113 :       tree *slot = &(*member_vec)[ix];
    2151   7953944226 :       tree fn_name = OVL_NAME (*slot);
    2152              : 
    2153   3976972113 :       if (fn_name == name)
    2154              :         {
    2155              :           /* If we found an existing slot, it must be a function set.
    2156              :              Even with insertion after completion, because those only
    2157              :              happen with artificial fns that have unspellable names.
    2158              :              This means we do not have to deal with the stat hack
    2159              :              either.  */
    2160    159866740 :           gcc_checking_assert (OVL_P (*slot));
    2161    159866740 :           if (name == conv_op_identifier)
    2162              :             {
    2163       637777 :               gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
    2164              :               /* Skip the conv-op marker. */
    2165       637777 :               slot = &OVL_CHAIN (*slot);
    2166              :             }
    2167    159866740 :           return slot;
    2168              :         }
    2169              : 
    2170   3817105373 :       if (complete_p && fn_name > name)
    2171              :         break;
    2172              :     }
    2173              : 
    2174              :   /* No slot found, add one if the class is complete.  */
    2175    184615111 :   if (complete_p)
    2176              :     {
    2177              :       /* Do exact allocation, as we don't expect to add many.  */
    2178     32664332 :       gcc_assert (name != conv_op_identifier);
    2179     32664332 :       vec_safe_reserve_exact (member_vec, 1);
    2180     32664332 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2181     32664332 :       member_vec->quick_insert (ix, NULL_TREE);
    2182     32664332 :       return &(*member_vec)[ix];
    2183              :     }
    2184              : 
    2185              :   return NULL;
    2186              : }
    2187              : 
    2188              : /* KLASS is an incomplete class to which we're adding a method NAME.
    2189              :    Add a slot and deal with conv_op marker handling.  */
    2190              : 
    2191              : tree *
    2192    151950761 : add_member_slot (tree klass, tree name)
    2193              : {
    2194    151950761 :   gcc_assert (!COMPLETE_TYPE_P (klass));
    2195              : 
    2196    151950761 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2197    151950761 :   vec_safe_push (member_vec, NULL_TREE);
    2198    151950761 :   CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2199              : 
    2200    151950761 :   tree *slot = &member_vec->last ();
    2201    151950761 :   if (IDENTIFIER_CONV_OP_P (name))
    2202              :     {
    2203              :       /* Install the marker prefix.  */
    2204      2034353 :       *slot = ovl_make (conv_op_marker, NULL_TREE);
    2205      2034353 :       slot = &OVL_CHAIN (*slot);
    2206              :     }
    2207              : 
    2208    151950761 :   return slot;
    2209              : }
    2210              : 
    2211              : /* Comparison function to compare two MEMBER_VEC entries by name.
    2212              :    Because we can have duplicates during insertion of TYPE_FIELDS, we
    2213              :    do extra checking so deduping doesn't have to deal with so many
    2214              :    cases.  */
    2215              : 
    2216              : static int
    2217   5936229655 : member_name_cmp (const void *a_p, const void *b_p)
    2218              : {
    2219   5936229655 :   tree a = *(const tree *)a_p;
    2220   5936229655 :   tree b = *(const tree *)b_p;
    2221   5936229655 :   tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
    2222   5936229655 :   tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
    2223              : 
    2224   5936229655 :   gcc_checking_assert (name_a && name_b);
    2225   5936229655 :   if (name_a != name_b)
    2226   8717508278 :     return name_a < name_b ? -1 : +1;
    2227              : 
    2228     18672481 :   if (name_a == conv_op_identifier)
    2229              :     {
    2230              :       /* Strip the conv-op markers. */
    2231      1108282 :       gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
    2232              :                            && OVL_FUNCTION (b) == conv_op_marker);
    2233       554141 :       a = OVL_CHAIN (a);
    2234       554141 :       b = OVL_CHAIN (b);
    2235              :     }
    2236              : 
    2237     18672481 :   if (TREE_CODE (a) == OVERLOAD)
    2238      6610780 :     a = OVL_FUNCTION (a);
    2239     18672481 :   if (TREE_CODE (b) == OVERLOAD)
    2240      6174531 :     b = OVL_FUNCTION (b);
    2241              : 
    2242     18672481 :   if (id_equal (name_a, "_"))
    2243              :     {
    2244              :       /* Sort name-independent members first.  */
    2245          306 :       if (name_independent_decl_p (a))
    2246              :         {
    2247          276 :           if (name_independent_decl_p (b))
    2248              :             {
    2249          225 :               if (DECL_UID (a) != DECL_UID (b))
    2250          225 :                 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
    2251            0 :               gcc_assert (a == b);
    2252              :               return 0;
    2253              :             }
    2254              :           else
    2255              :             return -1;
    2256              :         }
    2257           30 :       else if (name_independent_decl_p (b))
    2258              :         return +1;
    2259              :     }
    2260              : 
    2261              :   /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
    2262     18672175 :   if (TREE_CODE (a) != TREE_CODE (b))
    2263              :     {
    2264              :       /* If one of them is a TYPE_DECL, it loses.  */
    2265     17013605 :       if (TREE_CODE (a) == TYPE_DECL)
    2266              :         return +1;
    2267     17013272 :       else if (TREE_CODE (b) == TYPE_DECL)
    2268              :         return -1;
    2269              : 
    2270              :       /* If one of them is a USING_DECL, it loses.  */
    2271     17012930 :       if (TREE_CODE (a) == USING_DECL)
    2272              :         return +1;
    2273      8797184 :       else if (TREE_CODE (b) == USING_DECL)
    2274              :         return -1;
    2275              : 
    2276              :       /* There are no other cases with different kinds of decls, as
    2277              :          duplicate detection should have kicked in earlier.  However,
    2278              :          some erroneous cases get though. */
    2279            0 :       gcc_assert (errorcount);
    2280              :     }
    2281              : 
    2282              :   /* Using source location would be the best thing here, but we can
    2283              :      get identically-located decls in the following circumstances:
    2284              : 
    2285              :      1) duplicate artificial type-decls for the same type.
    2286              : 
    2287              :      2) pack expansions of using-decls.
    2288              : 
    2289              :      We should not be doing #1, but in either case it doesn't matter
    2290              :      how we order these.  Use UID as a proxy for source ordering, so
    2291              :      that identically-located decls still have a well-defined stable
    2292              :      ordering.  */
    2293      1658570 :   if (DECL_UID (a) != DECL_UID (b))
    2294      1658552 :     return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
    2295           18 :   gcc_assert (a == b);
    2296              :   return 0;
    2297              : }
    2298              : 
    2299              : static struct {
    2300              :   gt_pointer_operator new_value;
    2301              :   void *cookie;
    2302              : } resort_data;
    2303              : 
    2304              : /* This routine compares two fields like member_name_cmp but using the
    2305              :    pointer operator in resort_field_decl_data.  We don't have to deal
    2306              :    with duplicates here.  */
    2307              : 
    2308              : static int
    2309     10965189 : resort_member_name_cmp (const void *a_p, const void *b_p)
    2310              : {
    2311     10965189 :   tree a = *(const tree *)a_p;
    2312     10965189 :   tree b = *(const tree *)b_p;
    2313     21930378 :   tree name_a = OVL_NAME (a);
    2314     21930378 :   tree name_b = OVL_NAME (b);
    2315              : 
    2316     10965189 :   resort_data.new_value (&name_a, &name_a, resort_data.cookie);
    2317     10965189 :   resort_data.new_value (&name_b, &name_b, resort_data.cookie);
    2318              : 
    2319     10965189 :   gcc_checking_assert (name_a != name_b);
    2320              : 
    2321     10965189 :   return name_a < name_b ? -1 : +1;
    2322              : }
    2323              : 
    2324              : /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered.  */
    2325              : 
    2326              : void
    2327        56539 : resort_type_member_vec (void *obj, void */*orig_obj*/,
    2328              :                         gt_pointer_operator new_value, void* cookie)
    2329              : {
    2330        56539 :   if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
    2331              :     {
    2332        56539 :       resort_data.new_value = new_value;
    2333        56539 :       resort_data.cookie = cookie;
    2334        56539 :       member_vec->qsort (resort_member_name_cmp);
    2335              :     }
    2336        56539 : }
    2337              : 
    2338              : /* Recursively count the number of fields in KLASS, including anonymous
    2339              :    union members.  */
    2340              : 
    2341              : static unsigned
    2342     72071967 : count_class_fields (tree klass)
    2343              : {
    2344     72071967 :   unsigned n_fields = 0;
    2345              : 
    2346    590079101 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2347    518007134 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2348              :       /* Functions are dealt with separately.  */;
    2349    244220773 :     else if (TREE_CODE (fields) == FIELD_DECL
    2350    244220773 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2351       292621 :       n_fields += count_class_fields (TREE_TYPE (fields));
    2352    243928152 :     else if (DECL_NAME (fields))
    2353              :       {
    2354    219430707 :         if (TYPE_DECL_WAS_UNNAMED (fields))
    2355              :           /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    2356              :              purposes.  */
    2357           10 :           continue;
    2358    219430697 :         n_fields += 1;
    2359              :       }
    2360              : 
    2361     72071967 :   return n_fields;
    2362              : }
    2363              : 
    2364              : /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
    2365              :    Recurse for anonymous members.  MEMBER_VEC must have space.  */
    2366              : 
    2367              : static void
    2368     25533354 : member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
    2369              : {
    2370    434838597 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2371    409305243 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2372              :       /* Functions are handled separately.  */;
    2373    135518894 :     else if (TREE_CODE (fields) == FIELD_DECL
    2374    135518894 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2375       282489 :       member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
    2376    135236405 :     else if (DECL_NAME (fields))
    2377              :       {
    2378    130241996 :         tree field = fields;
    2379              :         /* Mark a conv-op USING_DECL with the conv-op-marker.  */
    2380    130241996 :         if (TREE_CODE (field) == USING_DECL
    2381    130241996 :             && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
    2382       138512 :           field = ovl_make (conv_op_marker, field);
    2383    130103484 :         else if (TYPE_DECL_WAS_UNNAMED (field))
    2384              :           /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    2385              :              purposes.  */
    2386            9 :           continue;
    2387    130241987 :         member_vec->quick_push (field);
    2388              :       }
    2389     25533354 : }
    2390              : 
    2391              : /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
    2392              :    MEMBER_VEC must have space.  */
    2393              : 
    2394              : static void
    2395           21 : member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
    2396              : {
    2397           21 :   for (tree values = TYPE_VALUES (enumtype);
    2398           51 :        values; values = TREE_CHAIN (values))
    2399           30 :     member_vec->quick_push (TREE_VALUE (values));
    2400           21 : }
    2401              : 
    2402              : /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
    2403              :    DeDup adjacent DECLS of the same name.  We already dealt with
    2404              :    conflict resolution when adding the fields or methods themselves.
    2405              :    There are four cases (which could all be combined):
    2406              :    1) a TYPE_DECL and non TYPE_DECL.  Deploy STAT_HACK as appropriate.
    2407              :    2) a USING_DECL and an overload.  If the USING_DECL is dependent,
    2408              :    it wins.  Otherwise the OVERLOAD does.
    2409              :    3) two USING_DECLS.
    2410              :    4) name-independent members plus others. ...
    2411              : 
    2412              :    member_name_cmp will have ordered duplicates as
    2413              :    <name_independent><fns><using><type>  */
    2414              : 
    2415              : static void
    2416     25250886 : member_vec_dedup (vec<tree, va_gc> *member_vec)
    2417              : {
    2418     25250886 :   unsigned len = member_vec->length ();
    2419     25250886 :   unsigned store = 0;
    2420              : 
    2421     25250886 :   if (!len)
    2422              :     return;
    2423              : 
    2424     50501768 :   tree name = OVL_NAME ((*member_vec)[0]);
    2425    302238129 :   for (unsigned jx, ix = 0; ix < len; ix = jx)
    2426              :     {
    2427              :       tree current = NULL_TREE;
    2428              :       tree to_type = NULL_TREE;
    2429              :       tree to_using = NULL_TREE;
    2430              :       tree marker = NULL_TREE;
    2431              :       unsigned name_independent = ix;
    2432              : 
    2433    558591107 :       for (jx = ix; jx < len; jx++)
    2434              :         {
    2435    533340223 :           tree next = (*member_vec)[jx];
    2436    533340223 :           if (jx != ix)
    2437              :             {
    2438    256352978 :               tree next_name = OVL_NAME (next);
    2439    256352978 :               if (next_name != name)
    2440              :                 {
    2441              :                   name = next_name;
    2442              :                   break;
    2443              :                 }
    2444              :             }
    2445              : 
    2446    281603862 :           if (IDENTIFIER_CONV_OP_P (name))
    2447              :             {
    2448      2172865 :               marker = next;
    2449      2172865 :               next = OVL_CHAIN (next);
    2450              :             }
    2451              : 
    2452    281603862 :           if (TREE_CODE (next) == USING_DECL)
    2453              :             {
    2454     12348266 :               if (IDENTIFIER_CTOR_P (name))
    2455              :                 /* Dependent inherited ctor. */
    2456       296490 :                 continue;
    2457              : 
    2458     12051776 :               next = strip_using_decl (next);
    2459     12051776 :               if (TREE_CODE (next) == USING_DECL)
    2460              :                 {
    2461      9960373 :                   to_using = next;
    2462      9960373 :                   continue;
    2463              :                 }
    2464              : 
    2465      2091403 :               if (is_overloaded_fn (next))
    2466      1763306 :                 continue;
    2467              :             }
    2468              : 
    2469    269583693 :           if (DECL_DECLARES_TYPE_P (next))
    2470              :             {
    2471     75894285 :               to_type = next;
    2472     75894285 :               continue;
    2473              :             }
    2474              : 
    2475    193689408 :           if (name_independent_decl_p (next))
    2476          160 :             name_independent = jx + 1;
    2477    193689248 :           else if (!current)
    2478    193508609 :             current = next;
    2479              :         }
    2480              : 
    2481    276987245 :       if (to_using)
    2482              :         {
    2483      9832153 :           if (!current)
    2484              :             current = to_using;
    2485              :           else
    2486      2026722 :             current = ovl_make (to_using, current);
    2487              :         }
    2488              : 
    2489    276987245 :       if (to_type)
    2490              :         {
    2491     75611664 :           if (!current)
    2492              :             current = to_type;
    2493              :           else
    2494          159 :             current = stat_hack (current, to_type);
    2495              :         }
    2496              : 
    2497    276987405 :       for (unsigned kx = name_independent; kx > ix; --kx)
    2498          160 :         if (!current)
    2499           98 :           current = (*member_vec)[kx - 1];
    2500           62 :         else if (current == to_type)
    2501            6 :           current = stat_hack ((*member_vec)[kx - 1], to_type);
    2502              :         else
    2503              :           {
    2504           56 :             current = ovl_make ((*member_vec)[kx - 1], current);
    2505           56 :             OVL_NAME_INDEPENDENT_DECL_P (current) = 1;
    2506              :           }
    2507              : 
    2508    276987245 :       if (current)
    2509              :         {
    2510    276925643 :           if (marker)
    2511              :             {
    2512      2034353 :               OVL_CHAIN (marker) = current;
    2513      2034353 :               current = marker;
    2514              :             }
    2515    276925643 :           (*member_vec)[store++] = current;
    2516              :         }
    2517              :     }
    2518              : 
    2519     29929103 :   while (store++ < len)
    2520      4678219 :     member_vec->pop ();
    2521              : }
    2522              : 
    2523              : /* Add the non-function members to CLASSTYPE_MEMBER_VEC.  If there is
    2524              :    no existing MEMBER_VEC and fewer than 8 fields, do nothing.  We
    2525              :    know there must be at least 1 field -- the self-reference
    2526              :    TYPE_DECL, except for anon aggregates, which will have at least
    2527              :    one field anyway.  If EXTRA < 0, always create the vector.  */
    2528              : 
    2529              : vec<tree, va_gc> *
    2530     71779322 : set_class_bindings (tree klass, int extra)
    2531              : {
    2532     71779322 :   unsigned n_fields = count_class_fields (klass);
    2533     71779322 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2534              : 
    2535     71779322 :   if (member_vec || n_fields >= 8 || extra < 0)
    2536              :     {
    2537              :       /* Append the new fields.  */
    2538     25250859 :       vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
    2539     25250859 :       member_vec_append_class_fields (member_vec, klass);
    2540              :     }
    2541              : 
    2542     71779322 :   if (member_vec)
    2543              :     {
    2544     25250859 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2545     25250859 :       member_vec->qsort (member_name_cmp);
    2546     25250859 :       member_vec_dedup (member_vec);
    2547              :     }
    2548              : 
    2549     71779322 :   return member_vec;
    2550              : }
    2551              : 
    2552              : /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case.  */
    2553              : 
    2554              : void
    2555           45 : insert_late_enum_def_bindings (tree klass, tree enumtype)
    2556              : {
    2557           45 :   int n_fields;
    2558           45 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2559              : 
    2560              :   /* The enum bindings will already be on the TYPE_FIELDS, so don't
    2561              :      count them twice.  */
    2562           45 :   if (!member_vec)
    2563           24 :     n_fields = count_class_fields (klass);
    2564              :   else
    2565           21 :     n_fields = list_length (TYPE_VALUES (enumtype));
    2566              : 
    2567           45 :   if (member_vec || n_fields >= 8)
    2568              :     {
    2569           27 :       vec_safe_reserve_exact (member_vec, n_fields);
    2570           27 :       if (CLASSTYPE_MEMBER_VEC (klass))
    2571           21 :         member_vec_append_enum_values (member_vec, enumtype);
    2572              :       else
    2573            6 :         member_vec_append_class_fields (member_vec, klass);
    2574           27 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2575           27 :       member_vec->qsort (member_name_cmp);
    2576           27 :       member_vec_dedup (member_vec);
    2577              :     }
    2578           45 : }
    2579              : 
    2580              : /* The binding oracle; see cp-tree.h.  */
    2581              : 
    2582              : cp_binding_oracle_function *cp_binding_oracle;
    2583              : 
    2584              : /* If we have a binding oracle, ask it for all namespace-scoped
    2585              :    definitions of NAME.  */
    2586              : 
    2587              : static inline void
    2588   5323542067 : query_oracle (tree name)
    2589              : {
    2590   5323542067 :   if (!cp_binding_oracle)
    2591              :     return;
    2592              : 
    2593              :   /* LOOKED_UP holds the set of identifiers that we have already
    2594              :      looked up with the oracle.  */
    2595            0 :   static hash_set<tree> looked_up;
    2596            0 :   if (looked_up.add (name))
    2597              :     return;
    2598              : 
    2599            0 :   cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
    2600              : }
    2601              : 
    2602              : #ifndef ENABLE_SCOPE_CHECKING
    2603              : #  define ENABLE_SCOPE_CHECKING 0
    2604              : #else
    2605              : #  define ENABLE_SCOPE_CHECKING 1
    2606              : #endif
    2607              : 
    2608              : /* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
    2609              : 
    2610              : static GTY((deletable)) cxx_binding *free_bindings;
    2611              : 
    2612              : /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
    2613              :    field to NULL.  */
    2614              : 
    2615              : static inline void
    2616   1406951306 : cxx_binding_init (cxx_binding *binding, tree value, tree type)
    2617              : {
    2618   1406951306 :   binding->value = value;
    2619   1406951306 :   binding->type = type;
    2620   1406951306 :   binding->previous = NULL;
    2621              : }
    2622              : 
    2623              : /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
    2624              : 
    2625              : static cxx_binding *
    2626   1406951306 : cxx_binding_make (tree value, tree type)
    2627              : {
    2628   1406951306 :   cxx_binding *binding = free_bindings;
    2629              : 
    2630   1406951306 :   if (binding)
    2631   1397350740 :     free_bindings = binding->previous;
    2632              :   else
    2633      9600566 :     binding = ggc_alloc<cxx_binding> ();
    2634              : 
    2635              :   /* Clear flags by default.  */
    2636   1406951306 :   LOCAL_BINDING_P (binding) = false;
    2637   1406951306 :   INHERITED_VALUE_BINDING_P (binding) = false;
    2638   1406951306 :   HIDDEN_TYPE_BINDING_P (binding) = false;
    2639              : 
    2640   1406951306 :   cxx_binding_init (binding, value, type);
    2641              : 
    2642   1406951306 :   return binding;
    2643              : }
    2644              : 
    2645              : /* Put BINDING back on the free list.  */
    2646              : 
    2647              : static inline void
    2648   1406948441 : cxx_binding_free (cxx_binding *binding)
    2649              : {
    2650   1406948441 :   binding->scope = NULL;
    2651   1406948441 :   binding->previous = free_bindings;
    2652   1406948441 :   free_bindings = binding;
    2653    927947487 : }
    2654              : 
    2655              : /* Create a new binding for NAME (with the indicated VALUE and TYPE
    2656              :    bindings) in the class scope indicated by SCOPE.  */
    2657              : 
    2658              : static cxx_binding *
    2659    479003795 : new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
    2660              : {
    2661    479003795 :   cp_class_binding cb = {cxx_binding_make (value, type), name};
    2662    479003795 :   cxx_binding *binding = cb.base;
    2663    479003795 :   vec_safe_push (scope->class_shadowed, cb);
    2664    479003795 :   binding->scope = scope;
    2665    479003795 :   return binding;
    2666              : }
    2667              : 
    2668              : /* Make DECL the innermost binding for ID.  The LEVEL is the binding
    2669              :    level at which this declaration is being bound.  */
    2670              : 
    2671              : void
    2672    338445421 : push_binding (tree id, tree decl, cp_binding_level* level)
    2673              : {
    2674    338445421 :   cxx_binding *binding;
    2675              : 
    2676    338445421 :   if (level != class_binding_level)
    2677              :     {
    2678     13047687 :       binding = cxx_binding_make (decl, NULL_TREE);
    2679     13047687 :       binding->scope = level;
    2680              :     }
    2681              :   else
    2682    325397734 :     binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
    2683              : 
    2684              :   /* Now, fill in the binding information.  */
    2685    338445421 :   binding->previous = IDENTIFIER_BINDING (id);
    2686    338445421 :   LOCAL_BINDING_P (binding) = (level != class_binding_level);
    2687              : 
    2688              :   /* And put it on the front of the list of bindings for ID.  */
    2689    338445421 :   IDENTIFIER_BINDING (id) = binding;
    2690    338445421 : }
    2691              : 
    2692              : /* Remove the binding for DECL which should be the innermost binding
    2693              :    for ID.  */
    2694              : 
    2695              : void
    2696    951555074 : pop_local_binding (tree id, tree decl)
    2697              : {
    2698   1880791465 :   if (!id || IDENTIFIER_ANON_P (id))
    2699              :     /* It's easiest to write the loops that call this function without
    2700              :        checking whether or not the entities involved have names.  We
    2701              :        get here for such an entity.  */
    2702              :     return;
    2703              : 
    2704              :   /* Get the innermost binding for ID.  */
    2705    927947933 :   cxx_binding *binding = IDENTIFIER_BINDING (id);
    2706              : 
    2707              :   /* The name should be bound.  */
    2708    927947933 :   gcc_assert (binding != NULL);
    2709              : 
    2710              :   /* The DECL will be either the ordinary binding or the type binding
    2711              :      for this identifier.  Remove that binding.  We don't have to
    2712              :      clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
    2713              :      away.  */
    2714    927947933 :   if (binding->value == decl)
    2715    927947115 :     binding->value = NULL_TREE;
    2716          818 :   else if (binding->type == decl)
    2717          150 :     binding->type = NULL_TREE;
    2718              :   /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    2719              :      purposes.  */
    2720          668 :   else if (!TYPE_DECL_WAS_UNNAMED (decl))
    2721              :     {
    2722              :       /* Name-independent variable was found after at least one declaration
    2723              :          with the same name.  */
    2724          667 :       gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
    2725          667 :       if (TREE_VALUE (binding->value) != decl)
    2726              :         {
    2727          259 :           binding->value = nreverse (binding->value);
    2728              :           /* Skip over TREE_LISTs added in pushdecl for check_local_shadow
    2729              :              detected declarations, formerly at the tail, now at the start
    2730              :              of the list.  */
    2731          268 :           while (TREE_PURPOSE (binding->value) == error_mark_node)
    2732            9 :             binding->value = TREE_CHAIN (binding->value);
    2733              :         }
    2734          667 :       gcc_assert (TREE_VALUE (binding->value) == decl);
    2735          667 :       binding->value = TREE_CHAIN (binding->value);
    2736          667 :       while (binding->value
    2737          762 :              && TREE_PURPOSE (binding->value) == error_mark_node)
    2738           95 :         binding->value = TREE_CHAIN (binding->value);
    2739              :     }
    2740              : 
    2741    927947933 :   if (!binding->value && !binding->type)
    2742              :     {
    2743              :       /* We're completely done with the innermost binding for this
    2744              :          identifier.  Unhook it from the list of bindings.  */
    2745    927947487 :       IDENTIFIER_BINDING (id) = binding->previous;
    2746              : 
    2747              :       /* Add it to the free list.  */
    2748    927947487 :       cxx_binding_free (binding);
    2749              :     }
    2750              : }
    2751              : 
    2752              : /* Remove the bindings for the decls of the current level and leave
    2753              :    the current scope.  */
    2754              : 
    2755              : void
    2756    209607872 : pop_bindings_and_leave_scope (void)
    2757              : {
    2758    478637299 :   for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
    2759              :     {
    2760    269029427 :       tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
    2761    538058854 :       tree name = OVL_NAME (decl);
    2762              : 
    2763    269029427 :       pop_local_binding (name, decl);
    2764              :     }
    2765              : 
    2766    209607872 :   leave_scope ();
    2767    209607872 : }
    2768              : 
    2769              : /* Strip non dependent using declarations. If DECL is dependent,
    2770              :    surreptitiously create a typename_type and return it.  */
    2771              : 
    2772              : tree
    2773   9201390136 : strip_using_decl (tree decl)
    2774              : {
    2775   9201390136 :   if (decl == NULL_TREE)
    2776              :     return NULL_TREE;
    2777              : 
    2778   6498670642 :   while (TREE_CODE (decl) == USING_DECL
    2779    119918668 :          && !DECL_DEPENDENT_P (decl)
    2780   6591615046 :          && (LIKELY (!cp_preserve_using_decl)
    2781         6384 :              || TREE_CODE (USING_DECL_DECLS (decl)) == NAMESPACE_DECL))
    2782     92944400 :     decl = USING_DECL_DECLS (decl);
    2783              : 
    2784     26974268 :   if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
    2785   6432700506 :       && USING_DECL_TYPENAME_P (decl))
    2786              :     {
    2787              :       /* We have found a type introduced by a using
    2788              :          declaration at class scope that refers to a dependent
    2789              :          type.
    2790              : 
    2791              :          using typename :: [opt] nested-name-specifier unqualified-id ;
    2792              :       */
    2793       260730 :       decl = make_typename_type (USING_DECL_SCOPE (decl),
    2794       260730 :                                  DECL_NAME (decl),
    2795              :                                  typename_type, tf_error);
    2796       260730 :       if (decl != error_mark_node)
    2797       260730 :         decl = TYPE_NAME (decl);
    2798              :     }
    2799              : 
    2800              :   return decl;
    2801              : }
    2802              : 
    2803              : /* Return true if OVL is an overload for an anticipated builtin.  */
    2804              : 
    2805              : static bool
    2806     24177215 : anticipated_builtin_p (tree ovl)
    2807              : {
    2808     24177215 :   return (TREE_CODE (ovl) == OVERLOAD
    2809     22148867 :           && OVL_HIDDEN_P (ovl)
    2810     31175667 :           && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl)));
    2811              : }
    2812              : 
    2813              : /* BINDING records an existing declaration for a name in the current scope.
    2814              :    But, DECL is another declaration for that same identifier in the
    2815              :    same scope.  This is the `struct stat' hack whereby a non-typedef
    2816              :    class name or enum-name can be bound at the same level as some other
    2817              :    kind of entity.
    2818              :    3.3.7/1
    2819              : 
    2820              :      A class name (9.1) or enumeration name (7.2) can be hidden by the
    2821              :      name of an object, function, or enumerator declared in the same scope.
    2822              :      If a class or enumeration name and an object, function, or enumerator
    2823              :      are declared in the same scope (in any order) with the same name, the
    2824              :      class or enumeration name is hidden wherever the object, function, or
    2825              :      enumerator name is visible.
    2826              : 
    2827              :    It's the responsibility of the caller to check that
    2828              :    inserting this name is valid here.  Returns nonzero if the new binding
    2829              :    was successful.  */
    2830              : 
    2831              : static bool
    2832       210573 : supplement_binding (cxx_binding *binding, tree decl)
    2833              : {
    2834       210573 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    2835              : 
    2836       210573 :   tree bval = binding->value;
    2837       210573 :   bool ok = true;
    2838       210573 :   if (bval
    2839       210573 :       && TREE_CODE (bval) == TREE_LIST
    2840       210581 :       && name_independent_decl_p (TREE_VALUE (bval)))
    2841              :     bval = TREE_VALUE (bval);
    2842       210573 :   tree target_bval = strip_using_decl (bval);
    2843       210573 :   tree target_decl = strip_using_decl (decl);
    2844              : 
    2845        29563 :   if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
    2846        29380 :       && target_decl != target_bval
    2847       239944 :       && (TREE_CODE (target_bval) != TYPE_DECL
    2848              :           /* We allow pushing an enum multiple times in a class
    2849              :              template in order to handle late matching of underlying
    2850              :              type on an opaque-enum-declaration followed by an
    2851              :              enum-specifier.  */
    2852        29266 :           || (processing_template_decl
    2853         9254 :               && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
    2854            0 :               && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
    2855            0 :               && (dependent_type_p (ENUM_UNDERLYING_TYPE
    2856              :                                     (TREE_TYPE (target_decl)))
    2857            0 :                   || dependent_type_p (ENUM_UNDERLYING_TYPE
    2858              :                                        (TREE_TYPE (target_bval)))))))
    2859              :     /* The new name is the type name.  */
    2860          105 :     binding->type = decl;
    2861       210468 :   else if (/* TARGET_BVAL is null when push_class_level_binding moves
    2862              :               an inherited type-binding out of the way to make room
    2863              :               for a new value binding.  */
    2864              :            !target_bval
    2865              :            /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
    2866              :               has been used in a non-class scope prior declaration.
    2867              :               In that case, we should have already issued a
    2868              :               diagnostic; for graceful error recovery purpose, pretend
    2869              :               this was the intended declaration for that name.  */
    2870       210468 :            || target_bval == error_mark_node
    2871              :            /* If TARGET_BVAL is anticipated but has not yet been
    2872              :               declared, pretend it is not there at all.  */
    2873       420936 :            || anticipated_builtin_p (target_bval))
    2874            0 :     binding->value = decl;
    2875       210468 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2876        29638 :            && DECL_ARTIFICIAL (target_bval)
    2877        29605 :            && target_decl != target_bval
    2878       240064 :            && (TREE_CODE (target_decl) != TYPE_DECL
    2879        29416 :                || same_type_p (TREE_TYPE (target_decl),
    2880              :                                TREE_TYPE (target_bval))))
    2881              :     {
    2882              :       /* The old binding was a type name.  It was placed in
    2883              :          VALUE field because it was thought, at the point it was
    2884              :          declared, to be the only entity with such a name.  Move the
    2885              :          type name into the type slot; it is now hidden by the new
    2886              :          binding.  */
    2887        29584 :       binding->type = bval;
    2888        29584 :       binding->value = decl;
    2889        29584 :       binding->value_is_inherited = false;
    2890              :     }
    2891       180884 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2892           54 :            && TREE_CODE (target_decl) == TYPE_DECL
    2893           54 :            && DECL_NAME (target_decl) == DECL_NAME (target_bval)
    2894           54 :            && binding->scope->kind != sk_class
    2895       180884 :            && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
    2896              :                /* If either type involves template parameters, we must
    2897              :                   wait until instantiation.  */
    2898            0 :                || uses_template_parms (TREE_TYPE (target_decl))
    2899            0 :                || uses_template_parms (TREE_TYPE (target_bval))))
    2900              :     /* We have two typedef-names, both naming the same type to have
    2901              :        the same name.  In general, this is OK because of:
    2902              : 
    2903              :          [dcl.typedef]
    2904              : 
    2905              :          In a given scope, a typedef specifier can be used to redefine
    2906              :          the name of any type declared in that scope to refer to the
    2907              :          type to which it already refers.
    2908              : 
    2909              :        However, in class scopes, this rule does not apply due to the
    2910              :        stricter language in [class.mem] prohibiting redeclarations of
    2911              :        members.  */
    2912              :     ok = false;
    2913              :   /* There can be two block-scope declarations of the same variable,
    2914              :      so long as they are `extern' declarations.  However, there cannot
    2915              :      be two declarations of the same static data member:
    2916              : 
    2917              :        [class.mem]
    2918              : 
    2919              :        A member shall not be declared twice in the
    2920              :        member-specification.  */
    2921       180884 :   else if (VAR_P (target_decl)
    2922            9 :            && VAR_P (target_bval)
    2923            9 :            && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
    2924       180890 :            && !DECL_CLASS_SCOPE_P (target_decl))
    2925              :     {
    2926            0 :       duplicate_decls (decl, binding->value);
    2927            0 :       ok = false;
    2928              :     }
    2929       180884 :   else if (TREE_CODE (decl) == NAMESPACE_DECL
    2930            0 :            && TREE_CODE (bval) == NAMESPACE_DECL
    2931            0 :            && DECL_NAMESPACE_ALIAS (decl)
    2932            0 :            && DECL_NAMESPACE_ALIAS (bval)
    2933       180884 :            && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
    2934              :     /* [namespace.alias]
    2935              : 
    2936              :       In a declarative region, a namespace-alias-definition can be
    2937              :       used to redefine a namespace-alias declared in that declarative
    2938              :       region to refer only to the namespace to which it already
    2939              :       refers.  */
    2940              :     ok = false;
    2941       180884 :   else if (TREE_CODE (bval) == USING_DECL
    2942       180884 :            && CONST_DECL_USING_P (decl))
    2943              :     /* Let the clone hide the using-decl that introduced it.  */
    2944       180685 :     binding->value = decl;
    2945          199 :   else if (name_independent_decl_p (decl))
    2946              :     {
    2947           68 :       if (cxx_dialect < cxx26)
    2948           48 :         pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
    2949              :                  "name-independent declarations only available with "
    2950              :                  "%<-std=c++2c%> or %<-std=gnu++2c%>");
    2951           68 :       binding->value = name_lookup::ambiguous (decl, binding->value);
    2952              :     }
    2953          131 :   else if (binding->scope->kind != sk_class
    2954            9 :            && TREE_CODE (decl) == USING_DECL
    2955          140 :            && decls_match (target_bval, target_decl))
    2956              :     /* Since P1787 (DR 36) it is OK to redeclare entities via using-decl,
    2957              :        except in class scopes.  */
    2958              :     ok = false;
    2959              :   else
    2960              :     {
    2961          122 :       if (!error_operand_p (bval))
    2962          122 :         diagnose_name_conflict (decl, bval);
    2963              :       ok = false;
    2964              :     }
    2965              : 
    2966       421146 :   return ok;
    2967       210573 : }
    2968              : 
    2969              : /* Diagnose a name conflict between DECL and BVAL.
    2970              : 
    2971              :    This is non-static so maybe_push_used_methods can use it and avoid changing
    2972              :    the diagnostic for inherit/using4.C; otherwise it should not be used from
    2973              :    outside this file.  */
    2974              : 
    2975              : void
    2976          227 : diagnose_name_conflict (tree decl, tree bval)
    2977              : {
    2978          227 :   auto_diagnostic_group d;
    2979          227 :   if (TREE_CODE (decl) == TREE_CODE (bval)
    2980          171 :       && TREE_CODE (decl) != NAMESPACE_DECL
    2981          165 :       && !DECL_DECLARES_FUNCTION_P (decl)
    2982          117 :       && (TREE_CODE (decl) != TYPE_DECL
    2983           21 :           || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
    2984          341 :       && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
    2985              :     {
    2986           87 :       if (concept_definition_p (decl))
    2987            6 :         error ("redeclaration of %q#D with different template parameters",
    2988              :                decl);
    2989              :       else
    2990           72 :         error ("redeclaration of %q#D", decl);
    2991              :     }
    2992              :   else
    2993          149 :     error ("%q#D conflicts with a previous declaration", decl);
    2994              : 
    2995          227 :   inform (location_of (bval), "previous declaration %q#D", bval);
    2996          227 : }
    2997              : 
    2998              : /* Replace BINDING's current value on its scope's name list with
    2999              :    NEWVAL.  */
    3000              : 
    3001              : static void
    3002          145 : update_local_overload (cxx_binding *binding, tree newval)
    3003              : {
    3004          145 :   tree *d;
    3005              : 
    3006          187 :   for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
    3007          187 :     if (*d == binding->value)
    3008              :       {
    3009              :         /* Stitch new list node in.  */
    3010           75 :         *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
    3011           75 :         break;
    3012              :       }
    3013          112 :     else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
    3014              :       break;
    3015              : 
    3016          145 :   TREE_VALUE (*d) = newval;
    3017          145 : }
    3018              : 
    3019              : /* Compares the parameter-type-lists of ONE and TWO and
    3020              :    returns false if they are different.  If the DECLs are template
    3021              :    functions, the return types and the template parameter lists are
    3022              :    compared too (DR 565).  */
    3023              : 
    3024              : static bool
    3025      5249478 : matching_fn_p (tree one, tree two)
    3026              : {
    3027      5249478 :   if (TREE_CODE (one) != TREE_CODE (two))
    3028              :     return false;
    3029              : 
    3030      3204374 :   if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
    3031      3204374 :                   TYPE_ARG_TYPES (TREE_TYPE (two))))
    3032              :     return false;
    3033              : 
    3034          455 :   if (TREE_CODE (one) == TEMPLATE_DECL)
    3035              :     {
    3036              :       /* Compare template parms.  */
    3037          778 :       if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
    3038          389 :                                 DECL_TEMPLATE_PARMS (two)))
    3039              :         return false;
    3040              : 
    3041              :       /* And return type.  */
    3042          281 :       if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
    3043              :                         TREE_TYPE (TREE_TYPE (two))))
    3044              :         return false;
    3045              :     }
    3046              : 
    3047           77 :   if (!equivalently_constrained (one, two))
    3048              :     return false;
    3049              : 
    3050              :   return true;
    3051              : }
    3052              : 
    3053              : /* Push DECL into nonclass LEVEL BINDING or SLOT.  OLD is the current
    3054              :    binding value (possibly with anticipated builtins stripped).
    3055              :    Diagnose conflicts and return updated decl.  */
    3056              : 
    3057              : static tree
    3058   1288554187 : update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
    3059              :                 tree old, tree decl, bool hiding = false)
    3060              : {
    3061   1288554187 :   tree old_type = NULL_TREE;
    3062   1288554187 :   bool hide_type = false;
    3063   1288554187 :   bool hide_value = false;
    3064   1288554187 :   bool name_independent_p = false;
    3065              : 
    3066   1288554187 :   if (!slot)
    3067              :     {
    3068    914900381 :       old_type = binding->type;
    3069    914900381 :       hide_type = HIDDEN_TYPE_BINDING_P (binding);
    3070    914900381 :       if (!old_type)
    3071    914900378 :         hide_value = hide_type, hide_type = false;
    3072    914900381 :       name_independent_p = name_independent_decl_p (decl);
    3073              :     }
    3074    373653806 :   else if (STAT_HACK_P (*slot))
    3075              :     {
    3076          330 :       old_type = STAT_TYPE (*slot);
    3077          330 :       hide_type = STAT_TYPE_HIDDEN_P (*slot);
    3078          330 :       hide_value = STAT_DECL_HIDDEN_P (*slot);
    3079              :     }
    3080              : 
    3081   1288554187 :   tree to_val = decl;
    3082   1288554187 :   tree to_type = old_type;
    3083   1288554187 :   bool local_overload = false;
    3084              : 
    3085   1288554187 :   gcc_assert (!level || level->kind == sk_namespace ? !binding
    3086              :               : level->kind != sk_class && !slot);
    3087              : 
    3088   1288554187 :   if (old == error_mark_node)
    3089        98333 :     old = NULL_TREE;
    3090              : 
    3091   1288554187 :   tree old_bval = old;
    3092   1288554187 :   old = strip_using_decl (old);
    3093              : 
    3094   1288554187 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    3095              :     {
    3096              :       /* Pushing an artificial decl.  We should not find another
    3097              :          artificial decl here already -- lookup_elaborated_type will
    3098              :          have already found it.  */
    3099      6908050 :       gcc_checking_assert (!to_type
    3100              :                            && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
    3101              : 
    3102              :       if (old)
    3103              :         {
    3104              :           /* Put DECL into the type slot.  */
    3105              :           gcc_checking_assert (!to_type);
    3106              :           hide_type = hiding;
    3107              :           to_type = decl;
    3108              :           to_val = old_bval;
    3109              :         }
    3110              :       else
    3111              :         hide_value = hiding;
    3112              : 
    3113      6908050 :       goto done;
    3114              :     }
    3115              : 
    3116   1281646137 :   if (old && DECL_IMPLICIT_TYPEDEF_P (old))
    3117              :     {
    3118              :       /* OLD is an implicit typedef.  Move it to to_type.  */
    3119        69742 :       gcc_checking_assert (!to_type);
    3120              : 
    3121              :       to_type = old_bval;
    3122              :       hide_type = hide_value;
    3123              :       old = NULL_TREE;
    3124              :       hide_value = false;
    3125              :     }
    3126              : 
    3127   1281646137 :   if (DECL_DECLARES_FUNCTION_P (decl))
    3128              :     {
    3129    334775878 :       if (!old)
    3130              :         ;
    3131     23703692 :       else if (OVL_P (old))
    3132              :         {
    3133    536185833 :           for (ovl_iterator iter (old); iter; ++iter)
    3134              :             {
    3135    258319212 :               tree fn = *iter;
    3136              : 
    3137    258319212 :               if (iter.using_p () && matching_fn_p (fn, decl))
    3138              :                 {
    3139           35 :                   gcc_checking_assert (!iter.hidden_p ());
    3140              :                   /* If a function declaration in namespace scope or
    3141              :                      block scope has the same name and the same
    3142              :                      parameter-type- list (8.3.5) as a function
    3143              :                      introduced by a using-declaration, and the
    3144              :                      declarations do not declare the same function,
    3145              :                      the program is ill-formed.  [namespace.udecl]/14 */
    3146           35 :                   if (tree match = duplicate_decls (decl, fn, hiding))
    3147            8 :                     return match;
    3148              :                   else
    3149              :                     /* FIXME: To preserve existing error behavior, we
    3150              :                        still push the decl.  This might change.  */
    3151           27 :                     diagnose_name_conflict (decl, fn);
    3152              :                 }
    3153              :             }
    3154     23703684 :         }
    3155              :       else
    3156            0 :         goto conflict;
    3157              : 
    3158    334775870 :       if (to_type != old_type
    3159        38043 :           && warn_shadow
    3160            6 :           && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
    3161    334775876 :           && !(DECL_IN_SYSTEM_HEADER (decl)
    3162            0 :                && DECL_IN_SYSTEM_HEADER (to_type)))
    3163            6 :         warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
    3164              :                  decl, to_type);
    3165              : 
    3166    334775870 :       local_overload = old && level && level->kind != sk_namespace;
    3167    334775870 :       to_val = ovl_insert (decl, old, -int (hiding));
    3168              :     }
    3169    946870259 :   else if (old)
    3170              :     {
    3171          450 :       if (name_independent_p)
    3172          399 :         to_val = name_lookup::ambiguous (decl, old);
    3173           51 :       else if (TREE_CODE (old) != TREE_CODE (decl))
    3174              :         /* Different kinds of decls conflict.  */
    3175            0 :         goto conflict;
    3176           51 :       else if (TREE_CODE (old) == TYPE_DECL)
    3177              :         {
    3178            6 :           if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    3179              :             /* Two type decls to the same type.  Do nothing.  */
    3180              :             return old;
    3181              :           else
    3182            0 :             goto conflict;
    3183              :         }
    3184           45 :       else if (TREE_CODE (old) == NAMESPACE_DECL)
    3185              :         {
    3186              :           /* Two maybe-aliased namespaces.  If they're to the same target
    3187              :              namespace, that's ok.  */
    3188            9 :           if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
    3189            6 :             goto conflict;
    3190              : 
    3191              :           /* The new one must be an alias at this point.  */
    3192            3 :           gcc_assert (DECL_NAMESPACE_ALIAS (decl));
    3193              :           return old;
    3194              :         }
    3195           36 :       else if (TREE_CODE (old) == VAR_DECL)
    3196              :         {
    3197           27 :           if (tree match = duplicate_decls (decl, old))
    3198              :             {
    3199            9 :               gcc_checking_assert (!hide_value && !hiding);
    3200              :               return match;
    3201              :             }
    3202              :           else
    3203           18 :             goto conflict;
    3204              :         }
    3205              :       else
    3206              :         {
    3207            9 :         conflict:
    3208           33 :           diagnose_name_conflict (decl, old_bval);
    3209           33 :           to_val = NULL_TREE;
    3210              :         }
    3211              :     }
    3212    946869809 :   else if (hiding)
    3213        31536 :     hide_value = true;
    3214              : 
    3215   1288554161 :  done:
    3216   1288554161 :   if (to_val)
    3217              :     {
    3218   1288554128 :       if (local_overload)
    3219              :         {
    3220          109 :           gcc_checking_assert (binding->value && OVL_P (binding->value));
    3221          109 :           update_local_overload (binding, to_val);
    3222              :         }
    3223   1288554019 :       else if (level
    3224   1288554019 :                && !(TREE_CODE (decl) == NAMESPACE_DECL
    3225      1018371 :                     && !DECL_NAMESPACE_ALIAS (decl)))
    3226              :         /* Don't add namespaces here.  They're done in
    3227              :            push_namespace.  */
    3228   1287657006 :         add_decl_to_level (level, decl);
    3229              : 
    3230   1288554128 :       if (slot)
    3231              :         {
    3232    373653768 :           if (STAT_HACK_P (*slot))
    3233              :             {
    3234          330 :               STAT_TYPE (*slot) = to_type;
    3235          330 :               STAT_DECL (*slot) = to_val;
    3236          330 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    3237          330 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    3238              :             }
    3239    373653438 :           else if (to_type || hide_value)
    3240              :             {
    3241       215888 :               *slot = stat_hack (to_val, to_type);
    3242       215888 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    3243       215888 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    3244              :             }
    3245              :           else
    3246              :             {
    3247    373437550 :               gcc_checking_assert (!hide_type);
    3248    373437550 :               *slot = to_val;
    3249              :             }
    3250              :         }
    3251              :       else
    3252              :         {
    3253    914900360 :           binding->type = to_type;
    3254    914900360 :           binding->value = to_val;
    3255    914900360 :           HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
    3256              :         }
    3257              :     }
    3258              : 
    3259              :   return decl;
    3260              : }
    3261              : 
    3262              : /* Table of identifiers to extern C declarations (or LISTS thereof).  */
    3263              : 
    3264              : static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
    3265              : 
    3266              : /* DECL has C linkage. If we have an existing instance, make sure the
    3267              :    new one is compatible.  Make sure it has the same exception
    3268              :    specification [7.5, 7.6].  Add DECL to the map.  */
    3269              : 
    3270              : static void
    3271    292062762 : check_extern_c_conflict (tree decl)
    3272              : {
    3273              :   /* Ignore artificial or system header decls.  */
    3274    292062762 :   if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
    3275    291844155 :     return;
    3276              : 
    3277              :   /* This only applies to decls at namespace scope.  */
    3278       218607 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
    3279              :     return;
    3280              : 
    3281       218595 :   if (!extern_c_decls)
    3282        40053 :     extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
    3283              : 
    3284       218595 :   tree *slot = extern_c_decls
    3285       218595 :     ->find_slot_with_hash (DECL_NAME (decl),
    3286       218595 :                            IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
    3287       218595 :   if (tree old = *slot)
    3288              :     {
    3289          397 :       if (TREE_CODE (old) == OVERLOAD)
    3290           18 :         old = OVL_FUNCTION (old);
    3291              : 
    3292          397 :       int mismatch = 0;
    3293          397 :       if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
    3294              :         ; /* If they're in the same context, we'll have already complained
    3295              :              about a (possible) mismatch, when inserting the decl.  */
    3296          379 :       else if (!decls_match (decl, old))
    3297              :         mismatch = 1;
    3298          358 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    3299          704 :                && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
    3300          346 :                                       TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
    3301              :                                       ce_normal))
    3302              :         mismatch = -1;
    3303          355 :       else if (DECL_ASSEMBLER_NAME_SET_P (old))
    3304            9 :         SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
    3305              : 
    3306            9 :       if (mismatch)
    3307              :         {
    3308           24 :           auto_diagnostic_group d;
    3309           24 :           pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3310              :                    "conflicting C language linkage declaration %q#D", decl);
    3311           24 :           inform (DECL_SOURCE_LOCATION (old),
    3312              :                   "previous declaration %q#D", old);
    3313           24 :           if (mismatch < 0)
    3314            3 :             inform (DECL_SOURCE_LOCATION (decl),
    3315              :                     "due to different exception specifications");
    3316           24 :         }
    3317              :       else
    3318              :         {
    3319          373 :           if (old == *slot)
    3320              :             /* The hash table expects OVERLOADS, so construct one with
    3321              :                OLD as both the function and the chain.  This allocate
    3322              :                an excess OVERLOAD node, but it's rare to have multiple
    3323              :                extern "C" decls of the same name.  And we save
    3324              :                complicating the hash table logic (which is used
    3325              :                elsewhere).  */
    3326          367 :             *slot = ovl_make (old, old);
    3327              : 
    3328          373 :           slot = &OVL_CHAIN (*slot);
    3329              : 
    3330              :           /* Chain it on for c_linkage_binding's use.  */
    3331          373 :           *slot = tree_cons (NULL_TREE, decl, *slot);
    3332              :         }
    3333              :     }
    3334              :   else
    3335       218198 :     *slot = decl;
    3336              : }
    3337              : 
    3338              : /* Returns a list of C-linkage decls with the name NAME.  Used in
    3339              :    c-family/c-pragma.cc to implement redefine_extname pragma.  */
    3340              : 
    3341              : tree
    3342           18 : c_linkage_bindings (tree name)
    3343              : {
    3344           18 :   if (extern_c_decls)
    3345           24 :     if (tree *slot = extern_c_decls
    3346           12 :         ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
    3347              :       {
    3348            6 :         tree result = *slot;
    3349            6 :         if (TREE_CODE (result) == OVERLOAD)
    3350            3 :           result = OVL_CHAIN (result);
    3351            6 :         return result;
    3352              :       }
    3353              : 
    3354              :   return NULL_TREE;
    3355              : }
    3356              : 
    3357              : /* Subroutine of check_local_shadow.  */
    3358              : 
    3359              : static void
    3360          174 : inform_shadowed (tree shadowed)
    3361              : {
    3362          174 :   inform (DECL_SOURCE_LOCATION (shadowed),
    3363              :           "shadowed declaration is here");
    3364          174 : }
    3365              : 
    3366              : /* DECL is being declared at a local scope.  Emit suitable shadow
    3367              :    warnings.  */
    3368              : 
    3369              : static tree
    3370    914900381 : check_local_shadow (tree decl)
    3371              : {
    3372              :   /* Don't complain about the parms we push and then pop
    3373              :      while tentatively parsing a function declarator.  */
    3374    914900381 :   if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
    3375              :     return NULL_TREE;
    3376              : 
    3377    677634864 :   if (DECL_FUNCTION_SCOPE_P (decl))
    3378              :     {
    3379    428764263 :       tree ctx = DECL_CONTEXT (decl);
    3380    857528526 :       if (DECL_CLONED_FUNCTION_P (ctx)
    3381    401840435 :           || DECL_TEMPLATE_INSTANTIATED (ctx)
    3382    343360104 :           || (DECL_LANG_SPECIFIC (ctx)
    3383    343360104 :               && DECL_DEFAULTED_FN (ctx))
    3384    809971271 :           || (LAMBDA_FUNCTION_P (ctx)
    3385     11466020 :               && LAMBDA_EXPR_REGEN_INFO (CLASSTYPE_LAMBDA_EXPR
    3386              :                                          (DECL_CONTEXT (ctx)))))
    3387              :         /* It suffices to check shadowing only when actually parsing.
    3388              :            So punt for clones, instantiations, defaulted functions and
    3389              :            regenerated lambdas.  This optimization helps reduce lazy
    3390              :            loading cascades with modules.  */
    3391              :         return NULL_TREE;
    3392              :     }
    3393              : 
    3394    590154272 :   tree old = NULL_TREE;
    3395    590154272 :   cp_binding_level *old_scope = NULL;
    3396    590154272 :   if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
    3397              :     {
    3398      2779157 :       old = binding->value;
    3399      2779157 :       old_scope = binding->scope;
    3400              :     }
    3401              : 
    3402      2779157 :   if (old
    3403      2779157 :       && (TREE_CODE (old) == PARM_DECL
    3404      1409958 :           || VAR_P (old)
    3405       154215 :           || (TREE_CODE (old) == TYPE_DECL
    3406        68673 :               && (!DECL_ARTIFICIAL (old)
    3407        55687 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3408      2693521 :       && DECL_FUNCTION_SCOPE_P (old)
    3409      5415169 :       && (!DECL_ARTIFICIAL (decl)
    3410      2422287 :           || is_capture_proxy (decl)
    3411       381076 :           || DECL_IMPLICIT_TYPEDEF_P (decl)
    3412       381046 :           || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
    3413              :     {
    3414              :       /* DECL shadows a local thing possibly of interest.  */
    3415              : 
    3416              :       /* DR 2211: check that captures and parameters
    3417              :          do not have the same name. */
    3418      2254972 :       if (is_capture_proxy (decl))
    3419              :         {
    3420      2041211 :           if (current_lambda_expr ()
    3421      2041211 :               && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
    3422           61 :               && TREE_CODE (old) == PARM_DECL
    3423      2041236 :               && DECL_NAME (decl) != this_identifier)
    3424           25 :             error_at (DECL_SOURCE_LOCATION (old),
    3425              :                       "lambda parameter %qD "
    3426              :                       "previously declared as a capture", old);
    3427      2041211 :           return NULL_TREE;
    3428              :         }
    3429              :       /* Don't complain if it's from an enclosing function.  */
    3430       213761 :       else if (DECL_CONTEXT (old) == current_function_decl
    3431       213761 :                && ((TREE_CODE (decl) != PARM_DECL
    3432       167710 :                     && TREE_CODE (old) == PARM_DECL)
    3433              :                    /* We should also give an error for
    3434              :                        [x=1]{ int x; }  */
    3435       157837 :                    || (is_capture_proxy (old)
    3436           20 :                        && !is_normal_capture_proxy (old))))
    3437              :         {
    3438              :           /* Go to where the parms should be and see if we find
    3439              :              them there.  */
    3440         9879 :           cp_binding_level *b = current_binding_level->level_chain;
    3441              : 
    3442         9879 :           if (in_function_try_handler && b->kind == sk_catch)
    3443           39 :             b = b->level_chain;
    3444              : 
    3445              :           /* Skip artificially added scopes which aren't present
    3446              :              in the C++ standard, e.g. for function-try-block or
    3447              :              ctor/dtor cleanups.  */
    3448         9966 :           while (b->artificial)
    3449           87 :             b = b->level_chain;
    3450              : 
    3451              :           /* [basic.scope.param] A parameter name shall not be redeclared
    3452              :              in the outermost block of the function definition.  */
    3453         9879 :           if (b->kind == sk_function_parms)
    3454              :             {
    3455          120 :               if (name_independent_decl_p (decl))
    3456              :                 return old;
    3457              : 
    3458          102 :               auto_diagnostic_group d;
    3459          102 :               bool emit = true;
    3460          102 :               if (DECL_EXTERNAL (decl))
    3461           40 :                 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
    3462              :                                 "declaration of %q#D shadows a parameter",
    3463              :                                 decl);
    3464              :               else
    3465           62 :                 error_at (DECL_SOURCE_LOCATION (decl),
    3466              :                           "declaration of %q#D shadows a parameter", decl);
    3467          102 :               if (emit)
    3468          102 :                 inform (DECL_SOURCE_LOCATION (old),
    3469              :                         "%q#D previously declared here", old);
    3470          102 :               return NULL_TREE;
    3471          102 :             }
    3472              :         }
    3473              : 
    3474              :       /* The local structure or class can't use parameters of
    3475              :          the containing function anyway.  */
    3476       213641 :       if (DECL_CONTEXT (old) != current_function_decl)
    3477              :         {
    3478        46051 :           for (cp_binding_level *scope = current_binding_level;
    3479       110972 :                scope != old_scope; scope = scope->level_chain)
    3480       109257 :             if (scope->kind == sk_class
    3481       157080 :                 && !LAMBDA_TYPE_P (scope->this_entity))
    3482              :               return NULL_TREE;
    3483              :         }
    3484              :       /* Error if redeclaring a local declared in a
    3485              :          init-statement or in the condition of an if or
    3486              :          switch statement when the new declaration is in the
    3487              :          outermost block of the controlled statement.
    3488              :          Redeclaring a variable from a for or while condition is
    3489              :          detected elsewhere.  */
    3490       167590 :       else if (VAR_P (old)
    3491       146732 :                && old_scope == current_binding_level->level_chain
    3492         2912 :                && (old_scope->kind == sk_cond
    3493         2807 :                    || old_scope->kind == sk_for
    3494         2708 :                    || old_scope->kind == sk_template_for))
    3495              :         {
    3496          225 :           if (name_independent_decl_p (decl))
    3497              :             return old;
    3498              : 
    3499          142 :           auto_diagnostic_group d;
    3500          142 :           bool emit = true;
    3501          142 :           if (DECL_EXTERNAL (decl))
    3502           48 :             emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
    3503              :                             "redeclaration of %q#D", decl);
    3504              :           else
    3505           94 :             error_at (DECL_SOURCE_LOCATION (decl),
    3506              :                       "redeclaration of %q#D", decl);
    3507          142 :           if (emit)
    3508          142 :             inform (DECL_SOURCE_LOCATION (old),
    3509              :                     "%q#D previously declared here", old);
    3510          142 :           return NULL_TREE;
    3511          142 :         }
    3512              :       /* C++11:
    3513              :          3.3.3/3:  The name declared in an exception-declaration (...)
    3514              :          shall not be redeclared in the outermost block of the handler.
    3515              :          3.3.3/2:  A parameter name shall not be redeclared (...) in
    3516              :          the outermost block of any handler associated with a
    3517              :          function-try-block.  */
    3518       167365 :       else if (TREE_CODE (old) == VAR_DECL
    3519       146507 :                && old_scope == current_binding_level->level_chain
    3520         2687 :                && old_scope->kind == sk_catch)
    3521              :         {
    3522           18 :           if (name_independent_decl_p (decl))
    3523              :             return old;
    3524              : 
    3525           15 :           auto_diagnostic_group d;
    3526           15 :           bool emit;
    3527           15 :           if (DECL_EXTERNAL (decl))
    3528            6 :             emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
    3529              :                             "redeclaration of %q#D", decl);
    3530              :           else
    3531            9 :             emit = permerror (DECL_SOURCE_LOCATION (decl),
    3532              :                               "redeclaration of %q#D", decl);
    3533           15 :           if (emit)
    3534           15 :             inform (DECL_SOURCE_LOCATION (old),
    3535              :                     "%q#D previously declared here", old);
    3536           15 :           return NULL_TREE;
    3537           15 :         }
    3538              : 
    3539              :       /* Don't emit -Wshadow* warnings for name-independent decls.  */
    3540       169062 :       if (name_independent_decl_p (decl) || name_independent_decl_p (old))
    3541              :         return NULL_TREE;
    3542              : 
    3543              :       /* If '-Wshadow=compatible-local' is specified without other
    3544              :          -Wshadow= flags, we will warn only when the type of the
    3545              :          shadowing variable (DECL) can be converted to that of the
    3546              :          shadowed parameter (OLD_LOCAL). The reason why we only check
    3547              :          if DECL's type can be converted to OLD_LOCAL's type (but not the
    3548              :          other way around) is because when users accidentally shadow a
    3549              :          parameter, more than often they would use the variable
    3550              :          thinking (mistakenly) it's still the parameter. It would be
    3551              :          rare that users would use the variable in the place that
    3552              :          expects the parameter but thinking it's a new decl.
    3553              :          If either object is a TYPE_DECL, '-Wshadow=compatible-local'
    3554              :          warns regardless of whether one of the types involved
    3555              :          is a subclass of the other, since that is never okay.  */
    3556              : 
    3557       168889 :       enum opt_code warning_code;
    3558       168889 :       if (warn_shadow)
    3559              :         warning_code = OPT_Wshadow;
    3560       168820 :       else if ((TREE_CODE (decl) == TYPE_DECL)
    3561       168820 :                ^ (TREE_CODE (old) == TYPE_DECL))
    3562              :         /* If exactly one is a type, they aren't compatible.  */
    3563              :         warning_code = OPT_Wshadow_local;
    3564       168799 :       else if ((TREE_TYPE (old)
    3565       168799 :                 && TREE_TYPE (decl)
    3566       168796 :                 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    3567        33867 :                || TREE_CODE (decl) == TYPE_DECL
    3568        23268 :                || TREE_CODE (old) == TYPE_DECL
    3569       192067 :                || (!dependent_type_p (TREE_TYPE (decl))
    3570         4478 :                    && !dependent_type_p (TREE_TYPE (old))
    3571              :                    /* If the new decl uses auto, we don't yet know
    3572              :                       its type (the old type cannot be using auto
    3573              :                       at this point, without also being
    3574              :                       dependent).  This is an indication we're
    3575              :                       (now) doing the shadow checking too
    3576              :                       early.  */
    3577         4451 :                    && !type_uses_auto (TREE_TYPE (decl))
    3578         4180 :                    && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
    3579              :                                        decl, LOOKUP_IMPLICIT, tf_none)))
    3580              :         warning_code = OPT_Wshadow_compatible_local;
    3581              :       else
    3582              :         warning_code = OPT_Wshadow_local;
    3583              : 
    3584       168889 :       const char *msg;
    3585       168889 :       if (TREE_CODE (old) == PARM_DECL)
    3586              :         msg = "declaration of %q#D shadows a parameter";
    3587       157821 :       else if (is_capture_proxy (old))
    3588              :         msg = "declaration of %qD shadows a lambda capture";
    3589              :       else
    3590       157606 :         msg = "declaration of %qD shadows a previous local";
    3591              : 
    3592       168889 :       auto_diagnostic_group d;
    3593       168889 :       if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
    3594          117 :         inform_shadowed (old);
    3595       168889 :       return NULL_TREE;
    3596       168889 :     }
    3597              : 
    3598    587899300 :   if (!warn_shadow)
    3599              :     return NULL_TREE;
    3600              : 
    3601              :   /* Don't emit -Wshadow for name-independent decls.  */
    3602          904 :   if (name_independent_decl_p (decl))
    3603              :     return NULL_TREE;
    3604              : 
    3605              :   /* Don't warn for artificial things that are not implicit typedefs.  */
    3606          754 :   if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
    3607              :     return NULL_TREE;
    3608              : 
    3609          403 :   if (nonlambda_method_basetype ())
    3610           93 :     if (tree member = lookup_member (current_nonlambda_class_type (),
    3611           93 :                                      DECL_NAME (decl), /*protect=*/0,
    3612              :                                      /*want_type=*/false, tf_warning_or_error))
    3613              :       {
    3614           33 :         member = MAYBE_BASELINK_FUNCTIONS (member);
    3615              : 
    3616              :         /* Warn if a variable shadows a non-function, or the variable
    3617              :            is a function or a pointer-to-function.  */
    3618           24 :         if ((!OVL_P (member)
    3619            9 :              || TREE_CODE (decl) == FUNCTION_DECL
    3620            9 :              || (TREE_TYPE (decl)
    3621            6 :                  && (TYPE_PTRFN_P (TREE_TYPE (decl))
    3622            6 :                      || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
    3623           60 :             && !warning_suppressed_p (decl, OPT_Wshadow))
    3624              :           {
    3625           27 :             auto_diagnostic_group d;
    3626           27 :             if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
    3627              :                             "declaration of %qD shadows a member of %qT",
    3628              :                             decl, current_nonlambda_class_type ())
    3629           27 :                 && DECL_P (member))
    3630              :               {
    3631           27 :                 inform_shadowed (member);
    3632           27 :                 suppress_warning (decl, OPT_Wshadow);
    3633              :               }
    3634           27 :           }
    3635           33 :         return NULL_TREE;
    3636              :       }
    3637              : 
    3638              :   /* Now look for a namespace shadow.  */
    3639          370 :   old = find_namespace_value (current_namespace, DECL_NAME (decl));
    3640          370 :   if (old
    3641           75 :       && (VAR_P (old)
    3642           51 :           || (TREE_CODE (old) == TYPE_DECL
    3643           15 :               && (!DECL_ARTIFICIAL (old)
    3644            9 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3645           33 :       && !DECL_EXTERNAL (decl)
    3646           30 :       && !instantiating_current_function_p ()
    3647          400 :       && !warning_suppressed_p (decl, OPT_Wshadow))
    3648              :     /* XXX shadow warnings in outer-more namespaces */
    3649              :     {
    3650           30 :       auto_diagnostic_group d;
    3651           30 :       if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
    3652              :                       "declaration of %qD shadows a global declaration",
    3653              :                       decl))
    3654              :         {
    3655           30 :           inform_shadowed (old);
    3656           30 :           suppress_warning (decl, OPT_Wshadow);
    3657              :         }
    3658           30 :       return NULL_TREE;
    3659           30 :     }
    3660              : 
    3661              :   return NULL_TREE;
    3662              : }
    3663              : 
    3664              : /* DECL is being pushed inside function CTX.  Set its context, if
    3665              :    needed.  */
    3666              : 
    3667              : static void
    3668    412650140 : set_decl_context_in_fn (tree ctx, tree decl)
    3669              : {
    3670    412650140 :   if (TREE_CODE (decl) == FUNCTION_DECL
    3671    412650140 :       || (VAR_P (decl) && DECL_EXTERNAL (decl)))
    3672              :     /* Make sure local externs are marked as such.  OMP UDRs really
    3673              :        are nested functions.  */
    3674        61117 :     gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
    3675              :                          && (DECL_NAMESPACE_SCOPE_P (decl)
    3676              :                              || (TREE_CODE (decl) == FUNCTION_DECL
    3677              :                                  && DECL_OMP_DECLARE_REDUCTION_P (decl))));
    3678              : 
    3679    412650140 :   if (!DECL_CONTEXT (decl)
    3680              :       /* When parsing the parameter list of a function declarator,
    3681              :          don't set DECL_CONTEXT to an enclosing function.  */
    3682    413869993 :       && !(TREE_CODE (decl) == PARM_DECL
    3683      1219853 :            && parsing_function_declarator ()))
    3684     93704730 :     DECL_CONTEXT (decl) = ctx;
    3685    412650140 : }
    3686              : 
    3687              : /* DECL is a local extern decl.  Find or create the namespace-scope
    3688              :    decl that it aliases.  Also, determines the linkage of DECL.  */
    3689              : 
    3690              : void
    3691        60670 : push_local_extern_decl_alias (tree decl)
    3692              : {
    3693        60670 :   if (flag_reflection)
    3694              :     {
    3695          802 :       if (lookup_attribute ("internal ", "annotation ",
    3696          802 :                             DECL_ATTRIBUTES (decl)))
    3697            4 :         error_at (DECL_SOURCE_LOCATION (decl),
    3698              :                   "annotation applied to block scope extern %qD",
    3699              :                   decl);
    3700          802 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    3701          851 :         for (tree arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
    3702           53 :           if (lookup_attribute ("internal ", "annotation ",
    3703           53 :                                 DECL_ATTRIBUTES (arg)))
    3704            1 :             error_at (DECL_SOURCE_LOCATION (arg),
    3705              :                       "annotation applied to parameter %qD of block scope "
    3706              :                       "extern", arg);
    3707              :     }
    3708              : 
    3709        60670 :   if (dependent_type_p (TREE_TYPE (decl))
    3710        60670 :       || (processing_template_decl
    3711        39277 :           && VAR_P (decl)
    3712           43 :           && CP_DECL_THREAD_LOCAL_P (decl)))
    3713              :     return;
    3714              :   /* EH specs were not part of the function type prior to c++17, but
    3715              :      we still can't go pushing dependent eh specs into the namespace.  */
    3716        60583 :   if (cxx_dialect < cxx17
    3717         2418 :       && TREE_CODE (decl) == FUNCTION_DECL
    3718        62621 :       && (value_dependent_expression_p
    3719         2038 :           (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
    3720              :     return;
    3721              : 
    3722        60582 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
    3723              :                        || !DECL_TEMPLATE_INFO (decl));
    3724        60582 :   if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
    3725              :     /* We're instantiating a non-dependent local decl, it already
    3726              :        knows the alias.  */
    3727              :     return;
    3728              : 
    3729        60350 :   tree alias = NULL_TREE;
    3730              : 
    3731        60350 :   if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
    3732              :     /* Do not let a VLA creep into a namespace.  Diagnostic will be
    3733              :        emitted in layout_var_decl later.  */
    3734            3 :     alias = error_mark_node;
    3735              :   else
    3736              :     {
    3737              :       /* First look for a decl that matches.  */
    3738        60347 :       tree ns = CP_DECL_CONTEXT (decl);
    3739        60347 :       tree binding = find_namespace_value (ns, DECL_NAME (decl));
    3740              : 
    3741        60347 :       if (binding && TREE_CODE (binding) != TREE_LIST)
    3742          964 :         for (ovl_iterator iter (binding); iter; ++iter)
    3743          668 :           if (decls_match (decl, *iter, /*record_versions*/false))
    3744              :             {
    3745          455 :               alias = *iter;
    3746          455 :               if (!validate_constexpr_redeclaration (alias, decl))
    3747           12 :                 return;
    3748          443 :               if (TREE_CODE (decl) == FUNCTION_DECL)
    3749          178 :                 merge_decl_arguments (decl, alias, false, true, true);
    3750              :               break;
    3751              :             }
    3752              : 
    3753          556 :       if (!alias)
    3754              :         {
    3755              :           /* No existing namespace-scope decl.  Make one.  */
    3756        59892 :           alias = copy_decl (decl);
    3757        59892 :           if (TREE_CODE (alias) == FUNCTION_DECL)
    3758              :             {
    3759              :               /* Recontextualize the parms.  */
    3760        59064 :               for (tree *chain = &DECL_ARGUMENTS (alias);
    3761       105117 :                    *chain; chain = &DECL_CHAIN (*chain))
    3762              :                 {
    3763        46053 :                   tree next = DECL_CHAIN (*chain);
    3764        46053 :                   *chain = copy_decl (*chain);
    3765        46053 :                   DECL_CHAIN (*chain) = next;
    3766        46053 :                   DECL_CONTEXT (*chain) = alias;
    3767              :                 }
    3768              : 
    3769        59064 :               tree type = TREE_TYPE (alias);
    3770        59064 :               for (tree args = TYPE_ARG_TYPES (type);
    3771       164066 :                    args; args = TREE_CHAIN (args))
    3772       105047 :                 if (TREE_PURPOSE (args))
    3773              :                   {
    3774              :                     /* There are default args.  Lose them.  */
    3775           45 :                     tree nargs = NULL_TREE;
    3776           45 :                     tree *chain = &nargs;
    3777           45 :                     for (args = TYPE_ARG_TYPES (type);
    3778          111 :                          args; args = TREE_CHAIN (args))
    3779          111 :                       if (args == void_list_node)
    3780              :                         {
    3781           45 :                           *chain = args;
    3782           45 :                           break;
    3783              :                         }
    3784              :                       else
    3785              :                         {
    3786           66 :                           *chain
    3787           66 :                             = build_tree_list (NULL_TREE, TREE_VALUE (args));
    3788           66 :                           chain = &TREE_CHAIN (*chain);
    3789              :                         }
    3790              : 
    3791           45 :                     bool no_named_args_stdarg
    3792           45 :                       = TYPE_NO_NAMED_ARGS_STDARG_P (type);
    3793           45 :                     tree fn_type
    3794           45 :                       = build_function_type (TREE_TYPE (type), nargs,
    3795              :                                              no_named_args_stdarg);
    3796              : 
    3797           45 :                     fn_type = apply_memfn_quals
    3798           45 :                       (fn_type, type_memfn_quals (type));
    3799              : 
    3800           45 :                     fn_type = build_cp_fntype_variant
    3801           45 :                       (fn_type, type_memfn_rqual (type),
    3802           45 :                        TYPE_RAISES_EXCEPTIONS (type),
    3803           45 :                        TYPE_HAS_LATE_RETURN_TYPE (type));
    3804              : 
    3805           45 :                     TREE_TYPE (alias) = fn_type;
    3806           45 :                     break;
    3807              :                   }
    3808              :             }
    3809              : 
    3810              :           /* This is the real thing.  */
    3811        59892 :           DECL_LOCAL_DECL_P (alias) = false;
    3812              : 
    3813              :           /* Expected default linkage is from the namespace.  */
    3814        59892 :           TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
    3815        59892 :           push_nested_namespace (ns);
    3816        59892 :           alias = pushdecl (alias, /* hiding= */true);
    3817        59892 :           pop_nested_namespace (ns);
    3818        59892 :           if (VAR_P (decl)
    3819          828 :               && CP_DECL_THREAD_LOCAL_P (decl)
    3820        59913 :               && alias != error_mark_node)
    3821           18 :             set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
    3822              : 
    3823              :           /* Adjust visibility.  */
    3824        59892 :           determine_visibility (alias);
    3825              :         }
    3826              :     }
    3827              : 
    3828        60338 :   retrofit_lang_decl (decl);
    3829        60338 :   DECL_LOCAL_DECL_ALIAS (decl) = alias;
    3830              : }
    3831              : 
    3832              : /* If DECL has non-internal linkage, and we have a module vector,
    3833              :    record it in the appropriate slot.  We have already checked for
    3834              :    duplicates.  */
    3835              : 
    3836              : static void
    3837       235741 : maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
    3838              : {
    3839       235741 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
    3840              :     return;
    3841              : 
    3842           89 :   if (decl_linkage (decl) == lk_internal)
    3843              :     return;
    3844              : 
    3845           89 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    3846           89 :   bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
    3847          178 :                       && DECL_MODULE_ATTACH_P (not_tmpl));
    3848              :   tree *gslot = get_fixed_binding_slot
    3849           89 :     (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    3850              :      true);
    3851              : 
    3852              :   /* A namespace is always global module so there's no need to mark
    3853              :      the current binding slot as such.  */
    3854           89 :   if (!is_attached && TREE_CODE (decl) != NAMESPACE_DECL)
    3855              :     {
    3856           83 :       binding_slot &orig
    3857           83 :         = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
    3858              : 
    3859           83 :       if (!STAT_HACK_P (tree (orig)))
    3860           20 :         orig = stat_hack (tree (orig));
    3861              : 
    3862           83 :       MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
    3863              :     }
    3864              : 
    3865           89 :   add_mergeable_namespace_entity (gslot, decl);
    3866              : }
    3867              : 
    3868              : /* DECL is being pushed.  Check whether it hides or ambiguates
    3869              :    something seen as an import.  This include decls seen in our own
    3870              :    interface, which is OK.  Also, check for merging a
    3871              :    global/partition decl.  */
    3872              : 
    3873              : static tree
    3874         1086 : check_module_override (tree decl, tree mvec, bool hiding,
    3875              :                        tree scope, tree name)
    3876              : {
    3877         1086 :   tree match = NULL_TREE;
    3878         1086 :   bitmap imports = get_import_bitmap ();
    3879         1086 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
    3880         1086 :   unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
    3881              : 
    3882         1086 :   tree nontmpl = STRIP_TEMPLATE (decl);
    3883         2106 :   bool attached = DECL_LANG_SPECIFIC (nontmpl) && DECL_MODULE_ATTACH_P (nontmpl);
    3884              : 
    3885              :   /* For deduction guides we don't do normal name lookup, but rather consider
    3886              :      any reachable declaration, so we should check for overriding here too.  */
    3887         1086 :   bool any_reachable = deduction_guide_p (decl);
    3888              : 
    3889              :   /* DECL might have an originating module if it's an instantiation of a
    3890              :      friend; we want to look at all reachable decls in that module.  */
    3891         1086 :   unsigned decl_mod = get_originating_module (decl);
    3892              : 
    3893         1086 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    3894              :     {
    3895         1086 :       cluster++;
    3896         1086 :       ix--;
    3897              :     }
    3898              : 
    3899         1763 :   for (; ix--; cluster++)
    3900         2864 :     for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    3901              :       {
    3902              :         /* Are we importing this module?  */
    3903         2187 :         if (cluster->indices[jx].span != 1)
    3904          517 :           continue;
    3905         1670 :         unsigned cluster_mod = cluster->indices[jx].base;
    3906         1670 :         if (!cluster_mod)
    3907         1086 :           continue;
    3908          584 :         bool c_any_reachable = (any_reachable || cluster_mod == decl_mod);
    3909          584 :         if (!c_any_reachable && !bitmap_bit_p (imports, cluster_mod))
    3910           12 :           continue;
    3911              :         /* Is it loaded? */
    3912          572 :         if (cluster->slots[jx].is_lazy ())
    3913           18 :           lazy_load_binding (cluster_mod, scope, name, &cluster->slots[jx]);
    3914          572 :         tree bind = cluster->slots[jx];
    3915          572 :         if (!bind)
    3916              :           /* Errors could cause there to be nothing.  */
    3917            0 :           continue;
    3918              : 
    3919          572 :         tree type = NULL_TREE;
    3920          572 :         if (STAT_HACK_P (bind))
    3921              :           {
    3922              :             /* If there was a matching STAT_TYPE here then xref_tag
    3923              :                should have found it, but we need to check anyway because
    3924              :                a conflicting using-declaration may exist.  */
    3925          554 :             if (c_any_reachable)
    3926              :               {
    3927          118 :                 type = STAT_TYPE (bind);
    3928          118 :                 bind = STAT_DECL (bind);
    3929              :               }
    3930              :             else
    3931              :               {
    3932          436 :                 if (STAT_TYPE_VISIBLE_P (bind))
    3933           92 :                   type = STAT_TYPE (bind);
    3934          436 :                 bind = STAT_VISIBLE (bind);
    3935              :               }
    3936              :           }
    3937              : 
    3938          554 :         if (type)
    3939              :           {
    3940            3 :             match = duplicate_decls (decl, strip_using_decl (type), hiding);
    3941            3 :             if (match)
    3942            0 :               goto matched;
    3943              :           }
    3944              : 
    3945         1609 :         for (ovl_iterator iter (strip_using_decl (bind)); iter; ++iter)
    3946              :           {
    3947          950 :             match = duplicate_decls (decl, *iter, hiding);
    3948          950 :             if (match)
    3949          418 :               goto matched;
    3950              :           }
    3951              :       }
    3952              : 
    3953          668 :   if (TREE_PUBLIC (scope)
    3954              :       /* Namespaces are dealt with specially in
    3955              :          make_namespace_finish.  */
    3956          668 :       && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
    3957              :     {
    3958              :       /* Look in the appropriate mergeable decl slot.  */
    3959          632 :       tree mergeable = NULL_TREE;
    3960          632 :       if (attached)
    3961           42 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
    3962              :                                            / BINDING_VECTOR_SLOTS_PER_CLUSTER)
    3963           21 :           .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
    3964              :       else
    3965          611 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
    3966              : 
    3967        10529 :       for (ovl_iterator iter (mergeable); iter; ++iter)
    3968              :         {
    3969         5148 :           match = duplicate_decls (decl, *iter, hiding);
    3970         5148 :           if (match)
    3971           74 :             goto matched;
    3972              :         }
    3973              :     }
    3974              : 
    3975              :   return NULL_TREE;
    3976              : 
    3977          492 :  matched:
    3978          492 :   if (match != error_mark_node)
    3979              :     {
    3980          441 :       if (attached)
    3981          113 :         BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
    3982              :       else
    3983          328 :         BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
    3984              :     }
    3985              : 
    3986              :   return match;
    3987              : }
    3988              : 
    3989              : /* Record DECL as belonging to the current lexical scope.  Check for
    3990              :    errors (such as an incompatible declaration for the same name
    3991              :    already seen in the same scope).
    3992              : 
    3993              :    The new binding is hidden if HIDING is true (an anticipated builtin
    3994              :    or hidden friend).
    3995              : 
    3996              :    Returns either DECL or an old decl for the same name.  If an old
    3997              :    decl is returned, it may have been smashed to agree with what DECL
    3998              :    says.  */
    3999              : 
    4000              : tree
    4001   1323922494 : pushdecl (tree decl, bool hiding)
    4002              : {
    4003   1323922494 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    4004              : 
    4005   1323922494 :   if (decl == error_mark_node)
    4006              :     return error_mark_node;
    4007              : 
    4008   1323922488 :   if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
    4009    412650140 :     set_decl_context_in_fn (current_function_decl, decl);
    4010              : 
    4011              :   /* The binding level we will be pushing into.  During local class
    4012              :      pushing, we want to push to the containing scope.  */
    4013   1323922488 :   cp_binding_level *level = current_binding_level;
    4014   1323922677 :   while (level->kind == sk_class
    4015   1323922677 :          || level->kind == sk_cleanup)
    4016          189 :     level = level->level_chain;
    4017              : 
    4018              :   /* An anonymous namespace has a NULL DECL_NAME, but we still want to
    4019              :      insert it.  Other NULL-named decls, not so much.  */
    4020   1323922488 :   tree name = DECL_NAME (decl);
    4021   2624737555 :   if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
    4022              :     {
    4023   1298781591 :       cxx_binding *binding = NULL; /* Local scope binding.  */
    4024   1298781591 :       tree ns = NULL_TREE; /* Searched namespace.  */
    4025   1298781591 :       tree *slot = NULL; /* Binding slot in namespace.  */
    4026   1298781591 :       tree *mslot = NULL; /* Current module slot in namespace.  */
    4027   1298781591 :       tree old = NULL_TREE;
    4028   1298781591 :       bool name_independent_p = false;
    4029   1298781591 :       bool name_independent_diagnosed_p = false;
    4030              : 
    4031   1298781591 :       if (level->kind == sk_namespace)
    4032              :         {
    4033              :           /* We look in the decl's namespace for an existing
    4034              :              declaration, even though we push into the current
    4035              :              namespace.  */
    4036   1037395087 :           ns = (DECL_NAMESPACE_SCOPE_P (decl)
    4037    767757404 :                 ? CP_DECL_CONTEXT (decl) : current_namespace);
    4038              :           /* Create the binding, if this is current namespace, because
    4039              :              that's where we'll be pushing anyway.  */
    4040    383878702 :           slot = find_namespace_slot (ns, name, ns == current_namespace);
    4041    383878702 :           if (slot)
    4042              :             {
    4043    767757330 :               mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
    4044    383878665 :                                               ns == current_namespace);
    4045    383878665 :               old = MAYBE_STAT_DECL (*mslot);
    4046              :             }
    4047              :         }
    4048              :       else
    4049              :         {
    4050    914902889 :           binding = find_local_binding (level, name);
    4051    914902889 :           if (binding)
    4052         3059 :             old = binding->value;
    4053    914902889 :           name_independent_p = name_independent_decl_p (decl);
    4054              :         }
    4055              : 
    4056   1298781591 :       if (old == error_mark_node)
    4057        98333 :         old = NULL_TREE;
    4058              : 
    4059   1298781591 :       tree oldi, oldn;
    4060   1322683094 :       for (oldi = old; oldi; oldi = oldn)
    4061              :         {
    4062     34128847 :           if (TREE_CODE (oldi) == TREE_LIST)
    4063              :             {
    4064          120 :               gcc_checking_assert (level->kind != sk_namespace
    4065              :                                    && name_independent_decl_p
    4066              :                                                         (TREE_VALUE (old)));
    4067           60 :               oldn = TREE_CHAIN (oldi);
    4068           60 :               oldi = TREE_VALUE (oldi);
    4069              :             }
    4070              :           else
    4071              :             oldn = NULL_TREE;
    4072    587039421 :           for (ovl_iterator iter (oldi); iter; ++iter)
    4073    288800451 :             if (iter.using_p ())
    4074              :               ; /* Ignore using decls here.  */
    4075    285253775 :             else if (iter.hidden_p ()
    4076    101610697 :                      && TREE_CODE (*iter) == FUNCTION_DECL
    4077     78159387 :                      && DECL_LANG_SPECIFIC (*iter)
    4078    363413162 :                      && DECL_MODULE_IMPORT_P (*iter))
    4079              :               ; /* An undeclared builtin imported from elsewhere.  */
    4080    285253772 :             else if (name_independent_p)
    4081              :               {
    4082              :                 /* Ignore name-independent declarations.  */
    4083          358 :                 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
    4084          273 :                   pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
    4085              :                            "name-independent declarations only available with "
    4086              :                            "%<-std=c++2c%> or %<-std=gnu++2c%>");
    4087              :                 name_independent_diagnosed_p = true;
    4088              :               }
    4089    570506828 :             else if (tree match
    4090    285253414 :                      = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
    4091              :               {
    4092     10227344 :                 if (match == error_mark_node)
    4093              :                   ;
    4094     10226311 :                 else if (TREE_CODE (match) == TYPE_DECL)
    4095        46513 :                   gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
    4096              :                                        == (level->kind == sk_namespace
    4097              :                                            ? NULL_TREE : TREE_TYPE (match)));
    4098     10179798 :                 else if (iter.hidden_p () && !hiding)
    4099              :                   {
    4100              :                     /* Unhiding a previously hidden decl.  */
    4101      3948148 :                     tree head = iter.reveal_node (oldi);
    4102      3948148 :                     if (head != oldi)
    4103              :                       {
    4104        17523 :                         gcc_checking_assert (ns);
    4105        17523 :                         if (STAT_HACK_P (*slot))
    4106            0 :                           STAT_DECL (*slot) = head;
    4107              :                         else
    4108        17523 :                           *slot = head;
    4109              :                       }
    4110      3948148 :                     if (DECL_EXTERN_C_P (match))
    4111              :                       /* We need to check and register the decl now.  */
    4112      3676837 :                       check_extern_c_conflict (match);
    4113              :                   }
    4114      6231650 :                 else if (slot
    4115      6231650 :                          && !hiding
    4116      4730435 :                          && STAT_HACK_P (*slot)
    4117      6231675 :                          && STAT_DECL_HIDDEN_P (*slot))
    4118              :                   {
    4119              :                     /* Unhide the non-function.  */
    4120           25 :                     gcc_checking_assert (oldi == match);
    4121           25 :                     if (!STAT_TYPE (*slot))
    4122           25 :                       *slot = match;
    4123              :                     else
    4124            0 :                       STAT_DECL (*slot) = match;
    4125              :                   }
    4126     10227344 :                 return match;
    4127              :               }
    4128              :         }
    4129              : 
    4130              :       /* Skip a hidden builtin we failed to match already.  There can
    4131              :          only be one.  */
    4132   1288554247 :       if (old && anticipated_builtin_p (old))
    4133       831511 :         old = OVL_CHAIN (old);
    4134              : 
    4135              :       /* Check for redeclaring an import.  */
    4136   1288554247 :       if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
    4137         2172 :         if (tree match
    4138         1086 :             = check_module_override (decl, *slot, hiding, ns, name))
    4139              :           {
    4140          492 :             if (match == error_mark_node)
    4141              :               return match;
    4142              : 
    4143              :             /* We found a decl in an interface, push it into this
    4144              :                binding.  */
    4145          441 :             decl = update_binding (NULL, binding, mslot, old,
    4146              :                                    match, hiding);
    4147              : 
    4148          441 :             return decl;
    4149              :           }
    4150              : 
    4151              :       /* We are pushing a new decl.  */
    4152              : 
    4153   1288553755 :       if (hiding)
    4154              :         ; /* Hidden bindings don't shadow anything.  */
    4155              :       else
    4156   1209872155 :         check_template_shadow (decl);
    4157              : 
    4158   1288553755 :       if (DECL_DECLARES_FUNCTION_P (decl))
    4159              :         {
    4160    334775555 :           check_default_args (decl);
    4161              : 
    4162    334775555 :           if (hiding)
    4163              :             {
    4164     78544558 :               if (level->kind != sk_namespace)
    4165              :                 {
    4166              :                   /* In a local class, a friend function declaration must
    4167              :                      find a matching decl in the innermost non-class scope.
    4168              :                      [class.friend/11] */
    4169            9 :                   error_at (DECL_SOURCE_LOCATION (decl),
    4170              :                             "friend declaration %qD in local class without "
    4171              :                             "prior local declaration", decl);
    4172              :                   /* Don't attempt to push it.  */
    4173            9 :                   return error_mark_node;
    4174              :                 }
    4175              :             }
    4176              :         }
    4177              : 
    4178   1288553746 :       if (level->kind != sk_namespace)
    4179              :         {
    4180    914900381 :           tree local_shadow = check_local_shadow (decl);
    4181    914900381 :           if (name_independent_p && local_shadow)
    4182              :             {
    4183          104 :               if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
    4184          100 :                 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
    4185              :                          "name-independent declarations only available with "
    4186              :                          "%<-std=c++2c%> or %<-std=gnu++2c%>");
    4187          104 :               name_independent_diagnosed_p = true;
    4188              :               /* When a name-independent declaration is pushed into a scope
    4189              :                  which itself does not contain a _ named declaration yet (so
    4190              :                  _ name lookups wouldn't be normally ambiguous), but it
    4191              :                  shadows a _ declaration in some outer scope in cases
    4192              :                  described in [basic.scope.block]/2 where if the names of
    4193              :                  the shadowed and shadowing declarations were different it
    4194              :                  would be ill-formed program, arrange for _ name lookups
    4195              :                  in this scope to be ambiguous.  */
    4196          104 :               if (old == NULL_TREE)
    4197              :                 {
    4198          104 :                   old = build_tree_list (error_mark_node, local_shadow);
    4199          104 :                   TREE_TYPE (old) = error_mark_node;
    4200              :                 }
    4201              :             }
    4202              : 
    4203    914900381 :           if (TREE_CODE (decl) == NAMESPACE_DECL)
    4204              :             /* A local namespace alias.  */
    4205       109580 :             set_identifier_type_value_with_scope (name, NULL_TREE, level);
    4206              : 
    4207    914900381 :           if (!binding)
    4208    914899824 :             binding = create_local_binding (level, name);
    4209              :         }
    4210    373653365 :       else if (!slot)
    4211              :         {
    4212           37 :           ns = current_namespace;
    4213           37 :           slot = find_namespace_slot (ns, name, true);
    4214           37 :           mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
    4215              :           /* Update OLD to reflect the namespace we're going to be
    4216              :              pushing into.  */
    4217           37 :           old = MAYBE_STAT_DECL (*mslot);
    4218              :         }
    4219              : 
    4220   1288553746 :       old = update_binding (level, binding, mslot, old, decl, hiding);
    4221              : 
    4222   1288553746 :       if (old != decl)
    4223              :         /* An existing decl matched, use it.  */
    4224              :         decl = old;
    4225              :       else
    4226              :         {
    4227   1288553722 :           if (TREE_CODE (decl) == TYPE_DECL)
    4228              :             {
    4229    254536681 :               tree type = TREE_TYPE (decl);
    4230              : 
    4231    254536681 :               if (type != error_mark_node)
    4232              :                 {
    4233    254536613 :                   if (TYPE_NAME (type) != decl)
    4234     22540755 :                     set_underlying_type (decl);
    4235              : 
    4236    254536613 :                   set_identifier_type_value_with_scope (name, decl, level);
    4237              : 
    4238    254536613 :                   if (level->kind != sk_namespace
    4239    254536613 :                       && !instantiating_current_function_p ())
    4240              :                     /* This is a locally defined typedef in a function that
    4241              :                        is not a template instantation, record it to implement
    4242              :                        -Wunused-local-typedefs.  */
    4243    238074696 :                     record_locally_defined_typedef (decl);
    4244              :                 }
    4245              :             }
    4246   1034017041 :           else if (VAR_OR_FUNCTION_DECL_P (decl))
    4247              :             {
    4248    396903529 :               if (DECL_EXTERN_C_P (decl))
    4249    288376810 :                 check_extern_c_conflict (decl);
    4250              : 
    4251    396903529 :               if (!DECL_LOCAL_DECL_P (decl)
    4252    396903529 :                   && VAR_P (decl))
    4253     87357985 :                 maybe_register_incomplete_var (decl);
    4254              : 
    4255    396903529 :               if (DECL_LOCAL_DECL_P (decl)
    4256    396903529 :                   && NAMESPACE_SCOPE_P (decl))
    4257        60664 :                 push_local_extern_decl_alias (decl);
    4258              :             }
    4259              : 
    4260   1288553722 :           if (level->kind == sk_namespace
    4261    373653347 :               && TREE_PUBLIC (level->this_entity)
    4262   1662202148 :               && module_maybe_has_cmi_p ())
    4263       235741 :             maybe_record_mergeable_decl (slot, name, decl);
    4264              :         }
    4265              :     }
    4266              :   else
    4267     25140897 :     add_decl_to_level (level, decl);
    4268              : 
    4269              :   return decl;
    4270   1323922494 : }
    4271              : 
    4272              : /* A mergeable entity is being loaded into namespace NS slot NAME.
    4273              :    Create and return the appropriate vector slot for that.  Either a
    4274              :    GMF slot or a module-specific one.  */
    4275              : 
    4276              : tree *
    4277       147294 : mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
    4278              : {
    4279       147294 :   tree *mslot = find_namespace_slot (ns, name, true);
    4280       147294 :   tree *vslot = get_fixed_binding_slot
    4281       293817 :     (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    4282              :      true);
    4283              : 
    4284       147294 :   gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
    4285       147294 :   *vec = *mslot;
    4286              : 
    4287       147294 :   return vslot;
    4288              : }
    4289              : 
    4290              : /* DECL is a new mergeable namespace-scope decl.  Add it to the
    4291              :    mergeable entities on GSLOT.  */
    4292              : 
    4293              : void
    4294        70058 : add_mergeable_namespace_entity (tree *gslot, tree decl)
    4295              : {
    4296        70058 :   *gslot = ovl_make (decl, *gslot);
    4297        70058 : }
    4298              : 
    4299              : /* A mergeable entity of KLASS called NAME is being loaded.  Return
    4300              :    the set of things it could be.  All such non-as_base classes have
    4301              :    been given a member vec.  */
    4302              : 
    4303              : tree
    4304       161894 : lookup_class_binding (tree klass, tree name)
    4305              : {
    4306       161894 :   tree found = NULL_TREE;
    4307              : 
    4308       161894 :   if (!COMPLETE_TYPE_P (klass))
    4309              :     ;
    4310       161894 :   else if (TYPE_LANG_SPECIFIC (klass))
    4311              :     {
    4312       160430 :       vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    4313              : 
    4314       160430 :       found = member_vec_binary_search (member_vec, name);
    4315       160430 :       if (!found)
    4316              :         ;
    4317       160017 :       else if (STAT_HACK_P (found))
    4318              :         /* Rearrange the stat hack so that we don't need to expose that
    4319              :            internal detail.  */
    4320            6 :         found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
    4321       160011 :       else if (IDENTIFIER_CONV_OP_P (name))
    4322              :         {
    4323          835 :           gcc_checking_assert (name == conv_op_identifier);
    4324          835 :           found = OVL_CHAIN (found);
    4325              :         }
    4326              :     }
    4327              :   else
    4328              :     {
    4329         1464 :       gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
    4330              :                            || TYPE_PTRMEMFUNC_P (klass));
    4331         1464 :       found = fields_linear_search (klass, name, false);
    4332              :     }
    4333              : 
    4334       161894 :   return found;
    4335              : }
    4336              : 
    4337              : /* Whether this using is declared in the module purview.  */
    4338              : 
    4339              : bool
    4340        30462 : ovl_iterator::purview_p () const
    4341              : {
    4342        30462 :   gcc_checking_assert (using_p ());
    4343        30462 :   if (TREE_CODE (ovl) == USING_DECL)
    4344         5056 :     return DECL_MODULE_PURVIEW_P (ovl);
    4345        25406 :   return OVL_PURVIEW_P (ovl);
    4346              : }
    4347              : 
    4348              : /* Whether this using is exported from this module.  */
    4349              : 
    4350              : bool
    4351        30462 : ovl_iterator::exporting_p () const
    4352              : {
    4353        30462 :   gcc_checking_assert (using_p ());
    4354        30462 :   if (TREE_CODE (ovl) == USING_DECL)
    4355         5056 :     return DECL_MODULE_EXPORT_P (ovl);
    4356        25406 :   return OVL_EXPORT_P (ovl);
    4357              : }
    4358              : 
    4359              : /* Given a namespace NS, walk all of its bindings, calling CALLBACK
    4360              :    for all visible decls.  Any lazy module bindings will be loaded
    4361              :    at this point.  */
    4362              : 
    4363              : void
    4364          122 : walk_namespace_bindings (tree ns, void (*callback) (tree decl, void *data),
    4365              :                          void *data)
    4366              : {
    4367         1546 :   for (tree o : *DECL_NAMESPACE_BINDINGS (ns))
    4368          712 :     if (TREE_CODE (o) == BINDING_VECTOR)
    4369              :       {
    4370              :         /* Modules may have duplicate entities on the binding slot.
    4371              :            Keep track of this as needed, and ensure we only call
    4372              :            the callback once per entity.  */
    4373          154 :         hash_set<tree> seen;
    4374          383 :         const auto callback_maybe_dup = [&](tree decl)
    4375              :           {
    4376          229 :             if (!seen.add (decl))
    4377          172 :               callback (decl, data);
    4378          383 :           };
    4379              : 
    4380              :         /* First the current module.  There should be no dups yet,
    4381              :            but track anything that might be dup'd later.  */
    4382          154 :         binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (o);
    4383          154 :         if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    4384              :           {
    4385           57 :             tree value = bind;
    4386           57 :             if (STAT_HACK_P (bind))
    4387              :               {
    4388           57 :                 if (STAT_TYPE (bind) && !STAT_TYPE_HIDDEN_P (bind))
    4389            0 :                   callback_maybe_dup (STAT_TYPE (bind));
    4390           57 :                 if (STAT_DECL_HIDDEN_P (bind))
    4391              :                   value = NULL_TREE;
    4392              :                 else
    4393           57 :                   value = STAT_DECL (bind);
    4394              :               }
    4395           57 :             value = ovl_skip_hidden (value);
    4396          114 :             for (tree decl : ovl_range (value))
    4397           57 :               callback_maybe_dup (decl);
    4398              :           }
    4399              : 
    4400              :         /* Now the imported bindings.  */
    4401              :         /* FIXME: We probably want names visible from the outermost point of
    4402              :            constant evaluation, regardless of instantiations.  */
    4403          154 :         bitmap imports = get_import_bitmap ();
    4404          154 :         tree name = BINDING_VECTOR_NAME (o);
    4405              : 
    4406          154 :         unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (o);
    4407          154 :         if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    4408              :           {
    4409          154 :             ix--;
    4410          154 :             cluster++;
    4411              :           }
    4412              : 
    4413              :         /* Do this in forward order, so we load modules in an order
    4414              :            the user expects.  */
    4415          327 :         for (; ix--; cluster++)
    4416          519 :           for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    4417              :             {
    4418              :               /* Are we importing this module?  */
    4419          346 :               if (unsigned base = cluster->indices[jx].base)
    4420          173 :                 if (unsigned span = cluster->indices[jx].span)
    4421          173 :                   do
    4422          173 :                     if (bitmap_bit_p (imports, base))
    4423          173 :                       goto found;
    4424            0 :                   while (++base, --span);
    4425          173 :               continue;
    4426              : 
    4427          173 :             found:;
    4428              :               /* Is it loaded?  */
    4429          173 :               unsigned mod = cluster->indices[jx].base;
    4430          173 :               if (cluster->slots[jx].is_lazy ())
    4431              :                 {
    4432           11 :                   gcc_assert (cluster->indices[jx].span == 1);
    4433           11 :                   lazy_load_binding (mod, ns, name, &cluster->slots[jx]);
    4434              :                 }
    4435              : 
    4436          173 :               tree bind = cluster->slots[jx];
    4437          173 :               if (!bind)
    4438              :                 /* Load errors could mean there's nothing here.  */
    4439            0 :                 continue;
    4440              : 
    4441              :               /* Extract what we can see from here.  If there's no
    4442              :                  stat_hack, then everything was exported.  */
    4443          173 :               tree value = bind;
    4444          173 :               if (STAT_HACK_P (bind))
    4445              :                 {
    4446          153 :                   if (STAT_TYPE_VISIBLE_P (bind))
    4447           38 :                     callback_maybe_dup (STAT_TYPE (bind));
    4448          153 :                   value = STAT_VISIBLE (bind);
    4449              :                 }
    4450          173 :               value = ovl_skip_hidden (value);
    4451          326 :               for (tree decl : ovl_range (value))
    4452          134 :                 callback_maybe_dup (decl);
    4453          173 :             }
    4454          154 :       }
    4455              :     else
    4456              :       {
    4457          558 :         tree bind = o;
    4458          558 :         if (STAT_HACK_P (bind))
    4459              :           {
    4460           29 :             if (STAT_TYPE (bind) && !STAT_TYPE_HIDDEN_P (bind))
    4461           28 :               callback (STAT_TYPE (bind), data);
    4462           29 :             if (STAT_DECL_HIDDEN_P (bind))
    4463              :               bind = NULL_TREE;
    4464              :             else
    4465           29 :               bind = STAT_DECL (bind);
    4466              :           }
    4467          558 :         bind = ovl_skip_hidden (bind);
    4468         1180 :         for (tree decl : ovl_range (bind))
    4469          589 :           callback (decl, data);
    4470              :       }
    4471          122 : }
    4472              : 
    4473              : /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
    4474              :    for all decls of the current module.  When partitions are involved,
    4475              :    decls might be mentioned more than once.   Return the accumulation of
    4476              :    CALLBACK results.  */
    4477              : 
    4478              : unsigned
    4479      7434354 : walk_module_binding (tree binding, bitmap partitions,
    4480              :                      bool (*callback) (tree decl, WMB_Flags, void *data),
    4481              :                      void *data)
    4482              : {
    4483      7434354 :   tree current = binding;
    4484      7434354 :   unsigned count = 0;
    4485              : 
    4486      7434354 :   if (TREE_CODE (binding) == BINDING_VECTOR)
    4487        42381 :     current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
    4488              : 
    4489      7442779 :   bool decl_hidden = false;
    4490      7434354 :   if (tree type = MAYBE_STAT_TYPE (current))
    4491              :     {
    4492           94 :       WMB_Flags flags = WMB_None;
    4493           94 :       if (STAT_TYPE_HIDDEN_P (current))
    4494            0 :         flags = WMB_Flags (flags | WMB_Hidden);
    4495           94 :       if (TREE_CODE (type) == USING_DECL)
    4496              :         {
    4497            3 :           flags = WMB_Flags (flags | WMB_Using);
    4498            3 :           if (DECL_MODULE_PURVIEW_P (type))
    4499            3 :             flags = WMB_Flags (flags | WMB_Purview);
    4500            3 :           if (DECL_MODULE_EXPORT_P (type))
    4501            3 :             flags = WMB_Flags (flags | WMB_Export);
    4502              :         }
    4503           94 :       count += callback (type, flags, data);
    4504           94 :       decl_hidden = STAT_DECL_HIDDEN_P (current);
    4505              :     }
    4506              : 
    4507     14920788 :   for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
    4508              :     {
    4509      7486434 :       if (iter.hidden_p ())
    4510              :         decl_hidden = true;
    4511      7486434 :       if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
    4512              :         {
    4513      5688205 :           WMB_Flags flags = WMB_None;
    4514      5688205 :           if (decl_hidden)
    4515         9732 :             flags = WMB_Flags (flags | WMB_Hidden);
    4516      5688205 :           if (iter.using_p ())
    4517              :             {
    4518        30450 :               flags = WMB_Flags (flags | WMB_Using);
    4519        30450 :               if (iter.purview_p ())
    4520        25755 :                 flags = WMB_Flags (flags | WMB_Purview);
    4521        30450 :               if (iter.exporting_p ())
    4522        25117 :                 flags = WMB_Flags (flags | WMB_Export);
    4523              :             }
    4524      5688205 :           count += callback (*iter, flags, data);
    4525              :         }
    4526      7486434 :       decl_hidden = false;
    4527              :     }
    4528              : 
    4529      7434354 :   if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
    4530              :     {
    4531              :       /* Process partition slots.  */
    4532          311 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    4533          311 :       unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    4534          311 :       if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    4535              :         {
    4536          311 :           ix--;
    4537          311 :           cluster++;
    4538              :         }
    4539              : 
    4540              :       /* There could be duplicate module or GMF entries.  */
    4541          311 :       bool maybe_dups = (BINDING_VECTOR_PARTITION_DUPS_P (binding)
    4542          311 :                          || BINDING_VECTOR_GLOBAL_DUPS_P (binding));
    4543              : 
    4544          664 :       for (; ix--; cluster++)
    4545         1059 :         for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    4546          706 :           if (!cluster->slots[jx].is_lazy ())
    4547          700 :             if (tree bind = cluster->slots[jx])
    4548              :               {
    4549          411 :                 if (TREE_CODE (bind) == NAMESPACE_DECL
    4550          411 :                     && !DECL_NAMESPACE_ALIAS (bind))
    4551              :                   {
    4552           33 :                     if (unsigned base = cluster->indices[jx].base)
    4553           33 :                       if (unsigned span = cluster->indices[jx].span)
    4554           33 :                         do
    4555           33 :                           if (bitmap_bit_p (partitions, base))
    4556           30 :                             goto found;
    4557            3 :                         while (++base, --span);
    4558              :                     /* Not a partition's namespace.  */
    4559            3 :                     continue;
    4560           30 :                   found:
    4561              : 
    4562           30 :                     WMB_Flags flags = WMB_None;
    4563           30 :                     if (maybe_dups)
    4564            0 :                       flags = WMB_Flags (flags | WMB_Dups);
    4565           30 :                     count += callback (bind, flags, data);
    4566            3 :                   }
    4567          378 :                 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
    4568              :                   {
    4569          219 :                     if (tree btype = STAT_TYPE (bind))
    4570              :                       {
    4571            0 :                         WMB_Flags flags = WMB_None;
    4572            0 :                         if (maybe_dups)
    4573            0 :                           flags = WMB_Flags (flags | WMB_Dups);
    4574            0 :                         if (STAT_TYPE_HIDDEN_P (bind))
    4575            0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    4576            0 :                         if (TREE_CODE (btype) == USING_DECL)
    4577              :                           {
    4578            0 :                             flags = WMB_Flags (flags | WMB_Using);
    4579            0 :                             if (DECL_MODULE_PURVIEW_P (btype))
    4580            0 :                               flags = WMB_Flags (flags | WMB_Purview);
    4581            0 :                             if (DECL_MODULE_EXPORT_P (btype))
    4582            0 :                               flags = WMB_Flags (flags | WMB_Export);
    4583              :                           }
    4584            0 :                         count += callback (btype, flags, data);
    4585              :                       }
    4586          219 :                     bool part_hidden = STAT_DECL_HIDDEN_P (bind);
    4587          219 :                     for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
    4588          447 :                          iter; ++iter)
    4589              :                       {
    4590          228 :                         if (iter.hidden_p ())
    4591              :                           part_hidden = true;
    4592          228 :                         gcc_checking_assert
    4593              :                           (!(part_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
    4594              : 
    4595          228 :                         WMB_Flags flags = WMB_None;
    4596          228 :                         if (maybe_dups)
    4597           96 :                           flags = WMB_Flags (flags | WMB_Dups);
    4598          228 :                         if (part_hidden)
    4599            0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    4600          228 :                         if (iter.using_p ())
    4601              :                           {
    4602           12 :                             flags = WMB_Flags (flags | WMB_Using);
    4603           12 :                             if (iter.purview_p ())
    4604           12 :                               flags = WMB_Flags (flags | WMB_Purview);
    4605           12 :                             if (iter.exporting_p ())
    4606           12 :                               flags = WMB_Flags (flags | WMB_Export);
    4607              :                           }
    4608          228 :                         count += callback (*iter, flags, data);
    4609          228 :                         part_hidden = false;
    4610              :                       }
    4611              :                   }
    4612              :               }
    4613              :     }
    4614              : 
    4615      7434354 :   return count;
    4616              : }
    4617              : 
    4618              : /* Imported module MOD has a binding to NS::NAME, stored in section
    4619              :    SNUM.  */
    4620              : 
    4621              : bool
    4622       190112 : import_module_binding  (tree ns, tree name, unsigned mod, unsigned snum)
    4623              : {
    4624       190112 :   tree *slot = find_namespace_slot (ns, name, true);
    4625       190112 :   binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
    4626              : 
    4627       190112 :   if (mslot->is_lazy () || *mslot)
    4628              :     /* Oops, something was already there.  */
    4629              :     return false;
    4630              : 
    4631       190112 :   mslot->set_lazy (snum);
    4632       190112 :   return true;
    4633              : }
    4634              : 
    4635              : /* An import of MODULE is binding NS::NAME.  There should be no
    4636              :    existing binding for >= MODULE.  GLOBAL_P indicates whether the
    4637              :    bindings include global module entities.  PARTITION_P is true if
    4638              :    it is part of the current module.  VALUE and TYPE are the value
    4639              :    and type bindings.  VISIBLE are the value bindings being exported.
    4640              :    INTERNAL is a TREE_LIST of any TU-local names visible for ADL.  */
    4641              : 
    4642              : bool
    4643       108316 : set_module_binding (tree ns, tree name, unsigned mod, bool global_p,
    4644              :                     bool partition_p, tree value, tree type, tree visible,
    4645              :                     tree internal)
    4646              : {
    4647       108316 :   if (!value && !internal)
    4648              :     /* Bogus BMIs could give rise to nothing to bind.  */
    4649              :     return false;
    4650              : 
    4651       108301 :   gcc_assert (!value
    4652              :               || TREE_CODE (value) != NAMESPACE_DECL
    4653              :               || DECL_NAMESPACE_ALIAS (value));
    4654       108316 :   gcc_checking_assert (mod);
    4655              : 
    4656       108316 :   tree *slot = find_namespace_slot (ns, name, true);
    4657       108316 :   binding_slot *mslot = search_imported_binding_slot (slot, mod);
    4658              : 
    4659       108316 :   if (!mslot || !mslot->is_lazy ())
    4660              :     /* Again, bogus BMI could give find to missing or already loaded slot.  */
    4661              :     return false;
    4662              : 
    4663       108316 :   tree bind = value;
    4664       108316 :   if (type || visible != bind || internal || partition_p || global_p)
    4665              :     {
    4666       106590 :       bind = stat_hack (bind, type);
    4667       106590 :       STAT_VISIBLE (bind) = visible;
    4668          822 :       if ((partition_p && TREE_PUBLIC (ns))
    4669       106596 :           || (type && DECL_MODULE_EXPORT_P (type)))
    4670          842 :         STAT_TYPE_VISIBLE_P (bind) = true;
    4671              :     }
    4672              : 
    4673              :   /* If this has internal declarations, track them for diagnostics.  */
    4674       108316 :   if (internal)
    4675              :     {
    4676           15 :       if (!BINDING_VECTOR_INTERNAL_DECLS (*slot))
    4677           15 :         BINDING_VECTOR_INTERNAL_DECLS (*slot)
    4678           30 :           = module_tree_map_t::create_ggc ();
    4679           15 :       bool existed = BINDING_VECTOR_INTERNAL_DECLS (*slot)->put (mod, internal);
    4680           15 :       gcc_checking_assert (!existed);
    4681           15 :       MODULE_BINDING_INTERNAL_DECLS_P (bind) = true;
    4682              :     }
    4683              : 
    4684              :   /* Note if this is this-module and/or global binding.  */
    4685       108316 :   if (partition_p)
    4686          822 :     MODULE_BINDING_PARTITION_P (bind) = true;
    4687       108316 :   if (global_p)
    4688       105566 :     MODULE_BINDING_GLOBAL_P (bind) = true;
    4689              : 
    4690       108316 :   *mslot = bind;
    4691              : 
    4692       108316 :   return true;
    4693              : }
    4694              : 
    4695              : void
    4696        72176 : add_module_namespace_decl (tree ns, tree decl)
    4697              : {
    4698        72176 :   gcc_assert (!DECL_CHAIN (decl));
    4699        72176 :   gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
    4700              :                          && DECL_LOCAL_DECL_P (decl)));
    4701        72176 :   if (CHECKING_P)
    4702              :     /* Expensive already-there? check.  */
    4703    115474497 :     for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
    4704    115402321 :          probe = DECL_CHAIN (probe))
    4705    115402321 :       gcc_assert (decl != probe);
    4706              : 
    4707        72176 :   add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
    4708              : 
    4709        72176 :   if (VAR_P (decl))
    4710         2222 :     maybe_register_incomplete_var (decl);
    4711              : 
    4712        69954 :   if (VAR_OR_FUNCTION_DECL_P (decl)
    4713        94639 :       && DECL_EXTERN_C_P (decl))
    4714         9115 :     check_extern_c_conflict (decl);
    4715        72176 : }
    4716              : 
    4717              : /* Enter DECL into the symbol table, if that's appropriate.  Returns
    4718              :    DECL, or a modified version thereof.  */
    4719              : 
    4720              : tree
    4721    144606954 : maybe_push_decl (tree decl)
    4722              : {
    4723    144606954 :   tree type = TREE_TYPE (decl);
    4724              : 
    4725              :   /* Add this decl to the current binding level, but not if it comes
    4726              :      from another scope, e.g. a static member variable.  TEM may equal
    4727              :      DECL or it may be a previous decl of the same name.  */
    4728    144606954 :   if (decl == error_mark_node
    4729    144606949 :       || (TREE_CODE (decl) != PARM_DECL
    4730    144606949 :           && DECL_CONTEXT (decl) != NULL_TREE
    4731              :           /* Definitions of namespace members outside their namespace are
    4732              :              possible.  */
    4733     51652117 :           && !DECL_NAMESPACE_SCOPE_P (decl))
    4734    143168929 :       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
    4735    143168929 :       || type == unknown_type_node
    4736              :       /* The declaration of a template specialization does not affect
    4737              :          the functions available for overload resolution, so we do not
    4738              :          call pushdecl.  */
    4739    287775883 :       || (TREE_CODE (decl) == FUNCTION_DECL
    4740     37148607 :           && DECL_TEMPLATE_SPECIALIZATION (decl)))
    4741      1530233 :     return decl;
    4742              :   else
    4743    143076721 :     return pushdecl (decl);
    4744              : }
    4745              : 
    4746              : /* Bind DECL to ID in the current_binding_level, assumed to be a local
    4747              :    binding level.  If IS_USING is true, DECL got here through a
    4748              :    using-declaration.  */
    4749              : 
    4750              : static void
    4751     13047714 : push_local_binding (tree id, tree decl, bool is_using)
    4752              : {
    4753              :   /* Skip over any local classes.  This makes sense if we call
    4754              :      push_local_binding with a friend decl of a local class.  */
    4755     13047714 :   cp_binding_level *b = innermost_nonclass_level ();
    4756              : 
    4757     13047714 :   gcc_assert (b->kind != sk_namespace);
    4758     13047714 :   if (find_local_binding (b, id))
    4759              :     {
    4760              :       /* Supplement the existing binding.  */
    4761           27 :       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
    4762              :         /* It didn't work.  Something else must be bound at this
    4763              :            level.  Do not add DECL to the list of things to pop
    4764              :            later.  */
    4765              :         return;
    4766              :     }
    4767              :   else
    4768              :     /* Create a new binding.  */
    4769     13047687 :     push_binding (id, decl, b);
    4770              : 
    4771     13047705 :   if (TREE_CODE (decl) == OVERLOAD || is_using)
    4772              :     /* We must put the OVERLOAD or using into a TREE_LIST since we
    4773              :        cannot use the decl's chain itself.  */
    4774     13047705 :     decl = build_tree_list (id, decl);
    4775              : 
    4776              :   /* And put DECL on the list of things declared by the current
    4777              :      binding level.  */
    4778     13047705 :   add_decl_to_level (b, decl);
    4779              : }
    4780              : 
    4781              : /* Lookup the FRIEND_TMPL within all merged module imports.  Used to dedup
    4782              :    instantiations of temploid hidden friends from imported modules.  */
    4783              : 
    4784              : tree
    4785          306 : lookup_imported_hidden_friend (tree friend_tmpl)
    4786              : {
    4787              :   /* For a class-scope friend class it should have been found by regular
    4788              :      name lookup.  Otherwise we're looking in the current namespace.  */
    4789          306 :   gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
    4790              : 
    4791          306 :   tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
    4792          306 :   if (!DECL_LANG_SPECIFIC (inner)
    4793          360 :       || !DECL_MODULE_ENTITY_P (inner))
    4794              :     return NULL_TREE;
    4795              : 
    4796              :   /* Load any templates matching FRIEND_TMPL from importers.  */
    4797           36 :   lazy_load_pendings (friend_tmpl);
    4798              : 
    4799           36 :   tree name = DECL_NAME (inner);
    4800           36 :   tree *slot = find_namespace_slot (current_namespace, name, false);
    4801           36 :   if (!slot || !*slot || TREE_CODE (*slot) != BINDING_VECTOR)
    4802              :     return NULL_TREE;
    4803              : 
    4804              :   /* We're only interested in declarations attached to the same module
    4805              :      as the friend class we're attempting to instantiate.  */
    4806           21 :   int m = get_originating_module (friend_tmpl, /*global=-1*/true);
    4807           21 :   gcc_assert (m != 0);
    4808              : 
    4809              :   /* First check whether there's a reachable declaration attached to the module
    4810              :      we're looking for.  */
    4811           21 :   if (m > 0)
    4812            0 :     if (binding_slot *mslot = search_imported_binding_slot (slot, m))
    4813              :       {
    4814            0 :         if (mslot->is_lazy ())
    4815            0 :           lazy_load_binding (m, current_namespace, name, mslot);
    4816            0 :         for (ovl_iterator iter (*mslot); iter; ++iter)
    4817            0 :           if (DECL_CLASS_TEMPLATE_P (*iter))
    4818            0 :             return *iter;
    4819              :       }
    4820              : 
    4821              :   /* Otherwise, look in the mergeable slots for this name, in case an importer
    4822              :      has already instantiated this declaration.  */
    4823              :   tree *vslot = get_fixed_binding_slot
    4824           21 :     (slot, name, m > 0 ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL, false);
    4825           21 :   if (!vslot || !*vslot)
    4826              :     return NULL_TREE;
    4827              : 
    4828              :   /* There should be at most one class template from the module we're
    4829              :      looking for, return it.  */
    4830           21 :   for (ovl_iterator iter (*vslot); iter; ++iter)
    4831           42 :     if (DECL_CLASS_TEMPLATE_P (*iter)
    4832           42 :         && get_originating_module (*iter, true) == m)
    4833           21 :       return *iter;
    4834              : 
    4835            0 :   return NULL_TREE;
    4836              : }
    4837              : 
    4838              : 
    4839              : /* true means unconditionally make a BLOCK for the next level pushed.  */
    4840              : 
    4841              : static bool keep_next_level_flag;
    4842              : 
    4843              : static int binding_depth = 0;
    4844              : 
    4845              : static void
    4846            0 : indent (int depth)
    4847              : {
    4848            0 :   int i;
    4849              : 
    4850            0 :   for (i = 0; i < depth * 2; i++)
    4851            0 :     putc (' ', stderr);
    4852            0 : }
    4853              : 
    4854              : /* Return a string describing the kind of SCOPE we have.  */
    4855              : static const char *
    4856            0 : cp_binding_level_descriptor (cp_binding_level *scope)
    4857              : {
    4858              :   /* The order of this table must match the "scope_kind"
    4859              :      enumerators.  */
    4860            0 :   static const char* scope_kind_names[] = {
    4861              :     "block-scope",
    4862              :     "cleanup-scope",
    4863              :     "try-scope",
    4864              :     "catch-scope",
    4865              :     "for-scope",
    4866              :     "template-for-scope",
    4867              :     "cond-init-scope",
    4868              :     "stmt-expr-scope",
    4869              :     "function-parameter-scope",
    4870              :     "class-scope",
    4871              :     "enum-scope",
    4872              :     "namespace-scope",
    4873              :     "template-parameter-scope",
    4874              :     "template-explicit-spec-scope",
    4875              :     "transaction-scope",
    4876              :     "openmp-scope",
    4877              :     "lambda-scope",
    4878              :     "contract-check-scope"
    4879              :   };
    4880            0 :   static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
    4881              :                  "must keep names aligned with scope_kind enum");
    4882              : 
    4883            0 :   scope_kind kind = scope->kind;
    4884            0 :   if (kind == sk_template_parms && scope->explicit_spec_p)
    4885            0 :     kind = sk_template_spec;
    4886              : 
    4887            0 :   return scope_kind_names[kind];
    4888              : }
    4889              : 
    4890              : /* Output a debugging information about SCOPE when performing
    4891              :    ACTION at LINE.  */
    4892              : static void
    4893            0 : cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
    4894              : {
    4895            0 :   const char *desc = cp_binding_level_descriptor (scope);
    4896            0 :   if (scope->this_entity)
    4897            0 :     verbatim ("%s %<%s(%E)%> %p %d", action, desc,
    4898              :               scope->this_entity, (void *) scope, line);
    4899              :   else
    4900            0 :     verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
    4901            0 : }
    4902              : 
    4903              : /* A chain of binding_level structures awaiting reuse.  */
    4904              : 
    4905              : static GTY((deletable)) cp_binding_level *free_binding_level;
    4906              : 
    4907              : /* Insert SCOPE as the innermost binding level.  */
    4908              : 
    4909              : void
    4910   1319127816 : push_binding_level (cp_binding_level *scope)
    4911              : {
    4912              :   /* Add it to the front of currently active scopes stack.  */
    4913   1319127816 :   scope->level_chain = current_binding_level;
    4914   1319127816 :   current_binding_level = scope;
    4915   1319127816 :   keep_next_level_flag = false;
    4916              : 
    4917   1319127816 :   if (ENABLE_SCOPE_CHECKING)
    4918              :     {
    4919              :       scope->binding_depth = binding_depth;
    4920              :       indent (binding_depth);
    4921              :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    4922              :                               "push");
    4923              :       binding_depth++;
    4924              :     }
    4925   1319127816 : }
    4926              : 
    4927              : /* Create a new KIND scope and make it the top of the active scopes stack.
    4928              :    ENTITY is the scope of the associated C++ entity (namespace, class,
    4929              :    function, C++0x enumeration); it is NULL otherwise.  */
    4930              : 
    4931              : cp_binding_level *
    4932   1115952251 : begin_scope (scope_kind kind, tree entity)
    4933              : {
    4934   1115952251 :   cp_binding_level *scope;
    4935              : 
    4936              :   /* Reuse or create a struct for this binding level.  */
    4937   1115952251 :   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
    4938              :     {
    4939   1071624269 :       scope = free_binding_level;
    4940   1071624269 :       free_binding_level = scope->level_chain;
    4941   1071624269 :       memset (scope, 0, sizeof (cp_binding_level));
    4942              :     }
    4943              :   else
    4944     44327982 :     scope = ggc_cleared_alloc<cp_binding_level> ();
    4945              : 
    4946   1115952251 :   scope->this_entity = entity;
    4947   1115952251 :   scope->more_cleanups_ok = true;
    4948   1115952251 :   switch (kind)
    4949              :     {
    4950          444 :     case sk_cleanup:
    4951          444 :       scope->keep = true;
    4952          444 :       break;
    4953              : 
    4954      5944273 :     case sk_template_spec:
    4955      5944273 :       scope->explicit_spec_p = true;
    4956      5944273 :       kind = sk_template_parms;
    4957              :       /* Fall through.  */
    4958    748060447 :     case sk_template_parms:
    4959    748060447 :     case sk_block:
    4960    748060447 :     case sk_try:
    4961    748060447 :     case sk_catch:
    4962    748060447 :     case sk_for:
    4963    748060447 :     case sk_template_for:
    4964    748060447 :     case sk_cond:
    4965    748060447 :     case sk_class:
    4966    748060447 :     case sk_scoped_enum:
    4967    748060447 :     case sk_transaction:
    4968    748060447 :     case sk_omp:
    4969    748060447 :     case sk_contract:
    4970    748060447 :     case sk_stmt_expr:
    4971    748060447 :     case sk_lambda:
    4972    748060447 :       scope->keep = keep_next_level_flag;
    4973    748060447 :       break;
    4974              : 
    4975    367793027 :     case sk_function_parms:
    4976    367793027 :       scope->keep = keep_next_level_flag;
    4977    367793027 :       break;
    4978              : 
    4979        98333 :     case sk_namespace:
    4980        98333 :       NAMESPACE_LEVEL (entity) = scope;
    4981        98333 :       break;
    4982              : 
    4983            0 :     default:
    4984              :       /* Should not happen.  */
    4985            0 :       gcc_unreachable ();
    4986   1115952251 :       break;
    4987              :     }
    4988   1115952251 :   scope->kind = kind;
    4989              : 
    4990   1115952251 :   push_binding_level (scope);
    4991              : 
    4992   1115952251 :   return scope;
    4993              : }
    4994              : 
    4995              : /* We're about to leave current scope.  Pop the top of the stack of
    4996              :    currently active scopes.  Return the enclosing scope, now active.  */
    4997              : 
    4998              : cp_binding_level *
    4999   1372342259 : leave_scope (void)
    5000              : {
    5001   1372342259 :   cp_binding_level *scope = current_binding_level;
    5002              : 
    5003   1372342259 :   if (scope->kind == sk_namespace && class_binding_level)
    5004            0 :     current_binding_level = class_binding_level;
    5005              : 
    5006              :   /* We cannot leave a scope, if there are none left.  */
    5007   1372342259 :   if (NAMESPACE_LEVEL (global_namespace))
    5008   1372342259 :     gcc_assert (!global_scope_p (scope));
    5009              : 
    5010   1372342259 :   if (ENABLE_SCOPE_CHECKING)
    5011              :     {
    5012              :       indent (--binding_depth);
    5013              :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    5014              :                               "leave");
    5015              :     }
    5016              : 
    5017              :   /* Move one nesting level up.  */
    5018   1372342259 :   current_binding_level = scope->level_chain;
    5019              : 
    5020              :   /* Namespace-scopes are left most probably temporarily, not
    5021              :      completely; they can be reopened later, e.g. in namespace-extension
    5022              :      or any name binding activity that requires us to resume a
    5023              :      namespace.  For classes, we cache some binding levels.  For other
    5024              :      scopes, we just make the structure available for reuse.  */
    5025   1372342259 :   if (scope->kind != sk_namespace
    5026   1319013136 :       && scope != previous_class_level)
    5027              :     {
    5028   1042924973 :       scope->level_chain = free_binding_level;
    5029   1042924973 :       gcc_assert (!ENABLE_SCOPE_CHECKING
    5030              :                   || scope->binding_depth == binding_depth);
    5031   1042924973 :       free_binding_level = scope;
    5032              :     }
    5033              : 
    5034   1372342259 :   if (scope->kind == sk_class)
    5035              :     {
    5036              :       /* Reset DEFINING_CLASS_P to allow for reuse of a
    5037              :          class-defining scope in a non-defining context.  */
    5038    442611136 :       scope->defining_class_p = 0;
    5039              : 
    5040              :       /* Find the innermost enclosing class scope, and reset
    5041              :          CLASS_BINDING_LEVEL appropriately.  */
    5042    442611136 :       class_binding_level = NULL;
    5043   1810659248 :       for (scope = current_binding_level; scope; scope = scope->level_chain)
    5044    979448895 :         if (scope->kind == sk_class)
    5045              :           {
    5046     54011919 :             class_binding_level = scope;
    5047     54011919 :             break;
    5048              :           }
    5049              :     }
    5050              : 
    5051   1372342259 :   return current_binding_level;
    5052              : }
    5053              : 
    5054              : /* When we exit a toplevel class scope, we save its binding level so
    5055              :    that we can restore it quickly.  Here, we've entered some other
    5056              :    class, so we must invalidate our cache.  */
    5057              : 
    5058              : void
    5059     29791091 : invalidate_class_lookup_cache (void)
    5060              : {
    5061     29791091 :   previous_class_level->level_chain = free_binding_level;
    5062     29791091 :   free_binding_level = previous_class_level;
    5063     29791091 :   previous_class_level = NULL;
    5064     29791091 : }
    5065              : 
    5066              : static void
    5067     53329132 : resume_scope (cp_binding_level* b)
    5068              : {
    5069              :   /* Resuming binding levels is meant only for namespaces,
    5070              :      and those cannot nest into classes.  */
    5071     53329132 :   gcc_assert (!class_binding_level);
    5072              :   /* Also, resuming a non-directly nested namespace is a no-no.  */
    5073     53329132 :   gcc_assert (b->level_chain == current_binding_level);
    5074     53329132 :   current_binding_level = b;
    5075     53329132 :   if (ENABLE_SCOPE_CHECKING)
    5076              :     {
    5077              :       b->binding_depth = binding_depth;
    5078              :       indent (binding_depth);
    5079              :       cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
    5080              :       binding_depth++;
    5081              :     }
    5082     53329132 : }
    5083              : 
    5084              : /* Return the innermost binding level that is not for a class scope.  */
    5085              : 
    5086              : static cp_binding_level *
    5087   1682309342 : innermost_nonclass_level (void)
    5088              : {
    5089   1682309342 :   cp_binding_level *b;
    5090              : 
    5091   1682309342 :   b = current_binding_level;
    5092   1937510646 :   while (b->kind == sk_class)
    5093    255201304 :     b = b->level_chain;
    5094              : 
    5095   1682309342 :   return b;
    5096              : }
    5097              : 
    5098              : /* We're defining an object of type TYPE.  If it needs a cleanup, but
    5099              :    we're not allowed to add any more objects with cleanups to the current
    5100              :    scope, create a new binding level.  */
    5101              : 
    5102              : void
    5103      8468023 : maybe_push_cleanup_level (tree type)
    5104              : {
    5105      8468023 :   if (type != error_mark_node
    5106      8467831 :       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
    5107      8818534 :       && current_binding_level->more_cleanups_ok == 0)
    5108              :     {
    5109          444 :       begin_scope (sk_cleanup, NULL);
    5110          444 :       current_binding_level->statement_list = push_stmt_list ();
    5111              :     }
    5112      8468023 : }
    5113              : 
    5114              : /* Return true if we are in the global binding level.  */
    5115              : 
    5116              : bool
    5117       449733 : global_bindings_p (void)
    5118              : {
    5119       449733 :   return global_scope_p (current_binding_level);
    5120              : }
    5121              : 
    5122              : /* True if we are currently in a toplevel binding level.  This
    5123              :    means either the global binding level or a namespace in a toplevel
    5124              :    binding level.  Since there are no non-toplevel namespace levels,
    5125              :    this really means any namespace or template parameter level.  We
    5126              :    also include a class whose context is toplevel.  */
    5127              : 
    5128              : bool
    5129   1636201900 : toplevel_bindings_p (void)
    5130              : {
    5131   1636201900 :   cp_binding_level *b = innermost_nonclass_level ();
    5132              : 
    5133   1636201900 :   return b->kind == sk_namespace || b->kind == sk_template_parms;
    5134              : }
    5135              : 
    5136              : /* True if this is a namespace scope, or if we are defining a class
    5137              :    which is itself at namespace scope, or whose enclosing class is
    5138              :    such a class, etc.  */
    5139              : 
    5140              : bool
    5141     33057922 : namespace_bindings_p (void)
    5142              : {
    5143     33057922 :   cp_binding_level *b = innermost_nonclass_level ();
    5144              : 
    5145     33057922 :   return b->kind == sk_namespace;
    5146              : }
    5147              : 
    5148              : /* True if the innermost non-class scope is a block scope.  */
    5149              : 
    5150              : bool
    5151         1806 : local_bindings_p (void)
    5152              : {
    5153         1806 :   cp_binding_level *b = innermost_nonclass_level ();
    5154         1806 :   return b->kind < sk_function_parms || b->kind == sk_omp;
    5155              : }
    5156              : 
    5157              : /* True if the current level needs to have a BLOCK made.  */
    5158              : 
    5159              : bool
    5160    365206115 : kept_level_p (void)
    5161              : {
    5162    365206115 :   return (current_binding_level->blocks != NULL_TREE
    5163    327792937 :           || current_binding_level->keep
    5164    319886462 :           || current_binding_level->kind == sk_cleanup
    5165    319886462 :           || current_binding_level->names != NULL_TREE
    5166    648748977 :           || current_binding_level->using_directives);
    5167              : }
    5168              : 
    5169              : /* Returns the kind of the innermost scope.  */
    5170              : 
    5171              : scope_kind
    5172   2872613870 : innermost_scope_kind (void)
    5173              : {
    5174   2872613870 :   return current_binding_level->kind;
    5175              : }
    5176              : 
    5177              : /* Returns true if this scope was created to store template parameters.  */
    5178              : 
    5179              : bool
    5180    650265124 : template_parm_scope_p (void)
    5181              : {
    5182    650265124 :   return innermost_scope_kind () == sk_template_parms;
    5183              : }
    5184              : 
    5185              : /* If KEEP is true, make a BLOCK node for the next binding level,
    5186              :    unconditionally.  Otherwise, use the normal logic to decide whether
    5187              :    or not to create a BLOCK.  */
    5188              : 
    5189              : void
    5190     13342371 : keep_next_level (bool keep)
    5191              : {
    5192     13342371 :   keep_next_level_flag = keep;
    5193     13342371 : }
    5194              : 
    5195              : /* Return the list of declarations of the current local scope.  */
    5196              : 
    5197              : tree
    5198    367736142 : get_local_decls (void)
    5199              : {
    5200    367736142 :   gcc_assert (current_binding_level->kind != sk_namespace
    5201              :               && current_binding_level->kind != sk_class);
    5202    367736142 :   return current_binding_level->names;
    5203              : }
    5204              : 
    5205              : /* Return how many function prototypes we are currently nested inside.  */
    5206              : 
    5207              : int
    5208    280231788 : function_parm_depth (void)
    5209              : {
    5210    280231788 :   int level = 0;
    5211    280231788 :   cp_binding_level *b;
    5212              : 
    5213    280231788 :   for (b = current_binding_level;
    5214    561199702 :        b->kind == sk_function_parms;
    5215    280967914 :        b = b->level_chain)
    5216    280967914 :     ++level;
    5217              : 
    5218    280231788 :   return level;
    5219              : }
    5220              : 
    5221              : /* For debugging.  */
    5222              : static int no_print_functions = 0;
    5223              : static int no_print_builtins = 0;
    5224              : 
    5225              : static void
    5226            0 : print_binding_level (cp_binding_level* lvl)
    5227              : {
    5228            0 :   tree t;
    5229            0 :   int i = 0, len;
    5230            0 :   if (lvl->this_entity)
    5231            0 :     print_node_brief (stderr, "entity=", lvl->this_entity, 1);
    5232            0 :   fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
    5233            0 :   if (lvl->more_cleanups_ok)
    5234            0 :     fprintf (stderr, " more-cleanups-ok");
    5235            0 :   if (lvl->have_cleanups)
    5236            0 :     fprintf (stderr, " have-cleanups");
    5237            0 :   fprintf (stderr, "\n");
    5238            0 :   if (lvl->names)
    5239              :     {
    5240            0 :       fprintf (stderr, " names:\t");
    5241              :       /* We can probably fit 3 names to a line?  */
    5242            0 :       for (t = lvl->names; t; t = TREE_CHAIN (t))
    5243              :         {
    5244            0 :           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
    5245            0 :             continue;
    5246            0 :           if (no_print_builtins
    5247            0 :               && (TREE_CODE (t) == TYPE_DECL)
    5248            0 :               && DECL_IS_UNDECLARED_BUILTIN (t))
    5249            0 :             continue;
    5250              : 
    5251              :           /* Function decls tend to have longer names.  */
    5252            0 :           if (TREE_CODE (t) == FUNCTION_DECL)
    5253              :             len = 3;
    5254              :           else
    5255            0 :             len = 2;
    5256            0 :           i += len;
    5257            0 :           if (i > 6)
    5258              :             {
    5259            0 :               fprintf (stderr, "\n\t");
    5260            0 :               i = len;
    5261              :             }
    5262            0 :           print_node_brief (stderr, "", t, 0);
    5263            0 :           if (t == error_mark_node)
    5264              :             break;
    5265              :         }
    5266            0 :       if (i)
    5267            0 :         fprintf (stderr, "\n");
    5268              :     }
    5269            0 :   if (vec_safe_length (lvl->class_shadowed))
    5270              :     {
    5271            0 :       size_t i;
    5272            0 :       cp_class_binding *b;
    5273            0 :       fprintf (stderr, " class-shadowed:");
    5274            0 :       FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
    5275            0 :         fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
    5276            0 :       fprintf (stderr, "\n");
    5277              :     }
    5278            0 :   if (lvl->type_shadowed)
    5279              :     {
    5280            0 :       fprintf (stderr, " type-shadowed:");
    5281            0 :       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
    5282              :         {
    5283            0 :           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
    5284              :         }
    5285            0 :       fprintf (stderr, "\n");
    5286              :     }
    5287            0 : }
    5288              : 
    5289              : DEBUG_FUNCTION void
    5290            0 : debug (cp_binding_level &ref)
    5291              : {
    5292            0 :   print_binding_level (&ref);
    5293            0 : }
    5294              : 
    5295              : DEBUG_FUNCTION void
    5296            0 : debug (cp_binding_level *ptr)
    5297              : {
    5298            0 :   if (ptr)
    5299            0 :     debug (*ptr);
    5300              :   else
    5301            0 :     fprintf (stderr, "<nil>\n");
    5302            0 : }
    5303              : 
    5304              : static void
    5305            0 : print_other_binding_stack (cp_binding_level *stack)
    5306              : {
    5307            0 :   cp_binding_level *level;
    5308            0 :   for (level = stack; !global_scope_p (level); level = level->level_chain)
    5309              :     {
    5310            0 :       fprintf (stderr, "binding level %p\n", (void *) level);
    5311            0 :       print_binding_level (level);
    5312              :     }
    5313            0 : }
    5314              : 
    5315              : DEBUG_FUNCTION void
    5316            0 : print_binding_stack (void)
    5317              : {
    5318            0 :   cp_binding_level *b;
    5319            0 :   fprintf (stderr, "current_binding_level=%p\n"
    5320              :            "class_binding_level=%p\n"
    5321              :            "NAMESPACE_LEVEL (global_namespace)=%p\n",
    5322            0 :            (void *) current_binding_level, (void *) class_binding_level,
    5323            0 :            (void *) NAMESPACE_LEVEL (global_namespace));
    5324            0 :   if (class_binding_level)
    5325              :     {
    5326            0 :       for (b = class_binding_level; b; b = b->level_chain)
    5327            0 :         if (b == current_binding_level)
    5328              :           break;
    5329            0 :       if (b)
    5330              :         b = class_binding_level;
    5331              :       else
    5332            0 :         b = current_binding_level;
    5333              :     }
    5334              :   else
    5335            0 :     b = current_binding_level;
    5336            0 :   print_other_binding_stack (b);
    5337            0 :   fprintf (stderr, "global:\n");
    5338            0 :   print_binding_level (NAMESPACE_LEVEL (global_namespace));
    5339            0 : }
    5340              : 
    5341              : /* Push a definition of struct, union or enum tag named ID.  into
    5342              :    binding_level B.  DECL is a TYPE_DECL for the type.  DECL has
    5343              :    already been pushed into its binding level.  This is bookkeeping to
    5344              :    find it easily.  */
    5345              : 
    5346              : static void
    5347    408486837 : set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
    5348              : {
    5349    408486837 :   if (b->kind == sk_namespace)
    5350              :     /* At namespace scope we should not see an identifier type value.  */
    5351     26236796 :     gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
    5352              :                          /* But we might end up here with ill-formed input.  */
    5353              :                          || seen_error ());
    5354              :   else
    5355              :     {
    5356              :       /* Push the current type value, so we can restore it later  */
    5357    382250041 :       tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
    5358    382250041 :       b->type_shadowed = tree_cons (id, old, b->type_shadowed);
    5359    382250041 :       tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
    5360    382250041 :       TREE_TYPE (b->type_shadowed) = type;
    5361    382250041 :       SET_IDENTIFIER_TYPE_VALUE (id, type);
    5362              :     }
    5363    408486837 : }
    5364              : 
    5365              : /* As set_identifier_type_value_with_scope, but using
    5366              :    current_binding_level.  */
    5367              : 
    5368              : void
    5369    143974741 : set_identifier_type_value (tree id, tree decl)
    5370              : {
    5371    143974741 :   set_identifier_type_value_with_scope (id, decl, current_binding_level);
    5372    143974741 : }
    5373              : 
    5374              : /* Return the name for the constructor (or destructor) for the
    5375              :    specified class.  */
    5376              : 
    5377              : tree
    5378    385264281 : constructor_name (tree type)
    5379              : {
    5380    385264281 :   tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
    5381              : 
    5382    385264281 :   return decl ? DECL_NAME (decl) : NULL_TREE;
    5383              : }
    5384              : 
    5385              : /* Returns TRUE if NAME is the name for the constructor for TYPE,
    5386              :    which must be a class type.  */
    5387              : 
    5388              : bool
    5389    361075308 : constructor_name_p (tree name, tree type)
    5390              : {
    5391    361075308 :   gcc_assert (MAYBE_CLASS_TYPE_P (type));
    5392              : 
    5393              :   /* These don't have names.  */
    5394    361075308 :   if (TREE_CODE (type) == DECLTYPE_TYPE
    5395    361075305 :       || TREE_CODE (type) == TYPEOF_TYPE)
    5396              :     return false;
    5397              : 
    5398    361075305 :   if (name && name == constructor_name (type))
    5399              :     return true;
    5400              : 
    5401              :   return false;
    5402              : }
    5403              : 
    5404              : /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
    5405              :    caller to set DECL_CONTEXT properly.
    5406              : 
    5407              :    Warning: For class and block-scope this must only be used when X
    5408              :    will be the new innermost binding for its name, as we tack it onto
    5409              :    the front of IDENTIFIER_BINDING without checking to see if the
    5410              :    current IDENTIFIER_BINDING comes from a closer binding level than
    5411              :    LEVEL.
    5412              : 
    5413              :    Warning: For namespace scope, this will look in LEVEL for an
    5414              :    existing binding to match, but if not found will push the decl into
    5415              :    CURRENT_NAMESPACE.  Use push_nested_namespace/pushdecl/
    5416              :    pop_nested_namespace if you really need to push it into a foreign
    5417              :    namespace.  */
    5418              : 
    5419              : static tree
    5420     64547891 : do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
    5421              : {
    5422     64547891 :   cp_binding_level *b;
    5423              : 
    5424     64547891 :   if (level->kind == sk_class)
    5425              :     {
    5426            0 :       gcc_checking_assert (!hiding);
    5427            0 :       b = class_binding_level;
    5428            0 :       class_binding_level = level;
    5429            0 :       pushdecl_class_level (x);
    5430            0 :       class_binding_level = b;
    5431              :     }
    5432              :   else
    5433              :     {
    5434     64547891 :       tree function_decl = current_function_decl;
    5435     64547891 :       if (level->kind == sk_namespace)
    5436     60129845 :         current_function_decl = NULL_TREE;
    5437     64547891 :       b = current_binding_level;
    5438     64547891 :       current_binding_level = level;
    5439     64547891 :       x = pushdecl (x, hiding);
    5440     64547891 :       current_binding_level = b;
    5441     64547891 :       current_function_decl = function_decl;
    5442              :     }
    5443     64547891 :   return x;
    5444              : }
    5445              : 
    5446              : /* Inject X into the local scope just before the function parms.  */
    5447              : 
    5448              : tree
    5449      2299003 : pushdecl_outermost_localscope (tree x)
    5450              : {
    5451      2299003 :   cp_binding_level *b = NULL;
    5452      2299003 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5453              : 
    5454              :   /* Find the block scope just inside the function parms.  */
    5455      2299003 :   cp_binding_level *n = current_binding_level;
    5456      2517428 :   while (n && n->kind != sk_block)
    5457       218425 :     n = n->level_chain;
    5458      6398534 :   for (; n && n->kind != sk_function_parms; n = b->level_chain)
    5459      4099531 :     b = n;
    5460              : 
    5461      2299003 :   return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
    5462      2299003 : }
    5463              : 
    5464              : /* Checks if BINDING is a binding that we can export.  */
    5465              : 
    5466              : static bool
    5467        50227 : check_can_export_using_decl (tree binding)
    5468              : {
    5469              :   /* Declarations in header units are always OK.  */
    5470        50227 :   if (header_module_p ())
    5471              :     return true;
    5472              : 
    5473              :   /* We want the linkage of the underlying entity, so strip typedefs.
    5474              :      If the underlying entity is a builtin type then we're OK.  */
    5475        38979 :   tree entity = binding;
    5476        38979 :   if (TREE_CODE (entity) == TYPE_DECL)
    5477              :     {
    5478         1661 :       entity = TYPE_MAIN_DECL (TREE_TYPE (entity));
    5479         1661 :       if (!entity)
    5480              :         return true;
    5481              :     }
    5482              : 
    5483        38631 :   linkage_kind linkage = decl_linkage (entity);
    5484        38631 :   tree not_tmpl = STRIP_TEMPLATE (entity);
    5485              : 
    5486              :   /* Attachment is determined by the owner of an enumerator.  */
    5487        38631 :   if (TREE_CODE (not_tmpl) == CONST_DECL)
    5488           27 :     not_tmpl = TYPE_NAME (DECL_CONTEXT (not_tmpl));
    5489              : 
    5490              :   /* If the using decl is exported, the things it refers to must
    5491              :      have external linkage.  decl_linkage returns lk_external for
    5492              :      module linkage so also check for attachment.  */
    5493        38631 :   if (linkage != lk_external
    5494        38631 :       || (DECL_LANG_SPECIFIC (not_tmpl)
    5495        37977 :           && DECL_MODULE_ATTACH_P (not_tmpl)
    5496           92 :           && !DECL_MODULE_EXPORT_P (not_tmpl)))
    5497              :     {
    5498          148 :       auto_diagnostic_group d;
    5499          148 :       bool diag = true;
    5500              : 
    5501              :       /* As an extension, we'll allow exposing internal entities from
    5502              :          the GMF, to aid in migration to modules.  For now, we only
    5503              :          support this for functions and variables; see also 
    5504              :          depset::is_tu_local.  */
    5505          148 :       bool relaxed = (VAR_OR_FUNCTION_DECL_P (not_tmpl)
    5506          148 :                       && !(DECL_LANG_SPECIFIC (not_tmpl)
    5507           72 :                            && DECL_MODULE_PURVIEW_P (not_tmpl)));
    5508          148 :       if (relaxed)
    5509              :         {
    5510            9 :           gcc_checking_assert (linkage != lk_external);
    5511            9 :           diag = (warning_enabled_at (DECL_SOURCE_LOCATION (entity),
    5512            9 :                                       OPT_Wexpose_global_module_tu_local)
    5513           18 :                   && pedwarn (input_location,
    5514            9 :                               OPT_Wexpose_global_module_tu_local,
    5515              :                               "exporting %q#D that does not have "
    5516              :                               "external linkage", binding));
    5517              :         }
    5518              :       else
    5519          139 :         error ("exporting %q#D that does not have external linkage", binding);
    5520              : 
    5521          148 :       if (diag)
    5522              :         {
    5523          148 :           if (linkage == lk_none)
    5524           30 :             inform (DECL_SOURCE_LOCATION (entity),
    5525              :                     "%q#D declared here with no linkage", entity);
    5526          118 :           else if (linkage == lk_internal)
    5527           56 :             inform (DECL_SOURCE_LOCATION (entity),
    5528              :                     "%q#D declared here with internal linkage", entity);
    5529              :           else
    5530           62 :             inform (DECL_SOURCE_LOCATION (entity),
    5531              :                     "%q#D declared here with module linkage", entity);
    5532              :         }
    5533              : 
    5534          148 :       return relaxed;
    5535          148 :     }
    5536              : 
    5537              :   return true;
    5538              : }
    5539              : 
    5540              : /* Process a local-scope or namespace-scope using declaration.  LOOKUP
    5541              :    is the result of qualified lookup (both value & type are
    5542              :    significant).  FN_SCOPE_P indicates if we're at function-scope (as
    5543              :    opposed to namespace-scope).  *VALUE_P and *TYPE_P are the current
    5544              :    bindings, which are altered to reflect the newly brought in
    5545              :    declarations.  */
    5546              : 
    5547              : static bool
    5548     19275252 : do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
    5549              :                          bool insert_p, tree *value_p, tree *type_p)
    5550              : {
    5551     19275252 :   tree value = *value_p;
    5552     19275252 :   tree type = *type_p;
    5553     19275252 :   bool failed = false;
    5554              : 
    5555              :   /* Shift the old and new bindings around so we're comparing class and
    5556              :      enumeration names to each other.  */
    5557     19275252 :   if (value && DECL_IMPLICIT_TYPEDEF_P (strip_using_decl (value)))
    5558              :     {
    5559              :       type = value;
    5560              :       value = NULL_TREE;
    5561              :     }
    5562              : 
    5563     19275252 :   if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
    5564              :     {
    5565       175941 :       lookup.type = lookup.value;
    5566       175941 :       lookup.value = NULL_TREE;
    5567              :     }
    5568              : 
    5569              :   /* Only process exporting if we're going to be inserting.  */
    5570     19275252 :   bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
    5571              : 
    5572              :   /* First do the value binding.  */
    5573     19275252 :   if (!lookup.value)
    5574              :     /* Nothing (only implicit typedef found).  */
    5575       176119 :     gcc_checking_assert (lookup.type);
    5576     19099133 :   else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
    5577              :     {
    5578     15318227 :       for (lkp_iterator usings (lookup.value); usings; ++usings)
    5579              :         {
    5580      9983315 :           tree new_fn = *usings;
    5581      9983315 :           tree inner = STRIP_TEMPLATE (new_fn);
    5582      9983315 :           bool exporting_p = revealing_p && module_exporting_p ();
    5583        43292 :           if (exporting_p)
    5584        43292 :             exporting_p = check_can_export_using_decl (new_fn);
    5585              : 
    5586              :           /* [namespace.udecl]
    5587              : 
    5588              :              If a function declaration in namespace scope or block
    5589              :              scope has the same name and the same parameter types as a
    5590              :              function introduced by a using declaration the program is
    5591              :              ill-formed.  */
    5592              :           /* This seems overreaching, asking core -- why do we care
    5593              :              about decls in the namespace that we cannot name (because
    5594              :              they are not transitively imported.  We just check the
    5595              :              decls that are in this TU.  */
    5596      9983315 :           bool found = false;
    5597    167141596 :           for (ovl_iterator old (value); !found && old; ++old)
    5598              :             {
    5599     78979825 :               tree old_fn = *old;
    5600              : 
    5601     78979825 :               if (new_fn == old_fn)
    5602              :                 {
    5603              :                   /* The function already exists in the current
    5604              :                      namespace.  We will still want to insert it if
    5605              :                      it is revealing a not-revealed thing.  */
    5606       301956 :                   found = true;
    5607       301956 :                   if (old.hidden_p ())
    5608              :                     /* The function was merged with a hidden built-in;
    5609              :                        insert it again as not hidden.  */
    5610              :                     found = false;
    5611       301227 :                   else if (!revealing_p)
    5612              :                     ;
    5613        28134 :                   else if (old.using_p ())
    5614              :                     {
    5615              :                       /* Update in place.  'tis ok.  */
    5616        22980 :                       OVL_PURVIEW_P (old.get_using ()) = true;
    5617        22980 :                       if (exporting_p)
    5618        22956 :                         OVL_EXPORT_P (old.get_using ()) = true;
    5619              :                     }
    5620         5154 :                   else if (!DECL_LANG_SPECIFIC (inner)
    5621         5154 :                            || !DECL_MODULE_PURVIEW_P (inner)
    5622         5169 :                            || (exporting_p && !DECL_MODULE_EXPORT_P (inner)))
    5623              :                     /* We need to re-insert this function as a revealed
    5624              :                        (possibly exported) declaration.  We can't remove
    5625              :                        the existing decl because that will change any
    5626              :                        overloads cached in template functions.  */
    5627              :                     found = false;
    5628              :                   break;
    5629              :                 }
    5630     78677869 :               else if (old.using_p ())
    5631     74036536 :                 continue; /* This is a using decl. */
    5632      4641333 :               else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
    5633      2760734 :                 continue; /* This is an anticipated builtin.  */
    5634      1880599 :               else if (!matching_fn_p (new_fn, old_fn))
    5635      1880560 :                 continue; /* Parameters do not match.  */
    5636           39 :               else if (decls_match (new_fn, old_fn))
    5637              :                 {
    5638              :                   /* Extern "C" in different namespaces.  But similarly
    5639              :                      to above, if revealing a not-revealed thing we may
    5640              :                      need to reinsert.  */
    5641           18 :                   found = true;
    5642           18 :                   if (revealing_p
    5643           18 :                       && (!DECL_LANG_SPECIFIC (inner)
    5644            6 :                           || !DECL_MODULE_PURVIEW_P (inner)
    5645            3 :                           || (exporting_p && !DECL_MODULE_EXPORT_P (inner))))
    5646              :                     found = false;
    5647              :                   break;
    5648              :                 }
    5649              :               else
    5650              :                 {
    5651           21 :                   diagnose_name_conflict (new_fn, old_fn);
    5652           21 :                   failed = true;
    5653           21 :                   found = true;
    5654           21 :                   break;
    5655              :                 }
    5656              :             }
    5657              : 
    5658      9983315 :           if (!found && insert_p)
    5659              :             /* Unlike the decl-pushing case we don't drop anticipated
    5660              :                builtins here.  They don't cause a problem, and we'd
    5661              :                like to match them with a future declaration.  */
    5662      9684752 :             value = ovl_insert (new_fn, value, 1 + revealing_p + exporting_p);
    5663              :         }
    5664      5334912 :     }
    5665     13764221 :   else if (value
    5666              :            /* Ignore anticipated builtins.  */
    5667        65271 :            && !anticipated_builtin_p (value)
    5668     13829488 :            && !decls_match (lookup.value, strip_using_decl (value)))
    5669              :     {
    5670           15 :       diagnose_name_conflict (lookup.value, value);
    5671           15 :       failed = true;
    5672              :     }
    5673     13764206 :   else if (insert_p)
    5674              :     {
    5675              :       /* A using-decl does not necessarily have the same purview-ness or
    5676              :          exporting as the declaration it reveals, so build a USING_DECL
    5677              :          that we can attach this information to.  This also gives us a
    5678              :          location for the using-decl that we can use in diagnostics.
    5679              : 
    5680              :          But this is unnecessary if we're just redeclaring the same decl;
    5681              :          in that case we can just mark it purview or exported directly.  */
    5682     13762909 :       if (value != lookup.value)
    5683              :         {
    5684     13728290 :           value = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5685     13728290 :           USING_DECL_DECLS (value) = lookup.value;
    5686     13728290 :           USING_DECL_SCOPE (value) = CP_DECL_CONTEXT (lookup.value);
    5687     13728290 :           DECL_CONTEXT (value) = current_scope ();
    5688     13728290 :           DECL_MODULE_PURVIEW_P (value) = module_purview_p ();
    5689              :         }
    5690              :       else
    5691        34619 :         set_instantiating_module (value);
    5692              : 
    5693     13762909 :       if (revealing_p
    5694         6312 :           && module_exporting_p ()
    5695     13769086 :           && check_can_export_using_decl (lookup.value))
    5696              :         {
    5697         6086 :           if (TREE_CODE (value) == TEMPLATE_DECL)
    5698         1493 :             DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (value)) = true;
    5699         6086 :           DECL_MODULE_EXPORT_P (value) = true;
    5700              :         }
    5701              :     }
    5702              : 
    5703              :   /* Now the type binding.  */
    5704     19275252 :   if (lookup.type)
    5705              :     {
    5706       176138 :       if (type && !decls_match (lookup.type, strip_using_decl (type)))
    5707              :         {
    5708            3 :           diagnose_name_conflict (lookup.type, type);
    5709            3 :           failed = true;
    5710              :         }
    5711       176135 :       else if (insert_p)
    5712              :         {
    5713              :           /* As with revealing value bindings.  */
    5714       175957 :           if (type != lookup.type)
    5715              :             {
    5716       175588 :               type = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5717       175588 :               USING_DECL_DECLS (type) = lookup.type;
    5718       175588 :               USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (lookup.type);
    5719       175588 :               DECL_CONTEXT (type) = current_scope ();
    5720       175588 :               DECL_MODULE_PURVIEW_P (type) = module_purview_p ();
    5721              :             }
    5722              :           else
    5723          369 :             set_instantiating_module (type);
    5724              : 
    5725       175957 :           if (revealing_p
    5726          777 :               && module_exporting_p ()
    5727       176715 :               && check_can_export_using_decl (lookup.type))
    5728          743 :             DECL_MODULE_EXPORT_P (type) = true;
    5729              :         }
    5730              :     }
    5731              : 
    5732     19275074 :   if (insert_p)
    5733              :     {
    5734              :       /* If value is empty, shift any class or enumeration name back.  */
    5735     19271708 :       if (!value)
    5736              :         {
    5737       175932 :           value = type;
    5738       175932 :           type = NULL_TREE;
    5739              :         }
    5740     19271708 :       *value_p = value;
    5741     19271708 :       *type_p = type;
    5742              :     }
    5743              : 
    5744     19275252 :   return failed;
    5745              : }
    5746              : 
    5747              : /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
    5748              :    Both are namespaces.  */
    5749              : 
    5750              : bool
    5751    127961670 : is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
    5752              : {
    5753    127961670 :   int depth = SCOPE_DEPTH (ancestor);
    5754              : 
    5755    127961670 :   if (!depth && !inline_only)
    5756              :     /* The global namespace encloses everything.  */
    5757              :     return true;
    5758              : 
    5759    130096644 :   while (SCOPE_DEPTH (descendant) > depth
    5760    130096644 :          && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
    5761      2225918 :     descendant = CP_DECL_CONTEXT (descendant);
    5762              : 
    5763    127870726 :   return ancestor == descendant;
    5764              : }
    5765              : 
    5766              : /* Returns true if ROOT (a non-alias namespace, class, or function)
    5767              :    encloses CHILD.  CHILD may be either a class type or a namespace
    5768              :    (maybe alias).  */
    5769              : 
    5770              : bool
    5771     96299329 : is_ancestor (tree root, tree child)
    5772              : {
    5773     96299329 :   gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
    5774              :                         && !DECL_NAMESPACE_ALIAS (root))
    5775              :                        || TREE_CODE (root) == FUNCTION_DECL
    5776              :                        || CLASS_TYPE_P (root));
    5777     96299329 :   gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
    5778              :                        || CLASS_TYPE_P (child));
    5779              : 
    5780              :   /* The global namespace encloses everything.  Early-out for the
    5781              :      common case.  */
    5782     96299329 :   if (root == global_namespace)
    5783              :     return true;
    5784              : 
    5785              :   /* Search CHILD until we reach namespace scope.  */
    5786    202301505 :   while (TREE_CODE (child) != NAMESPACE_DECL)
    5787              :     {
    5788              :       /* If we've reached the ROOT, it encloses CHILD.  */
    5789    106270172 :       if (root == child)
    5790              :         return true;
    5791              : 
    5792              :       /* Go out one level.  */
    5793    106270121 :       if (TYPE_P (child))
    5794    104118227 :         child = TYPE_NAME (child);
    5795    106270121 :       child = CP_DECL_CONTEXT (child);
    5796              :     }
    5797              : 
    5798     96031333 :   if (TREE_CODE (root) != NAMESPACE_DECL)
    5799              :     /* Failed to meet the non-namespace we were looking for.  */
    5800              :     return false;
    5801              : 
    5802     96031318 :   if (tree alias = DECL_NAMESPACE_ALIAS (child))
    5803            3 :     child = alias;
    5804              : 
    5805     96031318 :   return is_nested_namespace (root, child);
    5806              : }
    5807              : 
    5808              : /* Enter the class or namespace scope indicated by T suitable for name
    5809              :    lookup.  T can be arbitrary scope, not necessary nested inside the
    5810              :    current scope.  Returns a non-null scope to pop iff pop_scope
    5811              :    should be called later to exit this scope.  */
    5812              : 
    5813              : tree
    5814    344330656 : push_scope (tree t)
    5815              : {
    5816    344330656 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5817     83035757 :     push_decl_namespace (t);
    5818    261294899 :   else if (CLASS_TYPE_P (t))
    5819              :     {
    5820    196824196 :       if (!at_class_scope_p ()
    5821    196824196 :           || !same_type_p (current_class_type, t))
    5822     93153704 :         push_nested_class (t);
    5823              :       else
    5824              :         /* T is the same as the current scope.  There is therefore no
    5825              :            need to re-enter the scope.  Since we are not actually
    5826              :            pushing a new scope, our caller should not call
    5827              :            pop_scope.  */
    5828              :         t = NULL_TREE;
    5829              :     }
    5830              : 
    5831    344330656 :   return t;
    5832              : }
    5833              : 
    5834              : /* Leave scope pushed by push_scope.  */
    5835              : 
    5836              : void
    5837    240660632 : pop_scope (tree t)
    5838              : {
    5839    240660632 :   if (t == NULL_TREE)
    5840              :     return;
    5841    240660161 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5842     83035754 :     pop_decl_namespace ();
    5843    157624407 :   else if CLASS_TYPE_P (t)
    5844     93153704 :     pop_nested_class ();
    5845              : }
    5846              : 
    5847              : /* Subroutine of push_inner_scope.  */
    5848              : 
    5849              : static void
    5850       263498 : push_inner_scope_r (tree outer, tree inner)
    5851              : {
    5852       263498 :   tree prev;
    5853              : 
    5854       263498 :   if (outer == inner
    5855       263498 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5856              :     return;
    5857              : 
    5858       263489 :   prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5859       263489 :   if (outer != prev)
    5860         1716 :     push_inner_scope_r (outer, prev);
    5861       263489 :   if (TREE_CODE (inner) == NAMESPACE_DECL)
    5862              :     {
    5863              :       cp_binding_level *save_template_parm = 0;
    5864              :       /* Temporary take out template parameter scopes.  They are saved
    5865              :          in reversed order in save_template_parm.  */
    5866       108577 :       while (current_binding_level->kind == sk_template_parms)
    5867              :         {
    5868        54221 :           cp_binding_level *b = current_binding_level;
    5869        54221 :           current_binding_level = b->level_chain;
    5870        54221 :           b->level_chain = save_template_parm;
    5871        54221 :           save_template_parm = b;
    5872              :         }
    5873              : 
    5874        54356 :       resume_scope (NAMESPACE_LEVEL (inner));
    5875        54356 :       current_namespace = inner;
    5876              : 
    5877              :       /* Restore template parameter scopes.  */
    5878       108577 :       while (save_template_parm)
    5879              :         {
    5880        54221 :           cp_binding_level *b = save_template_parm;
    5881        54221 :           save_template_parm = b->level_chain;
    5882        54221 :           b->level_chain = current_binding_level;
    5883        54221 :           current_binding_level = b;
    5884              :         }
    5885              :     }
    5886              :   else
    5887       209133 :     pushclass (inner);
    5888              : }
    5889              : 
    5890              : /* Enter the scope INNER from current scope.  INNER must be a scope
    5891              :    nested inside current scope.  This works with both name lookup and
    5892              :    pushing name into scope.  In case a template parameter scope is present,
    5893              :    namespace is pushed under the template parameter scope according to
    5894              :    name lookup rule in 14.6.1/6.
    5895              : 
    5896              :    Return the former current scope suitable for pop_inner_scope.  */
    5897              : 
    5898              : tree
    5899       261782 : push_inner_scope (tree inner)
    5900              : {
    5901       261782 :   tree outer = current_scope ();
    5902       261782 :   if (!outer)
    5903            0 :     outer = current_namespace;
    5904              : 
    5905       261782 :   push_inner_scope_r (outer, inner);
    5906       261782 :   return outer;
    5907              : }
    5908              : 
    5909              : /* Exit the current scope INNER back to scope OUTER.  */
    5910              : 
    5911              : void
    5912       261782 : pop_inner_scope (tree outer, tree inner)
    5913              : {
    5914       261782 :   if (outer == inner
    5915       261782 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5916              :     return;
    5917              : 
    5918       525265 :   while (outer != inner)
    5919              :     {
    5920       263492 :       if (TREE_CODE (inner) == NAMESPACE_DECL)
    5921              :         {
    5922              :           cp_binding_level *save_template_parm = 0;
    5923              :           /* Temporary take out template parameter scopes.  They are saved
    5924              :              in reversed order in save_template_parm.  */
    5925       108577 :           while (current_binding_level->kind == sk_template_parms)
    5926              :             {
    5927        54221 :               cp_binding_level *b = current_binding_level;
    5928        54221 :               current_binding_level = b->level_chain;
    5929        54221 :               b->level_chain = save_template_parm;
    5930        54221 :               save_template_parm = b;
    5931              :             }
    5932              : 
    5933        54356 :           pop_namespace ();
    5934              : 
    5935              :           /* Restore template parameter scopes.  */
    5936       162933 :           while (save_template_parm)
    5937              :             {
    5938        54221 :               cp_binding_level *b = save_template_parm;
    5939        54221 :               save_template_parm = b->level_chain;
    5940        54221 :               b->level_chain = current_binding_level;
    5941        54221 :               current_binding_level = b;
    5942              :             }
    5943              :         }
    5944              :       else
    5945       209136 :         popclass ();
    5946              : 
    5947       263489 :       inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5948              :     }
    5949              : }
    5950              : 
    5951              : /* Do a pushlevel for class declarations.  */
    5952              : 
    5953              : void
    5954    239451837 : pushlevel_class (void)
    5955              : {
    5956    239451837 :   class_binding_level = begin_scope (sk_class, current_class_type);
    5957    239451837 : }
    5958              : 
    5959              : /* ...and a poplevel for class declarations.  */
    5960              : 
    5961              : void
    5962    442611139 : poplevel_class (void)
    5963              : {
    5964    442611139 :   cp_binding_level *level = class_binding_level;
    5965    442611139 :   cp_class_binding *cb;
    5966    442611139 :   size_t i;
    5967    442611139 :   tree shadowed;
    5968              : 
    5969    442611139 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5970    442611139 :   gcc_assert (level != 0);
    5971              : 
    5972              :   /* If we're leaving a toplevel class, cache its binding level.  */
    5973    442611136 :   if (current_class_depth == 1)
    5974    276088163 :     previous_class_level = level;
    5975    442611136 :   for (shadowed = level->type_shadowed;
    5976   1422719544 :        shadowed;
    5977    980108408 :        shadowed = TREE_CHAIN (shadowed))
    5978    980108408 :     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
    5979              : 
    5980              :   /* Remove the bindings for all of the class-level declarations.  */
    5981    442611136 :   if (level->class_shadowed)
    5982              :     {
    5983    627035545 :       FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
    5984              :         {
    5985    479000954 :           IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
    5986    479000954 :           cxx_binding_free (cb->base);
    5987              :         }
    5988    148034591 :       ggc_free (level->class_shadowed);
    5989    148034591 :       level->class_shadowed = NULL;
    5990              :     }
    5991              : 
    5992              :   /* Now, pop out of the binding level which we created up in the
    5993              :      `pushlevel_class' routine.  */
    5994    442611136 :   gcc_assert (current_binding_level == level);
    5995    442611136 :   leave_scope ();
    5996    442611136 : }
    5997              : 
    5998              : /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
    5999              :    appropriate.  DECL is the value to which a name has just been
    6000              :    bound.  CLASS_TYPE is the class in which the lookup occurred.  */
    6001              : 
    6002              : static void
    6003    153606061 : set_inherited_value_binding_p (cxx_binding *binding, tree decl,
    6004              :                                tree class_type)
    6005              : {
    6006    153606061 :   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
    6007              :     {
    6008    153227210 :       tree context;
    6009              : 
    6010    153227210 :       if (is_overloaded_fn (decl))
    6011     44098486 :         context = ovl_scope (decl);
    6012              :       else
    6013              :         {
    6014    109128724 :           gcc_assert (DECL_P (decl));
    6015    109128724 :           context = context_for_name_lookup (decl);
    6016              :         }
    6017              : 
    6018    153227210 :       if (is_properly_derived_from (class_type, context))
    6019     18168575 :         INHERITED_VALUE_BINDING_P (binding) = 1;
    6020              :       else
    6021    135058635 :         INHERITED_VALUE_BINDING_P (binding) = 0;
    6022              :     }
    6023       378851 :   else if (binding->value == decl)
    6024              :     /* We only encounter a TREE_LIST when there is an ambiguity in the
    6025              :        base classes.  Such an ambiguity can be overridden by a
    6026              :        definition in this class.  */
    6027       378851 :     INHERITED_VALUE_BINDING_P (binding) = 1;
    6028              :   else
    6029            0 :     INHERITED_VALUE_BINDING_P (binding) = 0;
    6030    153606061 : }
    6031              : 
    6032              : /* Make the declaration of X appear in CLASS scope.  */
    6033              : 
    6034              : bool
    6035    190822934 : pushdecl_class_level (tree x)
    6036              : {
    6037    190822934 :   bool is_valid = true;
    6038              : 
    6039              :   /* Do nothing if we're adding to an outer lambda closure type,
    6040              :      outer_binding will add it later if it's needed.  */
    6041    190822934 :   if (current_class_type != class_binding_level->this_entity)
    6042              :     return true;
    6043              : 
    6044    190822934 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6045              :   /* Get the name of X.  */
    6046    381645868 :   tree name = OVL_NAME (x);
    6047              : 
    6048    190822934 :   if (name)
    6049              :     {
    6050    190423922 :       is_valid = push_class_level_binding (name, x);
    6051    190423922 :       if (TREE_CODE (x) == TYPE_DECL)
    6052    137885454 :         set_identifier_type_value (name, x);
    6053              :     }
    6054       399012 :   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
    6055              :     {
    6056              :       /* If X is an anonymous aggregate, all of its members are
    6057              :          treated as if they were members of the class containing the
    6058              :          aggregate, for naming purposes.  */
    6059       265240 :       location_t save_location = input_location;
    6060       265240 :       tree anon = TREE_TYPE (x);
    6061       265240 :       if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
    6062       410180 :         for (unsigned ix = member_vec->length (); ix--;)
    6063              :           {
    6064       380920 :             tree binding = (*member_vec)[ix];
    6065       380920 :             if (STAT_HACK_P (binding))
    6066              :               {
    6067            0 :                 if (!pushdecl_class_level (STAT_TYPE (binding)))
    6068            0 :                   is_valid = false;
    6069            0 :                 binding = STAT_DECL (binding);
    6070              :               }
    6071       380920 :             if (!pushdecl_class_level (binding))
    6072            0 :               is_valid = false;
    6073              :         }
    6074              :       else
    6075      1031444 :         for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
    6076       795464 :           if (TREE_CODE (f) == FIELD_DECL)
    6077              :             {
    6078       485190 :               input_location = DECL_SOURCE_LOCATION (f);
    6079       485190 :               if (!pushdecl_class_level (f))
    6080       795464 :                 is_valid = false;
    6081              :             }
    6082       265240 :       input_location = save_location;
    6083              :     }
    6084    190822934 :   return is_valid;
    6085    190822934 : }
    6086              : 
    6087              : /* Return the BINDING (if any) for NAME in SCOPE, which is a class
    6088              :    scope.  If the value returned is non-NULL, and the PREVIOUS field
    6089              :    is not set, callers must set the PREVIOUS field explicitly.  */
    6090              : 
    6091              : static cxx_binding *
    6092   2129014607 : get_class_binding (tree name, cp_binding_level *scope)
    6093              : {
    6094   2129014607 :   tree class_type;
    6095   2129014607 :   tree type_binding;
    6096   2129014607 :   tree value_binding;
    6097   2129014607 :   cxx_binding *binding;
    6098              : 
    6099   2129014607 :   class_type = scope->this_entity;
    6100              : 
    6101              :   /* Get the type binding.  */
    6102   2129014607 :   type_binding = lookup_member (class_type, name,
    6103              :                                 /*protect=*/2, /*want_type=*/true,
    6104              :                                 tf_warning_or_error);
    6105              :   /* Get the value binding.  */
    6106   2129014607 :   value_binding = lookup_member (class_type, name,
    6107              :                                  /*protect=*/2, /*want_type=*/false,
    6108              :                                  tf_warning_or_error);
    6109              : 
    6110              :   /* If we found either a type binding or a value binding, create a
    6111              :      new binding object.  */
    6112   2129014607 :   if (type_binding || value_binding)
    6113              :     {
    6114    153606061 :       binding = new_class_binding (name,
    6115              :                                    value_binding,
    6116              :                                    type_binding,
    6117              :                                    scope);
    6118    153606061 :       set_inherited_value_binding_p (binding, value_binding, class_type);
    6119              :     }
    6120              :   else
    6121              :     binding = NULL;
    6122              : 
    6123   2129014607 :   return binding;
    6124              : }
    6125              : 
    6126              : /* Make the declaration(s) of X appear in CLASS scope under the name
    6127              :    NAME.  Returns true if the binding is valid.  */
    6128              : 
    6129              : bool
    6130    466374305 : push_class_level_binding (tree name, tree x)
    6131              : {
    6132    466374305 :   cxx_binding *binding;
    6133    466374305 :   tree decl = x;
    6134    466374305 :   bool ok;
    6135              : 
    6136    466374305 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6137              : 
    6138              :   /* The class_binding_level will be NULL if x is a template
    6139              :      parameter name in a member template.  */
    6140    466374305 :   if (!class_binding_level)
    6141              :     return true;
    6142              : 
    6143    466374305 :   if (name == error_mark_node)
    6144              :     return false;
    6145              : 
    6146              :   /* Can happen for an erroneous declaration (c++/60384).  */
    6147    466374305 :   if (!identifier_p (name))
    6148              :     {
    6149            3 :       gcc_assert (errorcount || sorrycount);
    6150              :       return false;
    6151              :     }
    6152              : 
    6153              :   /* Check for invalid member names.  But don't worry about a default
    6154              :      argument-scope lambda being pushed after the class is complete.  */
    6155    466374323 :   gcc_assert (TYPE_BEING_DEFINED (current_class_type)
    6156              :               || LAMBDA_TYPE_P (TREE_TYPE (decl)));
    6157              :   /* Check that we're pushing into the right binding level.  */
    6158    466374302 :   gcc_assert (current_class_type == class_binding_level->this_entity);
    6159              : 
    6160              :   /* We could have been passed a tree list if this is an ambiguous
    6161              :      declaration. If so, pull the declaration out because
    6162              :      check_template_shadow will not handle a TREE_LIST.  */
    6163    466374302 :   if (TREE_CODE (decl) == TREE_LIST
    6164    466374302 :       && TREE_TYPE (decl) == error_mark_node)
    6165            0 :     decl = TREE_VALUE (decl);
    6166              : 
    6167    466374302 :   if (!check_template_shadow (decl))
    6168              :     return false;
    6169              : 
    6170              :   /* [class.mem]
    6171              : 
    6172              :      If T is the name of a class, then each of the following shall
    6173              :      have a name different from T:
    6174              : 
    6175              :      -- every static data member of class T;
    6176              : 
    6177              :      -- every member of class T that is itself a type;
    6178              : 
    6179              :      -- every enumerator of every member of class T that is an
    6180              :         enumerated type;
    6181              : 
    6182              :      -- every member of every anonymous union that is a member of
    6183              :         class T.
    6184              : 
    6185              :      (Non-static data members were also forbidden to have the same
    6186              :      name as T until TC1.)  */
    6187    466374254 :   if ((VAR_P (x)
    6188    466374254 :        || TREE_CODE (x) == CONST_DECL
    6189    447931925 :        || (TREE_CODE (x) == TYPE_DECL
    6190    137885433 :            && !DECL_SELF_REFERENCE_P (x))
    6191              :        /* A data member of an anonymous union.  */
    6192    378211818 :        || (TREE_CODE (x) == FIELD_DECL
    6193     27540419 :            && DECL_CONTEXT (x) != current_class_type))
    6194    536885552 :       && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
    6195              :     {
    6196           24 :       tree scope = context_for_name_lookup (x);
    6197           24 :       if (TYPE_P (scope) && same_type_p (scope, current_class_type))
    6198              :         {
    6199           24 :           error_at (DECL_SOURCE_LOCATION (x),
    6200              :                     "%qD has the same name as the class in which it is "
    6201              :                     "declared", x);
    6202           24 :           return false;
    6203              :         }
    6204              :     }
    6205              : 
    6206              :   /* Get the current binding for NAME in this class, if any.  */
    6207    466374230 :   binding = IDENTIFIER_BINDING (name);
    6208    466374230 :   if (!binding || binding->scope != class_binding_level)
    6209              :     {
    6210    337033373 :       binding = get_class_binding (name, class_binding_level);
    6211              :       /* If a new binding was created, put it at the front of the
    6212              :          IDENTIFIER_BINDING list.  */
    6213    337033373 :       if (binding)
    6214              :         {
    6215     11635639 :           binding->previous = IDENTIFIER_BINDING (name);
    6216     11635639 :           IDENTIFIER_BINDING (name) = binding;
    6217              :         }
    6218              :     }
    6219              : 
    6220              :   /* If there is already a binding, then we may need to update the
    6221              :      current value.  */
    6222    140976496 :   if (binding && binding->value)
    6223              :     {
    6224    140976496 :       tree bval = binding->value;
    6225    140976496 :       tree old_decl = NULL_TREE;
    6226    140976496 :       tree target_decl = strip_using_decl (decl);
    6227    140976496 :       tree target_bval = strip_using_decl (bval);
    6228              : 
    6229    140976496 :       if (INHERITED_VALUE_BINDING_P (binding))
    6230              :         {
    6231              :           /* If the old binding was from a base class, and was for a
    6232              :              tag name, slide it over to make room for the new binding.
    6233              :              The old binding is still visible if explicitly qualified
    6234              :              with a class-key.  */
    6235     13966235 :           if (TREE_CODE (target_bval) == TYPE_DECL
    6236      6820461 :               && DECL_ARTIFICIAL (target_bval)
    6237     14906134 :               && !(TREE_CODE (target_decl) == TYPE_DECL
    6238       939889 :                    && DECL_ARTIFICIAL (target_decl)))
    6239              :             {
    6240        81692 :               old_decl = binding->type;
    6241        81692 :               binding->type = bval;
    6242        81692 :               binding->value = NULL_TREE;
    6243        81692 :               INHERITED_VALUE_BINDING_P (binding) = 0;
    6244              :             }
    6245              :           else
    6246              :             {
    6247     13884543 :               old_decl = bval;
    6248              :               /* Any inherited type declaration is hidden by the type
    6249              :                  declaration in the derived class.  */
    6250     13884543 :               if (TREE_CODE (target_decl) == TYPE_DECL
    6251     13884543 :                   && DECL_ARTIFICIAL (target_decl))
    6252       858716 :                 binding->type = NULL_TREE;
    6253              :             }
    6254              :         }
    6255    127010261 :       else if (TREE_CODE (decl) == USING_DECL
    6256        11087 :                && TREE_CODE (bval) == USING_DECL
    6257    127010367 :                && same_type_p (USING_DECL_SCOPE (decl),
    6258              :                                USING_DECL_SCOPE (bval)))
    6259              :         /* This is a using redeclaration that will be diagnosed later
    6260              :            in supplement_binding */
    6261              :         ;
    6262    127010228 :       else if (TREE_CODE (decl) == USING_DECL
    6263        11054 :                && TREE_CODE (bval) == USING_DECL
    6264           73 :                && DECL_DEPENDENT_P (decl)
    6265    127010247 :                && DECL_DEPENDENT_P (bval))
    6266              :         return true;
    6267    127010209 :       else if (TREE_CODE (decl) == USING_DECL
    6268        11035 :                && DECL_DEPENDENT_P (decl)
    6269    127020160 :                && OVL_P (target_bval))
    6270              :         /* The new dependent using beats an old overload.  */
    6271              :         old_decl = bval;
    6272    127000258 :       else if (TREE_CODE (bval) == USING_DECL
    6273       669485 :                && DECL_DEPENDENT_P (bval)
    6274    127387944 :                && OVL_P (target_decl))
    6275              :         /* The old dependent using beats a new overload.  */
    6276              :         return true;
    6277    126612584 :       else if (OVL_P (target_decl)
    6278    126402095 :                && OVL_P (target_bval))
    6279              :         /* The new overload set contains the old one.  */
    6280              :         old_decl = bval;
    6281              : 
    6282    140378257 :       if (old_decl && binding->scope == class_binding_level)
    6283              :         {
    6284    140378257 :           binding->value = x;
    6285              :           /* It is always safe to clear INHERITED_VALUE_BINDING_P
    6286              :              here.  This function is only used to register bindings
    6287              :              from with the class definition itself.  */
    6288    140378257 :           INHERITED_VALUE_BINDING_P (binding) = 0;
    6289    140378257 :           return true;
    6290              :         }
    6291              :     }
    6292              : 
    6293              :   /* Note that we declared this value so that we can issue an error if
    6294              :      this is an invalid redeclaration of a name already used for some
    6295              :      other purpose.  */
    6296    325608280 :   note_name_declared_in_class (name, decl);
    6297              : 
    6298              :   /* If we didn't replace an existing binding, put the binding on the
    6299              :      stack of bindings for the identifier, and update the shadowed
    6300              :      list.  */
    6301    325608280 :   if (binding && binding->scope == class_binding_level)
    6302              :     /* Supplement the existing binding.  */
    6303       210546 :     ok = supplement_binding (binding, decl);
    6304              :   else
    6305              :     {
    6306              :       /* Create a new binding.  */
    6307    325397734 :       push_binding (name, decl, class_binding_level);
    6308    325397734 :       ok = true;
    6309              :     }
    6310              : 
    6311              :   return ok;
    6312    466374305 : }
    6313              : 
    6314              : /* Process and lookup a using decl SCOPE::lookup.name, filling in
    6315              :    lookup.values & lookup.type.  Return a USING_DECL, or NULL_TREE on
    6316              :    failure.  */
    6317              : 
    6318              : static tree
    6319     22388230 : lookup_using_decl (tree scope, name_lookup &lookup)
    6320              : {
    6321     22388230 :   tree current = current_scope ();
    6322     22388230 :   bool dependent_p = false;
    6323     22388230 :   tree binfo = NULL_TREE;
    6324     22388230 :   base_kind b_kind = bk_not_base;
    6325              : 
    6326              :   /* Because C++20 breaks the invariant that only member using-decls
    6327              :      refer to members and only non-member using-decls refer to
    6328              :      non-members, we first do the lookups, and then do validation that
    6329              :      what we found is ok.  */
    6330              : 
    6331     22388230 :   if (TREE_CODE (scope) == ENUMERAL_TYPE
    6332     12759299 :       && cxx_dialect < cxx20
    6333     12759299 :       && UNSCOPED_ENUM_P (scope)
    6334     22388240 :       && !TYPE_FUNCTION_SCOPE_P (scope))
    6335              :     {
    6336              :       /* PR c++/60265 argued that since C++11 added explicit enum scope, we
    6337              :          should allow it as meaning the enclosing scope.  I don't see any
    6338              :          justification for this in C++11, but let's keep allowing it.  */
    6339            9 :       tree ctx = CP_TYPE_CONTEXT (scope);
    6340           24 :       if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
    6341     22388230 :         scope = ctx;
    6342              :     }
    6343              : 
    6344              :   /* You cannot using-decl a destructor.  */
    6345     22388230 :   if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
    6346              :     {
    6347            6 :       error ("%<%T%s%D%> names destructor", scope,
    6348            3 :              &"::"[scope == global_namespace ? 2 : 0], lookup.name);
    6349            3 :       return NULL_TREE;
    6350              :     }
    6351              : 
    6352     22388227 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    6353              :     {
    6354              :       /* Naming a namespace member.  */
    6355      6693129 :       qualified_namespace_lookup (scope, &lookup);
    6356              : 
    6357      6693129 :       if (TYPE_P (current)
    6358            3 :           && (!lookup.value
    6359            0 :               || lookup.type
    6360            0 :               || cxx_dialect < cxx20
    6361            0 :               || TREE_CODE (lookup.value) != CONST_DECL))
    6362              :         {
    6363            3 :           error ("using-declaration for non-member at class scope");
    6364            3 :           return NULL_TREE;
    6365              :         }
    6366              :     }
    6367     15695098 :   else if (TREE_CODE (scope) == ENUMERAL_TYPE)
    6368              :     {
    6369              :       /* Naming an enumeration member.  */
    6370     12759289 :       if (cxx_dialect < cxx20)
    6371            6 :         error ("%<using%> with enumeration scope %q#T "
    6372              :                "only available with %<-std=c++20%> or %<-std=gnu++20%>",
    6373              :                scope);
    6374     12759289 :       lookup.value = lookup_enumerator (scope, lookup.name);
    6375              :     }
    6376              :   else
    6377              :     {
    6378              :       /* Naming a class member.  This is awkward in C++20, because we
    6379              :          might be naming an enumerator of an unrelated class.  */
    6380              : 
    6381      2935809 :       tree npscope = scope;
    6382      2935809 :       if (PACK_EXPANSION_P (scope))
    6383         9175 :         npscope = PACK_EXPANSION_PATTERN (scope);
    6384              : 
    6385      2935809 :       if (!MAYBE_CLASS_TYPE_P (npscope))
    6386              :         {
    6387            9 :           error ("%qT is not a class, namespace, or enumeration", npscope);
    6388            9 :           return NULL_TREE;
    6389              :         }
    6390              : 
    6391              :       /* Using T::T declares inheriting ctors, even if T is a typedef.  */
    6392      2935800 :       if (lookup.name == TYPE_IDENTIFIER (npscope)
    6393      2935800 :           || constructor_name_p (lookup.name, npscope))
    6394              :         {
    6395       308102 :           if (!TYPE_P (current))
    6396              :             {
    6397            0 :               error ("non-member using-declaration names constructor of %qT",
    6398              :                      npscope);
    6399            0 :               return NULL_TREE;
    6400              :             }
    6401       308102 :           maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
    6402       308102 :           lookup.name = ctor_identifier;
    6403       308102 :           CLASSTYPE_NON_AGGREGATE (current) = true;
    6404              :         }
    6405              : 
    6406      2935800 :       if (!TYPE_P (current) && cxx_dialect < cxx20)
    6407              :         {
    6408            3 :           error ("using-declaration for member at non-class scope");
    6409            3 :           return NULL_TREE;
    6410              :         }
    6411              : 
    6412      2935797 :       bool depscope = dependent_scope_p (scope);
    6413              : 
    6414      2935797 :       if (depscope)
    6415              :         /* Leave binfo null.  */;
    6416      2062003 :       else if (TYPE_P (current))
    6417              :         {
    6418      2061986 :           binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
    6419      2061986 :           gcc_checking_assert (b_kind >= bk_not_base);
    6420              : 
    6421      2061986 :           if (b_kind == bk_not_base && any_dependent_bases_p ())
    6422              :             /* Treat as-if dependent.  */
    6423              :             depscope = true;
    6424      2061965 :           else if (lookup.name == ctor_identifier
    6425      2061965 :                    && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
    6426              :             {
    6427           15 :               if (any_dependent_bases_p ())
    6428              :                 depscope = true;
    6429              :               else
    6430              :                 {
    6431           15 :                   error ("%qT is not a direct base of %qT", scope, current);
    6432           15 :                   return NULL_TREE;
    6433              :                 }
    6434              :             }
    6435              : 
    6436      2061971 :           if (b_kind < bk_proper_base)
    6437           52 :             binfo = TYPE_BINFO (scope);
    6438              :         }
    6439              :       else
    6440           17 :         binfo = TYPE_BINFO (scope);
    6441              : 
    6442      4123949 :       dependent_p = (depscope
    6443      2061988 :                      || (IDENTIFIER_CONV_OP_P (lookup.name)
    6444       138495 :                          && dependent_type_p (TREE_TYPE (lookup.name))));
    6445              : 
    6446      2061961 :       if (!dependent_p)
    6447      2061961 :         lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
    6448              :                                       /*want_type=*/false, tf_none);
    6449              : 
    6450              :       /* If the lookup in the base contains a dependent using, this
    6451              :          using is also dependent.  */
    6452      2061961 :       if (!dependent_p && lookup.value && dependent_type_p (scope))
    6453              :         {
    6454           27 :           tree val = lookup.value;
    6455           27 :           if (tree fns = maybe_get_fns (val))
    6456            9 :             val = fns;
    6457           75 :           for (tree f: lkp_range (val))
    6458           27 :             if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
    6459              :               {
    6460              :                 dependent_p = true;
    6461              :                 break;
    6462              :               }
    6463              :         }
    6464              : 
    6465      2935782 :       if (!depscope && b_kind < bk_proper_base)
    6466              :         {
    6467           48 :           if (cxx_dialect >= cxx20 && lookup.value
    6468           34 :               && TREE_CODE (lookup.value) == CONST_DECL)
    6469              :             {
    6470              :               /* Using an unrelated enum; check access here rather
    6471              :                  than separately for class and non-class using.  */
    6472           21 :               perform_or_defer_access_check
    6473           21 :                 (binfo, lookup.value, lookup.value, tf_warning_or_error);
    6474              :               /* And then if this is a copy from handle_using_decl, look
    6475              :                  through to the original enumerator.  */
    6476           21 :               if (CONST_DECL_USING_P (lookup.value))
    6477            9 :                 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
    6478              :             }
    6479           27 :           else if (!TYPE_P (current))
    6480              :             {
    6481            2 :               error ("using-declaration for member at non-class scope");
    6482            2 :               return NULL_TREE;
    6483              :             }
    6484              :           else
    6485              :             {
    6486           25 :               auto_diagnostic_group g;
    6487           25 :               error_not_base_type (scope, current);
    6488           16 :               if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
    6489           28 :                   && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
    6490            3 :                 inform (input_location,
    6491              :                         "did you mean %<using enum %T::%D%>?",
    6492              :                         scope, lookup.name);
    6493           25 :               return NULL_TREE;
    6494           25 :             }
    6495              :         }
    6496              :     }
    6497              : 
    6498              :   /* Did we find anything sane?  */
    6499     15695044 :   if (dependent_p)
    6500              :     ;
    6501     21514343 :   else if (!lookup.value)
    6502              :     {
    6503           57 :       error ("%qD has not been declared in %qD", lookup.name, scope);
    6504           57 :       return NULL_TREE;
    6505              :     }
    6506     21514286 :   else if (TREE_CODE (lookup.value) == TREE_LIST
    6507              :            /* We can (independently) have ambiguous implicit typedefs.  */
    6508     21514286 :            || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
    6509              :     {
    6510            3 :       auto_diagnostic_group d;
    6511            3 :       error ("reference to %qD is ambiguous", lookup.name);
    6512            3 :       print_candidates (input_location,
    6513            3 :                         TREE_CODE (lookup.value) == TREE_LIST
    6514              :                         ? lookup.value : lookup.type);
    6515            3 :       return NULL_TREE;
    6516            3 :     }
    6517     21514283 :   else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    6518              :     {
    6519            6 :       error ("using-declaration may not name namespace %qD", lookup.value);
    6520            6 :       return NULL_TREE;
    6521              :     }
    6522              : 
    6523     22388104 :   if (TYPE_P (current))
    6524              :     {
    6525              :       /* In class scope.  */
    6526              : 
    6527              :       /* Cannot introduce a constructor name.  */
    6528      3116396 :       if (constructor_name_p (lookup.name, current))
    6529              :         {
    6530            3 :           error ("%<%T::%D%> names constructor in %qT",
    6531              :                  scope, lookup.name, current);
    6532            3 :           return NULL_TREE;
    6533              :         }
    6534              : 
    6535      3116393 :       if (lookup.value && BASELINK_P (lookup.value))
    6536              :         /* The binfo from which the functions came does not matter.  */
    6537      1865489 :         lookup.value = BASELINK_FUNCTIONS (lookup.value);
    6538              :     }
    6539              : 
    6540     22388101 :   tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    6541     22388101 :   USING_DECL_SCOPE (using_decl) = scope;
    6542     22388101 :   USING_DECL_DECLS (using_decl) = lookup.value;
    6543     22388101 :   DECL_DEPENDENT_P (using_decl) = dependent_p;
    6544     22388101 :   DECL_CONTEXT (using_decl) = current;
    6545     22388101 :   if (TYPE_P (current) && b_kind == bk_not_base)
    6546      1054507 :     USING_DECL_UNRELATED_P (using_decl) = true;
    6547              : 
    6548              :   return using_decl;
    6549              : }
    6550              : 
    6551              : /* Process "using SCOPE::NAME" in a class scope.  Return the
    6552              :    USING_DECL created.  */
    6553              : 
    6554              : tree
    6555      3116484 : do_class_using_decl (tree scope, tree name)
    6556              : {
    6557      3116484 :   if (name == error_mark_node
    6558      3116481 :       || scope == error_mark_node)
    6559              :     return NULL_TREE;
    6560              : 
    6561      3116478 :   name_lookup lookup (name);
    6562      3116478 :   return lookup_using_decl (scope, lookup);
    6563      3116478 : }
    6564              : 
    6565              : 
    6566              : /* Return the binding for NAME in NS in the current TU.  If NS is
    6567              :    NULL, look in global_namespace.  We will not find declarations
    6568              :    from imports.  Users of this who, having found nothing, push a new
    6569              :    decl must be prepared for that pushing to match an existing decl.  */
    6570              : 
    6571              : tree
    6572     12227097 : get_namespace_binding (tree ns, tree name)
    6573              : {
    6574     12227097 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6575     12227097 :   if (!ns)
    6576     12227097 :     ns = global_namespace;
    6577     12227097 :   gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
    6578     12227097 :   tree ret = NULL_TREE;
    6579              : 
    6580     12227097 :   if (tree *b = find_namespace_slot (ns, name))
    6581              :     {
    6582      5075466 :       ret = *b;
    6583              : 
    6584      5075466 :       if (TREE_CODE (ret) == BINDING_VECTOR)
    6585            4 :         ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
    6586            4 :       if (ret)
    6587      5075466 :         ret = strip_using_decl (MAYBE_STAT_DECL (ret));
    6588              :     }
    6589              : 
    6590     24454194 :   return ret;
    6591     12227097 : }
    6592              : 
    6593              : /* Push internal DECL into the global namespace.  Does not do the
    6594              :    full overload fn handling and does not add it to the list of things
    6595              :    in the namespace.  */
    6596              : 
    6597              : void
    6598      3783388 : set_global_binding (tree decl)
    6599              : {
    6600      3783388 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6601              : 
    6602      3783388 :   tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
    6603              : 
    6604      3783388 :   if (*slot)
    6605              :     /* The user's placed something in the implementor's namespace.  */
    6606            0 :     diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
    6607              : 
    6608              :   /* Force the binding, so compiler internals continue to work.  */
    6609      3783388 :   *slot = decl;
    6610      3783388 : }
    6611              : 
    6612              : /* Set the context of a declaration to scope. Complain if we are not
    6613              :    outside scope.  */
    6614              : 
    6615              : void
    6616       238110 : set_decl_namespace (tree decl, tree scope, bool friendp)
    6617              : {
    6618              :   /* Get rid of namespace aliases.  */
    6619       238110 :   scope = ORIGINAL_NAMESPACE (scope);
    6620              : 
    6621              :   /* It is ok for friends to be qualified in parallel space.  */
    6622       238110 :   if (!friendp && !is_nested_namespace (current_namespace, scope))
    6623            6 :     error ("declaration of %qD not in a namespace surrounding %qD",
    6624              :            decl, scope);
    6625       238110 :   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6626              : 
    6627              :   /* See whether this has been declared in the namespace or inline
    6628              :      children.  */
    6629       238110 :   tree old = NULL_TREE;
    6630       238110 :   {
    6631       238110 :     name_lookup lookup (DECL_NAME (decl),
    6632       238110 :                         LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
    6633       238110 :     if (!lookup.search_qualified (scope, /*usings=*/false))
    6634              :       /* No old declaration at all.  */
    6635           30 :       goto not_found;
    6636       238080 :     old = lookup.value;
    6637       238110 :   }
    6638              : 
    6639              :   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
    6640       238080 :   if (TREE_CODE (old) == TREE_LIST)
    6641              :     {
    6642            9 :     ambiguous:
    6643           15 :       auto_diagnostic_group d;
    6644           15 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6645           15 :       error ("reference to %qD is ambiguous", decl);
    6646           15 :       print_candidates (input_location, old);
    6647           15 :       return;
    6648              :     }
    6649              : 
    6650       238071 :   if (!DECL_DECLARES_FUNCTION_P (decl))
    6651              :     {
    6652              :       /* Don't compare non-function decls with decls_match here, since
    6653              :          it can't check for the correct constness at this
    6654              :          point.  pushdecl will find those errors later.  */
    6655              : 
    6656              :       /* We might have found it in an inline namespace child of SCOPE.  */
    6657        14191 :       if (TREE_CODE (decl) == TREE_CODE (old))
    6658           35 :         DECL_CONTEXT (decl) = DECL_CONTEXT (old);
    6659              : 
    6660        14156 :     found:
    6661              :       /* Writing "N::i" to declare something directly in "N" is invalid.  */
    6662        57347 :       if (CP_DECL_CONTEXT (decl) == current_namespace
    6663        57347 :           && at_namespace_scope_p ())
    6664            3 :         error_at (DECL_SOURCE_LOCATION (decl),
    6665              :                   "explicit qualification in declaration of %qD", decl);
    6666        57347 :       return;
    6667              :     }
    6668              : 
    6669              :   /* Since decl is a function, old should contain a function decl.  */
    6670       223880 :   if (!OVL_P (old))
    6671              :     {
    6672            9 :     not_found:
    6673              :       /* It didn't work, go back to the explicit scope.  */
    6674           45 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6675           45 :       error ("%qD should have been declared inside %qD", decl, scope);
    6676              : 
    6677           45 :       return;
    6678              :     }
    6679              : 
    6680              :   /* We handle these in check_explicit_instantiation_namespace.  */
    6681       223871 :   if (processing_explicit_instantiation)
    6682              :     return;
    6683       223693 :   if (processing_template_decl || processing_specialization)
    6684              :     /* We have not yet called push_template_decl to turn a
    6685              :        FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
    6686              :        match.  But, we'll check later, when we construct the
    6687              :        template.  */
    6688              :     return;
    6689              : 
    6690              :   /* Instantiations or specializations of templates may be declared as
    6691              :      friends in any namespace.  */
    6692       121669 :   if (friendp && DECL_USE_TEMPLATE (decl))
    6693              :     return;
    6694              : 
    6695        43180 :   tree found = NULL_TREE;
    6696        43180 :   bool hidden_p = false;
    6697        43180 :   bool saw_template = false;
    6698              : 
    6699       105278 :   for (lkp_iterator iter (old); iter; ++iter)
    6700              :     {
    6701        62104 :       if (iter.using_p ())
    6702            0 :         continue;
    6703              : 
    6704        62104 :       tree ofn = *iter;
    6705              : 
    6706              :       /* Adjust DECL_CONTEXT first so decls_match will return true
    6707              :          if DECL will match a declaration in an inline namespace.  */
    6708        62104 :       DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
    6709        62104 :       if (decls_match (decl, ofn))
    6710              :         {
    6711        43168 :           if (found)
    6712              :             {
    6713              :               /* We found more than one matching declaration.  This
    6714              :                  can happen if we have two inline namespace children,
    6715              :                  each containing a suitable declaration.  */
    6716            6 :               DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6717            6 :               goto ambiguous;
    6718              :             }
    6719        43162 :           found = ofn;
    6720        43162 :           hidden_p = iter.hidden_p ();
    6721              :         }
    6722        18936 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    6723        18936 :                && TREE_CODE (ofn) == TEMPLATE_DECL)
    6724        62098 :         saw_template = true;
    6725              :     }
    6726              : 
    6727        43174 :   if (!found && friendp && saw_template)
    6728              :     {
    6729              :       /* "[if no non-template match is found,] each remaining function template
    6730              :          is replaced with the specialization chosen by deduction from the
    6731              :          friend declaration or discarded if deduction fails."
    6732              : 
    6733              :          So tell check_explicit_specialization to look for a match.  */
    6734           12 :       SET_DECL_IMPLICIT_INSTANTIATION (decl);
    6735           12 :       DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
    6736           12 :       return;
    6737              :     }
    6738              : 
    6739        43162 :   if (found)
    6740              :     {
    6741        43156 :       if (hidden_p)
    6742              :         {
    6743            6 :           auto_diagnostic_group d;
    6744            6 :           pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    6745              :                    "%qD has not been declared within %qD", decl, scope);
    6746            6 :           inform (DECL_SOURCE_LOCATION (found),
    6747              :                   "only here as a %<friend%>");
    6748            6 :         }
    6749        43156 :       DECL_CONTEXT (decl) = DECL_CONTEXT (found);
    6750        43156 :       goto found;
    6751              :     }
    6752              : 
    6753            6 :   goto not_found;
    6754              : }
    6755              : 
    6756              : /* Return the namespace where the current declaration is declared.  */
    6757              : 
    6758              : tree
    6759   1494605063 : current_decl_namespace (void)
    6760              : {
    6761   1494605063 :   tree result;
    6762              :   /* If we have been pushed into a different namespace, use it.  */
    6763   1494605063 :   if (!vec_safe_is_empty (decl_namespace_list))
    6764     67712650 :     return decl_namespace_list->last ();
    6765              : 
    6766   1426892413 :   if (current_class_type)
    6767    672324412 :     result = decl_namespace_context (current_class_type);
    6768    754568001 :   else if (current_function_decl)
    6769    278426893 :     result = decl_namespace_context (current_function_decl);
    6770              :   else
    6771    476141108 :     result = current_namespace;
    6772              :   return result;
    6773              : }
    6774              : 
    6775              : /* Process any ATTRIBUTES on a namespace definition.  Returns true if
    6776              :    attribute visibility is seen.  */
    6777              : 
    6778              : bool
    6779      6963185 : handle_namespace_attrs (tree ns, tree attributes)
    6780              : {
    6781      6963185 :   tree d;
    6782      6963185 :   bool saw_vis = false;
    6783              : 
    6784      6963185 :   if (attributes == error_mark_node)
    6785              :     return false;
    6786              : 
    6787      9947822 :   for (d = attributes; d; d = TREE_CHAIN (d))
    6788              :     {
    6789      2984637 :       tree name = get_attribute_name (d);
    6790      2984637 :       tree args = TREE_VALUE (d);
    6791              : 
    6792      2984637 :       if (is_attribute_p ("visibility", name)
    6793      2984637 :           && is_attribute_namespace_p ("gnu", d))
    6794              :         {
    6795              :           /* attribute visibility is a property of the syntactic block
    6796              :              rather than the namespace as a whole, so we don't touch the
    6797              :              NAMESPACE_DECL at all.  */
    6798      2927812 :           tree x = args ? TREE_VALUE (args) : NULL_TREE;
    6799      2927812 :           if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
    6800              :             {
    6801            0 :               warning (OPT_Wattributes,
    6802              :                        "%qD attribute requires a single NTBS argument",
    6803              :                        name);
    6804            0 :               continue;
    6805              :             }
    6806              : 
    6807      2927812 :           if (!TREE_PUBLIC (ns))
    6808            0 :             warning (OPT_Wattributes,
    6809              :                      "%qD attribute is meaningless since members of the "
    6810              :                      "anonymous namespace get local symbols", name);
    6811              : 
    6812      2927812 :           push_visibility (TREE_STRING_POINTER (x), 1);
    6813      2927812 :           saw_vis = true;
    6814              :         }
    6815        56825 :       else if (is_attribute_p ("abi_tag", name)
    6816        56825 :                && is_attribute_namespace_p ("gnu", d))
    6817              :         {
    6818        46988 :           if (!DECL_NAME (ns))
    6819              :             {
    6820            3 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6821              :                        "namespace", name);
    6822            3 :               continue;
    6823              :             }
    6824        46985 :           if (!DECL_NAMESPACE_INLINE_P (ns))
    6825              :             {
    6826            0 :               warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
    6827              :                        "namespace", name);
    6828            0 :               continue;
    6829              :             }
    6830        46985 :           if (!args)
    6831              :             {
    6832           18 :               tree dn = DECL_NAME (ns);
    6833           18 :               args = build_string (IDENTIFIER_LENGTH (dn) + 1,
    6834           18 :                                    IDENTIFIER_POINTER (dn));
    6835           18 :               TREE_TYPE (args) = char_array_type_node;
    6836           18 :               args = fix_string_type (args);
    6837           18 :               args = build_tree_list (NULL_TREE, args);
    6838              :             }
    6839        46985 :           if (check_abi_tag_args (args, name))
    6840        46985 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6841        46985 :                                               DECL_ATTRIBUTES (ns));
    6842              :         }
    6843         9837 :       else if (is_attribute_p ("deprecated", name)
    6844         9837 :                && is_attribute_namespace_p ("", d))
    6845              :         {
    6846         9721 :           if (!DECL_NAME (ns))
    6847              :             {
    6848            7 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6849              :                        "namespace", name);
    6850            7 :               continue;
    6851              :             }
    6852        19361 :           if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
    6853              :             {
    6854            0 :               error ("deprecated message is not a string");
    6855            0 :               continue;
    6856              :             }
    6857         9714 :           TREE_DEPRECATED (ns) = 1;
    6858         9714 :           if (args)
    6859         9647 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6860         9647 :                                               DECL_ATTRIBUTES (ns));
    6861              :         }
    6862          116 :       else if (annotation_p (d))
    6863              :         {
    6864           20 :           const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (d));
    6865           20 :           bool no_add_attrs = false;
    6866           20 :           as->handler (&ns, name, args, 0, &no_add_attrs);
    6867           20 :           if (!no_add_attrs)
    6868           18 :             DECL_ATTRIBUTES (ns) = tree_cons (TREE_PURPOSE (d), args,
    6869           18 :                                               DECL_ATTRIBUTES (ns));
    6870              :         }
    6871           96 :       else if (!attribute_ignored_p (d))
    6872              :         {
    6873           81 :           warning (OPT_Wattributes, "%qD attribute directive ignored",
    6874              :                    name);
    6875           81 :           continue;
    6876              :         }
    6877              :     }
    6878              : 
    6879              :   return saw_vis;
    6880              : }
    6881              : 
    6882              : /* Temporarily set the namespace for the current declaration.  */
    6883              : 
    6884              : void
    6885     83223225 : push_decl_namespace (tree decl)
    6886              : {
    6887     83223225 :   if (TREE_CODE (decl) != NAMESPACE_DECL)
    6888            0 :     decl = decl_namespace_context (decl);
    6889     83223225 :   vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
    6890     83223225 : }
    6891              : 
    6892              : /* [namespace.memdef]/2 */
    6893              : 
    6894              : void
    6895     83223222 : pop_decl_namespace (void)
    6896              : {
    6897     83223222 :   decl_namespace_list->pop ();
    6898     83223222 : }
    6899              : 
    6900              : /* Process a namespace-alias declaration.  */
    6901              : 
    6902              : void
    6903       121828 : do_namespace_alias (location_t loc, tree alias, tree name_space)
    6904              : {
    6905       121828 :   if (name_space == error_mark_node)
    6906              :     return;
    6907              : 
    6908       121819 :   if (TREE_CODE (name_space) == NAMESPACE_DECL)
    6909       121818 :     name_space = ORIGINAL_NAMESPACE (name_space);
    6910              :   else
    6911            1 :     gcc_assert (TREE_CODE (name_space) == SPLICE_EXPR);
    6912              : 
    6913              :   /* Build the alias.  */
    6914       121819 :   alias = build_lang_decl_loc (loc, NAMESPACE_DECL, alias, void_type_node);
    6915       121819 :   DECL_NAMESPACE_ALIAS (alias) = name_space;
    6916       121819 :   DECL_EXTERNAL (alias) = 1;
    6917       121819 :   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
    6918       121819 :   TREE_PUBLIC (alias) = TREE_PUBLIC (CP_DECL_CONTEXT (alias));
    6919              : 
    6920       121819 :   alias = pushdecl (alias);
    6921              : 
    6922       243638 :   if (!DECL_P (alias) || !DECL_NAMESPACE_ALIAS (alias))
    6923              :     return;
    6924              : 
    6925       121816 :   set_originating_module (alias);
    6926       121816 :   check_module_decl_linkage (alias);
    6927              : 
    6928              :   /* Emit debug info for namespace alias.  */
    6929       121816 :   if (!building_stmt_list_p ())
    6930        12236 :     (*debug_hooks->early_global_decl) (alias);
    6931              : }
    6932              : 
    6933              : /* Like pushdecl, only it places DECL in the current namespace,
    6934              :    if appropriate.  */
    6935              : 
    6936              : tree
    6937     53318938 : pushdecl_namespace_level (tree decl, bool hiding)
    6938              : {
    6939     53318938 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6940     53318938 :   return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
    6941     53318938 :                                  hiding);
    6942     53318938 : }
    6943              : 
    6944              : /* Wrapper around push_local_binding to push the bindings for
    6945              :    a non-member USING_DECL with NAME and VALUE.  LOOKUP, if non-null,
    6946              :    is the result of name lookup during template parsing.  */
    6947              : 
    6948              : static void
    6949     13047771 : push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
    6950              : {
    6951     13047771 :   tree type = NULL_TREE;
    6952              : 
    6953     13047771 :   cxx_binding *binding = find_local_binding (current_binding_level, name);
    6954     13047771 :   if (binding)
    6955              :     {
    6956           84 :       value = binding->value;
    6957           84 :       type = binding->type;
    6958              :     }
    6959              : 
    6960     13047771 :   if (lookup)
    6961     12787358 :     do_nonmember_using_decl (*lookup, true, true, &value, &type);
    6962              : 
    6963     13047771 :   if (!value)
    6964              :     ;
    6965     13047771 :   else if (binding && value == binding->value)
    6966              :     /* Redeclaration of this USING_DECL.  */;
    6967           48 :   else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
    6968              :     {
    6969              :       /* We already have this binding, so replace it.  */
    6970           36 :       update_local_overload (IDENTIFIER_BINDING (name), value);
    6971           36 :       IDENTIFIER_BINDING (name)->value = value;
    6972              :     }
    6973              :   else
    6974              :     /* Install the new binding.  */
    6975     13047699 :     push_local_binding (name, value, /*using=*/true);
    6976              : 
    6977     13047771 :   if (!type)
    6978              :     ;
    6979           21 :   else if (binding && type == binding->type)
    6980              :     ;
    6981              :   else
    6982              :     {
    6983           15 :       push_local_binding (name, type, /*using=*/true);
    6984           15 :       set_identifier_type_value (name, type);
    6985              :     }
    6986     13047771 : }
    6987              : 
    6988              : /* Overload for push_using_decl_bindings that doesn't take a name_lookup.  */
    6989              : 
    6990              : void
    6991       260413 : push_using_decl_bindings (tree name, tree value)
    6992              : {
    6993       260413 :   push_using_decl_bindings (nullptr, name, value);
    6994       260413 : }
    6995              : 
    6996              : /* Process a using declaration in non-class scope.  */
    6997              : 
    6998              : void
    6999     19271752 : finish_nonmember_using_decl (tree scope, tree name)
    7000              : {
    7001     19271752 :   gcc_checking_assert (current_binding_level->kind != sk_class);
    7002              : 
    7003     19271752 :   if (scope == error_mark_node || name == error_mark_node)
    7004           44 :     return;
    7005              : 
    7006     19271752 :   name_lookup lookup (name);
    7007              : 
    7008     19271752 :   tree using_decl = lookup_using_decl (scope, lookup);
    7009     19271752 :   if (!using_decl)
    7010           44 :     return;
    7011              : 
    7012              :   /* Emit debug info.  */
    7013     19271708 :   if (!processing_template_decl)
    7014      6504665 :     cp_emit_debug_info_for_using (lookup.value,
    7015      6504665 :                                   current_binding_level->this_entity);
    7016              : 
    7017     19271708 :   if (current_binding_level->kind == sk_namespace)
    7018              :     {
    7019      6484350 :       tree *slot = find_namespace_slot (current_namespace, name, true);
    7020      6484350 :       tree *mslot = get_fixed_binding_slot (slot, name,
    7021              :                                             BINDING_SLOT_CURRENT, true);
    7022      6484350 :       bool failed = false;
    7023              : 
    7024      6484350 :       if (mslot != slot)
    7025              :         {
    7026              :           /* A module vector.  I presume the binding list is going to
    7027              :              be sparser than the import bitmap.  Hence iterate over
    7028              :              the former checking for bits set in the bitmap.  */
    7029         4221 :           bitmap imports = get_import_bitmap ();
    7030         4221 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    7031              : 
    7032              :           /* Scan the imported bindings.  */
    7033         4221 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    7034         4221 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    7035              :             {
    7036         4221 :               ix--;
    7037         4221 :               cluster++;
    7038              :             }
    7039              : 
    7040              :           /* Do this in forward order, so we load modules in an order
    7041              :              the user expects.  */
    7042         8442 :           for (; ix--; cluster++)
    7043        12663 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    7044              :               {
    7045              :                 /* Are we importing this module?  */
    7046         8442 :                 if (unsigned base = cluster->indices[jx].base)
    7047         4099 :                   if (unsigned span = cluster->indices[jx].span)
    7048         4099 :                     do
    7049         4099 :                       if (bitmap_bit_p (imports, base))
    7050         3544 :                         goto found;
    7051          555 :                     while (++base, --span);
    7052         4898 :                 continue;
    7053              : 
    7054         3544 :               found:;
    7055              :                 /* Is it loaded?  */
    7056         3544 :                 if (cluster->slots[jx].is_lazy ())
    7057              :                   {
    7058            0 :                     gcc_assert (cluster->indices[jx].span == 1);
    7059            0 :                     lazy_load_binding (cluster->indices[jx].base,
    7060              :                                        scope, name, &cluster->slots[jx]);
    7061              :                   }
    7062              : 
    7063         3544 :                 tree value = cluster->slots[jx];
    7064         3544 :                 if (!value)
    7065              :                   /* Load errors could mean there's nothing here.  */
    7066            0 :                   continue;
    7067              : 
    7068              :                 /* Extract what we can see from here.  If there's no
    7069              :                    stat_hack, then everything was exported.  */
    7070         3544 :                 tree type = NULL_TREE;
    7071              : 
    7072              :                 /* If no stat hack, everything is visible.  */
    7073         3544 :                 if (STAT_HACK_P (value))
    7074              :                   {
    7075         3526 :                     if (STAT_TYPE_VISIBLE_P (value))
    7076            0 :                       type = STAT_TYPE (value);
    7077         3526 :                     value = STAT_VISIBLE (value);
    7078              :                   }
    7079              : 
    7080         3544 :                 if (do_nonmember_using_decl (lookup, false, false,
    7081              :                                              &value, &type))
    7082              :                   {
    7083            0 :                     failed = true;
    7084            0 :                     break;
    7085              :                   }
    7086         4898 :               }
    7087              :         }
    7088              : 
    7089         4221 :       if (!failed)
    7090              :         {
    7091              :           /* Now do the current slot.  */
    7092      6484350 :           tree value = MAYBE_STAT_DECL (*mslot);
    7093      6484350 :           tree type = MAYBE_STAT_TYPE (*mslot);
    7094              : 
    7095      6484350 :           do_nonmember_using_decl (lookup, false, true, &value, &type);
    7096              : 
    7097              :           // FIXME: Partition mergeableness?
    7098      6484350 :           if (STAT_HACK_P (*mslot))
    7099              :             {
    7100         1223 :               STAT_DECL (*mslot) = value;
    7101         1223 :               STAT_TYPE (*mslot) = type;
    7102              :             }
    7103      6483127 :           else if (type)
    7104           22 :             *mslot = stat_hack (value, type);
    7105              :           else
    7106      6483105 :             *mslot = value;
    7107              :         }
    7108              :     }
    7109              :   else
    7110              :     {
    7111     12787358 :       add_decl_expr (using_decl);
    7112     12787358 :       if (DECL_DEPENDENT_P (using_decl))
    7113            7 :         lookup.value = using_decl;
    7114     12787358 :       push_using_decl_bindings (&lookup, name, NULL_TREE);
    7115              :     }
    7116     19271752 : }
    7117              : 
    7118              : /* Return the declarations that are members of the namespace NS.  */
    7119              : 
    7120              : tree
    7121           18 : cp_namespace_decls (tree ns)
    7122              : {
    7123           18 :   return NAMESPACE_LEVEL (ns)->names;
    7124              : }
    7125              : 
    7126              : /* Given a lookup that returned VAL, use FLAGS to decide if we want to
    7127              :    ignore it or not.  Subroutine of lookup_name_1 and lookup_type_scope.  */
    7128              : 
    7129              : static bool
    7130   3566242201 : qualify_lookup (tree val, LOOK_want want)
    7131              : {
    7132   3566242201 :   if (val == NULL_TREE)
    7133              :     return false;
    7134              : 
    7135   3566242201 :   if (bool (want & LOOK_want::TYPE))
    7136              :     {
    7137     43463036 :       tree target_val = strip_using_decl (val);
    7138              : 
    7139     43463036 :       if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
    7140              :         return true;
    7141              :     }
    7142              : 
    7143   3523081472 :   if (bool (want & LOOK_want::TYPE_NAMESPACE))
    7144       512452 :     return TREE_CODE (val) == NAMESPACE_DECL;
    7145              : 
    7146              :   return true;
    7147              : }
    7148              : 
    7149              : /* Is there a "using namespace std;" directive within USINGS?  */
    7150              : 
    7151              : static bool
    7152         5378 : using_directives_contain_std_p (vec<tree, va_gc> *usings)
    7153              : {
    7154         5378 :   if (!usings)
    7155              :     return false;
    7156              : 
    7157           46 :   for (unsigned ix = usings->length (); ix--;)
    7158           31 :     if (strip_using_decl ((*usings)[ix]) == std_node)
    7159              :       return true;
    7160              : 
    7161              :   return false;
    7162              : }
    7163              : 
    7164              : /* Is there a "using namespace std;" directive within the current
    7165              :    namespace (or its ancestors)?
    7166              :    Compare with name_lookup::search_unqualified.  */
    7167              : 
    7168              : static bool
    7169         1718 : has_using_namespace_std_directive_p ()
    7170              : {
    7171         1718 :   for (cp_binding_level *level = current_binding_level;
    7172         7080 :        level;
    7173         5362 :        level = level->level_chain)
    7174         5378 :     if (using_directives_contain_std_p (level->using_directives))
    7175              :       return true;
    7176              : 
    7177              :   return false;
    7178              : }
    7179              : 
    7180              : /* Subclass of deferred_diagnostic, for issuing a note when
    7181              :    --param cxx-max-namespaces-for-diagnostic-help is reached.
    7182              : 
    7183              :    The note should be issued after the error, but before any other
    7184              :    deferred diagnostics.  This is handled by decorating a wrapped
    7185              :    deferred_diagnostic, and emitting a note before that wrapped note is
    7186              :    deleted.  */
    7187              : 
    7188              : class namespace_limit_reached : public deferred_diagnostic
    7189              : {
    7190              :  public:
    7191            3 :   namespace_limit_reached (location_t loc, unsigned limit, tree name,
    7192              :                            std::unique_ptr<deferred_diagnostic> wrapped)
    7193            3 :   : deferred_diagnostic (loc),
    7194            3 :     m_limit (limit), m_name (name),
    7195            3 :     m_wrapped (std::move (wrapped))
    7196              :   {
    7197              :   }
    7198              : 
    7199            6 :   ~namespace_limit_reached ()
    7200            3 :   {
    7201              :     /* Unconditionally warn that the search was truncated.  */
    7202            3 :     inform (get_location (),
    7203              :             "maximum limit of %d namespaces searched for %qE",
    7204              :             m_limit, m_name);
    7205              :     /* m_wrapped will be implicitly deleted after this, emitting any followup
    7206              :        diagnostic after the above note.  */
    7207            6 :   }
    7208              : 
    7209              :  private:
    7210              :   unsigned m_limit;
    7211              :   tree m_name;
    7212              :   std::unique_ptr<deferred_diagnostic> m_wrapped;
    7213              : };
    7214              : 
    7215              : /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
    7216              :    Emit a note showing the location of the declaration of the suggestion.  */
    7217              : 
    7218              : class show_candidate_location : public deferred_diagnostic
    7219              : {
    7220              :  public:
    7221          110 :   show_candidate_location (location_t loc, tree candidate)
    7222          110 :   : deferred_diagnostic (loc),
    7223          110 :     m_candidate (candidate)
    7224              :   {
    7225              :   }
    7226              : 
    7227          220 :   ~show_candidate_location ()
    7228          110 :   {
    7229          110 :     inform (location_of (m_candidate), "%qE declared here", m_candidate);
    7230          220 :   }
    7231              : 
    7232              :  private:
    7233              :   tree m_candidate;
    7234              : };
    7235              : 
    7236              : /* Subclass of deferred_diagnostic, for use when there are multiple candidates
    7237              :    to be suggested by suggest_alternatives_for.
    7238              : 
    7239              :    Emit a series of notes showing the various suggestions.  */
    7240              : 
    7241              : class suggest_alternatives : public deferred_diagnostic
    7242              : {
    7243              :  public:
    7244           17 :   suggest_alternatives (location_t loc, vec<tree> candidates)
    7245           17 :   : deferred_diagnostic (loc),
    7246           17 :     m_candidates (candidates)
    7247              :   {
    7248              :   }
    7249              : 
    7250           34 :   ~suggest_alternatives ()
    7251           17 :   {
    7252           17 :     if (m_candidates.length ())
    7253              :       {
    7254           17 :         inform_n (get_location (), m_candidates.length (),
    7255              :                   "suggested alternative:",
    7256              :                   "suggested alternatives:");
    7257          108 :         for (unsigned ix = 0; ix != m_candidates.length (); ix++)
    7258              :           {
    7259           37 :             tree val = m_candidates[ix];
    7260              : 
    7261           37 :             inform (location_of (val), "  %qE", val);
    7262              :           }
    7263              :       }
    7264           17 :     m_candidates.release ();
    7265           34 :   }
    7266              : 
    7267              :  private:
    7268              :   vec<tree> m_candidates;
    7269              : };
    7270              : 
    7271              : /* A class for encapsulating the result of a search across
    7272              :    multiple namespaces (and scoped enums within them) for an
    7273              :    unrecognized name seen at a given source location.  */
    7274              : 
    7275              : class namespace_hints
    7276              : {
    7277              :  public:
    7278              :   namespace_hints (location_t loc, tree name);
    7279              : 
    7280              :   name_hint convert_candidates_to_name_hint ();
    7281              :   name_hint maybe_decorate_with_limit (name_hint);
    7282              : 
    7283              :  private:
    7284              :   void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
    7285              : 
    7286              :   location_t m_loc;
    7287              :   tree m_name;
    7288              :   vec<tree> m_candidates;
    7289              : 
    7290              :   /* Value of "--param cxx-max-namespaces-for-diagnostic-help".  */
    7291              :   unsigned m_limit;
    7292              : 
    7293              :   /* Was the limit reached?  */
    7294              :   bool m_limited;
    7295              : };
    7296              : 
    7297              : /* Constructor for namespace_hints.  Search namespaces and scoped enums,
    7298              :    looking for an exact match for unrecognized NAME seen at LOC.  */
    7299              : 
    7300         1883 : namespace_hints::namespace_hints (location_t loc, tree name)
    7301         1883 : : m_loc(loc), m_name (name)
    7302              : {
    7303         1883 :   auto_vec<tree> worklist;
    7304              : 
    7305         1883 :   m_candidates = vNULL;
    7306         1883 :   m_limited = false;
    7307         1883 :   m_limit = param_cxx_max_namespaces_for_diagnostic_help;
    7308              : 
    7309              :   /* Breadth-first search of namespaces.  Up to limit namespaces
    7310              :      searched (limit zero == unlimited).  */
    7311         1883 :   worklist.safe_push (global_namespace);
    7312        16438 :   for (unsigned ix = 0; ix != worklist.length (); ix++)
    7313              :     {
    7314         6336 :       tree ns = worklist[ix];
    7315         6336 :       name_lookup lookup (name);
    7316              : 
    7317         6336 :       if (lookup.search_qualified (ns, false))
    7318          132 :         m_candidates.safe_push (lookup.value);
    7319              : 
    7320         6336 :       if (!m_limited)
    7321              :         {
    7322              :           /* Look for child namespaces.  We have to do this
    7323              :              indirectly because they are chained in reverse order,
    7324              :              which is confusing to the user.  */
    7325         6324 :           auto_vec<tree> children;
    7326              : 
    7327         6324 :           for (tree decl = NAMESPACE_LEVEL (ns)->names;
    7328      5060474 :                decl; decl = TREE_CHAIN (decl))
    7329              :             {
    7330      5054150 :               if (TREE_CODE (decl) == NAMESPACE_DECL
    7331         4589 :                   && !DECL_NAMESPACE_ALIAS (decl)
    7332      5058732 :                   && !DECL_NAMESPACE_INLINE_P (decl))
    7333         4462 :                 children.safe_push (decl);
    7334              : 
    7335              :               /* Look for exact matches for NAME within scoped enums.
    7336              :                  These aren't added to the worklist, and so don't count
    7337              :                  against the search limit.  */
    7338      5054150 :               if (TREE_CODE (decl) == TYPE_DECL)
    7339              :                 {
    7340        54513 :                   tree type = TREE_TYPE (decl);
    7341        54513 :                   if (SCOPED_ENUM_P (type))
    7342         1435 :                     maybe_add_candidate_for_scoped_enum (type, name);
    7343              :                 }
    7344              :             }
    7345              : 
    7346        17101 :           while (!m_limited && !children.is_empty ())
    7347              :             {
    7348         8912 :               if (worklist.length () == m_limit)
    7349            3 :                 m_limited = true;
    7350              :               else
    7351         4453 :                 worklist.safe_push (children.pop ());
    7352              :             }
    7353         6324 :         }
    7354         6336 :     }
    7355         1883 : }
    7356              : 
    7357              : /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
    7358              :    for m_name, an IDENTIFIER_NODE for which name lookup failed.
    7359              : 
    7360              :    If m_candidates is non-empty, use it to generate a suggestion and/or
    7361              :    a deferred diagnostic that lists the possible candidate(s).
    7362              : */
    7363              : 
    7364              : name_hint
    7365         1883 : namespace_hints::convert_candidates_to_name_hint ()
    7366              : {
    7367              :   /* How many candidates do we have?  */
    7368              : 
    7369              :   /* If we have just one candidate, issue a name_hint with it as a suggestion
    7370              :      (so that consumers are able to suggest it within the error message and emit
    7371              :      it as a fix-it hint), and with a note showing the candidate's location.  */
    7372         1883 :   if (m_candidates.length () == 1)
    7373              :     {
    7374          110 :       tree candidate = m_candidates[0];
    7375              :       /* Clean up CANDIDATES.  */
    7376          110 :       m_candidates.release ();
    7377          110 :       return name_hint (expr_to_string (candidate),
    7378          110 :                         std::make_unique<show_candidate_location> (m_loc,
    7379          110 :                                                                    candidate));
    7380              :     }
    7381         1773 :   else if (m_candidates.length () > 1)
    7382              :     /* If we have more than one candidate, issue a name_hint without a single
    7383              :        "suggestion", but with a deferred diagnostic that lists the
    7384              :        various candidates.  This takes ownership of m_candidates.  */
    7385           17 :     return name_hint (NULL,
    7386           17 :                       std::make_unique<suggest_alternatives> (m_loc,
    7387           17 :                                                               m_candidates));
    7388              : 
    7389              :   /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary.  */
    7390         1756 :   gcc_assert (m_candidates.length () == 0);
    7391         1756 :   gcc_assert (m_candidates == vNULL);
    7392              : 
    7393         1756 :   return name_hint ();
    7394              : }
    7395              : 
    7396              : /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
    7397              :    then we want to emit a note about after the error, but before
    7398              :    any other deferred diagnostics.
    7399              : 
    7400              :    Handle this by figuring out what hint is needed, then optionally
    7401              :    decorating HINT with a namespace_limit_reached wrapper.  */
    7402              : 
    7403              : name_hint
    7404         1883 : namespace_hints::maybe_decorate_with_limit (name_hint hint)
    7405              : {
    7406         1883 :   if (m_limited)
    7407            3 :     return name_hint
    7408              :       (hint.suggestion (),
    7409            3 :        std::make_unique<namespace_limit_reached> (m_loc, m_limit,
    7410            3 :                                                   m_name,
    7411            6 :                                                   hint.take_deferred ()));
    7412              :   else
    7413         1880 :     return hint;
    7414              : }
    7415              : 
    7416              : /* Look inside SCOPED_ENUM for exact matches for NAME.
    7417              :    If one is found, add its CONST_DECL to m_candidates.  */
    7418              : 
    7419              : void
    7420         1435 : namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
    7421              :                                                       tree name)
    7422              : {
    7423         1435 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    7424              : 
    7425         2391 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    7426              :     {
    7427          971 :       tree id = TREE_PURPOSE (iter);
    7428          971 :       if (id == name)
    7429              :         {
    7430           15 :           m_candidates.safe_push (TREE_VALUE (iter));
    7431           15 :           return;
    7432              :         }
    7433              :     }
    7434              : }
    7435              : 
    7436              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    7437              :    name lookup failed.
    7438              : 
    7439              :    Search through all available namespaces and any scoped enums within them
    7440              :    and generate a suggestion and/or a deferred diagnostic that lists possible
    7441              :    candidate(s).
    7442              : 
    7443              :    If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
    7444              :    look for near-matches and suggest the best near-match, if there is one.
    7445              : 
    7446              :    If nothing is found, then an empty name_hint is returned.  */
    7447              : 
    7448              : name_hint
    7449         1824 : suggest_alternatives_for (location_t location, tree name,
    7450              :                           bool suggest_misspellings)
    7451              : {
    7452              :   /* First, search for exact matches in other namespaces.  */
    7453         1824 :   namespace_hints ns_hints (location, name);
    7454         1824 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    7455              : 
    7456              :   /* Otherwise, try other approaches.  */
    7457         1824 :   if (!result)
    7458         1718 :     result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
    7459              : 
    7460         1824 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    7461         1824 : }
    7462              : 
    7463              : /* The second half of suggest_alternatives_for, for when no exact matches
    7464              :    were found in other namespaces.  */
    7465              : 
    7466              : static name_hint
    7467         1718 : suggest_alternatives_for_1 (location_t location, tree name,
    7468              :                             bool suggest_misspellings)
    7469              : {
    7470              :   /* No candidates were found in the available namespaces.  */
    7471              : 
    7472              :   /* If there's a "using namespace std;" active, and this
    7473              :      is one of the most common "std::" names, then it's probably a
    7474              :      missing #include.  */
    7475         1718 :   if (has_using_namespace_std_directive_p ())
    7476              :     {
    7477           16 :       name_hint hint = maybe_suggest_missing_std_header (location, name);
    7478           16 :       if (hint)
    7479              :         return hint;
    7480            0 :     }
    7481              : 
    7482              :   /* Look for exact matches for builtin defines that would have been
    7483              :      defined if the user had passed a command-line option (e.g. -fopenmp
    7484              :      for "_OPENMP").  */
    7485         1702 :   diagnostics::option_id option_id
    7486         1702 :     = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
    7487         1702 :   if (option_id.m_idx > 0)
    7488            6 :     return name_hint
    7489              :       (nullptr,
    7490            6 :        std::make_unique<suggest_missing_option> (location,
    7491           12 :                                                  IDENTIFIER_POINTER (name),
    7492            6 :                                                  option_id));
    7493              : 
    7494              :   /* Otherwise, consider misspellings.  */
    7495         1696 :   if (!suggest_misspellings)
    7496            0 :     return name_hint ();
    7497              : 
    7498         1696 :   return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
    7499              : }
    7500              : 
    7501              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    7502              :    name lookup failed.
    7503              : 
    7504              :    Search through all available namespaces and generate a suggestion and/or
    7505              :    a deferred diagnostic that lists possible candidate(s).
    7506              : 
    7507              :    This is similiar to suggest_alternatives_for, but doesn't fallback to
    7508              :    the other approaches used by that function.  */
    7509              : 
    7510              : name_hint
    7511           59 : suggest_alternatives_in_other_namespaces (location_t location, tree name)
    7512              : {
    7513           59 :   namespace_hints ns_hints (location, name);
    7514              : 
    7515           59 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    7516              : 
    7517           59 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    7518           59 : }
    7519              : 
    7520              : /* A well-known name within the C++ standard library, returned by
    7521              :    get_std_name_hint.
    7522              : 
    7523              :    The gperf-generated file contains the definition of the class
    7524              :    "std_name_hint_lookup" with a static member function which
    7525              :    returns the pointer to a structure "std_name_hint" which
    7526              :    is also defined in that file.  */
    7527              : 
    7528              : #include "std-name-hint.h"
    7529              : 
    7530              : /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
    7531              :    for some of the most common names within "std::".
    7532              :    Given non-NULL NAME, return the std_name_hint for it, or NULL.  */
    7533              : 
    7534              : static const std_name_hint *
    7535          179 : get_std_name_hint (const char *name)
    7536              : {
    7537          179 :   return std_name_hint_lookup::find(name, strlen(name));
    7538              : }
    7539              : 
    7540              : /* Describe DIALECT.  */
    7541              : 
    7542              : const char *
    7543         4651 : get_cxx_dialect_name (enum cxx_dialect dialect)
    7544              : {
    7545         4651 :   switch (dialect)
    7546              :     {
    7547            0 :     default:
    7548            0 :       gcc_unreachable ();
    7549              :     case cxx98:
    7550              :       return "C++98";
    7551            4 :     case cxx11:
    7552            4 :       return "C++11";
    7553            4 :     case cxx14:
    7554            4 :       return "C++14";
    7555         1460 :     case cxx17:
    7556         1460 :       return "C++17";
    7557         1588 :     case cxx20:
    7558         1588 :       return "C++20";
    7559           47 :     case cxx23:
    7560           47 :       return "C++23";
    7561         1548 :     case cxx26:
    7562         1548 :       return "C++26";
    7563              :     }
    7564              : }
    7565              : 
    7566              : /* Subclass of deferred_diagnostic for use for names in the "std" namespace
    7567              :    that weren't recognized, but for which we know which header it ought to be
    7568              :    in.
    7569              : 
    7570              :    Emit a note either suggesting the header to be included, or noting that
    7571              :    the current dialect is too early for the given name.  */
    7572              : 
    7573              : class missing_std_header : public deferred_diagnostic
    7574              : {
    7575              :  public:
    7576          152 :   missing_std_header (location_t loc,
    7577              :                       const char *name_str,
    7578              :                       const std_name_hint *header_hint)
    7579          152 :   : deferred_diagnostic (loc),
    7580          152 :     m_name_str (name_str),
    7581          152 :     m_header_hint (header_hint)
    7582              :   {}
    7583          304 :   ~missing_std_header ()
    7584          152 :   {
    7585          152 :     gcc_rich_location richloc (get_location ());
    7586          152 :     if (cxx_dialect >= m_header_hint->min_dialect)
    7587              :       {
    7588          144 :         const char *header = m_header_hint->header;
    7589          144 :         maybe_add_include_fixit (&richloc, header, true);
    7590          144 :         inform (&richloc,
    7591              :                 "%<std::%s%> is defined in header %qs;"
    7592              :                 " this is probably fixable by adding %<#include %s%>",
    7593              :                 m_name_str, header, header);
    7594              :       }
    7595              :     else
    7596            8 :       inform (&richloc,
    7597              :               "%<std::%s%> is only available from %s onwards",
    7598              :               m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
    7599          304 :   }
    7600              : 
    7601              : private:
    7602              :   const char *m_name_str;
    7603              :   const std_name_hint *m_header_hint;
    7604              : };
    7605              : 
    7606              : /* Attempt to generate a name_hint that suggests pertinent header files
    7607              :    for NAME at LOCATION, for common names within the "std" namespace,
    7608              :    or an empty name_hint if this isn't applicable.  */
    7609              : 
    7610              : static name_hint
    7611          179 : maybe_suggest_missing_std_header (location_t location, tree name)
    7612              : {
    7613          179 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    7614              : 
    7615          179 :   const char *name_str = IDENTIFIER_POINTER (name);
    7616          179 :   const std_name_hint *header_hint = get_std_name_hint (name_str);
    7617          179 :   if (!header_hint)
    7618           27 :     return name_hint ();
    7619              : 
    7620          152 :   return name_hint (nullptr,
    7621          152 :                     std::make_unique<missing_std_header> (location, name_str,
    7622          152 :                                                           header_hint));
    7623              : }
    7624              : 
    7625              : /* Attempt to generate a name_hint that suggests a missing header file
    7626              :    for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
    7627              :    applicable.  */
    7628              : 
    7629              : name_hint
    7630          487 : maybe_suggest_missing_header (location_t location, tree name, tree scope)
    7631              : {
    7632          487 :   if (scope == NULL_TREE)
    7633            0 :     return name_hint ();
    7634          487 :   if (TREE_CODE (scope) != NAMESPACE_DECL)
    7635           33 :     return name_hint ();
    7636              :   /* We only offer suggestions for the "std" namespace.  */
    7637          454 :   if (scope != std_node)
    7638          291 :     return name_hint ();
    7639          163 :   return maybe_suggest_missing_std_header (location, name);
    7640              : }
    7641              : 
    7642              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
    7643              :    lookup failed within the explicitly provided SCOPE.
    7644              : 
    7645              :    Suggest the best meaningful candidates (if any), otherwise
    7646              :    an empty name_hint is returned.  */
    7647              : 
    7648              : name_hint
    7649          320 : suggest_alternative_in_explicit_scope (location_t location, tree name,
    7650              :                                        tree scope)
    7651              : {
    7652              :   /* Something went very wrong; don't suggest anything.  */
    7653          320 :   if (name == error_mark_node)
    7654            0 :     return name_hint ();
    7655              : 
    7656          320 :   if (TREE_CODE (scope) != NAMESPACE_DECL)
    7657           25 :     return name_hint ();
    7658              : 
    7659              :   /* Resolve any namespace aliases.  */
    7660          295 :   scope = ORIGINAL_NAMESPACE (scope);
    7661              : 
    7662          295 :   name_hint hint = maybe_suggest_missing_header (location, name, scope);
    7663          295 :   if (hint)
    7664          121 :     return hint;
    7665              : 
    7666          174 :   cp_binding_level *level = NAMESPACE_LEVEL (scope);
    7667              : 
    7668          174 :   best_match <tree, const char *> bm (name);
    7669          174 :   consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
    7670              : 
    7671              :   /* See if we have a good suggesion for the user.  */
    7672          174 :   const char *fuzzy_name = bm.get_best_meaningful_candidate ();
    7673          174 :   if (fuzzy_name)
    7674           43 :     return name_hint (fuzzy_name, NULL);
    7675              : 
    7676          131 :   return name_hint ();
    7677          295 : }
    7678              : 
    7679              : /* Given NAME, look within SCOPED_ENUM for possible spell-correction
    7680              :    candidates.  */
    7681              : 
    7682              : name_hint
    7683           15 : suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
    7684              : {
    7685           15 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    7686              : 
    7687           15 :   best_match <tree, const char *> bm (name);
    7688           48 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    7689              :     {
    7690           33 :       tree id = TREE_PURPOSE (iter);
    7691           33 :       bm.consider (IDENTIFIER_POINTER (id));
    7692              :     }
    7693           15 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    7694              : }
    7695              : 
    7696              : /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
    7697              :    or a class TYPE).
    7698              : 
    7699              :    WANT as for lookup_name_1.
    7700              : 
    7701              :    Returns a DECL (or OVERLOAD, or BASELINK) representing the
    7702              :    declaration found.  If no suitable declaration can be found,
    7703              :    ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
    7704              :    neither a class-type nor a namespace a diagnostic is issued.  */
    7705              : 
    7706              : tree
    7707    545551529 : lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
    7708              : {
    7709    545551529 :   tree t = NULL_TREE;
    7710              : 
    7711    545551529 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    7712              :     {
    7713    409462812 :       name_lookup lookup (name, want);
    7714              : 
    7715    409462812 :       if (qualified_namespace_lookup (scope, &lookup))
    7716              :         {
    7717    401449775 :           t = lookup.value;
    7718              : 
    7719              :           /* If we have a known type overload, pull it out.  This can happen
    7720              :              for using decls.  */
    7721    401449775 :           if (TREE_CODE (t) == OVERLOAD
    7722    246234145 :               && TREE_TYPE (t) != unknown_type_node
    7723    402528900 :               && LIKELY (!cp_preserve_using_decl))
    7724      1079124 :             t = OVL_FUNCTION (t);
    7725              :         }
    7726    409462812 :     }
    7727    136088717 :   else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
    7728     14426425 :     t = lookup_enumerator (scope, name);
    7729    121662292 :   else if (is_class_type (scope, complain))
    7730    121640344 :     t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
    7731              :                        tf_warning_or_error);
    7732              : 
    7733    545529527 :   if (!t)
    7734      8150762 :     return error_mark_node;
    7735              :   return t;
    7736              : }
    7737              : 
    7738              : /* Wrapper for the above that takes a string argument.  The function name is
    7739              :    not at the beginning of the line to keep this wrapper out of etags.  */
    7740              : 
    7741       146254 : tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
    7742              : {
    7743       146254 :   return lookup_qualified_name (t, get_identifier (p), w, c);
    7744              : }
    7745              : 
    7746              : /* [namespace.qual]
    7747              :    Accepts the NAME to lookup and its qualifying SCOPE.
    7748              :    Returns the name/type pair found into the cxx_binding *RESULT,
    7749              :    or false on error.  */
    7750              : 
    7751              : static bool
    7752    416155941 : qualified_namespace_lookup (tree scope, name_lookup *lookup)
    7753              : {
    7754    416155941 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7755    416155941 :   query_oracle (lookup->name);
    7756    416155941 :   bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
    7757    832311882 :   return found;
    7758    416155941 : }
    7759              : 
    7760              : /* If DECL is suitably visible to the user, consider its name for
    7761              :    spelling correction.  */
    7762              : 
    7763              : static void
    7764         2130 : consider_decl (tree decl,  best_match <tree, const char *> &bm,
    7765              :                bool consider_impl_names)
    7766              : {
    7767              :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    7768              :      within range for).  */
    7769         2130 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7770              :     return;
    7771              : 
    7772         2047 :   tree suggestion = DECL_NAME (decl);
    7773         2047 :   if (!suggestion)
    7774              :     return;
    7775              : 
    7776              :   /* Don't suggest names that are for anonymous aggregate types, as
    7777              :      they are an implementation detail generated by the compiler.  */
    7778         1940 :   if (IDENTIFIER_ANON_P (suggestion))
    7779              :     return;
    7780              : 
    7781         1843 :   const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
    7782              : 
    7783              :   /* Ignore internal names with spaces in them.  */
    7784         1843 :   if (strchr (suggestion_str, ' '))
    7785              :     return;
    7786              : 
    7787              :   /* Don't suggest names that are reserved for use by the
    7788              :      implementation, unless NAME began with an underscore.  */
    7789         1843 :   if (!consider_impl_names
    7790         1843 :       && name_reserved_for_implementation_p (suggestion_str))
    7791              :     return;
    7792              : 
    7793         1813 :   bm.consider (suggestion_str);
    7794              : }
    7795              : 
    7796              : /* If DECL is suitably visible to the user, add its name to VEC and
    7797              :    return true.  Otherwise return false.  */
    7798              : 
    7799              : static bool
    7800      3397787 : maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
    7801              : {
    7802              :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    7803              :      within range for).  */
    7804      3397787 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7805              :     return false;
    7806              : 
    7807      3396998 :   tree suggestion = DECL_NAME (decl);
    7808      3396998 :   if (!suggestion)
    7809              :     return false;
    7810              : 
    7811              :   /* Don't suggest names that are for anonymous aggregate types, as
    7812              :      they are an implementation detail generated by the compiler.  */
    7813      3396998 :   if (IDENTIFIER_ANON_P (suggestion))
    7814              :     return false;
    7815              : 
    7816      3396998 :   vec.safe_push (suggestion);
    7817              : 
    7818      3396998 :   return true;
    7819              : }
    7820              : 
    7821              : /* Examing the namespace binding BINDING, and add at most one instance
    7822              :    of the name, if it contains a visible entity of interest.  Return
    7823              :    true if we added something.  */
    7824              : 
    7825              : bool
    7826      5874841 : maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
    7827              :                               lookup_name_fuzzy_kind kind)
    7828              : {
    7829      5874841 :   tree value = NULL_TREE;
    7830              : 
    7831      5874841 :   if (STAT_HACK_P (binding))
    7832              :     {
    7833          450 :       if (!STAT_TYPE_HIDDEN_P (binding)
    7834          450 :           && STAT_TYPE (binding))
    7835              :         {
    7836          257 :           if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
    7837              :             return true;
    7838              :         }
    7839          193 :       else if (!STAT_DECL_HIDDEN_P (binding))
    7840          118 :         value = STAT_DECL (binding);
    7841              :     }
    7842              :   else
    7843              :     value = binding;
    7844              : 
    7845      5874584 :   value = ovl_skip_hidden (value);
    7846      5874584 :   if (value)
    7847              :     {
    7848      5012074 :       value = OVL_FIRST (value);
    7849      5012074 :       if (kind != FUZZY_LOOKUP_TYPENAME
    7850      5012074 :           || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
    7851      3397530 :         if (maybe_add_fuzzy_decl (vec, value))
    7852              :           return true;
    7853              :     }
    7854              : 
    7855              :   /* Nothing found.  */
    7856              :   return false;
    7857              : }
    7858              : 
    7859              : /* Helper function for lookup_name_fuzzy.
    7860              :    Traverse binding level LVL, looking for good name matches for NAME
    7861              :    (and BM).  */
    7862              : static void
    7863         7356 : consider_binding_level (tree name, best_match <tree, const char *> &bm,
    7864              :                         cp_binding_level *lvl, bool look_within_fields,
    7865              :                         enum lookup_name_fuzzy_kind kind)
    7866              : {
    7867         7356 :   if (look_within_fields)
    7868         1062 :     if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
    7869              :       {
    7870          429 :         tree type = lvl->this_entity;
    7871          429 :         bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
    7872          429 :         tree best_matching_field
    7873          429 :           = lookup_member_fuzzy (type, name, want_type_p);
    7874          429 :         if (best_matching_field)
    7875           10 :           bm.consider (IDENTIFIER_POINTER (best_matching_field));
    7876              :       }
    7877              : 
    7878              :   /* Only suggest names reserved for the implementation if NAME begins
    7879              :      with an underscore.  */
    7880         7356 :   bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
    7881              : 
    7882         7356 :   if (lvl->kind != sk_namespace)
    7883         7397 :     for (tree t = lvl->names; t; t = TREE_CHAIN (t))
    7884              :       {
    7885         2765 :         tree d = t;
    7886              : 
    7887              :         /* OVERLOADs or decls from using declaration are wrapped into
    7888              :            TREE_LIST.  */
    7889         2765 :         if (TREE_CODE (d) == TREE_LIST)
    7890           27 :           d = OVL_FIRST (TREE_VALUE (d));
    7891              : 
    7892              :         /* Don't use bindings from implicitly declared functions,
    7893              :            as they were likely misspellings themselves.  */
    7894         2765 :         if (TREE_TYPE (d) == error_mark_node)
    7895          317 :           continue;
    7896              : 
    7897              :         /* If we want a typename, ignore non-types.  */
    7898         2766 :         if (kind == FUZZY_LOOKUP_TYPENAME
    7899         2448 :             && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
    7900          318 :           continue;
    7901              : 
    7902         2130 :         consider_decl (d, bm, consider_implementation_names);
    7903              :       }
    7904              :   else
    7905              :     {
    7906              :       /* We need to iterate over the namespace hash table, in order to
    7907              :          not mention hidden entities.  But hash table iteration is
    7908              :          (essentially) unpredictable, our correction-distance measure
    7909              :          is very granular, and we pick the first of equal distances.
    7910              :          Hence, we need to call the distance-measurer in a predictable
    7911              :          order.  So, iterate over the namespace hash, inserting
    7912              :          visible names into a vector.  Then sort the vector.  Then
    7913              :          determine spelling distance.  */
    7914              : 
    7915         2724 :       tree ns = lvl->this_entity;
    7916         2724 :       auto_vec<tree> vec;
    7917              : 
    7918         2724 :       hash_table<named_decl_hash>::iterator end
    7919         2724 :         (DECL_NAMESPACE_BINDINGS (ns)->end ());
    7920      5877655 :       for (hash_table<named_decl_hash>::iterator iter
    7921     11755310 :              (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
    7922              :         {
    7923      5874931 :           tree binding = *iter;
    7924              : 
    7925      5874931 :           if (TREE_CODE (binding) == BINDING_VECTOR)
    7926              :             {
    7927          351 :               bitmap imports = get_import_bitmap ();
    7928          351 :               binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    7929              : 
    7930          351 :               if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    7931           30 :                 if (maybe_add_fuzzy_binding (vec, bind, kind))
    7932           18 :                   continue;
    7933              : 
    7934              :               /* Scan the imported bindings.  */
    7935          333 :               unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    7936          333 :               if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    7937              :                 {
    7938          333 :                   ix--;
    7939          333 :                   cluster++;
    7940              :                 }
    7941              : 
    7942          675 :               for (; ix--; cluster++)
    7943          828 :                 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
    7944              :                      jx++)
    7945              :                   {
    7946              :                     /* Are we importing this module?  */
    7947          678 :                     if (unsigned base = cluster->indices[jx].base)
    7948          303 :                       if (unsigned span = cluster->indices[jx].span)
    7949          306 :                         do
    7950          306 :                           if (bitmap_bit_p (imports, base))
    7951          267 :                             goto found;
    7952           39 :                         while (++base, --span);
    7953          411 :                     continue;
    7954              : 
    7955          267 :                   found:;
    7956              :                     /* Is it loaded?  */
    7957          267 :                     if (cluster->slots[jx].is_lazy ())
    7958              :                       /* Let's not read in everything on the first
    7959              :                          spello! **/
    7960           36 :                       continue;
    7961          231 :                     if (tree bind = cluster->slots[jx])
    7962          231 :                       if (maybe_add_fuzzy_binding (vec, bind, kind))
    7963              :                         break;
    7964          411 :                   }
    7965              :             }
    7966              :           else
    7967      5874580 :             maybe_add_fuzzy_binding (vec, binding, kind);
    7968              :         }
    7969              : 
    7970    178792641 :       vec.qsort ([] (const void *a_, const void *b_)
    7971              :                  {
    7972              :                    return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
    7973              :                                   IDENTIFIER_POINTER (*(const tree *)b_));
    7974              :                  });
    7975              : 
    7976              :       /* Examine longest to shortest.  */
    7977      3402446 :       for (unsigned ix = vec.length (); ix--;)
    7978              :         {
    7979      3396998 :           const char *str = IDENTIFIER_POINTER (vec[ix]);
    7980              : 
    7981              :           /* Ignore internal names with spaces in them.  */
    7982      3396998 :           if (strchr (str, ' '))
    7983        70038 :             continue;
    7984              : 
    7985              :           /* Don't suggest names that are reserved for use by the
    7986              :              implementation, unless NAME began with an underscore.  */
    7987      6332171 :           if (!consider_implementation_names
    7988      3326960 :               && name_reserved_for_implementation_p (str))
    7989      3005211 :             continue;
    7990              : 
    7991       321749 :           bm.consider (str);
    7992              :         }
    7993         2724 :     }
    7994         7356 : }
    7995              : 
    7996              : /* Subclass of deferred_diagnostic.  Notify the user that the
    7997              :    given macro was used before it was defined.
    7998              :    This can be done in the C++ frontend since tokenization happens
    7999              :    upfront.  */
    8000              : 
    8001              : class macro_use_before_def : public deferred_diagnostic
    8002              : {
    8003              :  public:
    8004              :   /* Factory function.  Return a new macro_use_before_def instance if
    8005              :      appropriate, or return NULL. */
    8006              :   static std::unique_ptr<macro_use_before_def>
    8007           38 :   maybe_make (location_t use_loc, cpp_hashnode *macro)
    8008              :   {
    8009           38 :     location_t def_loc = cpp_macro_definition_location (macro);
    8010           38 :     if (def_loc == UNKNOWN_LOCATION)
    8011            0 :       return nullptr;
    8012              : 
    8013              :     /* We only want to issue a note if the macro was used *before* it was
    8014              :        defined.
    8015              :        We don't want to issue a note for cases where a macro was incorrectly
    8016              :        used, leaving it unexpanded (e.g. by using the wrong argument
    8017              :        count).  */
    8018           38 :     if (!linemap_location_before_p (line_table, use_loc, def_loc))
    8019            0 :       return nullptr;
    8020              : 
    8021           38 :     return std::make_unique<macro_use_before_def> (use_loc, macro);
    8022              :   }
    8023              : 
    8024              :   /* Ctor.  LOC is the location of the usage.  MACRO is the
    8025              :      macro that was used.  */
    8026           38 :   macro_use_before_def (location_t loc, cpp_hashnode *macro)
    8027           38 :   : deferred_diagnostic (loc), m_macro (macro)
    8028              :   {
    8029           38 :     gcc_assert (macro);
    8030           38 :   }
    8031              : 
    8032           76 :   ~macro_use_before_def ()
    8033           38 :   {
    8034           38 :     if (is_suppressed_p ())
    8035            0 :       return;
    8036              : 
    8037           38 :     inform (get_location (), "the macro %qs had not yet been defined",
    8038           38 :             (const char *)m_macro->ident.str);
    8039           76 :     inform (cpp_macro_definition_location (m_macro),
    8040              :             "it was later defined here");
    8041           76 :   }
    8042              : 
    8043              :  private:
    8044              :   cpp_hashnode *m_macro;
    8045              : };
    8046              : 
    8047              : /* Determine if it can ever make sense to offer RID as a suggestion for
    8048              :    a misspelling.
    8049              : 
    8050              :    Subroutine of lookup_name_fuzzy.  */
    8051              : 
    8052              : static bool
    8053       476528 : suggest_rid_p  (enum rid rid)
    8054              : {
    8055       476528 :   switch (rid)
    8056              :     {
    8057              :     /* Support suggesting function-like keywords.  */
    8058              :     case RID_STATIC_ASSERT:
    8059              :       return true;
    8060              : 
    8061       472420 :     default:
    8062              :       /* Support suggesting the various decl-specifier words, to handle
    8063              :          e.g. "singed" vs "signed" typos.  */
    8064       472420 :       if (cp_keyword_starts_decl_specifier_p (rid))
    8065              :         return true;
    8066              : 
    8067              :       /* Otherwise, don't offer it.  This avoids suggesting e.g. "if"
    8068              :          and "do" for short misspellings, which are likely to lead to
    8069              :          nonsensical results.  */
    8070              :       return false;
    8071              :     }
    8072              : }
    8073              : 
    8074              : /* Search for near-matches for NAME within the current bindings, and within
    8075              :    macro names, returning the best match as a const char *, or NULL if
    8076              :    no reasonable match is found.
    8077              : 
    8078              :    Use LOC for any deferred diagnostics.  */
    8079              : 
    8080              : name_hint
    8081         2368 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
    8082              : {
    8083         2368 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    8084              : 
    8085              :   /* Look up function-like macros first; maybe misusing them. */
    8086         4736 :   auto cpp_node = cpp_lookup (parse_in,
    8087         2368 :                               (const unsigned char*)IDENTIFIER_POINTER (name),
    8088         2368 :                               IDENTIFIER_LENGTH (name));
    8089         2368 :   if (cpp_node && cpp_fun_like_macro_p (cpp_node))
    8090           27 :     return name_hint
    8091              :       (nullptr,
    8092           27 :        std::make_unique<macro_like_function_used> (loc,
    8093           54 :                                                    IDENTIFIER_POINTER (name)));
    8094              : 
    8095              :   /* Then, try some well-known names in the C++ standard library, in case
    8096              :      the user forgot a #include.  */
    8097         2341 :   const char *header_hint
    8098         2341 :     = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
    8099         2341 :   if (header_hint)
    8100          249 :     return name_hint
    8101              :       (nullptr,
    8102          249 :        std::make_unique<suggest_missing_header> (loc,
    8103          498 :                                                  IDENTIFIER_POINTER (name),
    8104          249 :                                                  header_hint));
    8105              : 
    8106         2092 :   best_match <tree, const char *> bm (name);
    8107              : 
    8108         2092 :   cp_binding_level *lvl;
    8109         3154 :   for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
    8110         1062 :     consider_binding_level (name, bm, lvl, true, kind);
    8111              : 
    8112        10304 :   for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
    8113         6120 :     consider_binding_level (name, bm, lvl, false, kind);
    8114              : 
    8115              :   /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
    8116              :      as:
    8117              :        x = SOME_OTHER_MACRO (y);
    8118              :      then "SOME_OTHER_MACRO" will survive to the frontend and show up
    8119              :      as a misspelled identifier.
    8120              : 
    8121              :      Use the best distance so far so that a candidate is only set if
    8122              :      a macro is better than anything so far.  This allows early rejection
    8123              :      (without calculating the edit distance) of macro names that must have
    8124              :      distance >= bm.get_best_distance (), and means that we only get a
    8125              :      non-NULL result for best_macro_match if it's better than any of
    8126              :      the identifiers already checked.  */
    8127         2092 :   best_macro_match bmm (name, bm.get_best_distance (), parse_in);
    8128         2092 :   cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
    8129              :   /* If a macro is the closest so far to NAME, consider it.  */
    8130         2092 :   if (best_macro)
    8131           27 :     bm.consider ((const char *)best_macro->ident.str);
    8132         2065 :   else if (bmm.get_best_distance () == 0)
    8133              :     {
    8134              :       /* If we have an exact match for a macro name, then either the
    8135              :          macro was used with the wrong argument count, or the macro
    8136              :          has been used before it was defined.  */
    8137           74 :       if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
    8138           41 :         if (cpp_user_macro_p (macro))
    8139           38 :           return name_hint (NULL,
    8140           38 :                             macro_use_before_def::maybe_make (loc, macro));
    8141              :     }
    8142              : 
    8143              :   /* Try the "starts_decl_specifier_p" keywords to detect
    8144              :      "singed" vs "signed" typos.  */
    8145       478582 :   for (unsigned i = 0; i < num_c_common_reswords; i++)
    8146              :     {
    8147       476528 :       const c_common_resword *resword = &c_common_reswords[i];
    8148              : 
    8149       476528 :       if (!suggest_rid_p (resword->rid))
    8150       351234 :         continue;
    8151              : 
    8152       125294 :       tree resword_identifier = ridpointers [resword->rid];
    8153       125294 :       if (!resword_identifier)
    8154            0 :         continue;
    8155       125294 :       gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
    8156              : 
    8157              :       /* Only consider reserved words that survived the
    8158              :          filtering in init_reswords (e.g. for -std).  */
    8159       125294 :       if (!IDENTIFIER_KEYWORD_P (resword_identifier))
    8160        11638 :         continue;
    8161              : 
    8162       113656 :       bm.consider (IDENTIFIER_POINTER (resword_identifier));
    8163              :     }
    8164              : 
    8165         2054 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    8166              : }
    8167              : 
    8168              : /* Subroutine of outer_binding.
    8169              : 
    8170              :    Returns TRUE if BINDING is a binding to a template parameter of
    8171              :    SCOPE.  In that case SCOPE is the scope of a primary template
    8172              :    parameter -- in the sense of G++, i.e, a template that has its own
    8173              :    template header.
    8174              : 
    8175              :    Returns FALSE otherwise.  */
    8176              : 
    8177              : static bool
    8178    433099143 : binding_to_template_parms_of_scope_p (cxx_binding *binding,
    8179              :                                       cp_binding_level *scope)
    8180              : {
    8181    433099143 :   tree binding_value, tmpl, tinfo;
    8182    433099143 :   int level;
    8183              : 
    8184    433099143 :   if (!binding || !scope || !scope->this_entity)
    8185              :     return false;
    8186              : 
    8187    249702490 :   binding_value = binding->value ?  binding->value : binding->type;
    8188    249702490 :   tinfo = get_template_info (scope->this_entity);
    8189              : 
    8190              :   /* BINDING_VALUE must be a template parm.  */
    8191    249702490 :   if (binding_value == NULL_TREE
    8192    249702490 :       || (!DECL_P (binding_value)
    8193    249702490 :           || !DECL_TEMPLATE_PARM_P (binding_value)))
    8194              :     return false;
    8195              : 
    8196              :   /*  The level of BINDING_VALUE.  */
    8197    499404980 :   level =
    8198    249702490 :     template_type_parameter_p (binding_value)
    8199    249702490 :     ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
    8200              :                          (TREE_TYPE (binding_value)))
    8201     83802543 :     : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
    8202              : 
    8203              :   /* The template of the current scope, iff said scope is a primary
    8204              :      template.  */
    8205    249702490 :   tmpl = (tinfo
    8206    212786141 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
    8207    212786138 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
    8208    249702490 :           ? TI_TEMPLATE (tinfo)
    8209              :           : NULL_TREE);
    8210              : 
    8211              :   /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
    8212              :      then BINDING_VALUE is a parameter of TMPL.  */
    8213    184105483 :   return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
    8214              : }
    8215              : 
    8216              : /* Return the innermost non-namespace binding for NAME from a scope
    8217              :    containing BINDING, or, if BINDING is NULL, the current scope.
    8218              :    Please note that for a given template, the template parameters are
    8219              :    considered to be in the scope containing the current scope.
    8220              :    If CLASS_P is false, then class bindings are ignored.  */
    8221              : 
    8222              : cxx_binding *
    8223   6452226386 : outer_binding (tree name,
    8224              :                cxx_binding *binding,
    8225              :                bool class_p)
    8226              : {
    8227   6452226386 :   cxx_binding *outer;
    8228   6452226386 :   cp_binding_level *scope;
    8229   6452226386 :   cp_binding_level *outer_scope;
    8230              : 
    8231   6452226386 :   if (binding)
    8232              :     {
    8233      7894321 :       scope = binding->scope->level_chain;
    8234      7894321 :       outer = binding->previous;
    8235              :     }
    8236              :   else
    8237              :     {
    8238   6444332065 :       scope = current_binding_level;
    8239   6444332065 :       outer = IDENTIFIER_BINDING (name);
    8240              :     }
    8241   6452226386 :   outer_scope = outer ? outer->scope : NULL;
    8242              : 
    8243              :   /* Because we create class bindings lazily, we might be missing a
    8244              :      class binding for NAME.  If there are any class binding levels
    8245              :      between the LAST_BINDING_LEVEL and the scope in which OUTER was
    8246              :      declared, we must lookup NAME in those class scopes.  */
    8247   6452226386 :   if (class_p)
    8248  16174930917 :     while (scope && scope != outer_scope && scope->kind != sk_namespace)
    8249              :       {
    8250  11956788763 :         if (scope->kind == sk_class)
    8251              :           {
    8252   1791981234 :             cxx_binding *class_binding;
    8253              : 
    8254   1791981234 :             class_binding = get_class_binding (name, scope);
    8255   1791981234 :             if (class_binding)
    8256              :               {
    8257              :                 /* Thread this new class-scope binding onto the
    8258              :                    IDENTIFIER_BINDING list so that future lookups
    8259              :                    find it quickly.  */
    8260    141970422 :                 if (BASELINK_P (class_binding->value))
    8261              :                   /* Don't put a BASELINK in IDENTIFIER_BINDING.  */
    8262     39948260 :                   class_binding->value
    8263     39948260 :                     = BASELINK_FUNCTIONS (class_binding->value);
    8264    141970422 :                 class_binding->previous = outer;
    8265    141970422 :                 if (binding)
    8266            3 :                   binding->previous = class_binding;
    8267              :                 else
    8268    141970419 :                   IDENTIFIER_BINDING (name) = class_binding;
    8269    141970422 :                 return class_binding;
    8270              :               }
    8271              :           }
    8272              :         /* If we are in a member template, the template parms of the member
    8273              :            template are considered to be inside the scope of the containing
    8274              :            class, but within G++ the class bindings are all pushed between the
    8275              :            template parms and the function body.  So if the outer binding is
    8276              :            a template parm for the current scope, return it now rather than
    8277              :            look for a class binding.  */
    8278   5441250589 :         if (outer_scope && outer_scope->kind == sk_template_parms
    8279  12247917484 :             && binding_to_template_parms_of_scope_p (outer, scope))
    8280              :           return outer;
    8281              : 
    8282  11645715492 :         scope = scope->level_chain;
    8283              :       }
    8284              : 
    8285              :   return outer;
    8286              : }
    8287              : 
    8288              : /* Return the innermost block-scope or class-scope value binding for
    8289              :    NAME, or NULL_TREE if there is no such binding.  */
    8290              : 
    8291              : tree
    8292    931782834 : innermost_non_namespace_value (tree name)
    8293              : {
    8294    931782834 :   cxx_binding *binding;
    8295    931782834 :   binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
    8296    931782834 :   return binding ? binding->value : NULL_TREE;
    8297              : }
    8298              : 
    8299              : /* True iff current_binding_level is within the potential scope of local
    8300              :    variable DECL. */
    8301              : 
    8302              : bool
    8303          200 : decl_in_scope_p (tree decl)
    8304              : {
    8305          200 :   gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
    8306              : 
    8307          200 :   tree name = DECL_NAME (decl);
    8308              : 
    8309          200 :   for (cxx_binding *iter = NULL;
    8310          211 :        (iter = outer_binding (name, iter, /*class_p=*/false)); )
    8311              :     {
    8312           74 :       if (!LOCAL_BINDING_P (iter))
    8313              :         return false;
    8314           74 :       if (iter->value == decl)
    8315              :         return true;
    8316              :     }
    8317              : 
    8318              :   return false;
    8319              : }
    8320              : 
    8321              : /* Look up NAME in the current binding level and its superiors in the
    8322              :    namespace of variables, functions and typedefs.  Return a ..._DECL
    8323              :    node of some kind representing its definition if there is only one
    8324              :    such declaration, or return a TREE_LIST with all the overloaded
    8325              :    definitions if there are many, or return NULL_TREE if it is undefined.
    8326              :    Hidden name, either friend declaration or built-in function, are
    8327              :    not ignored.
    8328              : 
    8329              :    WHERE controls which scopes are considered.  It is a bit mask of
    8330              :    LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
    8331              :    (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
    8332              :    scopes).  It is an error for no bits to be set.  These scopes are
    8333              :    searched from innermost to outermost.
    8334              : 
    8335              :    WANT controls what kind of entity we'd happy with.
    8336              :    LOOK_want::NORMAL for normal lookup (implicit typedefs can be
    8337              :    hidden).  LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
    8338              :    for only NAMESPACE_DECLS.  These two can be bit-ored to find
    8339              :    namespace or type.
    8340              : 
    8341              :    WANT can also have LOOK_want::HIDDEN_FRIEND or
    8342              :    LOOK_want::HIDDEN_LAMBDa added to it.  */
    8343              : 
    8344              : tree
    8345   4907386126 : lookup_name (tree name, LOOK_where where, LOOK_want want)
    8346              : {
    8347   4907386126 :   tree val = NULL_TREE;
    8348              : 
    8349   4907386126 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8350              : 
    8351   4907386126 :   gcc_checking_assert (unsigned (where) != 0);
    8352              :   /* If we're looking for hidden lambda things, we shouldn't be
    8353              :      looking in namespace scope.  */
    8354   4907386126 :   gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
    8355              :                        || !bool (where & LOOK_where::NAMESPACE));
    8356   4907386126 :   query_oracle (name);
    8357              : 
    8358              :   /* Conversion operators are handled specially because ordinary
    8359              :      unqualified name lookup will not find template conversion
    8360              :      operators.  */
    8361   4907386126 :   if (IDENTIFIER_CONV_OP_P (name))
    8362              :     {
    8363      1004579 :       cp_binding_level *level;
    8364              : 
    8365      1004579 :       for (level = current_binding_level;
    8366      2794610 :            level && level->kind != sk_namespace;
    8367      1790031 :            level = level->level_chain)
    8368              :         {
    8369      1965910 :           tree class_type;
    8370      1965910 :           tree operators;
    8371              : 
    8372              :           /* A conversion operator can only be declared in a class
    8373              :              scope.  */
    8374      1965910 :           if (level->kind != sk_class)
    8375       827669 :             continue;
    8376              : 
    8377              :           /* Lookup the conversion operator in the class.  */
    8378      1138241 :           class_type = level->this_entity;
    8379      1138241 :           operators = lookup_fnfields (class_type, name, /*protect=*/0,
    8380              :                                        tf_warning_or_error);
    8381      1138241 :           if (operators)
    8382              :             return operators;
    8383              :         }
    8384              : 
    8385              :       return NULL_TREE;
    8386              :     }
    8387              : 
    8388              :   /* First, look in non-namespace scopes.  */
    8389              : 
    8390   4906381547 :   if (current_class_type == NULL_TREE)
    8391              :     /* Maybe avoid searching the binding stack at all.  */
    8392   1815488158 :     where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
    8393              : 
    8394   4906381547 :   if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
    8395              :     for (cxx_binding *iter = nullptr;
    8396   4913671910 :          (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
    8397              :       {
    8398              :         /* Skip entities we don't want.  */
    8399   4422039086 :         if (!bool (where & (LOCAL_BINDING_P (iter)
    8400              :                             ? LOOK_where::BLOCK : LOOK_where::CLASS)))
    8401        15520 :           continue;
    8402              : 
    8403              :         /* If this is the kind of thing we're looking for, we're done.  */
    8404   3570249634 :         if (iter->value)
    8405              :           {
    8406   3570249634 :             tree binding = NULL_TREE;
    8407              : 
    8408   3506840778 :             if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
    8409   3570249569 :                 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
    8410   3570018153 :                     || !is_lambda_ignored_entity (iter->value))
    8411   7132703397 :                 && qualify_lookup (iter->value, want))
    8412   3562370955 :               binding = iter->value;
    8413      7878679 :             else if (bool (want & LOOK_want::TYPE)
    8414        82784 :                      && !HIDDEN_TYPE_BINDING_P (iter)
    8415      7961448 :                      && iter->type)
    8416   3570249634 :               binding = iter->type;
    8417              : 
    8418   3570249634 :             if (binding)
    8419              :               {
    8420   3562371025 :                 val = strip_using_decl (binding);
    8421   3562371025 :                 break;
    8422              :               }
    8423              :           }
    8424              :       }
    8425              : 
    8426              :   /* Now lookup in namespace scopes.  */
    8427   4906381547 :   if (!val && bool (where & LOOK_where::NAMESPACE))
    8428              :     {
    8429   1344010306 :       name_lookup lookup (name, want);
    8430   1344010306 :       if (lookup.search_unqualified
    8431   1344010306 :           (current_decl_namespace (), current_binding_level))
    8432   1042107593 :         val = lookup.value;
    8433   1344010306 :     }
    8434              : 
    8435              :   /* If we have a known type overload, pull it out.  This can happen
    8436              :      for both using decls and unhidden functions.  */
    8437   4906381547 :   if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
    8438      2782333 :     val = OVL_FUNCTION (val);
    8439              : 
    8440              :   return val;
    8441   4907386126 : }
    8442              : 
    8443              : tree
    8444    310427586 : lookup_name (tree name)
    8445              : {
    8446    310427586 :   return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
    8447              : }
    8448              : 
    8449              : /* Look up NAME for type used in elaborated name specifier in
    8450              :    the scopes given by HOW.
    8451              : 
    8452              :    Unlike lookup_name_1, we make sure that NAME is actually
    8453              :    declared in the desired scope, not from inheritance, nor using
    8454              :    directive.  For using declaration, there is DR138 still waiting
    8455              :    to be resolved.  Hidden name coming from an earlier friend
    8456              :    declaration is also returned, and will be made visible unless HOW
    8457              :    is TAG_how::HIDDEN_FRIEND.
    8458              : 
    8459              :    A TYPE_DECL best matching the NAME is returned.  Catching error
    8460              :    and issuing diagnostics are caller's responsibility.  */
    8461              : 
    8462              : tree
    8463     23774569 : lookup_elaborated_type (tree name, TAG_how how)
    8464              : {
    8465     23774569 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8466              : 
    8467     23774569 :   cp_binding_level *b = current_binding_level;
    8468              : 
    8469     23774569 :   if (b->kind != sk_namespace)
    8470              :     /* Look in non-namespace scopes.  */
    8471              :     for (cxx_binding *iter = NULL;
    8472     16617159 :          (iter = outer_binding (name, iter, /*class_p=*/ true)); )
    8473              :       {
    8474              :         /* First check we're supposed to be looking in this scope --
    8475              :            if we're not, we're done.  */
    8476       944111 :         for (; b != iter->scope; b = b->level_chain)
    8477       615999 :           if (!(b->kind == sk_cleanup
    8478       453913 :                 || b->kind == sk_template_parms
    8479       162095 :                 || b->kind == sk_function_parms
    8480       162065 :                 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    8481              :             return NULL_TREE;
    8482              : 
    8483              :         /* Check if this is the kind of thing we're looking for.  If
    8484              :            HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
    8485              :            come from base class.  For ITER->VALUE, we can simply use
    8486              :            INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
    8487              :            our own check.
    8488              : 
    8489              :            We check ITER->TYPE before ITER->VALUE in order to handle
    8490              :              typedef struct C {} C;
    8491              :            correctly.  */
    8492              : 
    8493       490195 :         if (tree type = strip_using_decl (iter->type))
    8494              :           {
    8495        51831 :             if (qualify_lookup (type, LOOK_want::TYPE)
    8496        51831 :                 && (how != TAG_how::CURRENT_ONLY
    8497          121 :                     || LOCAL_BINDING_P (iter)
    8498          121 :                     || DECL_CONTEXT (type) == iter->scope->this_entity))
    8499              :               {
    8500        51782 :                 if (how != TAG_how::HIDDEN_FRIEND)
    8501              :                   /* It is no longer a hidden binding.  */
    8502           75 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8503              : 
    8504        51782 :                 return type;
    8505              :               }
    8506              :           }
    8507              :         else
    8508              :           {
    8509       438364 :             tree value = strip_using_decl (iter->value);
    8510       438364 :             if (qualify_lookup (value, LOOK_want::TYPE)
    8511       438364 :                 && (how != TAG_how::CURRENT_ONLY
    8512       102426 :                     || !INHERITED_VALUE_BINDING_P (iter)))
    8513              :               {
    8514       438232 :                 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
    8515              :                   /* It is no longer a hidden binding.  */
    8516       102432 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8517              : 
    8518       438232 :                 return value;
    8519              :               }
    8520              :           }
    8521              :       }
    8522              : 
    8523              :   /* Now check if we can look in namespace scope.  */
    8524     37129455 :   for (; b->kind != sk_namespace; b = b->level_chain)
    8525              :     if (!(b->kind == sk_cleanup
    8526              :           || b->kind == sk_template_parms
    8527              :           || b->kind == sk_function_parms
    8528      3944789 :           || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    8529              :       return NULL_TREE;
    8530              : 
    8531              :   /* Look in the innermost namespace.  */
    8532     19692075 :   tree ns = b->this_entity;
    8533              : 
    8534              :   /* If an import is going to provide a definition for this tag,
    8535              :      load it now so that we don't get confused later when processing
    8536              :      this tag's definition.  */
    8537     19692075 :   if (modules_p ())
    8538        78466 :     lazy_load_pendings (ns, name);
    8539              : 
    8540     19692075 :   if (tree *slot = find_namespace_slot (ns, name))
    8541              :     {
    8542      3298413 :       tree bind = *slot;
    8543      3298413 :       if (TREE_CODE (bind) == BINDING_VECTOR)
    8544           99 :         bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
    8545              : 
    8546           99 :       if (bind)
    8547              :         {
    8548              :           /* If this is the kind of thing we're looking for, we're done.  */
    8549      3298341 :           if (tree type = strip_using_decl (MAYBE_STAT_TYPE (bind)))
    8550              :             {
    8551          167 :               if (how != TAG_how::HIDDEN_FRIEND)
    8552              :                 /* No longer hidden.  */
    8553          167 :                 STAT_TYPE_HIDDEN_P (*slot) = false;
    8554              : 
    8555          167 :               return type;
    8556              :             }
    8557      3298174 :           else if (tree decl = strip_using_decl (MAYBE_STAT_DECL (bind)))
    8558              :             {
    8559      3298174 :               if (qualify_lookup (decl, LOOK_want::TYPE))
    8560              :                 {
    8561      2945620 :                   if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
    8562      3423711 :                       && STAT_DECL_HIDDEN_P (bind))
    8563              :                     {
    8564       134802 :                       if (STAT_TYPE (bind))
    8565            0 :                         STAT_DECL_HIDDEN_P (bind) = false;
    8566              :                       else
    8567              :                         {
    8568              :                           /* There is no type, just remove the stat
    8569              :                              hack.  */
    8570       134802 :                           if (*slot == bind)
    8571       134799 :                             *slot = decl;
    8572              :                           else
    8573            3 :                             BINDING_VECTOR_CLUSTER (*slot, 0)
    8574            3 :                               .slots[BINDING_SLOT_CURRENT] = decl;
    8575              :                         }
    8576              :                     }
    8577      3288892 :                   return decl;
    8578              :                 }
    8579              :             }
    8580              :         }
    8581              : 
    8582         9354 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    8583              :         {
    8584              :           /* We could be redeclaring a global module entity, (from GMF
    8585              :              or header unit), or from another partition, or
    8586              :              specializing an imported template.  */
    8587           72 :           bitmap imports = get_import_bitmap ();
    8588           72 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    8589              : 
    8590              :           /* Scan the imported bindings.  */
    8591           72 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    8592           72 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    8593              :             {
    8594           72 :               ix--;
    8595           72 :               cluster++;
    8596              :             }
    8597              : 
    8598              :           /* Do this in forward order, so we load modules in an order
    8599              :              the user expects.  */
    8600           84 :           for (; ix--; cluster++)
    8601          156 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    8602              :               {
    8603              :                 /* Are we importing this module?  */
    8604          144 :                 if (unsigned base = cluster->indices[jx].base)
    8605           61 :                   if (unsigned span = cluster->indices[jx].span)
    8606           61 :                     do
    8607           61 :                       if (bitmap_bit_p (imports, base))
    8608           60 :                         goto found;
    8609            1 :                     while (++base, --span);
    8610           84 :                 continue;
    8611              : 
    8612           60 :               found:;
    8613              :                 /* Is it loaded?  */
    8614           60 :                 if (cluster->slots[jx].is_lazy ())
    8615              :                   {
    8616           32 :                     gcc_assert (cluster->indices[jx].span == 1);
    8617           32 :                     lazy_load_binding (cluster->indices[jx].base,
    8618              :                                        ns, name, &cluster->slots[jx]);
    8619              :                   }
    8620           60 :                 tree bind = cluster->slots[jx];
    8621           60 :                 if (!bind)
    8622              :                   /* Load errors could mean there's nothing here.  */
    8623            0 :                   continue;
    8624              : 
    8625              :                 /* Extract what we can see from here.  If there's no
    8626              :                    stat_hack, then everything was exported.  */
    8627           60 :                 tree type = NULL_TREE;
    8628              : 
    8629              :                 /* If no stat hack, everything is visible.  */
    8630           60 :                 if (STAT_HACK_P (bind))
    8631              :                   {
    8632           51 :                     if (STAT_TYPE_VISIBLE_P (bind))
    8633           18 :                       type = STAT_TYPE (bind);
    8634           51 :                     bind = STAT_VISIBLE (bind);
    8635              :                   }
    8636              : 
    8637           51 :                 if (type && qualify_lookup (type, LOOK_want::TYPE))
    8638            3 :                   return strip_using_decl (type);
    8639              : 
    8640           57 :                 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
    8641           57 :                   return strip_using_decl (bind);
    8642           84 :               }
    8643              : 
    8644           12 :           if (!module_purview_p ())
    8645              :             {
    8646              :               /* We're in the global module, perhaps there's a tag
    8647              :                  there?  */
    8648              : 
    8649              :               /* FIXME: In general we should probably merge global module
    8650              :                  classes in check_module_override rather than here, but for
    8651              :                  GCC14 let's just fix lazy declarations of __class_type_info in
    8652              :                  build_dynamic_cast_1.  */
    8653           12 :               if (current_namespace == abi_node)
    8654              :                 {
    8655            9 :                   tree g = (BINDING_VECTOR_CLUSTER (*slot, 0)
    8656            9 :                             .slots[BINDING_SLOT_GLOBAL]);
    8657            9 :                   for (ovl_iterator iter (g); iter; ++iter)
    8658            9 :                     if (qualify_lookup (*iter, LOOK_want::TYPE))
    8659            9 :                       return *iter;
    8660              :                 }
    8661              :             }
    8662              :         }
    8663              :     }
    8664              : 
    8665              :   return NULL_TREE;
    8666     23774569 : }
    8667              : 
    8668              : /* The type TYPE is being declared.  If it is a class template, or a
    8669              :    specialization of a class template, do any processing required and
    8670              :    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
    8671              :    being declared a friend.  B is the binding level at which this TYPE
    8672              :    should be bound.
    8673              : 
    8674              :    Returns the TYPE_DECL for TYPE, which may have been altered by this
    8675              :    processing.  */
    8676              : 
    8677              : static tree
    8678     23103415 : maybe_process_template_type_declaration (tree type, int is_friend,
    8679              :                                          cp_binding_level *b)
    8680              : {
    8681     23103415 :   tree decl = TYPE_NAME (type);
    8682              : 
    8683     23103415 :   if (processing_template_parmlist)
    8684              :     /* You can't declare a new template type in a template parameter
    8685              :        list.  But, you can declare a non-template type:
    8686              : 
    8687              :          template <class A*> struct S;
    8688              : 
    8689              :        is a forward-declaration of `A'.  */
    8690              :     ;
    8691     23103265 :   else if (b->kind == sk_namespace
    8692      6810892 :            && current_binding_level->kind != sk_namespace)
    8693              :     /* If this new type is being injected into a containing scope,
    8694              :        then it's not a template type.  */
    8695              :     ;
    8696              :   else
    8697              :     {
    8698     22979582 :       gcc_assert (MAYBE_CLASS_TYPE_P (type)
    8699              :                   || TREE_CODE (type) == ENUMERAL_TYPE);
    8700              : 
    8701     22979582 :       if (processing_template_decl)
    8702              :         {
    8703     13660682 :           decl = push_template_decl (decl, is_friend);
    8704     13660682 :           if (decl == error_mark_node)
    8705              :             return error_mark_node;
    8706              : 
    8707              :           /* If the current binding level is the binding level for the
    8708              :              template parameters (see the comment in
    8709              :              begin_template_parm_list) and the enclosing level is a class
    8710              :              scope, and we're not looking at a friend, push the
    8711              :              declaration of the member class into the class scope.  In the
    8712              :              friend case, push_template_decl will already have put the
    8713              :              friend into global scope, if appropriate.  */
    8714     13660661 :           if (TREE_CODE (type) != ENUMERAL_TYPE
    8715     13315962 :               && !is_friend && b->kind == sk_template_parms
    8716     10568576 :               && b->level_chain->kind == sk_class)
    8717              :             {
    8718       702799 :               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
    8719              : 
    8720       702799 :               if (!COMPLETE_TYPE_P (current_class_type))
    8721       702778 :                 maybe_add_class_template_decl_list (current_class_type,
    8722              :                                                     type, /*friend_p=*/0);
    8723              :             }
    8724              :         }
    8725              :     }
    8726              : 
    8727              :   return decl;
    8728              : }
    8729              : 
    8730              : /* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
    8731              :    that the NAME is a class template, the tag is processed but not pushed.
    8732              : 
    8733              :    The pushed scope depend on the SCOPE parameter:
    8734              :    - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
    8735              :      scope.
    8736              :    - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
    8737              :      non-template-parameter scope.  This case is needed for forward
    8738              :      declarations.
    8739              :    - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
    8740              :      TS_GLOBAL case except that names within template-parameter scopes
    8741              :      are not pushed at all.
    8742              : 
    8743              :    Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
    8744              : 
    8745              : tree
    8746     23103415 : pushtag (tree name, tree type, TAG_how how)
    8747              : {
    8748     23103415 :   tree decl;
    8749              : 
    8750     23103415 :   gcc_assert (identifier_p (name));
    8751              : 
    8752     23103415 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8753              : 
    8754     23103415 :   cp_binding_level *b = current_binding_level;
    8755     23237535 :   while (true)
    8756              :     {
    8757     23237535 :       if (/* Cleanup scopes are not scopes from the point of view of
    8758              :              the language.  */
    8759     23237535 :           b->kind == sk_cleanup
    8760              :           /* Neither are function parameter scopes.  */
    8761     23237529 :           || b->kind == sk_function_parms
    8762              :           /* Neither are the scopes used to hold template parameters
    8763              :              for an explicit specialization.  For an ordinary template
    8764              :              declaration, these scopes are not scopes from the point of
    8765              :              view of the language.  */
    8766     23225565 :           || (b->kind == sk_template_parms
    8767     11052141 :               && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
    8768        12033 :         b = b->level_chain;
    8769     23225502 :       else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
    8770              :         {
    8771       122087 :           b = b->level_chain;
    8772       122087 :           if (b->kind == sk_template_parms)
    8773          483 :             b = b->level_chain;
    8774              :         }
    8775              :       else
    8776              :         break;
    8777              :     }
    8778              : 
    8779              :   /* Do C++ gratuitous typedefing.  */
    8780     23103415 :   if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
    8781              :     {
    8782     23103415 :       tree tdef;
    8783     23103415 :       tree context = TYPE_CONTEXT (type);
    8784              : 
    8785     23103415 :       if (! context)
    8786              :         {
    8787              :           cp_binding_level *cb = b;
    8788     36685261 :           while (cb->kind != sk_namespace
    8789     20008435 :                  && cb->kind != sk_class
    8790     52739302 :                  && (cb->kind != sk_function_parms
    8791      2118948 :                      || !cb->this_entity))
    8792     13935097 :             cb = cb->level_chain;
    8793     22750164 :           tree cs = cb->this_entity;
    8794              : 
    8795     22750164 :           gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
    8796              :                                ? cs == current_function_decl
    8797              :                                : TYPE_P (cs) ? cs == current_class_type
    8798              :                                : cs == current_namespace);
    8799              : 
    8800     22750164 :           if (how == TAG_how::CURRENT_ONLY
    8801       253801 :               || (cs && TREE_CODE (cs) == FUNCTION_DECL))
    8802              :             context = cs;
    8803       253708 :           else if (cs && TYPE_P (cs))
    8804              :             /* When declaring a friend class of a local class, we want
    8805              :                to inject the newly named class into the scope
    8806              :                containing the local class, not the namespace
    8807              :                scope.  */
    8808       130098 :             context = decl_function_context (get_type_decl (cs));
    8809              :         }
    8810     22750071 :       if (!context)
    8811       253705 :         context = current_namespace;
    8812              : 
    8813     23103415 :       tdef = create_implicit_typedef (name, type);
    8814     23103415 :       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
    8815     23103415 :       set_originating_module (tdef);
    8816              : 
    8817     23103415 :       decl = maybe_process_template_type_declaration
    8818     23103415 :         (type, how == TAG_how::HIDDEN_FRIEND, b);
    8819     23103415 :       if (decl == error_mark_node)
    8820              :         return decl;
    8821              : 
    8822     23103394 :       if (b->kind == sk_class)
    8823              :         {
    8824      3121381 :           if (!TYPE_BEING_DEFINED (current_class_type))
    8825              :             /* Don't push anywhere if the class is complete; a lambda in an
    8826              :                NSDMI is not a member of the class.  */
    8827              :             ;
    8828      3119492 :           else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
    8829              :             /* Put this TYPE_DECL on the TYPE_FIELDS list for the
    8830              :                class.  But if it's a member template class, we want
    8831              :                the TEMPLATE_DECL, not the TYPE_DECL, so this is done
    8832              :                later.  */
    8833      3119149 :             finish_member_declaration (decl);
    8834              :           else
    8835          343 :             pushdecl_class_level (decl);
    8836              :         }
    8837     19982013 :       else if (b->kind == sk_template_parms)
    8838              :         {
    8839              :           /* Do not push the tag here -- we'll want to push the
    8840              :              TEMPLATE_DECL.  */
    8841     11052060 :           if (b->level_chain->kind != sk_class)
    8842      9865903 :             set_identifier_type_value_with_scope (name, tdef, b->level_chain);
    8843              :         }
    8844              :       else
    8845              :         {
    8846      8929953 :           decl = do_pushdecl_with_scope
    8847      8929953 :             (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
    8848      8929953 :           if (decl == error_mark_node)
    8849              :             return decl;
    8850              : 
    8851      8929905 :           if (DECL_CONTEXT (decl) == std_node
    8852      2346428 :               && init_list_identifier == DECL_NAME (TYPE_NAME (type))
    8853      8929911 :               && !CLASSTYPE_TEMPLATE_INFO (type))
    8854              :             {
    8855            6 :               error ("declaration of %<std::initializer_list%> does not match "
    8856              :                      "%<#include <initializer_list>%>, isn%'t a template");
    8857            6 :               return error_mark_node;
    8858              :             }
    8859              :         }
    8860              : 
    8861     23103340 :       TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
    8862              : 
    8863              :       /* If this is a local class, keep track of it.  We need this
    8864              :          information for name-mangling, and so that it is possible to
    8865              :          find all function definitions in a translation unit in a
    8866              :          convenient way.  (It's otherwise tricky to find a member
    8867              :          function definition it's only pointed to from within a local
    8868              :          class.)  */
    8869     23103340 :       if (TYPE_FUNCTION_SCOPE_P (type))
    8870              :         {
    8871      2118926 :           if (processing_template_decl)
    8872              :             {
    8873              :               /* Push a DECL_EXPR so we call pushtag at the right time in
    8874              :                  template instantiation rather than in some nested context.  */
    8875      1202436 :               add_decl_expr (decl);
    8876              :             }
    8877              :           /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead.  */
    8878      1829204 :           else if (!LAMBDA_TYPE_P (type))
    8879       582172 :             determine_local_discriminator (TYPE_NAME (type));
    8880              :         }
    8881              :     }
    8882              : 
    8883     23103340 :   if (b->kind == sk_class
    8884     23103340 :       && !COMPLETE_TYPE_P (current_class_type))
    8885      3119528 :     maybe_add_class_template_decl_list (current_class_type,
    8886              :                                         type, /*friend_p=*/0);
    8887              : 
    8888     23103340 :   decl = TYPE_NAME (type);
    8889     23103340 :   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    8890              : 
    8891              :   /* Set type visibility now if this is a forward declaration.  */
    8892     23103340 :   TREE_PUBLIC (decl) = 1;
    8893     23103340 :   determine_visibility (decl);
    8894     23103340 :   check_module_decl_linkage (decl);
    8895              : 
    8896     23103340 :   return type;
    8897     23103415 : }
    8898              : 
    8899              : /* Subroutines for reverting temporarily to top-level for instantiation
    8900              :    of templates and such.  We actually need to clear out the class- and
    8901              :    local-value slots of all identifiers, so that only the global values
    8902              :    are at all visible.  Simply setting current_binding_level to the global
    8903              :    scope isn't enough, because more binding levels may be pushed.  */
    8904              : struct saved_scope *scope_chain;
    8905              : 
    8906              : /* Return true if ID has not already been marked.  */
    8907              : 
    8908              : static inline bool
    8909   2859205853 : store_binding_p (tree id)
    8910              : {
    8911   5712964655 :   if (!id || !IDENTIFIER_BINDING (id))
    8912              :     return false;
    8913              : 
    8914   2628898177 :   if (IDENTIFIER_MARKED (id))
    8915            0 :     return false;
    8916              : 
    8917              :   return true;
    8918              : }
    8919              : 
    8920              : /* Add an appropriate binding to *OLD_BINDINGS which needs to already
    8921              :    have enough space reserved.  */
    8922              : 
    8923              : static void
    8924   1081751642 : store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
    8925              : {
    8926   1081751642 :   cxx_saved_binding saved;
    8927              : 
    8928   1081751642 :   gcc_checking_assert (store_binding_p (id));
    8929              : 
    8930   1081751642 :   IDENTIFIER_MARKED (id) = 1;
    8931              : 
    8932   1081751642 :   saved.identifier = id;
    8933   1081751642 :   saved.binding = IDENTIFIER_BINDING (id);
    8934   1081751642 :   saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
    8935   1081751642 :   (*old_bindings)->quick_push (saved);
    8936   1081751642 :   IDENTIFIER_BINDING (id) = NULL;
    8937   1081751642 : }
    8938              : 
    8939              : static void
    8940    603095743 : store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
    8941              : {
    8942    603095743 :   static vec<tree> bindings_need_stored;
    8943    603095743 :   tree t, id;
    8944    603095743 :   size_t i;
    8945              : 
    8946    603095743 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8947   1688049495 :   for (t = names; t; t = TREE_CHAIN (t))
    8948              :     {
    8949    481858009 :       if (TREE_CODE (t) == TREE_LIST)
    8950     16213627 :         id = TREE_PURPOSE (t);
    8951              :       else
    8952    465644382 :         id = DECL_NAME (t);
    8953              : 
    8954    481858009 :       if (store_binding_p (id))
    8955    465394893 :         bindings_need_stored.safe_push (id);
    8956              :     }
    8957    603095743 :   if (!bindings_need_stored.is_empty ())
    8958              :     {
    8959    180590490 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8960    826575873 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8961              :         {
    8962              :           /* We can apparently have duplicates in NAMES.  */
    8963    465394893 :           if (store_binding_p (id))
    8964    465394550 :             store_binding (id, old_bindings);
    8965              :         }
    8966    180590490 :       bindings_need_stored.truncate (0);
    8967              :     }
    8968    603095743 : }
    8969              : 
    8970              : /* Like store_bindings, but NAMES is a vector of cp_class_binding
    8971              :    objects, rather than a TREE_LIST.  */
    8972              : 
    8973              : static void
    8974    382610394 : store_class_bindings (vec<cp_class_binding, va_gc> *names,
    8975              :                       vec<cxx_saved_binding, va_gc> **old_bindings)
    8976              : {
    8977    382610394 :   static vec<tree> bindings_need_stored;
    8978    382610394 :   size_t i;
    8979    382610394 :   cp_class_binding *cb;
    8980              : 
    8981   1212811703 :   for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
    8982    830201309 :     if (store_binding_p (cb->identifier))
    8983    616357092 :       bindings_need_stored.safe_push (cb->identifier);
    8984    382610394 :   if (!bindings_need_stored.is_empty ())
    8985              :     {
    8986     75617181 :       tree id;
    8987     75617181 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8988    767591454 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8989    616357092 :         store_binding (id, old_bindings);
    8990     75617181 :       bindings_need_stored.truncate (0);
    8991              :     }
    8992    382610394 : }
    8993              : 
    8994              : /* A chain of saved_scope structures awaiting reuse.  */
    8995              : 
    8996              : static GTY((deletable)) struct saved_scope *free_saved_scope;
    8997              : 
    8998              : /* Temporarily make the current scope the global namespace, saving away
    8999              :    the current scope for pop_from_top_level.  */
    9000              : 
    9001              : void
    9002    503343027 : push_to_top_level (void)
    9003              : {
    9004    503343027 :   struct saved_scope *s;
    9005    503343027 :   cp_binding_level *b;
    9006    503343027 :   cxx_saved_binding *sb;
    9007    503343027 :   size_t i;
    9008    503343027 :   bool need_pop;
    9009              : 
    9010    503343027 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9011              : 
    9012              :   /* Reuse or create a new structure for this saved scope.  */
    9013    503343027 :   if (free_saved_scope != NULL)
    9014              :     {
    9015    502110559 :       s = free_saved_scope;
    9016    502110559 :       free_saved_scope = s->prev;
    9017              : 
    9018    502110559 :       vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
    9019    502110559 :       memset (s, 0, sizeof (*s));
    9020              :       /* Also reuse the structure's old_bindings vector.  */
    9021    502110559 :       vec_safe_truncate (old_bindings, 0);
    9022    502110559 :       s->old_bindings = old_bindings;
    9023              :     }
    9024              :   else
    9025      1232468 :     s = ggc_cleared_alloc<saved_scope> ();
    9026              : 
    9027    503343027 :   b = scope_chain ? current_binding_level : 0;
    9028              : 
    9029              :   /* If we're in the middle of some function, save our state.  */
    9030    503343027 :   if (cfun)
    9031              :     {
    9032     86710018 :       need_pop = true;
    9033     86710018 :       push_function_context ();
    9034              :     }
    9035              :   else
    9036              :     need_pop = false;
    9037              : 
    9038    503343027 :   if (scope_chain && previous_class_level)
    9039    140444994 :     store_class_bindings (previous_class_level->class_shadowed,
    9040              :                           &s->old_bindings);
    9041              : 
    9042              :   /* Save and clear any IDENTIFIER_BINDING from local scopes.  */
    9043   1106438770 :   for (; b; b = b->level_chain)
    9044              :     {
    9045   1106340437 :       tree t;
    9046              : 
    9047              :       /* We don't need to consider namespace scopes, they don't affect
    9048              :          IDENTIFIER_BINDING.  */
    9049   1106340437 :       if (b->kind == sk_namespace)
    9050              :         {
    9051              :           /* Jump straight to '::'.  */
    9052    503244694 :           b = NAMESPACE_LEVEL (global_namespace);
    9053    503244694 :           break;
    9054              :         }
    9055              : 
    9056    603095743 :       store_bindings (b->names, &s->old_bindings);
    9057              :       /* We also need to check class_shadowed to save class-level type
    9058              :          bindings, since pushclass doesn't fill in b->names.  */
    9059    603095743 :       if (b->kind == sk_class)
    9060    242165400 :         store_class_bindings (b->class_shadowed, &s->old_bindings);
    9061              : 
    9062              :       /* Unwind type-value slots back to top level.  */
    9063   1011731075 :       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
    9064    408635332 :         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
    9065              :     }
    9066              : 
    9067   1585094669 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
    9068   1081751642 :     IDENTIFIER_MARKED (sb->identifier) = 0;
    9069              : 
    9070    503343027 :   s->prev = scope_chain;
    9071    503343027 :   s->bindings = b;
    9072    503343027 :   s->need_pop_function_context = need_pop;
    9073    503343027 :   s->function_decl = current_function_decl;
    9074    503343027 :   s->unevaluated_operand = cp_unevaluated_operand;
    9075    503343027 :   s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
    9076    503343027 :   s->suppress_location_wrappers = suppress_location_wrappers;
    9077    503343027 :   s->x_stmt_tree.stmts_are_full_exprs_p = true;
    9078              : 
    9079    503343027 :   scope_chain = s;
    9080    503343027 :   current_function_decl = NULL_TREE;
    9081    503343027 :   current_lang_base = NULL;
    9082    503343027 :   current_lang_name = lang_name_cplusplus;
    9083    503343027 :   current_namespace = global_namespace;
    9084    503343027 :   push_class_stack ();
    9085    503343027 :   cp_unevaluated_operand = 0;
    9086    503343027 :   c_inhibit_evaluation_warnings = 0;
    9087    503343027 :   suppress_location_wrappers = 0;
    9088    503343027 : }
    9089              : 
    9090              : void
    9091    503217604 : pop_from_top_level (void)
    9092              : {
    9093    503217604 :   struct saved_scope *s = scope_chain;
    9094    503217604 :   cxx_saved_binding *saved;
    9095    503217604 :   size_t i;
    9096              : 
    9097    503217604 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9098              : 
    9099    503217604 :   pop_class_stack ();
    9100              : 
    9101    503217604 :   release_tree_vector (current_lang_base);
    9102              : 
    9103    503217604 :   scope_chain = s->prev;
    9104   1584966432 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
    9105              :     {
    9106   1081748828 :       tree id = saved->identifier;
    9107              : 
    9108   1081748828 :       IDENTIFIER_BINDING (id) = saved->binding;
    9109   1081748828 :       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
    9110              :     }
    9111              : 
    9112              :   /* If we were in the middle of compiling a function, restore our
    9113              :      state.  */
    9114    503217604 :   if (s->need_pop_function_context)
    9115     86709997 :     pop_function_context ();
    9116    503217604 :   current_function_decl = s->function_decl;
    9117    503217604 :   cp_unevaluated_operand = s->unevaluated_operand;
    9118    503217604 :   c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
    9119    503217604 :   suppress_location_wrappers = s->suppress_location_wrappers;
    9120              : 
    9121              :   /* Make this saved_scope structure available for reuse by
    9122              :      push_to_top_level.  */
    9123    503217604 :   s->prev = free_saved_scope;
    9124    503217604 :   free_saved_scope = s;
    9125    503217604 : }
    9126              : 
    9127              : namespace {
    9128              : 
    9129              : /* Helper class for saving/restoring relevant global flags for the
    9130              :    function-local case of maybe_push_to_top_level.  */
    9131              : 
    9132              : struct local_state_t
    9133              : {
    9134              :   int cp_unevaluated_operand;
    9135              :   int c_inhibit_evaluation_warnings;
    9136              :   int cp_noexcept_operand_;
    9137              :   bool has_cfun;
    9138              : 
    9139              :   static local_state_t
    9140      1936051 :   save_and_clear ()
    9141              :   {
    9142      1936051 :     local_state_t s;
    9143      1936051 :     s.cp_unevaluated_operand = ::cp_unevaluated_operand;
    9144      1936051 :     ::cp_unevaluated_operand = 0;
    9145      1936051 :     s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
    9146      1936051 :     ::c_inhibit_evaluation_warnings = 0;
    9147      1936051 :     s.cp_noexcept_operand_ = ::cp_noexcept_operand;
    9148      1936051 :     ::cp_noexcept_operand = 0;
    9149      1936051 :     s.has_cfun = !!cfun;
    9150      1936051 :     if (s.has_cfun)
    9151      1550943 :       push_function_context ();
    9152      1936051 :     return s;
    9153              :   }
    9154              : 
    9155              :   void
    9156      1936051 :   restore () const
    9157              :   {
    9158      1936051 :     ::cp_unevaluated_operand = this->cp_unevaluated_operand;
    9159      1936051 :     ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
    9160      1936051 :     ::cp_noexcept_operand = this->cp_noexcept_operand_;
    9161      1936051 :     if (this->has_cfun)
    9162      1550943 :       pop_function_context ();
    9163      1936051 :   }
    9164              : };
    9165              : 
    9166              : vec<local_state_t> local_state_stack;
    9167              : 
    9168              : } // anon namespace
    9169              : 
    9170              : /* Like push_to_top_level, but not if D is function-local.  Returns whether we
    9171              :    did push to top.  */
    9172              : 
    9173              : bool
    9174     73404181 : maybe_push_to_top_level (tree d)
    9175              : {
    9176              :   /* Push if D isn't function-local, or is a lambda function, for which name
    9177              :      resolution is already done.  */
    9178     73404181 :   const bool push_to_top
    9179     74385454 :     = (LAMBDA_FUNCTION_P (d)
    9180     73302074 :        || (TREE_CODE (d) == TYPE_DECL
    9181     33991028 :            && TREE_TYPE (d)
    9182     67583159 :            && LAMBDA_TYPE_P (TREE_TYPE (d)))
    9183     72537740 :        || !current_function_decl
    9184     96842814 :        || !decl_function_context (d));
    9185              : 
    9186     73404181 :   if (push_to_top)
    9187     71468130 :     push_to_top_level ();
    9188              :   else
    9189              :     {
    9190      1936051 :       gcc_assert (!processing_template_decl);
    9191      1936051 :       local_state_stack.safe_push (local_state_t::save_and_clear ());
    9192              :     }
    9193              : 
    9194     73404181 :   return push_to_top;
    9195              : }
    9196              : 
    9197              : /* Return from whatever maybe_push_to_top_level did.  */
    9198              : 
    9199              : void
    9200     73404172 : maybe_pop_from_top_level (bool push_to_top)
    9201              : {
    9202     73404172 :   if (push_to_top)
    9203     71468121 :     pop_from_top_level ();
    9204              :   else
    9205      1936051 :     local_state_stack.pop ().restore ();
    9206     73404172 : }
    9207              : 
    9208              : /* Push into the scope of the namespace NS, even if it is deeply
    9209              :    nested within another namespace.  */
    9210              : 
    9211              : void
    9212     89864609 : push_nested_namespace (tree ns)
    9213              : {
    9214     89864609 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9215     89864609 :   if (ns == global_namespace)
    9216     43884888 :     push_to_top_level ();
    9217              :   else
    9218              :     {
    9219     89075866 :       push_nested_namespace (CP_DECL_CONTEXT (ns));
    9220     45979721 :       resume_scope (NAMESPACE_LEVEL (ns));
    9221     45979721 :       current_namespace = ns;
    9222              :     }
    9223     89864609 : }
    9224              : 
    9225              : /* Pop back from the scope of the namespace NS, which was previously
    9226              :    entered with push_nested_namespace.  */
    9227              : 
    9228              : void
    9229     43874094 : pop_nested_namespace (tree ns)
    9230              : {
    9231     43874094 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9232    133727909 :   while (ns != global_namespace)
    9233              :     {
    9234     45979721 :       ns = CP_DECL_CONTEXT (ns);
    9235     45979721 :       current_namespace = ns;
    9236     45979721 :       leave_scope ();
    9237              :     }
    9238              : 
    9239     43874094 :   pop_from_top_level ();
    9240     43874094 : }
    9241              : 
    9242              : /* Add TARGET to USINGS, if it does not already exist there.  We used
    9243              :    to build the complete graph of usings at this point, from the POV
    9244              :    of the source namespaces.  Now we build that as we perform the
    9245              :    unqualified search.  */
    9246              : 
    9247              : static void
    9248       196006 : add_using_namespace (vec<tree, va_gc> *&usings, tree target,
    9249              :                      bool imported = false)
    9250              : {
    9251              :   /* Find if this using already exists.  */
    9252       196006 :   tree old = NULL_TREE;
    9253       196006 :   if (usings)
    9254         1595 :     for (tree t : *usings)
    9255         1069 :       if (USING_DECL_DECLS (t) == target)
    9256              :         {
    9257              :           old = t;
    9258              :           break;
    9259              :         }
    9260              : 
    9261       196006 :   tree decl = old;
    9262       196006 :   if (!decl)
    9263              :     {
    9264       195480 :       decl = build_lang_decl (USING_DECL, NULL_TREE, NULL_TREE);
    9265       195480 :       USING_DECL_DECLS (decl) = target;
    9266       195480 :       DECL_MODULE_IMPORT_P (decl) = imported;
    9267              :     }
    9268              : 
    9269              :   /* Update module flags in case that has changed.  */
    9270       196006 :   if (modules_p ())
    9271              :     {
    9272         1053 :       if (module_purview_p ())
    9273          482 :         DECL_MODULE_PURVIEW_P (decl) = true;
    9274         1053 :       if (module_exporting_p ())
    9275          279 :         DECL_MODULE_EXPORT_P (decl) = true;
    9276         1053 :       if (!imported)
    9277          846 :         DECL_MODULE_IMPORT_P (decl) = false;
    9278              :     }
    9279              : 
    9280       196006 :   if (!old)
    9281       195480 :     vec_safe_push (usings, decl);
    9282       196006 : }
    9283              : 
    9284              : /* Convenience overload for the above, taking the user as its first
    9285              :    parameter, for use when importing a using-directive.  */
    9286              : 
    9287              : void
    9288          149 : add_imported_using_namespace (tree ns, tree target)
    9289              : {
    9290          298 :   add_using_namespace (NAMESPACE_LEVEL (ns)->using_directives,
    9291          149 :                        ORIGINAL_NAMESPACE (target),
    9292              :                        /*imported=*/true);
    9293          149 : }
    9294              : 
    9295              : /* Tell the debug system of a using directive.  */
    9296              : 
    9297              : static void
    9298       210773 : emit_debug_info_using_namespace (tree from, tree target, bool implicit)
    9299              : {
    9300              :   /* Emit debugging info.  */
    9301       210773 :   tree context = from != global_namespace ? from : NULL_TREE;
    9302       210773 :   debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
    9303              :                                         implicit);
    9304       210773 : }
    9305              : 
    9306              : /* Process a using directive.  */
    9307              : 
    9308              : void
    9309       194441 : finish_using_directive (tree target, tree attribs)
    9310              : {
    9311       194441 :   if (target == error_mark_node)
    9312              :     return;
    9313              : 
    9314       194421 :   if (current_binding_level->kind != sk_namespace)
    9315       163253 :     add_stmt (build_stmt (input_location, USING_STMT, target));
    9316              :   else
    9317        62336 :     emit_debug_info_using_namespace (current_binding_level->this_entity,
    9318        31168 :                                      ORIGINAL_NAMESPACE (target), false);
    9319              : 
    9320       388842 :   add_using_namespace (current_binding_level->using_directives,
    9321       194421 :                        ORIGINAL_NAMESPACE (target));
    9322              : 
    9323       194421 :   bool diagnosed = false;
    9324       194421 :   if (attribs != error_mark_node)
    9325       194498 :     for (tree a = attribs; a; a = TREE_CHAIN (a))
    9326              :       {
    9327           77 :         tree name = get_attribute_name (a);
    9328           77 :         if (current_binding_level->kind == sk_namespace
    9329           77 :             && is_attribute_p ("strong", name))
    9330              :           {
    9331           12 :             auto_diagnostic_group d;
    9332           18 :             if (warning (0, "%<strong%> using directive no longer supported")
    9333           12 :                 && CP_DECL_CONTEXT (target) == current_namespace)
    9334            6 :               inform (DECL_SOURCE_LOCATION (target),
    9335              :                       "you can use an inline namespace instead");
    9336           12 :           }
    9337           62 :         else if ((flag_openmp || flag_openmp_simd)
    9338            3 :                  && get_attribute_namespace (a) == omp_identifier
    9339           68 :                  && (is_attribute_p ("directive", name)
    9340            0 :                      || is_attribute_p ("sequence", name)
    9341            0 :                      || is_attribute_p ("decl", name)))
    9342              :           {
    9343            3 :             if (!diagnosed)
    9344              :               {
    9345            3 :                 if (tree ar = TREE_VALUE (a))
    9346              :                   {
    9347            3 :                     tree d = TREE_VALUE (ar);
    9348            3 :                     gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
    9349            3 :                     error ("%<omp::%s%> not allowed to be specified in "
    9350              :                            "this context",
    9351            3 :                            TREE_PUBLIC (d) ? "decl" : "directive");
    9352              :                   }
    9353              :                 else
    9354            0 :                   error ("%<omp::%E%> not allowed to be specified in this "
    9355              :                          "context", name);
    9356              :                 diagnosed = true;
    9357              :               }
    9358              :           }
    9359           62 :         else if (annotation_p (a))
    9360            1 :           error ("annotation on using directive");
    9361           61 :         else if (!attribute_ignored_p (a))
    9362           46 :           warning (OPT_Wattributes, "%qD attribute directive ignored", name);
    9363              :       }
    9364              : }
    9365              : 
    9366              : /* Pushes X into the global namespace.  */
    9367              : 
    9368              : tree
    9369       369019 : pushdecl_top_level (tree x)
    9370              : {
    9371       369019 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9372       369019 :   push_to_top_level ();
    9373       369019 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9374       369019 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9375       369019 :   x = pushdecl_namespace_level (x);
    9376       369019 :   pop_from_top_level ();
    9377       738038 :   return x;
    9378       369019 : }
    9379              : 
    9380              : /* Pushes X into the global namespace and calls cp_finish_decl to
    9381              :    register the variable, initializing it with INIT.  */
    9382              : 
    9383              : tree
    9384      2272004 : pushdecl_top_level_and_finish (tree x, tree init)
    9385              : {
    9386      2272004 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9387      2272004 :   push_to_top_level ();
    9388      2272004 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9389      2272004 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9390      2272004 :   x = pushdecl_namespace_level (x);
    9391      2272004 :   cp_finish_decl (x, init, false, NULL_TREE, 0);
    9392      2272004 :   pop_from_top_level ();
    9393      4544008 :   return x;
    9394      2272004 : }
    9395              : 
    9396              : /* Enter the namespaces from current_namerspace to NS.  */
    9397              : 
    9398              : static int
    9399      6398478 : push_inline_namespaces (tree ns)
    9400              : {
    9401      6398478 :   int count = 0;
    9402      6398478 :   if (ns != current_namespace)
    9403              :     {
    9404           18 :       gcc_assert (ns != global_namespace);
    9405           27 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    9406           18 :       resume_scope (NAMESPACE_LEVEL (ns));
    9407           18 :       current_namespace = ns;
    9408           18 :       count++;
    9409              :     }
    9410      6398478 :   return count;
    9411              : }
    9412              : 
    9413              : /* SLOT is the (possibly empty) binding slot for NAME in CTX.
    9414              :    Reuse or create a namespace NAME.  NAME is null for the anonymous
    9415              :    namespace.  */
    9416              : 
    9417              : static tree
    9418         2858 : reuse_namespace (tree *slot, tree ctx, tree name)
    9419              : {
    9420         2858 :   if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
    9421              :     {
    9422              :       /* Public namespace.  Shared.  */
    9423         1057 :       tree *global_slot = slot;
    9424         1057 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    9425          206 :         global_slot = get_fixed_binding_slot (slot, name,
    9426              :                                               BINDING_SLOT_GLOBAL, false);
    9427              : 
    9428         1060 :       for (ovl_iterator iter (*global_slot); iter; ++iter)
    9429              :         {
    9430         1057 :           tree decl = *iter;
    9431              : 
    9432         1057 :           if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
    9433         1054 :             return decl;
    9434              :         }
    9435              :     }
    9436              :   return NULL_TREE;
    9437              : }
    9438              : 
    9439              : static tree
    9440       898342 : make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
    9441              : {
    9442              :   /* Create the namespace.  */
    9443       898342 :   tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
    9444       898342 :   DECL_SOURCE_LOCATION (ns) = loc;
    9445       898342 :   SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
    9446       898342 :   if (!SCOPE_DEPTH (ns))
    9447              :     /* We only allow depth 255. */
    9448            0 :     sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
    9449       898342 :   DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
    9450              : 
    9451       898342 :   if (!name)
    9452              :     /* Anon-namespaces in different header-unit imports are distinct.
    9453              :        But that's ok as their contents all have internal linkage.
    9454              :        (This is different to how they'd behave as textual includes,
    9455              :        but doing this at all is really odd source.)  */
    9456         1451 :     SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
    9457       896891 :   else if (TREE_PUBLIC (ctx))
    9458       896812 :     TREE_PUBLIC (ns) = true;
    9459              : 
    9460       898342 :   if (inline_p)
    9461       178169 :     DECL_NAMESPACE_INLINE_P (ns) = true;
    9462              : 
    9463       898342 :   return ns;
    9464              : }
    9465              : 
    9466              : /* NS was newly created, finish off making it.  */
    9467              : 
    9468              : static void
    9469       898360 : make_namespace_finish (tree ns, tree *slot, bool from_import = false)
    9470              : {
    9471       898360 :   if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
    9472              :     {
    9473              :       /* Merge into global slot.  */
    9474         1749 :       tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
    9475              :                                             BINDING_SLOT_GLOBAL, true);
    9476         1749 :       *gslot = ns;
    9477              :     }
    9478              : 
    9479       898360 :   tree ctx = CP_DECL_CONTEXT (ns);
    9480       898360 :   cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
    9481       898360 :   scope->this_entity = ns;
    9482       898360 :   scope->more_cleanups_ok = true;
    9483       898360 :   scope->kind = sk_namespace;
    9484       898360 :   scope->level_chain = NAMESPACE_LEVEL (ctx);
    9485       898360 :   NAMESPACE_LEVEL (ns) = scope;
    9486              : 
    9487       898360 :   if (DECL_NAMESPACE_INLINE_P (ns))
    9488       178169 :     vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
    9489              : 
    9490       898360 :   if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
    9491       179605 :     emit_debug_info_using_namespace (ctx, ns, true);
    9492              : 
    9493              :   /* An unnamed namespace implicitly has a using-directive inserted so
    9494              :      that its contents are usable in the surrounding context.  */
    9495       898360 :   if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
    9496         1436 :     add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns,
    9497              :                          from_import);
    9498       898360 : }
    9499              : 
    9500              : /* NS is a possibly-imported namespace that is now needed for
    9501              :    a declaration.  Add it to the current TU's binding slot.  */
    9502              : 
    9503              : void
    9504        30429 : expose_existing_namespace (tree ns)
    9505              : {
    9506        30429 :   if (!modules_p ())
    9507              :     return;
    9508              : 
    9509        30429 :   tree bind = *find_namespace_slot (CP_DECL_CONTEXT (ns), DECL_NAME (ns));
    9510        30429 :   if (bind != ns)
    9511              :     {
    9512         1227 :       auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
    9513         1227 :       binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
    9514         1227 :       gcc_checking_assert (!(tree)slot || (tree)slot == ns);
    9515         1227 :       slot = ns;
    9516              :     }
    9517              : 
    9518        30429 :   if (module_purview_p ())
    9519        13768 :     DECL_MODULE_PURVIEW_P (ns) = true;
    9520              : }
    9521              : 
    9522              : /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE,
    9523              :    then we enter an anonymous namespace.  If MAKE_INLINE is true, then
    9524              :    we create an inline namespace (it is up to the caller to check upon
    9525              :    redefinition). Return the number of namespaces entered.  */
    9526              : 
    9527              : int
    9528      7295049 : push_namespace (tree name, bool make_inline)
    9529              : {
    9530      7295049 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9531      7295049 :   int count = 0;
    9532              : 
    9533              :   /* We should not get here if the global_namespace is not yet constructed
    9534              :      nor if NAME designates the global namespace:  The global scope is
    9535              :      constructed elsewhere.  */
    9536      7295049 :   gcc_checking_assert (global_namespace != NULL && name != global_identifier);
    9537              : 
    9538      7295049 :   tree ns = NULL_TREE;
    9539      7295049 :   {
    9540      7295049 :     name_lookup lookup (name);
    9541      7295049 :     if (!lookup.search_qualified (current_namespace, /*usings=*/false))
    9542              :       ;
    9543      6398475 :     else if (TREE_CODE (lookup.value) == TREE_LIST)
    9544              :       {
    9545              :         /* An ambiguous lookup.  If exactly one is a namespace, we
    9546              :            want that.  If more than one is a namespace, error, but
    9547              :            pick one of them.  */
    9548              :         /* DR2061 can cause us to find multiple namespaces of the same
    9549              :            name.  We must treat that carefully and avoid thinking we
    9550              :            need to push a new (possibly) duplicate namespace.  Hey,
    9551              :            if you want to use the same identifier within an inline
    9552              :            nest, knock yourself out.  */
    9553            9 :         for (tree *chain = &lookup.value, next; (next = *chain);)
    9554              :           {
    9555            6 :             tree decl = TREE_VALUE (next);
    9556            6 :             if (TREE_CODE (decl) == NAMESPACE_DECL)
    9557              :               {
    9558            6 :                 if (!ns)
    9559              :                   ns = decl;
    9560            3 :                 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
    9561            6 :                   ns = decl;
    9562              : 
    9563              :                 /* Advance.  */
    9564            6 :                 chain = &TREE_CHAIN (next);
    9565              :               }
    9566              :             else
    9567              :               /* Stitch out.  */
    9568            0 :               *chain = TREE_CHAIN (next);
    9569              :           }
    9570              : 
    9571            3 :         if (TREE_CHAIN (lookup.value))
    9572              :           {
    9573            3 :             error ("%<namespace %E%> is ambiguous", name);
    9574            3 :             print_candidates (input_location, lookup.value);
    9575              :           }
    9576              :       }
    9577      6398472 :     else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    9578              :       ns = lookup.value;
    9579              : 
    9580      6398463 :     if (ns)
    9581      6398463 :       if (tree dna = DECL_NAMESPACE_ALIAS (ns))
    9582              :         {
    9583              :           /* A namespace alias is not allowed here, but if the alias
    9584              :              is for a namespace also inside the current scope,
    9585              :              accept it with a diagnostic.  That's better than dying
    9586              :              horribly.  */
    9587            9 :           if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
    9588              :             {
    9589            6 :               error ("namespace alias %qD not allowed here, "
    9590              :                      "assuming %qD", ns, dna);
    9591            6 :               ns = dna;
    9592              :             }
    9593              :           else
    9594              :             ns = NULL_TREE;
    9595              :         }
    9596      7295049 :   }
    9597              : 
    9598      7295049 :   if (ns)
    9599              :     {
    9600              :       /* DR2061.  NS might be a member of an inline namespace.  We
    9601              :          need to push into those namespaces.  */
    9602      6398460 :       if (modules_p ())
    9603        60080 :         for (tree ctx = ns; ctx != current_namespace;
    9604        23339 :              ctx = CP_DECL_CONTEXT (ctx))
    9605        23339 :           expose_existing_namespace (ctx);
    9606              : 
    9607      6398460 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    9608      6398460 :       if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
    9609              :         /* It's not builtin now.  */
    9610        34877 :         DECL_SOURCE_LOCATION (ns) = input_location;
    9611              :     }
    9612              :   else
    9613              :     {
    9614              :       /* Before making a new namespace, see if we already have one in
    9615              :          the existing partitions of the current namespace.  */
    9616       896589 :       tree *slot = find_namespace_slot (current_namespace, name, false);
    9617       896589 :       if (slot)
    9618           51 :         ns = reuse_namespace (slot, current_namespace, name);
    9619       896589 :       if (!ns)
    9620       896559 :         ns = make_namespace (current_namespace, name,
    9621              :                              input_location, make_inline);
    9622              : 
    9623       896589 :       if (pushdecl (ns) == error_mark_node)
    9624              :         ns = NULL_TREE;
    9625              :       else
    9626              :         {
    9627              :           /* Finish up making the namespace.  */
    9628       896577 :           add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
    9629       896577 :           if (!slot)
    9630              :             {
    9631       896538 :               slot = find_namespace_slot (current_namespace, name);
    9632              :               /* This should find the slot created by pushdecl.  */
    9633       896538 :               gcc_checking_assert (slot && *slot == ns);
    9634              :             }
    9635              :           else
    9636              :             {
    9637              :               /* pushdecl could have expanded the hash table, so
    9638              :                  slot might be invalid.  */
    9639           39 :               slot = find_namespace_slot (current_namespace, name);
    9640           39 :               gcc_checking_assert (slot);
    9641              :             }
    9642       896577 :           make_namespace_finish (ns, slot);
    9643              :         }
    9644              :     }
    9645              : 
    9646      7295037 :   if (ns)
    9647              :     {
    9648              :       /* A public namespace is exported only if explicitly marked, or
    9649              :          it contains exported entities.  */
    9650      7295037 :       if (module_exporting_p ())
    9651              :         {
    9652        11301 :           if (TREE_PUBLIC (ns))
    9653        11258 :             DECL_MODULE_EXPORT_P (ns) = true;
    9654           43 :           else if (!header_module_p ())
    9655              :             {
    9656           12 :               if (name)
    9657              :                 {
    9658            6 :                   auto_diagnostic_group d;
    9659            6 :                   error_at (input_location, "exporting namespace %qD with "
    9660              :                             "internal linkage", ns);
    9661            6 :                   inform (input_location, "%qD has internal linkage because "
    9662              :                           "it was declared in an unnamed namespace", ns);
    9663            6 :                 }
    9664              :               else
    9665            6 :                 error_at (input_location, "exporting unnamed namespace");
    9666              :             }
    9667              :         }
    9668      7295037 :       if (module_purview_p ())
    9669        12558 :         DECL_MODULE_PURVIEW_P (ns) = true;
    9670              : 
    9671      7617650 :       if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
    9672              :         {
    9673            3 :           auto_diagnostic_group d;
    9674            3 :           error_at (input_location,
    9675              :                     "inline namespace must be specified at initial definition");
    9676            3 :           inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
    9677            3 :         }
    9678      7295037 :       resume_scope (NAMESPACE_LEVEL (ns));
    9679      7295037 :       current_namespace = ns;
    9680      7295037 :       count++;
    9681              :     }
    9682              : 
    9683     14590098 :   return count;
    9684      7295049 : }
    9685              : 
    9686              : /* Pop from the scope of the current namespace.  */
    9687              : 
    9688              : void
    9689      7349402 : pop_namespace (void)
    9690              : {
    9691      7349402 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9692              : 
    9693      7349402 :   gcc_assert (current_namespace != global_namespace);
    9694      7349402 :   current_namespace = CP_DECL_CONTEXT (current_namespace);
    9695              :   /* The binding level is not popped, as it might be re-opened later.  */
    9696      7349402 :   leave_scope ();
    9697      7349402 : }
    9698              : 
    9699              : /* An IMPORT is an import that is defining namespace NAME inside CTX.  Find or
    9700              :    create that namespace and add it to the container's binding-vector.   */
    9701              : 
    9702              : tree
    9703         2807 : add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
    9704              :                         bool inline_p, bool visible_p)
    9705              : {
    9706              :   // FIXME: Something is not correct about the VISIBLE_P handling.  We
    9707              :   // need to insert this namespace into
    9708              :   // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
    9709              :   // (b) The importing module's slot (always)
    9710              :   // (c) Do we need to put it in the CURRENT slot?  This is the
    9711              :   // confused piece.
    9712              : 
    9713         2807 :   tree *slot = find_namespace_slot (ctx, name, true);
    9714         2807 :   tree decl = reuse_namespace (slot, ctx, name);
    9715              : 
    9716              :   /* Creating and binding.  */
    9717         2807 :   if (!decl)
    9718              :     {
    9719         1783 :       decl = make_namespace (ctx, name, loc, inline_p);
    9720         1783 :       make_namespace_finish (decl, slot, true);
    9721              :     }
    9722         1024 :   else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
    9723              :     {
    9724            0 :       auto_diagnostic_group d;
    9725            0 :       error_at (loc, "%s namespace %qD conflicts with reachable definition",
    9726              :                 inline_p ? "inline" : "non-inline", decl);
    9727            0 :       inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
    9728              :               inline_p ? "non-inline" : "inline");
    9729            0 :     }
    9730              : 
    9731         2807 :   if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
    9732              :     {
    9733              :       /* See if we can extend the final slot.  */
    9734         1895 :       binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
    9735         1895 :       gcc_checking_assert (last->indices[0].span);
    9736              :       unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
    9737              : 
    9738         3614 :       while (--jx)
    9739         1895 :         if (last->indices[jx].span)
    9740              :           break;
    9741         1895 :       tree final = last->slots[jx];
    9742         1895 :       if (visible_p == !STAT_HACK_P (final)
    9743         1575 :           && MAYBE_STAT_DECL (final) == decl
    9744          143 :           && last->indices[jx].base + last->indices[jx].span == import
    9745         2035 :           && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
    9746              :               || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
    9747              :                   && jx >= BINDING_SLOTS_FIXED)))
    9748              :         {
    9749          140 :           last->indices[jx].span++;
    9750          140 :           return decl;
    9751              :         }
    9752              :     }
    9753              : 
    9754              :   /* Append a new slot.  */
    9755         2667 :   tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
    9756              : 
    9757         2667 :   gcc_assert (!*mslot);
    9758         2667 :   *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
    9759              : 
    9760         2667 :   return decl;
    9761              : }
    9762              : 
    9763              : /* Pop off extraneous binding levels left over due to syntax errors.
    9764              :    We don't pop past namespaces, as they might be valid.  */
    9765              : 
    9766              : void
    9767        96815 : pop_everything (void)
    9768              : {
    9769        96815 :   if (ENABLE_SCOPE_CHECKING)
    9770              :     verbatim ("XXX entering %<pop_everything ()%>");
    9771        96833 :   while (!namespace_bindings_p ())
    9772              :     {
    9773           18 :       if (current_binding_level->kind == sk_class)
    9774            0 :         pop_nested_class ();
    9775              :       else
    9776           18 :         poplevel (0, 0, 0);
    9777              :     }
    9778        96815 :   if (ENABLE_SCOPE_CHECKING)
    9779              :     verbatim ("XXX leaving %<pop_everything ()%>");
    9780        96815 : }
    9781              : 
    9782              : /* Emit debugging information for using declarations and directives.
    9783              :    If input tree is overloaded fn then emit debug info for all
    9784              :    candidates.  */
    9785              : 
    9786              : void
    9787      8644824 : cp_emit_debug_info_for_using (tree t, tree context)
    9788              : {
    9789              :   /* Don't try to emit any debug information if we have errors.  */
    9790      8644824 :   if (seen_error ())
    9791              :     return;
    9792              : 
    9793              :   /* Do not supply context to imported_module_or_decl, if
    9794              :      it is a global namespace.  */
    9795      8640921 :   if (context == global_namespace)
    9796       175616 :     context = NULL_TREE;
    9797              : 
    9798      8640921 :   t = MAYBE_BASELINK_FUNCTIONS (t);
    9799              : 
    9800     18789093 :   for (lkp_iterator iter (t); iter; ++iter)
    9801              :     {
    9802     10148172 :       tree fn = *iter;
    9803              : 
    9804     10148172 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    9805              :         /* FIXME: Handle TEMPLATE_DECLs.  */
    9806       830488 :         continue;
    9807              : 
    9808              :       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
    9809              :          of a builtin function.  */
    9810     12147375 :       if (TREE_CODE (fn) == FUNCTION_DECL
    9811      7713936 :           && DECL_EXTERNAL (fn)
    9812     17022105 :           && fndecl_built_in_p (fn))
    9813      2829691 :         continue;
    9814              : 
    9815      6487993 :       if (building_stmt_list_p ())
    9816         1213 :         add_stmt (build_stmt (input_location, USING_STMT, fn));
    9817              :       else
    9818      6486780 :         debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
    9819              :                                               false, false);
    9820              :     }
    9821              : }
    9822              : 
    9823              : /* True if D is a local declaration in dependent scope.  Assumes that it is
    9824              :    (part of) the current lookup result for its name.  */
    9825              : 
    9826              : bool
    9827     61145014 : dependent_local_decl_p (tree d)
    9828              : {
    9829     61145014 :   if (!DECL_LOCAL_DECL_P (d))
    9830              :     return false;
    9831              : 
    9832        39205 :   cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
    9833        39205 :   cp_binding_level *l = b->scope;
    9834        99488 :   while (!l->this_entity)
    9835        60283 :     l = l->level_chain;
    9836        39205 :   return uses_template_parms (l->this_entity);
    9837              : }
    9838              : 
    9839              : 
    9840              : 
    9841              : #include "gt-cp-name-lookup.h"
        

Generated by: LCOV version 2.4-beta

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