LCOV - code coverage report
Current view: top level - gcc/cp - name-lookup.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.5 % 4217 3986
Test Date: 2026-03-28 14:25:54 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       388672 : stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
      65              : {
      66       388672 :   tree result = make_node (OVERLOAD);
      67              : 
      68              :   /* Mark this as a lookup, so we can tell this is a stat hack.  */
      69       388672 :   OVL_LOOKUP_P (result) = true;
      70       388672 :   STAT_DECL (result) = decl;
      71       388672 :   STAT_TYPE (result) = type;
      72       388672 :   return result;
      73              : }
      74              : 
      75              : /* Create a local binding level for NAME.  */
      76              : 
      77              : static cxx_binding *
      78    925782178 : create_local_binding (cp_binding_level *level, tree name)
      79              : {
      80    925782178 :   cxx_binding *binding = cxx_binding_make (NULL, NULL);
      81              : 
      82    925782178 :   LOCAL_BINDING_P (binding) = true;
      83    925782178 :   binding->scope = level;
      84    925782178 :   binding->previous = IDENTIFIER_BINDING (name);
      85              : 
      86    925782178 :   IDENTIFIER_BINDING (name) = binding;
      87              : 
      88    925782178 :   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  11528006889 : find_namespace_slot (tree ns, tree name, bool create_p = false)
      96              : {
      97  11528006889 :   tree *slot = DECL_NAMESPACE_BINDINGS (ns)
      98  22664366082 :     ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
      99              :                            create_p ? INSERT : NO_INSERT);
     100  11528006889 :   return slot;
     101              : }
     102              : 
     103              : static tree
     104        59514 : find_namespace_value (tree ns, tree name)
     105              : {
     106        59514 :   tree *b = find_namespace_slot (ns, name);
     107              : 
     108        59514 :   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       104380 : search_imported_binding_slot (tree *slot, unsigned ix)
     118              : {
     119       104380 :   gcc_assert (ix);
     120              : 
     121       104380 :   if (!*slot)
     122              :     return NULL;
     123              : 
     124       104380 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
     125              :     return NULL;
     126              : 
     127       104380 :   unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     128       104380 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
     129              : 
     130       104380 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     131              :     {
     132       104380 :       clusters--;
     133       104380 :       cluster++;
     134              :     }
     135              : 
     136       211838 :   while (clusters > 1)
     137              :     {
     138         3078 :       unsigned half = clusters / 2;
     139         3078 :       gcc_checking_assert (cluster[half].indices[0].span);
     140         3078 :       if (cluster[half].indices[0].base > ix)
     141              :         clusters = half;
     142              :       else
     143              :         {
     144          817 :           clusters -= half;
     145          817 :           cluster += half;
     146              :         }
     147              :     }
     148              : 
     149       104380 :   if (clusters)
     150              :     /* Is it in this cluster?  */
     151       207949 :     for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     152              :       {
     153       207949 :         if (!cluster->indices[off].span)
     154              :           break;
     155       207949 :         if (cluster->indices[off].base > ix)
     156              :           break;
     157              : 
     158       207949 :         if (cluster->indices[off].base + cluster->indices[off].span > ix)
     159       104380 :           return &cluster->slots[off];
     160              :       }
     161              : 
     162              :   return NULL;
     163              : }
     164              : 
     165              : static void
     166        95379 : init_global_partition (binding_cluster *cluster, tree decl)
     167              : {
     168        95379 :   bool named = true;
     169              : 
     170        95379 :   if (header_module_p ())
     171              :     named = false;
     172        95301 :   else if (TREE_PUBLIC (decl)
     173        52108 :            && TREE_CODE (decl) == NAMESPACE_DECL
     174        96135 :            && !DECL_NAMESPACE_ALIAS (decl))
     175              :     named = false;
     176        94474 :   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        95379 :     mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
     187              : 
     188        95379 :   if (*mslot)
     189        20596 :     decl = ovl_make (decl, *mslot);
     190        95379 :   *mslot = decl;
     191              : 
     192        95379 :   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        95379 : }
     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    387773694 : get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
     211              : {
     212    387773694 :   gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
     213              : 
     214              :   /* An assumption is that the fixed slots all reside in one cluster.  */
     215    387773694 :   gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
     216              : 
     217    387773694 :   if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
     218              :     {
     219    387642685 :       if (ix == BINDING_SLOT_CURRENT)
     220              :         /* The current TU can just use slot directly.  */
     221              :         return slot;
     222              : 
     223       184577 :       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       184577 :       bool partition_slot = true;
     230       184577 :       unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
     231              :                         + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
     232              :                        / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     233       184577 :       tree new_vec = make_binding_vec (name, want);
     234       184577 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
     235       184577 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
     236              : 
     237              :       /* Initialize the fixed slots.  */
     238       553731 :       for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
     239              :         {
     240       369154 :           cluster[0].indices[jx].base = 0;
     241       369154 :           cluster[0].indices[jx].span = 1;
     242       369154 :           cluster[0].slots[jx] = NULL_TREE;
     243              :         }
     244              : 
     245       184577 :       if (partition_slot)
     246              :         {
     247       184577 :           unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     248       184577 :           unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
     249       184577 :           cluster[ind].indices[off].base = 0;
     250       184577 :           cluster[ind].indices[off].span = 1;
     251       184577 :           cluster[ind].slots[off] = NULL_TREE;
     252              :         }
     253              : 
     254       184577 :       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        74783 :           if (tree type = strip_using_decl (MAYBE_STAT_TYPE (orig)))
     261           17 :             init_global_partition (cluster, type);
     262              : 
     263       170145 :           for (ovl_iterator iter (strip_using_decl (MAYBE_STAT_DECL (orig)));
     264       170145 :                iter; ++iter)
     265              :             {
     266        95362 :               tree decl = *iter;
     267              : 
     268              :               /* Internal linkage entities are in deduplicateable.  */
     269        95362 :               init_global_partition (cluster, decl);
     270              :             }
     271              : 
     272        74783 :           if (cluster[0].slots[BINDING_SLOT_GLOBAL]
     273        74783 :               && !(TREE_CODE (orig) == NAMESPACE_DECL
     274        75631 :                    && !DECL_NAMESPACE_ALIAS (orig)))
     275              :             {
     276              :               /* Note that we had some GMF entries.  */
     277        73942 :               if (!STAT_HACK_P (orig))
     278        73915 :                 orig = stat_hack (orig);
     279              : 
     280        73942 :               MODULE_BINDING_GLOBAL_P (orig) = true;
     281              :             }
     282              : 
     283        74783 :           cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
     284              :         }
     285              : 
     286       184577 :       *slot = new_vec;
     287       184577 :     }
     288              :   else
     289       131009 :     gcc_checking_assert (create >= 0);
     290              : 
     291       315586 :   unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     292       315586 :   binding_cluster &cluster
     293       315586 :     = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     294              : 
     295              :   /* There must always be slots for these indices  */
     296       315586 :   gcc_checking_assert (cluster.indices[off].span == 1
     297              :                        && !cluster.indices[off].base
     298              :                        && !cluster.slots[off].is_lazy ());
     299              : 
     300       315586 :   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       184810 : append_imported_binding_slot (tree *slot, tree name, unsigned ix)
     308              : {
     309       184810 :   gcc_checking_assert (ix);
     310              : 
     311       184810 :   if (!*slot ||  TREE_CODE (*slot) != BINDING_VECTOR)
     312              :     /* Make an initial module vector.  */
     313       171295 :     get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
     314        13515 :   else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
     315        13515 :            ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
     316              :     /* There is space in the last cluster.  */;
     317        11904 :   else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
     318        11904 :            != 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        11904 :       unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     325        11904 :       unsigned want = (have * 3 + 1) / 2;
     326              : 
     327        11904 :       if (want > (unsigned short)~0)
     328              :         want = (unsigned short)~0;
     329              : 
     330        11904 :       tree new_vec = make_binding_vec (name, want);
     331        11904 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
     332        23808 :       BINDING_VECTOR_INTERNAL_DECLS (new_vec)
     333        11904 :         = BINDING_VECTOR_INTERNAL_DECLS (*slot);
     334        23808 :       BINDING_VECTOR_GLOBAL_DUPS_P (new_vec)
     335        11904 :         = BINDING_VECTOR_GLOBAL_DUPS_P (*slot);
     336        23808 :       BINDING_VECTOR_PARTITION_DUPS_P (new_vec)
     337        11904 :         = BINDING_VECTOR_PARTITION_DUPS_P (*slot);
     338        23808 :       memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
     339        23808 :               BINDING_VECTOR_CLUSTER_BASE (*slot),
     340        11904 :               have * sizeof (binding_cluster));
     341        11904 :       *slot = new_vec;
     342              :     }
     343              : 
     344       184810 :   binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
     345       357716 :   for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     346       357716 :     if (!last->indices[off].span)
     347              :       {
     348              :         /* Fill the free slot of the cluster.  */
     349       184810 :         last->indices[off].base = ix;
     350       184810 :         last->indices[off].span = 1;
     351       184810 :         last->slots[off] = NULL_TREE;
     352              :         /* Check monotonicity.  */
     353       184810 :         gcc_checking_assert (last[off ? 0 : -1]
     354              :                              .indices[off ? off - 1
     355              :                                       : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
     356              :                              .base < ix);
     357       184810 :         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   1335508967 : add_decl_to_level (cp_binding_level *b, tree decl)
     367              : {
     368   1335508967 :   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   1335508967 :   gcc_assert (b->names != decl);
     375              : 
     376              :   /* We build up the list in reverse order, and reverse it later if
     377              :      necessary.  */
     378   1335508967 :   TREE_CHAIN (decl) = b->names;
     379   1335508967 :   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   1335508967 :   if (b->kind == sk_namespace
     385   1335508967 :       && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     386    366756665 :           || (TREE_CODE (decl) == FUNCTION_DECL
     387    302084119 :               && (!TREE_PUBLIC (decl)
     388    301969786 :                   || decl_internal_context_p (decl)
     389    301968610 :                   || DECL_DECLARED_INLINE_P (decl)))))
     390     17214149 :     vec_safe_push (static_decls, decl);
     391   1335508967 : }
     392              : 
     393              : /* Find the binding for NAME in the local binding level B.  */
     394              : 
     395              : static cxx_binding *
     396    952051200 : find_local_binding (cp_binding_level *b, tree name)
     397              : {
     398    952051200 :   if (cxx_binding *binding = IDENTIFIER_BINDING (name))
     399            0 :     for (;; b = b->level_chain)
     400              :       {
     401      5742386 :         if (binding->scope == b)
     402              :           return binding;
     403              : 
     404              :         /* Cleanup contours are transparent to the language.  */
     405      5739214 :         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   1823304017 :   name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
     443   1823304017 :   : name (n), value (NULL_TREE), type (NULL_TREE),
     444   1823304017 :     want (w), tentative (false),
     445   1823304017 :     deduping (false), scopes (NULL), previous (NULL)
     446              :   {
     447   1823304017 :     preserve_state ();
     448              :   }
     449   1823304017 :   ~name_lookup ()
     450              :   {
     451   1823304017 :     gcc_checking_assert (!deduping);
     452   1823304017 :     restore_state ();
     453   1823304017 :   }
     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   1878516272 :   void dedup (bool state)
     462              :   {
     463   1878516272 :     if (deduping != state)
     464              :       {
     465    130350524 :         deduping = state;
     466    130350524 :         lookup_mark (value, state);
     467              :       }
     468   1878516272 :   }
     469              : 
     470              : protected:
     471  23656489642 :   static bool seen_p (tree scope)
     472              :   {
     473  23656489642 :     return LOOKUP_SEEN_P (scope);
     474              :   }
     475     76279578 :   static bool found_p (tree scope)
     476              :   {
     477     76279578 :     return LOOKUP_FOUND_P (scope);
     478              :   }
     479              : 
     480              :   void mark_seen (tree scope); /* Mark and add to scope vector. */
     481    483445995 :   static void mark_found (tree scope)
     482              :   {
     483    483445995 :     gcc_checking_assert (seen_p (scope));
     484    483445995 :     LOOKUP_FOUND_P (scope) = true;
     485    483445995 :   }
     486  11538946816 :   bool see_and_mark (tree scope)
     487              :   {
     488  11538946816 :     bool ret = seen_p (scope);
     489  11538946816 :     if (!ret)
     490   2553034241 :       mark_seen (scope);
     491  11206384449 :     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   1823304017 : name_lookup::preserve_state ()
     561              : {
     562   1823304017 :   previous = active;
     563   1823304017 :   if (previous)
     564              :     {
     565         1610 :       unsigned length = vec_safe_length (previous->scopes);
     566         1610 :       vec_safe_reserve (previous->scopes, length);
     567         7799 :       for (unsigned ix = length; ix--;)
     568              :         {
     569         6189 :           tree decl = (*previous->scopes)[ix];
     570              : 
     571         6189 :           gcc_checking_assert (LOOKUP_SEEN_P (decl));
     572         6189 :           LOOKUP_SEEN_P (decl) = false;
     573              : 
     574              :           /* Preserve the FOUND_P state on the interrupted lookup's
     575              :              stack.  */
     576         6189 :           if (LOOKUP_FOUND_P (decl))
     577              :             {
     578          946 :               LOOKUP_FOUND_P (decl) = false;
     579          946 :               previous->scopes->quick_push (decl);
     580              :             }
     581              :         }
     582              : 
     583         1610 :       tentative = previous->tentative;
     584              : 
     585              :       /* Unmark the outer partial lookup.  */
     586         1610 :       if (previous->deduping)
     587           12 :         lookup_mark (previous->value, false);
     588              :     }
     589              :   else
     590   1823302407 :     scopes = shared_scopes;
     591   1823304017 :   active = this;
     592   1823304017 : }
     593              : 
     594              : /* Restore the marking state of a lookup we interrupted.  */
     595              : 
     596              : void
     597   1823304017 : name_lookup::restore_state ()
     598              : {
     599   1823304017 :   gcc_checking_assert (!deduping);
     600              : 
     601              :   /* Unmark and empty this lookup's scope stack.  */
     602  14852992483 :   for (unsigned ix = vec_safe_length (scopes); ix--;)
     603              :     {
     604  11206384449 :       tree decl = scopes->pop ();
     605  11206384449 :       gcc_checking_assert (LOOKUP_SEEN_P (decl));
     606  11206384449 :       LOOKUP_SEEN_P (decl) = false;
     607  11206384449 :       LOOKUP_FOUND_P (decl) = false;
     608              :     }
     609              : 
     610   1823304017 :   active = previous;
     611   1823304017 :   if (previous)
     612              :     {
     613         1610 :       free (scopes);
     614              : 
     615         1610 :       unsigned length = vec_safe_length (previous->scopes);
     616         7799 :       for (unsigned ix = 0; ix != length; ix++)
     617              :         {
     618         6908 :           tree decl = (*previous->scopes)[ix];
     619         6908 :           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          946 :               do
     624              :                 {
     625          946 :                   tree decl = previous->scopes->pop ();
     626          946 :                   gcc_checking_assert (LOOKUP_SEEN_P (decl)
     627              :                                        && !LOOKUP_FOUND_P (decl));
     628          946 :                   LOOKUP_FOUND_P (decl) = true;
     629              :                 }
     630          946 :               while (++ix != length);
     631              :               break;
     632              :             }
     633              : 
     634         6189 :           gcc_checking_assert (!LOOKUP_FOUND_P (decl));
     635         6189 :           LOOKUP_SEEN_P (decl) = true;
     636              :         }
     637              : 
     638              :       /* Remark the outer partial lookup.  */
     639         1610 :       if (previous->deduping)
     640           12 :         lookup_mark (previous->value, true);
     641              :     }
     642              :   else
     643   1823302407 :     shared_scopes = scopes;
     644   1823304017 : }
     645              : 
     646              : void
     647  11206384449 : name_lookup::mark_seen (tree scope)
     648              : {
     649  11206384449 :   gcc_checking_assert (!seen_p (scope));
     650  11206384449 :   LOOKUP_SEEN_P (scope) = true;
     651  11206384449 :   vec_safe_push (scopes, scope);
     652  11206384449 : }
     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          698 : name_lookup::ambiguous (tree thing, tree current)
     672              : {
     673          698 :   if (TREE_CODE (current) != TREE_LIST)
     674              :     {
     675          529 :       current = build_tree_list (NULL_TREE, current);
     676          529 :       TREE_TYPE (current) = error_mark_node;
     677              :     }
     678          698 :   current = tree_cons (NULL_TREE, thing, current);
     679          698 :   TREE_TYPE (current) = error_mark_node;
     680              : 
     681          698 :   return current;
     682              : }
     683              : 
     684              : /* FNS is a new overload set to add to the exising set.  */
     685              : 
     686              : void
     687    667057293 : name_lookup::add_overload (tree fns)
     688              : {
     689    667057293 :   if (!deduping && TREE_CODE (fns) == OVERLOAD)
     690              :     {
     691    461223900 :       tree probe = fns;
     692    461223900 :       if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     693    461028437 :         probe = ovl_skip_hidden (probe);
     694    461223900 :       if (probe && TREE_CODE (probe) == OVERLOAD
     695    922447800 :           && OVL_DEDUP_P (probe))
     696              :         /* We're about to add something found by multiple paths, so need to
     697              :            engage deduping mode.  */
     698     30557816 :         dedup (true);
     699              :     }
     700              : 
     701    667057293 :   value = lookup_maybe_add (fns, value, deduping);
     702    667057293 : }
     703              : 
     704              : /* Add a NEW_VAL, a found value binding into the current value binding.  */
     705              : 
     706              : void
     707   1466533841 : name_lookup::add_value (tree new_val)
     708              : {
     709   1466533841 :   if (OVL_P (new_val) && (!value || OVL_P (value)))
     710    596764951 :     add_overload (new_val);
     711    869768890 :   else if (!value)
     712    869533528 :     value = new_val;
     713       235362 :   else if (value == new_val)
     714              :     ;
     715        12934 :   else if ((TREE_CODE (value) == TYPE_DECL
     716        12769 :             && TREE_CODE (new_val) == TYPE_DECL
     717        25700 :             && 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   1466533841 : }
     732              : 
     733              : /* Add a NEW_TYPE, a found type binding into the current type binding.  */
     734              : 
     735              : void
     736       411614 : name_lookup::add_type (tree new_type)
     737              : {
     738       411614 :   if (!type)
     739       411611 :     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       411614 : }
     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   1488747458 : name_lookup::process_binding (tree new_val, tree new_type)
     751              : {
     752              :   /* Did we really see a type? */
     753   1488747458 :   if (new_type
     754   1488747458 :       && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
     755              :     new_type = NULL_TREE;
     756              : 
     757   1488747458 :   new_val = strip_using_decl (new_val);
     758   1488747458 :   new_type = strip_using_decl (new_type);
     759              : 
     760              :   /* Do we really see a value? */
     761   1488747458 :   if (new_val)
     762   1466582066 :     switch (TREE_CODE (new_val))
     763              :       {
     764    260804823 :       case TEMPLATE_DECL:
     765              :         /* If we expect types or namespaces, and not templates,
     766              :            or this is not a template class.  */
     767    260804823 :         if (bool (want & LOOK_want::TYPE_NAMESPACE)
     768    260804823 :             && !DECL_TYPE_TEMPLATE_P (new_val))
     769              :           new_val = NULL_TREE;
     770              :         break;
     771    256343663 :       case TYPE_DECL:
     772    256343663 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
     773    256343663 :             || (new_type && bool (want & LOOK_want::TYPE)))
     774              :           new_val = NULL_TREE;
     775              :         break;
     776    261738798 :       case NAMESPACE_DECL:
     777    261738798 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
     778              :           new_val = NULL_TREE;
     779              :         break;
     780    687694782 :       default:
     781    687694782 :         if (bool (want & LOOK_want::TYPE_NAMESPACE))
     782              :           new_val = NULL_TREE;
     783              :       }
     784              : 
     785              :   if (!new_val)
     786              :     {
     787     22261762 :       new_val = new_type;
     788     22261762 :       new_type = NULL_TREE;
     789              :     }
     790              : 
     791              :   /* Merge into the lookup  */
     792     22261762 :   if (new_val)
     793   1466533841 :     add_value (new_val);
     794   1488747458 :   if (new_type)
     795       411614 :     add_type (new_type);
     796              : 
     797   1488747458 :   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        81494 : 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        81494 :   if (new_val && !new_type
     811        76728 :       && TREE_CODE (new_val) == NAMESPACE_DECL
     812        28206 :       && TREE_PUBLIC (new_val)
     813       109694 :       && !DECL_NAMESPACE_ALIAS (new_val))
     814              :     {
     815        28170 :       if (marker & 2)
     816              :         return marker;
     817        14671 :       marker |= 2;
     818              :     }
     819              : 
     820        67995 :   if (new_type || new_val)
     821        63304 :     marker |= process_binding (new_val, new_type);
     822              : 
     823              :   return marker;
     824              : }
     825              : 
     826              : /* Look in exactly namespace SCOPE.  */
     827              : 
     828              : bool
     829  10844427402 : name_lookup::search_namespace_only (tree scope)
     830              : {
     831  10844427402 :   bool found = false;
     832  10844427402 :   if (tree *binding = find_namespace_slot (scope, name))
     833              :     {
     834   1488736439 :       tree val = *binding;
     835   1488736439 :       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        52285 :           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        52285 :           unsigned orig_mod = 0;
     847        52285 :           bitmap orig_imp = visible_from_instantiation_origination (&orig_mod);
     848              : 
     849        52285 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
     850        52285 :           int marker = 0;
     851        52285 :           int dup_detect = 0;
     852              : 
     853        52285 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
     854              :             {
     855        29147 :               if (!deduping)
     856              :                 {
     857        28698 :                   if (named_module_purview_p ())
     858              :                     {
     859         9767 :                       dup_detect |= 2;
     860              : 
     861         9767 :                       if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
     862              :                         dup_detect |= 1;
     863              :                     }
     864              :                   else
     865              :                     dup_detect |= 1;
     866              :                 }
     867        29147 :               tree type = NULL_TREE;
     868        29147 :               tree value = bind;
     869              : 
     870        29147 :               if (STAT_HACK_P (bind))
     871              :                 {
     872        12925 :                   type = STAT_TYPE (bind);
     873        12925 :                   value = STAT_DECL (bind);
     874              : 
     875        12925 :                   if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     876              :                     {
     877        12858 :                       if (STAT_TYPE_HIDDEN_P (bind))
     878            0 :                         type = NULL_TREE;
     879        12858 :                       if (STAT_DECL_HIDDEN_P (bind))
     880              :                         value = NULL_TREE;
     881              :                       else
     882        12858 :                         value = ovl_skip_hidden (value);
     883              :                     }
     884              :                 }
     885        16222 :               else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     886        16216 :                 value = ovl_skip_hidden (value);
     887              : 
     888        29147 :               marker = process_module_binding (value, type, marker);
     889              :             }
     890              : 
     891              :           /* Scan the imported bindings.  */
     892        52285 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
     893        52285 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     894              :             {
     895        52285 :               ix--;
     896        52285 :               cluster++;
     897              :             }
     898              : 
     899              :           /* Do this in forward order, so we load modules in an order
     900              :              the user expects.  */
     901       107492 :           for (; ix--; cluster++)
     902       165621 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
     903              :               {
     904              :                 /* Are we importing this module?  */
     905       110414 :                 if (unsigned base = cluster->indices[jx].base)
     906        53420 :                   if (unsigned span = cluster->indices[jx].span)
     907        54389 :                     do
     908        54389 :                       if (bool (want & LOOK_want::ANY_REACHABLE)
     909        54298 :                           || bitmap_bit_p (imports, base)
     910        57582 :                           || (orig_imp && bitmap_bit_p (orig_imp, base)))
     911        52347 :                         goto found;
     912         2042 :                     while (++base, --span);
     913        58067 :                 continue;
     914              : 
     915        52347 :               found:;
     916              :                 /* Is it loaded?  */
     917        52347 :                 unsigned mod = cluster->indices[jx].base;
     918        52347 :                 if (cluster->slots[jx].is_lazy ())
     919              :                   {
     920         4789 :                     gcc_assert (cluster->indices[jx].span == 1);
     921         4789 :                     lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
     922              :                   }
     923        52347 :                 tree bind = cluster->slots[jx];
     924        52347 :                 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        52347 :                 tree type = NULL_TREE;
     931              : 
     932              :                 /* If STAT_HACK_P is false, everything is visible, and
     933              :                    there's no duplication possibilities.  */
     934        52347 :                 if (STAT_HACK_P (bind))
     935              :                   {
     936        31279 :                     if (!deduping)
     937              :                       {
     938              :                         /* Do we need to engage deduplication?  */
     939        28034 :                         int dup = 0;
     940        28034 :                         if (MODULE_BINDING_GLOBAL_P (bind))
     941        25630 :                           dup |= 1;
     942        28034 :                         if (MODULE_BINDING_PARTITION_P (bind))
     943         1827 :                           dup |= 2;
     944        28034 :                         if (unsigned hit = dup_detect & dup)
     945              :                           {
     946        12782 :                             if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
     947        14352 :                                 || (hit & 2
     948          162 :                                     && BINDING_VECTOR_PARTITION_DUPS_P (val)))
     949        11491 :                               dedup (true);
     950              :                           }
     951        28034 :                         dup_detect |= dup;
     952              :                       }
     953              : 
     954        31279 :                     if (bool (want & LOOK_want::ANY_REACHABLE)
     955        31279 :                         || mod == orig_mod)
     956              :                       {
     957         7508 :                         type = STAT_TYPE (bind);
     958         7508 :                         bind = STAT_DECL (bind);
     959              :                       }
     960              :                     else
     961              :                       {
     962        23771 :                         if (STAT_TYPE_VISIBLE_P (bind))
     963         1857 :                           type = STAT_TYPE (bind);
     964        23771 :                         bind = STAT_VISIBLE (bind);
     965              :                       }
     966              :                   }
     967              : 
     968              :                 /* And process it.  */
     969        52347 :                 marker = process_module_binding (bind, type, marker);
     970        58067 :               }
     971        52285 :           found |= marker & 1;
     972              :         }
     973              :       else
     974              :         {
     975              :           /* Only a current module binding, visible from the current module.  */
     976   1488684154 :           tree bind = *binding;
     977   1488684154 :           tree value = bind, type = NULL_TREE;
     978              : 
     979   1488684154 :           if (STAT_HACK_P (bind))
     980              :             {
     981       460322 :               type = STAT_TYPE (bind);
     982       460322 :               value = STAT_DECL (bind);
     983              : 
     984       460322 :               if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     985              :                 {
     986       460112 :                   if (STAT_TYPE_HIDDEN_P (bind))
     987            3 :                     type = NULL_TREE;
     988       460112 :                   if (STAT_DECL_HIDDEN_P (bind))
     989              :                     value = NULL_TREE;
     990              :                   else
     991       459687 :                     value = ovl_skip_hidden (value);
     992              :                 }
     993              :             }
     994   1488223832 :           else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     995   1487515171 :             value = ovl_skip_hidden (value);
     996              : 
     997   1488684154 :           found |= process_binding (value, type);
     998              :         }
     999              :     }
    1000              : 
    1001  10844427402 :   return found;
    1002              : }
    1003              : 
    1004              : /* Conditionally look in namespace SCOPE and inline children.  */
    1005              : 
    1006              : bool
    1007   2214461955 : name_lookup::search_namespace (tree scope)
    1008              : {
    1009   2214461955 :   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   2214461955 :   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   2214461955 :   if (name)
    1019              :     /* Recursively look in its inline children.  */
    1020   2214460164 :     if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1021   2336782632 :       for (unsigned ix = inlinees->length (); ix--;)
    1022   1786749573 :         found |= search_namespace ((*inlinees)[ix]);
    1023              : 
    1024   2214461955 :   if (found)
    1025    438741699 :     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     19005317 : 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     19005317 :   if (found_p (scope))
    1039              :     return true;
    1040              : 
    1041     19005317 :   bool found = false;
    1042     19005317 :   if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
    1043      3346186 :     for (unsigned ix = usings->length (); ix--;)
    1044      1673493 :       found |= search_qualified (strip_using_decl ((*usings)[ix]), true);
    1045              : 
    1046              :   /* Look in its inline children.  */
    1047     19005317 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1048      7252619 :     for (unsigned ix = inlinees->length (); ix--;)
    1049      3684625 :       found |= search_usings ((*inlinees)[ix]);
    1050              : 
    1051     19005317 :   if (found)
    1052        21590 :     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    427712382 : name_lookup::search_qualified (tree scope, bool usings)
    1068              : {
    1069    427712382 :   bool found = false;
    1070              : 
    1071    427712382 :   if (seen_p (scope))
    1072            0 :     found = found_p (scope);
    1073              :   else
    1074              :     {
    1075    427712382 :       found = search_namespace (scope);
    1076    427712382 :       if (!found && usings)
    1077     15320692 :         found = search_usings (scope);
    1078              :     }
    1079              : 
    1080    427712382 :   dedup (false);
    1081              : 
    1082    427712382 :   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   8739735875 : name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
    1090              : {
    1091   8739735875 :   if (see_and_mark (scope))
    1092   8739735875 :     return;
    1093              : 
    1094              :   /* Record it.  */
    1095              :   tree common = scope;
    1096  17275630620 :   while (SCOPE_DEPTH (common) > depth)
    1097   8622280412 :     common = CP_DECL_CONTEXT (common);
    1098   8653350208 :   queue.safe_push (using_pair (common, scope));
    1099              : 
    1100              :   /* Queue its inline children.  */
    1101   8653350208 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1102   8551481751 :     for (unsigned ix = inlinees->length (); ix--;)
    1103   6527810405 :       queue_namespace (queue, depth, (*inlinees)[ix]);
    1104              : 
    1105              :   /* Queue its using targets.  */
    1106   8653350208 :   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  13152673638 : name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
    1113              : {
    1114  13152673638 :   if (usings)
    1115    106935866 :     for (unsigned ix = usings->length (); ix--;)
    1116     53710011 :       queue_namespace (queue, depth, strip_using_decl ((*usings)[ix]));
    1117  13152673638 : }
    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   1323382623 : name_lookup::search_unqualified (tree scope, cp_binding_level *level)
    1127              : {
    1128   1323382623 :   using_queue queue;
    1129   1323382623 :   bool found = false;
    1130              : 
    1131              :   /* Queue local using-directives.  */
    1132   5822706053 :   for (; level->kind != sk_namespace; level = level->level_chain)
    1133   4499323430 :     queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
    1134              : 
    1135   4426648797 :   for (; !found; scope = CP_DECL_CONTEXT (scope))
    1136              :     {
    1137   2158215459 :       gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
    1138   2158215459 :       int depth = SCOPE_DEPTH (scope);
    1139              : 
    1140              :       /* Queue namespaces reachable from SCOPE. */
    1141   2158215459 :       queue_namespace (queue, depth, scope);
    1142              : 
    1143              :       /* Search every queued namespace where SCOPE is the common
    1144              :          ancestor.  Adjust the others.  */
    1145   2158215459 :       unsigned ix = 0;
    1146   2204873167 :       do
    1147              :         {
    1148   2204873167 :           using_pair &pair = queue[ix];
    1149  10882855240 :           while (pair.first == scope)
    1150              :             {
    1151   8629965447 :               found |= search_namespace_only (pair.second);
    1152   8629965447 :               pair = queue.pop ();
    1153   8629965447 :               if (ix == queue.length ())
    1154   2156856541 :                 goto done;
    1155              :             }
    1156              :           /* The depth is the same as SCOPE, find the parent scope.  */
    1157     48016626 :           if (SCOPE_DEPTH (pair.first) == depth)
    1158     48012234 :             pair.first = CP_DECL_CONTEXT (pair.first);
    1159     48016626 :           ix++;
    1160              :         }
    1161     96033252 :       while (ix < queue.length ());
    1162      1358918 :     done:;
    1163   2158215459 :       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   1552109113 :       if (bool (want & LOOK_want::HIDDEN_FRIEND))
    1173              :         break;
    1174              :     }
    1175              : 
    1176   1323382623 :   dedup (false);
    1177              : 
    1178   1323382623 :   return found;
    1179   1323382623 : }
    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     78935755 : name_lookup::add_fns (tree fns)
    1186              : {
    1187     78935755 :   if (!fns)
    1188              :     return;
    1189     70318106 :   else if (TREE_CODE (fns) == OVERLOAD)
    1190              :     {
    1191     32606746 :       if (TREE_TYPE (fns) != unknown_type_node)
    1192        85328 :         fns = OVL_FUNCTION (fns);
    1193              :     }
    1194     37711360 :   else if (!DECL_DECLARES_FUNCTION_P (fns))
    1195              :     return;
    1196              : 
    1197     70292327 :   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    259551984 : name_lookup::adl_namespace_fns (tree scope, bitmap imports,
    1209              :                                 bitmap inst_path, bitmap assocs)
    1210              : {
    1211    259551984 :   if (tree *binding = find_namespace_slot (scope, name))
    1212              :     {
    1213     54698982 :       tree val = *binding;
    1214     54698982 :       if (TREE_CODE (val) != BINDING_VECTOR)
    1215     54674682 :         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        24300 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
    1222        24300 :           int dup_detect = 0;
    1223              : 
    1224        24300 :           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        15937 :               if (!deduping)
    1230              :                 {
    1231         5983 :                   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        15937 :               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        24300 :           if (tentative)
    1248              :             return;
    1249              : 
    1250              :           /* Scan the imported bindings.  */
    1251        24299 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
    1252        24299 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    1253              :             {
    1254        24299 :               ix--;
    1255        24299 :               cluster++;
    1256              :             }
    1257              : 
    1258              :           /* Do this in forward order, so we load modules in an order
    1259              :              the user expects.  */
    1260        50052 :           for (; ix--; cluster++)
    1261        77259 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    1262              :               {
    1263        51506 :                 int mod = cluster->indices[jx].base;
    1264              : 
    1265              :                 /* Functions are never on merged slots.  */
    1266        51506 :                 if (!mod || cluster->indices[jx].span != 1)
    1267        32770 :                   continue;
    1268              : 
    1269              :                 /* Is this slot accessible here?  */
    1270        18736 :                 bool visible = bitmap_bit_p (imports, mod);
    1271        18736 :                 bool on_inst_path = inst_path && bitmap_bit_p (inst_path, mod);
    1272         9424 :                 if (!visible && !on_inst_path
    1273         9421 :                     && !(assocs && bitmap_bit_p (assocs, mod)))
    1274            3 :                   continue;
    1275              : 
    1276              :                 /* Is it loaded?  */
    1277        18733 :                 if (cluster->slots[jx].is_lazy ())
    1278           87 :                   lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
    1279              : 
    1280        18733 :                 tree bind = cluster->slots[jx];
    1281        18733 :                 if (!bind)
    1282              :                   /* Load errors could mean there's nothing here.  */
    1283            0 :                   continue;
    1284              : 
    1285        18733 :                 if (STAT_HACK_P (bind))
    1286              :                   {
    1287        18640 :                     if (!deduping)
    1288              :                       {
    1289              :                         /* Do we need to engage deduplication?  */
    1290         8670 :                         int dup = 0;
    1291         8670 :                         if (MODULE_BINDING_GLOBAL_P (bind))
    1292         8607 :                           dup |= 1;
    1293         8670 :                         if (MODULE_BINDING_PARTITION_P (bind))
    1294            3 :                           dup |= 2;
    1295         8670 :                         if (unsigned hit = dup_detect & dup)
    1296         4560 :                           if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
    1297         5052 :                               || (hit & 2
    1298            3 :                                   && BINDING_VECTOR_PARTITION_DUPS_P (val)))
    1299         4071 :                             dedup (true);
    1300         8670 :                         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        18640 :                     if (on_inst_path)
    1307              :                       {
    1308              :                         /* If there are any internal functions visible, naming
    1309              :                            them outside that module is ill-formed.  */
    1310         9312 :                         auto_diagnostic_group d;
    1311         9312 :                         if (MODULE_BINDING_INTERNAL_DECLS_P (bind)
    1312         9312 :                             && 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         9312 :                         bind = STAT_DECL (bind);
    1331         9312 :                       }
    1332              :                     else
    1333         9328 :                       bind = STAT_VISIBLE (bind);
    1334              :                   }
    1335              : 
    1336        18733 :                 bind = ovl_skip_hidden (bind);
    1337        18733 :                 if (on_inst_path || visible)
    1338        18697 :                   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     59779764 : name_lookup::adl_class_fns (tree type)
    1359              : {
    1360              :   /* Add friends.  */
    1361     59779764 :   for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
    1362    171134868 :        list; list = TREE_CHAIN (list))
    1363    111355104 :     if (name == FRIEND_NAME (list))
    1364              :       {
    1365     12301634 :         tree context = NULL_TREE; /* Lazily computed.  */
    1366     36531934 :         for (tree friends = FRIEND_DECLS (list); friends;
    1367     24230300 :              friends = TREE_CHAIN (friends))
    1368              :           {
    1369     24230300 :             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     24230300 :             if (!context)
    1375     12301634 :               context = decl_namespace_context (type);
    1376        20572 :             if (cxx_dialect < cxx20
    1377     24230300 :                 ? CP_DECL_CONTEXT (fn) != context
    1378     24209728 :                 : !DECL_NAMESPACE_SCOPE_P (fn))
    1379          425 :               continue;
    1380              : 
    1381     24229875 :             dedup (true);
    1382              : 
    1383              :             /* Template specializations are never found by name lookup.
    1384              :                (Templates themselves can be found, but not template
    1385              :                specializations.)  */
    1386     24229875 :             if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
    1387         3436 :               continue;
    1388              : 
    1389     24226439 :             add_fns (fn);
    1390              :           }
    1391              :       }
    1392     59779764 : }
    1393              : 
    1394              : /* Find the containing non-inlined namespace, add it and all its
    1395              :    inlinees.  */
    1396              : 
    1397              : void
    1398    283399146 : name_lookup::adl_namespace (tree scope)
    1399              : {
    1400    489707512 :   if (see_and_mark (scope))
    1401              :     return;
    1402              : 
    1403              :   /* Look down into inline namespaces.  */
    1404    259551984 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1405    270657192 :     for (unsigned ix = inlinees->length (); ix--;)
    1406    206308366 :       adl_namespace ((*inlinees)[ix]);
    1407              : 
    1408    259551984 :   if (DECL_NAMESPACE_INLINE_P (scope))
    1409              :     /* Mark parent.  */
    1410    206308366 :     adl_namespace (CP_DECL_CONTEXT (scope));
    1411              : }
    1412              : 
    1413              : /* Adds the class and its friends to the lookup structure.  */
    1414              : 
    1415              : void
    1416     59943802 : 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     59943802 :   if (!CLASS_TYPE_P (type))
    1421              :     return;
    1422              : 
    1423     59943802 :   type = TYPE_MAIN_VARIANT (type);
    1424              : 
    1425     59943802 :   if (see_and_mark (type))
    1426              :     return;
    1427              : 
    1428     59783535 :   tree context = decl_namespace_context (type);
    1429     59783535 :   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     53823559 : name_lookup::adl_bases (tree type)
    1437              : {
    1438     53823559 :   adl_class_only (type);
    1439              : 
    1440              :   /* Process baseclasses.  */
    1441     53823559 :   if (tree binfo = TYPE_BINFO (type))
    1442              :     {
    1443              :       tree base_binfo;
    1444              :       int i;
    1445              : 
    1446     59916910 :       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    1447      9140853 :         adl_bases (BINFO_TYPE (base_binfo));
    1448              :     }
    1449     53823559 : }
    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     57355678 : name_lookup::adl_class (tree type)
    1469              : {
    1470              :   /* Backend build structures, such as __builtin_va_list, aren't
    1471              :      affected by all this.  */
    1472     57355678 :   if (!CLASS_TYPE_P (type))
    1473              :     return;
    1474              : 
    1475     57274261 :   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     57274261 :   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     44682706 :   if (!tentative)
    1486     44679605 :     complete_type (type);
    1487              : 
    1488     44682706 :   adl_bases (type);
    1489     44682706 :   mark_found (type);
    1490              : 
    1491     44682706 :   if (TYPE_CLASS_SCOPE_P (type))
    1492      4156499 :     adl_class_only (TYPE_CONTEXT (type));
    1493              : 
    1494              :   /* Process template arguments.  */
    1495     44682706 :   if (CLASSTYPE_TEMPLATE_INFO (type)
    1496     44682706 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
    1497              :     {
    1498     25759902 :       tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1499     77886319 :       for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
    1500     52126417 :         adl_template_arg (TREE_VEC_ELT (list, i));
    1501              :     }
    1502              : }
    1503              : 
    1504              : void
    1505     35097672 : name_lookup::adl_enum (tree type)
    1506              : {
    1507     35097672 :   type = TYPE_MAIN_VARIANT (type);
    1508     35097672 :   if (see_and_mark (type))
    1509              :     return;
    1510              : 
    1511     19236767 :   if (TYPE_CLASS_SCOPE_P (type))
    1512      1963738 :     adl_class_only (TYPE_CONTEXT (type));
    1513              :   else
    1514     17273029 :     adl_namespace (decl_namespace_context (type));
    1515              : }
    1516              : 
    1517              : void
    1518    107815070 : name_lookup::adl_expr (tree expr)
    1519              : {
    1520    107815070 :   if (!expr)
    1521              :     return;
    1522              : 
    1523    107815070 :   gcc_assert (!TYPE_P (expr));
    1524              : 
    1525    107815070 :   if (TREE_TYPE (expr) != unknown_type_node)
    1526              :     {
    1527    107808812 :       adl_type (unlowered_expr_type (expr));
    1528    107808812 :       return;
    1529              :     }
    1530              : 
    1531         6258 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1532          458 :     expr = TREE_OPERAND (expr, 0);
    1533         6258 :   if (TREE_CODE (expr) == COMPONENT_REF
    1534         6258 :       || TREE_CODE (expr) == OFFSET_REF)
    1535          422 :     expr = TREE_OPERAND (expr, 1);
    1536         6258 :   expr = MAYBE_BASELINK_FUNCTIONS (expr);
    1537              : 
    1538         6258 :   if (OVL_P (expr))
    1539        12086 :     for (lkp_iterator iter (expr); iter; ++iter)
    1540         6199 :       adl_type (TREE_TYPE (*iter));
    1541          371 :   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          371 :       adl_expr (TREE_OPERAND (expr, 0));
    1551              : 
    1552              :       /* Now the arguments.  */
    1553          371 :       if (tree args = TREE_OPERAND (expr, 1))
    1554          751 :         for (int ix = TREE_VEC_LENGTH (args); ix--;)
    1555          380 :           adl_template_arg (TREE_VEC_ELT (args, ix));
    1556              :     }
    1557              : }
    1558              : 
    1559              : void
    1560    152372386 : name_lookup::adl_type (tree type)
    1561              : {
    1562    169344195 :   if (!type)
    1563              :     return;
    1564              : 
    1565    169344195 :   if (TYPE_PTRDATAMEM_P (type))
    1566              :     {
    1567              :       /* Pointer to member: associate class type and value type.  */
    1568         1623 :       adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
    1569         1623 :       adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
    1570         1623 :       return;
    1571              :     }
    1572    169342572 :   else if (REFLECTION_TYPE_P (type))
    1573              :     {
    1574              :       /* The namespace std::meta is an associated namespace of
    1575              :          std::meta::info.  */
    1576        28396 :       adl_namespace (std_meta_node);
    1577        28396 :       return;
    1578              :     }
    1579              : 
    1580    169314176 :   switch (TREE_CODE (type))
    1581              :     {
    1582     56203639 :     case RECORD_TYPE:
    1583     56203639 :       if (TYPE_PTRMEMFUNC_P (type))
    1584              :         {
    1585        45778 :           adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
    1586        45778 :           return;
    1587              :         }
    1588              :       /* FALLTHRU */
    1589     57355678 :     case UNION_TYPE:
    1590     57355678 :       adl_class (type);
    1591     57355678 :       return;
    1592              : 
    1593       118952 :     case METHOD_TYPE:
    1594              :       /* The basetype is referenced in the first arg type, so just
    1595              :          fall through.  */
    1596       118952 :     case FUNCTION_TYPE:
    1597              :       /* Associate the parameter types.  */
    1598       544927 :       for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
    1599       425975 :         adl_type (TREE_VALUE (args));
    1600              :       /* FALLTHROUGH */
    1601              : 
    1602     16924396 :     case POINTER_TYPE:
    1603     16924396 :     case REFERENCE_TYPE:
    1604     16924396 :     case ARRAY_TYPE:
    1605     16924396 :       adl_type (TREE_TYPE (type));
    1606     16924396 :       return;
    1607              : 
    1608     35097672 :     case ENUMERAL_TYPE:
    1609     35097672 :       adl_enum (type);
    1610     35097672 :       return;
    1611              : 
    1612         4175 :     case LANG_TYPE:
    1613         4175 :       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     52329479 : 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     52329479 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    1645     52329479 :       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
    1646              :     ;
    1647     52329479 :   else if (TREE_CODE (arg) == TEMPLATE_DECL)
    1648              :     {
    1649         5826 :       tree ctx = CP_DECL_CONTEXT (arg);
    1650              : 
    1651              :       /* It's not a member template.  */
    1652         5826 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
    1653         5820 :         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     52323653 :   else if (ARGUMENT_PACK_P (arg))
    1660              :     {
    1661        58862 :       tree args = ARGUMENT_PACK_ARGS (arg);
    1662        58862 :       int i, len = TREE_VEC_LENGTH (args);
    1663       261544 :       for (i = 0; i < len; ++i)
    1664       202682 :         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     52264791 :   else if (TYPE_P (arg))
    1669     44129697 :     adl_type (arg);
    1670     52329479 : }
    1671              : 
    1672              : /* Perform ADL lookup.  FNS is the existing lookup result and ARGS are
    1673              :    the call arguments.  */
    1674              : 
    1675              : tree
    1676     58348108 : name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
    1677              : {
    1678     58348108 :   gcc_checking_assert (!vec_safe_length (scopes));
    1679              : 
    1680              :   /* Gather each associated entity onto the lookup's scope list.  */
    1681     58348108 :   unsigned ix;
    1682     58348108 :   tree arg;
    1683              : 
    1684    166478955 :   FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
    1685              :     /* OMP reduction operators put an ADL-significant type as the
    1686              :        first arg. */
    1687    108130847 :     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    108130767 :     else if (!tentative || !type_dependent_expression_p (arg))
    1693    107814699 :       adl_expr (arg);
    1694              : 
    1695     58348108 :   if (vec_safe_length (scopes))
    1696              :     {
    1697              :       /* Now do the lookups.  */
    1698     42639102 :       value = fns;
    1699     42639102 :       if (fns)
    1700     29978686 :         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     42639102 :       bitmap_obstack_initialize (NULL);
    1706     42639102 :       hash_map<tree, bitmap> ns_mod_assocs;
    1707     42639102 :       if (modules_p () && !tentative)
    1708              :         {
    1709       967842 :           for (tree scope : scopes)
    1710       687276 :             if (TYPE_P (scope))
    1711              :               {
    1712       172897 :                 int mod = get_originating_module (TYPE_NAME (scope),
    1713              :                                                   /*global_m1=*/true);
    1714       172897 :                 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     42639102 :       bitmap inst_path = NULL;
    1731              :       /* VISIBLE is the regular import bitmap.  */
    1732     42639102 :       bitmap visible = visible_instantiation_path (&inst_path);
    1733              : 
    1734    381211388 :       for (unsigned ix = scopes->length (); ix--;)
    1735              :         {
    1736    338572286 :           tree scope = (*scopes)[ix];
    1737    338572286 :           if (TREE_CODE (scope) == NAMESPACE_DECL)
    1738              :             {
    1739    259551984 :               tree ctx = scope;
    1740    543528473 :               while (DECL_NAMESPACE_INLINE_P (ctx))
    1741    283976489 :                 ctx = CP_DECL_CONTEXT (ctx);
    1742    259551984 :               bitmap *assocs = ns_mod_assocs.get (ctx);
    1743    259551984 :               adl_namespace_fns (scope, visible, inst_path,
    1744              :                                  assocs ? *assocs : NULL);
    1745              :             }
    1746     79020302 :           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     59779764 :             adl_class_fns (scope);
    1751              :         }
    1752              : 
    1753     42639398 :       for (auto refs : ns_mod_assocs)
    1754          296 :         BITMAP_FREE (refs.second);
    1755     42639102 :       bitmap_obstack_release (NULL);
    1756              : 
    1757     42639102 :       fns = value;
    1758     42639102 :       dedup (false);
    1759     42639102 :     }
    1760              : 
    1761     58348108 :   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     58348108 : lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args,
    1779              :                       bool tentative/*=false*/)
    1780              : {
    1781     58348108 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    1782     58348108 :   name_lookup lookup (name);
    1783     58348108 :   lookup.tentative = tentative;
    1784     58348108 :   return lookup.search_adl (fns, args);
    1785     58348108 : }
    1786              : 
    1787              : /* FNS is an overload set of conversion functions.  Return the
    1788              :    overloads converting to TYPE.  */
    1789              : 
    1790              : static tree
    1791       661559 : extract_conversion_operator (tree fns, tree type)
    1792              : {
    1793       661559 :   tree convs = NULL_TREE;
    1794       661559 :   tree tpls = NULL_TREE;
    1795              : 
    1796      1889662 :   for (ovl_iterator iter (fns); iter; ++iter)
    1797              :     {
    1798       851234 :       if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
    1799       545747 :         convs = lookup_add (*iter, convs);
    1800              : 
    1801       851234 :       if (TREE_CODE (*iter) == TEMPLATE_DECL)
    1802       187318 :         tpls = lookup_add (*iter, tpls);
    1803              :     }
    1804              : 
    1805       661559 :   if (!convs)
    1806       252782 :     convs = tpls;
    1807              : 
    1808       661559 :   return convs;
    1809              : }
    1810              : 
    1811              : /* Binary search of (ordered) MEMBER_VEC for NAME.  */
    1812              : 
    1813              : static tree
    1814   3181441535 : member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
    1815              : {
    1816  18631442005 :   for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
    1817              :     {
    1818  13197896666 :       unsigned mid = (lo + hi) / 2;
    1819  13197896666 :       tree binding = (*member_vec)[mid];
    1820  26395793332 :       tree binding_name = OVL_NAME (binding);
    1821              : 
    1822  13197896666 :       if (binding_name > name)
    1823              :         hi = mid;
    1824   6655372119 :       else if (binding_name < name)
    1825   5726034388 :         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    864355970 : member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
    1837              : {
    1838   7464659797 :   for (int ix = member_vec->length (); ix--;)
    1839   6669432649 :     if (tree binding = (*member_vec)[ix])
    1840   6669432649 :       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   3296108688 : fields_linear_search (tree klass, tree name, bool want_type)
    1850              : {
    1851  40465162562 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1852              :     {
    1853  37407710217 :       tree decl = fields;
    1854              : 
    1855  37407710217 :       if (TREE_CODE (decl) == FIELD_DECL
    1856  37407710217 :           && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1857              :         {
    1858     35217434 :           if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
    1859              :             return temp;
    1860              :         }
    1861              : 
    1862  37407698084 :       if (DECL_NAME (decl) != name)
    1863  37119602737 :         continue;
    1864              : 
    1865    288095347 :       if (TREE_CODE (decl) == USING_DECL)
    1866              :         {
    1867       968917 :           decl = strip_using_decl (decl);
    1868       968917 :           if (is_overloaded_fn (decl))
    1869       453636 :             continue;
    1870              :         }
    1871              : 
    1872    287641711 :       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    287641689 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1878              :         /* Functions are found separately.  */
    1879     48534372 :         continue;
    1880              : 
    1881    239107317 :       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          677 : name_independent_linear_search (tree val, tree klass, tree name)
    1894              : {
    1895         3269 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1896              :     {
    1897         2592 :       tree decl = fields;
    1898              : 
    1899         2592 :       if (TREE_CODE (decl) == FIELD_DECL
    1900         2592 :           && 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         2592 :       if (DECL_NAME (decl) != name)
    1910         2541 :         continue;
    1911              : 
    1912           51 :       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           51 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1920              :         /* Functions are found separately.  */
    1921            0 :         continue;
    1922              : 
    1923           51 :     add:
    1924           51 :       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          677 :   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     35217434 : search_anon_aggr (tree anon, tree name, bool want_type)
    1960              : {
    1961     35217434 :   gcc_assert (COMPLETE_TYPE_P (anon));
    1962     35217434 :   tree ret = get_class_binding_direct (anon, name, want_type);
    1963     35217434 :   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   6546179235 : get_class_binding_direct (tree klass, tree name, bool want_type)
    1975              : {
    1976   6546179235 :   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   6546179235 :   bool conv_op = IDENTIFIER_CONV_OP_P (name);
    1981   6546179235 :   tree lookup = conv_op ? conv_op_identifier : name;
    1982   6546179235 :   tree val = NULL_TREE;
    1983   6546179235 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    1984              : 
    1985   6546179235 :   if (COMPLETE_TYPE_P (klass) && member_vec)
    1986              :     {
    1987   3181281282 :       val = member_vec_binary_search (member_vec, lookup);
    1988   3181281282 :       if (!val)
    1989              :         ;
    1990    929177891 :       else if (TREE_CODE (val) == OVERLOAD
    1991    929177891 :                && 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    929177730 :       else if (STAT_HACK_P (val))
    2035          201 :         val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
    2036    929177529 :       else if (want_type && !DECL_DECLARES_TYPE_P (val))
    2037              :         val = NULL_TREE;
    2038              :     }
    2039              :   else
    2040              :     {
    2041   3364897953 :       if (member_vec && !want_type)
    2042    864355970 :         val = member_vec_linear_search (member_vec, lookup);
    2043              : 
    2044   3364897953 :       if (id_equal (lookup, "_") && !want_type)
    2045          677 :         val = name_independent_linear_search (val, klass, lookup);
    2046   3364897276 :       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   3296107256 :         if (tree field_val = fields_linear_search (klass, lookup, want_type))
    2050              :           {
    2051    238654911 :             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   6546179235 :   if (val && conv_op)
    2061              :     {
    2062     35189318 :       gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
    2063     35189318 :       val = OVL_CHAIN (val);
    2064     35189318 :       if (tree type = TREE_TYPE (name))
    2065       661559 :         val = extract_conversion_operator (val, type);
    2066              :     }
    2067              : 
    2068   6546179235 :   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   3585349474 : maybe_lazily_declare (tree klass, tree name)
    2076              : {
    2077              :   /* See big comment about module_state::write_pendings regarding adding
    2078              :      a check bit.  */
    2079   3585349474 :   if (modules_p ())
    2080      9066889 :     lazy_load_pendings (TYPE_NAME (klass));
    2081              : 
    2082              :   /* Lazily declare functions, if we're going to search these.  */
    2083   3585349474 :   if (IDENTIFIER_CTOR_P (name))
    2084              :     {
    2085    117133733 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
    2086      4173077 :         lazily_declare_fn (sfk_constructor, klass);
    2087    117133733 :       if (CLASSTYPE_LAZY_COPY_CTOR (klass))
    2088      6903078 :         lazily_declare_fn (sfk_copy_constructor, klass);
    2089    117133733 :       if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
    2090      5761597 :         lazily_declare_fn (sfk_move_constructor, klass);
    2091              :     }
    2092   3468215741 :   else if (IDENTIFIER_DTOR_P (name))
    2093              :     {
    2094     98919711 :       if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
    2095      7831563 :         lazily_declare_fn (sfk_destructor, klass);
    2096              :     }
    2097   3369296030 :   else if (name == assign_op_identifier)
    2098              :     {
    2099     17124043 :       if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
    2100      1746973 :         lazily_declare_fn (sfk_copy_assignment, klass);
    2101     17124043 :       if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
    2102      1235157 :         lazily_declare_fn (sfk_move_assignment, klass);
    2103              :     }
    2104   3585349474 : }
    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   5948676739 : get_class_binding (tree klass, tree name, bool want_type /*=false*/)
    2112              : {
    2113   5948676739 :   klass = complete_type (klass);
    2114              : 
    2115   5948676739 :   if (COMPLETE_TYPE_P (klass))
    2116   3585349474 :     maybe_lazily_declare (klass, name);
    2117              : 
    2118   5948676739 :   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    360983054 : find_member_slot (tree klass, tree name)
    2128              : {
    2129    360983054 :   bool complete_p = COMPLETE_TYPE_P (klass);
    2130              : 
    2131    360983054 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2132    360983054 :   if (!member_vec)
    2133              :     {
    2134     25654329 :       vec_alloc (member_vec, 8);
    2135     25654329 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2136     25654329 :       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      2210015 :         member_vec = set_class_bindings (klass, 6);
    2142              :     }
    2143              : 
    2144    360983054 :   if (IDENTIFIER_CONV_OP_P (name))
    2145      2823488 :     name = conv_op_identifier;
    2146              : 
    2147    360983054 :   unsigned ix, length = member_vec->length ();
    2148   4247433282 :   for (ix = 0; ix < length; ix++)
    2149              :     {
    2150   4065457714 :       tree *slot = &(*member_vec)[ix];
    2151   8130915428 :       tree fn_name = OVL_NAME (*slot);
    2152              : 
    2153   4065457714 :       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    166851948 :           gcc_checking_assert (OVL_P (*slot));
    2161    166851948 :           if (name == conv_op_identifier)
    2162              :             {
    2163       695147 :               gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
    2164              :               /* Skip the conv-op marker. */
    2165       695147 :               slot = &OVL_CHAIN (*slot);
    2166              :             }
    2167    166851948 :           return slot;
    2168              :         }
    2169              : 
    2170   3898605766 :       if (complete_p && fn_name > name)
    2171              :         break;
    2172              :     }
    2173              : 
    2174              :   /* No slot found, add one if the class is complete.  */
    2175    194131106 :   if (complete_p)
    2176              :     {
    2177              :       /* Do exact allocation, as we don't expect to add many.  */
    2178     37697756 :       gcc_assert (name != conv_op_identifier);
    2179     37697756 :       vec_safe_reserve_exact (member_vec, 1);
    2180     37697756 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2181     37697756 :       member_vec->quick_insert (ix, NULL_TREE);
    2182     37697756 :       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    156433332 : add_member_slot (tree klass, tree name)
    2193              : {
    2194    156433332 :   gcc_assert (!COMPLETE_TYPE_P (klass));
    2195              : 
    2196    156433332 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2197    156433332 :   vec_safe_push (member_vec, NULL_TREE);
    2198    156433332 :   CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2199              : 
    2200    156433332 :   tree *slot = &member_vec->last ();
    2201    156433332 :   if (IDENTIFIER_CONV_OP_P (name))
    2202              :     {
    2203              :       /* Install the marker prefix.  */
    2204      2128341 :       *slot = ovl_make (conv_op_marker, NULL_TREE);
    2205      2128341 :       slot = &OVL_CHAIN (*slot);
    2206              :     }
    2207              : 
    2208    156433332 :   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   6027036272 : member_name_cmp (const void *a_p, const void *b_p)
    2218              : {
    2219   6027036272 :   tree a = *(const tree *)a_p;
    2220   6027036272 :   tree b = *(const tree *)b_p;
    2221   6027036272 :   tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
    2222   6027036272 :   tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
    2223              : 
    2224   6027036272 :   gcc_checking_assert (name_a && name_b);
    2225   6027036272 :   if (name_a != name_b)
    2226   8849358889 :     return name_a < name_b ? -1 : +1;
    2227              : 
    2228     18505771 :   if (name_a == conv_op_identifier)
    2229              :     {
    2230              :       /* Strip the conv-op markers. */
    2231      1097122 :       gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
    2232              :                            && OVL_FUNCTION (b) == conv_op_marker);
    2233       548561 :       a = OVL_CHAIN (a);
    2234       548561 :       b = OVL_CHAIN (b);
    2235              :     }
    2236              : 
    2237     18505771 :   if (TREE_CODE (a) == OVERLOAD)
    2238      6527218 :     a = OVL_FUNCTION (a);
    2239     18505771 :   if (TREE_CODE (b) == OVERLOAD)
    2240      6117465 :     b = OVL_FUNCTION (b);
    2241              : 
    2242     18505771 :   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     18505465 :   if (TREE_CODE (a) != TREE_CODE (b))
    2263              :     {
    2264              :       /* If one of them is a TYPE_DECL, it loses.  */
    2265     16812502 :       if (TREE_CODE (a) == TYPE_DECL)
    2266              :         return +1;
    2267     16812169 :       else if (TREE_CODE (b) == TYPE_DECL)
    2268              :         return -1;
    2269              : 
    2270              :       /* If one of them is a USING_DECL, it loses.  */
    2271     16811827 :       if (TREE_CODE (a) == USING_DECL)
    2272              :         return +1;
    2273      8682547 :       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      1692963 :   if (DECL_UID (a) != DECL_UID (b))
    2294      1692945 :     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     10296588 : resort_member_name_cmp (const void *a_p, const void *b_p)
    2310              : {
    2311     10296588 :   tree a = *(const tree *)a_p;
    2312     10296588 :   tree b = *(const tree *)b_p;
    2313     20593176 :   tree name_a = OVL_NAME (a);
    2314     20593176 :   tree name_b = OVL_NAME (b);
    2315              : 
    2316     10296588 :   resort_data.new_value (&name_a, &name_a, resort_data.cookie);
    2317     10296588 :   resort_data.new_value (&name_b, &name_b, resort_data.cookie);
    2318              : 
    2319     10296588 :   gcc_checking_assert (name_a != name_b);
    2320              : 
    2321     10296588 :   return name_a < name_b ? -1 : +1;
    2322              : }
    2323              : 
    2324              : /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered.  */
    2325              : 
    2326              : void
    2327        53326 : resort_type_member_vec (void *obj, void */*orig_obj*/,
    2328              :                         gt_pointer_operator new_value, void* cookie)
    2329              : {
    2330        53326 :   if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
    2331              :     {
    2332        53326 :       resort_data.new_value = new_value;
    2333        53326 :       resort_data.cookie = cookie;
    2334        53326 :       member_vec->qsort (resort_member_name_cmp);
    2335              :     }
    2336        53326 : }
    2337              : 
    2338              : /* Recursively count the number of fields in KLASS, including anonymous
    2339              :    union members.  */
    2340              : 
    2341              : static unsigned
    2342     76663676 : count_class_fields (tree klass)
    2343              : {
    2344     76663676 :   unsigned n_fields = 0;
    2345              : 
    2346    610527502 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2347    533863826 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2348              :       /* Functions are dealt with separately.  */;
    2349    255522993 :     else if (TREE_CODE (fields) == FIELD_DECL
    2350    255522993 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2351       303131 :       n_fields += count_class_fields (TREE_TYPE (fields));
    2352    255219862 :     else if (DECL_NAME (fields))
    2353              :       {
    2354    228917090 :         if (TYPE_DECL_WAS_UNNAMED (fields))
    2355              :           /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    2356              :              purposes.  */
    2357           10 :           continue;
    2358    228917080 :         n_fields += 1;
    2359              :       }
    2360              : 
    2361     76663676 :   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     26497667 : member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
    2369              : {
    2370    443958170 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2371    417460503 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2372              :       /* Functions are handled separately.  */;
    2373    139119682 :     else if (TREE_CODE (fields) == FIELD_DECL
    2374    139119682 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2375       293092 :       member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
    2376    138826590 :     else if (DECL_NAME (fields))
    2377              :       {
    2378    133758313 :         tree field = fields;
    2379              :         /* Mark a conv-op USING_DECL with the conv-op-marker.  */
    2380    133758313 :         if (TREE_CODE (field) == USING_DECL
    2381    133758313 :             && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
    2382       137117 :           field = ovl_make (conv_op_marker, field);
    2383    133621196 :         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    133758304 :         member_vec->quick_push (field);
    2388              :       }
    2389     26497667 : }
    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     26204596 : member_vec_dedup (vec<tree, va_gc> *member_vec)
    2417              : {
    2418     26204596 :   unsigned len = member_vec->length ();
    2419     26204596 :   unsigned store = 0;
    2420              : 
    2421     26204596 :   if (!len)
    2422              :     return;
    2423              : 
    2424     52409188 :   tree name = OVL_NAME ((*member_vec)[0]);
    2425    310908458 :   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    573983603 :       for (jx = ix; jx < len; jx++)
    2434              :         {
    2435    547779009 :           tree next = (*member_vec)[jx];
    2436    547779009 :           if (jx != ix)
    2437              :             {
    2438    263075145 :               tree next_name = OVL_NAME (next);
    2439    263075145 :               if (next_name != name)
    2440              :                 {
    2441              :                   name = next_name;
    2442              :                   break;
    2443              :                 }
    2444              :             }
    2445              : 
    2446    289279739 :           if (IDENTIFIER_CONV_OP_P (name))
    2447              :             {
    2448      2265458 :               marker = next;
    2449      2265458 :               next = OVL_CHAIN (next);
    2450              :             }
    2451              : 
    2452    289279739 :           if (TREE_CODE (next) == USING_DECL)
    2453              :             {
    2454     12166534 :               if (IDENTIFIER_CTOR_P (name))
    2455              :                 /* Dependent inherited ctor. */
    2456       309554 :                 continue;
    2457              : 
    2458     11856980 :               next = strip_using_decl (next);
    2459     11856980 :               if (TREE_CODE (next) == USING_DECL)
    2460              :                 {
    2461      9797459 :                   to_using = next;
    2462      9797459 :                   continue;
    2463              :                 }
    2464              : 
    2465      2059521 :               if (is_overloaded_fn (next))
    2466      1734101 :                 continue;
    2467              :             }
    2468              : 
    2469    277438625 :           if (DECL_DECLARES_TYPE_P (next))
    2470              :             {
    2471     77353290 :               to_type = next;
    2472     77353290 :               continue;
    2473              :             }
    2474              : 
    2475    200085335 :           if (name_independent_decl_p (next))
    2476          160 :             name_independent = jx + 1;
    2477    200085175 :           else if (!current)
    2478    199905568 :             current = next;
    2479              :         }
    2480              : 
    2481    284703864 :       if (to_using)
    2482              :         {
    2483      9671220 :           if (!current)
    2484              :             current = to_using;
    2485              :           else
    2486      1993872 :             current = ovl_make (to_using, current);
    2487              :         }
    2488              : 
    2489    284703864 :       if (to_type)
    2490              :         {
    2491     77060066 :           if (!current)
    2492              :             current = to_type;
    2493              :           else
    2494          159 :             current = stat_hack (current, to_type);
    2495              :         }
    2496              : 
    2497    284704024 :       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    284703864 :       if (current)
    2509              :         {
    2510    284642921 :           if (marker)
    2511              :             {
    2512      2128341 :               OVL_CHAIN (marker) = current;
    2513      2128341 :               current = marker;
    2514              :             }
    2515    284642921 :           (*member_vec)[store++] = current;
    2516              :         }
    2517              :     }
    2518              : 
    2519     30841412 :   while (store++ < len)
    2520      4636818 :     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     76360524 : set_class_bindings (tree klass, int extra)
    2531              : {
    2532     76360524 :   unsigned n_fields = count_class_fields (klass);
    2533     76360524 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2534              : 
    2535     76360524 :   if (member_vec || n_fields >= 8 || extra < 0)
    2536              :     {
    2537              :       /* Append the new fields.  */
    2538     26204569 :       vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
    2539     26204569 :       member_vec_append_class_fields (member_vec, klass);
    2540              :     }
    2541              : 
    2542     76360524 :   if (member_vec)
    2543              :     {
    2544     26204569 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2545     26204569 :       member_vec->qsort (member_name_cmp);
    2546     26204569 :       member_vec_dedup (member_vec);
    2547              :     }
    2548              : 
    2549     76360524 :   return member_vec;
    2550              : }
    2551              : 
    2552              : /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case.  */
    2553              : 
    2554              : void
    2555           42 : insert_late_enum_def_bindings (tree klass, tree enumtype)
    2556              : {
    2557           42 :   int n_fields;
    2558           42 :   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           42 :   if (!member_vec)
    2563           21 :     n_fields = count_class_fields (klass);
    2564              :   else
    2565           21 :     n_fields = list_length (TYPE_VALUES (enumtype));
    2566              : 
    2567           42 :   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           42 : }
    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   5245636501 : query_oracle (tree name)
    2589              : {
    2590   5245636501 :   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   1428587350 : cxx_binding_init (cxx_binding *binding, tree value, tree type)
    2617              : {
    2618   1428587350 :   binding->value = value;
    2619   1428587350 :   binding->type = type;
    2620   1428587350 :   binding->previous = NULL;
    2621              : }
    2622              : 
    2623              : /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
    2624              : 
    2625              : static cxx_binding *
    2626   1428587350 : cxx_binding_make (tree value, tree type)
    2627              : {
    2628   1428587350 :   cxx_binding *binding = free_bindings;
    2629              : 
    2630   1428587350 :   if (binding)
    2631   1419204023 :     free_bindings = binding->previous;
    2632              :   else
    2633      9383327 :     binding = ggc_alloc<cxx_binding> ();
    2634              : 
    2635              :   /* Clear flags by default.  */
    2636   1428587350 :   LOCAL_BINDING_P (binding) = false;
    2637   1428587350 :   INHERITED_VALUE_BINDING_P (binding) = false;
    2638   1428587350 :   HIDDEN_TYPE_BINDING_P (binding) = false;
    2639              : 
    2640   1428587350 :   cxx_binding_init (binding, value, type);
    2641              : 
    2642   1428587350 :   return binding;
    2643              : }
    2644              : 
    2645              : /* Put BINDING back on the free list.  */
    2646              : 
    2647              : static inline void
    2648   1428584485 : cxx_binding_free (cxx_binding *binding)
    2649              : {
    2650   1428584485 :   binding->scope = NULL;
    2651   1428584485 :   binding->previous = free_bindings;
    2652   1428584485 :   free_bindings = binding;
    2653    938915076 : }
    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    489672250 : new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
    2660              : {
    2661    489672250 :   cp_class_binding cb = {cxx_binding_make (value, type), name};
    2662    489672250 :   cxx_binding *binding = cb.base;
    2663    489672250 :   vec_safe_push (scope->class_shadowed, cb);
    2664    489672250 :   binding->scope = scope;
    2665    489672250 :   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    351458068 : push_binding (tree id, tree decl, cp_binding_level* level)
    2673              : {
    2674    351458068 :   cxx_binding *binding;
    2675              : 
    2676    351458068 :   if (level != class_binding_level)
    2677              :     {
    2678     13132922 :       binding = cxx_binding_make (decl, NULL_TREE);
    2679     13132922 :       binding->scope = level;
    2680              :     }
    2681              :   else
    2682    338325146 :     binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
    2683              : 
    2684              :   /* Now, fill in the binding information.  */
    2685    351458068 :   binding->previous = IDENTIFIER_BINDING (id);
    2686    351458068 :   LOCAL_BINDING_P (binding) = (level != class_binding_level);
    2687              : 
    2688              :   /* And put it on the front of the list of bindings for ID.  */
    2689    351458068 :   IDENTIFIER_BINDING (id) = binding;
    2690    351458068 : }
    2691              : 
    2692              : /* Remove the binding for DECL which should be the innermost binding
    2693              :    for ID.  */
    2694              : 
    2695              : void
    2696    962916929 : pop_local_binding (tree id, tree decl)
    2697              : {
    2698   1903450712 :   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    938915524 :   cxx_binding *binding = IDENTIFIER_BINDING (id);
    2706              : 
    2707              :   /* The name should be bound.  */
    2708    938915524 :   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    938915524 :   if (binding->value == decl)
    2715    938914702 :     binding->value = NULL_TREE;
    2716          822 :   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          672 :   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          671 :       gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
    2725          671 :       if (TREE_VALUE (binding->value) != decl)
    2726              :         {
    2727          261 :           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          270 :           while (TREE_PURPOSE (binding->value) == error_mark_node)
    2732            9 :             binding->value = TREE_CHAIN (binding->value);
    2733              :         }
    2734          671 :       gcc_assert (TREE_VALUE (binding->value) == decl);
    2735          671 :       binding->value = TREE_CHAIN (binding->value);
    2736          671 :       while (binding->value
    2737          766 :              && TREE_PURPOSE (binding->value) == error_mark_node)
    2738           95 :         binding->value = TREE_CHAIN (binding->value);
    2739              :     }
    2740              : 
    2741    938915524 :   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    938915076 :       IDENTIFIER_BINDING (id) = binding->previous;
    2746              : 
    2747              :       /* Add it to the free list.  */
    2748    938915076 :       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    206283537 : pop_bindings_and_leave_scope (void)
    2757              : {
    2758    471318769 :   for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
    2759              :     {
    2760    265035232 :       tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
    2761    530070464 :       tree name = OVL_NAME (decl);
    2762              : 
    2763    265035232 :       pop_local_binding (name, decl);
    2764              :     }
    2765              : 
    2766    206283537 :   leave_scope ();
    2767    206283537 : }
    2768              : 
    2769              : /* Strip non dependent using declarations. If DECL is dependent,
    2770              :    surreptitiously create a typename_type and return it.  */
    2771              : 
    2772              : tree
    2773   9119876427 : strip_using_decl (tree decl)
    2774              : {
    2775   9119876427 :   if (decl == NULL_TREE)
    2776              :     return NULL_TREE;
    2777              : 
    2778   6424406133 :   while (TREE_CODE (decl) == USING_DECL
    2779    118597835 :          && !DECL_DEPENDENT_P (decl)
    2780   6516458734 :          && (LIKELY (!cp_preserve_using_decl)
    2781         6310 :              || TREE_CODE (USING_DECL_DECLS (decl)) == NAMESPACE_DECL))
    2782     92052597 :     decl = USING_DECL_DECLS (decl);
    2783              : 
    2784     26545238 :   if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
    2785   6358898770 :       && 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       258583 :       decl = make_typename_type (USING_DECL_SCOPE (decl),
    2794       258583 :                                  DECL_NAME (decl),
    2795              :                                  typename_type, tf_error);
    2796       258583 :       if (decl != error_mark_node)
    2797       258583 :         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     23885577 : anticipated_builtin_p (tree ovl)
    2807              : {
    2808     23885577 :   return (TREE_CODE (ovl) == OVERLOAD
    2809     21871270 :           && OVL_HIDDEN_P (ovl)
    2810     30871870 :           && 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       225725 : supplement_binding (cxx_binding *binding, tree decl)
    2833              : {
    2834       225725 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    2835              : 
    2836       225725 :   tree bval = binding->value;
    2837       225725 :   bool ok = true;
    2838       225725 :   if (bval
    2839       225725 :       && TREE_CODE (bval) == TREE_LIST
    2840       225733 :       && name_independent_decl_p (TREE_VALUE (bval)))
    2841              :     bval = TREE_VALUE (bval);
    2842       225725 :   tree target_bval = strip_using_decl (bval);
    2843       225725 :   tree target_decl = strip_using_decl (decl);
    2844              : 
    2845        45747 :   if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
    2846        45564 :       && target_decl != target_bval
    2847       271280 :       && (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        45450 :           || (processing_template_decl
    2853         9158 :               && 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       225620 :   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       225620 :            || 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       451240 :            || anticipated_builtin_p (target_bval))
    2874            0 :     binding->value = decl;
    2875       225620 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2876        45822 :            && DECL_ARTIFICIAL (target_bval)
    2877        45789 :            && target_decl != target_bval
    2878       271400 :            && (TREE_CODE (target_decl) != TYPE_DECL
    2879        45600 :                || 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        45768 :       binding->type = bval;
    2888        45768 :       binding->value = decl;
    2889        45768 :       binding->value_is_inherited = false;
    2890              :     }
    2891       179852 :   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       179852 :            && (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       179852 :   else if (VAR_P (target_decl)
    2922            9 :            && VAR_P (target_bval)
    2923            9 :            && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
    2924       179858 :            && !DECL_CLASS_SCOPE_P (target_decl))
    2925              :     {
    2926            0 :       duplicate_decls (decl, binding->value);
    2927            0 :       ok = false;
    2928              :     }
    2929       179852 :   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       179852 :            && 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       179852 :   else if (TREE_CODE (bval) == USING_DECL
    2942       179852 :            && CONST_DECL_USING_P (decl))
    2943              :     /* Let the clone hide the using-decl that introduced it.  */
    2944       179653 :     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       451450 :   return ok;
    2967       225725 : }
    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      4812638 : matching_fn_p (tree one, tree two)
    3026              : {
    3027      4812638 :   if (TREE_CODE (one) != TREE_CODE (two))
    3028              :     return false;
    3029              : 
    3030      3002542 :   if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
    3031      3002542 :                   TYPE_ARG_TYPES (TREE_TYPE (two))))
    3032              :     return false;
    3033              : 
    3034          409 :   if (TREE_CODE (one) == TEMPLATE_DECL)
    3035              :     {
    3036              :       /* Compare template parms.  */
    3037          686 :       if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
    3038          343 :                                 DECL_TEMPLATE_PARMS (two)))
    3039              :         return false;
    3040              : 
    3041              :       /* And return type.  */
    3042          261 :       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   1296797096 : update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
    3059              :                 tree old, tree decl, bool hiding = false)
    3060              : {
    3061   1296797096 :   tree old_type = NULL_TREE;
    3062   1296797096 :   bool hide_type = false;
    3063   1296797096 :   bool hide_value = false;
    3064   1296797096 :   bool name_independent_p = false;
    3065              : 
    3066   1296797096 :   if (!slot)
    3067              :     {
    3068    925782737 :       old_type = binding->type;
    3069    925782737 :       hide_type = HIDDEN_TYPE_BINDING_P (binding);
    3070    925782737 :       if (!old_type)
    3071    925782734 :         hide_value = hide_type, hide_type = false;
    3072    925782737 :       name_independent_p = name_independent_decl_p (decl);
    3073              :     }
    3074    371014359 :   else if (STAT_HACK_P (*slot))
    3075              :     {
    3076          331 :       old_type = STAT_TYPE (*slot);
    3077          331 :       hide_type = STAT_TYPE_HIDDEN_P (*slot);
    3078          331 :       hide_value = STAT_DECL_HIDDEN_P (*slot);
    3079              :     }
    3080              : 
    3081   1296797096 :   tree to_val = decl;
    3082   1296797096 :   tree to_type = old_type;
    3083   1296797096 :   bool local_overload = false;
    3084              : 
    3085   1296797096 :   gcc_assert (!level || level->kind == sk_namespace ? !binding
    3086              :               : level->kind != sk_class && !slot);
    3087              : 
    3088   1296797096 :   if (old == error_mark_node)
    3089        98033 :     old = NULL_TREE;
    3090              : 
    3091   1296797096 :   tree old_bval = old;
    3092   1296797096 :   old = strip_using_decl (old);
    3093              : 
    3094   1296797096 :   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      6861588 :       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      6861588 :       goto done;
    3114              :     }
    3115              : 
    3116   1289935508 :   if (old && DECL_IMPLICIT_TYPEDEF_P (old))
    3117              :     {
    3118              :       /* OLD is an implicit typedef.  Move it to to_type.  */
    3119        68686 :       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   1289935508 :   if (DECL_DECLARES_FUNCTION_P (decl))
    3128              :     {
    3129    332821425 :       if (!old)
    3130              :         ;
    3131     23402812 :       else if (OVL_P (old))
    3132              :         {
    3133    531709884 :           for (ovl_iterator iter (old); iter; ++iter)
    3134              :             {
    3135    256199691 :               tree fn = *iter;
    3136              : 
    3137    256199691 :               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     23402804 :         }
    3155              :       else
    3156            0 :         goto conflict;
    3157              : 
    3158    332821417 :       if (to_type != old_type
    3159        37521 :           && warn_shadow
    3160            6 :           && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
    3161    332821423 :           && !(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    332821417 :       local_overload = old && level && level->kind != sk_namespace;
    3167    332821417 :       to_val = ovl_insert (decl, old, -int (hiding));
    3168              :     }
    3169    957114083 :   else if (old)
    3170              :     {
    3171          452 :       if (name_independent_p)
    3172          401 :         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    957113631 :   else if (hiding)
    3213        30929 :     hide_value = true;
    3214              : 
    3215   1296797070 :  done:
    3216   1296797070 :   if (to_val)
    3217              :     {
    3218   1296797037 :       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   1296796928 :       else if (level
    3224   1296796928 :                && !(TREE_CODE (decl) == NAMESPACE_DECL
    3225       999814 :                     && !DECL_NAMESPACE_ALIAS (decl)))
    3226              :         /* Don't add namespaces here.  They're done in
    3227              :            push_namespace.  */
    3228   1295917046 :         add_decl_to_level (level, decl);
    3229              : 
    3230   1296797037 :       if (slot)
    3231              :         {
    3232    371014321 :           if (STAT_HACK_P (*slot))
    3233              :             {
    3234          331 :               STAT_TYPE (*slot) = to_type;
    3235          331 :               STAT_DECL (*slot) = to_val;
    3236          331 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    3237          331 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    3238              :             }
    3239    371013990 :           else if (to_type || hide_value)
    3240              :             {
    3241       211481 :               *slot = stat_hack (to_val, to_type);
    3242       211481 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    3243       211481 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    3244              :             }
    3245              :           else
    3246              :             {
    3247    370802509 :               gcc_checking_assert (!hide_type);
    3248    370802509 :               *slot = to_val;
    3249              :             }
    3250              :         }
    3251              :       else
    3252              :         {
    3253    925782716 :           binding->type = to_type;
    3254    925782716 :           binding->value = to_val;
    3255    925782716 :           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    290764294 : check_extern_c_conflict (tree decl)
    3272              : {
    3273              :   /* Ignore artificial or system header decls.  */
    3274    290764294 :   if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
    3275    290550260 :     return;
    3276              : 
    3277              :   /* This only applies to decls at namespace scope.  */
    3278       214034 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
    3279              :     return;
    3280              : 
    3281       214022 :   if (!extern_c_decls)
    3282        39885 :     extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
    3283              : 
    3284       214022 :   tree *slot = extern_c_decls
    3285       214022 :     ->find_slot_with_hash (DECL_NAME (decl),
    3286       214022 :                            IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
    3287       214022 :   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       213625 :     *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    925782737 : 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    925782737 :   if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
    3375              :     return NULL_TREE;
    3376              : 
    3377    692753749 :   if (DECL_FUNCTION_SCOPE_P (decl))
    3378              :     {
    3379    448540808 :       tree ctx = DECL_CONTEXT (decl);
    3380    897081616 :       if (DECL_CLONED_FUNCTION_P (ctx)
    3381    418911848 :           || DECL_TEMPLATE_INSTANTIATED (ctx)
    3382    340671829 :           || (DECL_LANG_SPECIFIC (ctx)
    3383    340671829 :               && DECL_DEFAULTED_FN (ctx))
    3384    828605769 :           || (LAMBDA_FUNCTION_P (ctx)
    3385     16672610 :               && 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    579761431 :   tree old = NULL_TREE;
    3395    579761431 :   cp_binding_level *old_scope = NULL;
    3396    579761431 :   if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
    3397              :     {
    3398      2736124 :       old = binding->value;
    3399      2736124 :       old_scope = binding->scope;
    3400              :     }
    3401              : 
    3402      2736124 :   if (old
    3403      2736124 :       && (TREE_CODE (old) == PARM_DECL
    3404      1388741 :           || VAR_P (old)
    3405       152576 :           || (TREE_CODE (old) == TYPE_DECL
    3406        67897 :               && (!DECL_ARTIFICIAL (old)
    3407        55132 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3408      2651351 :       && DECL_FUNCTION_SCOPE_P (old)
    3409      5330521 :       && (!DECL_ARTIFICIAL (decl)
    3410      2383804 :           || is_capture_proxy (decl)
    3411       373411 :           || DECL_IMPLICIT_TYPEDEF_P (decl)
    3412       373381 :           || (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      2221022 :       if (is_capture_proxy (decl))
    3419              :         {
    3420      2010393 :           if (current_lambda_expr ()
    3421      2010393 :               && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
    3422           61 :               && TREE_CODE (old) == PARM_DECL
    3423      2010418 :               && 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      2010393 :           return NULL_TREE;
    3428              :         }
    3429              :       /* Don't complain if it's from an enclosing function.  */
    3430       210629 :       else if (DECL_CONTEXT (old) == current_function_decl
    3431       210629 :                && ((TREE_CODE (decl) != PARM_DECL
    3432       165770 :                     && TREE_CODE (old) == PARM_DECL)
    3433              :                    /* We should also give an error for
    3434              :                        [x=1]{ int x; }  */
    3435       155990 :                    || (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         9786 :           cp_binding_level *b = current_binding_level->level_chain;
    3441              : 
    3442         9786 :           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         9873 :           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         9786 :           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       210509 :       if (DECL_CONTEXT (old) != current_function_decl)
    3477              :         {
    3478        44859 :           for (cp_binding_level *scope = current_binding_level;
    3479       107682 :                scope != old_scope; scope = scope->level_chain)
    3480       106082 :             if (scope->kind == sk_class
    3481       152598 :                 && !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       165650 :       else if (VAR_P (old)
    3491       145105 :                && old_scope == current_binding_level->level_chain
    3492         2780 :                && (old_scope->kind == sk_cond
    3493         2675 :                    || old_scope->kind == sk_for
    3494         2576 :                    || 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       165425 :       else if (TREE_CODE (old) == VAR_DECL
    3519       144880 :                && old_scope == current_binding_level->level_chain
    3520         2555 :                && 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       167007 :       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       166835 :       enum opt_code warning_code;
    3558       166835 :       if (warn_shadow)
    3559              :         warning_code = OPT_Wshadow;
    3560       166766 :       else if ((TREE_CODE (decl) == TYPE_DECL)
    3561       166766 :                ^ (TREE_CODE (old) == TYPE_DECL))
    3562              :         /* If exactly one is a type, they aren't compatible.  */
    3563              :         warning_code = OPT_Wshadow_local;
    3564       166745 :       else if ((TREE_TYPE (old)
    3565       166745 :                 && TREE_TYPE (decl)
    3566       166742 :                 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    3567        33371 :                || TREE_CODE (decl) == TYPE_DECL
    3568        22993 :                || TREE_CODE (old) == TYPE_DECL
    3569       189738 :                || (!dependent_type_p (TREE_TYPE (decl))
    3570         4435 :                    && !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         4408 :                    && !type_uses_auto (TREE_TYPE (decl))
    3578         4137 :                    && 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       166835 :       const char *msg;
    3585       166835 :       if (TREE_CODE (old) == PARM_DECL)
    3586              :         msg = "declaration of %q#D shadows a parameter";
    3587       155942 :       else if (is_capture_proxy (old))
    3588              :         msg = "declaration of %qD shadows a lambda capture";
    3589              :       else
    3590       155755 :         msg = "declaration of %qD shadows a previous local";
    3591              : 
    3592       166835 :       auto_diagnostic_group d;
    3593       166835 :       if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
    3594          117 :         inform_shadowed (old);
    3595       166835 :       return NULL_TREE;
    3596       166835 :     }
    3597              : 
    3598    577540409 :   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    433610612 : set_decl_context_in_fn (tree ctx, tree decl)
    3669              : {
    3670    433610612 :   if (TREE_CODE (decl) == FUNCTION_DECL
    3671    433610612 :       || (VAR_P (decl) && DECL_EXTERNAL (decl)))
    3672              :     /* Make sure local externs are marked as such.  OMP UDRs really
    3673              :        are nested functions.  */
    3674        59914 :     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    433610612 :   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    434808775 :       && !(TREE_CODE (decl) == PARM_DECL
    3683      1198163 :            && parsing_function_declarator ()))
    3684    101076135 :     DECL_CONTEXT (decl) = ctx;
    3685    433610612 : }
    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        59467 : push_local_extern_decl_alias (tree decl)
    3692              : {
    3693        59467 :   if (dependent_type_p (TREE_TYPE (decl))
    3694        59467 :       || (processing_template_decl
    3695        38606 :           && VAR_P (decl)
    3696           43 :           && CP_DECL_THREAD_LOCAL_P (decl)))
    3697              :     return;
    3698              :   /* EH specs were not part of the function type prior to c++17, but
    3699              :      we still can't go pushing dependent eh specs into the namespace.  */
    3700        59380 :   if (cxx_dialect < cxx17
    3701         2374 :       && TREE_CODE (decl) == FUNCTION_DECL
    3702        61374 :       && (value_dependent_expression_p
    3703         1994 :           (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
    3704              :     return;
    3705              : 
    3706        59379 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
    3707              :                        || !DECL_TEMPLATE_INFO (decl));
    3708        59379 :   if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
    3709              :     /* We're instantiating a non-dependent local decl, it already
    3710              :        knows the alias.  */
    3711              :     return;
    3712              : 
    3713        59147 :   tree alias = NULL_TREE;
    3714              : 
    3715        59147 :   if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
    3716              :     /* Do not let a VLA creep into a namespace.  Diagnostic will be
    3717              :        emitted in layout_var_decl later.  */
    3718            3 :     alias = error_mark_node;
    3719              :   else
    3720              :     {
    3721              :       /* First look for a decl that matches.  */
    3722        59144 :       tree ns = CP_DECL_CONTEXT (decl);
    3723        59144 :       tree binding = find_namespace_value (ns, DECL_NAME (decl));
    3724              : 
    3725        59144 :       if (binding && TREE_CODE (binding) != TREE_LIST)
    3726          964 :         for (ovl_iterator iter (binding); iter; ++iter)
    3727          668 :           if (decls_match (decl, *iter, /*record_versions*/false))
    3728              :             {
    3729          455 :               alias = *iter;
    3730          455 :               if (!validate_constexpr_redeclaration (alias, decl))
    3731           12 :                 return;
    3732          443 :               if (TREE_CODE (decl) == FUNCTION_DECL)
    3733          178 :                 merge_decl_arguments (decl, alias, false, true, true);
    3734              :               break;
    3735              :             }
    3736              : 
    3737          556 :       if (!alias)
    3738              :         {
    3739              :           /* No existing namespace-scope decl.  Make one.  */
    3740        58689 :           alias = copy_decl (decl);
    3741        58689 :           if (TREE_CODE (alias) == FUNCTION_DECL)
    3742              :             {
    3743              :               /* Recontextualize the parms.  */
    3744        57864 :               for (tree *chain = &DECL_ARGUMENTS (alias);
    3745       103449 :                    *chain; chain = &DECL_CHAIN (*chain))
    3746              :                 {
    3747        45585 :                   tree next = DECL_CHAIN (*chain);
    3748        45585 :                   *chain = copy_decl (*chain);
    3749        45585 :                   DECL_CHAIN (*chain) = next;
    3750        45585 :                   DECL_CONTEXT (*chain) = alias;
    3751              :                 }
    3752              : 
    3753        57864 :               tree type = TREE_TYPE (alias);
    3754        57864 :               for (tree args = TYPE_ARG_TYPES (type);
    3755       161198 :                    args; args = TREE_CHAIN (args))
    3756       103379 :                 if (TREE_PURPOSE (args))
    3757              :                   {
    3758              :                     /* There are default args.  Lose them.  */
    3759           45 :                     tree nargs = NULL_TREE;
    3760           45 :                     tree *chain = &nargs;
    3761           45 :                     for (args = TYPE_ARG_TYPES (type);
    3762          111 :                          args; args = TREE_CHAIN (args))
    3763          111 :                       if (args == void_list_node)
    3764              :                         {
    3765           45 :                           *chain = args;
    3766           45 :                           break;
    3767              :                         }
    3768              :                       else
    3769              :                         {
    3770           66 :                           *chain
    3771           66 :                             = build_tree_list (NULL_TREE, TREE_VALUE (args));
    3772           66 :                           chain = &TREE_CHAIN (*chain);
    3773              :                         }
    3774              : 
    3775           45 :                     bool no_named_args_stdarg
    3776           45 :                       = TYPE_NO_NAMED_ARGS_STDARG_P (type);
    3777           45 :                     tree fn_type
    3778           45 :                       = build_function_type (TREE_TYPE (type), nargs,
    3779              :                                              no_named_args_stdarg);
    3780              : 
    3781           45 :                     fn_type = apply_memfn_quals
    3782           45 :                       (fn_type, type_memfn_quals (type));
    3783              : 
    3784           45 :                     fn_type = build_cp_fntype_variant
    3785           45 :                       (fn_type, type_memfn_rqual (type),
    3786           45 :                        TYPE_RAISES_EXCEPTIONS (type),
    3787           45 :                        TYPE_HAS_LATE_RETURN_TYPE (type));
    3788              : 
    3789           45 :                     TREE_TYPE (alias) = fn_type;
    3790           45 :                     break;
    3791              :                   }
    3792              :             }
    3793              : 
    3794              :           /* This is the real thing.  */
    3795        58689 :           DECL_LOCAL_DECL_P (alias) = false;
    3796              : 
    3797              :           /* Expected default linkage is from the namespace.  */
    3798        58689 :           TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
    3799        58689 :           push_nested_namespace (ns);
    3800        58689 :           alias = pushdecl (alias, /* hiding= */true);
    3801        58689 :           pop_nested_namespace (ns);
    3802        58689 :           if (VAR_P (decl)
    3803          825 :               && CP_DECL_THREAD_LOCAL_P (decl)
    3804        58710 :               && alias != error_mark_node)
    3805           18 :             set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
    3806              : 
    3807              :           /* Adjust visibility.  */
    3808        58689 :           determine_visibility (alias);
    3809              :         }
    3810              :     }
    3811              : 
    3812        59135 :   retrofit_lang_decl (decl);
    3813        59135 :   DECL_LOCAL_DECL_ALIAS (decl) = alias;
    3814              : }
    3815              : 
    3816              : /* If DECL has non-internal linkage, and we have a module vector,
    3817              :    record it in the appropriate slot.  We have already checked for
    3818              :    duplicates.  */
    3819              : 
    3820              : static void
    3821       213481 : maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
    3822              : {
    3823       213481 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
    3824              :     return;
    3825              : 
    3826           91 :   if (decl_linkage (decl) == lk_internal)
    3827              :     return;
    3828              : 
    3829           91 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    3830           91 :   bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
    3831          182 :                       && DECL_MODULE_ATTACH_P (not_tmpl));
    3832              :   tree *gslot = get_fixed_binding_slot
    3833           91 :     (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    3834              :      true);
    3835              : 
    3836              :   /* A namespace is always global module so there's no need to mark
    3837              :      the current binding slot as such.  */
    3838           91 :   if (!is_attached && TREE_CODE (decl) != NAMESPACE_DECL)
    3839              :     {
    3840           85 :       binding_slot &orig
    3841           85 :         = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
    3842              : 
    3843           85 :       if (!STAT_HACK_P (tree (orig)))
    3844           21 :         orig = stat_hack (tree (orig));
    3845              : 
    3846           85 :       MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
    3847              :     }
    3848              : 
    3849           91 :   add_mergeable_namespace_entity (gslot, decl);
    3850              : }
    3851              : 
    3852              : /* DECL is being pushed.  Check whether it hides or ambiguates
    3853              :    something seen as an import.  This include decls seen in our own
    3854              :    interface, which is OK.  Also, check for merging a
    3855              :    global/partition decl.  */
    3856              : 
    3857              : static tree
    3858         1086 : check_module_override (tree decl, tree mvec, bool hiding,
    3859              :                        tree scope, tree name)
    3860              : {
    3861         1086 :   tree match = NULL_TREE;
    3862         1086 :   bitmap imports = get_import_bitmap ();
    3863         1086 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
    3864         1086 :   unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
    3865              : 
    3866         1086 :   tree nontmpl = STRIP_TEMPLATE (decl);
    3867         2106 :   bool attached = DECL_LANG_SPECIFIC (nontmpl) && DECL_MODULE_ATTACH_P (nontmpl);
    3868              : 
    3869              :   /* For deduction guides we don't do normal name lookup, but rather consider
    3870              :      any reachable declaration, so we should check for overriding here too.  */
    3871         1086 :   bool any_reachable = deduction_guide_p (decl);
    3872              : 
    3873              :   /* DECL might have an originating module if it's an instantiation of a
    3874              :      friend; we want to look at all reachable decls in that module.  */
    3875         1086 :   unsigned decl_mod = get_originating_module (decl);
    3876              : 
    3877         1086 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    3878              :     {
    3879         1086 :       cluster++;
    3880         1086 :       ix--;
    3881              :     }
    3882              : 
    3883         1763 :   for (; ix--; cluster++)
    3884         2864 :     for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    3885              :       {
    3886              :         /* Are we importing this module?  */
    3887         2187 :         if (cluster->indices[jx].span != 1)
    3888          515 :           continue;
    3889         1672 :         unsigned cluster_mod = cluster->indices[jx].base;
    3890         1672 :         if (!cluster_mod)
    3891         1086 :           continue;
    3892          586 :         bool c_any_reachable = (any_reachable || cluster_mod == decl_mod);
    3893          586 :         if (!c_any_reachable && !bitmap_bit_p (imports, cluster_mod))
    3894           12 :           continue;
    3895              :         /* Is it loaded? */
    3896          574 :         if (cluster->slots[jx].is_lazy ())
    3897           18 :           lazy_load_binding (cluster_mod, scope, name, &cluster->slots[jx]);
    3898          574 :         tree bind = cluster->slots[jx];
    3899          574 :         if (!bind)
    3900              :           /* Errors could cause there to be nothing.  */
    3901            0 :           continue;
    3902              : 
    3903          574 :         tree type = NULL_TREE;
    3904          574 :         if (STAT_HACK_P (bind))
    3905              :           {
    3906              :             /* If there was a matching STAT_TYPE here then xref_tag
    3907              :                should have found it, but we need to check anyway because
    3908              :                a conflicting using-declaration may exist.  */
    3909          556 :             if (c_any_reachable)
    3910              :               {
    3911          120 :                 type = STAT_TYPE (bind);
    3912          120 :                 bind = STAT_DECL (bind);
    3913              :               }
    3914              :             else
    3915              :               {
    3916          436 :                 if (STAT_TYPE_VISIBLE_P (bind))
    3917           92 :                   type = STAT_TYPE (bind);
    3918          436 :                 bind = STAT_VISIBLE (bind);
    3919              :               }
    3920              :           }
    3921              : 
    3922          556 :         if (type)
    3923              :           {
    3924            3 :             match = duplicate_decls (decl, strip_using_decl (type), hiding);
    3925            3 :             if (match)
    3926            0 :               goto matched;
    3927              :           }
    3928              : 
    3929         1679 :         for (ovl_iterator iter (strip_using_decl (bind)); iter; ++iter)
    3930              :           {
    3931          984 :             match = duplicate_decls (decl, *iter, hiding);
    3932          984 :             if (match)
    3933          418 :               goto matched;
    3934              :           }
    3935              :       }
    3936              : 
    3937          668 :   if (TREE_PUBLIC (scope)
    3938              :       /* Namespaces are dealt with specially in
    3939              :          make_namespace_finish.  */
    3940          668 :       && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
    3941              :     {
    3942              :       /* Look in the appropriate mergeable decl slot.  */
    3943          632 :       tree mergeable = NULL_TREE;
    3944          632 :       if (attached)
    3945           42 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
    3946              :                                            / BINDING_VECTOR_SLOTS_PER_CLUSTER)
    3947           21 :           .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
    3948              :       else
    3949          611 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
    3950              : 
    3951        10667 :       for (ovl_iterator iter (mergeable); iter; ++iter)
    3952              :         {
    3953         5215 :           match = duplicate_decls (decl, *iter, hiding);
    3954         5215 :           if (match)
    3955           72 :             goto matched;
    3956              :         }
    3957              :     }
    3958              : 
    3959              :   return NULL_TREE;
    3960              : 
    3961          490 :  matched:
    3962          490 :   if (match != error_mark_node)
    3963              :     {
    3964          439 :       if (attached)
    3965          113 :         BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
    3966              :       else
    3967          326 :         BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
    3968              :     }
    3969              : 
    3970              :   return match;
    3971              : }
    3972              : 
    3973              : /* Record DECL as belonging to the current lexical scope.  Check for
    3974              :    errors (such as an incompatible declaration for the same name
    3975              :    already seen in the same scope).
    3976              : 
    3977              :    The new binding is hidden if HIDING is true (an anticipated builtin
    3978              :    or hidden friend).
    3979              : 
    3980              :    Returns either DECL or an old decl for the same name.  If an old
    3981              :    decl is returned, it may have been smashed to agree with what DECL
    3982              :    says.  */
    3983              : 
    3984              : tree
    3985   1332425172 : pushdecl (tree decl, bool hiding)
    3986              : {
    3987   1332425172 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    3988              : 
    3989   1332425172 :   if (decl == error_mark_node)
    3990              :     return error_mark_node;
    3991              : 
    3992   1332425166 :   if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
    3993    433610612 :     set_decl_context_in_fn (current_function_decl, decl);
    3994              : 
    3995              :   /* The binding level we will be pushing into.  During local class
    3996              :      pushing, we want to push to the containing scope.  */
    3997   1332425166 :   cp_binding_level *level = current_binding_level;
    3998   1332425311 :   while (level->kind == sk_class
    3999   1332425311 :          || level->kind == sk_cleanup)
    4000          145 :     level = level->level_chain;
    4001              : 
    4002              :   /* An anonymous namespace has a NULL DECL_NAME, but we still want to
    4003              :      insert it.  Other NULL-named decls, not so much.  */
    4004   1332425166 :   tree name = DECL_NAME (decl);
    4005   2641680918 :   if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
    4006              :     {
    4007   1306909732 :       cxx_binding *binding = NULL; /* Local scope binding.  */
    4008   1306909732 :       tree ns = NULL_TREE; /* Searched namespace.  */
    4009   1306909732 :       tree *slot = NULL; /* Binding slot in namespace.  */
    4010   1306909732 :       tree *mslot = NULL; /* Current module slot in namespace.  */
    4011   1306909732 :       tree old = NULL_TREE;
    4012   1306909732 :       bool name_independent_p = false;
    4013   1306909732 :       bool name_independent_diagnosed_p = false;
    4014              : 
    4015   1306909732 :       if (level->kind == sk_namespace)
    4016              :         {
    4017              :           /* We look in the decl's namespace for an existing
    4018              :              declaration, even though we push into the current
    4019              :              namespace.  */
    4020   1030617027 :           ns = (DECL_NAMESPACE_SCOPE_P (decl)
    4021    762248974 :                 ? CP_DECL_CONTEXT (decl) : current_namespace);
    4022              :           /* Create the binding, if this is current namespace, because
    4023              :              that's where we'll be pushing anyway.  */
    4024    381124487 :           slot = find_namespace_slot (ns, name, ns == current_namespace);
    4025    381124487 :           if (slot)
    4026              :             {
    4027    762248900 :               mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
    4028    381124450 :                                               ns == current_namespace);
    4029    381124450 :               old = MAYBE_STAT_DECL (*mslot);
    4030              :             }
    4031              :         }
    4032              :       else
    4033              :         {
    4034    925785245 :           binding = find_local_binding (level, name);
    4035    925785245 :           if (binding)
    4036         3061 :             old = binding->value;
    4037    925785245 :           name_independent_p = name_independent_decl_p (decl);
    4038              :         }
    4039              : 
    4040   1306909732 :       if (old == error_mark_node)
    4041        98033 :         old = NULL_TREE;
    4042              : 
    4043   1306909732 :       tree oldi, oldn;
    4044   1330507870 :       for (oldi = old; oldi; oldi = oldn)
    4045              :         {
    4046     33710714 :           if (TREE_CODE (oldi) == TREE_LIST)
    4047              :             {
    4048          120 :               gcc_checking_assert (level->kind != sk_namespace
    4049              :                                    && name_independent_decl_p
    4050              :                                                         (TREE_VALUE (old)));
    4051           60 :               oldn = TREE_CHAIN (oldi);
    4052           60 :               oldi = TREE_VALUE (oldi);
    4053              :             }
    4054              :           else
    4055              :             oldn = NULL_TREE;
    4056    581851526 :           for (ovl_iterator iter (oldi); iter; ++iter)
    4057    286268220 :             if (iter.using_p ())
    4058              :               ; /* Ignore using decls here.  */
    4059    282770923 :             else if (iter.hidden_p ()
    4060    101640043 :                      && TREE_CODE (*iter) == FUNCTION_DECL
    4061     77923820 :                      && DECL_LANG_SPECIFIC (*iter)
    4062    360694743 :                      && DECL_MODULE_IMPORT_P (*iter))
    4063              :               ; /* An undeclared builtin imported from elsewhere.  */
    4064    282770920 :             else if (name_independent_p)
    4065              :               {
    4066              :                 /* Ignore name-independent declarations.  */
    4067          360 :                 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
    4068          276 :                   pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
    4069              :                            "name-independent declarations only available with "
    4070              :                            "%<-std=c++2c%> or %<-std=gnu++2c%>");
    4071              :                 name_independent_diagnosed_p = true;
    4072              :               }
    4073    565541120 :             else if (tree match
    4074    282770560 :                      = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
    4075              :               {
    4076     10112576 :                 if (match == error_mark_node)
    4077              :                   ;
    4078     10111546 :                 else if (TREE_CODE (match) == TYPE_DECL)
    4079        46109 :                   gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
    4080              :                                        == (level->kind == sk_namespace
    4081              :                                            ? NULL_TREE : TREE_TYPE (match)));
    4082     10065437 :                 else if (iter.hidden_p () && !hiding)
    4083              :                   {
    4084              :                     /* Unhiding a previously hidden decl.  */
    4085      3886273 :                     tree head = iter.reveal_node (oldi);
    4086      3886273 :                     if (head != oldi)
    4087              :                       {
    4088        17341 :                         gcc_checking_assert (ns);
    4089        17341 :                         if (STAT_HACK_P (*slot))
    4090            0 :                           STAT_DECL (*slot) = head;
    4091              :                         else
    4092        17341 :                           *slot = head;
    4093              :                       }
    4094      3886273 :                     if (DECL_EXTERN_C_P (match))
    4095              :                       /* We need to check and register the decl now.  */
    4096      3619422 :                       check_extern_c_conflict (match);
    4097              :                   }
    4098      6179164 :                 else if (slot
    4099      6179164 :                          && !hiding
    4100      4632630 :                          && STAT_HACK_P (*slot)
    4101      6179189 :                          && STAT_DECL_HIDDEN_P (*slot))
    4102              :                   {
    4103              :                     /* Unhide the non-function.  */
    4104           25 :                     gcc_checking_assert (oldi == match);
    4105           25 :                     if (!STAT_TYPE (*slot))
    4106           25 :                       *slot = match;
    4107              :                     else
    4108            0 :                       STAT_DECL (*slot) = match;
    4109              :                   }
    4110     10112576 :                 return match;
    4111              :               }
    4112              :         }
    4113              : 
    4114              :       /* Skip a hidden builtin we failed to match already.  There can
    4115              :          only be one.  */
    4116   1296797156 :       if (old && anticipated_builtin_p (old))
    4117       818254 :         old = OVL_CHAIN (old);
    4118              : 
    4119              :       /* Check for redeclaring an import.  */
    4120   1296797156 :       if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
    4121         2172 :         if (tree match
    4122         1086 :             = check_module_override (decl, *slot, hiding, ns, name))
    4123              :           {
    4124          490 :             if (match == error_mark_node)
    4125              :               return match;
    4126              : 
    4127              :             /* We found a decl in an interface, push it into this
    4128              :                binding.  */
    4129          439 :             decl = update_binding (NULL, binding, mslot, old,
    4130              :                                    match, hiding);
    4131              : 
    4132          439 :             return decl;
    4133              :           }
    4134              : 
    4135              :       /* We are pushing a new decl.  */
    4136              : 
    4137   1296796666 :       if (hiding)
    4138              :         ; /* Hidden bindings don't shadow anything.  */
    4139              :       else
    4140   1218341036 :         check_template_shadow (decl);
    4141              : 
    4142   1296796666 :       if (DECL_DECLARES_FUNCTION_P (decl))
    4143              :         {
    4144    332821102 :           check_default_args (decl);
    4145              : 
    4146    332821102 :           if (hiding)
    4147              :             {
    4148     78321802 :               if (level->kind != sk_namespace)
    4149              :                 {
    4150              :                   /* In a local class, a friend function declaration must
    4151              :                      find a matching decl in the innermost non-class scope.
    4152              :                      [class.friend/11] */
    4153            9 :                   error_at (DECL_SOURCE_LOCATION (decl),
    4154              :                             "friend declaration %qD in local class without "
    4155              :                             "prior local declaration", decl);
    4156              :                   /* Don't attempt to push it.  */
    4157            9 :                   return error_mark_node;
    4158              :                 }
    4159              :             }
    4160              :         }
    4161              : 
    4162   1296796657 :       if (level->kind != sk_namespace)
    4163              :         {
    4164    925782737 :           tree local_shadow = check_local_shadow (decl);
    4165    925782737 :           if (name_independent_p && local_shadow)
    4166              :             {
    4167          104 :               if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
    4168          100 :                 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
    4169              :                          "name-independent declarations only available with "
    4170              :                          "%<-std=c++2c%> or %<-std=gnu++2c%>");
    4171          104 :               name_independent_diagnosed_p = true;
    4172              :               /* When a name-independent declaration is pushed into a scope
    4173              :                  which itself does not contain a _ named declaration yet (so
    4174              :                  _ name lookups wouldn't be normally ambiguous), but it
    4175              :                  shadows a _ declaration in some outer scope in cases
    4176              :                  described in [basic.scope.block]/2 where if the names of
    4177              :                  the shadowed and shadowing declarations were different it
    4178              :                  would be ill-formed program, arrange for _ name lookups
    4179              :                  in this scope to be ambiguous.  */
    4180          104 :               if (old == NULL_TREE)
    4181              :                 {
    4182          104 :                   old = build_tree_list (error_mark_node, local_shadow);
    4183          104 :                   TREE_TYPE (old) = error_mark_node;
    4184              :                 }
    4185              :             }
    4186              : 
    4187    925782737 :           if (TREE_CODE (decl) == NAMESPACE_DECL)
    4188              :             /* A local namespace alias.  */
    4189       108428 :             set_identifier_type_value_with_scope (name, NULL_TREE, level);
    4190              : 
    4191    925782737 :           if (!binding)
    4192    925782178 :             binding = create_local_binding (level, name);
    4193              :         }
    4194    371013920 :       else if (!slot)
    4195              :         {
    4196           37 :           ns = current_namespace;
    4197           37 :           slot = find_namespace_slot (ns, name, true);
    4198           37 :           mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
    4199              :           /* Update OLD to reflect the namespace we're going to be
    4200              :              pushing into.  */
    4201           37 :           old = MAYBE_STAT_DECL (*mslot);
    4202              :         }
    4203              : 
    4204   1296796657 :       old = update_binding (level, binding, mslot, old, decl, hiding);
    4205              : 
    4206   1296796657 :       if (old != decl)
    4207              :         /* An existing decl matched, use it.  */
    4208              :         decl = old;
    4209              :       else
    4210              :         {
    4211   1296796633 :           if (TREE_CODE (decl) == TYPE_DECL)
    4212              :             {
    4213    251071696 :               tree type = TREE_TYPE (decl);
    4214              : 
    4215    251071696 :               if (type != error_mark_node)
    4216              :                 {
    4217    251071628 :                   if (TYPE_NAME (type) != decl)
    4218     22135409 :                     set_underlying_type (decl);
    4219              : 
    4220    251071628 :                   set_identifier_type_value_with_scope (name, decl, level);
    4221              : 
    4222    251071628 :                   if (level->kind != sk_namespace
    4223    251071628 :                       && !instantiating_current_function_p ())
    4224              :                     /* This is a locally defined typedef in a function that
    4225              :                        is not a template instantation, record it to implement
    4226              :                        -Wunused-local-typedefs.  */
    4227    233682582 :                     record_locally_defined_typedef (decl);
    4228              :                 }
    4229              :             }
    4230   1045724937 :           else if (VAR_OR_FUNCTION_DECL_P (decl))
    4231              :             {
    4232    404360550 :               if (DECL_EXTERN_C_P (decl))
    4233    287136108 :                 check_extern_c_conflict (decl);
    4234              : 
    4235    404360550 :               if (!DECL_LOCAL_DECL_P (decl)
    4236    404360550 :                   && VAR_P (decl))
    4237     96333302 :                 maybe_register_incomplete_var (decl);
    4238              : 
    4239    404360550 :               if (DECL_LOCAL_DECL_P (decl)
    4240    404360550 :                   && NAMESPACE_SCOPE_P (decl))
    4241        59461 :                 push_local_extern_decl_alias (decl);
    4242              :             }
    4243              : 
    4244   1296796633 :           if (level->kind == sk_namespace
    4245    371013902 :               && TREE_PUBLIC (level->this_entity)
    4246   1667805712 :               && module_maybe_has_cmi_p ())
    4247       213481 :             maybe_record_mergeable_decl (slot, name, decl);
    4248              :         }
    4249              :     }
    4250              :   else
    4251     25515434 :     add_decl_to_level (level, decl);
    4252              : 
    4253              :   return decl;
    4254   1332425172 : }
    4255              : 
    4256              : /* A mergeable entity is being loaded into namespace NS slot NAME.
    4257              :    Create and return the appropriate vector slot for that.  Either a
    4258              :    GMF slot or a module-specific one.  */
    4259              : 
    4260              : tree *
    4261       137912 : mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
    4262              : {
    4263       137912 :   tree *mslot = find_namespace_slot (ns, name, true);
    4264       137912 :   tree *vslot = get_fixed_binding_slot
    4265       275059 :     (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    4266              :      true);
    4267              : 
    4268       137912 :   gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
    4269       137912 :   *vec = *mslot;
    4270              : 
    4271       137912 :   return vslot;
    4272              : }
    4273              : 
    4274              : /* DECL is a new mergeable namespace-scope decl.  Add it to the
    4275              :    mergeable entities on GSLOT.  */
    4276              : 
    4277              : void
    4278        61986 : add_mergeable_namespace_entity (tree *gslot, tree decl)
    4279              : {
    4280        61986 :   *gslot = ovl_make (decl, *gslot);
    4281        61986 : }
    4282              : 
    4283              : /* A mergeable entity of KLASS called NAME is being loaded.  Return
    4284              :    the set of things it could be.  All such non-as_base classes have
    4285              :    been given a member vec.  */
    4286              : 
    4287              : tree
    4288       161685 : lookup_class_binding (tree klass, tree name)
    4289              : {
    4290       161685 :   tree found = NULL_TREE;
    4291              : 
    4292       161685 :   if (!COMPLETE_TYPE_P (klass))
    4293              :     ;
    4294       161685 :   else if (TYPE_LANG_SPECIFIC (klass))
    4295              :     {
    4296       160253 :       vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    4297              : 
    4298       160253 :       found = member_vec_binary_search (member_vec, name);
    4299       160253 :       if (!found)
    4300              :         ;
    4301       159840 :       else if (STAT_HACK_P (found))
    4302              :         /* Rearrange the stat hack so that we don't need to expose that
    4303              :            internal detail.  */
    4304            6 :         found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
    4305       159834 :       else if (IDENTIFIER_CONV_OP_P (name))
    4306              :         {
    4307          836 :           gcc_checking_assert (name == conv_op_identifier);
    4308          836 :           found = OVL_CHAIN (found);
    4309              :         }
    4310              :     }
    4311              :   else
    4312              :     {
    4313         1432 :       gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
    4314              :                            || TYPE_PTRMEMFUNC_P (klass));
    4315         1432 :       found = fields_linear_search (klass, name, false);
    4316              :     }
    4317              : 
    4318       161685 :   return found;
    4319              : }
    4320              : 
    4321              : /* Whether this using is declared in the module purview.  */
    4322              : 
    4323              : bool
    4324        21572 : ovl_iterator::purview_p () const
    4325              : {
    4326        21572 :   gcc_checking_assert (using_p ());
    4327        21572 :   if (TREE_CODE (ovl) == USING_DECL)
    4328         4404 :     return DECL_MODULE_PURVIEW_P (ovl);
    4329        17168 :   return OVL_PURVIEW_P (ovl);
    4330              : }
    4331              : 
    4332              : /* Whether this using is exported from this module.  */
    4333              : 
    4334              : bool
    4335        21572 : ovl_iterator::exporting_p () const
    4336              : {
    4337        21572 :   gcc_checking_assert (using_p ());
    4338        21572 :   if (TREE_CODE (ovl) == USING_DECL)
    4339         4404 :     return DECL_MODULE_EXPORT_P (ovl);
    4340        17168 :   return OVL_EXPORT_P (ovl);
    4341              : }
    4342              : 
    4343              : /* Given a namespace NS, walk all of its bindings, calling CALLBACK
    4344              :    for all visible decls.  Any lazy module bindings will be loaded
    4345              :    at this point.  */
    4346              : 
    4347              : void
    4348          102 : walk_namespace_bindings (tree ns, void (*callback) (tree decl, void *data),
    4349              :                          void *data)
    4350              : {
    4351         1262 :   for (tree o : *DECL_NAMESPACE_BINDINGS (ns))
    4352          580 :     if (TREE_CODE (o) == BINDING_VECTOR)
    4353              :       {
    4354              :         /* Modules may have duplicate entities on the binding slot.
    4355              :            Keep track of this as needed, and ensure we only call
    4356              :            the callback once per entity.  */
    4357          154 :         hash_set<tree> seen;
    4358          383 :         const auto callback_maybe_dup = [&](tree decl)
    4359              :           {
    4360          229 :             if (!seen.add (decl))
    4361          172 :               callback (decl, data);
    4362          383 :           };
    4363              : 
    4364              :         /* First the current module.  There should be no dups yet,
    4365              :            but track anything that might be dup'd later.  */
    4366          154 :         binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (o);
    4367          154 :         if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    4368              :           {
    4369           57 :             tree value = bind;
    4370           57 :             if (STAT_HACK_P (bind))
    4371              :               {
    4372           57 :                 if (STAT_TYPE (bind) && !STAT_TYPE_HIDDEN_P (bind))
    4373            0 :                   callback_maybe_dup (STAT_TYPE (bind));
    4374           57 :                 if (STAT_DECL_HIDDEN_P (bind))
    4375              :                   value = NULL_TREE;
    4376              :                 else
    4377           57 :                   value = STAT_DECL (bind);
    4378              :               }
    4379           57 :             value = ovl_skip_hidden (value);
    4380          114 :             for (tree decl : ovl_range (value))
    4381           57 :               callback_maybe_dup (decl);
    4382              :           }
    4383              : 
    4384              :         /* Now the imported bindings.  */
    4385              :         /* FIXME: We probably want names visible from the outermost point of
    4386              :            constant evaluation, regardless of instantiations.  */
    4387          154 :         bitmap imports = get_import_bitmap ();
    4388          154 :         tree name = BINDING_VECTOR_NAME (o);
    4389              : 
    4390          154 :         unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (o);
    4391          154 :         if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    4392              :           {
    4393          154 :             ix--;
    4394          154 :             cluster++;
    4395              :           }
    4396              : 
    4397              :         /* Do this in forward order, so we load modules in an order
    4398              :            the user expects.  */
    4399          327 :         for (; ix--; cluster++)
    4400          519 :           for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    4401              :             {
    4402              :               /* Are we importing this module?  */
    4403          346 :               if (unsigned base = cluster->indices[jx].base)
    4404          173 :                 if (unsigned span = cluster->indices[jx].span)
    4405          173 :                   do
    4406          173 :                     if (bitmap_bit_p (imports, base))
    4407          173 :                       goto found;
    4408            0 :                   while (++base, --span);
    4409          173 :               continue;
    4410              : 
    4411          173 :             found:;
    4412              :               /* Is it loaded?  */
    4413          173 :               unsigned mod = cluster->indices[jx].base;
    4414          173 :               if (cluster->slots[jx].is_lazy ())
    4415              :                 {
    4416           11 :                   gcc_assert (cluster->indices[jx].span == 1);
    4417           11 :                   lazy_load_binding (mod, ns, name, &cluster->slots[jx]);
    4418              :                 }
    4419              : 
    4420          173 :               tree bind = cluster->slots[jx];
    4421          173 :               if (!bind)
    4422              :                 /* Load errors could mean there's nothing here.  */
    4423            0 :                 continue;
    4424              : 
    4425              :               /* Extract what we can see from here.  If there's no
    4426              :                  stat_hack, then everything was exported.  */
    4427          173 :               tree value = bind;
    4428          173 :               if (STAT_HACK_P (bind))
    4429              :                 {
    4430          153 :                   if (STAT_TYPE_VISIBLE_P (bind))
    4431           38 :                     callback_maybe_dup (STAT_TYPE (bind));
    4432          153 :                   value = STAT_VISIBLE (bind);
    4433              :                 }
    4434          173 :               value = ovl_skip_hidden (value);
    4435          326 :               for (tree decl : ovl_range (value))
    4436          134 :                 callback_maybe_dup (decl);
    4437          173 :             }
    4438          154 :       }
    4439              :     else
    4440              :       {
    4441          426 :         tree bind = o;
    4442          426 :         if (STAT_HACK_P (bind))
    4443              :           {
    4444           29 :             if (STAT_TYPE (bind) && !STAT_TYPE_HIDDEN_P (bind))
    4445           28 :               callback (STAT_TYPE (bind), data);
    4446           29 :             if (STAT_DECL_HIDDEN_P (bind))
    4447              :               bind = NULL_TREE;
    4448              :             else
    4449           29 :               bind = STAT_DECL (bind);
    4450              :           }
    4451          426 :         bind = ovl_skip_hidden (bind);
    4452          916 :         for (tree decl : ovl_range (bind))
    4453          457 :           callback (decl, data);
    4454              :       }
    4455          102 : }
    4456              : 
    4457              : /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
    4458              :    for all decls of the current module.  When partitions are involved,
    4459              :    decls might be mentioned more than once.   Return the accumulation of
    4460              :    CALLBACK results.  */
    4461              : 
    4462              : unsigned
    4463      7335231 : walk_module_binding (tree binding, bitmap partitions,
    4464              :                      bool (*callback) (tree decl, WMB_Flags, void *data),
    4465              :                      void *data)
    4466              : {
    4467      7335231 :   tree current = binding;
    4468      7335231 :   unsigned count = 0;
    4469              : 
    4470      7335231 :   if (TREE_CODE (binding) == BINDING_VECTOR)
    4471        34971 :     current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
    4472              : 
    4473      7341435 :   bool decl_hidden = false;
    4474      7335231 :   if (tree type = MAYBE_STAT_TYPE (current))
    4475              :     {
    4476           84 :       WMB_Flags flags = WMB_None;
    4477           84 :       if (STAT_TYPE_HIDDEN_P (current))
    4478            0 :         flags = WMB_Flags (flags | WMB_Hidden);
    4479           84 :       if (TREE_CODE (type) == USING_DECL)
    4480              :         {
    4481            3 :           flags = WMB_Flags (flags | WMB_Using);
    4482            3 :           if (DECL_MODULE_PURVIEW_P (type))
    4483            3 :             flags = WMB_Flags (flags | WMB_Purview);
    4484            3 :           if (DECL_MODULE_EXPORT_P (type))
    4485            3 :             flags = WMB_Flags (flags | WMB_Export);
    4486              :         }
    4487           84 :       count += callback (type, flags, data);
    4488           84 :       decl_hidden = STAT_DECL_HIDDEN_P (current);
    4489              :     }
    4490              : 
    4491     14714014 :   for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
    4492              :     {
    4493      7378783 :       if (iter.hidden_p ())
    4494              :         decl_hidden = true;
    4495      7378783 :       if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
    4496              :         {
    4497      5598851 :           WMB_Flags flags = WMB_None;
    4498      5598851 :           if (decl_hidden)
    4499         8407 :             flags = WMB_Flags (flags | WMB_Hidden);
    4500      5598851 :           if (iter.using_p ())
    4501              :             {
    4502        21560 :               flags = WMB_Flags (flags | WMB_Using);
    4503        21560 :               if (iter.purview_p ())
    4504        17389 :                 flags = WMB_Flags (flags | WMB_Purview);
    4505        21560 :               if (iter.exporting_p ())
    4506        16751 :                 flags = WMB_Flags (flags | WMB_Export);
    4507              :             }
    4508      5598851 :           count += callback (*iter, flags, data);
    4509              :         }
    4510      7378783 :       decl_hidden = false;
    4511              :     }
    4512              : 
    4513      7335231 :   if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
    4514              :     {
    4515              :       /* Process partition slots.  */
    4516          308 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    4517          308 :       unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    4518          308 :       if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    4519              :         {
    4520          308 :           ix--;
    4521          308 :           cluster++;
    4522              :         }
    4523              : 
    4524              :       /* There could be duplicate module or GMF entries.  */
    4525          308 :       bool maybe_dups = (BINDING_VECTOR_PARTITION_DUPS_P (binding)
    4526          308 :                          || BINDING_VECTOR_GLOBAL_DUPS_P (binding));
    4527              : 
    4528          655 :       for (; ix--; cluster++)
    4529         1041 :         for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    4530          694 :           if (!cluster->slots[jx].is_lazy ())
    4531          688 :             if (tree bind = cluster->slots[jx])
    4532              :               {
    4533          402 :                 if (TREE_CODE (bind) == NAMESPACE_DECL
    4534          402 :                     && !DECL_NAMESPACE_ALIAS (bind))
    4535              :                   {
    4536           33 :                     if (unsigned base = cluster->indices[jx].base)
    4537           33 :                       if (unsigned span = cluster->indices[jx].span)
    4538           33 :                         do
    4539           33 :                           if (bitmap_bit_p (partitions, base))
    4540           30 :                             goto found;
    4541            3 :                         while (++base, --span);
    4542              :                     /* Not a partition's namespace.  */
    4543            3 :                     continue;
    4544           30 :                   found:
    4545              : 
    4546           30 :                     WMB_Flags flags = WMB_None;
    4547           30 :                     if (maybe_dups)
    4548            0 :                       flags = WMB_Flags (flags | WMB_Dups);
    4549           30 :                     count += callback (bind, flags, data);
    4550            3 :                   }
    4551          369 :                 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
    4552              :                   {
    4553          213 :                     if (tree btype = STAT_TYPE (bind))
    4554              :                       {
    4555            0 :                         WMB_Flags flags = WMB_None;
    4556            0 :                         if (maybe_dups)
    4557            0 :                           flags = WMB_Flags (flags | WMB_Dups);
    4558            0 :                         if (STAT_TYPE_HIDDEN_P (bind))
    4559            0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    4560            0 :                         if (TREE_CODE (btype) == USING_DECL)
    4561              :                           {
    4562            0 :                             flags = WMB_Flags (flags | WMB_Using);
    4563            0 :                             if (DECL_MODULE_PURVIEW_P (btype))
    4564            0 :                               flags = WMB_Flags (flags | WMB_Purview);
    4565            0 :                             if (DECL_MODULE_EXPORT_P (btype))
    4566            0 :                               flags = WMB_Flags (flags | WMB_Export);
    4567              :                           }
    4568            0 :                         count += callback (btype, flags, data);
    4569              :                       }
    4570          213 :                     bool part_hidden = STAT_DECL_HIDDEN_P (bind);
    4571          213 :                     for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
    4572          435 :                          iter; ++iter)
    4573              :                       {
    4574          222 :                         if (iter.hidden_p ())
    4575              :                           part_hidden = true;
    4576          222 :                         gcc_checking_assert
    4577              :                           (!(part_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
    4578              : 
    4579          222 :                         WMB_Flags flags = WMB_None;
    4580          222 :                         if (maybe_dups)
    4581           90 :                           flags = WMB_Flags (flags | WMB_Dups);
    4582          222 :                         if (part_hidden)
    4583            0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    4584          222 :                         if (iter.using_p ())
    4585              :                           {
    4586           12 :                             flags = WMB_Flags (flags | WMB_Using);
    4587           12 :                             if (iter.purview_p ())
    4588           12 :                               flags = WMB_Flags (flags | WMB_Purview);
    4589           12 :                             if (iter.exporting_p ())
    4590           12 :                               flags = WMB_Flags (flags | WMB_Export);
    4591              :                           }
    4592          222 :                         count += callback (*iter, flags, data);
    4593          222 :                         part_hidden = false;
    4594              :                       }
    4595              :                   }
    4596              :               }
    4597              :     }
    4598              : 
    4599      7335231 :   return count;
    4600              : }
    4601              : 
    4602              : /* Imported module MOD has a binding to NS::NAME, stored in section
    4603              :    SNUM.  */
    4604              : 
    4605              : bool
    4606       182284 : import_module_binding  (tree ns, tree name, unsigned mod, unsigned snum)
    4607              : {
    4608       182284 :   tree *slot = find_namespace_slot (ns, name, true);
    4609       182284 :   binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
    4610              : 
    4611       182284 :   if (mslot->is_lazy () || *mslot)
    4612              :     /* Oops, something was already there.  */
    4613              :     return false;
    4614              : 
    4615       182284 :   mslot->set_lazy (snum);
    4616       182284 :   return true;
    4617              : }
    4618              : 
    4619              : /* An import of MODULE is binding NS::NAME.  There should be no
    4620              :    existing binding for >= MODULE.  GLOBAL_P indicates whether the
    4621              :    bindings include global module entities.  PARTITION_P is true if
    4622              :    it is part of the current module.  VALUE and TYPE are the value
    4623              :    and type bindings.  VISIBLE are the value bindings being exported.
    4624              :    INTERNAL is a TREE_LIST of any TU-local names visible for ADL.  */
    4625              : 
    4626              : bool
    4627       104380 : set_module_binding (tree ns, tree name, unsigned mod, bool global_p,
    4628              :                     bool partition_p, tree value, tree type, tree visible,
    4629              :                     tree internal)
    4630              : {
    4631       104380 :   if (!value && !internal)
    4632              :     /* Bogus BMIs could give rise to nothing to bind.  */
    4633              :     return false;
    4634              : 
    4635       104365 :   gcc_assert (!value
    4636              :               || TREE_CODE (value) != NAMESPACE_DECL
    4637              :               || DECL_NAMESPACE_ALIAS (value));
    4638       104380 :   gcc_checking_assert (mod);
    4639              : 
    4640       104380 :   tree *slot = find_namespace_slot (ns, name, true);
    4641       104380 :   binding_slot *mslot = search_imported_binding_slot (slot, mod);
    4642              : 
    4643       104380 :   if (!mslot || !mslot->is_lazy ())
    4644              :     /* Again, bogus BMI could give find to missing or already loaded slot.  */
    4645              :     return false;
    4646              : 
    4647       104380 :   tree bind = value;
    4648       104380 :   if (type || visible != bind || internal || partition_p || global_p)
    4649              :     {
    4650       102654 :       bind = stat_hack (bind, type);
    4651       102654 :       STAT_VISIBLE (bind) = visible;
    4652          816 :       if ((partition_p && TREE_PUBLIC (ns))
    4653       102660 :           || (type && DECL_MODULE_EXPORT_P (type)))
    4654          836 :         STAT_TYPE_VISIBLE_P (bind) = true;
    4655              :     }
    4656              : 
    4657              :   /* If this has internal declarations, track them for diagnostics.  */
    4658       104380 :   if (internal)
    4659              :     {
    4660           15 :       if (!BINDING_VECTOR_INTERNAL_DECLS (*slot))
    4661           15 :         BINDING_VECTOR_INTERNAL_DECLS (*slot)
    4662           30 :           = module_tree_map_t::create_ggc ();
    4663           15 :       bool existed = BINDING_VECTOR_INTERNAL_DECLS (*slot)->put (mod, internal);
    4664           15 :       gcc_checking_assert (!existed);
    4665           15 :       MODULE_BINDING_INTERNAL_DECLS_P (bind) = true;
    4666              :     }
    4667              : 
    4668              :   /* Note if this is this-module and/or global binding.  */
    4669       104380 :   if (partition_p)
    4670          816 :     MODULE_BINDING_PARTITION_P (bind) = true;
    4671       104380 :   if (global_p)
    4672       101636 :     MODULE_BINDING_GLOBAL_P (bind) = true;
    4673              : 
    4674       104380 :   *mslot = bind;
    4675              : 
    4676       104380 :   return true;
    4677              : }
    4678              : 
    4679              : void
    4680        64099 : add_module_namespace_decl (tree ns, tree decl)
    4681              : {
    4682        64099 :   gcc_assert (!DECL_CHAIN (decl));
    4683        64099 :   gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
    4684              :                          && DECL_LOCAL_DECL_P (decl)));
    4685        64099 :   if (CHECKING_P)
    4686              :     /* Expensive already-there? check.  */
    4687    102243053 :     for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
    4688    102178954 :          probe = DECL_CHAIN (probe))
    4689    102178954 :       gcc_assert (decl != probe);
    4690              : 
    4691        64099 :   add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
    4692              : 
    4693        64099 :   if (VAR_P (decl))
    4694         2100 :     maybe_register_incomplete_var (decl);
    4695              : 
    4696        61999 :   if (VAR_OR_FUNCTION_DECL_P (decl)
    4697        84265 :       && DECL_EXTERN_C_P (decl))
    4698         8764 :     check_extern_c_conflict (decl);
    4699        64099 : }
    4700              : 
    4701              : /* Enter DECL into the symbol table, if that's appropriate.  Returns
    4702              :    DECL, or a modified version thereof.  */
    4703              : 
    4704              : tree
    4705    151009258 : maybe_push_decl (tree decl)
    4706              : {
    4707    151009258 :   tree type = TREE_TYPE (decl);
    4708              : 
    4709              :   /* Add this decl to the current binding level, but not if it comes
    4710              :      from another scope, e.g. a static member variable.  TEM may equal
    4711              :      DECL or it may be a previous decl of the same name.  */
    4712    151009258 :   if (decl == error_mark_node
    4713    151009253 :       || (TREE_CODE (decl) != PARM_DECL
    4714    151009253 :           && DECL_CONTEXT (decl) != NULL_TREE
    4715              :           /* Definitions of namespace members outside their namespace are
    4716              :              possible.  */
    4717     50743608 :           && !DECL_NAMESPACE_SCOPE_P (decl))
    4718    149514020 :       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
    4719    149514020 :       || type == unknown_type_node
    4720              :       /* The declaration of a template specialization does not affect
    4721              :          the functions available for overload resolution, so we do not
    4722              :          call pushdecl.  */
    4723    300523278 :       || (TREE_CODE (decl) == FUNCTION_DECL
    4724     36473391 :           && DECL_TEMPLATE_SPECIALIZATION (decl)))
    4725      1584331 :     return decl;
    4726              :   else
    4727    149424927 :     return pushdecl (decl);
    4728              : }
    4729              : 
    4730              : /* Bind DECL to ID in the current_binding_level, assumed to be a local
    4731              :    binding level.  If IS_USING is true, DECL got here through a
    4732              :    using-declaration.  */
    4733              : 
    4734              : static void
    4735     13132949 : push_local_binding (tree id, tree decl, bool is_using)
    4736              : {
    4737              :   /* Skip over any local classes.  This makes sense if we call
    4738              :      push_local_binding with a friend decl of a local class.  */
    4739     13132949 :   cp_binding_level *b = innermost_nonclass_level ();
    4740              : 
    4741     13132949 :   gcc_assert (b->kind != sk_namespace);
    4742     13132949 :   if (find_local_binding (b, id))
    4743              :     {
    4744              :       /* Supplement the existing binding.  */
    4745           27 :       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
    4746              :         /* It didn't work.  Something else must be bound at this
    4747              :            level.  Do not add DECL to the list of things to pop
    4748              :            later.  */
    4749              :         return;
    4750              :     }
    4751              :   else
    4752              :     /* Create a new binding.  */
    4753     13132922 :     push_binding (id, decl, b);
    4754              : 
    4755     13132940 :   if (TREE_CODE (decl) == OVERLOAD || is_using)
    4756              :     /* We must put the OVERLOAD or using into a TREE_LIST since we
    4757              :        cannot use the decl's chain itself.  */
    4758     13132940 :     decl = build_tree_list (id, decl);
    4759              : 
    4760              :   /* And put DECL on the list of things declared by the current
    4761              :      binding level.  */
    4762     13132940 :   add_decl_to_level (b, decl);
    4763              : }
    4764              : 
    4765              : /* Lookup the FRIEND_TMPL within all merged module imports.  Used to dedup
    4766              :    instantiations of temploid hidden friends from imported modules.  */
    4767              : 
    4768              : tree
    4769          262 : lookup_imported_hidden_friend (tree friend_tmpl)
    4770              : {
    4771              :   /* For a class-scope friend class it should have been found by regular
    4772              :      name lookup.  Otherwise we're looking in the current namespace.  */
    4773          262 :   gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
    4774              : 
    4775          262 :   tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
    4776          262 :   if (!DECL_LANG_SPECIFIC (inner)
    4777          316 :       || !DECL_MODULE_ENTITY_P (inner))
    4778              :     return NULL_TREE;
    4779              : 
    4780              :   /* Load any templates matching FRIEND_TMPL from importers.  */
    4781           36 :   lazy_load_pendings (friend_tmpl);
    4782              : 
    4783           36 :   tree name = DECL_NAME (inner);
    4784           36 :   tree *slot = find_namespace_slot (current_namespace, name, false);
    4785           36 :   if (!slot || !*slot || TREE_CODE (*slot) != BINDING_VECTOR)
    4786              :     return NULL_TREE;
    4787              : 
    4788              :   /* We're only interested in declarations attached to the same module
    4789              :      as the friend class we're attempting to instantiate.  */
    4790           21 :   int m = get_originating_module (friend_tmpl, /*global=-1*/true);
    4791           21 :   gcc_assert (m != 0);
    4792              : 
    4793              :   /* First check whether there's a reachable declaration attached to the module
    4794              :      we're looking for.  */
    4795           21 :   if (m > 0)
    4796            0 :     if (binding_slot *mslot = search_imported_binding_slot (slot, m))
    4797              :       {
    4798            0 :         if (mslot->is_lazy ())
    4799            0 :           lazy_load_binding (m, current_namespace, name, mslot);
    4800            0 :         for (ovl_iterator iter (*mslot); iter; ++iter)
    4801            0 :           if (DECL_CLASS_TEMPLATE_P (*iter))
    4802            0 :             return *iter;
    4803              :       }
    4804              : 
    4805              :   /* Otherwise, look in the mergeable slots for this name, in case an importer
    4806              :      has already instantiated this declaration.  */
    4807              :   tree *vslot = get_fixed_binding_slot
    4808           21 :     (slot, name, m > 0 ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL, false);
    4809           21 :   if (!vslot || !*vslot)
    4810              :     return NULL_TREE;
    4811              : 
    4812              :   /* There should be at most one class template from the module we're
    4813              :      looking for, return it.  */
    4814           21 :   for (ovl_iterator iter (*vslot); iter; ++iter)
    4815           42 :     if (DECL_CLASS_TEMPLATE_P (*iter)
    4816           42 :         && get_originating_module (*iter, true) == m)
    4817           21 :       return *iter;
    4818              : 
    4819            0 :   return NULL_TREE;
    4820              : }
    4821              : 
    4822              : 
    4823              : /* true means unconditionally make a BLOCK for the next level pushed.  */
    4824              : 
    4825              : static bool keep_next_level_flag;
    4826              : 
    4827              : static int binding_depth = 0;
    4828              : 
    4829              : static void
    4830            0 : indent (int depth)
    4831              : {
    4832            0 :   int i;
    4833              : 
    4834            0 :   for (i = 0; i < depth * 2; i++)
    4835            0 :     putc (' ', stderr);
    4836            0 : }
    4837              : 
    4838              : /* Return a string describing the kind of SCOPE we have.  */
    4839              : static const char *
    4840            0 : cp_binding_level_descriptor (cp_binding_level *scope)
    4841              : {
    4842              :   /* The order of this table must match the "scope_kind"
    4843              :      enumerators.  */
    4844            0 :   static const char* scope_kind_names[] = {
    4845              :     "block-scope",
    4846              :     "cleanup-scope",
    4847              :     "try-scope",
    4848              :     "catch-scope",
    4849              :     "for-scope",
    4850              :     "template-for-scope",
    4851              :     "cond-init-scope",
    4852              :     "stmt-expr-scope",
    4853              :     "function-parameter-scope",
    4854              :     "class-scope",
    4855              :     "enum-scope",
    4856              :     "namespace-scope",
    4857              :     "template-parameter-scope",
    4858              :     "template-explicit-spec-scope",
    4859              :     "transaction-scope",
    4860              :     "openmp-scope",
    4861              :     "lambda-scope",
    4862              :     "contract-check-scope"
    4863              :   };
    4864            0 :   static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
    4865              :                  "must keep names aligned with scope_kind enum");
    4866              : 
    4867            0 :   scope_kind kind = scope->kind;
    4868            0 :   if (kind == sk_template_parms && scope->explicit_spec_p)
    4869            0 :     kind = sk_template_spec;
    4870              : 
    4871            0 :   return scope_kind_names[kind];
    4872              : }
    4873              : 
    4874              : /* Output a debugging information about SCOPE when performing
    4875              :    ACTION at LINE.  */
    4876              : static void
    4877            0 : cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
    4878              : {
    4879            0 :   const char *desc = cp_binding_level_descriptor (scope);
    4880            0 :   if (scope->this_entity)
    4881            0 :     verbatim ("%s %<%s(%E)%> %p %d", action, desc,
    4882              :               scope->this_entity, (void *) scope, line);
    4883              :   else
    4884            0 :     verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
    4885            0 : }
    4886              : 
    4887              : /* A chain of binding_level structures awaiting reuse.  */
    4888              : 
    4889              : static GTY((deletable)) cp_binding_level *free_binding_level;
    4890              : 
    4891              : /* Insert SCOPE as the innermost binding level.  */
    4892              : 
    4893              : void
    4894   1401143777 : push_binding_level (cp_binding_level *scope)
    4895              : {
    4896              :   /* Add it to the front of currently active scopes stack.  */
    4897   1401143777 :   scope->level_chain = current_binding_level;
    4898   1401143777 :   current_binding_level = scope;
    4899   1401143777 :   keep_next_level_flag = false;
    4900              : 
    4901   1401143777 :   if (ENABLE_SCOPE_CHECKING)
    4902              :     {
    4903              :       scope->binding_depth = binding_depth;
    4904              :       indent (binding_depth);
    4905              :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    4906              :                               "push");
    4907              :       binding_depth++;
    4908              :     }
    4909   1401143777 : }
    4910              : 
    4911              : /* Create a new KIND scope and make it the top of the active scopes stack.
    4912              :    ENTITY is the scope of the associated C++ entity (namespace, class,
    4913              :    function, C++0x enumeration); it is NULL otherwise.  */
    4914              : 
    4915              : cp_binding_level *
    4916   1197255843 : begin_scope (scope_kind kind, tree entity)
    4917              : {
    4918   1197255843 :   cp_binding_level *scope;
    4919              : 
    4920              :   /* Reuse or create a struct for this binding level.  */
    4921   1197255843 :   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
    4922              :     {
    4923   1147893646 :       scope = free_binding_level;
    4924   1147893646 :       free_binding_level = scope->level_chain;
    4925   1147893646 :       memset (scope, 0, sizeof (cp_binding_level));
    4926              :     }
    4927              :   else
    4928     49362197 :     scope = ggc_cleared_alloc<cp_binding_level> ();
    4929              : 
    4930   1197255843 :   scope->this_entity = entity;
    4931   1197255843 :   scope->more_cleanups_ok = true;
    4932   1197255843 :   switch (kind)
    4933              :     {
    4934          312 :     case sk_cleanup:
    4935          312 :       scope->keep = true;
    4936          312 :       break;
    4937              : 
    4938      5786691 :     case sk_template_spec:
    4939      5786691 :       scope->explicit_spec_p = true;
    4940      5786691 :       kind = sk_template_parms;
    4941              :       /* Fall through.  */
    4942    826498655 :     case sk_template_parms:
    4943    826498655 :     case sk_block:
    4944    826498655 :     case sk_try:
    4945    826498655 :     case sk_catch:
    4946    826498655 :     case sk_for:
    4947    826498655 :     case sk_template_for:
    4948    826498655 :     case sk_cond:
    4949    826498655 :     case sk_class:
    4950    826498655 :     case sk_scoped_enum:
    4951    826498655 :     case sk_transaction:
    4952    826498655 :     case sk_omp:
    4953    826498655 :     case sk_contract:
    4954    826498655 :     case sk_stmt_expr:
    4955    826498655 :     case sk_lambda:
    4956    826498655 :       scope->keep = keep_next_level_flag;
    4957    826498655 :       break;
    4958              : 
    4959    370658843 :     case sk_function_parms:
    4960    370658843 :       scope->keep = keep_next_level_flag;
    4961    370658843 :       break;
    4962              : 
    4963        98033 :     case sk_namespace:
    4964        98033 :       NAMESPACE_LEVEL (entity) = scope;
    4965        98033 :       break;
    4966              : 
    4967            0 :     default:
    4968              :       /* Should not happen.  */
    4969            0 :       gcc_unreachable ();
    4970   1197255843 :       break;
    4971              :     }
    4972   1197255843 :   scope->kind = kind;
    4973              : 
    4974   1197255843 :   push_binding_level (scope);
    4975              : 
    4976   1197255843 :   return scope;
    4977              : }
    4978              : 
    4979              : /* We're about to leave current scope.  Pop the top of the stack of
    4980              :    currently active scopes.  Return the enclosing scope, now active.  */
    4981              : 
    4982              : cp_binding_level *
    4983   1454954846 : leave_scope (void)
    4984              : {
    4985   1454954846 :   cp_binding_level *scope = current_binding_level;
    4986              : 
    4987   1454954846 :   if (scope->kind == sk_namespace && class_binding_level)
    4988            0 :     current_binding_level = class_binding_level;
    4989              : 
    4990              :   /* We cannot leave a scope, if there are none left.  */
    4991   1454954846 :   if (NAMESPACE_LEVEL (global_namespace))
    4992   1454954846 :     gcc_assert (!global_scope_p (scope));
    4993              : 
    4994   1454954846 :   if (ENABLE_SCOPE_CHECKING)
    4995              :     {
    4996              :       indent (--binding_depth);
    4997              :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    4998              :                               "leave");
    4999              :     }
    5000              : 
    5001              :   /* Move one nesting level up.  */
    5002   1454954846 :   current_binding_level = scope->level_chain;
    5003              : 
    5004              :   /* Namespace-scopes are left most probably temporarily, not
    5005              :      completely; they can be reopened later, e.g. in namespace-extension
    5006              :      or any name binding activity that requires us to resume a
    5007              :      namespace.  For classes, we cache some binding levels.  For other
    5008              :      scopes, we just make the structure available for reuse.  */
    5009   1454954846 :   if (scope->kind != sk_namespace
    5010   1401029397 :       && scope != previous_class_level)
    5011              :     {
    5012   1119643222 :       scope->level_chain = free_binding_level;
    5013   1119643222 :       gcc_assert (!ENABLE_SCOPE_CHECKING
    5014              :                   || scope->binding_depth == binding_depth);
    5015   1119643222 :       free_binding_level = scope;
    5016              :     }
    5017              : 
    5018   1454954846 :   if (scope->kind == sk_class)
    5019              :     {
    5020              :       /* Reset DEFINING_CLASS_P to allow for reuse of a
    5021              :          class-defining scope in a non-defining context.  */
    5022    495289398 :       scope->defining_class_p = 0;
    5023              : 
    5024              :       /* Find the innermost enclosing class scope, and reset
    5025              :          CLASS_BINDING_LEVEL appropriately.  */
    5026    495289398 :       class_binding_level = NULL;
    5027   1977496212 :       for (scope = current_binding_level; scope; scope = scope->level_chain)
    5028   1052477266 :         if (scope->kind == sk_class)
    5029              :           {
    5030     65559850 :             class_binding_level = scope;
    5031     65559850 :             break;
    5032              :           }
    5033              :     }
    5034              : 
    5035   1454954846 :   return current_binding_level;
    5036              : }
    5037              : 
    5038              : /* When we exit a toplevel class scope, we save its binding level so
    5039              :    that we can restore it quickly.  Here, we've entered some other
    5040              :    class, so we must invalidate our cache.  */
    5041              : 
    5042              : void
    5043     29335994 : invalidate_class_lookup_cache (void)
    5044              : {
    5045     29335994 :   previous_class_level->level_chain = free_binding_level;
    5046     29335994 :   free_binding_level = previous_class_level;
    5047     29335994 :   previous_class_level = NULL;
    5048     29335994 : }
    5049              : 
    5050              : static void
    5051     53925458 : resume_scope (cp_binding_level* b)
    5052              : {
    5053              :   /* Resuming binding levels is meant only for namespaces,
    5054              :      and those cannot nest into classes.  */
    5055     53925458 :   gcc_assert (!class_binding_level);
    5056              :   /* Also, resuming a non-directly nested namespace is a no-no.  */
    5057     53925458 :   gcc_assert (b->level_chain == current_binding_level);
    5058     53925458 :   current_binding_level = b;
    5059     53925458 :   if (ENABLE_SCOPE_CHECKING)
    5060              :     {
    5061              :       b->binding_depth = binding_depth;
    5062              :       indent (binding_depth);
    5063              :       cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
    5064              :       binding_depth++;
    5065              :     }
    5066     53925458 : }
    5067              : 
    5068              : /* Return the innermost binding level that is not for a class scope.  */
    5069              : 
    5070              : static cp_binding_level *
    5071   1670931884 : innermost_nonclass_level (void)
    5072              : {
    5073   1670931884 :   cp_binding_level *b;
    5074              : 
    5075   1670931884 :   b = current_binding_level;
    5076   1924907417 :   while (b->kind == sk_class)
    5077    253975533 :     b = b->level_chain;
    5078              : 
    5079   1670931884 :   return b;
    5080              : }
    5081              : 
    5082              : /* We're defining an object of type TYPE.  If it needs a cleanup, but
    5083              :    we're not allowed to add any more objects with cleanups to the current
    5084              :    scope, create a new binding level.  */
    5085              : 
    5086              : void
    5087      8334968 : maybe_push_cleanup_level (tree type)
    5088              : {
    5089      8334968 :   if (type != error_mark_node
    5090      8334776 :       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
    5091      8693931 :       && current_binding_level->more_cleanups_ok == 0)
    5092              :     {
    5093          312 :       begin_scope (sk_cleanup, NULL);
    5094          312 :       current_binding_level->statement_list = push_stmt_list ();
    5095              :     }
    5096      8334968 : }
    5097              : 
    5098              : /* Return true if we are in the global binding level.  */
    5099              : 
    5100              : bool
    5101       449858 : global_bindings_p (void)
    5102              : {
    5103       449858 :   return global_scope_p (current_binding_level);
    5104              : }
    5105              : 
    5106              : /* True if we are currently in a toplevel binding level.  This
    5107              :    means either the global binding level or a namespace in a toplevel
    5108              :    binding level.  Since there are no non-toplevel namespace levels,
    5109              :    this really means any namespace or template parameter level.  We
    5110              :    also include a class whose context is toplevel.  */
    5111              : 
    5112              : bool
    5113   1620188757 : toplevel_bindings_p (void)
    5114              : {
    5115   1620188757 :   cp_binding_level *b = innermost_nonclass_level ();
    5116              : 
    5117   1620188757 :   return b->kind == sk_namespace || b->kind == sk_template_parms;
    5118              : }
    5119              : 
    5120              : /* True if this is a namespace scope, or if we are defining a class
    5121              :    which is itself at namespace scope, or whose enclosing class is
    5122              :    such a class, etc.  */
    5123              : 
    5124              : bool
    5125     37608376 : namespace_bindings_p (void)
    5126              : {
    5127     37608376 :   cp_binding_level *b = innermost_nonclass_level ();
    5128              : 
    5129     37608376 :   return b->kind == sk_namespace;
    5130              : }
    5131              : 
    5132              : /* True if the innermost non-class scope is a block scope.  */
    5133              : 
    5134              : bool
    5135         1802 : local_bindings_p (void)
    5136              : {
    5137         1802 :   cp_binding_level *b = innermost_nonclass_level ();
    5138         1802 :   return b->kind < sk_function_parms || b->kind == sk_omp;
    5139              : }
    5140              : 
    5141              : /* True if the current level needs to have a BLOCK made.  */
    5142              : 
    5143              : bool
    5144    394010152 : kept_level_p (void)
    5145              : {
    5146    394010152 :   return (current_binding_level->blocks != NULL_TREE
    5147    351348035 :           || current_binding_level->keep
    5148    342331164 :           || current_binding_level->kind == sk_cleanup
    5149    342331164 :           || current_binding_level->names != NULL_TREE
    5150    698231620 :           || current_binding_level->using_directives);
    5151              : }
    5152              : 
    5153              : /* Returns the kind of the innermost scope.  */
    5154              : 
    5155              : scope_kind
    5156   2820880148 : innermost_scope_kind (void)
    5157              : {
    5158   2820880148 :   return current_binding_level->kind;
    5159              : }
    5160              : 
    5161              : /* Returns true if this scope was created to store template parameters.  */
    5162              : 
    5163              : bool
    5164    638634166 : template_parm_scope_p (void)
    5165              : {
    5166    638634166 :   return innermost_scope_kind () == sk_template_parms;
    5167              : }
    5168              : 
    5169              : /* If KEEP is true, make a BLOCK node for the next binding level,
    5170              :    unconditionally.  Otherwise, use the normal logic to decide whether
    5171              :    or not to create a BLOCK.  */
    5172              : 
    5173              : void
    5174     17596899 : keep_next_level (bool keep)
    5175              : {
    5176     17596899 :   keep_next_level_flag = keep;
    5177     17596899 : }
    5178              : 
    5179              : /* Return the list of declarations of the current local scope.  */
    5180              : 
    5181              : tree
    5182    370293787 : get_local_decls (void)
    5183              : {
    5184    370293787 :   gcc_assert (current_binding_level->kind != sk_namespace
    5185              :               && current_binding_level->kind != sk_class);
    5186    370293787 :   return current_binding_level->names;
    5187              : }
    5188              : 
    5189              : /* Return how many function prototypes we are currently nested inside.  */
    5190              : 
    5191              : int
    5192    275225056 : function_parm_depth (void)
    5193              : {
    5194    275225056 :   int level = 0;
    5195    275225056 :   cp_binding_level *b;
    5196              : 
    5197    275225056 :   for (b = current_binding_level;
    5198    551169177 :        b->kind == sk_function_parms;
    5199    275944121 :        b = b->level_chain)
    5200    275944121 :     ++level;
    5201              : 
    5202    275225056 :   return level;
    5203              : }
    5204              : 
    5205              : /* For debugging.  */
    5206              : static int no_print_functions = 0;
    5207              : static int no_print_builtins = 0;
    5208              : 
    5209              : static void
    5210            0 : print_binding_level (cp_binding_level* lvl)
    5211              : {
    5212            0 :   tree t;
    5213            0 :   int i = 0, len;
    5214            0 :   if (lvl->this_entity)
    5215            0 :     print_node_brief (stderr, "entity=", lvl->this_entity, 1);
    5216            0 :   fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
    5217            0 :   if (lvl->more_cleanups_ok)
    5218            0 :     fprintf (stderr, " more-cleanups-ok");
    5219            0 :   if (lvl->have_cleanups)
    5220            0 :     fprintf (stderr, " have-cleanups");
    5221            0 :   fprintf (stderr, "\n");
    5222            0 :   if (lvl->names)
    5223              :     {
    5224            0 :       fprintf (stderr, " names:\t");
    5225              :       /* We can probably fit 3 names to a line?  */
    5226            0 :       for (t = lvl->names; t; t = TREE_CHAIN (t))
    5227              :         {
    5228            0 :           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
    5229            0 :             continue;
    5230            0 :           if (no_print_builtins
    5231            0 :               && (TREE_CODE (t) == TYPE_DECL)
    5232            0 :               && DECL_IS_UNDECLARED_BUILTIN (t))
    5233            0 :             continue;
    5234              : 
    5235              :           /* Function decls tend to have longer names.  */
    5236            0 :           if (TREE_CODE (t) == FUNCTION_DECL)
    5237              :             len = 3;
    5238              :           else
    5239            0 :             len = 2;
    5240            0 :           i += len;
    5241            0 :           if (i > 6)
    5242              :             {
    5243            0 :               fprintf (stderr, "\n\t");
    5244            0 :               i = len;
    5245              :             }
    5246            0 :           print_node_brief (stderr, "", t, 0);
    5247            0 :           if (t == error_mark_node)
    5248              :             break;
    5249              :         }
    5250            0 :       if (i)
    5251            0 :         fprintf (stderr, "\n");
    5252              :     }
    5253            0 :   if (vec_safe_length (lvl->class_shadowed))
    5254              :     {
    5255            0 :       size_t i;
    5256            0 :       cp_class_binding *b;
    5257            0 :       fprintf (stderr, " class-shadowed:");
    5258            0 :       FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
    5259            0 :         fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
    5260            0 :       fprintf (stderr, "\n");
    5261              :     }
    5262            0 :   if (lvl->type_shadowed)
    5263              :     {
    5264            0 :       fprintf (stderr, " type-shadowed:");
    5265            0 :       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
    5266              :         {
    5267            0 :           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
    5268              :         }
    5269            0 :       fprintf (stderr, "\n");
    5270              :     }
    5271            0 : }
    5272              : 
    5273              : DEBUG_FUNCTION void
    5274            0 : debug (cp_binding_level &ref)
    5275              : {
    5276            0 :   print_binding_level (&ref);
    5277            0 : }
    5278              : 
    5279              : DEBUG_FUNCTION void
    5280            0 : debug (cp_binding_level *ptr)
    5281              : {
    5282            0 :   if (ptr)
    5283            0 :     debug (*ptr);
    5284              :   else
    5285            0 :     fprintf (stderr, "<nil>\n");
    5286            0 : }
    5287              : 
    5288              : static void
    5289            0 : print_other_binding_stack (cp_binding_level *stack)
    5290              : {
    5291            0 :   cp_binding_level *level;
    5292            0 :   for (level = stack; !global_scope_p (level); level = level->level_chain)
    5293              :     {
    5294            0 :       fprintf (stderr, "binding level %p\n", (void *) level);
    5295            0 :       print_binding_level (level);
    5296              :     }
    5297            0 : }
    5298              : 
    5299              : DEBUG_FUNCTION void
    5300            0 : print_binding_stack (void)
    5301              : {
    5302            0 :   cp_binding_level *b;
    5303            0 :   fprintf (stderr, "current_binding_level=%p\n"
    5304              :            "class_binding_level=%p\n"
    5305              :            "NAMESPACE_LEVEL (global_namespace)=%p\n",
    5306            0 :            (void *) current_binding_level, (void *) class_binding_level,
    5307            0 :            (void *) NAMESPACE_LEVEL (global_namespace));
    5308            0 :   if (class_binding_level)
    5309              :     {
    5310            0 :       for (b = class_binding_level; b; b = b->level_chain)
    5311            0 :         if (b == current_binding_level)
    5312              :           break;
    5313            0 :       if (b)
    5314              :         b = class_binding_level;
    5315              :       else
    5316            0 :         b = current_binding_level;
    5317              :     }
    5318              :   else
    5319            0 :     b = current_binding_level;
    5320            0 :   print_other_binding_stack (b);
    5321            0 :   fprintf (stderr, "global:\n");
    5322            0 :   print_binding_level (NAMESPACE_LEVEL (global_namespace));
    5323            0 : }
    5324              : 
    5325              : /* Push a definition of struct, union or enum tag named ID.  into
    5326              :    binding_level B.  DECL is a TYPE_DECL for the type.  DECL has
    5327              :    already been pushed into its binding level.  This is bookkeeping to
    5328              :    find it easily.  */
    5329              : 
    5330              : static void
    5331    412500940 : set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
    5332              : {
    5333    412500940 :   if (b->kind == sk_namespace)
    5334              :     /* At namespace scope we should not see an identifier type value.  */
    5335     25681215 :     gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
    5336              :                          /* But we might end up here with ill-formed input.  */
    5337              :                          || seen_error ());
    5338              :   else
    5339              :     {
    5340              :       /* Push the current type value, so we can restore it later  */
    5341    386819725 :       tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
    5342    386819725 :       b->type_shadowed = tree_cons (id, old, b->type_shadowed);
    5343    386819725 :       tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
    5344    386819725 :       TREE_TYPE (b->type_shadowed) = type;
    5345    386819725 :       SET_IDENTIFIER_TYPE_VALUE (id, type);
    5346              :     }
    5347    412500940 : }
    5348              : 
    5349              : /* As set_identifier_type_value_with_scope, but using
    5350              :    current_binding_level.  */
    5351              : 
    5352              : void
    5353    151674249 : set_identifier_type_value (tree id, tree decl)
    5354              : {
    5355    151674249 :   set_identifier_type_value_with_scope (id, decl, current_binding_level);
    5356    151674249 : }
    5357              : 
    5358              : /* Return the name for the constructor (or destructor) for the
    5359              :    specified class.  */
    5360              : 
    5361              : tree
    5362    382006827 : constructor_name (tree type)
    5363              : {
    5364    382006827 :   tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
    5365              : 
    5366    382006827 :   return decl ? DECL_NAME (decl) : NULL_TREE;
    5367              : }
    5368              : 
    5369              : /* Returns TRUE if NAME is the name for the constructor for TYPE,
    5370              :    which must be a class type.  */
    5371              : 
    5372              : bool
    5373    357242221 : constructor_name_p (tree name, tree type)
    5374              : {
    5375    357242221 :   gcc_assert (MAYBE_CLASS_TYPE_P (type));
    5376              : 
    5377              :   /* These don't have names.  */
    5378    357242221 :   if (TREE_CODE (type) == DECLTYPE_TYPE
    5379    357242218 :       || TREE_CODE (type) == TYPEOF_TYPE)
    5380              :     return false;
    5381              : 
    5382    357242218 :   if (name && name == constructor_name (type))
    5383              :     return true;
    5384              : 
    5385              :   return false;
    5386              : }
    5387              : 
    5388              : /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
    5389              :    caller to set DECL_CONTEXT properly.
    5390              : 
    5391              :    Warning: For class and block-scope this must only be used when X
    5392              :    will be the new innermost binding for its name, as we tack it onto
    5393              :    the front of IDENTIFIER_BINDING without checking to see if the
    5394              :    current IDENTIFIER_BINDING comes from a closer binding level than
    5395              :    LEVEL.
    5396              : 
    5397              :    Warning: For namespace scope, this will look in LEVEL for an
    5398              :    existing binding to match, but if not found will push the decl into
    5399              :    CURRENT_NAMESPACE.  Use push_nested_namespace/pushdecl/
    5400              :    pop_nested_namespace if you really need to push it into a foreign
    5401              :    namespace.  */
    5402              : 
    5403              : static tree
    5404     65721281 : do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
    5405              : {
    5406     65721281 :   cp_binding_level *b;
    5407              : 
    5408     65721281 :   if (level->kind == sk_class)
    5409              :     {
    5410            0 :       gcc_checking_assert (!hiding);
    5411            0 :       b = class_binding_level;
    5412            0 :       class_binding_level = level;
    5413            0 :       pushdecl_class_level (x);
    5414            0 :       class_binding_level = b;
    5415              :     }
    5416              :   else
    5417              :     {
    5418     65721281 :       tree function_decl = current_function_decl;
    5419     65721281 :       if (level->kind == sk_namespace)
    5420     59236150 :         current_function_decl = NULL_TREE;
    5421     65721281 :       b = current_binding_level;
    5422     65721281 :       current_binding_level = level;
    5423     65721281 :       x = pushdecl (x, hiding);
    5424     65721281 :       current_binding_level = b;
    5425     65721281 :       current_function_decl = function_decl;
    5426              :     }
    5427     65721281 :   return x;
    5428              : }
    5429              : 
    5430              : /* Inject X into the local scope just before the function parms.  */
    5431              : 
    5432              : tree
    5433      3961099 : pushdecl_outermost_localscope (tree x)
    5434              : {
    5435      3961099 :   cp_binding_level *b = NULL;
    5436      3961099 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5437              : 
    5438              :   /* Find the block scope just inside the function parms.  */
    5439      3961099 :   cp_binding_level *n = current_binding_level;
    5440      4177294 :   while (n && n->kind != sk_block)
    5441       216195 :     n = n->level_chain;
    5442      9724557 :   for (; n && n->kind != sk_function_parms; n = b->level_chain)
    5443      5763458 :     b = n;
    5444              : 
    5445      3961099 :   return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
    5446      3961099 : }
    5447              : 
    5448              : /* Checks if BINDING is a binding that we can export.  */
    5449              : 
    5450              : static bool
    5451        24847 : check_can_export_using_decl (tree binding)
    5452              : {
    5453              :   /* Declarations in header units are always OK.  */
    5454        24847 :   if (header_module_p ())
    5455              :     return true;
    5456              : 
    5457              :   /* We want the linkage of the underlying entity, so strip typedefs.
    5458              :      If the underlying entity is a builtin type then we're OK.  */
    5459        13599 :   tree entity = binding;
    5460        13599 :   if (TREE_CODE (entity) == TYPE_DECL)
    5461              :     {
    5462          641 :       entity = TYPE_MAIN_DECL (TREE_TYPE (entity));
    5463          641 :       if (!entity)
    5464              :         return true;
    5465              :     }
    5466              : 
    5467        13461 :   linkage_kind linkage = decl_linkage (entity);
    5468        13461 :   tree not_tmpl = STRIP_TEMPLATE (entity);
    5469              : 
    5470              :   /* Attachment is determined by the owner of an enumerator.  */
    5471        13461 :   if (TREE_CODE (not_tmpl) == CONST_DECL)
    5472           27 :     not_tmpl = TYPE_NAME (DECL_CONTEXT (not_tmpl));
    5473              : 
    5474              :   /* If the using decl is exported, the things it refers to must
    5475              :      have external linkage.  decl_linkage returns lk_external for
    5476              :      module linkage so also check for attachment.  */
    5477        13461 :   if (linkage != lk_external
    5478        13461 :       || (DECL_LANG_SPECIFIC (not_tmpl)
    5479        13331 :           && DECL_MODULE_ATTACH_P (not_tmpl)
    5480           92 :           && !DECL_MODULE_EXPORT_P (not_tmpl)))
    5481              :     {
    5482          136 :       auto_diagnostic_group d;
    5483          136 :       error ("exporting %q#D that does not have external linkage",
    5484              :              binding);
    5485          136 :       if (linkage == lk_none)
    5486           27 :         inform (DECL_SOURCE_LOCATION (entity),
    5487              :                 "%q#D declared here with no linkage", entity);
    5488          109 :       else if (linkage == lk_internal)
    5489           47 :         inform (DECL_SOURCE_LOCATION (entity),
    5490              :                 "%q#D declared here with internal linkage", entity);
    5491              :       else
    5492           62 :         inform (DECL_SOURCE_LOCATION (entity),
    5493              :                 "%q#D declared here with module linkage", entity);
    5494          136 :       return false;
    5495          136 :     }
    5496              : 
    5497              :   return true;
    5498              : }
    5499              : 
    5500              : /* Process a local-scope or namespace-scope using declaration.  LOOKUP
    5501              :    is the result of qualified lookup (both value & type are
    5502              :    significant).  FN_SCOPE_P indicates if we're at function-scope (as
    5503              :    opposed to namespace-scope).  *VALUE_P and *TYPE_P are the current
    5504              :    bindings, which are altered to reflect the newly brought in
    5505              :    declarations.  */
    5506              : 
    5507              : static bool
    5508     18984892 : do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
    5509              :                          bool insert_p, tree *value_p, tree *type_p)
    5510              : {
    5511     18984892 :   tree value = *value_p;
    5512     18984892 :   tree type = *type_p;
    5513     18984892 :   bool failed = false;
    5514              : 
    5515              :   /* Shift the old and new bindings around so we're comparing class and
    5516              :      enumeration names to each other.  */
    5517     18984892 :   if (value && DECL_IMPLICIT_TYPEDEF_P (strip_using_decl (value)))
    5518              :     {
    5519              :       type = value;
    5520              :       value = NULL_TREE;
    5521              :     }
    5522              : 
    5523     18984892 :   if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
    5524              :     {
    5525       172819 :       lookup.type = lookup.value;
    5526       172819 :       lookup.value = NULL_TREE;
    5527              :     }
    5528              : 
    5529              :   /* Only process exporting if we're going to be inserting.  */
    5530     18984892 :   bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
    5531              : 
    5532              :   /* First do the value binding.  */
    5533     18984892 :   if (!lookup.value)
    5534              :     /* Nothing (only implicit typedef found).  */
    5535       172997 :     gcc_checking_assert (lookup.type);
    5536     18811895 :   else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
    5537              :     {
    5538     14985079 :       for (lkp_iterator usings (lookup.value); usings; ++usings)
    5539              :         {
    5540      9776035 :           tree new_fn = *usings;
    5541      9776035 :           tree inner = STRIP_TEMPLATE (new_fn);
    5542      9776035 :           bool exporting_p = revealing_p && module_exporting_p ();
    5543        21050 :           if (exporting_p)
    5544        21050 :             exporting_p = check_can_export_using_decl (new_fn);
    5545              : 
    5546              :           /* [namespace.udecl]
    5547              : 
    5548              :              If a function declaration in namespace scope or block
    5549              :              scope has the same name and the same parameter types as a
    5550              :              function introduced by a using declaration the program is
    5551              :              ill-formed.  */
    5552              :           /* This seems overreaching, asking core -- why do we care
    5553              :              about decls in the namespace that we cannot name (because
    5554              :              they are not transitively imported.  We just check the
    5555              :              decls that are in this TU.  */
    5556      9776035 :           bool found = false;
    5557    163725069 :           for (ovl_iterator old (value); !found && old; ++old)
    5558              :             {
    5559     77348032 :               tree old_fn = *old;
    5560              : 
    5561     77348032 :               if (new_fn == old_fn)
    5562              :                 {
    5563              :                   /* The function already exists in the current
    5564              :                      namespace.  We will still want to insert it if
    5565              :                      it is revealing a not-revealed thing.  */
    5566       275907 :                   found = true;
    5567       275907 :                   if (old.hidden_p ())
    5568              :                     /* The function was merged with a hidden built-in;
    5569              :                        insert it again as not hidden.  */
    5570              :                     found = false;
    5571       275662 :                   else if (!revealing_p)
    5572              :                     ;
    5573         8179 :                   else if (old.using_p ())
    5574              :                     {
    5575              :                       /* Update in place.  'tis ok.  */
    5576         7870 :                       OVL_PURVIEW_P (old.get_using ()) = true;
    5577         7870 :                       if (exporting_p)
    5578         7846 :                         OVL_EXPORT_P (old.get_using ()) = true;
    5579              :                     }
    5580          309 :                   else if (!DECL_LANG_SPECIFIC (inner)
    5581          309 :                            || !DECL_MODULE_PURVIEW_P (inner)
    5582          324 :                            || (exporting_p && !DECL_MODULE_EXPORT_P (inner)))
    5583              :                     /* We need to re-insert this function as a revealed
    5584              :                        (possibly exported) declaration.  We can't remove
    5585              :                        the existing decl because that will change any
    5586              :                        overloads cached in template functions.  */
    5587              :                     found = false;
    5588              :                   break;
    5589              :                 }
    5590     77072125 :               else if (old.using_p ())
    5591     72867260 :                 continue; /* This is a using decl. */
    5592      4204865 :               else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
    5593      2715893 :                 continue; /* This is an anticipated builtin.  */
    5594      1488972 :               else if (!matching_fn_p (new_fn, old_fn))
    5595      1488933 :                 continue; /* Parameters do not match.  */
    5596           39 :               else if (decls_match (new_fn, old_fn))
    5597              :                 {
    5598              :                   /* Extern "C" in different namespaces.  But similarly
    5599              :                      to above, if revealing a not-revealed thing we may
    5600              :                      need to reinsert.  */
    5601           18 :                   found = true;
    5602           18 :                   if (revealing_p
    5603           18 :                       && (!DECL_LANG_SPECIFIC (inner)
    5604            6 :                           || !DECL_MODULE_PURVIEW_P (inner)
    5605            3 :                           || (exporting_p && !DECL_MODULE_EXPORT_P (inner))))
    5606              :                     found = false;
    5607              :                   break;
    5608              :                 }
    5609              :               else
    5610              :                 {
    5611           21 :                   diagnose_name_conflict (new_fn, old_fn);
    5612           21 :                   failed = true;
    5613           21 :                   found = true;
    5614           21 :                   break;
    5615              :                 }
    5616              :             }
    5617              : 
    5618      9776035 :           if (!found && insert_p)
    5619              :             /* Unlike the decl-pushing case we don't drop anticipated
    5620              :                builtins here.  They don't cause a problem, and we'd
    5621              :                like to match them with a future declaration.  */
    5622      9500154 :             value = ovl_insert (new_fn, value, 1 + revealing_p + exporting_p);
    5623              :         }
    5624      5209044 :     }
    5625     13602851 :   else if (value
    5626              :            /* Ignore anticipated builtins.  */
    5627        61846 :            && !anticipated_builtin_p (value)
    5628     13664693 :            && !decls_match (lookup.value, strip_using_decl (value)))
    5629              :     {
    5630           15 :       diagnose_name_conflict (lookup.value, value);
    5631           15 :       failed = true;
    5632              :     }
    5633     13602836 :   else if (insert_p)
    5634              :     {
    5635              :       /* A using-decl does not necessarily have the same purview-ness or
    5636              :          exporting as the declaration it reveals, so build a USING_DECL
    5637              :          that we can attach this information to.  This also gives us a
    5638              :          location for the using-decl that we can use in diagnostics.
    5639              : 
    5640              :          But this is unnecessary if we're just redeclaring the same decl;
    5641              :          in that case we can just mark it purview or exported directly.  */
    5642     13601540 :       if (value != lookup.value)
    5643              :         {
    5644     13569678 :           value = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5645     13569678 :           USING_DECL_DECLS (value) = lookup.value;
    5646     13569678 :           USING_DECL_SCOPE (value) = CP_DECL_CONTEXT (lookup.value);
    5647     13569678 :           DECL_CONTEXT (value) = current_scope ();
    5648     13569678 :           DECL_MODULE_PURVIEW_P (value) = module_purview_p ();
    5649              :         }
    5650              :       else
    5651        31862 :         set_instantiating_module (value);
    5652              : 
    5653     13601540 :       if (revealing_p
    5654         3563 :           && module_exporting_p ()
    5655     13604968 :           && check_can_export_using_decl (lookup.value))
    5656              :         {
    5657         3340 :           if (TREE_CODE (value) == TEMPLATE_DECL)
    5658           23 :             DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (value)) = true;
    5659         3340 :           DECL_MODULE_EXPORT_P (value) = true;
    5660              :         }
    5661              :     }
    5662              : 
    5663              :   /* Now the type binding.  */
    5664     18984892 :   if (lookup.type)
    5665              :     {
    5666       173016 :       if (type && !decls_match (lookup.type, strip_using_decl (type)))
    5667              :         {
    5668            3 :           diagnose_name_conflict (lookup.type, type);
    5669            3 :           failed = true;
    5670              :         }
    5671       173013 :       else if (insert_p)
    5672              :         {
    5673              :           /* As with revealing value bindings.  */
    5674       172835 :           if (type != lookup.type)
    5675              :             {
    5676       172802 :               type = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5677       172802 :               USING_DECL_DECLS (type) = lookup.type;
    5678       172802 :               USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (lookup.type);
    5679       172802 :               DECL_CONTEXT (type) = current_scope ();
    5680       172802 :               DECL_MODULE_PURVIEW_P (type) = module_purview_p ();
    5681              :             }
    5682              :           else
    5683           33 :             set_instantiating_module (type);
    5684              : 
    5685       172835 :           if (revealing_p
    5686          388 :               && module_exporting_p ()
    5687       173204 :               && check_can_export_using_decl (lookup.type))
    5688          357 :             DECL_MODULE_EXPORT_P (type) = true;
    5689              :         }
    5690              :     }
    5691              : 
    5692     18984714 :   if (insert_p)
    5693              :     {
    5694              :       /* If value is empty, shift any class or enumeration name back.  */
    5695     18982163 :       if (!value)
    5696              :         {
    5697       172810 :           value = type;
    5698       172810 :           type = NULL_TREE;
    5699              :         }
    5700     18982163 :       *value_p = value;
    5701     18982163 :       *type_p = type;
    5702              :     }
    5703              : 
    5704     18984892 :   return failed;
    5705              : }
    5706              : 
    5707              : /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
    5708              :    Both are namespaces.  */
    5709              : 
    5710              : bool
    5711    125852195 : is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
    5712              : {
    5713    125852195 :   int depth = SCOPE_DEPTH (ancestor);
    5714              : 
    5715    125852195 :   if (!depth && !inline_only)
    5716              :     /* The global namespace encloses everything.  */
    5717              :     return true;
    5718              : 
    5719    127855388 :   while (SCOPE_DEPTH (descendant) > depth
    5720    127855388 :          && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
    5721      2093315 :     descendant = CP_DECL_CONTEXT (descendant);
    5722              : 
    5723    125762073 :   return ancestor == descendant;
    5724              : }
    5725              : 
    5726              : /* Returns true if ROOT (a non-alias namespace, class, or function)
    5727              :    encloses CHILD.  CHILD may be either a class type or a namespace
    5728              :    (maybe alias).  */
    5729              : 
    5730              : bool
    5731     95031563 : is_ancestor (tree root, tree child)
    5732              : {
    5733     95031563 :   gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
    5734              :                         && !DECL_NAMESPACE_ALIAS (root))
    5735              :                        || TREE_CODE (root) == FUNCTION_DECL
    5736              :                        || CLASS_TYPE_P (root));
    5737     95031563 :   gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
    5738              :                        || CLASS_TYPE_P (child));
    5739              : 
    5740              :   /* The global namespace encloses everything.  Early-out for the
    5741              :      common case.  */
    5742     95031563 :   if (root == global_namespace)
    5743              :     return true;
    5744              : 
    5745              :   /* Search CHILD until we reach namespace scope.  */
    5746    200336681 :   while (TREE_CODE (child) != NAMESPACE_DECL)
    5747              :     {
    5748              :       /* If we've reached the ROOT, it encloses CHILD.  */
    5749    105569939 :       if (root == child)
    5750              :         return true;
    5751              : 
    5752              :       /* Go out one level.  */
    5753    105569888 :       if (TYPE_P (child))
    5754    103121399 :         child = TYPE_NAME (child);
    5755    105569888 :       child = CP_DECL_CONTEXT (child);
    5756              :     }
    5757              : 
    5758     94766742 :   if (TREE_CODE (root) != NAMESPACE_DECL)
    5759              :     /* Failed to meet the non-namespace we were looking for.  */
    5760              :     return false;
    5761              : 
    5762     94766727 :   if (tree alias = DECL_NAMESPACE_ALIAS (child))
    5763            3 :     child = alias;
    5764              : 
    5765     94766727 :   return is_nested_namespace (root, child);
    5766              : }
    5767              : 
    5768              : /* Enter the class or namespace scope indicated by T suitable for name
    5769              :    lookup.  T can be arbitrary scope, not necessary nested inside the
    5770              :    current scope.  Returns a non-null scope to pop iff pop_scope
    5771              :    should be called later to exit this scope.  */
    5772              : 
    5773              : tree
    5774    344148994 : push_scope (tree t)
    5775              : {
    5776    344148994 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5777     81395662 :     push_decl_namespace (t);
    5778    262753332 :   else if (CLASS_TYPE_P (t))
    5779              :     {
    5780    199417916 :       if (!at_class_scope_p ()
    5781    199417916 :           || !same_type_p (current_class_type, t))
    5782     97157081 :         push_nested_class (t);
    5783              :       else
    5784              :         /* T is the same as the current scope.  There is therefore no
    5785              :            need to re-enter the scope.  Since we are not actually
    5786              :            pushing a new scope, our caller should not call
    5787              :            pop_scope.  */
    5788              :         t = NULL_TREE;
    5789              :     }
    5790              : 
    5791    344148994 :   return t;
    5792              : }
    5793              : 
    5794              : /* Leave scope pushed by push_scope.  */
    5795              : 
    5796              : void
    5797    241888627 : pop_scope (tree t)
    5798              : {
    5799    241888627 :   if (t == NULL_TREE)
    5800              :     return;
    5801    241888156 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5802     81395659 :     pop_decl_namespace ();
    5803    160492497 :   else if CLASS_TYPE_P (t)
    5804     97157081 :     pop_nested_class ();
    5805              : }
    5806              : 
    5807              : /* Subroutine of push_inner_scope.  */
    5808              : 
    5809              : static void
    5810       258981 : push_inner_scope_r (tree outer, tree inner)
    5811              : {
    5812       258981 :   tree prev;
    5813              : 
    5814       258981 :   if (outer == inner
    5815       258981 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5816              :     return;
    5817              : 
    5818       258972 :   prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5819       258972 :   if (outer != prev)
    5820         1698 :     push_inner_scope_r (outer, prev);
    5821       258972 :   if (TREE_CODE (inner) == NAMESPACE_DECL)
    5822              :     {
    5823              :       cp_binding_level *save_template_parm = 0;
    5824              :       /* Temporary take out template parameter scopes.  They are saved
    5825              :          in reversed order in save_template_parm.  */
    5826       105867 :       while (current_binding_level->kind == sk_template_parms)
    5827              :         {
    5828        52866 :           cp_binding_level *b = current_binding_level;
    5829        52866 :           current_binding_level = b->level_chain;
    5830        52866 :           b->level_chain = save_template_parm;
    5831        52866 :           save_template_parm = b;
    5832              :         }
    5833              : 
    5834        53001 :       resume_scope (NAMESPACE_LEVEL (inner));
    5835        53001 :       current_namespace = inner;
    5836              : 
    5837              :       /* Restore template parameter scopes.  */
    5838       105867 :       while (save_template_parm)
    5839              :         {
    5840        52866 :           cp_binding_level *b = save_template_parm;
    5841        52866 :           save_template_parm = b->level_chain;
    5842        52866 :           b->level_chain = current_binding_level;
    5843        52866 :           current_binding_level = b;
    5844              :         }
    5845              :     }
    5846              :   else
    5847       205971 :     pushclass (inner);
    5848              : }
    5849              : 
    5850              : /* Enter the scope INNER from current scope.  INNER must be a scope
    5851              :    nested inside current scope.  This works with both name lookup and
    5852              :    pushing name into scope.  In case a template parameter scope is present,
    5853              :    namespace is pushed under the template parameter scope according to
    5854              :    name lookup rule in 14.6.1/6.
    5855              : 
    5856              :    Return the former current scope suitable for pop_inner_scope.  */
    5857              : 
    5858              : tree
    5859       257283 : push_inner_scope (tree inner)
    5860              : {
    5861       257283 :   tree outer = current_scope ();
    5862       257283 :   if (!outer)
    5863            0 :     outer = current_namespace;
    5864              : 
    5865       257283 :   push_inner_scope_r (outer, inner);
    5866       257283 :   return outer;
    5867              : }
    5868              : 
    5869              : /* Exit the current scope INNER back to scope OUTER.  */
    5870              : 
    5871              : void
    5872       257283 : pop_inner_scope (tree outer, tree inner)
    5873              : {
    5874       257283 :   if (outer == inner
    5875       257283 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5876              :     return;
    5877              : 
    5878       516249 :   while (outer != inner)
    5879              :     {
    5880       258975 :       if (TREE_CODE (inner) == NAMESPACE_DECL)
    5881              :         {
    5882              :           cp_binding_level *save_template_parm = 0;
    5883              :           /* Temporary take out template parameter scopes.  They are saved
    5884              :              in reversed order in save_template_parm.  */
    5885       105867 :           while (current_binding_level->kind == sk_template_parms)
    5886              :             {
    5887        52866 :               cp_binding_level *b = current_binding_level;
    5888        52866 :               current_binding_level = b->level_chain;
    5889        52866 :               b->level_chain = save_template_parm;
    5890        52866 :               save_template_parm = b;
    5891              :             }
    5892              : 
    5893        53001 :           pop_namespace ();
    5894              : 
    5895              :           /* Restore template parameter scopes.  */
    5896       158868 :           while (save_template_parm)
    5897              :             {
    5898        52866 :               cp_binding_level *b = save_template_parm;
    5899        52866 :               save_template_parm = b->level_chain;
    5900        52866 :               b->level_chain = current_binding_level;
    5901        52866 :               current_binding_level = b;
    5902              :             }
    5903              :         }
    5904              :       else
    5905       205974 :         popclass ();
    5906              : 
    5907       258972 :       inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5908              :     }
    5909              : }
    5910              : 
    5911              : /* Do a pushlevel for class declarations.  */
    5912              : 
    5913              : void
    5914    291417730 : pushlevel_class (void)
    5915              : {
    5916    291417730 :   class_binding_level = begin_scope (sk_class, current_class_type);
    5917    291417730 : }
    5918              : 
    5919              : /* ...and a poplevel for class declarations.  */
    5920              : 
    5921              : void
    5922    495289401 : poplevel_class (void)
    5923              : {
    5924    495289401 :   cp_binding_level *level = class_binding_level;
    5925    495289401 :   cp_class_binding *cb;
    5926    495289401 :   size_t i;
    5927    495289401 :   tree shadowed;
    5928              : 
    5929    495289401 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5930    495289401 :   gcc_assert (level != 0);
    5931              : 
    5932              :   /* If we're leaving a toplevel class, cache its binding level.  */
    5933    495289398 :   if (current_class_depth == 1)
    5934    281386175 :     previous_class_level = level;
    5935    495289398 :   for (shadowed = level->type_shadowed;
    5936   1468062579 :        shadowed;
    5937    972773181 :        shadowed = TREE_CHAIN (shadowed))
    5938    972773181 :     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
    5939              : 
    5940              :   /* Remove the bindings for all of the class-level declarations.  */
    5941    495289398 :   if (level->class_shadowed)
    5942              :     {
    5943    641374800 :       FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
    5944              :         {
    5945    489669409 :           IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
    5946    489669409 :           cxx_binding_free (cb->base);
    5947              :         }
    5948    151705391 :       ggc_free (level->class_shadowed);
    5949    151705391 :       level->class_shadowed = NULL;
    5950              :     }
    5951              : 
    5952              :   /* Now, pop out of the binding level which we created up in the
    5953              :      `pushlevel_class' routine.  */
    5954    495289398 :   gcc_assert (current_binding_level == level);
    5955    495289398 :   leave_scope ();
    5956    495289398 : }
    5957              : 
    5958              : /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
    5959              :    appropriate.  DECL is the value to which a name has just been
    5960              :    bound.  CLASS_TYPE is the class in which the lookup occurred.  */
    5961              : 
    5962              : static void
    5963    151347104 : set_inherited_value_binding_p (cxx_binding *binding, tree decl,
    5964              :                                tree class_type)
    5965              : {
    5966    151347104 :   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
    5967              :     {
    5968    150974837 :       tree context;
    5969              : 
    5970    150974837 :       if (is_overloaded_fn (decl))
    5971     43545546 :         context = ovl_scope (decl);
    5972              :       else
    5973              :         {
    5974    107429291 :           gcc_assert (DECL_P (decl));
    5975    107429291 :           context = context_for_name_lookup (decl);
    5976              :         }
    5977              : 
    5978    150974837 :       if (is_properly_derived_from (class_type, context))
    5979     18062109 :         INHERITED_VALUE_BINDING_P (binding) = 1;
    5980              :       else
    5981    132912728 :         INHERITED_VALUE_BINDING_P (binding) = 0;
    5982              :     }
    5983       372267 :   else if (binding->value == decl)
    5984              :     /* We only encounter a TREE_LIST when there is an ambiguity in the
    5985              :        base classes.  Such an ambiguity can be overridden by a
    5986              :        definition in this class.  */
    5987       372267 :     INHERITED_VALUE_BINDING_P (binding) = 1;
    5988              :   else
    5989            0 :     INHERITED_VALUE_BINDING_P (binding) = 0;
    5990    151347104 : }
    5991              : 
    5992              : /* Make the declaration of X appear in CLASS scope.  */
    5993              : 
    5994              : bool
    5995    199356379 : pushdecl_class_level (tree x)
    5996              : {
    5997    199356379 :   bool is_valid = true;
    5998              : 
    5999              :   /* Do nothing if we're adding to an outer lambda closure type,
    6000              :      outer_binding will add it later if it's needed.  */
    6001    199356379 :   if (current_class_type != class_binding_level->this_entity)
    6002              :     return true;
    6003              : 
    6004    199356379 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6005              :   /* Get the name of X.  */
    6006    398712758 :   tree name = OVL_NAME (x);
    6007              : 
    6008    199356379 :   if (name)
    6009              :     {
    6010    198951055 :       is_valid = push_class_level_binding (name, x);
    6011    198951055 :       if (TREE_CODE (x) == TYPE_DECL)
    6012    144854637 :         set_identifier_type_value (name, x);
    6013              :     }
    6014       405324 :   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
    6015              :     {
    6016              :       /* If X is an anonymous aggregate, all of its members are
    6017              :          treated as if they were members of the class containing the
    6018              :          aggregate, for naming purposes.  */
    6019       276031 :       location_t save_location = input_location;
    6020       276031 :       tree anon = TREE_TYPE (x);
    6021       276031 :       if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
    6022       785110 :         for (unsigned ix = member_vec->length (); ix--;)
    6023              :           {
    6024       739666 :             tree binding = (*member_vec)[ix];
    6025       739666 :             if (STAT_HACK_P (binding))
    6026              :               {
    6027            0 :                 if (!pushdecl_class_level (STAT_TYPE (binding)))
    6028            0 :                   is_valid = false;
    6029            0 :                 binding = STAT_DECL (binding);
    6030              :               }
    6031       739666 :             if (!pushdecl_class_level (binding))
    6032            0 :               is_valid = false;
    6033              :         }
    6034              :       else
    6035      1008086 :         for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
    6036       777499 :           if (TREE_CODE (f) == FIELD_DECL)
    6037              :             {
    6038       473965 :               input_location = DECL_SOURCE_LOCATION (f);
    6039       473965 :               if (!pushdecl_class_level (f))
    6040       777499 :                 is_valid = false;
    6041              :             }
    6042       276031 :       input_location = save_location;
    6043              :     }
    6044    199356379 :   return is_valid;
    6045    199356379 : }
    6046              : 
    6047              : /* Return the BINDING (if any) for NAME in SCOPE, which is a class
    6048              :    scope.  If the value returned is non-NULL, and the PREVIOUS field
    6049              :    is not set, callers must set the PREVIOUS field explicitly.  */
    6050              : 
    6051              : static cxx_binding *
    6052   2112538870 : get_class_binding (tree name, cp_binding_level *scope)
    6053              : {
    6054   2112538870 :   tree class_type;
    6055   2112538870 :   tree type_binding;
    6056   2112538870 :   tree value_binding;
    6057   2112538870 :   cxx_binding *binding;
    6058              : 
    6059   2112538870 :   class_type = scope->this_entity;
    6060              : 
    6061              :   /* Get the type binding.  */
    6062   2112538870 :   type_binding = lookup_member (class_type, name,
    6063              :                                 /*protect=*/2, /*want_type=*/true,
    6064              :                                 tf_warning_or_error);
    6065              :   /* Get the value binding.  */
    6066   2112538870 :   value_binding = lookup_member (class_type, name,
    6067              :                                  /*protect=*/2, /*want_type=*/false,
    6068              :                                  tf_warning_or_error);
    6069              : 
    6070              :   /* If we found either a type binding or a value binding, create a
    6071              :      new binding object.  */
    6072   2112538870 :   if (type_binding || value_binding)
    6073              :     {
    6074    151347104 :       binding = new_class_binding (name,
    6075              :                                    value_binding,
    6076              :                                    type_binding,
    6077              :                                    scope);
    6078    151347104 :       set_inherited_value_binding_p (binding, value_binding, class_type);
    6079              :     }
    6080              :   else
    6081              :     binding = NULL;
    6082              : 
    6083   2112538870 :   return binding;
    6084              : }
    6085              : 
    6086              : /* Make the declaration(s) of X appear in CLASS scope under the name
    6087              :    NAME.  Returns true if the binding is valid.  */
    6088              : 
    6089              : bool
    6090    479750217 : push_class_level_binding (tree name, tree x)
    6091              : {
    6092    479750217 :   cxx_binding *binding;
    6093    479750217 :   tree decl = x;
    6094    479750217 :   bool ok;
    6095              : 
    6096    479750217 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6097              : 
    6098              :   /* The class_binding_level will be NULL if x is a template
    6099              :      parameter name in a member template.  */
    6100    479750217 :   if (!class_binding_level)
    6101              :     return true;
    6102              : 
    6103    479750217 :   if (name == error_mark_node)
    6104              :     return false;
    6105              : 
    6106              :   /* Can happen for an erroneous declaration (c++/60384).  */
    6107    479750217 :   if (!identifier_p (name))
    6108              :     {
    6109            3 :       gcc_assert (errorcount || sorrycount);
    6110              :       return false;
    6111              :     }
    6112              : 
    6113              :   /* Check for invalid member names.  But don't worry about a default
    6114              :      argument-scope lambda being pushed after the class is complete.  */
    6115    479750235 :   gcc_assert (TYPE_BEING_DEFINED (current_class_type)
    6116              :               || LAMBDA_TYPE_P (TREE_TYPE (decl)));
    6117              :   /* Check that we're pushing into the right binding level.  */
    6118    479750214 :   gcc_assert (current_class_type == class_binding_level->this_entity);
    6119              : 
    6120              :   /* We could have been passed a tree list if this is an ambiguous
    6121              :      declaration. If so, pull the declaration out because
    6122              :      check_template_shadow will not handle a TREE_LIST.  */
    6123    479750214 :   if (TREE_CODE (decl) == TREE_LIST
    6124    479750214 :       && TREE_TYPE (decl) == error_mark_node)
    6125            0 :     decl = TREE_VALUE (decl);
    6126              : 
    6127    479750214 :   if (!check_template_shadow (decl))
    6128              :     return false;
    6129              : 
    6130              :   /* [class.mem]
    6131              : 
    6132              :      If T is the name of a class, then each of the following shall
    6133              :      have a name different from T:
    6134              : 
    6135              :      -- every static data member of class T;
    6136              : 
    6137              :      -- every member of class T that is itself a type;
    6138              : 
    6139              :      -- every enumerator of every member of class T that is an
    6140              :         enumerated type;
    6141              : 
    6142              :      -- every member of every anonymous union that is a member of
    6143              :         class T.
    6144              : 
    6145              :      (Non-static data members were also forbidden to have the same
    6146              :      name as T until TC1.)  */
    6147    479750166 :   if ((VAR_P (x)
    6148    479750166 :        || TREE_CODE (x) == CONST_DECL
    6149    461502023 :        || (TREE_CODE (x) == TYPE_DECL
    6150    144854616 :            && !DECL_SELF_REFERENCE_P (x))
    6151              :        /* A data member of an anonymous union.  */
    6152    389039213 :        || (TREE_CODE (x) == FIELD_DECL
    6153     29055170 :            && DECL_CONTEXT (x) != current_class_type))
    6154    553335972 :       && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
    6155              :     {
    6156           24 :       tree scope = context_for_name_lookup (x);
    6157           24 :       if (TYPE_P (scope) && same_type_p (scope, current_class_type))
    6158              :         {
    6159           24 :           error_at (DECL_SOURCE_LOCATION (x),
    6160              :                     "%qD has the same name as the class in which it is "
    6161              :                     "declared", x);
    6162           24 :           return false;
    6163              :         }
    6164              :     }
    6165              : 
    6166              :   /* Get the current binding for NAME in this class, if any.  */
    6167    479750142 :   binding = IDENTIFIER_BINDING (name);
    6168    479750142 :   if (!binding || binding->scope != class_binding_level)
    6169              :     {
    6170    349969915 :       binding = get_class_binding (name, class_binding_level);
    6171              :       /* If a new binding was created, put it at the front of the
    6172              :          IDENTIFIER_BINDING list.  */
    6173    349969915 :       if (binding)
    6174              :         {
    6175     11644769 :           binding->previous = IDENTIFIER_BINDING (name);
    6176     11644769 :           IDENTIFIER_BINDING (name) = binding;
    6177              :         }
    6178              :     }
    6179              : 
    6180              :   /* If there is already a binding, then we may need to update the
    6181              :      current value.  */
    6182    141424996 :   if (binding && binding->value)
    6183              :     {
    6184    141424996 :       tree bval = binding->value;
    6185    141424996 :       tree old_decl = NULL_TREE;
    6186    141424996 :       tree target_decl = strip_using_decl (decl);
    6187    141424996 :       tree target_bval = strip_using_decl (bval);
    6188              : 
    6189    141424996 :       if (INHERITED_VALUE_BINDING_P (binding))
    6190              :         {
    6191              :           /* If the old binding was from a base class, and was for a
    6192              :              tag name, slide it over to make room for the new binding.
    6193              :              The old binding is still visible if explicitly qualified
    6194              :              with a class-key.  */
    6195     13933892 :           if (TREE_CODE (target_bval) == TYPE_DECL
    6196      6712232 :               && DECL_ARTIFICIAL (target_bval)
    6197     14856806 :               && !(TREE_CODE (target_decl) == TYPE_DECL
    6198       922904 :                    && DECL_ARTIFICIAL (target_decl)))
    6199              :             {
    6200        79892 :               old_decl = binding->type;
    6201        79892 :               binding->type = bval;
    6202        79892 :               binding->value = NULL_TREE;
    6203        79892 :               INHERITED_VALUE_BINDING_P (binding) = 0;
    6204              :             }
    6205              :           else
    6206              :             {
    6207     13854000 :               old_decl = bval;
    6208              :               /* Any inherited type declaration is hidden by the type
    6209              :                  declaration in the derived class.  */
    6210     13854000 :               if (TREE_CODE (target_decl) == TYPE_DECL
    6211     13854000 :                   && DECL_ARTIFICIAL (target_decl))
    6212       843531 :                 binding->type = NULL_TREE;
    6213              :             }
    6214              :         }
    6215    127491104 :       else if (TREE_CODE (decl) == USING_DECL
    6216        10987 :                && TREE_CODE (bval) == USING_DECL
    6217    127491210 :                && same_type_p (USING_DECL_SCOPE (decl),
    6218              :                                USING_DECL_SCOPE (bval)))
    6219              :         /* This is a using redeclaration that will be diagnosed later
    6220              :            in supplement_binding */
    6221              :         ;
    6222    127491071 :       else if (TREE_CODE (decl) == USING_DECL
    6223        10954 :                && TREE_CODE (bval) == USING_DECL
    6224           73 :                && DECL_DEPENDENT_P (decl)
    6225    127491090 :                && DECL_DEPENDENT_P (bval))
    6226              :         return true;
    6227    127491052 :       else if (TREE_CODE (decl) == USING_DECL
    6228        10935 :                && DECL_DEPENDENT_P (decl)
    6229    127500903 :                && OVL_P (target_bval))
    6230              :         /* The new dependent using beats an old overload.  */
    6231              :         old_decl = bval;
    6232    127481201 :       else if (TREE_CODE (bval) == USING_DECL
    6233       677034 :                && DECL_DEPENDENT_P (bval)
    6234    127862133 :                && OVL_P (target_decl))
    6235              :         /* The old dependent using beats a new overload.  */
    6236              :         return true;
    6237    127100281 :       else if (OVL_P (target_decl)
    6238    126874640 :                && OVL_P (target_bval))
    6239              :         /* The new overload set contains the old one.  */
    6240              :         old_decl = bval;
    6241              : 
    6242    140818359 :       if (old_decl && binding->scope == class_binding_level)
    6243              :         {
    6244    140818359 :           binding->value = x;
    6245              :           /* It is always safe to clear INHERITED_VALUE_BINDING_P
    6246              :              here.  This function is only used to register bindings
    6247              :              from with the class definition itself.  */
    6248    140818359 :           INHERITED_VALUE_BINDING_P (binding) = 0;
    6249    140818359 :           return true;
    6250              :         }
    6251              :     }
    6252              : 
    6253              :   /* Note that we declared this value so that we can issue an error if
    6254              :      this is an invalid redeclaration of a name already used for some
    6255              :      other purpose.  */
    6256    338550844 :   note_name_declared_in_class (name, decl);
    6257              : 
    6258              :   /* If we didn't replace an existing binding, put the binding on the
    6259              :      stack of bindings for the identifier, and update the shadowed
    6260              :      list.  */
    6261    338550844 :   if (binding && binding->scope == class_binding_level)
    6262              :     /* Supplement the existing binding.  */
    6263       225698 :     ok = supplement_binding (binding, decl);
    6264              :   else
    6265              :     {
    6266              :       /* Create a new binding.  */
    6267    338325146 :       push_binding (name, decl, class_binding_level);
    6268    338325146 :       ok = true;
    6269              :     }
    6270              : 
    6271              :   return ok;
    6272    479750217 : }
    6273              : 
    6274              : /* Process and lookup a using decl SCOPE::lookup.name, filling in
    6275              :    lookup.values & lookup.type.  Return a USING_DECL, or NULL_TREE on
    6276              :    failure.  */
    6277              : 
    6278              : static tree
    6279     22075573 : lookup_using_decl (tree scope, name_lookup &lookup)
    6280              : {
    6281     22075573 :   tree current = current_scope ();
    6282     22075573 :   bool dependent_p = false;
    6283     22075573 :   tree binfo = NULL_TREE;
    6284     22075573 :   base_kind b_kind = bk_not_base;
    6285              : 
    6286              :   /* Because C++20 breaks the invariant that only member using-decls
    6287              :      refer to members and only non-member using-decls refer to
    6288              :      non-members, we first do the lookups, and then do validation that
    6289              :      what we found is ok.  */
    6290              : 
    6291     22075573 :   if (TREE_CODE (scope) == ENUMERAL_TYPE
    6292     12620675 :       && cxx_dialect < cxx20
    6293     12620675 :       && UNSCOPED_ENUM_P (scope)
    6294     22075583 :       && !TYPE_FUNCTION_SCOPE_P (scope))
    6295              :     {
    6296              :       /* PR c++/60265 argued that since C++11 added explicit enum scope, we
    6297              :          should allow it as meaning the enclosing scope.  I don't see any
    6298              :          justification for this in C++11, but let's keep allowing it.  */
    6299            9 :       tree ctx = CP_TYPE_CONTEXT (scope);
    6300           24 :       if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
    6301     22075573 :         scope = ctx;
    6302              :     }
    6303              : 
    6304              :   /* You cannot using-decl a destructor.  */
    6305     22075573 :   if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
    6306              :     {
    6307            6 :       error ("%<%T%s%D%> names destructor", scope,
    6308            3 :              &"::"[scope == global_namespace ? 2 : 0], lookup.name);
    6309            3 :       return NULL_TREE;
    6310              :     }
    6311              : 
    6312     22075570 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    6313              :     {
    6314              :       /* Naming a namespace member.  */
    6315      6541176 :       qualified_namespace_lookup (scope, &lookup);
    6316              : 
    6317      6541176 :       if (TYPE_P (current)
    6318            3 :           && (!lookup.value
    6319            0 :               || lookup.type
    6320            0 :               || cxx_dialect < cxx20
    6321            0 :               || TREE_CODE (lookup.value) != CONST_DECL))
    6322              :         {
    6323            3 :           error ("using-declaration for non-member at class scope");
    6324            3 :           return NULL_TREE;
    6325              :         }
    6326              :     }
    6327     15534394 :   else if (TREE_CODE (scope) == ENUMERAL_TYPE)
    6328              :     {
    6329              :       /* Naming an enumeration member.  */
    6330     12620665 :       if (cxx_dialect < cxx20)
    6331            6 :         error ("%<using%> with enumeration scope %q#T "
    6332              :                "only available with %<-std=c++20%> or %<-std=gnu++20%>",
    6333              :                scope);
    6334     12620665 :       lookup.value = lookup_enumerator (scope, lookup.name);
    6335              :     }
    6336              :   else
    6337              :     {
    6338              :       /* Naming a class member.  This is awkward in C++20, because we
    6339              :          might be naming an enumerator of an unrelated class.  */
    6340              : 
    6341      2913729 :       tree npscope = scope;
    6342      2913729 :       if (PACK_EXPANSION_P (scope))
    6343         9079 :         npscope = PACK_EXPANSION_PATTERN (scope);
    6344              : 
    6345      2913729 :       if (!MAYBE_CLASS_TYPE_P (npscope))
    6346              :         {
    6347            9 :           error ("%qT is not a class, namespace, or enumeration", npscope);
    6348            9 :           return NULL_TREE;
    6349              :         }
    6350              : 
    6351              :       /* Using T::T declares inheriting ctors, even if T is a typedef.  */
    6352      2913720 :       if (lookup.name == TYPE_IDENTIFIER (npscope)
    6353      2913720 :           || constructor_name_p (lookup.name, npscope))
    6354              :         {
    6355       304589 :           if (!TYPE_P (current))
    6356              :             {
    6357            0 :               error ("non-member using-declaration names constructor of %qT",
    6358              :                      npscope);
    6359            0 :               return NULL_TREE;
    6360              :             }
    6361       304589 :           maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
    6362       304589 :           lookup.name = ctor_identifier;
    6363       304589 :           CLASSTYPE_NON_AGGREGATE (current) = true;
    6364              :         }
    6365              : 
    6366      2913720 :       if (!TYPE_P (current) && cxx_dialect < cxx20)
    6367              :         {
    6368            3 :           error ("using-declaration for member at non-class scope");
    6369            3 :           return NULL_TREE;
    6370              :         }
    6371              : 
    6372      2913717 :       bool depscope = dependent_scope_p (scope);
    6373              : 
    6374      2913717 :       if (depscope)
    6375              :         /* Leave binfo null.  */;
    6376      2051859 :       else if (TYPE_P (current))
    6377              :         {
    6378      2051842 :           binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
    6379      2051842 :           gcc_checking_assert (b_kind >= bk_not_base);
    6380              : 
    6381      2051842 :           if (b_kind == bk_not_base && any_dependent_bases_p ())
    6382              :             /* Treat as-if dependent.  */
    6383              :             depscope = true;
    6384      2051821 :           else if (lookup.name == ctor_identifier
    6385      2051821 :                    && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
    6386              :             {
    6387           15 :               if (any_dependent_bases_p ())
    6388              :                 depscope = true;
    6389              :               else
    6390              :                 {
    6391           15 :                   error ("%qT is not a direct base of %qT", scope, current);
    6392           15 :                   return NULL_TREE;
    6393              :                 }
    6394              :             }
    6395              : 
    6396      2051827 :           if (b_kind < bk_proper_base)
    6397           52 :             binfo = TYPE_BINFO (scope);
    6398              :         }
    6399              :       else
    6400           17 :         binfo = TYPE_BINFO (scope);
    6401              : 
    6402      4103661 :       dependent_p = (depscope
    6403      2051844 :                      || (IDENTIFIER_CONV_OP_P (lookup.name)
    6404       137100 :                          && dependent_type_p (TREE_TYPE (lookup.name))));
    6405              : 
    6406      2051817 :       if (!dependent_p)
    6407      2051817 :         lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
    6408              :                                       /*want_type=*/false, tf_none);
    6409              : 
    6410              :       /* If the lookup in the base contains a dependent using, this
    6411              :          using is also dependent.  */
    6412      2051817 :       if (!dependent_p && lookup.value && dependent_type_p (scope))
    6413              :         {
    6414           27 :           tree val = lookup.value;
    6415           27 :           if (tree fns = maybe_get_fns (val))
    6416            9 :             val = fns;
    6417           75 :           for (tree f: lkp_range (val))
    6418           27 :             if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
    6419              :               {
    6420              :                 dependent_p = true;
    6421              :                 break;
    6422              :               }
    6423              :         }
    6424              : 
    6425      2913702 :       if (!depscope && b_kind < bk_proper_base)
    6426              :         {
    6427           48 :           if (cxx_dialect >= cxx20 && lookup.value
    6428           34 :               && TREE_CODE (lookup.value) == CONST_DECL)
    6429              :             {
    6430              :               /* Using an unrelated enum; check access here rather
    6431              :                  than separately for class and non-class using.  */
    6432           21 :               perform_or_defer_access_check
    6433           21 :                 (binfo, lookup.value, lookup.value, tf_warning_or_error);
    6434              :               /* And then if this is a copy from handle_using_decl, look
    6435              :                  through to the original enumerator.  */
    6436           21 :               if (CONST_DECL_USING_P (lookup.value))
    6437            9 :                 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
    6438              :             }
    6439           27 :           else if (!TYPE_P (current))
    6440              :             {
    6441            2 :               error ("using-declaration for member at non-class scope");
    6442            2 :               return NULL_TREE;
    6443              :             }
    6444              :           else
    6445              :             {
    6446           25 :               auto_diagnostic_group g;
    6447           25 :               error_not_base_type (scope, current);
    6448           16 :               if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
    6449           28 :                   && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
    6450            3 :                 inform (input_location,
    6451              :                         "did you mean %<using enum %T::%D%>?",
    6452              :                         scope, lookup.name);
    6453           25 :               return NULL_TREE;
    6454           25 :             }
    6455              :         }
    6456              :     }
    6457              : 
    6458              :   /* Did we find anything sane?  */
    6459     15534340 :   if (dependent_p)
    6460              :     ;
    6461     21213622 :   else if (!lookup.value)
    6462              :     {
    6463           57 :       error ("%qD has not been declared in %qD", lookup.name, scope);
    6464           57 :       return NULL_TREE;
    6465              :     }
    6466     21213565 :   else if (TREE_CODE (lookup.value) == TREE_LIST
    6467              :            /* We can (independently) have ambiguous implicit typedefs.  */
    6468     21213565 :            || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
    6469              :     {
    6470            3 :       auto_diagnostic_group d;
    6471            3 :       error ("reference to %qD is ambiguous", lookup.name);
    6472            3 :       print_candidates (input_location,
    6473            3 :                         TREE_CODE (lookup.value) == TREE_LIST
    6474              :                         ? lookup.value : lookup.type);
    6475            3 :       return NULL_TREE;
    6476            3 :     }
    6477     21213562 :   else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    6478              :     {
    6479            6 :       error ("using-declaration may not name namespace %qD", lookup.value);
    6480            6 :       return NULL_TREE;
    6481              :     }
    6482              : 
    6483     22075447 :   if (TYPE_P (current))
    6484              :     {
    6485              :       /* In class scope.  */
    6486              : 
    6487              :       /* Cannot introduce a constructor name.  */
    6488      3093284 :       if (constructor_name_p (lookup.name, current))
    6489              :         {
    6490            3 :           error ("%<%T::%D%> names constructor in %qT",
    6491              :                  scope, lookup.name, current);
    6492            3 :           return NULL_TREE;
    6493              :         }
    6494              : 
    6495      3093281 :       if (lookup.value && BASELINK_P (lookup.value))
    6496              :         /* The binfo from which the functions came does not matter.  */
    6497      1851531 :         lookup.value = BASELINK_FUNCTIONS (lookup.value);
    6498              :     }
    6499              : 
    6500     22075444 :   tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    6501     22075444 :   USING_DECL_SCOPE (using_decl) = scope;
    6502     22075444 :   USING_DECL_DECLS (using_decl) = lookup.value;
    6503     22075444 :   DECL_DEPENDENT_P (using_decl) = dependent_p;
    6504     22075444 :   DECL_CONTEXT (using_decl) = current;
    6505     22075444 :   if (TYPE_P (current) && b_kind == bk_not_base)
    6506      1041539 :     USING_DECL_UNRELATED_P (using_decl) = true;
    6507              : 
    6508              :   return using_decl;
    6509              : }
    6510              : 
    6511              : /* Process "using SCOPE::NAME" in a class scope.  Return the
    6512              :    USING_DECL created.  */
    6513              : 
    6514              : tree
    6515      3093372 : do_class_using_decl (tree scope, tree name)
    6516              : {
    6517      3093372 :   if (name == error_mark_node
    6518      3093369 :       || scope == error_mark_node)
    6519              :     return NULL_TREE;
    6520              : 
    6521      3093366 :   name_lookup lookup (name);
    6522      3093366 :   return lookup_using_decl (scope, lookup);
    6523      3093366 : }
    6524              : 
    6525              : 
    6526              : /* Return the binding for NAME in NS in the current TU.  If NS is
    6527              :    NULL, look in global_namespace.  We will not find declarations
    6528              :    from imports.  Users of this who, having found nothing, push a new
    6529              :    decl must be prepared for that pushing to match an existing decl.  */
    6530              : 
    6531              : tree
    6532     11226679 : get_namespace_binding (tree ns, tree name)
    6533              : {
    6534     11226679 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6535     11226679 :   if (!ns)
    6536     11226679 :     ns = global_namespace;
    6537     11226679 :   gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
    6538     11226679 :   tree ret = NULL_TREE;
    6539              : 
    6540     11226679 :   if (tree *b = find_namespace_slot (ns, name))
    6541              :     {
    6542      5014523 :       ret = *b;
    6543              : 
    6544      5014523 :       if (TREE_CODE (ret) == BINDING_VECTOR)
    6545            0 :         ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
    6546            0 :       if (ret)
    6547      5014523 :         ret = strip_using_decl (MAYBE_STAT_DECL (ret));
    6548              :     }
    6549              : 
    6550     22453358 :   return ret;
    6551     11226679 : }
    6552              : 
    6553              : /* Push internal DECL into the global namespace.  Does not do the
    6554              :    full overload fn handling and does not add it to the list of things
    6555              :    in the namespace.  */
    6556              : 
    6557              : void
    6558      3771825 : set_global_binding (tree decl)
    6559              : {
    6560      3771825 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6561              : 
    6562      3771825 :   tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
    6563              : 
    6564      3771825 :   if (*slot)
    6565              :     /* The user's placed something in the implementor's namespace.  */
    6566            0 :     diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
    6567              : 
    6568              :   /* Force the binding, so compiler internals continue to work.  */
    6569      3771825 :   *slot = decl;
    6570      3771825 : }
    6571              : 
    6572              : /* Set the context of a declaration to scope. Complain if we are not
    6573              :    outside scope.  */
    6574              : 
    6575              : void
    6576       232922 : set_decl_namespace (tree decl, tree scope, bool friendp)
    6577              : {
    6578              :   /* Get rid of namespace aliases.  */
    6579       232922 :   scope = ORIGINAL_NAMESPACE (scope);
    6580              : 
    6581              :   /* It is ok for friends to be qualified in parallel space.  */
    6582       232922 :   if (!friendp && !is_nested_namespace (current_namespace, scope))
    6583            6 :     error ("declaration of %qD not in a namespace surrounding %qD",
    6584              :            decl, scope);
    6585       232922 :   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6586              : 
    6587              :   /* See whether this has been declared in the namespace or inline
    6588              :      children.  */
    6589       232922 :   tree old = NULL_TREE;
    6590       232922 :   {
    6591       232922 :     name_lookup lookup (DECL_NAME (decl),
    6592       232922 :                         LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
    6593       232922 :     if (!lookup.search_qualified (scope, /*usings=*/false))
    6594              :       /* No old declaration at all.  */
    6595           30 :       goto not_found;
    6596       232892 :     old = lookup.value;
    6597       232922 :   }
    6598              : 
    6599              :   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
    6600       232892 :   if (TREE_CODE (old) == TREE_LIST)
    6601              :     {
    6602            9 :     ambiguous:
    6603           15 :       auto_diagnostic_group d;
    6604           15 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6605           15 :       error ("reference to %qD is ambiguous", decl);
    6606           15 :       print_candidates (input_location, old);
    6607           15 :       return;
    6608              :     }
    6609              : 
    6610       232883 :   if (!DECL_DECLARES_FUNCTION_P (decl))
    6611              :     {
    6612              :       /* Don't compare non-function decls with decls_match here, since
    6613              :          it can't check for the correct constness at this
    6614              :          point.  pushdecl will find those errors later.  */
    6615              : 
    6616              :       /* We might have found it in an inline namespace child of SCOPE.  */
    6617        13731 :       if (TREE_CODE (decl) == TREE_CODE (old))
    6618           35 :         DECL_CONTEXT (decl) = DECL_CONTEXT (old);
    6619              : 
    6620        13696 :     found:
    6621              :       /* Writing "N::i" to declare something directly in "N" is invalid.  */
    6622        55917 :       if (CP_DECL_CONTEXT (decl) == current_namespace
    6623        55917 :           && at_namespace_scope_p ())
    6624            3 :         error_at (DECL_SOURCE_LOCATION (decl),
    6625              :                   "explicit qualification in declaration of %qD", decl);
    6626        55917 :       return;
    6627              :     }
    6628              : 
    6629              :   /* Since decl is a function, old should contain a function decl.  */
    6630       219152 :   if (!OVL_P (old))
    6631              :     {
    6632            9 :     not_found:
    6633              :       /* It didn't work, go back to the explicit scope.  */
    6634           45 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6635           45 :       error ("%qD should have been declared inside %qD", decl, scope);
    6636              : 
    6637           45 :       return;
    6638              :     }
    6639              : 
    6640              :   /* We handle these in check_explicit_instantiation_namespace.  */
    6641       219143 :   if (processing_explicit_instantiation)
    6642              :     return;
    6643       218965 :   if (processing_template_decl || processing_specialization)
    6644              :     /* We have not yet called push_template_decl to turn a
    6645              :        FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
    6646              :        match.  But, we'll check later, when we construct the
    6647              :        template.  */
    6648              :     return;
    6649              : 
    6650              :   /* Instantiations or specializations of templates may be declared as
    6651              :      friends in any namespace.  */
    6652       118167 :   if (friendp && DECL_USE_TEMPLATE (decl))
    6653              :     return;
    6654              : 
    6655        42210 :   tree found = NULL_TREE;
    6656        42210 :   bool hidden_p = false;
    6657        42210 :   bool saw_template = false;
    6658              : 
    6659       103152 :   for (lkp_iterator iter (old); iter; ++iter)
    6660              :     {
    6661        60948 :       if (iter.using_p ())
    6662            0 :         continue;
    6663              : 
    6664        60948 :       tree ofn = *iter;
    6665              : 
    6666              :       /* Adjust DECL_CONTEXT first so decls_match will return true
    6667              :          if DECL will match a declaration in an inline namespace.  */
    6668        60948 :       DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
    6669        60948 :       if (decls_match (decl, ofn))
    6670              :         {
    6671        42198 :           if (found)
    6672              :             {
    6673              :               /* We found more than one matching declaration.  This
    6674              :                  can happen if we have two inline namespace children,
    6675              :                  each containing a suitable declaration.  */
    6676            6 :               DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6677            6 :               goto ambiguous;
    6678              :             }
    6679        42192 :           found = ofn;
    6680        42192 :           hidden_p = iter.hidden_p ();
    6681              :         }
    6682        18750 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    6683        18750 :                && TREE_CODE (ofn) == TEMPLATE_DECL)
    6684        60942 :         saw_template = true;
    6685              :     }
    6686              : 
    6687        42204 :   if (!found && friendp && saw_template)
    6688              :     {
    6689              :       /* "[if no non-template match is found,] each remaining function template
    6690              :          is replaced with the specialization chosen by deduction from the
    6691              :          friend declaration or discarded if deduction fails."
    6692              : 
    6693              :          So tell check_explicit_specialization to look for a match.  */
    6694           12 :       SET_DECL_IMPLICIT_INSTANTIATION (decl);
    6695           12 :       DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
    6696           12 :       return;
    6697              :     }
    6698              : 
    6699        42192 :   if (found)
    6700              :     {
    6701        42186 :       if (hidden_p)
    6702              :         {
    6703            6 :           auto_diagnostic_group d;
    6704            6 :           pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    6705              :                    "%qD has not been declared within %qD", decl, scope);
    6706            6 :           inform (DECL_SOURCE_LOCATION (found),
    6707              :                   "only here as a %<friend%>");
    6708            6 :         }
    6709        42186 :       DECL_CONTEXT (decl) = DECL_CONTEXT (found);
    6710        42186 :       goto found;
    6711              :     }
    6712              : 
    6713            6 :   goto not_found;
    6714              : }
    6715              : 
    6716              : /* Return the namespace where the current declaration is declared.  */
    6717              : 
    6718              : tree
    6719   1471284878 : current_decl_namespace (void)
    6720              : {
    6721   1471284878 :   tree result;
    6722              :   /* If we have been pushed into a different namespace, use it.  */
    6723   1471284878 :   if (!vec_safe_is_empty (decl_namespace_list))
    6724     66527913 :     return decl_namespace_list->last ();
    6725              : 
    6726   1404756965 :   if (current_class_type)
    6727    662696752 :     result = decl_namespace_context (current_class_type);
    6728    742060213 :   else if (current_function_decl)
    6729    274783125 :     result = decl_namespace_context (current_function_decl);
    6730              :   else
    6731    467277088 :     result = current_namespace;
    6732              :   return result;
    6733              : }
    6734              : 
    6735              : /* Process any ATTRIBUTES on a namespace definition.  Returns true if
    6736              :    attribute visibility is seen.  */
    6737              : 
    6738              : bool
    6739      6812972 : handle_namespace_attrs (tree ns, tree attributes)
    6740              : {
    6741      6812972 :   tree d;
    6742      6812972 :   bool saw_vis = false;
    6743              : 
    6744      6812972 :   if (attributes == error_mark_node)
    6745              :     return false;
    6746              : 
    6747      9732580 :   for (d = attributes; d; d = TREE_CHAIN (d))
    6748              :     {
    6749      2919608 :       tree name = get_attribute_name (d);
    6750      2919608 :       tree args = TREE_VALUE (d);
    6751              : 
    6752      2919608 :       if (is_attribute_p ("visibility", name)
    6753      2919608 :           && is_attribute_namespace_p ("gnu", d))
    6754              :         {
    6755              :           /* attribute visibility is a property of the syntactic block
    6756              :              rather than the namespace as a whole, so we don't touch the
    6757              :              NAMESPACE_DECL at all.  */
    6758      2864073 :           tree x = args ? TREE_VALUE (args) : NULL_TREE;
    6759      2864073 :           if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
    6760              :             {
    6761            0 :               warning (OPT_Wattributes,
    6762              :                        "%qD attribute requires a single NTBS argument",
    6763              :                        name);
    6764            0 :               continue;
    6765              :             }
    6766              : 
    6767      2864073 :           if (!TREE_PUBLIC (ns))
    6768            0 :             warning (OPT_Wattributes,
    6769              :                      "%qD attribute is meaningless since members of the "
    6770              :                      "anonymous namespace get local symbols", name);
    6771              : 
    6772      2864073 :           push_visibility (TREE_STRING_POINTER (x), 1);
    6773      2864073 :           saw_vis = true;
    6774              :         }
    6775        55535 :       else if (is_attribute_p ("abi_tag", name)
    6776        55535 :                && is_attribute_namespace_p ("gnu", d))
    6777              :         {
    6778        45847 :           if (!DECL_NAME (ns))
    6779              :             {
    6780            3 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6781              :                        "namespace", name);
    6782            3 :               continue;
    6783              :             }
    6784        45844 :           if (!DECL_NAMESPACE_INLINE_P (ns))
    6785              :             {
    6786            0 :               warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
    6787              :                        "namespace", name);
    6788            0 :               continue;
    6789              :             }
    6790        45844 :           if (!args)
    6791              :             {
    6792           18 :               tree dn = DECL_NAME (ns);
    6793           18 :               args = build_string (IDENTIFIER_LENGTH (dn) + 1,
    6794           18 :                                    IDENTIFIER_POINTER (dn));
    6795           18 :               TREE_TYPE (args) = char_array_type_node;
    6796           18 :               args = fix_string_type (args);
    6797           18 :               args = build_tree_list (NULL_TREE, args);
    6798              :             }
    6799        45844 :           if (check_abi_tag_args (args, name))
    6800        45844 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6801        45844 :                                               DECL_ATTRIBUTES (ns));
    6802              :         }
    6803         9688 :       else if (is_attribute_p ("deprecated", name)
    6804         9688 :                && is_attribute_namespace_p ("", d))
    6805              :         {
    6806         9572 :           if (!DECL_NAME (ns))
    6807              :             {
    6808            7 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6809              :                        "namespace", name);
    6810            7 :               continue;
    6811              :             }
    6812        19063 :           if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
    6813              :             {
    6814            0 :               error ("deprecated message is not a string");
    6815            0 :               continue;
    6816              :             }
    6817         9565 :           TREE_DEPRECATED (ns) = 1;
    6818         9565 :           if (args)
    6819         9498 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6820         9498 :                                               DECL_ATTRIBUTES (ns));
    6821              :         }
    6822          116 :       else if (annotation_p (d))
    6823              :         {
    6824           20 :           const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (d));
    6825           20 :           bool no_add_attrs = false;
    6826           20 :           as->handler (&ns, name, args, 0, &no_add_attrs);
    6827           20 :           if (!no_add_attrs)
    6828           18 :             DECL_ATTRIBUTES (ns) = tree_cons (TREE_PURPOSE (d), args,
    6829           18 :                                               DECL_ATTRIBUTES (ns));
    6830              :         }
    6831           96 :       else if (!attribute_ignored_p (d))
    6832              :         {
    6833           81 :           warning (OPT_Wattributes, "%qD attribute directive ignored",
    6834              :                    name);
    6835           81 :           continue;
    6836              :         }
    6837              :     }
    6838              : 
    6839              :   return saw_vis;
    6840              : }
    6841              : 
    6842              : /* Temporarily set the namespace for the current declaration.  */
    6843              : 
    6844              : void
    6845     81712336 : push_decl_namespace (tree decl)
    6846              : {
    6847     81712336 :   if (TREE_CODE (decl) != NAMESPACE_DECL)
    6848            0 :     decl = decl_namespace_context (decl);
    6849     81712336 :   vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
    6850     81712336 : }
    6851              : 
    6852              : /* [namespace.memdef]/2 */
    6853              : 
    6854              : void
    6855     81712333 : pop_decl_namespace (void)
    6856              : {
    6857     81712333 :   decl_namespace_list->pop ();
    6858     81712333 : }
    6859              : 
    6860              : /* Process a namespace-alias declaration.  */
    6861              : 
    6862              : void
    6863       120398 : do_namespace_alias (location_t loc, tree alias, tree name_space)
    6864              : {
    6865       120398 :   if (name_space == error_mark_node)
    6866              :     return;
    6867              : 
    6868       120389 :   if (TREE_CODE (name_space) == NAMESPACE_DECL)
    6869       120388 :     name_space = ORIGINAL_NAMESPACE (name_space);
    6870              :   else
    6871            1 :     gcc_assert (TREE_CODE (name_space) == SPLICE_EXPR);
    6872              : 
    6873              :   /* Build the alias.  */
    6874       120389 :   alias = build_lang_decl_loc (loc, NAMESPACE_DECL, alias, void_type_node);
    6875       120389 :   DECL_NAMESPACE_ALIAS (alias) = name_space;
    6876       120389 :   DECL_EXTERNAL (alias) = 1;
    6877       120389 :   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
    6878       120389 :   TREE_PUBLIC (alias) = TREE_PUBLIC (CP_DECL_CONTEXT (alias));
    6879              : 
    6880       120389 :   alias = pushdecl (alias);
    6881              : 
    6882       240778 :   if (!DECL_P (alias) || !DECL_NAMESPACE_ALIAS (alias))
    6883              :     return;
    6884              : 
    6885       120386 :   set_originating_module (alias);
    6886       120386 :   check_module_decl_linkage (alias);
    6887              : 
    6888              :   /* Emit debug info for namespace alias.  */
    6889       120386 :   if (!building_stmt_list_p ())
    6890        11958 :     (*debug_hooks->early_global_decl) (alias);
    6891              : }
    6892              : 
    6893              : /* Like pushdecl, only it places DECL in the current namespace,
    6894              :    if appropriate.  */
    6895              : 
    6896              : tree
    6897     52564034 : pushdecl_namespace_level (tree decl, bool hiding)
    6898              : {
    6899     52564034 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6900     52564034 :   return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
    6901     52564034 :                                  hiding);
    6902     52564034 : }
    6903              : 
    6904              : /* Wrapper around push_local_binding to push the bindings for
    6905              :    a non-member USING_DECL with NAME and VALUE.  LOOKUP, if non-null,
    6906              :    is the result of name lookup during template parsing.  */
    6907              : 
    6908              : static void
    6909     13133006 : push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
    6910              : {
    6911     13133006 :   tree type = NULL_TREE;
    6912              : 
    6913     13133006 :   cxx_binding *binding = find_local_binding (current_binding_level, name);
    6914     13133006 :   if (binding)
    6915              :     {
    6916           84 :       value = binding->value;
    6917           84 :       type = binding->type;
    6918              :     }
    6919              : 
    6920     13133006 :   if (lookup)
    6921     12644103 :     do_nonmember_using_decl (*lookup, true, true, &value, &type);
    6922              : 
    6923     13133006 :   if (!value)
    6924              :     ;
    6925     13133006 :   else if (binding && value == binding->value)
    6926              :     /* Redeclaration of this USING_DECL.  */;
    6927           48 :   else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
    6928              :     {
    6929              :       /* We already have this binding, so replace it.  */
    6930           36 :       update_local_overload (IDENTIFIER_BINDING (name), value);
    6931           36 :       IDENTIFIER_BINDING (name)->value = value;
    6932              :     }
    6933              :   else
    6934              :     /* Install the new binding.  */
    6935     13132934 :     push_local_binding (name, value, /*using=*/true);
    6936              : 
    6937     13133006 :   if (!type)
    6938              :     ;
    6939           21 :   else if (binding && type == binding->type)
    6940              :     ;
    6941              :   else
    6942              :     {
    6943           15 :       push_local_binding (name, type, /*using=*/true);
    6944           15 :       set_identifier_type_value (name, type);
    6945              :     }
    6946     13133006 : }
    6947              : 
    6948              : /* Overload for push_using_decl_bindings that doesn't take a name_lookup.  */
    6949              : 
    6950              : void
    6951       488903 : push_using_decl_bindings (tree name, tree value)
    6952              : {
    6953       488903 :   push_using_decl_bindings (nullptr, name, value);
    6954       488903 : }
    6955              : 
    6956              : /* Process a using declaration in non-class scope.  */
    6957              : 
    6958              : void
    6959     18982207 : finish_nonmember_using_decl (tree scope, tree name)
    6960              : {
    6961     18982207 :   gcc_checking_assert (current_binding_level->kind != sk_class);
    6962              : 
    6963     18982207 :   if (scope == error_mark_node || name == error_mark_node)
    6964           44 :     return;
    6965              : 
    6966     18982207 :   name_lookup lookup (name);
    6967              : 
    6968     18982207 :   tree using_decl = lookup_using_decl (scope, lookup);
    6969     18982207 :   if (!using_decl)
    6970           44 :     return;
    6971              : 
    6972              :   /* Emit debug info.  */
    6973     18982163 :   if (!processing_template_decl)
    6974      6358094 :     cp_emit_debug_info_for_using (lookup.value,
    6975      6358094 :                                   current_binding_level->this_entity);
    6976              : 
    6977     18982163 :   if (current_binding_level->kind == sk_namespace)
    6978              :     {
    6979      6338060 :       tree *slot = find_namespace_slot (current_namespace, name, true);
    6980      6338060 :       tree *mslot = get_fixed_binding_slot (slot, name,
    6981              :                                             BINDING_SLOT_CURRENT, true);
    6982      6338060 :       bool failed = false;
    6983              : 
    6984      6338060 :       if (mslot != slot)
    6985              :         {
    6986              :           /* A module vector.  I presume the binding list is going to
    6987              :              be sparser than the import bitmap.  Hence iterate over
    6988              :              the former checking for bits set in the bitmap.  */
    6989         3288 :           bitmap imports = get_import_bitmap ();
    6990         3288 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    6991              : 
    6992              :           /* Scan the imported bindings.  */
    6993         3288 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    6994         3288 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    6995              :             {
    6996         3288 :               ix--;
    6997         3288 :               cluster++;
    6998              :             }
    6999              : 
    7000              :           /* Do this in forward order, so we load modules in an order
    7001              :              the user expects.  */
    7002         6576 :           for (; ix--; cluster++)
    7003         9864 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    7004              :               {
    7005              :                 /* Are we importing this module?  */
    7006         6576 :                 if (unsigned base = cluster->indices[jx].base)
    7007         3284 :                   if (unsigned span = cluster->indices[jx].span)
    7008         3284 :                     do
    7009         3284 :                       if (bitmap_bit_p (imports, base))
    7010         2729 :                         goto found;
    7011          555 :                     while (++base, --span);
    7012         3847 :                 continue;
    7013              : 
    7014         2729 :               found:;
    7015              :                 /* Is it loaded?  */
    7016         2729 :                 if (cluster->slots[jx].is_lazy ())
    7017              :                   {
    7018            0 :                     gcc_assert (cluster->indices[jx].span == 1);
    7019            0 :                     lazy_load_binding (cluster->indices[jx].base,
    7020              :                                        scope, name, &cluster->slots[jx]);
    7021              :                   }
    7022              : 
    7023         2729 :                 tree value = cluster->slots[jx];
    7024         2729 :                 if (!value)
    7025              :                   /* Load errors could mean there's nothing here.  */
    7026            0 :                   continue;
    7027              : 
    7028              :                 /* Extract what we can see from here.  If there's no
    7029              :                    stat_hack, then everything was exported.  */
    7030         2729 :                 tree type = NULL_TREE;
    7031              : 
    7032              :                 /* If no stat hack, everything is visible.  */
    7033         2729 :                 if (STAT_HACK_P (value))
    7034              :                   {
    7035         2711 :                     if (STAT_TYPE_VISIBLE_P (value))
    7036            0 :                       type = STAT_TYPE (value);
    7037         2711 :                     value = STAT_VISIBLE (value);
    7038              :                   }
    7039              : 
    7040         2729 :                 if (do_nonmember_using_decl (lookup, false, false,
    7041              :                                              &value, &type))
    7042              :                   {
    7043            0 :                     failed = true;
    7044            0 :                     break;
    7045              :                   }
    7046         3847 :               }
    7047              :         }
    7048              : 
    7049         3288 :       if (!failed)
    7050              :         {
    7051              :           /* Now do the current slot.  */
    7052      6338060 :           tree value = MAYBE_STAT_DECL (*mslot);
    7053      6338060 :           tree type = MAYBE_STAT_TYPE (*mslot);
    7054              : 
    7055      6338060 :           do_nonmember_using_decl (lookup, false, true, &value, &type);
    7056              : 
    7057              :           // FIXME: Partition mergeableness?
    7058      6338060 :           if (STAT_HACK_P (*mslot))
    7059              :             {
    7060          699 :               STAT_DECL (*mslot) = value;
    7061          699 :               STAT_TYPE (*mslot) = type;
    7062              :             }
    7063      6337361 :           else if (type)
    7064           22 :             *mslot = stat_hack (value, type);
    7065              :           else
    7066      6337339 :             *mslot = value;
    7067              :         }
    7068              :     }
    7069              :   else
    7070              :     {
    7071     12644103 :       add_decl_expr (using_decl);
    7072     12644103 :       if (DECL_DEPENDENT_P (using_decl))
    7073            7 :         lookup.value = using_decl;
    7074     12644103 :       push_using_decl_bindings (&lookup, name, NULL_TREE);
    7075              :     }
    7076     18982207 : }
    7077              : 
    7078              : /* Return the declarations that are members of the namespace NS.  */
    7079              : 
    7080              : tree
    7081           18 : cp_namespace_decls (tree ns)
    7082              : {
    7083           18 :   return NAMESPACE_LEVEL (ns)->names;
    7084              : }
    7085              : 
    7086              : /* Given a lookup that returned VAL, use FLAGS to decide if we want to
    7087              :    ignore it or not.  Subroutine of lookup_name_1 and lookup_type_scope.  */
    7088              : 
    7089              : static bool
    7090   3506419190 : qualify_lookup (tree val, LOOK_want want)
    7091              : {
    7092   3506419190 :   if (val == NULL_TREE)
    7093              :     return false;
    7094              : 
    7095   3506419190 :   if (bool (want & LOOK_want::TYPE))
    7096              :     {
    7097     42732058 :       tree target_val = strip_using_decl (val);
    7098              : 
    7099     42732058 :       if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
    7100              :         return true;
    7101              :     }
    7102              : 
    7103   3463985585 :   if (bool (want & LOOK_want::TYPE_NAMESPACE))
    7104       506390 :     return TREE_CODE (val) == NAMESPACE_DECL;
    7105              : 
    7106              :   return true;
    7107              : }
    7108              : 
    7109              : /* Is there a "using namespace std;" directive within USINGS?  */
    7110              : 
    7111              : static bool
    7112         5374 : using_directives_contain_std_p (vec<tree, va_gc> *usings)
    7113              : {
    7114         5374 :   if (!usings)
    7115              :     return false;
    7116              : 
    7117           46 :   for (unsigned ix = usings->length (); ix--;)
    7118           31 :     if (strip_using_decl ((*usings)[ix]) == std_node)
    7119              :       return true;
    7120              : 
    7121              :   return false;
    7122              : }
    7123              : 
    7124              : /* Is there a "using namespace std;" directive within the current
    7125              :    namespace (or its ancestors)?
    7126              :    Compare with name_lookup::search_unqualified.  */
    7127              : 
    7128              : static bool
    7129         1714 : has_using_namespace_std_directive_p ()
    7130              : {
    7131         1714 :   for (cp_binding_level *level = current_binding_level;
    7132         7072 :        level;
    7133         5358 :        level = level->level_chain)
    7134         5374 :     if (using_directives_contain_std_p (level->using_directives))
    7135              :       return true;
    7136              : 
    7137              :   return false;
    7138              : }
    7139              : 
    7140              : /* Subclass of deferred_diagnostic, for issuing a note when
    7141              :    --param cxx-max-namespaces-for-diagnostic-help is reached.
    7142              : 
    7143              :    The note should be issued after the error, but before any other
    7144              :    deferred diagnostics.  This is handled by decorating a wrapped
    7145              :    deferred_diagnostic, and emitting a note before that wrapped note is
    7146              :    deleted.  */
    7147              : 
    7148              : class namespace_limit_reached : public deferred_diagnostic
    7149              : {
    7150              :  public:
    7151            3 :   namespace_limit_reached (location_t loc, unsigned limit, tree name,
    7152              :                            std::unique_ptr<deferred_diagnostic> wrapped)
    7153            3 :   : deferred_diagnostic (loc),
    7154            3 :     m_limit (limit), m_name (name),
    7155            3 :     m_wrapped (std::move (wrapped))
    7156              :   {
    7157              :   }
    7158              : 
    7159            6 :   ~namespace_limit_reached ()
    7160            3 :   {
    7161              :     /* Unconditionally warn that the search was truncated.  */
    7162            3 :     inform (get_location (),
    7163              :             "maximum limit of %d namespaces searched for %qE",
    7164              :             m_limit, m_name);
    7165              :     /* m_wrapped will be implicitly deleted after this, emitting any followup
    7166              :        diagnostic after the above note.  */
    7167            6 :   }
    7168              : 
    7169              :  private:
    7170              :   unsigned m_limit;
    7171              :   tree m_name;
    7172              :   std::unique_ptr<deferred_diagnostic> m_wrapped;
    7173              : };
    7174              : 
    7175              : /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
    7176              :    Emit a note showing the location of the declaration of the suggestion.  */
    7177              : 
    7178              : class show_candidate_location : public deferred_diagnostic
    7179              : {
    7180              :  public:
    7181          110 :   show_candidate_location (location_t loc, tree candidate)
    7182          110 :   : deferred_diagnostic (loc),
    7183          110 :     m_candidate (candidate)
    7184              :   {
    7185              :   }
    7186              : 
    7187          220 :   ~show_candidate_location ()
    7188          110 :   {
    7189          110 :     inform (location_of (m_candidate), "%qE declared here", m_candidate);
    7190          220 :   }
    7191              : 
    7192              :  private:
    7193              :   tree m_candidate;
    7194              : };
    7195              : 
    7196              : /* Subclass of deferred_diagnostic, for use when there are multiple candidates
    7197              :    to be suggested by suggest_alternatives_for.
    7198              : 
    7199              :    Emit a series of notes showing the various suggestions.  */
    7200              : 
    7201              : class suggest_alternatives : public deferred_diagnostic
    7202              : {
    7203              :  public:
    7204           17 :   suggest_alternatives (location_t loc, vec<tree> candidates)
    7205           17 :   : deferred_diagnostic (loc),
    7206           17 :     m_candidates (candidates)
    7207              :   {
    7208              :   }
    7209              : 
    7210           34 :   ~suggest_alternatives ()
    7211           17 :   {
    7212           17 :     if (m_candidates.length ())
    7213              :       {
    7214           17 :         inform_n (get_location (), m_candidates.length (),
    7215              :                   "suggested alternative:",
    7216              :                   "suggested alternatives:");
    7217          108 :         for (unsigned ix = 0; ix != m_candidates.length (); ix++)
    7218              :           {
    7219           37 :             tree val = m_candidates[ix];
    7220              : 
    7221           37 :             inform (location_of (val), "  %qE", val);
    7222              :           }
    7223              :       }
    7224           17 :     m_candidates.release ();
    7225           34 :   }
    7226              : 
    7227              :  private:
    7228              :   vec<tree> m_candidates;
    7229              : };
    7230              : 
    7231              : /* A class for encapsulating the result of a search across
    7232              :    multiple namespaces (and scoped enums within them) for an
    7233              :    unrecognized name seen at a given source location.  */
    7234              : 
    7235              : class namespace_hints
    7236              : {
    7237              :  public:
    7238              :   namespace_hints (location_t loc, tree name);
    7239              : 
    7240              :   name_hint convert_candidates_to_name_hint ();
    7241              :   name_hint maybe_decorate_with_limit (name_hint);
    7242              : 
    7243              :  private:
    7244              :   void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
    7245              : 
    7246              :   location_t m_loc;
    7247              :   tree m_name;
    7248              :   vec<tree> m_candidates;
    7249              : 
    7250              :   /* Value of "--param cxx-max-namespaces-for-diagnostic-help".  */
    7251              :   unsigned m_limit;
    7252              : 
    7253              :   /* Was the limit reached?  */
    7254              :   bool m_limited;
    7255              : };
    7256              : 
    7257              : /* Constructor for namespace_hints.  Search namespaces and scoped enums,
    7258              :    looking for an exact match for unrecognized NAME seen at LOC.  */
    7259              : 
    7260         1879 : namespace_hints::namespace_hints (location_t loc, tree name)
    7261         1879 : : m_loc(loc), m_name (name)
    7262              : {
    7263         1879 :   auto_vec<tree> worklist;
    7264              : 
    7265         1879 :   m_candidates = vNULL;
    7266         1879 :   m_limited = false;
    7267         1879 :   m_limit = param_cxx_max_namespaces_for_diagnostic_help;
    7268              : 
    7269              :   /* Breadth-first search of namespaces.  Up to limit namespaces
    7270              :      searched (limit zero == unlimited).  */
    7271         1879 :   worklist.safe_push (global_namespace);
    7272        16406 :   for (unsigned ix = 0; ix != worklist.length (); ix++)
    7273              :     {
    7274         6324 :       tree ns = worklist[ix];
    7275         6324 :       name_lookup lookup (name);
    7276              : 
    7277         6324 :       if (lookup.search_qualified (ns, false))
    7278          132 :         m_candidates.safe_push (lookup.value);
    7279              : 
    7280         6324 :       if (!m_limited)
    7281              :         {
    7282              :           /* Look for child namespaces.  We have to do this
    7283              :              indirectly because they are chained in reverse order,
    7284              :              which is confusing to the user.  */
    7285         6312 :           auto_vec<tree> children;
    7286              : 
    7287         6312 :           for (tree decl = NAMESPACE_LEVEL (ns)->names;
    7288      5047662 :                decl; decl = TREE_CHAIN (decl))
    7289              :             {
    7290      5041350 :               if (TREE_CODE (decl) == NAMESPACE_DECL
    7291         4581 :                   && !DECL_NAMESPACE_ALIAS (decl)
    7292      5045924 :                   && !DECL_NAMESPACE_INLINE_P (decl))
    7293         4454 :                 children.safe_push (decl);
    7294              : 
    7295              :               /* Look for exact matches for NAME within scoped enums.
    7296              :                  These aren't added to the worklist, and so don't count
    7297              :                  against the search limit.  */
    7298      5041350 :               if (TREE_CODE (decl) == TYPE_DECL)
    7299              :                 {
    7300        54418 :                   tree type = TREE_TYPE (decl);
    7301        54418 :                   if (SCOPED_ENUM_P (type))
    7302         1431 :                     maybe_add_candidate_for_scoped_enum (type, name);
    7303              :                 }
    7304              :             }
    7305              : 
    7306        17069 :           while (!m_limited && !children.is_empty ())
    7307              :             {
    7308         8896 :               if (worklist.length () == m_limit)
    7309            3 :                 m_limited = true;
    7310              :               else
    7311         4445 :                 worklist.safe_push (children.pop ());
    7312              :             }
    7313         6312 :         }
    7314         6324 :     }
    7315         1879 : }
    7316              : 
    7317              : /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
    7318              :    for m_name, an IDENTIFIER_NODE for which name lookup failed.
    7319              : 
    7320              :    If m_candidates is non-empty, use it to generate a suggestion and/or
    7321              :    a deferred diagnostic that lists the possible candidate(s).
    7322              : */
    7323              : 
    7324              : name_hint
    7325         1879 : namespace_hints::convert_candidates_to_name_hint ()
    7326              : {
    7327              :   /* How many candidates do we have?  */
    7328              : 
    7329              :   /* If we have just one candidate, issue a name_hint with it as a suggestion
    7330              :      (so that consumers are able to suggest it within the error message and emit
    7331              :      it as a fix-it hint), and with a note showing the candidate's location.  */
    7332         1879 :   if (m_candidates.length () == 1)
    7333              :     {
    7334          110 :       tree candidate = m_candidates[0];
    7335              :       /* Clean up CANDIDATES.  */
    7336          110 :       m_candidates.release ();
    7337          110 :       return name_hint (expr_to_string (candidate),
    7338          110 :                         std::make_unique<show_candidate_location> (m_loc,
    7339          110 :                                                                    candidate));
    7340              :     }
    7341         1769 :   else if (m_candidates.length () > 1)
    7342              :     /* If we have more than one candidate, issue a name_hint without a single
    7343              :        "suggestion", but with a deferred diagnostic that lists the
    7344              :        various candidates.  This takes ownership of m_candidates.  */
    7345           17 :     return name_hint (NULL,
    7346           17 :                       std::make_unique<suggest_alternatives> (m_loc,
    7347           17 :                                                               m_candidates));
    7348              : 
    7349              :   /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary.  */
    7350         1752 :   gcc_assert (m_candidates.length () == 0);
    7351         1752 :   gcc_assert (m_candidates == vNULL);
    7352              : 
    7353         1752 :   return name_hint ();
    7354              : }
    7355              : 
    7356              : /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
    7357              :    then we want to emit a note about after the error, but before
    7358              :    any other deferred diagnostics.
    7359              : 
    7360              :    Handle this by figuring out what hint is needed, then optionally
    7361              :    decorating HINT with a namespace_limit_reached wrapper.  */
    7362              : 
    7363              : name_hint
    7364         1879 : namespace_hints::maybe_decorate_with_limit (name_hint hint)
    7365              : {
    7366         1879 :   if (m_limited)
    7367            3 :     return name_hint
    7368              :       (hint.suggestion (),
    7369            3 :        std::make_unique<namespace_limit_reached> (m_loc, m_limit,
    7370            3 :                                                   m_name,
    7371            6 :                                                   hint.take_deferred ()));
    7372              :   else
    7373         1876 :     return hint;
    7374              : }
    7375              : 
    7376              : /* Look inside SCOPED_ENUM for exact matches for NAME.
    7377              :    If one is found, add its CONST_DECL to m_candidates.  */
    7378              : 
    7379              : void
    7380         1431 : namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
    7381              :                                                       tree name)
    7382              : {
    7383         1431 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    7384              : 
    7385         2387 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    7386              :     {
    7387          971 :       tree id = TREE_PURPOSE (iter);
    7388          971 :       if (id == name)
    7389              :         {
    7390           15 :           m_candidates.safe_push (TREE_VALUE (iter));
    7391           15 :           return;
    7392              :         }
    7393              :     }
    7394              : }
    7395              : 
    7396              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    7397              :    name lookup failed.
    7398              : 
    7399              :    Search through all available namespaces and any scoped enums within them
    7400              :    and generate a suggestion and/or a deferred diagnostic that lists possible
    7401              :    candidate(s).
    7402              : 
    7403              :    If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
    7404              :    look for near-matches and suggest the best near-match, if there is one.
    7405              : 
    7406              :    If nothing is found, then an empty name_hint is returned.  */
    7407              : 
    7408              : name_hint
    7409         1820 : suggest_alternatives_for (location_t location, tree name,
    7410              :                           bool suggest_misspellings)
    7411              : {
    7412              :   /* First, search for exact matches in other namespaces.  */
    7413         1820 :   namespace_hints ns_hints (location, name);
    7414         1820 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    7415              : 
    7416              :   /* Otherwise, try other approaches.  */
    7417         1820 :   if (!result)
    7418         1714 :     result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
    7419              : 
    7420         1820 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    7421         1820 : }
    7422              : 
    7423              : /* The second half of suggest_alternatives_for, for when no exact matches
    7424              :    were found in other namespaces.  */
    7425              : 
    7426              : static name_hint
    7427         1714 : suggest_alternatives_for_1 (location_t location, tree name,
    7428              :                             bool suggest_misspellings)
    7429              : {
    7430              :   /* No candidates were found in the available namespaces.  */
    7431              : 
    7432              :   /* If there's a "using namespace std;" active, and this
    7433              :      is one of the most common "std::" names, then it's probably a
    7434              :      missing #include.  */
    7435         1714 :   if (has_using_namespace_std_directive_p ())
    7436              :     {
    7437           16 :       name_hint hint = maybe_suggest_missing_std_header (location, name);
    7438           16 :       if (hint)
    7439              :         return hint;
    7440            0 :     }
    7441              : 
    7442              :   /* Look for exact matches for builtin defines that would have been
    7443              :      defined if the user had passed a command-line option (e.g. -fopenmp
    7444              :      for "_OPENMP").  */
    7445         1698 :   diagnostics::option_id option_id
    7446         1698 :     = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
    7447         1698 :   if (option_id.m_idx > 0)
    7448            6 :     return name_hint
    7449              :       (nullptr,
    7450            6 :        std::make_unique<suggest_missing_option> (location,
    7451           12 :                                                  IDENTIFIER_POINTER (name),
    7452            6 :                                                  option_id));
    7453              : 
    7454              :   /* Otherwise, consider misspellings.  */
    7455         1692 :   if (!suggest_misspellings)
    7456            0 :     return name_hint ();
    7457              : 
    7458         1692 :   return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
    7459              : }
    7460              : 
    7461              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    7462              :    name lookup failed.
    7463              : 
    7464              :    Search through all available namespaces and generate a suggestion and/or
    7465              :    a deferred diagnostic that lists possible candidate(s).
    7466              : 
    7467              :    This is similiar to suggest_alternatives_for, but doesn't fallback to
    7468              :    the other approaches used by that function.  */
    7469              : 
    7470              : name_hint
    7471           59 : suggest_alternatives_in_other_namespaces (location_t location, tree name)
    7472              : {
    7473           59 :   namespace_hints ns_hints (location, name);
    7474              : 
    7475           59 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    7476              : 
    7477           59 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    7478           59 : }
    7479              : 
    7480              : /* A well-known name within the C++ standard library, returned by
    7481              :    get_std_name_hint.
    7482              : 
    7483              :    The gperf-generated file contains the definition of the class
    7484              :    "std_name_hint_lookup" with a static member function which
    7485              :    returns the pointer to a structure "std_name_hint" which
    7486              :    is also defined in that file.  */
    7487              : 
    7488              : #include "std-name-hint.h"
    7489              : 
    7490              : /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
    7491              :    for some of the most common names within "std::".
    7492              :    Given non-NULL NAME, return the std_name_hint for it, or NULL.  */
    7493              : 
    7494              : static const std_name_hint *
    7495          179 : get_std_name_hint (const char *name)
    7496              : {
    7497          179 :   return std_name_hint_lookup::find(name, strlen(name));
    7498              : }
    7499              : 
    7500              : /* Describe DIALECT.  */
    7501              : 
    7502              : const char *
    7503         4611 : get_cxx_dialect_name (enum cxx_dialect dialect)
    7504              : {
    7505         4611 :   switch (dialect)
    7506              :     {
    7507            0 :     default:
    7508            0 :       gcc_unreachable ();
    7509              :     case cxx98:
    7510              :       return "C++98";
    7511            4 :     case cxx11:
    7512            4 :       return "C++11";
    7513            4 :     case cxx14:
    7514            4 :       return "C++14";
    7515         1454 :     case cxx17:
    7516         1454 :       return "C++17";
    7517         1576 :     case cxx20:
    7518         1576 :       return "C++20";
    7519           37 :     case cxx23:
    7520           37 :       return "C++23";
    7521         1536 :     case cxx26:
    7522         1536 :       return "C++26";
    7523              :     }
    7524              : }
    7525              : 
    7526              : /* Subclass of deferred_diagnostic for use for names in the "std" namespace
    7527              :    that weren't recognized, but for which we know which header it ought to be
    7528              :    in.
    7529              : 
    7530              :    Emit a note either suggesting the header to be included, or noting that
    7531              :    the current dialect is too early for the given name.  */
    7532              : 
    7533              : class missing_std_header : public deferred_diagnostic
    7534              : {
    7535              :  public:
    7536          152 :   missing_std_header (location_t loc,
    7537              :                       const char *name_str,
    7538              :                       const std_name_hint *header_hint)
    7539          152 :   : deferred_diagnostic (loc),
    7540          152 :     m_name_str (name_str),
    7541          152 :     m_header_hint (header_hint)
    7542              :   {}
    7543          304 :   ~missing_std_header ()
    7544          152 :   {
    7545          152 :     gcc_rich_location richloc (get_location ());
    7546          152 :     if (cxx_dialect >= m_header_hint->min_dialect)
    7547              :       {
    7548          144 :         const char *header = m_header_hint->header;
    7549          144 :         maybe_add_include_fixit (&richloc, header, true);
    7550          144 :         inform (&richloc,
    7551              :                 "%<std::%s%> is defined in header %qs;"
    7552              :                 " this is probably fixable by adding %<#include %s%>",
    7553              :                 m_name_str, header, header);
    7554              :       }
    7555              :     else
    7556            8 :       inform (&richloc,
    7557              :               "%<std::%s%> is only available from %s onwards",
    7558              :               m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
    7559          304 :   }
    7560              : 
    7561              : private:
    7562              :   const char *m_name_str;
    7563              :   const std_name_hint *m_header_hint;
    7564              : };
    7565              : 
    7566              : /* Attempt to generate a name_hint that suggests pertinent header files
    7567              :    for NAME at LOCATION, for common names within the "std" namespace,
    7568              :    or an empty name_hint if this isn't applicable.  */
    7569              : 
    7570              : static name_hint
    7571          179 : maybe_suggest_missing_std_header (location_t location, tree name)
    7572              : {
    7573          179 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    7574              : 
    7575          179 :   const char *name_str = IDENTIFIER_POINTER (name);
    7576          179 :   const std_name_hint *header_hint = get_std_name_hint (name_str);
    7577          179 :   if (!header_hint)
    7578           27 :     return name_hint ();
    7579              : 
    7580          152 :   return name_hint (nullptr,
    7581          152 :                     std::make_unique<missing_std_header> (location, name_str,
    7582          152 :                                                           header_hint));
    7583              : }
    7584              : 
    7585              : /* Attempt to generate a name_hint that suggests a missing header file
    7586              :    for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
    7587              :    applicable.  */
    7588              : 
    7589              : name_hint
    7590          487 : maybe_suggest_missing_header (location_t location, tree name, tree scope)
    7591              : {
    7592          487 :   if (scope == NULL_TREE)
    7593            0 :     return name_hint ();
    7594          487 :   if (TREE_CODE (scope) != NAMESPACE_DECL)
    7595           33 :     return name_hint ();
    7596              :   /* We only offer suggestions for the "std" namespace.  */
    7597          454 :   if (scope != std_node)
    7598          291 :     return name_hint ();
    7599          163 :   return maybe_suggest_missing_std_header (location, name);
    7600              : }
    7601              : 
    7602              : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
    7603              :    lookup failed within the explicitly provided SCOPE.
    7604              : 
    7605              :    Suggest the best meaningful candidates (if any), otherwise
    7606              :    an empty name_hint is returned.  */
    7607              : 
    7608              : name_hint
    7609          320 : suggest_alternative_in_explicit_scope (location_t location, tree name,
    7610              :                                        tree scope)
    7611              : {
    7612              :   /* Something went very wrong; don't suggest anything.  */
    7613          320 :   if (name == error_mark_node)
    7614            0 :     return name_hint ();
    7615              : 
    7616          320 :   if (TREE_CODE (scope) != NAMESPACE_DECL)
    7617           25 :     return name_hint ();
    7618              : 
    7619              :   /* Resolve any namespace aliases.  */
    7620          295 :   scope = ORIGINAL_NAMESPACE (scope);
    7621              : 
    7622          295 :   name_hint hint = maybe_suggest_missing_header (location, name, scope);
    7623          295 :   if (hint)
    7624          121 :     return hint;
    7625              : 
    7626          174 :   cp_binding_level *level = NAMESPACE_LEVEL (scope);
    7627              : 
    7628          174 :   best_match <tree, const char *> bm (name);
    7629          174 :   consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
    7630              : 
    7631              :   /* See if we have a good suggesion for the user.  */
    7632          174 :   const char *fuzzy_name = bm.get_best_meaningful_candidate ();
    7633          174 :   if (fuzzy_name)
    7634           43 :     return name_hint (fuzzy_name, NULL);
    7635              : 
    7636          131 :   return name_hint ();
    7637          295 : }
    7638              : 
    7639              : /* Given NAME, look within SCOPED_ENUM for possible spell-correction
    7640              :    candidates.  */
    7641              : 
    7642              : name_hint
    7643           15 : suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
    7644              : {
    7645           15 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    7646              : 
    7647           15 :   best_match <tree, const char *> bm (name);
    7648           48 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    7649              :     {
    7650           33 :       tree id = TREE_PURPOSE (iter);
    7651           33 :       bm.consider (IDENTIFIER_POINTER (id));
    7652              :     }
    7653           15 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    7654              : }
    7655              : 
    7656              : /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
    7657              :    or a class TYPE).
    7658              : 
    7659              :    WANT as for lookup_name_1.
    7660              : 
    7661              :    Returns a DECL (or OVERLOAD, or BASELINK) representing the
    7662              :    declaration found.  If no suitable declaration can be found,
    7663              :    ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
    7664              :    neither a class-type nor a namespace a diagnostic is issued.  */
    7665              : 
    7666              : tree
    7667    561072329 : lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
    7668              : {
    7669    561072329 :   tree t = NULL_TREE;
    7670              : 
    7671    561072329 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    7672              :     {
    7673    412116943 :       name_lookup lookup (name, want);
    7674              : 
    7675    412116943 :       if (qualified_namespace_lookup (scope, &lookup))
    7676              :         {
    7677    398469708 :           t = lookup.value;
    7678              : 
    7679              :           /* If we have a known type overload, pull it out.  This can happen
    7680              :              for using decls.  */
    7681    398469708 :           if (TREE_CODE (t) == OVERLOAD
    7682    246149945 :               && TREE_TYPE (t) != unknown_type_node
    7683    399515301 :               && LIKELY (!cp_preserve_using_decl))
    7684      1045592 :             t = OVL_FUNCTION (t);
    7685              :         }
    7686    412116943 :     }
    7687    148955386 :   else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
    7688     14214427 :     t = lookup_enumerator (scope, name);
    7689    134740959 :   else if (is_class_type (scope, complain))
    7690    134719447 :     t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
    7691              :                        tf_warning_or_error);
    7692              : 
    7693    561050763 :   if (!t)
    7694     13865594 :     return error_mark_node;
    7695              :   return t;
    7696              : }
    7697              : 
    7698              : /* Wrapper for the above that takes a string argument.  The function name is
    7699              :    not at the beginning of the line to keep this wrapper out of etags.  */
    7700              : 
    7701       143972 : tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
    7702              : {
    7703       143972 :   return lookup_qualified_name (t, get_identifier (p), w, c);
    7704              : }
    7705              : 
    7706              : /* [namespace.qual]
    7707              :    Accepts the NAME to lookup and its qualifying SCOPE.
    7708              :    Returns the name/type pair found into the cxx_binding *RESULT,
    7709              :    or false on error.  */
    7710              : 
    7711              : static bool
    7712    418658119 : qualified_namespace_lookup (tree scope, name_lookup *lookup)
    7713              : {
    7714    418658119 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7715    418658119 :   query_oracle (lookup->name);
    7716    418658119 :   bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
    7717    837316238 :   return found;
    7718    418658119 : }
    7719              : 
    7720              : /* If DECL is suitably visible to the user, consider its name for
    7721              :    spelling correction.  */
    7722              : 
    7723              : static void
    7724         2130 : consider_decl (tree decl,  best_match <tree, const char *> &bm,
    7725              :                bool consider_impl_names)
    7726              : {
    7727              :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    7728              :      within range for).  */
    7729         2130 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7730              :     return;
    7731              : 
    7732         2047 :   tree suggestion = DECL_NAME (decl);
    7733         2047 :   if (!suggestion)
    7734              :     return;
    7735              : 
    7736              :   /* Don't suggest names that are for anonymous aggregate types, as
    7737              :      they are an implementation detail generated by the compiler.  */
    7738         1940 :   if (IDENTIFIER_ANON_P (suggestion))
    7739              :     return;
    7740              : 
    7741         1843 :   const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
    7742              : 
    7743              :   /* Ignore internal names with spaces in them.  */
    7744         1843 :   if (strchr (suggestion_str, ' '))
    7745              :     return;
    7746              : 
    7747              :   /* Don't suggest names that are reserved for use by the
    7748              :      implementation, unless NAME began with an underscore.  */
    7749         1843 :   if (!consider_impl_names
    7750         1843 :       && name_reserved_for_implementation_p (suggestion_str))
    7751              :     return;
    7752              : 
    7753         1813 :   bm.consider (suggestion_str);
    7754              : }
    7755              : 
    7756              : /* If DECL is suitably visible to the user, add its name to VEC and
    7757              :    return true.  Otherwise return false.  */
    7758              : 
    7759              : static bool
    7760      3388915 : maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
    7761              : {
    7762              :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    7763              :      within range for).  */
    7764      3388915 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7765              :     return false;
    7766              : 
    7767      3388126 :   tree suggestion = DECL_NAME (decl);
    7768      3388126 :   if (!suggestion)
    7769              :     return false;
    7770              : 
    7771              :   /* Don't suggest names that are for anonymous aggregate types, as
    7772              :      they are an implementation detail generated by the compiler.  */
    7773      3388126 :   if (IDENTIFIER_ANON_P (suggestion))
    7774              :     return false;
    7775              : 
    7776      3388126 :   vec.safe_push (suggestion);
    7777              : 
    7778      3388126 :   return true;
    7779              : }
    7780              : 
    7781              : /* Examing the namespace binding BINDING, and add at most one instance
    7782              :    of the name, if it contains a visible entity of interest.  Return
    7783              :    true if we added something.  */
    7784              : 
    7785              : bool
    7786      5863494 : maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
    7787              :                               lookup_name_fuzzy_kind kind)
    7788              : {
    7789      5863494 :   tree value = NULL_TREE;
    7790              : 
    7791      5863494 :   if (STAT_HACK_P (binding))
    7792              :     {
    7793          450 :       if (!STAT_TYPE_HIDDEN_P (binding)
    7794          450 :           && STAT_TYPE (binding))
    7795              :         {
    7796          257 :           if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
    7797              :             return true;
    7798              :         }
    7799          193 :       else if (!STAT_DECL_HIDDEN_P (binding))
    7800          118 :         value = STAT_DECL (binding);
    7801              :     }
    7802              :   else
    7803              :     value = binding;
    7804              : 
    7805      5863237 :   value = ovl_skip_hidden (value);
    7806      5863237 :   if (value)
    7807              :     {
    7808      5002696 :       value = OVL_FIRST (value);
    7809      5002696 :       if (kind != FUZZY_LOOKUP_TYPENAME
    7810      5002696 :           || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
    7811      3388658 :         if (maybe_add_fuzzy_decl (vec, value))
    7812              :           return true;
    7813              :     }
    7814              : 
    7815              :   /* Nothing found.  */
    7816              :   return false;
    7817              : }
    7818              : 
    7819              : /* Helper function for lookup_name_fuzzy.
    7820              :    Traverse binding level LVL, looking for good name matches for NAME
    7821              :    (and BM).  */
    7822              : static void
    7823         7352 : consider_binding_level (tree name, best_match <tree, const char *> &bm,
    7824              :                         cp_binding_level *lvl, bool look_within_fields,
    7825              :                         enum lookup_name_fuzzy_kind kind)
    7826              : {
    7827         7352 :   if (look_within_fields)
    7828         1062 :     if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
    7829              :       {
    7830          429 :         tree type = lvl->this_entity;
    7831          429 :         bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
    7832          429 :         tree best_matching_field
    7833          429 :           = lookup_member_fuzzy (type, name, want_type_p);
    7834          429 :         if (best_matching_field)
    7835           10 :           bm.consider (IDENTIFIER_POINTER (best_matching_field));
    7836              :       }
    7837              : 
    7838              :   /* Only suggest names reserved for the implementation if NAME begins
    7839              :      with an underscore.  */
    7840         7352 :   bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
    7841              : 
    7842         7352 :   if (lvl->kind != sk_namespace)
    7843         7397 :     for (tree t = lvl->names; t; t = TREE_CHAIN (t))
    7844              :       {
    7845         2765 :         tree d = t;
    7846              : 
    7847              :         /* OVERLOADs or decls from using declaration are wrapped into
    7848              :            TREE_LIST.  */
    7849         2765 :         if (TREE_CODE (d) == TREE_LIST)
    7850           27 :           d = OVL_FIRST (TREE_VALUE (d));
    7851              : 
    7852              :         /* Don't use bindings from implicitly declared functions,
    7853              :            as they were likely misspellings themselves.  */
    7854         2765 :         if (TREE_TYPE (d) == error_mark_node)
    7855          317 :           continue;
    7856              : 
    7857              :         /* If we want a typename, ignore non-types.  */
    7858         2766 :         if (kind == FUZZY_LOOKUP_TYPENAME
    7859         2448 :             && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
    7860          318 :           continue;
    7861              : 
    7862         2130 :         consider_decl (d, bm, consider_implementation_names);
    7863              :       }
    7864              :   else
    7865              :     {
    7866              :       /* We need to iterate over the namespace hash table, in order to
    7867              :          not mention hidden entities.  But hash table iteration is
    7868              :          (essentially) unpredictable, our correction-distance measure
    7869              :          is very granular, and we pick the first of equal distances.
    7870              :          Hence, we need to call the distance-measurer in a predictable
    7871              :          order.  So, iterate over the namespace hash, inserting
    7872              :          visible names into a vector.  Then sort the vector.  Then
    7873              :          determine spelling distance.  */
    7874              : 
    7875         2720 :       tree ns = lvl->this_entity;
    7876         2720 :       auto_vec<tree> vec;
    7877              : 
    7878         2720 :       hash_table<named_decl_hash>::iterator end
    7879         2720 :         (DECL_NAMESPACE_BINDINGS (ns)->end ());
    7880      5866304 :       for (hash_table<named_decl_hash>::iterator iter
    7881     11732608 :              (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
    7882              :         {
    7883      5863584 :           tree binding = *iter;
    7884              : 
    7885      5863584 :           if (TREE_CODE (binding) == BINDING_VECTOR)
    7886              :             {
    7887          351 :               bitmap imports = get_import_bitmap ();
    7888          351 :               binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    7889              : 
    7890          351 :               if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    7891           30 :                 if (maybe_add_fuzzy_binding (vec, bind, kind))
    7892           18 :                   continue;
    7893              : 
    7894              :               /* Scan the imported bindings.  */
    7895          333 :               unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    7896          333 :               if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    7897              :                 {
    7898          333 :                   ix--;
    7899          333 :                   cluster++;
    7900              :                 }
    7901              : 
    7902          675 :               for (; ix--; cluster++)
    7903          828 :                 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
    7904              :                      jx++)
    7905              :                   {
    7906              :                     /* Are we importing this module?  */
    7907          678 :                     if (unsigned base = cluster->indices[jx].base)
    7908          303 :                       if (unsigned span = cluster->indices[jx].span)
    7909          306 :                         do
    7910          306 :                           if (bitmap_bit_p (imports, base))
    7911          267 :                             goto found;
    7912           39 :                         while (++base, --span);
    7913          411 :                     continue;
    7914              : 
    7915          267 :                   found:;
    7916              :                     /* Is it loaded?  */
    7917          267 :                     if (cluster->slots[jx].is_lazy ())
    7918              :                       /* Let's not read in everything on the first
    7919              :                          spello! **/
    7920           36 :                       continue;
    7921          231 :                     if (tree bind = cluster->slots[jx])
    7922          231 :                       if (maybe_add_fuzzy_binding (vec, bind, kind))
    7923              :                         break;
    7924          411 :                   }
    7925              :             }
    7926              :           else
    7927      5863233 :             maybe_add_fuzzy_binding (vec, binding, kind);
    7928              :         }
    7929              : 
    7930    178323847 :       vec.qsort ([] (const void *a_, const void *b_)
    7931              :                  {
    7932              :                    return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
    7933              :                                   IDENTIFIER_POINTER (*(const tree *)b_));
    7934              :                  });
    7935              : 
    7936              :       /* Examine longest to shortest.  */
    7937      3393566 :       for (unsigned ix = vec.length (); ix--;)
    7938              :         {
    7939      3388126 :           const char *str = IDENTIFIER_POINTER (vec[ix]);
    7940              : 
    7941              :           /* Ignore internal names with spaces in them.  */
    7942      3388126 :           if (strchr (str, ' '))
    7943        69914 :             continue;
    7944              : 
    7945              :           /* Don't suggest names that are reserved for use by the
    7946              :              implementation, unless NAME began with an underscore.  */
    7947      6322451 :           if (!consider_implementation_names
    7948      3318212 :               && name_reserved_for_implementation_p (str))
    7949      3004239 :             continue;
    7950              : 
    7951       313973 :           bm.consider (str);
    7952              :         }
    7953         2720 :     }
    7954         7352 : }
    7955              : 
    7956              : /* Subclass of deferred_diagnostic.  Notify the user that the
    7957              :    given macro was used before it was defined.
    7958              :    This can be done in the C++ frontend since tokenization happens
    7959              :    upfront.  */
    7960              : 
    7961              : class macro_use_before_def : public deferred_diagnostic
    7962              : {
    7963              :  public:
    7964              :   /* Factory function.  Return a new macro_use_before_def instance if
    7965              :      appropriate, or return NULL. */
    7966              :   static std::unique_ptr<macro_use_before_def>
    7967           38 :   maybe_make (location_t use_loc, cpp_hashnode *macro)
    7968              :   {
    7969           38 :     location_t def_loc = cpp_macro_definition_location (macro);
    7970           38 :     if (def_loc == UNKNOWN_LOCATION)
    7971            0 :       return nullptr;
    7972              : 
    7973              :     /* We only want to issue a note if the macro was used *before* it was
    7974              :        defined.
    7975              :        We don't want to issue a note for cases where a macro was incorrectly
    7976              :        used, leaving it unexpanded (e.g. by using the wrong argument
    7977              :        count).  */
    7978           38 :     if (!linemap_location_before_p (line_table, use_loc, def_loc))
    7979            0 :       return nullptr;
    7980              : 
    7981           38 :     return std::make_unique<macro_use_before_def> (use_loc, macro);
    7982              :   }
    7983              : 
    7984              :   /* Ctor.  LOC is the location of the usage.  MACRO is the
    7985              :      macro that was used.  */
    7986           38 :   macro_use_before_def (location_t loc, cpp_hashnode *macro)
    7987           38 :   : deferred_diagnostic (loc), m_macro (macro)
    7988              :   {
    7989           38 :     gcc_assert (macro);
    7990           38 :   }
    7991              : 
    7992           76 :   ~macro_use_before_def ()
    7993           38 :   {
    7994           38 :     if (is_suppressed_p ())
    7995            0 :       return;
    7996              : 
    7997           38 :     inform (get_location (), "the macro %qs had not yet been defined",
    7998           38 :             (const char *)m_macro->ident.str);
    7999           76 :     inform (cpp_macro_definition_location (m_macro),
    8000              :             "it was later defined here");
    8001           76 :   }
    8002              : 
    8003              :  private:
    8004              :   cpp_hashnode *m_macro;
    8005              : };
    8006              : 
    8007              : /* Determine if it can ever make sense to offer RID as a suggestion for
    8008              :    a misspelling.
    8009              : 
    8010              :    Subroutine of lookup_name_fuzzy.  */
    8011              : 
    8012              : static bool
    8013       475600 : suggest_rid_p  (enum rid rid)
    8014              : {
    8015       475600 :   switch (rid)
    8016              :     {
    8017              :     /* Support suggesting function-like keywords.  */
    8018              :     case RID_STATIC_ASSERT:
    8019              :       return true;
    8020              : 
    8021       471500 :     default:
    8022              :       /* Support suggesting the various decl-specifier words, to handle
    8023              :          e.g. "singed" vs "signed" typos.  */
    8024       471500 :       if (cp_keyword_starts_decl_specifier_p (rid))
    8025              :         return true;
    8026              : 
    8027              :       /* Otherwise, don't offer it.  This avoids suggesting e.g. "if"
    8028              :          and "do" for short misspellings, which are likely to lead to
    8029              :          nonsensical results.  */
    8030              :       return false;
    8031              :     }
    8032              : }
    8033              : 
    8034              : /* Search for near-matches for NAME within the current bindings, and within
    8035              :    macro names, returning the best match as a const char *, or NULL if
    8036              :    no reasonable match is found.
    8037              : 
    8038              :    Use LOC for any deferred diagnostics.  */
    8039              : 
    8040              : name_hint
    8041         2364 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
    8042              : {
    8043         2364 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    8044              : 
    8045              :   /* Look up function-like macros first; maybe misusing them. */
    8046         4728 :   auto cpp_node = cpp_lookup (parse_in,
    8047         2364 :                               (const unsigned char*)IDENTIFIER_POINTER (name),
    8048         2364 :                               IDENTIFIER_LENGTH (name));
    8049         2364 :   if (cpp_node && cpp_fun_like_macro_p (cpp_node))
    8050           27 :     return name_hint
    8051              :       (nullptr,
    8052           27 :        std::make_unique<macro_like_function_used> (loc,
    8053           54 :                                                    IDENTIFIER_POINTER (name)));
    8054              : 
    8055              :   /* Then, try some well-known names in the C++ standard library, in case
    8056              :      the user forgot a #include.  */
    8057         2337 :   const char *header_hint
    8058         2337 :     = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
    8059         2337 :   if (header_hint)
    8060          249 :     return name_hint
    8061              :       (nullptr,
    8062          249 :        std::make_unique<suggest_missing_header> (loc,
    8063          498 :                                                  IDENTIFIER_POINTER (name),
    8064          249 :                                                  header_hint));
    8065              : 
    8066         2088 :   best_match <tree, const char *> bm (name);
    8067              : 
    8068         2088 :   cp_binding_level *lvl;
    8069         3150 :   for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
    8070         1062 :     consider_binding_level (name, bm, lvl, true, kind);
    8071              : 
    8072        10292 :   for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
    8073         6116 :     consider_binding_level (name, bm, lvl, false, kind);
    8074              : 
    8075              :   /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
    8076              :      as:
    8077              :        x = SOME_OTHER_MACRO (y);
    8078              :      then "SOME_OTHER_MACRO" will survive to the frontend and show up
    8079              :      as a misspelled identifier.
    8080              : 
    8081              :      Use the best distance so far so that a candidate is only set if
    8082              :      a macro is better than anything so far.  This allows early rejection
    8083              :      (without calculating the edit distance) of macro names that must have
    8084              :      distance >= bm.get_best_distance (), and means that we only get a
    8085              :      non-NULL result for best_macro_match if it's better than any of
    8086              :      the identifiers already checked.  */
    8087         2088 :   best_macro_match bmm (name, bm.get_best_distance (), parse_in);
    8088         2088 :   cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
    8089              :   /* If a macro is the closest so far to NAME, consider it.  */
    8090         2088 :   if (best_macro)
    8091           27 :     bm.consider ((const char *)best_macro->ident.str);
    8092         2061 :   else if (bmm.get_best_distance () == 0)
    8093              :     {
    8094              :       /* If we have an exact match for a macro name, then either the
    8095              :          macro was used with the wrong argument count, or the macro
    8096              :          has been used before it was defined.  */
    8097           74 :       if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
    8098           41 :         if (cpp_user_macro_p (macro))
    8099           38 :           return name_hint (NULL,
    8100           38 :                             macro_use_before_def::maybe_make (loc, macro));
    8101              :     }
    8102              : 
    8103              :   /* Try the "starts_decl_specifier_p" keywords to detect
    8104              :      "singed" vs "signed" typos.  */
    8105       477650 :   for (unsigned i = 0; i < num_c_common_reswords; i++)
    8106              :     {
    8107       475600 :       const c_common_resword *resword = &c_common_reswords[i];
    8108              : 
    8109       475600 :       if (!suggest_rid_p (resword->rid))
    8110       350550 :         continue;
    8111              : 
    8112       125050 :       tree resword_identifier = ridpointers [resword->rid];
    8113       125050 :       if (!resword_identifier)
    8114            0 :         continue;
    8115       125050 :       gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
    8116              : 
    8117              :       /* Only consider reserved words that survived the
    8118              :          filtering in init_reswords (e.g. for -std).  */
    8119       125050 :       if (!IDENTIFIER_KEYWORD_P (resword_identifier))
    8120        11638 :         continue;
    8121              : 
    8122       113412 :       bm.consider (IDENTIFIER_POINTER (resword_identifier));
    8123              :     }
    8124              : 
    8125         2050 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    8126              : }
    8127              : 
    8128              : /* Subroutine of outer_binding.
    8129              : 
    8130              :    Returns TRUE if BINDING is a binding to a template parameter of
    8131              :    SCOPE.  In that case SCOPE is the scope of a primary template
    8132              :    parameter -- in the sense of G++, i.e, a template that has its own
    8133              :    template header.
    8134              : 
    8135              :    Returns FALSE otherwise.  */
    8136              : 
    8137              : static bool
    8138    425178353 : binding_to_template_parms_of_scope_p (cxx_binding *binding,
    8139              :                                       cp_binding_level *scope)
    8140              : {
    8141    425178353 :   tree binding_value, tmpl, tinfo;
    8142    425178353 :   int level;
    8143              : 
    8144    425178353 :   if (!binding || !scope || !scope->this_entity)
    8145              :     return false;
    8146              : 
    8147    244750937 :   binding_value = binding->value ?  binding->value : binding->type;
    8148    244750937 :   tinfo = get_template_info (scope->this_entity);
    8149              : 
    8150              :   /* BINDING_VALUE must be a template parm.  */
    8151    244750937 :   if (binding_value == NULL_TREE
    8152    244750937 :       || (!DECL_P (binding_value)
    8153    244750937 :           || !DECL_TEMPLATE_PARM_P (binding_value)))
    8154              :     return false;
    8155              : 
    8156              :   /*  The level of BINDING_VALUE.  */
    8157    489501874 :   level =
    8158    244750937 :     template_type_parameter_p (binding_value)
    8159    244750937 :     ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
    8160              :                          (TREE_TYPE (binding_value)))
    8161     81991543 :     : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
    8162              : 
    8163              :   /* The template of the current scope, iff said scope is a primary
    8164              :      template.  */
    8165    244750937 :   tmpl = (tinfo
    8166    208579693 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
    8167    208579690 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
    8168    244750937 :           ? TI_TEMPLATE (tinfo)
    8169              :           : NULL_TREE);
    8170              : 
    8171              :   /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
    8172              :      then BINDING_VALUE is a parameter of TMPL.  */
    8173    180250506 :   return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
    8174              : }
    8175              : 
    8176              : /* Return the innermost non-namespace binding for NAME from a scope
    8177              :    containing BINDING, or, if BINDING is NULL, the current scope.
    8178              :    Please note that for a given template, the template parameters are
    8179              :    considered to be in the scope containing the current scope.
    8180              :    If CLASS_P is false, then class bindings are ignored.  */
    8181              : 
    8182              : cxx_binding *
    8183   6344654226 : outer_binding (tree name,
    8184              :                cxx_binding *binding,
    8185              :                bool class_p)
    8186              : {
    8187   6344654226 :   cxx_binding *outer;
    8188   6344654226 :   cp_binding_level *scope;
    8189   6344654226 :   cp_binding_level *outer_scope;
    8190              : 
    8191   6344654226 :   if (binding)
    8192              :     {
    8193      7786132 :       scope = binding->scope->level_chain;
    8194      7786132 :       outer = binding->previous;
    8195              :     }
    8196              :   else
    8197              :     {
    8198   6336868094 :       scope = current_binding_level;
    8199   6336868094 :       outer = IDENTIFIER_BINDING (name);
    8200              :     }
    8201   6344654226 :   outer_scope = outer ? outer->scope : NULL;
    8202              : 
    8203              :   /* Because we create class bindings lazily, we might be missing a
    8204              :      class binding for NAME.  If there are any class binding levels
    8205              :      between the LAST_BINDING_LEVEL and the scope in which OUTER was
    8206              :      declared, we must lookup NAME in those class scopes.  */
    8207   6344654226 :   if (class_p)
    8208  15903514644 :     while (scope && scope != outer_scope && scope->kind != sk_namespace)
    8209              :       {
    8210  11758045317 :         if (scope->kind == sk_class)
    8211              :           {
    8212   1762568955 :             cxx_binding *class_binding;
    8213              : 
    8214   1762568955 :             class_binding = get_class_binding (name, scope);
    8215   1762568955 :             if (class_binding)
    8216              :               {
    8217              :                 /* Thread this new class-scope binding onto the
    8218              :                    IDENTIFIER_BINDING list so that future lookups
    8219              :                    find it quickly.  */
    8220    139702335 :                 if (BASELINK_P (class_binding->value))
    8221              :                   /* Don't put a BASELINK in IDENTIFIER_BINDING.  */
    8222     39273102 :                   class_binding->value
    8223     39273102 :                     = BASELINK_FUNCTIONS (class_binding->value);
    8224    139702335 :                 class_binding->previous = outer;
    8225    139702335 :                 if (binding)
    8226            3 :                   binding->previous = class_binding;
    8227              :                 else
    8228    139702332 :                   IDENTIFIER_BINDING (name) = class_binding;
    8229    139702335 :                 return class_binding;
    8230              :               }
    8231              :           }
    8232              :         /* If we are in a member template, the template parms of the member
    8233              :            template are considered to be inside the scope of the containing
    8234              :            class, but within G++ the class bindings are all pushed between the
    8235              :            template parms and the function body.  So if the outer binding is
    8236              :            a template parm for the current scope, return it now rather than
    8237              :            look for a class binding.  */
    8238   5350565541 :         if (outer_scope && outer_scope->kind == sk_template_parms
    8239  12043521335 :             && binding_to_template_parms_of_scope_p (outer, scope))
    8240              :           return outer;
    8241              : 
    8242  11452770644 :         scope = scope->level_chain;
    8243              :       }
    8244              : 
    8245              :   return outer;
    8246              : }
    8247              : 
    8248              : /* Return the innermost block-scope or class-scope value binding for
    8249              :    NAME, or NULL_TREE if there is no such binding.  */
    8250              : 
    8251              : tree
    8252    915566254 : innermost_non_namespace_value (tree name)
    8253              : {
    8254    915566254 :   cxx_binding *binding;
    8255    915566254 :   binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
    8256    915566254 :   return binding ? binding->value : NULL_TREE;
    8257              : }
    8258              : 
    8259              : /* True iff current_binding_level is within the potential scope of local
    8260              :    variable DECL. */
    8261              : 
    8262              : bool
    8263          193 : decl_in_scope_p (tree decl)
    8264              : {
    8265          193 :   gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
    8266              : 
    8267          193 :   tree name = DECL_NAME (decl);
    8268              : 
    8269          193 :   for (cxx_binding *iter = NULL;
    8270          203 :        (iter = outer_binding (name, iter, /*class_p=*/false)); )
    8271              :     {
    8272           73 :       if (!LOCAL_BINDING_P (iter))
    8273              :         return false;
    8274           73 :       if (iter->value == decl)
    8275              :         return true;
    8276              :     }
    8277              : 
    8278              :   return false;
    8279              : }
    8280              : 
    8281              : /* Look up NAME in the current binding level and its superiors in the
    8282              :    namespace of variables, functions and typedefs.  Return a ..._DECL
    8283              :    node of some kind representing its definition if there is only one
    8284              :    such declaration, or return a TREE_LIST with all the overloaded
    8285              :    definitions if there are many, or return NULL_TREE if it is undefined.
    8286              :    Hidden name, either friend declaration or built-in function, are
    8287              :    not ignored.
    8288              : 
    8289              :    WHERE controls which scopes are considered.  It is a bit mask of
    8290              :    LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
    8291              :    (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
    8292              :    scopes).  It is an error for no bits to be set.  These scopes are
    8293              :    searched from innermost to outermost.
    8294              : 
    8295              :    WANT controls what kind of entity we'd happy with.
    8296              :    LOOK_want::NORMAL for normal lookup (implicit typedefs can be
    8297              :    hidden).  LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
    8298              :    for only NAMESPACE_DECLS.  These two can be bit-ored to find
    8299              :    namespace or type.
    8300              : 
    8301              :    WANT can also have LOOK_want::HIDDEN_FRIEND or
    8302              :    LOOK_want::HIDDEN_LAMBDa added to it.  */
    8303              : 
    8304              : tree
    8305   4826978382 : lookup_name (tree name, LOOK_where where, LOOK_want want)
    8306              : {
    8307   4826978382 :   tree val = NULL_TREE;
    8308              : 
    8309   4826978382 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8310              : 
    8311   4826978382 :   gcc_checking_assert (unsigned (where) != 0);
    8312              :   /* If we're looking for hidden lambda things, we shouldn't be
    8313              :      looking in namespace scope.  */
    8314   4826978382 :   gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
    8315              :                        || !bool (where & LOOK_where::NAMESPACE));
    8316   4826978382 :   query_oracle (name);
    8317              : 
    8318              :   /* Conversion operators are handled specially because ordinary
    8319              :      unqualified name lookup will not find template conversion
    8320              :      operators.  */
    8321   4826978382 :   if (IDENTIFIER_CONV_OP_P (name))
    8322              :     {
    8323       987172 :       cp_binding_level *level;
    8324              : 
    8325       987172 :       for (level = current_binding_level;
    8326      2747195 :            level && level->kind != sk_namespace;
    8327      1760023 :            level = level->level_chain)
    8328              :         {
    8329      1932390 :           tree class_type;
    8330      1932390 :           tree operators;
    8331              : 
    8332              :           /* A conversion operator can only be declared in a class
    8333              :              scope.  */
    8334      1932390 :           if (level->kind != sk_class)
    8335       813529 :             continue;
    8336              : 
    8337              :           /* Lookup the conversion operator in the class.  */
    8338      1118861 :           class_type = level->this_entity;
    8339      1118861 :           operators = lookup_fnfields (class_type, name, /*protect=*/0,
    8340              :                                        tf_warning_or_error);
    8341      1118861 :           if (operators)
    8342              :             return operators;
    8343              :         }
    8344              : 
    8345              :       return NULL_TREE;
    8346              :     }
    8347              : 
    8348              :   /* First, look in non-namespace scopes.  */
    8349              : 
    8350   4825991210 :   if (current_class_type == NULL_TREE)
    8351              :     /* Maybe avoid searching the binding stack at all.  */
    8352   1785282559 :     where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
    8353              : 
    8354   4825991210 :   if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
    8355              :     for (cxx_binding *iter = nullptr;
    8356   4832984272 :          (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
    8357              :       {
    8358              :         /* Skip entities we don't want.  */
    8359   4348351956 :         if (!bool (where & (LOCAL_BINDING_P (iter)
    8360              :                             ? LOOK_where::BLOCK : LOOK_where::CLASS)))
    8361        15499 :           continue;
    8362              : 
    8363              :         /* If this is the kind of thing we're looking for, we're done.  */
    8364   3510378813 :         if (iter->value)
    8365              :           {
    8366   3510378813 :             tree binding = NULL_TREE;
    8367              : 
    8368   3448052430 :             if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
    8369   3510378748 :                 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
    8370   3509181088 :                     || !is_lambda_ignored_entity (iter->value))
    8371   7013068413 :                 && qualify_lookup (iter->value, want))
    8372   3502608301 :               binding = iter->value;
    8373      7770512 :             else if (bool (want & LOOK_want::TYPE)
    8374        81275 :                      && !HIDDEN_TYPE_BINDING_P (iter)
    8375      7851772 :                      && iter->type)
    8376   3510378813 :               binding = iter->type;
    8377              : 
    8378   3510378813 :             if (binding)
    8379              :               {
    8380   3502608371 :                 val = strip_using_decl (binding);
    8381   3502608371 :                 break;
    8382              :               }
    8383              :           }
    8384              :       }
    8385              : 
    8386              :   /* Now lookup in namespace scopes.  */
    8387   4825991210 :   if (!val && bool (where & LOOK_where::NAMESPACE))
    8388              :     {
    8389   1323382623 :       name_lookup lookup (name, want);
    8390   1323382623 :       if (lookup.search_unqualified
    8391   1323382623 :           (current_decl_namespace (), current_binding_level))
    8392   1026598065 :         val = lookup.value;
    8393   1323382623 :     }
    8394              : 
    8395              :   /* If we have a known type overload, pull it out.  This can happen
    8396              :      for both using decls and unhidden functions.  */
    8397   4825991210 :   if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
    8398      2735067 :     val = OVL_FUNCTION (val);
    8399              : 
    8400              :   return val;
    8401   4826978382 : }
    8402              : 
    8403              : tree
    8404    305440155 : lookup_name (tree name)
    8405              : {
    8406    305440155 :   return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
    8407              : }
    8408              : 
    8409              : /* Look up NAME for type used in elaborated name specifier in
    8410              :    the scopes given by HOW.
    8411              : 
    8412              :    Unlike lookup_name_1, we make sure that NAME is actually
    8413              :    declared in the desired scope, not from inheritance, nor using
    8414              :    directive.  For using declaration, there is DR138 still waiting
    8415              :    to be resolved.  Hidden name coming from an earlier friend
    8416              :    declaration is also returned, and will be made visible unless HOW
    8417              :    is TAG_how::HIDDEN_FRIEND.
    8418              : 
    8419              :    A TYPE_DECL best matching the NAME is returned.  Catching error
    8420              :    and issuing diagnostics are caller's responsibility.  */
    8421              : 
    8422              : tree
    8423     23362945 : lookup_elaborated_type (tree name, TAG_how how)
    8424              : {
    8425     23362945 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8426              : 
    8427     23362945 :   cp_binding_level *b = current_binding_level;
    8428              : 
    8429     23362945 :   if (b->kind != sk_namespace)
    8430              :     /* Look in non-namespace scopes.  */
    8431              :     for (cxx_binding *iter = NULL;
    8432     16342066 :          (iter = outer_binding (name, iter, /*class_p=*/ true)); )
    8433              :       {
    8434              :         /* First check we're supposed to be looking in this scope --
    8435              :            if we're not, we're done.  */
    8436       917833 :         for (; b != iter->scope; b = b->level_chain)
    8437       595763 :           if (!(b->kind == sk_cleanup
    8438       436878 :                 || b->kind == sk_template_parms
    8439       158894 :                 || b->kind == sk_function_parms
    8440       158864 :                 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    8441              :             return NULL_TREE;
    8442              : 
    8443              :         /* Check if this is the kind of thing we're looking for.  If
    8444              :            HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
    8445              :            come from base class.  For ITER->VALUE, we can simply use
    8446              :            INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
    8447              :            our own check.
    8448              : 
    8449              :            We check ITER->TYPE before ITER->VALUE in order to handle
    8450              :              typedef struct C {} C;
    8451              :            correctly.  */
    8452              : 
    8453       480952 :         if (tree type = strip_using_decl (iter->type))
    8454              :           {
    8455        50571 :             if (qualify_lookup (type, LOOK_want::TYPE)
    8456        50571 :                 && (how != TAG_how::CURRENT_ONLY
    8457          118 :                     || LOCAL_BINDING_P (iter)
    8458          118 :                     || DECL_CONTEXT (type) == iter->scope->this_entity))
    8459              :               {
    8460        50522 :                 if (how != TAG_how::HIDDEN_FRIEND)
    8461              :                   /* It is no longer a hidden binding.  */
    8462           72 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8463              : 
    8464        50522 :                 return type;
    8465              :               }
    8466              :           }
    8467              :         else
    8468              :           {
    8469       430381 :             tree value = strip_using_decl (iter->value);
    8470       430381 :             if (qualify_lookup (value, LOOK_want::TYPE)
    8471       430381 :                 && (how != TAG_how::CURRENT_ONLY
    8472       101324 :                     || !INHERITED_VALUE_BINDING_P (iter)))
    8473              :               {
    8474       430249 :                 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
    8475              :                   /* It is no longer a hidden binding.  */
    8476       101330 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8477              : 
    8478       430249 :                 return value;
    8479              :               }
    8480              :           }
    8481              :       }
    8482              : 
    8483              :   /* Now check if we can look in namespace scope.  */
    8484     36425352 :   for (; b->kind != sk_namespace; b = b->level_chain)
    8485              :     if (!(b->kind == sk_cleanup
    8486              :           || b->kind == sk_template_parms
    8487              :           || b->kind == sk_function_parms
    8488      3936922 :           || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    8489              :       return NULL_TREE;
    8490              : 
    8491              :   /* Look in the innermost namespace.  */
    8492     19293068 :   tree ns = b->this_entity;
    8493              : 
    8494              :   /* If an import is going to provide a definition for this tag,
    8495              :      load it now so that we don't get confused later when processing
    8496              :      this tag's definition.  */
    8497     19293068 :   if (modules_p ())
    8498        74608 :     lazy_load_pendings (ns, name);
    8499              : 
    8500     19293068 :   if (tree *slot = find_namespace_slot (ns, name))
    8501              :     {
    8502      3248808 :       tree bind = *slot;
    8503      3248808 :       if (TREE_CODE (bind) == BINDING_VECTOR)
    8504           97 :         bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
    8505              : 
    8506           97 :       if (bind)
    8507              :         {
    8508              :           /* If this is the kind of thing we're looking for, we're done.  */
    8509      3248738 :           if (tree type = strip_using_decl (MAYBE_STAT_TYPE (bind)))
    8510              :             {
    8511          167 :               if (how != TAG_how::HIDDEN_FRIEND)
    8512              :                 /* No longer hidden.  */
    8513          167 :                 STAT_TYPE_HIDDEN_P (*slot) = false;
    8514              : 
    8515          167 :               return type;
    8516              :             }
    8517      3248571 :           else if (tree decl = strip_using_decl (MAYBE_STAT_DECL (bind)))
    8518              :             {
    8519      3248571 :               if (qualify_lookup (decl, LOOK_want::TYPE))
    8520              :                 {
    8521      2901147 :                   if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
    8522      3371083 :                       && STAT_DECL_HIDDEN_P (bind))
    8523              :                     {
    8524       131640 :                       if (STAT_TYPE (bind))
    8525            0 :                         STAT_DECL_HIDDEN_P (bind) = false;
    8526              :                       else
    8527              :                         {
    8528              :                           /* There is no type, just remove the stat
    8529              :                              hack.  */
    8530       131640 :                           if (*slot == bind)
    8531       131637 :                             *slot = decl;
    8532              :                           else
    8533            3 :                             BINDING_VECTOR_CLUSTER (*slot, 0)
    8534            3 :                               .slots[BINDING_SLOT_CURRENT] = decl;
    8535              :                         }
    8536              :                     }
    8537      3239426 :                   return decl;
    8538              :                 }
    8539              :             }
    8540              :         }
    8541              : 
    8542         9215 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    8543              :         {
    8544              :           /* We could be redeclaring a global module entity, (from GMF
    8545              :              or header unit), or from another partition, or
    8546              :              specializing an imported template.  */
    8547           70 :           bitmap imports = get_import_bitmap ();
    8548           70 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    8549              : 
    8550              :           /* Scan the imported bindings.  */
    8551           70 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    8552           70 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    8553              :             {
    8554           70 :               ix--;
    8555           70 :               cluster++;
    8556              :             }
    8557              : 
    8558              :           /* Do this in forward order, so we load modules in an order
    8559              :              the user expects.  */
    8560           80 :           for (; ix--; cluster++)
    8561          150 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    8562              :               {
    8563              :                 /* Are we importing this module?  */
    8564          140 :                 if (unsigned base = cluster->indices[jx].base)
    8565           61 :                   if (unsigned span = cluster->indices[jx].span)
    8566           61 :                     do
    8567           61 :                       if (bitmap_bit_p (imports, base))
    8568           60 :                         goto found;
    8569            1 :                     while (++base, --span);
    8570           80 :                 continue;
    8571              : 
    8572           60 :               found:;
    8573              :                 /* Is it loaded?  */
    8574           60 :                 if (cluster->slots[jx].is_lazy ())
    8575              :                   {
    8576           32 :                     gcc_assert (cluster->indices[jx].span == 1);
    8577           32 :                     lazy_load_binding (cluster->indices[jx].base,
    8578              :                                        ns, name, &cluster->slots[jx]);
    8579              :                   }
    8580           60 :                 tree bind = cluster->slots[jx];
    8581           60 :                 if (!bind)
    8582              :                   /* Load errors could mean there's nothing here.  */
    8583            0 :                   continue;
    8584              : 
    8585              :                 /* Extract what we can see from here.  If there's no
    8586              :                    stat_hack, then everything was exported.  */
    8587           60 :                 tree type = NULL_TREE;
    8588              : 
    8589              :                 /* If no stat hack, everything is visible.  */
    8590           60 :                 if (STAT_HACK_P (bind))
    8591              :                   {
    8592           51 :                     if (STAT_TYPE_VISIBLE_P (bind))
    8593           18 :                       type = STAT_TYPE (bind);
    8594           51 :                     bind = STAT_VISIBLE (bind);
    8595              :                   }
    8596              : 
    8597           51 :                 if (type && qualify_lookup (type, LOOK_want::TYPE))
    8598            3 :                   return strip_using_decl (type);
    8599              : 
    8600           57 :                 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
    8601           57 :                   return strip_using_decl (bind);
    8602           80 :               }
    8603              : 
    8604           10 :           if (!module_purview_p ())
    8605              :             {
    8606              :               /* We're in the global module, perhaps there's a tag
    8607              :                  there?  */
    8608              : 
    8609              :               /* FIXME: In general we should probably merge global module
    8610              :                  classes in check_module_override rather than here, but for
    8611              :                  GCC14 let's just fix lazy declarations of __class_type_info in
    8612              :                  build_dynamic_cast_1.  */
    8613           10 :               if (current_namespace == abi_node)
    8614              :                 {
    8615            7 :                   tree g = (BINDING_VECTOR_CLUSTER (*slot, 0)
    8616            7 :                             .slots[BINDING_SLOT_GLOBAL]);
    8617            7 :                   for (ovl_iterator iter (g); iter; ++iter)
    8618            7 :                     if (qualify_lookup (*iter, LOOK_want::TYPE))
    8619            7 :                       return *iter;
    8620              :                 }
    8621              :             }
    8622              :         }
    8623              :     }
    8624              : 
    8625              :   return NULL_TREE;
    8626     23362945 : }
    8627              : 
    8628              : /* The type TYPE is being declared.  If it is a class template, or a
    8629              :    specialization of a class template, do any processing required and
    8630              :    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
    8631              :    being declared a friend.  B is the binding level at which this TYPE
    8632              :    should be bound.
    8633              : 
    8634              :    Returns the TYPE_DECL for TYPE, which may have been altered by this
    8635              :    processing.  */
    8636              : 
    8637              : static tree
    8638     23138089 : maybe_process_template_type_declaration (tree type, int is_friend,
    8639              :                                          cp_binding_level *b)
    8640              : {
    8641     23138089 :   tree decl = TYPE_NAME (type);
    8642              : 
    8643     23138089 :   if (processing_template_parmlist)
    8644              :     /* You can't declare a new template type in a template parameter
    8645              :        list.  But, you can declare a non-template type:
    8646              : 
    8647              :          template <class A*> struct S;
    8648              : 
    8649              :        is a forward-declaration of `A'.  */
    8650              :     ;
    8651     23137942 :   else if (b->kind == sk_namespace
    8652      6672101 :            && current_binding_level->kind != sk_namespace)
    8653              :     /* If this new type is being injected into a containing scope,
    8654              :        then it's not a template type.  */
    8655              :     ;
    8656              :   else
    8657              :     {
    8658     23017380 :       gcc_assert (MAYBE_CLASS_TYPE_P (type)
    8659              :                   || TREE_CODE (type) == ENUMERAL_TYPE);
    8660              : 
    8661     23017380 :       if (processing_template_decl)
    8662              :         {
    8663     13377338 :           decl = push_template_decl (decl, is_friend);
    8664     13377338 :           if (decl == error_mark_node)
    8665              :             return error_mark_node;
    8666              : 
    8667              :           /* If the current binding level is the binding level for the
    8668              :              template parameters (see the comment in
    8669              :              begin_template_parm_list) and the enclosing level is a class
    8670              :              scope, and we're not looking at a friend, push the
    8671              :              declaration of the member class into the class scope.  In the
    8672              :              friend case, push_template_decl will already have put the
    8673              :              friend into global scope, if appropriate.  */
    8674     13377317 :           if (TREE_CODE (type) != ENUMERAL_TYPE
    8675     13042460 :               && !is_friend && b->kind == sk_template_parms
    8676     10318783 :               && b->level_chain->kind == sk_class)
    8677              :             {
    8678       672271 :               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
    8679              : 
    8680       672271 :               if (!COMPLETE_TYPE_P (current_class_type))
    8681       672250 :                 maybe_add_class_template_decl_list (current_class_type,
    8682              :                                                     type, /*friend_p=*/0);
    8683              :             }
    8684              :         }
    8685              :     }
    8686              : 
    8687              :   return decl;
    8688              : }
    8689              : 
    8690              : /* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
    8691              :    that the NAME is a class template, the tag is processed but not pushed.
    8692              : 
    8693              :    The pushed scope depend on the SCOPE parameter:
    8694              :    - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
    8695              :      scope.
    8696              :    - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
    8697              :      non-template-parameter scope.  This case is needed for forward
    8698              :      declarations.
    8699              :    - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
    8700              :      TS_GLOBAL case except that names within template-parameter scopes
    8701              :      are not pushed at all.
    8702              : 
    8703              :    Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
    8704              : 
    8705              : tree
    8706     23138089 : pushtag (tree name, tree type, TAG_how how)
    8707              : {
    8708     23138089 :   tree decl;
    8709              : 
    8710     23138089 :   gcc_assert (identifier_p (name));
    8711              : 
    8712     23138089 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8713              : 
    8714     23138089 :   cp_binding_level *b = current_binding_level;
    8715     23268960 :   while (true)
    8716              :     {
    8717     23268960 :       if (/* Cleanup scopes are not scopes from the point of view of
    8718              :              the language.  */
    8719     23268960 :           b->kind == sk_cleanup
    8720              :           /* Neither are function parameter scopes.  */
    8721     23268954 :           || b->kind == sk_function_parms
    8722              :           /* Neither are the scopes used to hold template parameters
    8723              :              for an explicit specialization.  For an ordinary template
    8724              :              declaration, these scopes are not scopes from the point of
    8725              :              view of the language.  */
    8726     23257135 :           || (b->kind == sk_template_parms
    8727     10794524 :               && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
    8728        11888 :         b = b->level_chain;
    8729     23257072 :       else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
    8730              :         {
    8731       118983 :           b = b->level_chain;
    8732       118983 :           if (b->kind == sk_template_parms)
    8733          483 :             b = b->level_chain;
    8734              :         }
    8735              :       else
    8736              :         break;
    8737              :     }
    8738              : 
    8739              :   /* Do C++ gratuitous typedefing.  */
    8740     23138089 :   if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
    8741              :     {
    8742     23138089 :       tree tdef;
    8743     23138089 :       tree context = TYPE_CONTEXT (type);
    8744              : 
    8745     23138089 :       if (! context)
    8746              :         {
    8747              :           cp_binding_level *cb = b;
    8748     37148160 :           while (cb->kind != sk_namespace
    8749     20829393 :                  && cb->kind != sk_class
    8750     54029394 :                  && (cb->kind != sk_function_parms
    8751      2523937 :                      || !cb->this_entity))
    8752     14357301 :             cb = cb->level_chain;
    8753     22790859 :           tree cs = cb->this_entity;
    8754              : 
    8755     22790859 :           gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
    8756              :                                ? cs == current_function_decl
    8757              :                                : TYPE_P (cs) ? cs == current_class_type
    8758              :                                : cs == current_namespace);
    8759              : 
    8760     22790859 :           if (how == TAG_how::CURRENT_ONLY
    8761       248898 :               || (cs && TREE_CODE (cs) == FUNCTION_DECL))
    8762              :             context = cs;
    8763       248805 :           else if (cs && TYPE_P (cs))
    8764              :             /* When declaring a friend class of a local class, we want
    8765              :                to inject the newly named class into the scope
    8766              :                containing the local class, not the namespace
    8767              :                scope.  */
    8768       128298 :             context = decl_function_context (get_type_decl (cs));
    8769              :         }
    8770     22790766 :       if (!context)
    8771       248802 :         context = current_namespace;
    8772              : 
    8773     23138089 :       tdef = create_implicit_typedef (name, type);
    8774     23138089 :       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
    8775     23138089 :       set_originating_module (tdef);
    8776              : 
    8777     23138089 :       decl = maybe_process_template_type_declaration
    8778     23138089 :         (type, how == TAG_how::HIDDEN_FRIEND, b);
    8779     23138089 :       if (decl == error_mark_node)
    8780              :         return decl;
    8781              : 
    8782     23138068 :       if (b->kind == sk_class)
    8783              :         {
    8784      3147474 :           if (!TYPE_BEING_DEFINED (current_class_type))
    8785              :             /* Don't push anywhere if the class is complete; a lambda in an
    8786              :                NSDMI is not a member of the class.  */
    8787              :             ;
    8788      3145592 :           else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
    8789              :             /* Put this TYPE_DECL on the TYPE_FIELDS list for the
    8790              :                class.  But if it's a member template class, we want
    8791              :                the TEMPLATE_DECL, not the TYPE_DECL, so this is done
    8792              :                later.  */
    8793      3145249 :             finish_member_declaration (decl);
    8794              :           else
    8795          343 :             pushdecl_class_level (decl);
    8796              :         }
    8797     19990594 :       else if (b->kind == sk_template_parms)
    8798              :         {
    8799              :           /* Do not push the tag here -- we'll want to push the
    8800              :              TEMPLATE_DECL.  */
    8801     10794443 :           if (b->level_chain->kind != sk_class)
    8802      9646635 :             set_identifier_type_value_with_scope (name, tdef, b->level_chain);
    8803              :         }
    8804              :       else
    8805              :         {
    8806      9196151 :           decl = do_pushdecl_with_scope
    8807      9196151 :             (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
    8808      9196151 :           if (decl == error_mark_node)
    8809              :             return decl;
    8810              : 
    8811      9196103 :           if (DECL_CONTEXT (decl) == std_node
    8812      2297239 :               && init_list_identifier == DECL_NAME (TYPE_NAME (type))
    8813      9196109 :               && !CLASSTYPE_TEMPLATE_INFO (type))
    8814              :             {
    8815            6 :               error ("declaration of %<std::initializer_list%> does not match "
    8816              :                      "%<#include <initializer_list>%>, isn%'t a template");
    8817            6 :               return error_mark_node;
    8818              :             }
    8819              :         }
    8820              : 
    8821     23138014 :       TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
    8822              : 
    8823              :       /* If this is a local class, keep track of it.  We need this
    8824              :          information for name-mangling, and so that it is possible to
    8825              :          find all function definitions in a translation unit in a
    8826              :          convenient way.  (It's otherwise tricky to find a member
    8827              :          function definition it's only pointed to from within a local
    8828              :          class.)  */
    8829     23138014 :       if (TYPE_FUNCTION_SCOPE_P (type))
    8830              :         {
    8831      2523915 :           if (processing_template_decl)
    8832              :             {
    8833              :               /* Push a DECL_EXPR so we call pushtag at the right time in
    8834              :                  template instantiation rather than in some nested context.  */
    8835      1184185 :               add_decl_expr (decl);
    8836              :             }
    8837              :           /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead.  */
    8838      2675683 :           else if (!LAMBDA_TYPE_P (type))
    8839       663134 :             determine_local_discriminator (TYPE_NAME (type));
    8840              :         }
    8841              :     }
    8842              : 
    8843     23138014 :   if (b->kind == sk_class
    8844     23138014 :       && !COMPLETE_TYPE_P (current_class_type))
    8845      3145622 :     maybe_add_class_template_decl_list (current_class_type,
    8846              :                                         type, /*friend_p=*/0);
    8847              : 
    8848     23138014 :   decl = TYPE_NAME (type);
    8849     23138014 :   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    8850              : 
    8851              :   /* Set type visibility now if this is a forward declaration.  */
    8852     23138014 :   TREE_PUBLIC (decl) = 1;
    8853     23138014 :   determine_visibility (decl);
    8854     23138014 :   check_module_decl_linkage (decl);
    8855              : 
    8856     23138014 :   return type;
    8857     23138089 : }
    8858              : 
    8859              : /* Subroutines for reverting temporarily to top-level for instantiation
    8860              :    of templates and such.  We actually need to clear out the class- and
    8861              :    local-value slots of all identifiers, so that only the global values
    8862              :    are at all visible.  Simply setting current_binding_level to the global
    8863              :    scope isn't enough, because more binding levels may be pushed.  */
    8864              : struct saved_scope *scope_chain;
    8865              : 
    8866              : /* Return true if ID has not already been marked.  */
    8867              : 
    8868              : static inline bool
    8869   3523864355 : store_binding_p (tree id)
    8870              : {
    8871   7041493133 :   if (!id || !IDENTIFIER_BINDING (id))
    8872              :     return false;
    8873              : 
    8874   3284543492 :   if (IDENTIFIER_MARKED (id))
    8875            0 :     return false;
    8876              : 
    8877              :   return true;
    8878              : }
    8879              : 
    8880              : /* Add an appropriate binding to *OLD_BINDINGS which needs to already
    8881              :    have enough space reserved.  */
    8882              : 
    8883              : static void
    8884   1303980523 : store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
    8885              : {
    8886   1303980523 :   cxx_saved_binding saved;
    8887              : 
    8888   1303980523 :   gcc_checking_assert (store_binding_p (id));
    8889              : 
    8890   1303980523 :   IDENTIFIER_MARKED (id) = 1;
    8891              : 
    8892   1303980523 :   saved.identifier = id;
    8893   1303980523 :   saved.binding = IDENTIFIER_BINDING (id);
    8894   1303980523 :   saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
    8895   1303980523 :   (*old_bindings)->quick_push (saved);
    8896   1303980523 :   IDENTIFIER_BINDING (id) = NULL;
    8897   1303980523 : }
    8898              : 
    8899              : static void
    8900    839856262 : store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
    8901              : {
    8902    839856262 :   static vec<tree> bindings_need_stored;
    8903    839856262 :   tree t, id;
    8904    839856262 :   size_t i;
    8905              : 
    8906    839856262 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8907   2385199476 :   for (t = names; t; t = TREE_CHAIN (t))
    8908              :     {
    8909    705486952 :       if (TREE_CODE (t) == TREE_LIST)
    8910     41909368 :         id = TREE_PURPOSE (t);
    8911              :       else
    8912    663577584 :         id = DECL_NAME (t);
    8913              : 
    8914    705486952 :       if (store_binding_p (id))
    8915    676582446 :         bindings_need_stored.safe_push (id);
    8916              :     }
    8917    839856262 :   if (!bindings_need_stored.is_empty ())
    8918              :     {
    8919    240284022 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8920   1157150490 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8921              :         {
    8922              :           /* We can apparently have duplicates in NAMES.  */
    8923    676582446 :           if (store_binding_p (id))
    8924    676582107 :             store_binding (id, old_bindings);
    8925              :         }
    8926    240284022 :       bindings_need_stored.truncate (0);
    8927              :     }
    8928    839856262 : }
    8929              : 
    8930              : /* Like store_bindings, but NAMES is a vector of cp_class_binding
    8931              :    objects, rather than a TREE_LIST.  */
    8932              : 
    8933              : static void
    8934    509975219 : store_class_bindings (vec<cp_class_binding, va_gc> *names,
    8935              :                       vec<cxx_saved_binding, va_gc> **old_bindings)
    8936              : {
    8937    509975219 :   static vec<tree> bindings_need_stored;
    8938    509975219 :   size_t i;
    8939    509975219 :   cp_class_binding *cb;
    8940              : 
    8941   1347789653 :   for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
    8942    837814434 :     if (store_binding_p (cb->identifier))
    8943    627398416 :       bindings_need_stored.safe_push (cb->identifier);
    8944    509975219 :   if (!bindings_need_stored.is_empty ())
    8945              :     {
    8946     78841202 :       tree id;
    8947     78841202 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8948    785080820 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8949    627398416 :         store_binding (id, old_bindings);
    8950     78841202 :       bindings_need_stored.truncate (0);
    8951              :     }
    8952    509975219 : }
    8953              : 
    8954              : /* A chain of saved_scope structures awaiting reuse.  */
    8955              : 
    8956              : static GTY((deletable)) struct saved_scope *free_saved_scope;
    8957              : 
    8958              : /* Temporarily make the current scope the global namespace, saving away
    8959              :    the current scope for pop_from_top_level.  */
    8960              : 
    8961              : void
    8962    664104779 : push_to_top_level (void)
    8963              : {
    8964    664104779 :   struct saved_scope *s;
    8965    664104779 :   cp_binding_level *b;
    8966    664104779 :   cxx_saved_binding *sb;
    8967    664104779 :   size_t i;
    8968    664104779 :   bool need_pop;
    8969              : 
    8970    664104779 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8971              : 
    8972              :   /* Reuse or create a new structure for this saved scope.  */
    8973    664104779 :   if (free_saved_scope != NULL)
    8974              :     {
    8975    662863335 :       s = free_saved_scope;
    8976    662863335 :       free_saved_scope = s->prev;
    8977              : 
    8978    662863335 :       vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
    8979    662863335 :       memset (s, 0, sizeof (*s));
    8980              :       /* Also reuse the structure's old_bindings vector.  */
    8981    662863335 :       vec_safe_truncate (old_bindings, 0);
    8982    662863335 :       s->old_bindings = old_bindings;
    8983              :     }
    8984              :   else
    8985      1241444 :     s = ggc_cleared_alloc<saved_scope> ();
    8986              : 
    8987    664104779 :   b = scope_chain ? current_binding_level : 0;
    8988              : 
    8989              :   /* If we're in the middle of some function, save our state.  */
    8990    664104779 :   if (cfun)
    8991              :     {
    8992    116975010 :       need_pop = true;
    8993    116975010 :       push_function_context ();
    8994              :     }
    8995              :   else
    8996              :     need_pop = false;
    8997              : 
    8998    664104779 :   if (scope_chain && previous_class_level)
    8999    162405420 :     store_class_bindings (previous_class_level->class_shadowed,
    9000              :                           &s->old_bindings);
    9001              : 
    9002              :   /* Save and clear any IDENTIFIER_BINDING from local scopes.  */
    9003   1503961041 :   for (; b; b = b->level_chain)
    9004              :     {
    9005   1503863008 :       tree t;
    9006              : 
    9007              :       /* We don't need to consider namespace scopes, they don't affect
    9008              :          IDENTIFIER_BINDING.  */
    9009   1503863008 :       if (b->kind == sk_namespace)
    9010              :         {
    9011              :           /* Jump straight to '::'.  */
    9012    664006746 :           b = NAMESPACE_LEVEL (global_namespace);
    9013    664006746 :           break;
    9014              :         }
    9015              : 
    9016    839856262 :       store_bindings (b->names, &s->old_bindings);
    9017              :       /* We also need to check class_shadowed to save class-level type
    9018              :          bindings, since pushclass doesn't fill in b->names.  */
    9019    839856262 :       if (b->kind == sk_class)
    9020    347569799 :         store_class_bindings (b->class_shadowed, &s->old_bindings);
    9021              : 
    9022              :       /* Unwind type-value slots back to top level.  */
    9023   1268579647 :       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
    9024    428723385 :         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
    9025              :     }
    9026              : 
    9027   1968085302 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
    9028   1303980523 :     IDENTIFIER_MARKED (sb->identifier) = 0;
    9029              : 
    9030    664104779 :   s->prev = scope_chain;
    9031    664104779 :   s->bindings = b;
    9032    664104779 :   s->need_pop_function_context = need_pop;
    9033    664104779 :   s->function_decl = current_function_decl;
    9034    664104779 :   s->unevaluated_operand = cp_unevaluated_operand;
    9035    664104779 :   s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
    9036    664104779 :   s->suppress_location_wrappers = suppress_location_wrappers;
    9037    664104779 :   s->x_stmt_tree.stmts_are_full_exprs_p = true;
    9038              : 
    9039    664104779 :   scope_chain = s;
    9040    664104779 :   current_function_decl = NULL_TREE;
    9041    664104779 :   current_lang_base = NULL;
    9042    664104779 :   current_lang_name = lang_name_cplusplus;
    9043    664104779 :   current_namespace = global_namespace;
    9044    664104779 :   push_class_stack ();
    9045    664104779 :   cp_unevaluated_operand = 0;
    9046    664104779 :   c_inhibit_evaluation_warnings = 0;
    9047    664104779 :   suppress_location_wrappers = 0;
    9048    664104779 : }
    9049              : 
    9050              : void
    9051    663979656 : pop_from_top_level (void)
    9052              : {
    9053    663979656 :   struct saved_scope *s = scope_chain;
    9054    663979656 :   cxx_saved_binding *saved;
    9055    663979656 :   size_t i;
    9056              : 
    9057    663979656 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9058              : 
    9059    663979656 :   pop_class_stack ();
    9060              : 
    9061    663979656 :   release_tree_vector (current_lang_base);
    9062              : 
    9063    663979656 :   scope_chain = s->prev;
    9064   1967957365 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
    9065              :     {
    9066   1303977709 :       tree id = saved->identifier;
    9067              : 
    9068   1303977709 :       IDENTIFIER_BINDING (id) = saved->binding;
    9069   1303977709 :       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
    9070              :     }
    9071              : 
    9072              :   /* If we were in the middle of compiling a function, restore our
    9073              :      state.  */
    9074    663979656 :   if (s->need_pop_function_context)
    9075    116974989 :     pop_function_context ();
    9076    663979656 :   current_function_decl = s->function_decl;
    9077    663979656 :   cp_unevaluated_operand = s->unevaluated_operand;
    9078    663979656 :   c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
    9079    663979656 :   suppress_location_wrappers = s->suppress_location_wrappers;
    9080              : 
    9081              :   /* Make this saved_scope structure available for reuse by
    9082              :      push_to_top_level.  */
    9083    663979656 :   s->prev = free_saved_scope;
    9084    663979656 :   free_saved_scope = s;
    9085    663979656 : }
    9086              : 
    9087              : namespace {
    9088              : 
    9089              : /* Helper class for saving/restoring relevant global flags for the
    9090              :    function-local case of maybe_push_to_top_level.  */
    9091              : 
    9092              : struct local_state_t
    9093              : {
    9094              :   int cp_unevaluated_operand;
    9095              :   int c_inhibit_evaluation_warnings;
    9096              :   int cp_noexcept_operand_;
    9097              :   bool has_cfun;
    9098              : 
    9099              :   static local_state_t
    9100      2272599 :   save_and_clear ()
    9101              :   {
    9102      2272599 :     local_state_t s;
    9103      2272599 :     s.cp_unevaluated_operand = ::cp_unevaluated_operand;
    9104      2272599 :     ::cp_unevaluated_operand = 0;
    9105      2272599 :     s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
    9106      2272599 :     ::c_inhibit_evaluation_warnings = 0;
    9107      2272599 :     s.cp_noexcept_operand_ = ::cp_noexcept_operand;
    9108      2272599 :     ::cp_noexcept_operand = 0;
    9109      2272599 :     s.has_cfun = !!cfun;
    9110      2272599 :     if (s.has_cfun)
    9111      1800216 :       push_function_context ();
    9112      2272599 :     return s;
    9113              :   }
    9114              : 
    9115              :   void
    9116      2272599 :   restore () const
    9117              :   {
    9118      2272599 :     ::cp_unevaluated_operand = this->cp_unevaluated_operand;
    9119      2272599 :     ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
    9120      2272599 :     ::cp_noexcept_operand = this->cp_noexcept_operand_;
    9121      2272599 :     if (this->has_cfun)
    9122      1800216 :       pop_function_context ();
    9123      2272599 :   }
    9124              : };
    9125              : 
    9126              : vec<local_state_t> local_state_stack;
    9127              : 
    9128              : } // anon namespace
    9129              : 
    9130              : /* Like push_to_top_level, but not if D is function-local.  Returns whether we
    9131              :    did push to top.  */
    9132              : 
    9133              : bool
    9134     89710791 : maybe_push_to_top_level (tree d)
    9135              : {
    9136              :   /* Push if D isn't function-local, or is a lambda function, for which name
    9137              :      resolution is already done.  */
    9138     89710791 :   const bool push_to_top
    9139     91801307 :     = (LAMBDA_FUNCTION_P (d)
    9140     89121034 :        || (TREE_CODE (d) == TYPE_DECL
    9141     38918568 :            && TREE_TYPE (d)
    9142     77442993 :            && LAMBDA_TYPE_P (TREE_TYPE (d)))
    9143     87406512 :        || !current_function_decl
    9144    119445419 :        || !decl_function_context (d));
    9145              : 
    9146     89710791 :   if (push_to_top)
    9147     87438192 :     push_to_top_level ();
    9148              :   else
    9149              :     {
    9150      2272599 :       gcc_assert (!processing_template_decl);
    9151      2272599 :       local_state_stack.safe_push (local_state_t::save_and_clear ());
    9152              :     }
    9153              : 
    9154     89710791 :   return push_to_top;
    9155              : }
    9156              : 
    9157              : /* Return from whatever maybe_push_to_top_level did.  */
    9158              : 
    9159              : void
    9160     89710782 : maybe_pop_from_top_level (bool push_to_top)
    9161              : {
    9162     89710782 :   if (push_to_top)
    9163     87438183 :     pop_from_top_level ();
    9164              :   else
    9165      2272599 :     local_state_stack.pop ().restore ();
    9166     89710782 : }
    9167              : 
    9168              : /* Push into the scope of the namespace NS, even if it is deeply
    9169              :    nested within another namespace.  */
    9170              : 
    9171              : void
    9172     90964996 : push_nested_namespace (tree ns)
    9173              : {
    9174     90964996 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9175     90964996 :   if (ns == global_namespace)
    9176     44234069 :     push_to_top_level ();
    9177              :   else
    9178              :     {
    9179     90176419 :       push_nested_namespace (CP_DECL_CONTEXT (ns));
    9180     46730927 :       resume_scope (NAMESPACE_LEVEL (ns));
    9181     46730927 :       current_namespace = ns;
    9182              :     }
    9183     90964996 : }
    9184              : 
    9185              : /* Pop back from the scope of the namespace NS, which was previously
    9186              :    entered with push_nested_namespace.  */
    9187              : 
    9188              : void
    9189     44223275 : pop_nested_namespace (tree ns)
    9190              : {
    9191     44223275 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9192    135177477 :   while (ns != global_namespace)
    9193              :     {
    9194     46730927 :       ns = CP_DECL_CONTEXT (ns);
    9195     46730927 :       current_namespace = ns;
    9196     46730927 :       leave_scope ();
    9197              :     }
    9198              : 
    9199     44223275 :   pop_from_top_level ();
    9200     44223275 : }
    9201              : 
    9202              : /* Add TARGET to USINGS, if it does not already exist there.  We used
    9203              :    to build the complete graph of usings at this point, from the POV
    9204              :    of the source namespaces.  Now we build that as we perform the
    9205              :    unqualified search.  */
    9206              : 
    9207              : static void
    9208       259675 : add_using_namespace (vec<tree, va_gc> *&usings, tree target,
    9209              :                      bool imported = false)
    9210              : {
    9211              :   /* Find if this using already exists.  */
    9212       259675 :   tree old = NULL_TREE;
    9213       259675 :   if (usings)
    9214         1593 :     for (tree t : *usings)
    9215         1067 :       if (USING_DECL_DECLS (t) == target)
    9216              :         {
    9217              :           old = t;
    9218              :           break;
    9219              :         }
    9220              : 
    9221       259675 :   tree decl = old;
    9222       259675 :   if (!decl)
    9223              :     {
    9224       259151 :       decl = build_lang_decl (USING_DECL, NULL_TREE, NULL_TREE);
    9225       259151 :       USING_DECL_DECLS (decl) = target;
    9226       259151 :       DECL_MODULE_IMPORT_P (decl) = imported;
    9227              :     }
    9228              : 
    9229              :   /* Update module flags in case that has changed.  */
    9230       259675 :   if (modules_p ())
    9231              :     {
    9232          990 :       if (module_purview_p ())
    9233          478 :         DECL_MODULE_PURVIEW_P (decl) = true;
    9234          990 :       if (module_exporting_p ())
    9235          277 :         DECL_MODULE_EXPORT_P (decl) = true;
    9236          990 :       if (!imported)
    9237          785 :         DECL_MODULE_IMPORT_P (decl) = false;
    9238              :     }
    9239              : 
    9240       259675 :   if (!old)
    9241       259151 :     vec_safe_push (usings, decl);
    9242       259675 : }
    9243              : 
    9244              : /* Convenience overload for the above, taking the user as its first
    9245              :    parameter, for use when importing a using-directive.  */
    9246              : 
    9247              : void
    9248          147 : add_imported_using_namespace (tree ns, tree target)
    9249              : {
    9250          294 :   add_using_namespace (NAMESPACE_LEVEL (ns)->using_directives,
    9251          147 :                        ORIGINAL_NAMESPACE (target),
    9252              :                        /*imported=*/true);
    9253          147 : }
    9254              : 
    9255              : /* Tell the debug system of a using directive.  */
    9256              : 
    9257              : static void
    9258       205770 : emit_debug_info_using_namespace (tree from, tree target, bool implicit)
    9259              : {
    9260              :   /* Emit debugging info.  */
    9261       205770 :   tree context = from != global_namespace ? from : NULL_TREE;
    9262       205770 :   debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
    9263              :                                         implicit);
    9264       205770 : }
    9265              : 
    9266              : /* Process a using directive.  */
    9267              : 
    9268              : void
    9269       258178 : finish_using_directive (tree target, tree attribs)
    9270              : {
    9271       258178 :   if (target == error_mark_node)
    9272              :     return;
    9273              : 
    9274       258158 :   if (current_binding_level->kind != sk_namespace)
    9275       227654 :     add_stmt (build_stmt (input_location, USING_STMT, target));
    9276              :   else
    9277        61008 :     emit_debug_info_using_namespace (current_binding_level->this_entity,
    9278        30504 :                                      ORIGINAL_NAMESPACE (target), false);
    9279              : 
    9280       516316 :   add_using_namespace (current_binding_level->using_directives,
    9281       258158 :                        ORIGINAL_NAMESPACE (target));
    9282              : 
    9283       258158 :   bool diagnosed = false;
    9284       258158 :   if (attribs != error_mark_node)
    9285       258235 :     for (tree a = attribs; a; a = TREE_CHAIN (a))
    9286              :       {
    9287           77 :         tree name = get_attribute_name (a);
    9288           77 :         if (current_binding_level->kind == sk_namespace
    9289           77 :             && is_attribute_p ("strong", name))
    9290              :           {
    9291           12 :             auto_diagnostic_group d;
    9292           18 :             if (warning (0, "%<strong%> using directive no longer supported")
    9293           12 :                 && CP_DECL_CONTEXT (target) == current_namespace)
    9294            6 :               inform (DECL_SOURCE_LOCATION (target),
    9295              :                       "you can use an inline namespace instead");
    9296           12 :           }
    9297           62 :         else if ((flag_openmp || flag_openmp_simd)
    9298            3 :                  && get_attribute_namespace (a) == omp_identifier
    9299           68 :                  && (is_attribute_p ("directive", name)
    9300            0 :                      || is_attribute_p ("sequence", name)
    9301            0 :                      || is_attribute_p ("decl", name)))
    9302              :           {
    9303            3 :             if (!diagnosed)
    9304              :               {
    9305            3 :                 if (tree ar = TREE_VALUE (a))
    9306              :                   {
    9307            3 :                     tree d = TREE_VALUE (ar);
    9308            3 :                     gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
    9309            3 :                     error ("%<omp::%s%> not allowed to be specified in "
    9310              :                            "this context",
    9311            3 :                            TREE_PUBLIC (d) ? "decl" : "directive");
    9312              :                   }
    9313              :                 else
    9314            0 :                   error ("%<omp::%E%> not allowed to be specified in this "
    9315              :                          "context", name);
    9316              :                 diagnosed = true;
    9317              :               }
    9318              :           }
    9319           62 :         else if (annotation_p (a))
    9320            1 :           error ("annotation on using directive");
    9321           61 :         else if (!attribute_ignored_p (a))
    9322           46 :           warning (OPT_Wattributes, "%qD attribute directive ignored", name);
    9323              :       }
    9324              : }
    9325              : 
    9326              : /* Pushes X into the global namespace.  */
    9327              : 
    9328              : tree
    9329       365833 : pushdecl_top_level (tree x)
    9330              : {
    9331       365833 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9332       365833 :   push_to_top_level ();
    9333       365833 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9334       365833 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9335       365833 :   x = pushdecl_namespace_level (x);
    9336       365833 :   pop_from_top_level ();
    9337       731666 :   return x;
    9338       365833 : }
    9339              : 
    9340              : /* Pushes X into the global namespace and calls cp_finish_decl to
    9341              :    register the variable, initializing it with INIT.  */
    9342              : 
    9343              : tree
    9344      2374104 : pushdecl_top_level_and_finish (tree x, tree init)
    9345              : {
    9346      2374104 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9347      2374104 :   push_to_top_level ();
    9348      2374104 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9349      2374104 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9350      2374104 :   x = pushdecl_namespace_level (x);
    9351      2374104 :   cp_finish_decl (x, init, false, NULL_TREE, 0);
    9352      2374104 :   pop_from_top_level ();
    9353      4748208 :   return x;
    9354      2374104 : }
    9355              : 
    9356              : /* Enter the namespaces from current_namerspace to NS.  */
    9357              : 
    9358              : static int
    9359      6262082 : push_inline_namespaces (tree ns)
    9360              : {
    9361      6262082 :   int count = 0;
    9362      6262082 :   if (ns != current_namespace)
    9363              :     {
    9364           18 :       gcc_assert (ns != global_namespace);
    9365           27 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    9366           18 :       resume_scope (NAMESPACE_LEVEL (ns));
    9367           18 :       current_namespace = ns;
    9368           18 :       count++;
    9369              :     }
    9370      6262082 :   return count;
    9371              : }
    9372              : 
    9373              : /* SLOT is the (possibly empty) binding slot for NAME in CTX.
    9374              :    Reuse or create a namespace NAME.  NAME is null for the anonymous
    9375              :    namespace.  */
    9376              : 
    9377              : static tree
    9378         2717 : reuse_namespace (tree *slot, tree ctx, tree name)
    9379              : {
    9380         2717 :   if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
    9381              :     {
    9382              :       /* Public namespace.  Shared.  */
    9383         1045 :       tree *global_slot = slot;
    9384         1045 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    9385          208 :         global_slot = get_fixed_binding_slot (slot, name,
    9386              :                                               BINDING_SLOT_GLOBAL, false);
    9387              : 
    9388         1048 :       for (ovl_iterator iter (*global_slot); iter; ++iter)
    9389              :         {
    9390         1045 :           tree decl = *iter;
    9391              : 
    9392         1045 :           if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
    9393         1042 :             return decl;
    9394              :         }
    9395              :     }
    9396              :   return NULL_TREE;
    9397              : }
    9398              : 
    9399              : static tree
    9400       881084 : make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
    9401              : {
    9402              :   /* Create the namespace.  */
    9403       881084 :   tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
    9404       881084 :   DECL_SOURCE_LOCATION (ns) = loc;
    9405       881084 :   SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
    9406       881084 :   if (!SCOPE_DEPTH (ns))
    9407              :     /* We only allow depth 255. */
    9408            0 :     sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
    9409       881084 :   DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
    9410              : 
    9411       881084 :   if (!name)
    9412              :     /* Anon-namespaces in different header-unit imports are distinct.
    9413              :        But that's ok as their contents all have internal linkage.
    9414              :        (This is different to how they'd behave as textual includes,
    9415              :        but doing this at all is really odd source.)  */
    9416         1385 :     SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
    9417       879699 :   else if (TREE_PUBLIC (ctx))
    9418       879620 :     TREE_PUBLIC (ns) = true;
    9419              : 
    9420       881084 :   if (inline_p)
    9421       173896 :     DECL_NAMESPACE_INLINE_P (ns) = true;
    9422              : 
    9423       881084 :   return ns;
    9424              : }
    9425              : 
    9426              : /* NS was newly created, finish off making it.  */
    9427              : 
    9428              : static void
    9429       881102 : make_namespace_finish (tree ns, tree *slot, bool from_import = false)
    9430              : {
    9431       881102 :   if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
    9432              :     {
    9433              :       /* Merge into global slot.  */
    9434         1620 :       tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
    9435              :                                             BINDING_SLOT_GLOBAL, true);
    9436         1620 :       *gslot = ns;
    9437              :     }
    9438              : 
    9439       881102 :   tree ctx = CP_DECL_CONTEXT (ns);
    9440       881102 :   cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
    9441       881102 :   scope->this_entity = ns;
    9442       881102 :   scope->more_cleanups_ok = true;
    9443       881102 :   scope->kind = sk_namespace;
    9444       881102 :   scope->level_chain = NAMESPACE_LEVEL (ctx);
    9445       881102 :   NAMESPACE_LEVEL (ns) = scope;
    9446              : 
    9447       881102 :   if (DECL_NAMESPACE_INLINE_P (ns))
    9448       173896 :     vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
    9449              : 
    9450       881102 :   if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
    9451       175266 :     emit_debug_info_using_namespace (ctx, ns, true);
    9452              : 
    9453              :   /* An unnamed namespace implicitly has a using-directive inserted so
    9454              :      that its contents are usable in the surrounding context.  */
    9455       881102 :   if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
    9456         1370 :     add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns,
    9457              :                          from_import);
    9458       881102 : }
    9459              : 
    9460              : /* NS is a possibly-imported namespace that is now needed for
    9461              :    a declaration.  Add it to the current TU's binding slot.  */
    9462              : 
    9463              : void
    9464        27647 : expose_existing_namespace (tree ns)
    9465              : {
    9466        27647 :   if (!modules_p ())
    9467              :     return;
    9468              : 
    9469        27647 :   tree bind = *find_namespace_slot (CP_DECL_CONTEXT (ns), DECL_NAME (ns));
    9470        27647 :   if (bind != ns)
    9471              :     {
    9472         1229 :       auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
    9473         1229 :       binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
    9474         1229 :       gcc_checking_assert (!(tree)slot || (tree)slot == ns);
    9475         1229 :       slot = ns;
    9476              :     }
    9477              : 
    9478        27647 :   if (module_purview_p ())
    9479        13225 :     DECL_MODULE_PURVIEW_P (ns) = true;
    9480              : }
    9481              : 
    9482              : /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE,
    9483              :    then we enter an anonymous namespace.  If MAKE_INLINE is true, then
    9484              :    we create an inline namespace (it is up to the caller to check upon
    9485              :    redefinition). Return the number of namespaces entered.  */
    9486              : 
    9487              : int
    9488      7141524 : push_namespace (tree name, bool make_inline)
    9489              : {
    9490      7141524 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9491      7141524 :   int count = 0;
    9492              : 
    9493              :   /* We should not get here if the global_namespace is not yet constructed
    9494              :      nor if NAME designates the global namespace:  The global scope is
    9495              :      constructed elsewhere.  */
    9496      7141524 :   gcc_checking_assert (global_namespace != NULL && name != global_identifier);
    9497              : 
    9498      7141524 :   tree ns = NULL_TREE;
    9499      7141524 :   {
    9500      7141524 :     name_lookup lookup (name);
    9501      7141524 :     if (!lookup.search_qualified (current_namespace, /*usings=*/false))
    9502              :       ;
    9503      6262079 :     else if (TREE_CODE (lookup.value) == TREE_LIST)
    9504              :       {
    9505              :         /* An ambiguous lookup.  If exactly one is a namespace, we
    9506              :            want that.  If more than one is a namespace, error, but
    9507              :            pick one of them.  */
    9508              :         /* DR2061 can cause us to find multiple namespaces of the same
    9509              :            name.  We must treat that carefully and avoid thinking we
    9510              :            need to push a new (possibly) duplicate namespace.  Hey,
    9511              :            if you want to use the same identifier within an inline
    9512              :            nest, knock yourself out.  */
    9513            9 :         for (tree *chain = &lookup.value, next; (next = *chain);)
    9514              :           {
    9515            6 :             tree decl = TREE_VALUE (next);
    9516            6 :             if (TREE_CODE (decl) == NAMESPACE_DECL)
    9517              :               {
    9518            6 :                 if (!ns)
    9519              :                   ns = decl;
    9520            3 :                 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
    9521            6 :                   ns = decl;
    9522              : 
    9523              :                 /* Advance.  */
    9524            6 :                 chain = &TREE_CHAIN (next);
    9525              :               }
    9526              :             else
    9527              :               /* Stitch out.  */
    9528            0 :               *chain = TREE_CHAIN (next);
    9529              :           }
    9530              : 
    9531            3 :         if (TREE_CHAIN (lookup.value))
    9532              :           {
    9533            3 :             error ("%<namespace %E%> is ambiguous", name);
    9534            3 :             print_candidates (input_location, lookup.value);
    9535              :           }
    9536              :       }
    9537      6262076 :     else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    9538              :       ns = lookup.value;
    9539              : 
    9540      6262067 :     if (ns)
    9541      6262067 :       if (tree dna = DECL_NAMESPACE_ALIAS (ns))
    9542              :         {
    9543              :           /* A namespace alias is not allowed here, but if the alias
    9544              :              is for a namespace also inside the current scope,
    9545              :              accept it with a diagnostic.  That's better than dying
    9546              :              horribly.  */
    9547            9 :           if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
    9548              :             {
    9549            6 :               error ("namespace alias %qD not allowed here, "
    9550              :                      "assuming %qD", ns, dna);
    9551            6 :               ns = dna;
    9552              :             }
    9553              :           else
    9554              :             ns = NULL_TREE;
    9555              :         }
    9556      7141524 :   }
    9557              : 
    9558      7141524 :   if (ns)
    9559              :     {
    9560              :       /* DR2061.  NS might be a member of an inline namespace.  We
    9561              :          need to push into those namespaces.  */
    9562      6262064 :       if (modules_p ())
    9563        55117 :         for (tree ctx = ns; ctx != current_namespace;
    9564        21347 :              ctx = CP_DECL_CONTEXT (ctx))
    9565        21347 :           expose_existing_namespace (ctx);
    9566              : 
    9567      6262064 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    9568      6262064 :       if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
    9569              :         /* It's not builtin now.  */
    9570        33837 :         DECL_SOURCE_LOCATION (ns) = input_location;
    9571              :     }
    9572              :   else
    9573              :     {
    9574              :       /* Before making a new namespace, see if we already have one in
    9575              :          the existing partitions of the current namespace.  */
    9576       879460 :       tree *slot = find_namespace_slot (current_namespace, name, false);
    9577       879460 :       if (slot)
    9578           51 :         ns = reuse_namespace (slot, current_namespace, name);
    9579       879460 :       if (!ns)
    9580       879430 :         ns = make_namespace (current_namespace, name,
    9581              :                              input_location, make_inline);
    9582              : 
    9583       879460 :       if (pushdecl (ns) == error_mark_node)
    9584              :         ns = NULL_TREE;
    9585              :       else
    9586              :         {
    9587              :           /* Finish up making the namespace.  */
    9588       879448 :           add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
    9589       879448 :           if (!slot)
    9590              :             {
    9591       879409 :               slot = find_namespace_slot (current_namespace, name);
    9592              :               /* This should find the slot created by pushdecl.  */
    9593       879409 :               gcc_checking_assert (slot && *slot == ns);
    9594              :             }
    9595              :           else
    9596              :             {
    9597              :               /* pushdecl could have expanded the hash table, so
    9598              :                  slot might be invalid.  */
    9599           39 :               slot = find_namespace_slot (current_namespace, name);
    9600           39 :               gcc_checking_assert (slot);
    9601              :             }
    9602       879448 :           make_namespace_finish (ns, slot);
    9603              :         }
    9604              :     }
    9605              : 
    9606      7141512 :   if (ns)
    9607              :     {
    9608              :       /* A public namespace is exported only if explicitly marked, or
    9609              :          it contains exported entities.  */
    9610      7141512 :       if (module_exporting_p ())
    9611              :         {
    9612        10753 :           if (TREE_PUBLIC (ns))
    9613        10710 :             DECL_MODULE_EXPORT_P (ns) = true;
    9614           43 :           else if (!header_module_p ())
    9615              :             {
    9616           12 :               if (name)
    9617              :                 {
    9618            6 :                   auto_diagnostic_group d;
    9619            6 :                   error_at (input_location, "exporting namespace %qD with "
    9620              :                             "internal linkage", ns);
    9621            6 :                   inform (input_location, "%qD has internal linkage because "
    9622              :                           "it was declared in an unnamed namespace", ns);
    9623            6 :                 }
    9624              :               else
    9625            6 :                 error_at (input_location, "exporting unnamed namespace");
    9626              :             }
    9627              :         }
    9628      7141512 :       if (module_purview_p ())
    9629        12006 :         DECL_MODULE_PURVIEW_P (ns) = true;
    9630              : 
    9631      7456787 :       if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
    9632              :         {
    9633            3 :           auto_diagnostic_group d;
    9634            3 :           error_at (input_location,
    9635              :                     "inline namespace must be specified at initial definition");
    9636            3 :           inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
    9637            3 :         }
    9638      7141512 :       resume_scope (NAMESPACE_LEVEL (ns));
    9639      7141512 :       current_namespace = ns;
    9640      7141512 :       count++;
    9641              :     }
    9642              : 
    9643     14283048 :   return count;
    9644      7141524 : }
    9645              : 
    9646              : /* Pop from the scope of the current namespace.  */
    9647              : 
    9648              : void
    9649      7194522 : pop_namespace (void)
    9650              : {
    9651      7194522 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9652              : 
    9653      7194522 :   gcc_assert (current_namespace != global_namespace);
    9654      7194522 :   current_namespace = CP_DECL_CONTEXT (current_namespace);
    9655              :   /* The binding level is not popped, as it might be re-opened later.  */
    9656      7194522 :   leave_scope ();
    9657      7194522 : }
    9658              : 
    9659              : /* An IMPORT is an import that is defining namespace NAME inside CTX.  Find or
    9660              :    create that namespace and add it to the container's binding-vector.   */
    9661              : 
    9662              : tree
    9663         2666 : add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
    9664              :                         bool inline_p, bool visible_p)
    9665              : {
    9666              :   // FIXME: Something is not correct about the VISIBLE_P handling.  We
    9667              :   // need to insert this namespace into
    9668              :   // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
    9669              :   // (b) The importing module's slot (always)
    9670              :   // (c) Do we need to put it in the CURRENT slot?  This is the
    9671              :   // confused piece.
    9672              : 
    9673         2666 :   tree *slot = find_namespace_slot (ctx, name, true);
    9674         2666 :   tree decl = reuse_namespace (slot, ctx, name);
    9675              : 
    9676              :   /* Creating and binding.  */
    9677         2666 :   if (!decl)
    9678              :     {
    9679         1654 :       decl = make_namespace (ctx, name, loc, inline_p);
    9680         1654 :       make_namespace_finish (decl, slot, true);
    9681              :     }
    9682         1012 :   else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
    9683              :     {
    9684            0 :       auto_diagnostic_group d;
    9685            0 :       error_at (loc, "%s namespace %qD conflicts with reachable definition",
    9686              :                 inline_p ? "inline" : "non-inline", decl);
    9687            0 :       inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
    9688              :               inline_p ? "non-inline" : "inline");
    9689            0 :     }
    9690              : 
    9691         2666 :   if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
    9692              :     {
    9693              :       /* See if we can extend the final slot.  */
    9694         1768 :       binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
    9695         1768 :       gcc_checking_assert (last->indices[0].span);
    9696              :       unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
    9697              : 
    9698         3358 :       while (--jx)
    9699         1768 :         if (last->indices[jx].span)
    9700              :           break;
    9701         1768 :       tree final = last->slots[jx];
    9702         1768 :       if (visible_p == !STAT_HACK_P (final)
    9703         1538 :           && MAYBE_STAT_DECL (final) == decl
    9704          143 :           && last->indices[jx].base + last->indices[jx].span == import
    9705         1908 :           && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
    9706              :               || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
    9707              :                   && jx >= BINDING_SLOTS_FIXED)))
    9708              :         {
    9709          140 :           last->indices[jx].span++;
    9710          140 :           return decl;
    9711              :         }
    9712              :     }
    9713              : 
    9714              :   /* Append a new slot.  */
    9715         2526 :   tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
    9716              : 
    9717         2526 :   gcc_assert (!*mslot);
    9718         2526 :   *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
    9719              : 
    9720         2526 :   return decl;
    9721              : }
    9722              : 
    9723              : /* Pop off extraneous binding levels left over due to syntax errors.
    9724              :    We don't pop past namespaces, as they might be valid.  */
    9725              : 
    9726              : void
    9727        96549 : pop_everything (void)
    9728              : {
    9729        96549 :   if (ENABLE_SCOPE_CHECKING)
    9730              :     verbatim ("XXX entering %<pop_everything ()%>");
    9731        96567 :   while (!namespace_bindings_p ())
    9732              :     {
    9733           18 :       if (current_binding_level->kind == sk_class)
    9734            0 :         pop_nested_class ();
    9735              :       else
    9736           18 :         poplevel (0, 0, 0);
    9737              :     }
    9738        96549 :   if (ENABLE_SCOPE_CHECKING)
    9739              :     verbatim ("XXX leaving %<pop_everything ()%>");
    9740        96549 : }
    9741              : 
    9742              : /* Emit debugging information for using declarations and directives.
    9743              :    If input tree is overloaded fn then emit debug info for all
    9744              :    candidates.  */
    9745              : 
    9746              : void
    9747      8471830 : cp_emit_debug_info_for_using (tree t, tree context)
    9748              : {
    9749              :   /* Don't try to emit any debug information if we have errors.  */
    9750      8471830 :   if (seen_error ())
    9751              :     return;
    9752              : 
    9753              :   /* Do not supply context to imported_module_or_decl, if
    9754              :      it is a global namespace.  */
    9755      8467548 :   if (context == global_namespace)
    9756       172796 :     context = NULL_TREE;
    9757              : 
    9758      8467548 :   t = MAYBE_BASELINK_FUNCTIONS (t);
    9759              : 
    9760     18403013 :   for (lkp_iterator iter (t); iter; ++iter)
    9761              :     {
    9762      9935465 :       tree fn = *iter;
    9763              : 
    9764      9935465 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    9765              :         /* FIXME: Handle TEMPLATE_DECLs.  */
    9766       797863 :         continue;
    9767              : 
    9768              :       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
    9769              :          of a builtin function.  */
    9770     11917896 :       if (TREE_CODE (fn) == FUNCTION_DECL
    9771      7552689 :           && DECL_EXTERNAL (fn)
    9772     16680873 :           && fndecl_built_in_p (fn))
    9773      2780294 :         continue;
    9774              : 
    9775      6357308 :       if (building_stmt_list_p ())
    9776         1206 :         add_stmt (build_stmt (input_location, USING_STMT, fn));
    9777              :       else
    9778      6356102 :         debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
    9779              :                                               false, false);
    9780              :     }
    9781              : }
    9782              : 
    9783              : /* True if D is a local declaration in dependent scope.  Assumes that it is
    9784              :    (part of) the current lookup result for its name.  */
    9785              : 
    9786              : bool
    9787     60089847 : dependent_local_decl_p (tree d)
    9788              : {
    9789     60089847 :   if (!DECL_LOCAL_DECL_P (d))
    9790              :     return false;
    9791              : 
    9792        38534 :   cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
    9793        38534 :   cp_binding_level *l = b->scope;
    9794        97661 :   while (!l->this_entity)
    9795        59127 :     l = l->level_chain;
    9796        38534 :   return uses_template_parms (l->this_entity);
    9797              : }
    9798              : 
    9799              : 
    9800              : 
    9801              : #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.