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