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