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-11 19:44:49 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       397555 : stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
      65              : {
      66       397555 :   tree result = make_node (OVERLOAD);
      67              : 
      68              :   /* Mark this as a lookup, so we can tell this is a stat hack.  */
      69       397555 :   OVL_LOOKUP_P (result) = true;
      70       397555 :   STAT_DECL (result) = decl;
      71       397555 :   STAT_TYPE (result) = type;
      72       397555 :   return result;
      73              : }
      74              : 
      75              : /* Create a local binding level for NAME.  */
      76              : 
      77              : static cxx_binding *
      78    908735245 : create_local_binding (cp_binding_level *level, tree name)
      79              : {
      80    908735245 :   cxx_binding *binding = cxx_binding_make (NULL, NULL);
      81              : 
      82    908735245 :   LOCAL_BINDING_P (binding) = true;
      83    908735245 :   binding->scope = level;
      84    908735245 :   binding->previous = IDENTIFIER_BINDING (name);
      85              : 
      86    908735245 :   IDENTIFIER_BINDING (name) = binding;
      87              : 
      88    908735245 :   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  11526565683 : find_namespace_slot (tree ns, tree name, bool create_p = false)
      96              : {
      97  11526565683 :   tree *slot = DECL_NAMESPACE_BINDINGS (ns)
      98  22659252531 :     ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
      99              :                            create_p ? INSERT : NO_INSERT);
     100  11526565683 :   return slot;
     101              : }
     102              : 
     103              : static tree
     104        60288 : find_namespace_value (tree ns, tree name)
     105              : {
     106        60288 :   tree *b = find_namespace_slot (ns, name);
     107              : 
     108        60288 :   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       108494 : search_imported_binding_slot (tree *slot, unsigned ix)
     118              : {
     119       108494 :   gcc_assert (ix);
     120              : 
     121       108494 :   if (!*slot)
     122              :     return NULL;
     123              : 
     124       108494 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
     125              :     return NULL;
     126              : 
     127       108494 :   unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     128       108494 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
     129              : 
     130       108494 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     131              :     {
     132       108494 :       clusters--;
     133       108494 :       cluster++;
     134              :     }
     135              : 
     136       220074 :   while (clusters > 1)
     137              :     {
     138         3086 :       unsigned half = clusters / 2;
     139         3086 :       gcc_checking_assert (cluster[half].indices[0].span);
     140         3086 :       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       108494 :   if (clusters)
     150              :     /* Is it in this cluster?  */
     151       216165 :     for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     152              :       {
     153       216165 :         if (!cluster->indices[off].span)
     154              :           break;
     155       216165 :         if (cluster->indices[off].base > ix)
     156              :           break;
     157              : 
     158       216165 :         if (cluster->indices[off].base + cluster->indices[off].span > ix)
     159       108494 :           return &cluster->slots[off];
     160              :       }
     161              : 
     162              :   return NULL;
     163              : }
     164              : 
     165              : static void
     166        97373 : init_global_partition (binding_cluster *cluster, tree decl)
     167              : {
     168        97373 :   bool named = true;
     169              : 
     170        97373 :   if (header_module_p ())
     171              :     named = false;
     172        97295 :   else if (TREE_PUBLIC (decl)
     173        54059 :            && TREE_CODE (decl) == NAMESPACE_DECL
     174        98149 :            && !DECL_NAMESPACE_ALIAS (decl))
     175              :     named = false;
     176        96448 :   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        97373 :     mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
     187              : 
     188        97373 :   if (*mslot)
     189        20629 :     decl = ovl_make (decl, *mslot);
     190        97373 :   *mslot = decl;
     191              : 
     192        97373 :   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        97373 : }
     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    389986991 : get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
     211              : {
     212    389986991 :   gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
     213              : 
     214              :   /* An assumption is that the fixed slots all reside in one cluster.  */
     215    389986991 :   gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
     216              : 
     217    389986991 :   if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
     218              :     {
     219    389848135 :       if (ix == BINDING_SLOT_CURRENT)
     220              :         /* The current TU can just use slot directly.  */
     221              :         return slot;
     222              : 
     223       195357 :       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       195357 :       bool partition_slot = true;
     230       195357 :       unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
     231              :                         + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
     232              :                        / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     233       195357 :       tree new_vec = make_binding_vec (name, want);
     234       195357 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
     235       195357 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
     236              : 
     237              :       /* Initialize the fixed slots.  */
     238       586071 :       for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
     239              :         {
     240       390714 :           cluster[0].indices[jx].base = 0;
     241       390714 :           cluster[0].indices[jx].span = 1;
     242       390714 :           cluster[0].slots[jx] = NULL_TREE;
     243              :         }
     244              : 
     245       195357 :       if (partition_slot)
     246              :         {
     247       195357 :           unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     248       195357 :           unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
     249       195357 :           cluster[ind].indices[off].base = 0;
     250       195357 :           cluster[ind].indices[off].span = 1;
     251       195357 :           cluster[ind].slots[off] = NULL_TREE;
     252              :         }
     253              : 
     254       195357 :       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        76744 :           if (tree type = strip_using_decl (MAYBE_STAT_TYPE (orig)))
     261           17 :             init_global_partition (cluster, type);
     262              : 
     263       174100 :           for (ovl_iterator iter (strip_using_decl (MAYBE_STAT_DECL (orig)));
     264       174100 :                iter; ++iter)
     265              :             {
     266        97356 :               tree decl = *iter;
     267              : 
     268              :               /* Internal linkage entities are in deduplicateable.  */
     269        97356 :               init_global_partition (cluster, decl);
     270              :             }
     271              : 
     272        76744 :           if (cluster[0].slots[BINDING_SLOT_GLOBAL]
     273        76744 :               && !(TREE_CODE (orig) == NAMESPACE_DECL
     274        77612 :                    && !DECL_NAMESPACE_ALIAS (orig)))
     275              :             {
     276              :               /* Note that we had some GMF entries.  */
     277        75883 :               if (!STAT_HACK_P (orig))
     278        75856 :                 orig = stat_hack (orig);
     279              : 
     280        75883 :               MODULE_BINDING_GLOBAL_P (orig) = true;
     281              :             }
     282              : 
     283        76744 :           cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
     284              :         }
     285              : 
     286       195357 :       *slot = new_vec;
     287       195357 :     }
     288              :   else
     289       138856 :     gcc_checking_assert (create >= 0);
     290              : 
     291       334213 :   unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     292       334213 :   binding_cluster &cluster
     293       334213 :     = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     294              : 
     295              :   /* There must always be slots for these indices  */
     296       334213 :   gcc_checking_assert (cluster.indices[off].span == 1
     297              :                        && !cluster.indices[off].base
     298              :                        && !cluster.slots[off].is_lazy ());
     299              : 
     300       334213 :   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       192952 : append_imported_binding_slot (tree *slot, tree name, unsigned ix)
     308              : {
     309       192952 :   gcc_checking_assert (ix);
     310              : 
     311       192952 :   if (!*slot ||  TREE_CODE (*slot) != BINDING_VECTOR)
     312              :     /* Make an initial module vector.  */
     313       179296 :     get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
     314        13656 :   else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
     315        13656 :            ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
     316              :     /* There is space in the last cluster.  */;
     317        11916 :   else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
     318        11916 :            != 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        11916 :       unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     325        11916 :       unsigned want = (have * 3 + 1) / 2;
     326              : 
     327        11916 :       if (want > (unsigned short)~0)
     328              :         want = (unsigned short)~0;
     329              : 
     330        11916 :       tree new_vec = make_binding_vec (name, want);
     331        11916 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
     332        23832 :       BINDING_VECTOR_INTERNAL_DECLS (new_vec)
     333        11916 :         = BINDING_VECTOR_INTERNAL_DECLS (*slot);
     334        23832 :       BINDING_VECTOR_GLOBAL_DUPS_P (new_vec)
     335        11916 :         = BINDING_VECTOR_GLOBAL_DUPS_P (*slot);
     336        23832 :       BINDING_VECTOR_PARTITION_DUPS_P (new_vec)
     337        11916 :         = BINDING_VECTOR_PARTITION_DUPS_P (*slot);
     338        23832 :       memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
     339        23832 :               BINDING_VECTOR_CLUSTER_BASE (*slot),
     340        11916 :               have * sizeof (binding_cluster));
     341        11916 :       *slot = new_vec;
     342              :     }
     343              : 
     344       192952 :   binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
     345       373988 :   for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     346       373988 :     if (!last->indices[off].span)
     347              :       {
     348              :         /* Fill the free slot of the cluster.  */
     349       192952 :         last->indices[off].base = ix;
     350       192952 :         last->indices[off].span = 1;
     351       192952 :         last->slots[off] = NULL_TREE;
     352              :         /* Check monotonicity.  */
     353       192952 :         gcc_checking_assert (last[off ? 0 : -1]
     354              :                              .indices[off ? off - 1
     355              :                                       : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
     356              :                              .base < ix);
     357       192952 :         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   1319881353 : add_decl_to_level (cp_binding_level *b, tree decl)
     367              : {
     368   1319881353 :   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   1319881353 :   gcc_assert (b->names != decl);
     375              : 
     376              :   /* We build up the list in reverse order, and reverse it later if
     377              :      necessary.  */
     378   1319881353 :   TREE_CHAIN (decl) = b->names;
     379   1319881353 :   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   1319881353 :   if (b->kind == sk_namespace
     385   1319881353 :       && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     386    368904457 :           || (TREE_CODE (decl) == FUNCTION_DECL
     387    303389577 :               && (!TREE_PUBLIC (decl)
     388    303273519 :                   || decl_internal_context_p (decl)
     389    303272343 :                   || DECL_DECLARED_INLINE_P (decl)))))
     390     17185936 :     vec_safe_push (static_decls, decl);
     391   1319881353 : }
     392              : 
     393              : /* Find the binding for NAME in the local binding level B.  */
     394              : 
     395              : static cxx_binding *
     396    934736439 : find_local_binding (cp_binding_level *b, tree name)
     397              : {
     398    934736439 :   if (cxx_binding *binding = IDENTIFIER_BINDING (name))
     399            0 :     for (;; b = b->level_chain)
     400              :       {
     401      3819598 :         if (binding->scope == b)
     402              :           return binding;
     403              : 
     404              :         /* Cleanup contours are transparent to the language.  */
     405      3816424 :         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   1820544325 :   name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
     443   1820544325 :   : name (n), value (NULL_TREE), type (NULL_TREE),
     444   1820544325 :     want (w), tentative (false),
     445   1820544325 :     deduping (false), scopes (NULL), previous (NULL)
     446              :   {
     447   1820544325 :     preserve_state ();
     448              :   }
     449   1820544325 :   ~name_lookup ()
     450              :   {
     451   1820544325 :     gcc_checking_assert (!deduping);
     452   1820544325 :     restore_state ();
     453   1820544325 :   }
     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   1867750983 :   void dedup (bool state)
     462              :   {
     463   1867750983 :     if (deduping != state)
     464              :       {
     465    116415502 :         deduping = state;
     466    116415502 :         lookup_mark (value, state);
     467              :       }
     468   1867750983 :   }
     469              : 
     470              : protected:
     471  23554655831 :   static bool seen_p (tree scope)
     472              :   {
     473  23554655831 :     return LOOKUP_SEEN_P (scope);
     474              :   }
     475     58796375 :   static bool found_p (tree scope)
     476              :   {
     477     58796375 :     return LOOKUP_FOUND_P (scope);
     478              :   }
     479              : 
     480              :   void mark_seen (tree scope); /* Mark and add to scope vector. */
     481    476076942 :   static void mark_found (tree scope)
     482              :   {
     483    476076942 :     gcc_checking_assert (seen_p (scope));
     484    476076942 :     LOOKUP_FOUND_P (scope) = true;
     485    476076942 :   }
     486  11470355424 :   bool see_and_mark (tree scope)
     487              :   {
     488  11470355424 :     bool ret = seen_p (scope);
     489  11470355424 :     if (!ret)
     490   2459360033 :       mark_seen (scope);
     491  11185552975 :     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   1820544325 : name_lookup::preserve_state ()
     561              : {
     562   1820544325 :   previous = active;
     563   1820544325 :   if (previous)
     564              :     {
     565         1677 :       unsigned length = vec_safe_length (previous->scopes);
     566         1677 :       vec_safe_reserve (previous->scopes, length);
     567         7989 :       for (unsigned ix = length; ix--;)
     568              :         {
     569         6312 :           tree decl = (*previous->scopes)[ix];
     570              : 
     571         6312 :           gcc_checking_assert (LOOKUP_SEEN_P (decl));
     572         6312 :           LOOKUP_SEEN_P (decl) = false;
     573              : 
     574              :           /* Preserve the FOUND_P state on the interrupted lookup's
     575              :              stack.  */
     576         6312 :           if (LOOKUP_FOUND_P (decl))
     577              :             {
     578          968 :               LOOKUP_FOUND_P (decl) = false;
     579          968 :               previous->scopes->quick_push (decl);
     580              :             }
     581              :         }
     582              : 
     583         1677 :       tentative = previous->tentative;
     584              : 
     585              :       /* Unmark the outer partial lookup.  */
     586         1677 :       if (previous->deduping)
     587           12 :         lookup_mark (previous->value, false);
     588              :     }
     589              :   else
     590   1820542648 :     scopes = shared_scopes;
     591   1820544325 :   active = this;
     592   1820544325 : }
     593              : 
     594              : /* Restore the marking state of a lookup we interrupted.  */
     595              : 
     596              : void
     597   1820544325 : name_lookup::restore_state ()
     598              : {
     599   1820544325 :   gcc_checking_assert (!deduping);
     600              : 
     601              :   /* Unmark and empty this lookup's scope stack.  */
     602  14826641625 :   for (unsigned ix = vec_safe_length (scopes); ix--;)
     603              :     {
     604  11185552975 :       tree decl = scopes->pop ();
     605  11185552975 :       gcc_checking_assert (LOOKUP_SEEN_P (decl));
     606  11185552975 :       LOOKUP_SEEN_P (decl) = false;
     607  11185552975 :       LOOKUP_FOUND_P (decl) = false;
     608              :     }
     609              : 
     610   1820544325 :   active = previous;
     611   1820544325 :   if (previous)
     612              :     {
     613         1677 :       free (scopes);
     614              : 
     615         1677 :       unsigned length = vec_safe_length (previous->scopes);
     616         7989 :       for (unsigned ix = 0; ix != length; ix++)
     617              :         {
     618         7053 :           tree decl = (*previous->scopes)[ix];
     619         7053 :           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          968 :               do
     624              :                 {
     625          968 :                   tree decl = previous->scopes->pop ();
     626          968 :                   gcc_checking_assert (LOOKUP_SEEN_P (decl)
     627              :                                        && !LOOKUP_FOUND_P (decl));
     628          968 :                   LOOKUP_FOUND_P (decl) = true;
     629              :                 }
     630          968 :               while (++ix != length);
     631              :               break;
     632              :             }
     633              : 
     634         6312 :           gcc_checking_assert (!LOOKUP_FOUND_P (decl));
     635         6312 :           LOOKUP_SEEN_P (decl) = true;
     636              :         }
     637              : 
     638              :       /* Remark the outer partial lookup.  */
     639         1677 :       if (previous->deduping)
     640           12 :         lookup_mark (previous->value, true);
     641              :     }
     642              :   else
     643   1820542648 :     shared_scopes = scopes;
     644   1820544325 : }
     645              : 
     646              : void
     647  11185552975 : name_lookup::mark_seen (tree scope)
     648              : {
     649  11185552975 :   gcc_checking_assert (!seen_p (scope));
     650  11185552975 :   LOOKUP_SEEN_P (scope) = true;
     651  11185552975 :   vec_safe_push (scopes, scope);
     652  11185552975 : }
     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          700 : name_lookup::ambiguous (tree thing, tree current)
     672              : {
     673          700 :   if (TREE_CODE (current) != TREE_LIST)
     674              :     {
     675          529 :       current = build_tree_list (NULL_TREE, current);
     676          529 :       TREE_TYPE (current) = error_mark_node;
     677              :     }
     678          700 :   current = tree_cons (NULL_TREE, thing, current);
     679          700 :   TREE_TYPE (current) = error_mark_node;
     680              : 
     681          700 :   return current;
     682              : }
     683              : 
     684              : /* FNS is a new overload set to add to the exising set.  */
     685              : 
     686              : void
     687    651433001 : name_lookup::add_overload (tree fns)
     688              : {
     689    651433001 :   if (!deduping && TREE_CODE (fns) == OVERLOAD)
     690              :     {
     691    456773083 :       tree probe = fns;
     692    456773083 :       if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     693    456571077 :         probe = ovl_skip_hidden (probe);
     694    456773083 :       if (probe && TREE_CODE (probe) == OVERLOAD
     695    913546166 :           && OVL_DEDUP_P (probe))
     696              :         /* We're about to add something found by multiple paths, so need to
     697              :            engage deduping mode.  */
     698     30779844 :         dedup (true);
     699              :     }
     700              : 
     701    651433001 :   value = lookup_maybe_add (fns, value, deduping);
     702    651433001 : }
     703              : 
     704              : /* Add a NEW_VAL, a found value binding into the current value binding.  */
     705              : 
     706              : void
     707   1475227546 : name_lookup::add_value (tree new_val)
     708              : {
     709   1475227546 :   if (OVL_P (new_val) && (!value || OVL_P (value)))
     710    595013470 :     add_overload (new_val);
     711    880214076 :   else if (!value)
     712    879978301 :     value = new_val;
     713       235775 :   else if (value == new_val)
     714              :     ;
     715        13330 :   else if ((TREE_CODE (value) == TYPE_DECL
     716        13165 :             && TREE_CODE (new_val) == TYPE_DECL
     717        26492 :             && 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   1475227546 : }
     732              : 
     733              : /* Add a NEW_TYPE, a found type binding into the current type binding.  */
     734              : 
     735              : void
     736       417326 : name_lookup::add_type (tree new_type)
     737              : {
     738       417326 :   if (!type)
     739       417323 :     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       417326 : }
     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   1496873254 : name_lookup::process_binding (tree new_val, tree new_type)
     751              : {
     752              :   /* Did we really see a type? */
     753   1496873254 :   if (new_type
     754   1496873254 :       && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
     755              :     new_type = NULL_TREE;
     756              : 
     757   1496873254 :   new_val = strip_using_decl (new_val);
     758   1496873254 :   new_type = strip_using_decl (new_type);
     759              : 
     760              :   /* Do we really see a value? */
     761   1496873254 :   if (new_val)
     762   1475276787 :     switch (TREE_CODE (new_val))
     763              :       {
     764    263890639 :       case TEMPLATE_DECL:
     765              :         /* If we expect types or namespaces, and not templates,
     766              :            or this is not a template class.  */
     767    263890639 :         if (bool (want & LOOK_want::TYPE_NAMESPACE)
     768    263890639 :             && !DECL_TYPE_TEMPLATE_P (new_val))
     769              :           new_val = NULL_TREE;
     770              :         break;
     771    259320964 :       case TYPE_DECL:
     772    259320964 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
     773    259320964 :             || (new_type && bool (want & LOOK_want::TYPE)))
     774              :           new_val = NULL_TREE;
     775              :         break;
     776    265051464 :       case NAMESPACE_DECL:
     777    265051464 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
     778              :           new_val = NULL_TREE;
     779              :         break;
     780    687013720 :       default:
     781    687013720 :         if (bool (want & LOOK_want::TYPE_NAMESPACE))
     782              :           new_val = NULL_TREE;
     783              :       }
     784              : 
     785              :   if (!new_val)
     786              :     {
     787     21694296 :       new_val = new_type;
     788     21694296 :       new_type = NULL_TREE;
     789              :     }
     790              : 
     791              :   /* Merge into the lookup  */
     792     21694296 :   if (new_val)
     793   1475227546 :     add_value (new_val);
     794   1496873254 :   if (new_type)
     795       417326 :     add_type (new_type);
     796              : 
     797   1496873254 :   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        86269 : 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        86269 :   if (new_val && !new_type
     811        81683 :       && TREE_CODE (new_val) == NAMESPACE_DECL
     812        32062 :       && TREE_PUBLIC (new_val)
     813       118325 :       && !DECL_NAMESPACE_ALIAS (new_val))
     814              :     {
     815        32026 :       if (marker & 2)
     816              :         return marker;
     817        16605 :       marker |= 2;
     818              :     }
     819              : 
     820        70848 :   if (new_type || new_val)
     821        66337 :     marker |= process_binding (new_val, new_type);
     822              : 
     823              :   return marker;
     824              : }
     825              : 
     826              : /* Look in exactly namespace SCOPE.  */
     827              : 
     828              : bool
     829  10889912174 : name_lookup::search_namespace_only (tree scope)
     830              : {
     831  10889912174 :   bool found = false;
     832  10889912174 :   if (tree *binding = find_namespace_slot (scope, name))
     833              :     {
     834   1496862189 :       tree val = *binding;
     835   1496862189 :       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        55272 :           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        55272 :           unsigned orig_mod = 0;
     847        55272 :           bitmap orig_imp = visible_from_instantiation_origination (&orig_mod);
     848              : 
     849        55272 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
     850        55272 :           int marker = 0;
     851        55272 :           int dup_detect = 0;
     852              : 
     853        55272 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
     854              :             {
     855        30933 :               if (!deduping)
     856              :                 {
     857        30484 :                   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        30933 :               tree type = NULL_TREE;
     868        30933 :               tree value = bind;
     869              : 
     870        30933 :               if (STAT_HACK_P (bind))
     871              :                 {
     872        12803 :                   type = STAT_TYPE (bind);
     873        12803 :                   value = STAT_DECL (bind);
     874              : 
     875        12803 :                   if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     876              :                     {
     877        12736 :                       if (STAT_TYPE_HIDDEN_P (bind))
     878            0 :                         type = NULL_TREE;
     879        12736 :                       if (STAT_DECL_HIDDEN_P (bind))
     880              :                         value = NULL_TREE;
     881              :                       else
     882        12736 :                         value = ovl_skip_hidden (value);
     883              :                     }
     884              :                 }
     885        18130 :               else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     886        18124 :                 value = ovl_skip_hidden (value);
     887              : 
     888        30933 :               marker = process_module_binding (value, type, marker);
     889              :             }
     890              : 
     891              :           /* Scan the imported bindings.  */
     892        55272 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
     893        55272 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     894              :             {
     895        55272 :               ix--;
     896        55272 :               cluster++;
     897              :             }
     898              : 
     899              :           /* Do this in forward order, so we load modules in an order
     900              :              the user expects.  */
     901       113468 :           for (; ix--; cluster++)
     902       174588 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
     903              :               {
     904              :                 /* Are we importing this module?  */
     905       116392 :                 if (unsigned base = cluster->indices[jx].base)
     906        56409 :                   if (unsigned span = cluster->indices[jx].span)
     907        57378 :                     do
     908        57378 :                       if (bool (want & LOOK_want::ANY_REACHABLE)
     909        57287 :                           || bitmap_bit_p (imports, base)
     910        60572 :                           || (orig_imp && bitmap_bit_p (orig_imp, base)))
     911        55336 :                         goto found;
     912         2042 :                     while (++base, --span);
     913        61056 :                 continue;
     914              : 
     915        55336 :               found:;
     916              :                 /* Is it loaded?  */
     917        55336 :                 unsigned mod = cluster->indices[jx].base;
     918        55336 :                 if (cluster->slots[jx].is_lazy ())
     919              :                   {
     920         5719 :                     gcc_assert (cluster->indices[jx].span == 1);
     921         5719 :                     lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
     922              :                   }
     923        55336 :                 tree bind = cluster->slots[jx];
     924        55336 :                 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        55336 :                 tree type = NULL_TREE;
     931              : 
     932              :                 /* If STAT_HACK_P is false, everything is visible, and
     933              :                    there's no duplication possibilities.  */
     934        55336 :                 if (STAT_HACK_P (bind))
     935              :                   {
     936        32328 :                     if (!deduping)
     937              :                       {
     938              :                         /* Do we need to engage deduplication?  */
     939        29794 :                         int dup = 0;
     940        29794 :                         if (MODULE_BINDING_GLOBAL_P (bind))
     941        27387 :                           dup |= 1;
     942        29794 :                         if (MODULE_BINDING_PARTITION_P (bind))
     943         1827 :                           dup |= 2;
     944        29794 :                         if (unsigned hit = dup_detect & dup)
     945              :                           {
     946        13361 :                             if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
     947        16156 :                                 || (hit & 2
     948          162 :                                     && BINDING_VECTOR_PARTITION_DUPS_P (val)))
     949        10845 :                               dedup (true);
     950              :                           }
     951        29794 :                         dup_detect |= dup;
     952              :                       }
     953              : 
     954        32328 :                     if (bool (want & LOOK_want::ANY_REACHABLE)
     955        32328 :                         || mod == orig_mod)
     956              :                       {
     957         7495 :                         type = STAT_TYPE (bind);
     958         7495 :                         bind = STAT_DECL (bind);
     959              :                       }
     960              :                     else
     961              :                       {
     962        24833 :                         if (STAT_TYPE_VISIBLE_P (bind))
     963         1857 :                           type = STAT_TYPE (bind);
     964        24833 :                         bind = STAT_VISIBLE (bind);
     965              :                       }
     966              :                   }
     967              : 
     968              :                 /* And process it.  */
     969        55336 :                 marker = process_module_binding (bind, type, marker);
     970        61056 :               }
     971        55272 :           found |= marker & 1;
     972              :         }
     973              :       else
     974              :         {
     975              :           /* Only a current module binding, visible from the current module.  */
     976   1496806917 :           tree bind = *binding;
     977   1496806917 :           tree value = bind, type = NULL_TREE;
     978              : 
     979   1496806917 :           if (STAT_HACK_P (bind))
     980              :             {
     981       466514 :               type = STAT_TYPE (bind);
     982       466514 :               value = STAT_DECL (bind);
     983              : 
     984       466514 :               if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     985              :                 {
     986       466282 :                   if (STAT_TYPE_HIDDEN_P (bind))
     987            3 :                     type = NULL_TREE;
     988       466282 :                   if (STAT_DECL_HIDDEN_P (bind))
     989              :                     value = NULL_TREE;
     990              :                   else
     991       465842 :                     value = ovl_skip_hidden (value);
     992              :                 }
     993              :             }
     994   1496340403 :           else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     995   1495685969 :             value = ovl_skip_hidden (value);
     996              : 
     997   1496806917 :           found |= process_binding (value, type);
     998              :         }
     999              :     }
    1000              : 
    1001  10889912174 :   return found;
    1002              : }
    1003              : 
    1004              : /* Conditionally look in namespace SCOPE and inline children.  */
    1005              : 
    1006              : bool
    1007   2187197725 : name_lookup::search_namespace (tree scope)
    1008              : {
    1009   2187197725 :   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   2187197725 :   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   2187197725 :   if (name)
    1019              :     /* Recursively look in its inline children.  */
    1020   2187195890 :     if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1021   2311125996 :       for (unsigned ix = inlinees->length (); ix--;)
    1022   1764527235 :         found |= search_namespace ((*inlinees)[ix]);
    1023              : 
    1024   2187197725 :   if (found)
    1025    439756185 :     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     12310088 : 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     12310088 :   if (found_p (scope))
    1039              :     return true;
    1040              : 
    1041     12310088 :   bool found = false;
    1042     12310088 :   if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
    1043      3353392 :     for (unsigned ix = usings->length (); ix--;)
    1044      1677075 :       found |= search_qualified (strip_using_decl ((*usings)[ix]), true);
    1045              : 
    1046              :   /* Look in its inline children.  */
    1047     12310088 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1048      5112572 :     for (unsigned ix = inlinees->length (); ix--;)
    1049      2634099 :       found |= search_usings ((*inlinees)[ix]);
    1050              : 
    1051     12310088 :   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    422670490 : name_lookup::search_qualified (tree scope, bool usings)
    1068              : {
    1069    422670490 :   bool found = false;
    1070              : 
    1071    422670490 :   if (seen_p (scope))
    1072            0 :     found = found_p (scope);
    1073              :   else
    1074              :     {
    1075    422670490 :       found = search_namespace (scope);
    1076    422670490 :       if (!found && usings)
    1077      9675989 :         found = search_usings (scope);
    1078              :     }
    1079              : 
    1080    422670490 :   dedup (false);
    1081              : 
    1082    422670490 :   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   8812800158 : name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
    1090              : {
    1091   8812800158 :   if (see_and_mark (scope))
    1092   8812800158 :     return;
    1093              : 
    1094              :   /* Record it.  */
    1095              :   tree common = scope;
    1096  17416040314 :   while (SCOPE_DEPTH (common) > depth)
    1097   8689847372 :     common = CP_DECL_CONTEXT (common);
    1098   8726192942 :   queue.safe_push (using_pair (common, scope));
    1099              : 
    1100              :   /* Queue its inline children.  */
    1101   8726192942 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1102   8623692603 :     for (unsigned ix = inlinees->length (); ix--;)
    1103   6580636097 :       queue_namespace (queue, depth, (*inlinees)[ix]);
    1104              : 
    1105              :   /* Queue its using targets.  */
    1106   8726192942 :   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  13260853721 : name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
    1113              : {
    1114  13260853721 :   if (usings)
    1115    107514756 :     for (unsigned ix = usings->length (); ix--;)
    1116     53997815 :       queue_namespace (queue, depth, strip_using_decl ((*usings)[ix]));
    1117  13260853721 : }
    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   1335859244 : name_lookup::search_unqualified (tree scope, cp_binding_level *level)
    1127              : {
    1128   1335859244 :   using_queue queue;
    1129   1335859244 :   bool found = false;
    1130              : 
    1131              :   /* Queue local using-directives.  */
    1132   5870520023 :   for (; level->kind != sk_namespace; level = level->level_chain)
    1133   4534660779 :     queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
    1134              : 
    1135   4465872930 :   for (; !found; scope = CP_DECL_CONTEXT (scope))
    1136              :     {
    1137   2178166246 :       gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
    1138   2178166246 :       int depth = SCOPE_DEPTH (scope);
    1139              : 
    1140              :       /* Queue namespaces reachable from SCOPE. */
    1141   2178166246 :       queue_namespace (queue, depth, scope);
    1142              : 
    1143              :       /* Search every queued namespace where SCOPE is the common
    1144              :          ancestor.  Adjust the others.  */
    1145   2178166246 :       unsigned ix = 0;
    1146   2225061421 :       do
    1147              :         {
    1148   2225061421 :           using_pair &pair = queue[ix];
    1149  10976028226 :           while (pair.first == scope)
    1150              :             {
    1151   8702714449 :               found |= search_namespace_only (pair.second);
    1152   8702714449 :               pair = queue.pop ();
    1153   8702714449 :               if (ix == queue.length ())
    1154   2176809065 :                 goto done;
    1155              :             }
    1156              :           /* The depth is the same as SCOPE, find the parent scope.  */
    1157     48252356 :           if (SCOPE_DEPTH (pair.first) == depth)
    1158     48247964 :             pair.first = CP_DECL_CONTEXT (pair.first);
    1159     48252356 :           ix++;
    1160              :         }
    1161     96504712 :       while (ix < queue.length ());
    1162      1357181 :     done:;
    1163   2178166246 :       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   1565421329 :       if (bool (want & LOOK_want::HIDDEN_FRIEND))
    1173              :         break;
    1174              :     }
    1175              : 
    1176   1335859244 :   dedup (false);
    1177              : 
    1178   1335859244 :   return found;
    1179   1335859244 : }
    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     62502235 : name_lookup::add_fns (tree fns)
    1186              : {
    1187     62502235 :   if (!fns)
    1188              :     return;
    1189     56437296 :   else if (TREE_CODE (fns) == OVERLOAD)
    1190              :     {
    1191     27002804 :       if (TREE_TYPE (fns) != unknown_type_node)
    1192        85937 :         fns = OVL_FUNCTION (fns);
    1193              :     }
    1194     29434492 :   else if (!DECL_DECLARES_FUNCTION_P (fns))
    1195              :     return;
    1196              : 
    1197     56419516 :   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    209174903 : name_lookup::adl_namespace_fns (tree scope, bitmap imports,
    1209              :                                 bitmap inst_path, bitmap assocs)
    1210              : {
    1211    209174903 :   if (tree *binding = find_namespace_slot (scope, name))
    1212              :     {
    1213     41627542 :       tree val = *binding;
    1214     41627542 :       if (TREE_CODE (val) != BINDING_VECTOR)
    1215     41603242 :         add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
    1216              :       else
    1217              :         {
    1218              :           /* I presume the binding list is going to be sparser than
    1219              :              the import bitmap.  Hence iterate over the former
    1220              :              checking for bits set in the bitmap.  */
    1221        24300 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
    1222        24300 :           int dup_detect = 0;
    1223              : 
    1224        24300 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    1225              :             {
    1226              :               /* The current TU's bindings must be visible, we don't
    1227              :                  need to check the bitmaps.  */
    1228              : 
    1229        15184 :               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        15184 :               add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
    1243              :             }
    1244              : 
    1245              :           /* When doing tentative name lookup we only care about entities
    1246              :              in the current TU.  */
    1247        24300 :           if (tentative)
    1248              :             return;
    1249              : 
    1250              :           /* Scan the imported bindings.  */
    1251        24299 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
    1252        24299 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    1253              :             {
    1254        24299 :               ix--;
    1255        24299 :               cluster++;
    1256              :             }
    1257              : 
    1258              :           /* Do this in forward order, so we load modules in an order
    1259              :              the user expects.  */
    1260        50103 :           for (; ix--; cluster++)
    1261        77412 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    1262              :               {
    1263        51608 :                 int mod = cluster->indices[jx].base;
    1264              : 
    1265              :                 /* Functions are never on merged slots.  */
    1266        51608 :                 if (!mod || cluster->indices[jx].span != 1)
    1267        32821 :                   continue;
    1268              : 
    1269              :                 /* Is this slot accessible here?  */
    1270        18787 :                 bool visible = bitmap_bit_p (imports, mod);
    1271        18787 :                 bool on_inst_path = inst_path && bitmap_bit_p (inst_path, mod);
    1272         9475 :                 if (!visible && !on_inst_path
    1273         9472 :                     && !(assocs && bitmap_bit_p (assocs, mod)))
    1274            3 :                   continue;
    1275              : 
    1276              :                 /* Is it loaded?  */
    1277        18784 :                 if (cluster->slots[jx].is_lazy ())
    1278           87 :                   lazy_load_binding (mod, scope, name, &cluster->slots[jx]);
    1279              : 
    1280        18784 :                 tree bind = cluster->slots[jx];
    1281        18784 :                 if (!bind)
    1282              :                   /* Load errors could mean there's nothing here.  */
    1283            0 :                   continue;
    1284              : 
    1285        18784 :                 if (STAT_HACK_P (bind))
    1286              :                   {
    1287        18691 :                     if (!deduping)
    1288              :                       {
    1289              :                         /* Do we need to engage deduplication?  */
    1290         9333 :                         int dup = 0;
    1291         9333 :                         if (MODULE_BINDING_GLOBAL_P (bind))
    1292         9270 :                           dup |= 1;
    1293         9333 :                         if (MODULE_BINDING_PARTITION_P (bind))
    1294            3 :                           dup |= 2;
    1295         9333 :                         if (unsigned hit = dup_detect & dup)
    1296         4610 :                           if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
    1297         5683 :                               || (hit & 2
    1298            3 :                                   && BINDING_VECTOR_PARTITION_DUPS_P (val)))
    1299         3540 :                             dedup (true);
    1300         9333 :                         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        18691 :                     if (on_inst_path)
    1307              :                       {
    1308              :                         /* If there are any internal functions visible, naming
    1309              :                            them outside that module is ill-formed.  */
    1310         9312 :                         auto_diagnostic_group d;
    1311         9312 :                         if (MODULE_BINDING_INTERNAL_DECLS_P (bind)
    1312         9312 :                             && pedwarn (input_location, OPT_Wexternal_tu_local,
    1313              :                                         "overload set for argument-dependent "
    1314              :                                         "lookup of %<%D::%D%> in module %qs "
    1315              :                                         "contains TU-local entities",
    1316              :                                         scope, name, module_name (mod, false)))
    1317              :                           {
    1318            6 :                             tree *tu_locals
    1319            6 :                               = BINDING_VECTOR_INTERNAL_DECLS (val)->get (mod);
    1320            6 :                             gcc_checking_assert (tu_locals && *tu_locals);
    1321           12 :                             for (tree t = *tu_locals; t; t = TREE_CHAIN (t))
    1322              :                               {
    1323            6 :                                 tree decl = TREE_VALUE (t);
    1324            6 :                                 inform (TU_LOCAL_ENTITY_LOCATION (decl),
    1325              :                                         "ignoring %qD declared here "
    1326              :                                         "with internal linkage",
    1327            6 :                                         TU_LOCAL_ENTITY_NAME (decl));
    1328              :                               }
    1329              :                           }
    1330         9312 :                         bind = STAT_DECL (bind);
    1331         9312 :                       }
    1332              :                     else
    1333         9379 :                       bind = STAT_VISIBLE (bind);
    1334              :                   }
    1335              : 
    1336        18784 :                 bind = ovl_skip_hidden (bind);
    1337        18784 :                 if (on_inst_path || visible)
    1338        18748 :                   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     48786226 : name_lookup::adl_class_fns (tree type)
    1359              : {
    1360              :   /* Add friends.  */
    1361     48786226 :   for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
    1362    148918227 :        list; list = TREE_CHAIN (list))
    1363    100132001 :     if (name == FRIEND_NAME (list))
    1364              :       {
    1365     10339978 :         tree context = NULL_TREE; /* Lazily computed.  */
    1366     31208905 :         for (tree friends = FRIEND_DECLS (list); friends;
    1367     20868927 :              friends = TREE_CHAIN (friends))
    1368              :           {
    1369     20868927 :             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     20868927 :             if (!context)
    1375     10339978 :               context = decl_namespace_context (type);
    1376        21153 :             if (cxx_dialect < cxx20
    1377     20868927 :                 ? CP_DECL_CONTEXT (fn) != context
    1378     20847774 :                 : !DECL_NAMESPACE_SCOPE_P (fn))
    1379          430 :               continue;
    1380              : 
    1381     20868497 :             dedup (true);
    1382              : 
    1383              :             /* Template specializations are never found by name lookup.
    1384              :                (Templates themselves can be found, but not template
    1385              :                specializations.)  */
    1386     20868497 :             if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
    1387         3436 :               continue;
    1388              : 
    1389     20865061 :             add_fns (fn);
    1390              :           }
    1391              :       }
    1392     48786226 : }
    1393              : 
    1394              : /* Find the containing non-inlined namespace, add it and all its
    1395              :    inlinees.  */
    1396              : 
    1397              : void
    1398    228772372 : name_lookup::adl_namespace (tree scope)
    1399              : {
    1400    396420208 :   if (see_and_mark (scope))
    1401              :     return;
    1402              : 
    1403              :   /* Look down into inline namespaces.  */
    1404    209174903 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1405    219770463 :     for (unsigned ix = inlinees->length (); ix--;)
    1406    167647836 :       adl_namespace ((*inlinees)[ix]);
    1407              : 
    1408    209174903 :   if (DECL_NAMESPACE_INLINE_P (scope))
    1409              :     /* Mark parent.  */
    1410    167647836 :     adl_namespace (CP_DECL_CONTEXT (scope));
    1411              : }
    1412              : 
    1413              : /* Adds the class and its friends to the lookup structure.  */
    1414              : 
    1415              : void
    1416     48953383 : 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     48953383 :   if (!CLASS_TYPE_P (type))
    1421              :     return;
    1422              : 
    1423     48953383 :   type = TYPE_MAIN_VARIANT (type);
    1424              : 
    1425     48953383 :   if (see_and_mark (type))
    1426              :     return;
    1427              : 
    1428     48792436 :   tree context = decl_namespace_context (type);
    1429     48792436 :   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     45256431 : name_lookup::adl_bases (tree type)
    1437              : {
    1438     45256431 :   adl_class_only (type);
    1439              : 
    1440              :   /* Process baseclasses.  */
    1441     45256431 :   if (tree binfo = TYPE_BINFO (type))
    1442              :     {
    1443              :       tree base_binfo;
    1444              :       int i;
    1445              : 
    1446     53413605 :       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    1447      8957266 :         adl_bases (BINFO_TYPE (base_binfo));
    1448              :     }
    1449     45256431 : }
    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     46569101 : name_lookup::adl_class (tree type)
    1469              : {
    1470              :   /* Backend build structures, such as __builtin_va_list, aren't
    1471              :      affected by all this.  */
    1472     46569101 :   if (!CLASS_TYPE_P (type))
    1473              :     return;
    1474              : 
    1475     46486287 :   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     46486287 :   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     36299165 :   if (!tentative)
    1486     36293915 :     complete_type (type);
    1487              : 
    1488     36299165 :   adl_bases (type);
    1489     36299165 :   mark_found (type);
    1490              : 
    1491     36299165 :   if (TYPE_CLASS_SCOPE_P (type))
    1492      1783132 :     adl_class_only (TYPE_CONTEXT (type));
    1493              : 
    1494              :   /* Process template arguments.  */
    1495     36299165 :   if (CLASSTYPE_TEMPLATE_INFO (type)
    1496     36299165 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
    1497              :     {
    1498     21409971 :       tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1499     60485505 :       for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
    1500     39075534 :         adl_template_arg (TREE_VEC_ELT (list, i));
    1501              :     }
    1502              : }
    1503              : 
    1504              : void
    1505     24983950 : name_lookup::adl_enum (tree type)
    1506              : {
    1507     24983950 :   type = TYPE_MAIN_VARIANT (type);
    1508     24983950 :   if (see_and_mark (type))
    1509              :     return;
    1510              : 
    1511     14194969 :   if (TYPE_CLASS_SCOPE_P (type))
    1512      1913814 :     adl_class_only (TYPE_CONTEXT (type));
    1513              :   else
    1514     12281155 :     adl_namespace (decl_namespace_context (type));
    1515              : }
    1516              : 
    1517              : void
    1518     87952607 : name_lookup::adl_expr (tree expr)
    1519              : {
    1520     87952607 :   if (!expr)
    1521              :     return;
    1522              : 
    1523     87952607 :   gcc_assert (!TYPE_P (expr));
    1524              : 
    1525     87952607 :   if (TREE_TYPE (expr) != unknown_type_node)
    1526              :     {
    1527     87945403 :       adl_type (unlowered_expr_type (expr));
    1528     87945403 :       return;
    1529              :     }
    1530              : 
    1531         7204 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1532          480 :     expr = TREE_OPERAND (expr, 0);
    1533         7204 :   if (TREE_CODE (expr) == COMPONENT_REF
    1534         7204 :       || TREE_CODE (expr) == OFFSET_REF)
    1535          444 :     expr = TREE_OPERAND (expr, 1);
    1536         7204 :   expr = MAYBE_BASELINK_FUNCTIONS (expr);
    1537              : 
    1538         7204 :   if (OVL_P (expr))
    1539        13934 :     for (lkp_iterator iter (expr); iter; ++iter)
    1540         7123 :       adl_type (TREE_TYPE (*iter));
    1541          393 :   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          393 :       adl_expr (TREE_OPERAND (expr, 0));
    1551              : 
    1552              :       /* Now the arguments.  */
    1553          393 :       if (tree args = TREE_OPERAND (expr, 1))
    1554          795 :         for (int ix = TREE_VEC_LENGTH (args); ix--;)
    1555          402 :           adl_template_arg (TREE_VEC_ELT (args, ix));
    1556              :     }
    1557              : }
    1558              : 
    1559              : void
    1560    119435204 : name_lookup::adl_type (tree type)
    1561              : {
    1562    130541256 :   if (!type)
    1563              :     return;
    1564              : 
    1565    130541256 :   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    130539626 :   else if (REFLECTION_TYPE_P (type))
    1573              :     {
    1574              :       /* The namespace std::meta is an associated namespace of
    1575              :          std::meta::info.  */
    1576        44790 :       adl_namespace (std_meta_node);
    1577        44790 :       return;
    1578              :     }
    1579              : 
    1580    130494836 :   switch (TREE_CODE (type))
    1581              :     {
    1582     45405099 :     case RECORD_TYPE:
    1583     45405099 :       if (TYPE_PTRMEMFUNC_P (type))
    1584              :         {
    1585        46097 :           adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
    1586        46097 :           return;
    1587              :         }
    1588              :       /* FALLTHRU */
    1589     46569101 :     case UNION_TYPE:
    1590     46569101 :       adl_class (type);
    1591     46569101 :       return;
    1592              : 
    1593       121082 :     case METHOD_TYPE:
    1594              :       /* The basetype is referenced in the first arg type, so just
    1595              :          fall through.  */
    1596       121082 :     case FUNCTION_TYPE:
    1597              :       /* Associate the parameter types.  */
    1598       552507 :       for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
    1599       431425 :         adl_type (TREE_VALUE (args));
    1600              :       /* FALLTHROUGH */
    1601              : 
    1602     11058313 :     case POINTER_TYPE:
    1603     11058313 :     case REFERENCE_TYPE:
    1604     11058313 :     case ARRAY_TYPE:
    1605     11058313 :       adl_type (TREE_TYPE (type));
    1606     11058313 :       return;
    1607              : 
    1608     24983950 :     case ENUMERAL_TYPE:
    1609     24983950 :       adl_enum (type);
    1610     24983950 :       return;
    1611              : 
    1612         4123 :     case LANG_TYPE:
    1613         4123 :       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     39280430 : 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     39280430 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    1645     39280430 :       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
    1646              :     ;
    1647     39280430 :   else if (TREE_CODE (arg) == TEMPLATE_DECL)
    1648              :     {
    1649         6161 :       tree ctx = CP_DECL_CONTEXT (arg);
    1650              : 
    1651              :       /* It's not a member template.  */
    1652         6161 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
    1653         6155 :         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     39274269 :   else if (ARGUMENT_PACK_P (arg))
    1660              :     {
    1661        59703 :       tree args = ARGUMENT_PACK_ARGS (arg);
    1662        59703 :       int i, len = TREE_VEC_LENGTH (args);
    1663       264197 :       for (i = 0; i < len; ++i)
    1664       204494 :         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     39214566 :   else if (TYPE_P (arg))
    1669     31049543 :     adl_type (arg);
    1670     39280430 : }
    1671              : 
    1672              : /* Perform ADL lookup.  FNS is the existing lookup result and ARGS are
    1673              :    the call arguments.  */
    1674              : 
    1675              : tree
    1676     48054473 : name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
    1677              : {
    1678     48054473 :   gcc_checking_assert (!vec_safe_length (scopes));
    1679              : 
    1680              :   /* Gather each associated entity onto the lookup's scope list.  */
    1681     48054473 :   unsigned ix;
    1682     48054473 :   tree arg;
    1683              : 
    1684    136460823 :   FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
    1685              :     /* OMP reduction operators put an ADL-significant type as the
    1686              :        first arg. */
    1687     88406350 :     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     88406270 :     else if (!tentative || !type_dependent_expression_p (arg))
    1693     87952214 :       adl_expr (arg);
    1694              : 
    1695     48054473 :   if (vec_safe_length (scopes))
    1696              :     {
    1697              :       /* Now do the lookups.  */
    1698     33620448 :       value = fns;
    1699     33620448 :       if (fns)
    1700     23937849 :         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     33620448 :       bitmap_obstack_initialize (NULL);
    1706     33620448 :       hash_map<tree, bitmap> ns_mod_assocs;
    1707     33620448 :       if (modules_p () && !tentative)
    1708              :         {
    1709      1254634 :           for (tree scope : scopes)
    1710       899890 :             if (TYPE_P (scope))
    1711              :               {
    1712       221256 :                 int mod = get_originating_module (TYPE_NAME (scope),
    1713              :                                                   /*global_m1=*/true);
    1714       221256 :                 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     33620448 :       bitmap inst_path = NULL;
    1731              :       /* VISIBLE is the regular import bitmap.  */
    1732     33620448 :       bitmap visible = visible_instantiation_path (&inst_path);
    1733              : 
    1734    305782756 :       for (unsigned ix = scopes->length (); ix--;)
    1735              :         {
    1736    272162308 :           tree scope = (*scopes)[ix];
    1737    272162308 :           if (TREE_CODE (scope) == NAMESPACE_DECL)
    1738              :             {
    1739    209174903 :               tree ctx = scope;
    1740    439350917 :               while (DECL_NAMESPACE_INLINE_P (ctx))
    1741    230176014 :                 ctx = CP_DECL_CONTEXT (ctx);
    1742    209174903 :               bitmap *assocs = ns_mod_assocs.get (ctx);
    1743    209174903 :               adl_namespace_fns (scope, visible, inst_path,
    1744              :                                  assocs ? *assocs : NULL);
    1745              :             }
    1746     62987405 :           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     48786226 :             adl_class_fns (scope);
    1751              :         }
    1752              : 
    1753     33620744 :       for (auto refs : ns_mod_assocs)
    1754          296 :         BITMAP_FREE (refs.second);
    1755     33620448 :       bitmap_obstack_release (NULL);
    1756              : 
    1757     33620448 :       fns = value;
    1758     33620448 :       dedup (false);
    1759     33620448 :     }
    1760              : 
    1761     48054473 :   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     48054473 : lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args,
    1779              :                       bool tentative/*=false*/)
    1780              : {
    1781     48054473 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    1782     48054473 :   name_lookup lookup (name);
    1783     48054473 :   lookup.tentative = tentative;
    1784     48054473 :   return lookup.search_adl (fns, args);
    1785     48054473 : }
    1786              : 
    1787              : /* FNS is an overload set of conversion functions.  Return the
    1788              :    overloads converting to TYPE.  */
    1789              : 
    1790              : static tree
    1791       668385 : extract_conversion_operator (tree fns, tree type)
    1792              : {
    1793       668385 :   tree convs = NULL_TREE;
    1794       668385 :   tree tpls = NULL_TREE;
    1795              : 
    1796      1908441 :   for (ovl_iterator iter (fns); iter; ++iter)
    1797              :     {
    1798       859250 :       if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
    1799       551080 :         convs = lookup_add (*iter, convs);
    1800              : 
    1801       859250 :       if (TREE_CODE (*iter) == TEMPLATE_DECL)
    1802       190065 :         tpls = lookup_add (*iter, tpls);
    1803              :     }
    1804              : 
    1805       668385 :   if (!convs)
    1806       255247 :     convs = tpls;
    1807              : 
    1808       668385 :   return convs;
    1809              : }
    1810              : 
    1811              : /* Binary search of (ordered) MEMBER_VEC for NAME.  */
    1812              : 
    1813              : static tree
    1814   3084075879 : member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
    1815              : {
    1816  18201186358 :   for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
    1817              :     {
    1818  12855395050 :       unsigned mid = (lo + hi) / 2;
    1819  12855395050 :       tree binding = (*member_vec)[mid];
    1820  25710790100 :       tree binding_name = OVL_NAME (binding);
    1821              : 
    1822  12855395050 :       if (binding_name > name)
    1823              :         hi = mid;
    1824   6403042932 :       else if (binding_name < name)
    1825   5580682482 :         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    845540221 : member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
    1837              : {
    1838   7418433662 :   for (int ix = member_vec->length (); ix--;)
    1839   6636325193 :     if (tree binding = (*member_vec)[ix])
    1840   6636325193 :       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   3115903672 : fields_linear_search (tree klass, tree name, bool want_type)
    1850              : {
    1851  40150047939 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1852              :     {
    1853  37206524804 :       tree decl = fields;
    1854              : 
    1855  37206524804 :       if (TREE_CODE (decl) == FIELD_DECL
    1856  37206524804 :           && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1857              :         {
    1858     35612253 :           if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
    1859              :             return temp;
    1860              :         }
    1861              : 
    1862  37206512408 :       if (DECL_NAME (decl) != name)
    1863  36987429401 :         continue;
    1864              : 
    1865    219083007 :       if (TREE_CODE (decl) == USING_DECL)
    1866              :         {
    1867       913529 :           decl = strip_using_decl (decl);
    1868       913529 :           if (is_overloaded_fn (decl))
    1869       407222 :             continue;
    1870              :         }
    1871              : 
    1872    218675785 :       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    218675751 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1878              :         /* Functions are found separately.  */
    1879     45844047 :         continue;
    1880              : 
    1881    172831704 :       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     35612253 : search_anon_aggr (tree anon, tree name, bool want_type)
    1960              : {
    1961     35612253 :   gcc_assert (COMPLETE_TYPE_P (anon));
    1962     35612253 :   tree ret = get_class_binding_direct (anon, name, want_type);
    1963     35612253 :   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   6262925054 : get_class_binding_direct (tree klass, tree name, bool want_type)
    1975              : {
    1976   6262925054 :   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   6262925054 :   bool conv_op = IDENTIFIER_CONV_OP_P (name);
    1981   6262925054 :   tree lookup = conv_op ? conv_op_identifier : name;
    1982   6262925054 :   tree val = NULL_TREE;
    1983   6262925054 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    1984              : 
    1985   6262925054 :   if (COMPLETE_TYPE_P (klass) && member_vec)
    1986              :     {
    1987   3083915187 :       val = member_vec_binary_search (member_vec, lookup);
    1988   3083915187 :       if (!val)
    1989              :         ;
    1990    822200192 :       else if (TREE_CODE (val) == OVERLOAD
    1991    822200192 :                && 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    822200031 :       else if (STAT_HACK_P (val))
    2035          201 :         val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
    2036    822199830 :       else if (want_type && !DECL_DECLARES_TYPE_P (val))
    2037              :         val = NULL_TREE;
    2038              :     }
    2039              :   else
    2040              :     {
    2041   3179009867 :       if (member_vec && !want_type)
    2042    845540221 :         val = member_vec_linear_search (member_vec, lookup);
    2043              : 
    2044   3179009867 :       if (id_equal (lookup, "_") && !want_type)
    2045          685 :         val = name_independent_linear_search (val, klass, lookup);
    2046   3179009182 :       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   3115902208 :         if (tree field_val = fields_linear_search (klass, lookup, want_type))
    2050              :           {
    2051    172379073 :             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   6262925054 :   if (val && conv_op)
    2061              :     {
    2062     33724360 :       gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
    2063     33724360 :       val = OVL_CHAIN (val);
    2064     33724360 :       if (tree type = TREE_TYPE (name))
    2065       668385 :         val = extract_conversion_operator (val, type);
    2066              :     }
    2067              : 
    2068   6262925054 :   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   3393022015 : maybe_lazily_declare (tree klass, tree name)
    2076              : {
    2077              :   /* See big comment about module_state::write_pendings regarding adding
    2078              :      a check bit.  */
    2079   3393022015 :   if (modules_p ())
    2080     11268198 :     lazy_load_pendings (TYPE_NAME (klass));
    2081              : 
    2082              :   /* Lazily declare functions, if we're going to search these.  */
    2083   3393022015 :   if (IDENTIFIER_CTOR_P (name))
    2084              :     {
    2085     85882829 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
    2086      3629301 :         lazily_declare_fn (sfk_constructor, klass);
    2087     85882829 :       if (CLASSTYPE_LAZY_COPY_CTOR (klass))
    2088      5764771 :         lazily_declare_fn (sfk_copy_constructor, klass);
    2089     85882829 :       if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
    2090      4716409 :         lazily_declare_fn (sfk_move_constructor, klass);
    2091              :     }
    2092   3307139186 :   else if (IDENTIFIER_DTOR_P (name))
    2093              :     {
    2094     78869766 :       if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
    2095      6684556 :         lazily_declare_fn (sfk_destructor, klass);
    2096              :     }
    2097   3228269420 :   else if (name == assign_op_identifier)
    2098              :     {
    2099     13834593 :       if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
    2100      1581249 :         lazily_declare_fn (sfk_copy_assignment, klass);
    2101     13834593 :       if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
    2102      1064559 :         lazily_declare_fn (sfk_move_assignment, klass);
    2103              :     }
    2104   3393022015 : }
    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   5723256650 : get_class_binding (tree klass, tree name, bool want_type /*=false*/)
    2112              : {
    2113   5723256650 :   klass = complete_type (klass);
    2114              : 
    2115   5723256650 :   if (COMPLETE_TYPE_P (klass))
    2116   3393022015 :     maybe_lazily_declare (klass, name);
    2117              : 
    2118   5723256650 :   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    342042667 : find_member_slot (tree klass, tree name)
    2128              : {
    2129    342042667 :   bool complete_p = COMPLETE_TYPE_P (klass);
    2130              : 
    2131    342042667 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2132    342042667 :   if (!member_vec)
    2133              :     {
    2134     24535808 :       vec_alloc (member_vec, 8);
    2135     24535808 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2136     24535808 :       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      2161158 :         member_vec = set_class_bindings (klass, 6);
    2142              :     }
    2143              : 
    2144    342042667 :   if (IDENTIFIER_CONV_OP_P (name))
    2145      2668694 :     name = conv_op_identifier;
    2146              : 
    2147    342042667 :   unsigned ix, length = member_vec->length ();
    2148   4121620079 :   for (ix = 0; ix < length; ix++)
    2149              :     {
    2150   3946722468 :       tree *slot = &(*member_vec)[ix];
    2151   7893444936 :       tree fn_name = OVL_NAME (*slot);
    2152              : 
    2153   3946722468 :       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    158794810 :           gcc_checking_assert (OVL_P (*slot));
    2161    158794810 :           if (name == conv_op_identifier)
    2162              :             {
    2163       635096 :               gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
    2164              :               /* Skip the conv-op marker. */
    2165       635096 :               slot = &OVL_CHAIN (*slot);
    2166              :             }
    2167    158794810 :           return slot;
    2168              :         }
    2169              : 
    2170   3787927658 :       if (complete_p && fn_name > name)
    2171              :         break;
    2172              :     }
    2173              : 
    2174              :   /* No slot found, add one if the class is complete.  */
    2175    183247857 :   if (complete_p)
    2176              :     {
    2177              :       /* Do exact allocation, as we don't expect to add many.  */
    2178     32460045 :       gcc_assert (name != conv_op_identifier);
    2179     32460045 :       vec_safe_reserve_exact (member_vec, 1);
    2180     32460045 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2181     32460045 :       member_vec->quick_insert (ix, NULL_TREE);
    2182     32460045 :       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    150787794 : add_member_slot (tree klass, tree name)
    2193              : {
    2194    150787794 :   gcc_assert (!COMPLETE_TYPE_P (klass));
    2195              : 
    2196    150787794 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2197    150787794 :   vec_safe_push (member_vec, NULL_TREE);
    2198    150787794 :   CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2199              : 
    2200    150787794 :   tree *slot = &member_vec->last ();
    2201    150787794 :   if (IDENTIFIER_CONV_OP_P (name))
    2202              :     {
    2203              :       /* Install the marker prefix.  */
    2204      2033598 :       *slot = ovl_make (conv_op_marker, NULL_TREE);
    2205      2033598 :       slot = &OVL_CHAIN (*slot);
    2206              :     }
    2207              : 
    2208    150787794 :   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   5892953650 : member_name_cmp (const void *a_p, const void *b_p)
    2218              : {
    2219   5892953650 :   tree a = *(const tree *)a_p;
    2220   5892953650 :   tree b = *(const tree *)b_p;
    2221   5892953650 :   tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
    2222   5892953650 :   tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
    2223              : 
    2224   5892953650 :   gcc_checking_assert (name_a && name_b);
    2225   5892953650 :   if (name_a != name_b)
    2226   8653957653 :     return name_a < name_b ? -1 : +1;
    2227              : 
    2228     18580464 :   if (name_a == conv_op_identifier)
    2229              :     {
    2230              :       /* Strip the conv-op markers. */
    2231      1104898 :       gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
    2232              :                            && OVL_FUNCTION (b) == conv_op_marker);
    2233       552449 :       a = OVL_CHAIN (a);
    2234       552449 :       b = OVL_CHAIN (b);
    2235              :     }
    2236              : 
    2237     18580464 :   if (TREE_CODE (a) == OVERLOAD)
    2238      6576347 :     a = OVL_FUNCTION (a);
    2239     18580464 :   if (TREE_CODE (b) == OVERLOAD)
    2240      6142489 :     b = OVL_FUNCTION (b);
    2241              : 
    2242     18580464 :   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     18580158 :   if (TREE_CODE (a) != TREE_CODE (b))
    2263              :     {
    2264              :       /* If one of them is a TYPE_DECL, it loses.  */
    2265     16932521 :       if (TREE_CODE (a) == TYPE_DECL)
    2266              :         return +1;
    2267     16932188 :       else if (TREE_CODE (b) == TYPE_DECL)
    2268              :         return -1;
    2269              : 
    2270              :       /* If one of them is a USING_DECL, it loses.  */
    2271     16931846 :       if (TREE_CODE (a) == USING_DECL)
    2272              :         return +1;
    2273      8755185 :       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      1647637 :   if (DECL_UID (a) != DECL_UID (b))
    2294      1647619 :     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     10973568 : resort_member_name_cmp (const void *a_p, const void *b_p)
    2310              : {
    2311     10973568 :   tree a = *(const tree *)a_p;
    2312     10973568 :   tree b = *(const tree *)b_p;
    2313     21947136 :   tree name_a = OVL_NAME (a);
    2314     21947136 :   tree name_b = OVL_NAME (b);
    2315              : 
    2316     10973568 :   resort_data.new_value (&name_a, &name_a, resort_data.cookie);
    2317     10973568 :   resort_data.new_value (&name_b, &name_b, resort_data.cookie);
    2318              : 
    2319     10973568 :   gcc_checking_assert (name_a != name_b);
    2320              : 
    2321     10973568 :   return name_a < name_b ? -1 : +1;
    2322              : }
    2323              : 
    2324              : /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered.  */
    2325              : 
    2326              : void
    2327        56634 : resort_type_member_vec (void *obj, void */*orig_obj*/,
    2328              :                         gt_pointer_operator new_value, void* cookie)
    2329              : {
    2330        56634 :   if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
    2331              :     {
    2332        56634 :       resort_data.new_value = new_value;
    2333        56634 :       resort_data.cookie = cookie;
    2334        56634 :       member_vec->qsort (resort_member_name_cmp);
    2335              :     }
    2336        56634 : }
    2337              : 
    2338              : /* Recursively count the number of fields in KLASS, including anonymous
    2339              :    union members.  */
    2340              : 
    2341              : static unsigned
    2342     71651573 : count_class_fields (tree klass)
    2343              : {
    2344     71651573 :   unsigned n_fields = 0;
    2345              : 
    2346    586153012 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2347    514501439 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2348              :       /* Functions are dealt with separately.  */;
    2349    242631562 :     else if (TREE_CODE (fields) == FIELD_DECL
    2350    242631562 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2351       290429 :       n_fields += count_class_fields (TREE_TYPE (fields));
    2352    242341133 :     else if (DECL_NAME (fields))
    2353              :       {
    2354    217980270 :         if (TYPE_DECL_WAS_UNNAMED (fields))
    2355              :           /* Ignore DECL_NAME given to unnamed TYPE_DECLs named for linkage
    2356              :              purposes.  */
    2357           18 :           continue;
    2358    217980252 :         n_fields += 1;
    2359              :       }
    2360              : 
    2361     71651573 :   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     25331421 : member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
    2369              : {
    2370    431665891 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2371    406334470 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2372              :       /* Functions are handled separately.  */;
    2373    134464605 :     else if (TREE_CODE (fields) == FIELD_DECL
    2374    134464605 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2375       280324 :       member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
    2376    134184281 :     else if (DECL_NAME (fields))
    2377              :       {
    2378    129254495 :         tree field = fields;
    2379              :         /* Mark a conv-op USING_DECL with the conv-op-marker.  */
    2380    129254495 :         if (TREE_CODE (field) == USING_DECL
    2381    129254495 :             && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
    2382       138089 :           field = ovl_make (conv_op_marker, field);
    2383    129116406 :         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    129254482 :         member_vec->quick_push (field);
    2388              :       }
    2389     25331421 : }
    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     25051118 : member_vec_dedup (vec<tree, va_gc> *member_vec)
    2417              : {
    2418     25051118 :   unsigned len = member_vec->length ();
    2419     25051118 :   unsigned store = 0;
    2420              : 
    2421     25051118 :   if (!len)
    2422              :     return;
    2423              : 
    2424     50102232 :   tree name = OVL_NAME ((*member_vec)[0]);
    2425    299912234 :   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    554316066 :       for (jx = ix; jx < len; jx++)
    2434              :         {
    2435    529264950 :           tree next = (*member_vec)[jx];
    2436    529264950 :           if (jx != ix)
    2437              :             {
    2438    254403832 :               tree next_name = OVL_NAME (next);
    2439    254403832 :               if (next_name != name)
    2440              :                 {
    2441              :                   name = next_name;
    2442              :                   break;
    2443              :                 }
    2444              :             }
    2445              : 
    2446    279454948 :           if (IDENTIFIER_CONV_OP_P (name))
    2447              :             {
    2448      2171687 :               marker = next;
    2449      2171687 :               next = OVL_CHAIN (next);
    2450              :             }
    2451              : 
    2452    279454948 :           if (TREE_CODE (next) == USING_DECL)
    2453              :             {
    2454     12251263 :               if (IDENTIFIER_CTOR_P (name))
    2455              :                 /* Dependent inherited ctor. */
    2456       295721 :                 continue;
    2457              : 
    2458     11955542 :               next = strip_using_decl (next);
    2459     11955542 :               if (TREE_CODE (next) == USING_DECL)
    2460              :                 {
    2461      9872517 :                   to_using = next;
    2462      9872517 :                   continue;
    2463              :                 }
    2464              : 
    2465      2083025 :               if (is_overloaded_fn (next))
    2466      1753687 :                 continue;
    2467              :             }
    2468              : 
    2469    267533023 :           if (DECL_DECLARES_TYPE_P (next))
    2470              :             {
    2471     75295269 :               to_type = next;
    2472     75295269 :               continue;
    2473              :             }
    2474              : 
    2475    192237754 :           if (name_independent_decl_p (next))
    2476          160 :             name_independent = jx + 1;
    2477    192237594 :           else if (!current)
    2478    192055665 :             current = next;
    2479              :         }
    2480              : 
    2481    274861118 :       if (to_using)
    2482              :         {
    2483      9744966 :           if (!current)
    2484              :             current = to_using;
    2485              :           else
    2486      2015773 :             current = ovl_make (to_using, current);
    2487              :         }
    2488              : 
    2489    274861118 :       if (to_type)
    2490              :         {
    2491     75014813 :           if (!current)
    2492              :             current = to_type;
    2493              :           else
    2494          159 :             current = stat_hack (current, to_type);
    2495              :         }
    2496              : 
    2497    274861278 :       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    274861118 :       if (current)
    2509              :         {
    2510    274799610 :           if (marker)
    2511              :             {
    2512      2033598 :               OVL_CHAIN (marker) = current;
    2513      2033598 :               current = marker;
    2514              :             }
    2515    274799610 :           (*member_vec)[store++] = current;
    2516              :         }
    2517              :     }
    2518              : 
    2519     29706454 :   while (store++ < len)
    2520      4655338 :     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     71361120 : set_class_bindings (tree klass, int extra)
    2531              : {
    2532     71361120 :   unsigned n_fields = count_class_fields (klass);
    2533     71361120 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2534              : 
    2535     71361120 :   if (member_vec || n_fields >= 8 || extra < 0)
    2536              :     {
    2537              :       /* Append the new fields.  */
    2538     25051091 :       vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
    2539     25051091 :       member_vec_append_class_fields (member_vec, klass);
    2540              :     }
    2541              : 
    2542     71361120 :   if (member_vec)
    2543              :     {
    2544     25051091 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2545     25051091 :       member_vec->qsort (member_name_cmp);
    2546     25051091 :       member_vec_dedup (member_vec);
    2547              :     }
    2548              : 
    2549     71361120 :   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   5290738492 : query_oracle (tree name)
    2589              : {
    2590   5290738492 :   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   1397449082 : cxx_binding_init (cxx_binding *binding, tree value, tree type)
    2617              : {
    2618   1397449082 :   binding->value = value;
    2619   1397449082 :   binding->type = type;
    2620   1397449082 :   binding->previous = NULL;
    2621              : }
    2622              : 
    2623              : /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
    2624              : 
    2625              : static cxx_binding *
    2626   1397449082 : cxx_binding_make (tree value, tree type)
    2627              : {
    2628   1397449082 :   cxx_binding *binding = free_bindings;
    2629              : 
    2630   1397449082 :   if (binding)
    2631   1387942380 :     free_bindings = binding->previous;
    2632              :   else
    2633      9506702 :     binding = ggc_alloc<cxx_binding> ();
    2634              : 
    2635              :   /* Clear flags by default.  */
    2636   1397449082 :   LOCAL_BINDING_P (binding) = false;
    2637   1397449082 :   INHERITED_VALUE_BINDING_P (binding) = false;
    2638   1397449082 :   HIDDEN_TYPE_BINDING_P (binding) = false;
    2639              : 
    2640   1397449082 :   cxx_binding_init (binding, value, type);
    2641              : 
    2642   1397449082 :   return binding;
    2643              : }
    2644              : 
    2645              : /* Put BINDING back on the free list.  */
    2646              : 
    2647              : static inline void
    2648   1397446217 : cxx_binding_free (cxx_binding *binding)
    2649              : {
    2650   1397446217 :   binding->scope = NULL;
    2651   1397446217 :   binding->previous = free_bindings;
    2652   1397446217 :   free_bindings = binding;
    2653    921734228 : }
    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    475714830 : new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
    2660              : {
    2661    475714830 :   cp_class_binding cb = {cxx_binding_make (value, type), name};
    2662    475714830 :   cxx_binding *binding = cb.base;
    2663    475714830 :   vec_safe_push (scope->class_shadowed, cb);
    2664    475714830 :   binding->scope = scope;
    2665    475714830 :   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    336042959 : push_binding (tree id, tree decl, cp_binding_level* level)
    2673              : {
    2674    336042959 :   cxx_binding *binding;
    2675              : 
    2676    336042959 :   if (level != class_binding_level)
    2677              :     {
    2678     12999007 :       binding = cxx_binding_make (decl, NULL_TREE);
    2679     12999007 :       binding->scope = level;
    2680              :     }
    2681              :   else
    2682    323043952 :     binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
    2683              : 
    2684              :   /* Now, fill in the binding information.  */
    2685    336042959 :   binding->previous = IDENTIFIER_BINDING (id);
    2686    336042959 :   LOCAL_BINDING_P (binding) = (level != class_binding_level);
    2687              : 
    2688              :   /* And put it on the front of the list of bindings for ID.  */
    2689    336042959 :   IDENTIFIER_BINDING (id) = binding;
    2690    336042959 : }
    2691              : 
    2692              : /* Remove the binding for DECL which should be the innermost binding
    2693              :    for ID.  */
    2694              : 
    2695              : void
    2696    945222573 : pop_local_binding (tree id, tree decl)
    2697              : {
    2698   1868242273 :   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    921734678 :   cxx_binding *binding = IDENTIFIER_BINDING (id);
    2706              : 
    2707              :   /* The name should be bound.  */
    2708    921734678 :   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    921734678 :   if (binding->value == decl)
    2715    921733854 :     binding->value = NULL_TREE;
    2716          824 :   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          674 :   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          673 :       gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
    2725          673 :       if (TREE_VALUE (binding->value) != decl)
    2726              :         {
    2727          261 :           binding->value = nreverse (binding->value);
    2728              :           /* Skip over TREE_LISTs added in pushdecl for check_local_shadow
    2729              :              detected declarations, formerly at the tail, now at the start
    2730              :              of the list.  */
    2731          270 :           while (TREE_PURPOSE (binding->value) == error_mark_node)
    2732            9 :             binding->value = TREE_CHAIN (binding->value);
    2733              :         }
    2734          673 :       gcc_assert (TREE_VALUE (binding->value) == decl);
    2735          673 :       binding->value = TREE_CHAIN (binding->value);
    2736          673 :       while (binding->value
    2737          768 :              && TREE_PURPOSE (binding->value) == error_mark_node)
    2738           95 :         binding->value = TREE_CHAIN (binding->value);
    2739              :     }
    2740              : 
    2741    921734678 :   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    921734228 :       IDENTIFIER_BINDING (id) = binding->previous;
    2746              : 
    2747              :       /* Add it to the free list.  */
    2748    921734228 :       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    208200595 : pop_bindings_and_leave_scope (void)
    2757              : {
    2758    475351755 :   for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
    2759              :     {
    2760    267151160 :       tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
    2761    534302320 :       tree name = OVL_NAME (decl);
    2762              : 
    2763    267151160 :       pop_local_binding (name, decl);
    2764              :     }
    2765              : 
    2766    208200595 :   leave_scope ();
    2767    208200595 : }
    2768              : 
    2769              : /* Strip non dependent using declarations. If DECL is dependent,
    2770              :    surreptitiously create a typename_type and return it.  */
    2771              : 
    2772              : tree
    2773   9146823695 : strip_using_decl (tree decl)
    2774              : {
    2775   9146823695 :   if (decl == NULL_TREE)
    2776              :     return NULL_TREE;
    2777              : 
    2778   6459476807 :   while (TREE_CODE (decl) == USING_DECL
    2779    119410007 :          && !DECL_DEPENDENT_P (decl)
    2780   6552075451 :          && (LIKELY (!cp_preserve_using_decl)
    2781         6402 :              || TREE_CODE (USING_DECL_DECLS (decl)) == NAMESPACE_DECL))
    2782     92598640 :     decl = USING_DECL_DECLS (decl);
    2783              : 
    2784     26811367 :   if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
    2785   6393689530 :       && 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       261408 :       decl = make_typename_type (USING_DECL_SCOPE (decl),
    2794       261408 :                                  DECL_NAME (decl),
    2795              :                                  typename_type, tf_error);
    2796       261408 :       if (decl != error_mark_node)
    2797       261408 :         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     24049734 : anticipated_builtin_p (tree ovl)
    2807              : {
    2808     24049734 :   return (TREE_CODE (ovl) == OVERLOAD
    2809     22025820 :           && OVL_HIDDEN_P (ovl)
    2810     31011562 :           && 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       211792 : supplement_binding (cxx_binding *binding, tree decl)
    2833              : {
    2834       211792 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    2835              : 
    2836       211792 :   tree bval = binding->value;
    2837       211792 :   bool ok = true;
    2838       211792 :   if (bval
    2839       211792 :       && TREE_CODE (bval) == TREE_LIST
    2840       211800 :       && name_independent_decl_p (TREE_VALUE (bval)))
    2841              :     bval = TREE_VALUE (bval);
    2842       211792 :   tree target_bval = strip_using_decl (bval);
    2843       211792 :   tree target_decl = strip_using_decl (decl);
    2844              : 
    2845        29492 :   if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
    2846        29309 :       && target_decl != target_bval
    2847       241092 :       && (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        29195 :           || (processing_template_decl
    2853         9223 :               && 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       211687 :   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       211687 :            || 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       423374 :            || anticipated_builtin_p (target_bval))
    2874            0 :     binding->value = decl;
    2875       211687 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2876        29567 :            && DECL_ARTIFICIAL (target_bval)
    2877        29534 :            && target_decl != target_bval
    2878       241212 :            && (TREE_CODE (target_decl) != TYPE_DECL
    2879        29345 :                || 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        29513 :       binding->type = bval;
    2888        29513 :       binding->value = decl;
    2889        29513 :       binding->value_is_inherited = false;
    2890              :     }
    2891       182174 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2892           54 :            && TREE_CODE (target_decl) == TYPE_DECL
    2893           54 :            && DECL_NAME (target_decl) == DECL_NAME (target_bval)
    2894           54 :            && binding->scope->kind != sk_class
    2895       182174 :            && (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       182174 :   else if (VAR_P (target_decl)
    2922            9 :            && VAR_P (target_bval)
    2923            9 :            && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
    2924       182180 :            && !DECL_CLASS_SCOPE_P (target_decl))
    2925              :     {
    2926            0 :       duplicate_decls (decl, binding->value);
    2927            0 :       ok = false;
    2928              :     }
    2929       182174 :   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       182174 :            && 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       182174 :   else if (TREE_CODE (bval) == USING_DECL
    2942       182174 :            && CONST_DECL_USING_P (decl))
    2943              :     /* Let the clone hide the using-decl that introduced it.  */
    2944       181975 :     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       423584 :   return ok;
    2967       211792 : }
    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      5764005 : matching_fn_p (tree one, tree two)
    3026              : {
    3027      5764005 :   if (TREE_CODE (one) != TREE_CODE (two))
    3028              :     return false;
    3029              : 
    3030      3411690 :   if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
    3031      3411690 :                   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   1281795210 : update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
    3059              :                 tree old, tree decl, bool hiding = false)
    3060              : {
    3061   1281795210 :   tree old_type = NULL_TREE;
    3062   1281795210 :   bool hide_type = false;
    3063   1281795210 :   bool hide_value = false;
    3064   1281795210 :   bool name_independent_p = false;
    3065              : 
    3066   1281795210 :   if (!slot)
    3067              :     {
    3068    908735806 :       old_type = binding->type;
    3069    908735806 :       hide_type = HIDDEN_TYPE_BINDING_P (binding);
    3070    908735806 :       if (!old_type)
    3071    908735803 :         hide_value = hide_type, hide_type = false;
    3072    908735806 :       name_independent_p = name_independent_decl_p (decl);
    3073              :     }
    3074    373059404 :   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   1281795210 :   tree to_val = decl;
    3082   1281795210 :   tree to_type = old_type;
    3083   1281795210 :   bool local_overload = false;
    3084              : 
    3085   1281795210 :   gcc_assert (!level || level->kind == sk_namespace ? !binding
    3086              :               : level->kind != sk_class && !slot);
    3087              : 
    3088   1281795210 :   if (old == error_mark_node)
    3089        98396 :     old = NULL_TREE;
    3090              : 
    3091   1281795210 :   tree old_bval = old;
    3092   1281795210 :   old = strip_using_decl (old);
    3093              : 
    3094   1281795210 :   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      6854795 :       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      6854795 :       goto done;
    3114              :     }
    3115              : 
    3116   1274940415 :   if (old && DECL_IMPLICIT_TYPEDEF_P (old))
    3117              :     {
    3118              :       /* OLD is an implicit typedef.  Move it to to_type.  */
    3119        69319 :       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   1274940415 :   if (DECL_DECLARES_FUNCTION_P (decl))
    3128              :     {
    3129    334481846 :       if (!old)
    3130              :         ;
    3131     23572616 :       else if (OVL_P (old))
    3132              :         {
    3133    534165469 :           for (ovl_iterator iter (old); iter; ++iter)
    3134              :             {
    3135    257364307 :               tree fn = *iter;
    3136              : 
    3137    257364307 :               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     23572608 :         }
    3155              :       else
    3156            0 :         goto conflict;
    3157              : 
    3158    334481838 :       if (to_type != old_type
    3159        37832 :           && warn_shadow
    3160            6 :           && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
    3161    334481844 :           && !(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    334481838 :       local_overload = old && level && level->kind != sk_namespace;
    3167    334481838 :       to_val = ovl_insert (decl, old, -int (hiding));
    3168              :     }
    3169    940458569 :   else if (old)
    3170              :     {
    3171          454 :       if (name_independent_p)
    3172          403 :         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    940458115 :   else if (hiding)
    3213        31303 :     hide_value = true;
    3214              : 
    3215   1281795184 :  done:
    3216   1281795184 :   if (to_val)
    3217              :     {
    3218   1281795151 :       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   1281795042 :       else if (level
    3224   1281795042 :                && !(TREE_CODE (decl) == NAMESPACE_DECL
    3225      1011953 :                     && !DECL_NAMESPACE_ALIAS (decl)))
    3226              :         /* Don't add namespaces here.  They're done in
    3227              :            push_namespace.  */
    3228   1280903995 :         add_decl_to_level (level, decl);
    3229              : 
    3230   1281795151 :       if (slot)
    3231              :         {
    3232    373059366 :           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    373059036 :           else if (to_type || hide_value)
    3240              :             {
    3241       214220 :               *slot = stat_hack (to_val, to_type);
    3242       214220 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    3243       214220 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    3244              :             }
    3245              :           else
    3246              :             {
    3247    372844816 :               gcc_checking_assert (!hide_type);
    3248    372844816 :               *slot = to_val;
    3249              :             }
    3250              :         }
    3251              :       else
    3252              :         {
    3253    908735785 :           binding->type = to_type;
    3254    908735785 :           binding->value = to_val;
    3255    908735785 :           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    292019763 : check_extern_c_conflict (tree decl)
    3272              : {
    3273              :   /* Ignore artificial or system header decls.  */
    3274    292019763 :   if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
    3275    291803015 :     return;
    3276              : 
    3277              :   /* This only applies to decls at namespace scope.  */
    3278       216748 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
    3279              :     return;
    3280              : 
    3281       216736 :   if (!extern_c_decls)
    3282        39967 :     extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
    3283              : 
    3284       216736 :   tree *slot = extern_c_decls
    3285       216736 :     ->find_slot_with_hash (DECL_NAME (decl),
    3286       216736 :                            IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
    3287       216736 :   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       216339 :     *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    908735806 : 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    908735806 :   if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
    3375              :     return NULL_TREE;
    3376              : 
    3377    673116817 :   if (DECL_FUNCTION_SCOPE_P (decl))
    3378              :     {
    3379    425889609 :       tree ctx = DECL_CONTEXT (decl);
    3380    851779218 :       if (DECL_CLONED_FUNCTION_P (ctx)
    3381    399227858 :           || DECL_TEMPLATE_INSTANTIATED (ctx)
    3382    341137579 :           || (DECL_LANG_SPECIFIC (ctx)
    3383    341137579 :               && DECL_DEFAULTED_FN (ctx))
    3384    804607086 :           || (LAMBDA_FUNCTION_P (ctx)
    3385     11440142 :               && 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    586290400 :   tree old = NULL_TREE;
    3395    586290400 :   cp_binding_level *old_scope = NULL;
    3396    586290400 :   if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
    3397              :     {
    3398      2769977 :       old = binding->value;
    3399      2769977 :       old_scope = binding->scope;
    3400              :     }
    3401              : 
    3402      2769977 :   if (old
    3403      2769977 :       && (TREE_CODE (old) == PARM_DECL
    3404      1405148 :           || VAR_P (old)
    3405       153700 :           || (TREE_CODE (old) == TYPE_DECL
    3406        68441 :               && (!DECL_ARTIFICIAL (old)
    3407        55519 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3408      2684624 :       && DECL_FUNCTION_SCOPE_P (old)
    3409      5397260 :       && (!DECL_ARTIFICIAL (decl)
    3410      2414447 :           || is_capture_proxy (decl)
    3411       378866 :           || DECL_IMPLICIT_TYPEDEF_P (decl)
    3412       378836 :           || (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      2248453 :       if (is_capture_proxy (decl))
    3419              :         {
    3420      2035581 :           if (current_lambda_expr ()
    3421      2035581 :               && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
    3422           61 :               && TREE_CODE (old) == PARM_DECL
    3423      2035606 :               && 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      2035581 :           return NULL_TREE;
    3428              :         }
    3429              :       /* Don't complain if it's from an enclosing function.  */
    3430       212872 :       else if (DECL_CONTEXT (old) == current_function_decl
    3431       212872 :                && ((TREE_CODE (decl) != PARM_DECL
    3432       167045 :                     && TREE_CODE (old) == PARM_DECL)
    3433              :                    /* We should also give an error for
    3434              :                        [x=1]{ int x; }  */
    3435       157200 :                    || (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         9851 :           cp_binding_level *b = current_binding_level->level_chain;
    3441              : 
    3442         9851 :           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         9938 :           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         9851 :           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       212752 :       if (DECL_CONTEXT (old) != current_function_decl)
    3477              :         {
    3478        45827 :           for (cp_binding_level *scope = current_binding_level;
    3479       111156 :                scope != old_scope; scope = scope->level_chain)
    3480       109273 :             if (scope->kind == sk_class
    3481       157040 :                 && !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       166925 :       else if (VAR_P (old)
    3491       146159 :                && old_scope == current_binding_level->level_chain
    3492         2922 :                && (old_scope->kind == sk_cond
    3493         2817 :                    || old_scope->kind == sk_for
    3494         2718 :                    || 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       166700 :       else if (TREE_CODE (old) == VAR_DECL
    3519       145934 :                && old_scope == current_binding_level->level_chain
    3520         2697 :                && 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       168565 :       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       168390 :       enum opt_code warning_code;
    3558       168390 :       if (warn_shadow)
    3559              :         warning_code = OPT_Wshadow;
    3560       168321 :       else if ((TREE_CODE (decl) == TYPE_DECL)
    3561       168321 :                ^ (TREE_CODE (old) == TYPE_DECL))
    3562              :         /* If exactly one is a type, they aren't compatible.  */
    3563              :         warning_code = OPT_Wshadow_local;
    3564       168300 :       else if ((TREE_TYPE (old)
    3565       168300 :                 && TREE_TYPE (decl)
    3566       168297 :                 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    3567        33775 :                || TREE_CODE (decl) == TYPE_DECL
    3568        23240 :                || TREE_CODE (old) == TYPE_DECL
    3569       191540 :                || (!dependent_type_p (TREE_TYPE (decl))
    3570         4456 :                    && !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         4429 :                    && !type_uses_auto (TREE_TYPE (decl))
    3578         4158 :                    && 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       168390 :       const char *msg;
    3585       168390 :       if (TREE_CODE (old) == PARM_DECL)
    3586              :         msg = "declaration of %q#D shadows a parameter";
    3587       157227 :       else if (is_capture_proxy (old))
    3588              :         msg = "declaration of %qD shadows a lambda capture";
    3589              :       else
    3590       156970 :         msg = "declaration of %qD shadows a previous local";
    3591              : 
    3592       168390 :       auto_diagnostic_group d;
    3593       168390 :       if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
    3594          117 :         inform_shadowed (old);
    3595       168390 :       return NULL_TREE;
    3596       168390 :     }
    3597              : 
    3598    584041947 :   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    409918235 : set_decl_context_in_fn (tree ctx, tree decl)
    3669              : {
    3670    409918235 :   if (TREE_CODE (decl) == FUNCTION_DECL
    3671    409918235 :       || (VAR_P (decl) && DECL_EXTERNAL (decl)))
    3672              :     /* Make sure local externs are marked as such.  OMP UDRs really
    3673              :        are nested functions.  */
    3674        60691 :     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    409918235 :   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    411132568 :       && !(TREE_CODE (decl) == PARM_DECL
    3683      1214333 :            && parsing_function_declarator ()))
    3684     93212474 :     DECL_CONTEXT (decl) = ctx;
    3685    409918235 : }
    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        60244 : push_local_extern_decl_alias (tree decl)
    3692              : {
    3693        60244 :   if (flag_reflection)
    3694              :     {
    3695          842 :       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          842 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    3700          903 :         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        60244 :   if (dependent_type_p (TREE_TYPE (decl))
    3708        60244 :       || (processing_template_decl
    3709        39031 :           && 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        60156 :   if (cxx_dialect < cxx17
    3715         2403 :       && TREE_CODE (decl) == FUNCTION_DECL
    3716        62179 :       && (value_dependent_expression_p
    3717         2023 :           (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
    3718              :     return;
    3719              : 
    3720        60155 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
    3721              :                        || !DECL_TEMPLATE_INFO (decl));
    3722        60155 :   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        59921 :   tree alias = NULL_TREE;
    3728              : 
    3729        59921 :   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        59918 :       tree ns = CP_DECL_CONTEXT (decl);
    3737        59918 :       tree binding = find_namespace_value (ns, DECL_NAME (decl));
    3738              : 
    3739        59918 :       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        59460 :           alias = copy_decl (decl);
    3755        59460 :           if (TREE_CODE (alias) == FUNCTION_DECL)
    3756              :             {
    3757              :               /* Recontextualize the parms.  */
    3758        58630 :               for (tree *chain = &DECL_ARGUMENTS (alias);
    3759       104529 :                    *chain; chain = &DECL_CHAIN (*chain))
    3760              :                 {
    3761        45899 :                   tree next = DECL_CHAIN (*chain);
    3762        45899 :                   *chain = copy_decl (*chain);
    3763        45899 :                   DECL_CHAIN (*chain) = next;
    3764        45899 :                   DECL_CONTEXT (*chain) = alias;
    3765              :                 }
    3766              : 
    3767        58630 :               tree type = TREE_TYPE (alias);
    3768        58630 :               for (tree args = TYPE_ARG_TYPES (type);
    3769       163044 :                    args; args = TREE_CHAIN (args))
    3770       104459 :                 if (TREE_PURPOSE (args))
    3771              :                   {
    3772              :                     /* There are default args.  Lose them.  */
    3773           45 :                     tree nargs = NULL_TREE;
    3774           45 :                     tree *chain = &nargs;
    3775           45 :                     for (args = TYPE_ARG_TYPES (type);
    3776          111 :                          args; args = TREE_CHAIN (args))
    3777          111 :                       if (args == void_list_node)
    3778              :                         {
    3779           45 :                           *chain = args;
    3780           45 :                           break;
    3781              :                         }
    3782              :                       else
    3783              :                         {
    3784           66 :                           *chain
    3785           66 :                             = build_tree_list (NULL_TREE, TREE_VALUE (args));
    3786           66 :                           chain = &TREE_CHAIN (*chain);
    3787              :                         }
    3788              : 
    3789           45 :                     bool no_named_args_stdarg
    3790           45 :                       = TYPE_NO_NAMED_ARGS_STDARG_P (type);
    3791           45 :                     tree fn_type
    3792           45 :                       = build_function_type (TREE_TYPE (type), nargs,
    3793              :                                              no_named_args_stdarg);
    3794              : 
    3795           45 :                     fn_type = apply_memfn_quals
    3796           45 :                       (fn_type, type_memfn_quals (type));
    3797              : 
    3798           45 :                     fn_type = build_cp_fntype_variant
    3799           45 :                       (fn_type, type_memfn_rqual (type),
    3800           45 :                        TYPE_RAISES_EXCEPTIONS (type),
    3801           45 :                        TYPE_HAS_LATE_RETURN_TYPE (type));
    3802              : 
    3803           45 :                     TREE_TYPE (alias) = fn_type;
    3804           45 :                     break;
    3805              :                   }
    3806              :             }
    3807              : 
    3808              :           /* This is the real thing.  */
    3809        59460 :           DECL_LOCAL_DECL_P (alias) = false;
    3810              : 
    3811              :           /* Expected default linkage is from the namespace.  */
    3812        59460 :           TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
    3813        59460 :           push_nested_namespace (ns);
    3814        59460 :           alias = pushdecl (alias, /* hiding= */true);
    3815        59460 :           pop_nested_namespace (ns);
    3816        59460 :           if (VAR_P (decl)
    3817          830 :               && CP_DECL_THREAD_LOCAL_P (decl)
    3818        59481 :               && alias != error_mark_node)
    3819           18 :             set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
    3820              : 
    3821              :           /* Adjust visibility.  */
    3822        59460 :           determine_visibility (alias);
    3823              :         }
    3824              :     }
    3825              : 
    3826        59909 :   retrofit_lang_decl (decl);
    3827        59909 :   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       268727 : maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
    3836              : {
    3837       268727 :   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        10529 :       for (ovl_iterator iter (mergeable); iter; ++iter)
    3966              :         {
    3967         5148 :           match = duplicate_decls (decl, *iter, hiding);
    3968         5148 :           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   1316972058 : pushdecl (tree decl, bool hiding)
    4000              : {
    4001   1316972058 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    4002              : 
    4003   1316972058 :   if (decl == error_mark_node)
    4004              :     return error_mark_node;
    4005              : 
    4006   1316972052 :   if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
    4007    409918235 :     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   1316972052 :   cp_binding_level *level = current_binding_level;
    4012   1316972231 :   while (level->kind == sk_class
    4013   1316972231 :          || level->kind == sk_cleanup)
    4014          179 :     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   1316972052 :   tree name = DECL_NAME (decl);
    4019   2610952008 :   if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
    4020              :     {
    4021   1291956632 :       cxx_binding *binding = NULL; /* Local scope binding.  */
    4022   1291956632 :       tree ns = NULL_TREE; /* Searched namespace.  */
    4023   1291956632 :       tree *slot = NULL; /* Binding slot in namespace.  */
    4024   1291956632 :       tree *mslot = NULL; /* Current module slot in namespace.  */
    4025   1291956632 :       tree old = NULL_TREE;
    4026   1291956632 :       bool name_independent_p = false;
    4027   1291956632 :       bool name_independent_diagnosed_p = false;
    4028              : 
    4029   1291956632 :       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   1035936892 :           ns = (DECL_NAMESPACE_SCOPE_P (decl)
    4035    766436636 :                 ? 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    383218318 :           slot = find_namespace_slot (ns, name, ns == current_namespace);
    4039    383218318 :           if (slot)
    4040              :             {
    4041    766436562 :               mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
    4042    383218281 :                                               ns == current_namespace);
    4043    383218281 :               old = MAYBE_STAT_DECL (*mslot);
    4044              :             }
    4045              :         }
    4046              :       else
    4047              :         {
    4048    908738314 :           binding = find_local_binding (level, name);
    4049    908738314 :           if (binding)
    4050         3063 :             old = binding->value;
    4051    908738314 :           name_independent_p = name_independent_decl_p (decl);
    4052              :         }
    4053              : 
    4054   1291956632 :       if (old == error_mark_node)
    4055        98396 :         old = NULL_TREE;
    4056              : 
    4057   1291956632 :       tree oldi, oldn;
    4058   1315726063 :       for (oldi = old; oldi; oldi = oldn)
    4059              :         {
    4060     33930793 :           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    584771989 :           for (ovl_iterator iter (oldi); iter; ++iter)
    4071    287689284 :             if (iter.using_p ())
    4072              :               ; /* Ignore using decls here.  */
    4073    284156840 :             else if (iter.hidden_p ()
    4074    101260308 :                      && TREE_CODE (*iter) == FUNCTION_DECL
    4075     77874858 :                      && DECL_LANG_SPECIFIC (*iter)
    4076    362031698 :                      && DECL_MODULE_IMPORT_P (*iter))
    4077              :               ; /* An undeclared builtin imported from elsewhere.  */
    4078    284156837 :             else if (name_independent_p)
    4079              :               {
    4080              :                 /* Ignore name-independent declarations.  */
    4081          364 :                 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    568312946 :             else if (tree match
    4088    284156473 :                      = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
    4089              :               {
    4090     10161362 :                 if (match == error_mark_node)
    4091              :                   ;
    4092     10160323 :                 else if (TREE_CODE (match) == TYPE_DECL)
    4093        46440 :                   gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
    4094              :                                        == (level->kind == sk_namespace
    4095              :                                            ? NULL_TREE : TREE_TYPE (match)));
    4096     10113883 :                 else if (iter.hidden_p () && !hiding)
    4097              :                   {
    4098              :                     /* Unhiding a previously hidden decl.  */
    4099      3922798 :                     tree head = iter.reveal_node (oldi);
    4100      3922798 :                     if (head != oldi)
    4101              :                       {
    4102        17459 :                         gcc_checking_assert (ns);
    4103        17459 :                         if (STAT_HACK_P (*slot))
    4104            0 :                           STAT_DECL (*slot) = head;
    4105              :                         else
    4106        17459 :                           *slot = head;
    4107              :                       }
    4108      3922798 :                     if (DECL_EXTERN_C_P (match))
    4109              :                       /* We need to check and register the decl now.  */
    4110      3653151 :                       check_extern_c_conflict (match);
    4111              :                   }
    4112      6191085 :                 else if (slot
    4113      6191085 :                          && !hiding
    4114      4696787 :                          && STAT_HACK_P (*slot)
    4115      6191110 :                          && 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     10161362 :                 return match;
    4125              :               }
    4126              :         }
    4127              : 
    4128              :       /* Skip a hidden builtin we failed to match already.  There can
    4129              :          only be one.  */
    4130   1281795270 :       if (old && anticipated_builtin_p (old))
    4131       826407 :         old = OVL_CHAIN (old);
    4132              : 
    4133              :       /* Check for redeclaring an import.  */
    4134   1281795270 :       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   1281794778 :       if (hiding)
    4152              :         ; /* Hidden bindings don't shadow anything.  */
    4153              :       else
    4154   1203135832 :         check_template_shadow (decl);
    4155              : 
    4156   1281794778 :       if (DECL_DECLARES_FUNCTION_P (decl))
    4157              :         {
    4158    334481523 :           check_default_args (decl);
    4159              : 
    4160    334481523 :           if (hiding)
    4161              :             {
    4162     78523096 :               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   1281794769 :       if (level->kind != sk_namespace)
    4177              :         {
    4178    908735806 :           tree local_shadow = check_local_shadow (decl);
    4179    908735806 :           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    908735806 :           if (TREE_CODE (decl) == NAMESPACE_DECL)
    4202              :             /* A local namespace alias.  */
    4203       109242 :             set_identifier_type_value_with_scope (name, NULL_TREE, level);
    4204              : 
    4205    908735806 :           if (!binding)
    4206    908735245 :             binding = create_local_binding (level, name);
    4207              :         }
    4208    373058963 :       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   1281794769 :       old = update_binding (level, binding, mslot, old, decl, hiding);
    4219              : 
    4220   1281794769 :       if (old != decl)
    4221              :         /* An existing decl matched, use it.  */
    4222              :         decl = old;
    4223              :       else
    4224              :         {
    4225   1281794745 :           if (TREE_CODE (decl) == TYPE_DECL)
    4226              :             {
    4227    252875974 :               tree type = TREE_TYPE (decl);
    4228              : 
    4229    252875974 :               if (type != error_mark_node)
    4230              :                 {
    4231    252875906 :                   if (TYPE_NAME (type) != decl)
    4232     22396181 :                     set_underlying_type (decl);
    4233              : 
    4234    252875906 :                   set_identifier_type_value_with_scope (name, decl, level);
    4235              : 
    4236    252875906 :                   if (level->kind != sk_namespace
    4237    252875906 :                       && !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    236530585 :                     record_locally_defined_typedef (decl);
    4242              :                 }
    4243              :             }
    4244   1028918771 :           else if (VAR_OR_FUNCTION_DECL_P (decl))
    4245              :             {
    4246    396308829 :               if (DECL_EXTERN_C_P (decl))
    4247    288357497 :                 check_extern_c_conflict (decl);
    4248              : 
    4249    396308829 :               if (!DECL_LOCAL_DECL_P (decl)
    4250    396308829 :                   && VAR_P (decl))
    4251     86899484 :                 maybe_register_incomplete_var (decl);
    4252              : 
    4253    396308829 :               if (DECL_LOCAL_DECL_P (decl)
    4254    396308829 :                   && NAMESPACE_SCOPE_P (decl))
    4255        60238 :                 push_local_extern_decl_alias (decl);
    4256              :             }
    4257              : 
    4258   1281794745 :           if (level->kind == sk_namespace
    4259    373058945 :               && TREE_PUBLIC (level->this_entity)
    4260   1654848791 :               && module_maybe_has_cmi_p ())
    4261       268727 :             maybe_record_mergeable_decl (slot, name, decl);
    4262              :         }
    4263              :     }
    4264              :   else
    4265     25015420 :     add_decl_to_level (level, decl);
    4266              : 
    4267              :   return decl;
    4268   1316972058 : }
    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       147480 : mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
    4276              : {
    4277       147480 :   tree *mslot = find_namespace_slot (ns, name, true);
    4278       147480 :   tree *vslot = get_fixed_binding_slot
    4279       294162 :     (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    4280              :      true);
    4281              : 
    4282       147480 :   gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
    4283       147480 :   *vec = *mslot;
    4284              : 
    4285       147480 :   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        70157 : add_mergeable_namespace_entity (tree *gslot, tree decl)
    4293              : {
    4294        70157 :   *gslot = ovl_make (decl, *gslot);
    4295        70157 : }
    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       162156 : lookup_class_binding (tree klass, tree name)
    4303              : {
    4304       162156 :   tree found = NULL_TREE;
    4305              : 
    4306       162156 :   if (!COMPLETE_TYPE_P (klass))
    4307              :     ;
    4308       162156 :   else if (TYPE_LANG_SPECIFIC (klass))
    4309              :     {
    4310       160692 :       vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    4311              : 
    4312       160692 :       found = member_vec_binary_search (member_vec, name);
    4313       160692 :       if (!found)
    4314              :         ;
    4315       160258 :       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       160252 :       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       162156 :   return found;
    4333              : }
    4334              : 
    4335              : /* Whether this using is declared in the module purview.  */
    4336              : 
    4337              : bool
    4338        40246 : ovl_iterator::purview_p () const
    4339              : {
    4340        40246 :   gcc_checking_assert (using_p ());
    4341        40246 :   if (TREE_CODE (ovl) == USING_DECL)
    4342         5856 :     return DECL_MODULE_PURVIEW_P (ovl);
    4343        34390 :   return OVL_PURVIEW_P (ovl);
    4344              : }
    4345              : 
    4346              : /* Whether this using is exported from this module.  */
    4347              : 
    4348              : bool
    4349        40246 : ovl_iterator::exporting_p () const
    4350              : {
    4351        40246 :   gcc_checking_assert (using_p ());
    4352        40246 :   if (TREE_CODE (ovl) == USING_DECL)
    4353         5856 :     return DECL_MODULE_EXPORT_P (ovl);
    4354        34390 :   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      7533739 : walk_module_binding (tree binding, bitmap partitions,
    4478              :                      bool (*callback) (tree decl, WMB_Flags, void *data),
    4479              :                      void *data)
    4480              : {
    4481      7533739 :   tree current = binding;
    4482      7533739 :   unsigned count = 0;
    4483              : 
    4484      7533739 :   if (TREE_CODE (binding) == BINDING_VECTOR)
    4485        42432 :     current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
    4486              : 
    4487      7542165 :   bool decl_hidden = false;
    4488      7533739 :   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     15136374 :   for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
    4506              :     {
    4507      7602635 :       if (iter.hidden_p ())
    4508              :         decl_hidden = true;
    4509      7602635 :       if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
    4510              :         {
    4511      5787015 :           WMB_Flags flags = WMB_None;
    4512      5787015 :           if (decl_hidden)
    4513        11043 :             flags = WMB_Flags (flags | WMB_Hidden);
    4514      5787015 :           if (iter.using_p ())
    4515              :             {
    4516        40234 :               flags = WMB_Flags (flags | WMB_Using);
    4517        40234 :               if (iter.purview_p ())
    4518        34806 :                 flags = WMB_Flags (flags | WMB_Purview);
    4519        40234 :               if (iter.exporting_p ())
    4520        34168 :                 flags = WMB_Flags (flags | WMB_Export);
    4521              :             }
    4522      5787015 :           count += callback (*iter, flags, data);
    4523              :         }
    4524      7602635 :       decl_hidden = false;
    4525              :     }
    4526              : 
    4527      7533739 :   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      7533739 :   return count;
    4614              : }
    4615              : 
    4616              : /* Imported module MOD has a binding to NS::NAME, stored in section
    4617              :    SNUM.  */
    4618              : 
    4619              : bool
    4620       190279 : import_module_binding  (tree ns, tree name, unsigned mod, unsigned snum)
    4621              : {
    4622       190279 :   tree *slot = find_namespace_slot (ns, name, true);
    4623       190279 :   binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
    4624              : 
    4625       190279 :   if (mslot->is_lazy () || *mslot)
    4626              :     /* Oops, something was already there.  */
    4627              :     return false;
    4628              : 
    4629       190279 :   mslot->set_lazy (snum);
    4630       190279 :   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       108494 : 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       108494 :   if (!value && !internal)
    4646              :     /* Bogus BMIs could give rise to nothing to bind.  */
    4647              :     return false;
    4648              : 
    4649       108479 :   gcc_assert (!value
    4650              :               || TREE_CODE (value) != NAMESPACE_DECL
    4651              :               || DECL_NAMESPACE_ALIAS (value));
    4652       108494 :   gcc_checking_assert (mod);
    4653              : 
    4654       108494 :   tree *slot = find_namespace_slot (ns, name, true);
    4655       108494 :   binding_slot *mslot = search_imported_binding_slot (slot, mod);
    4656              : 
    4657       108494 :   if (!mslot || !mslot->is_lazy ())
    4658              :     /* Again, bogus BMI could give find to missing or already loaded slot.  */
    4659              :     return false;
    4660              : 
    4661       108494 :   tree bind = value;
    4662       108494 :   if (type || visible != bind || internal || partition_p || global_p)
    4663              :     {
    4664       106762 :       bind = stat_hack (bind, type);
    4665       106762 :       STAT_VISIBLE (bind) = visible;
    4666          849 :       if ((partition_p && TREE_PUBLIC (ns))
    4667       106768 :           || (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       108494 :   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       108494 :   if (partition_p)
    4684          849 :     MODULE_BINDING_PARTITION_P (bind) = true;
    4685       108494 :   if (global_p)
    4686       105690 :     MODULE_BINDING_GLOBAL_P (bind) = true;
    4687              : 
    4688       108494 :   *mslot = bind;
    4689              : 
    4690       108494 :   return true;
    4691              : }
    4692              : 
    4693              : void
    4694        72302 : add_module_namespace_decl (tree ns, tree decl)
    4695              : {
    4696        72302 :   gcc_assert (!DECL_CHAIN (decl));
    4697        72302 :   gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
    4698              :                          && DECL_LOCAL_DECL_P (decl)));
    4699        72302 :   if (CHECKING_P)
    4700              :     /* Expensive already-there? check.  */
    4701    115707133 :     for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
    4702    115634831 :          probe = DECL_CHAIN (probe))
    4703    115634831 :       gcc_assert (decl != probe);
    4704              : 
    4705        72302 :   add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
    4706              : 
    4707        72302 :   if (VAR_P (decl))
    4708         2245 :     maybe_register_incomplete_var (decl);
    4709              : 
    4710        70057 :   if (VAR_OR_FUNCTION_DECL_P (decl)
    4711        94768 :       && DECL_EXTERN_C_P (decl))
    4712         9115 :     check_extern_c_conflict (decl);
    4713        72302 : }
    4714              : 
    4715              : /* Enter DECL into the symbol table, if that's appropriate.  Returns
    4716              :    DECL, or a modified version thereof.  */
    4717              : 
    4718              : tree
    4719    143741427 : maybe_push_decl (tree decl)
    4720              : {
    4721    143741427 :   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    143741427 :   if (decl == error_mark_node
    4727    143741422 :       || (TREE_CODE (decl) != PARM_DECL
    4728    143741422 :           && DECL_CONTEXT (decl) != NULL_TREE
    4729              :           /* Definitions of namespace members outside their namespace are
    4730              :              possible.  */
    4731     51273692 :           && !DECL_NAMESPACE_SCOPE_P (decl))
    4732    142320268 :       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
    4733    142320268 :       || 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    286061695 :       || (TREE_CODE (decl) == FUNCTION_DECL
    4738     36897224 :           && DECL_TEMPLATE_SPECIALIZATION (decl)))
    4739      1512039 :     return decl;
    4740              :   else
    4741    142229388 :     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     12999034 : 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     12999034 :   cp_binding_level *b = innermost_nonclass_level ();
    4754              : 
    4755     12999034 :   gcc_assert (b->kind != sk_namespace);
    4756     12999034 :   if (find_local_binding (b, id))
    4757              :     {
    4758              :       /* Supplement the existing binding.  */
    4759           27 :       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     12999007 :     push_binding (id, decl, b);
    4768              : 
    4769     12999025 :   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     12999025 :     decl = build_tree_list (id, decl);
    4773              : 
    4774              :   /* And put DECL on the list of things declared by the current
    4775              :      binding level.  */
    4776     12999025 :   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          284 : 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          284 :   gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
    4788              : 
    4789          284 :   tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
    4790          284 :   if (!DECL_LANG_SPECIFIC (inner)
    4791          338 :       || !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   1310343228 : push_binding_level (cp_binding_level *scope)
    4909              : {
    4910              :   /* Add it to the front of currently active scopes stack.  */
    4911   1310343228 :   scope->level_chain = current_binding_level;
    4912   1310343228 :   current_binding_level = scope;
    4913   1310343228 :   keep_next_level_flag = false;
    4914              : 
    4915   1310343228 :   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   1310343228 : }
    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   1108666709 : begin_scope (scope_kind kind, tree entity)
    4931              : {
    4932   1108666709 :   cp_binding_level *scope;
    4933              : 
    4934              :   /* Reuse or create a struct for this binding level.  */
    4935   1108666709 :   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
    4936              :     {
    4937   1064693119 :       scope = free_binding_level;
    4938   1064693119 :       free_binding_level = scope->level_chain;
    4939   1064693119 :       memset (scope, 0, sizeof (cp_binding_level));
    4940              :     }
    4941              :   else
    4942     43973590 :     scope = ggc_cleared_alloc<cp_binding_level> ();
    4943              : 
    4944   1108666709 :   scope->this_entity = entity;
    4945   1108666709 :   scope->more_cleanups_ok = true;
    4946   1108666709 :   switch (kind)
    4947              :     {
    4948          378 :     case sk_cleanup:
    4949          378 :       scope->keep = true;
    4950          378 :       break;
    4951              : 
    4952      5885534 :     case sk_template_spec:
    4953      5885534 :       scope->explicit_spec_p = true;
    4954      5885534 :       kind = sk_template_parms;
    4955              :       /* Fall through.  */
    4956    743324464 :     case sk_template_parms:
    4957    743324464 :     case sk_block:
    4958    743324464 :     case sk_try:
    4959    743324464 :     case sk_catch:
    4960    743324464 :     case sk_for:
    4961    743324464 :     case sk_template_for:
    4962    743324464 :     case sk_cond:
    4963    743324464 :     case sk_class:
    4964    743324464 :     case sk_scoped_enum:
    4965    743324464 :     case sk_transaction:
    4966    743324464 :     case sk_omp:
    4967    743324464 :     case sk_contract:
    4968    743324464 :     case sk_stmt_expr:
    4969    743324464 :     case sk_lambda:
    4970    743324464 :       scope->keep = keep_next_level_flag;
    4971    743324464 :       break;
    4972              : 
    4973    365243471 :     case sk_function_parms:
    4974    365243471 :       scope->keep = keep_next_level_flag;
    4975    365243471 :       break;
    4976              : 
    4977        98396 :     case sk_namespace:
    4978        98396 :       NAMESPACE_LEVEL (entity) = scope;
    4979        98396 :       break;
    4980              : 
    4981            0 :     default:
    4982              :       /* Should not happen.  */
    4983            0 :       gcc_unreachable ();
    4984   1108666709 :       break;
    4985              :     }
    4986   1108666709 :   scope->kind = kind;
    4987              : 
    4988   1108666709 :   push_binding_level (scope);
    4989              : 
    4990   1108666709 :   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   1363459288 : leave_scope (void)
    4998              : {
    4999   1363459288 :   cp_binding_level *scope = current_binding_level;
    5000              : 
    5001   1363459288 :   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   1363459288 :   if (NAMESPACE_LEVEL (global_namespace))
    5006   1363459288 :     gcc_assert (!global_scope_p (scope));
    5007              : 
    5008   1363459288 :   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   1363459288 :   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   1363459288 :   if (scope->kind != sk_namespace
    5024   1310228485 :       && scope != previous_class_level)
    5025              :     {
    5026   1036213127 :       scope->level_chain = free_binding_level;
    5027   1036213127 :       gcc_assert (!ENABLE_SCOPE_CHECKING
    5028              :                   || scope->binding_depth == binding_depth);
    5029   1036213127 :       free_binding_level = scope;
    5030              :     }
    5031              : 
    5032   1363459288 :   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    439596271 :       scope->defining_class_p = 0;
    5037              : 
    5038              :       /* Find the innermost enclosing class scope, and reset
    5039              :          CLASS_BINDING_LEVEL appropriately.  */
    5040    439596271 :       class_binding_level = NULL;
    5041   1798502430 :       for (scope = current_binding_level; scope; scope = scope->level_chain)
    5042    972840206 :         if (scope->kind == sk_class)
    5043              :           {
    5044     53530318 :             class_binding_level = scope;
    5045     53530318 :             break;
    5046              :           }
    5047              :     }
    5048              : 
    5049   1363459288 :   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     29557624 : invalidate_class_lookup_cache (void)
    5058              : {
    5059     29557624 :   previous_class_level->level_chain = free_binding_level;
    5060     29557624 :   free_binding_level = previous_class_level;
    5061     29557624 :   previous_class_level = NULL;
    5062     29557624 : }
    5063              : 
    5064              : static void
    5065     53230812 : resume_scope (cp_binding_level* b)
    5066              : {
    5067              :   /* Resuming binding levels is meant only for namespaces,
    5068              :      and those cannot nest into classes.  */
    5069     53230812 :   gcc_assert (!class_binding_level);
    5070              :   /* Also, resuming a non-directly nested namespace is a no-no.  */
    5071     53230812 :   gcc_assert (b->level_chain == current_binding_level);
    5072     53230812 :   current_binding_level = b;
    5073     53230812 :   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     53230812 : }
    5081              : 
    5082              : /* Return the innermost binding level that is not for a class scope.  */
    5083              : 
    5084              : static cp_binding_level *
    5085   1671668492 : innermost_nonclass_level (void)
    5086              : {
    5087   1671668492 :   cp_binding_level *b;
    5088              : 
    5089   1671668492 :   b = current_binding_level;
    5090   1924821008 :   while (b->kind == sk_class)
    5091    253152516 :     b = b->level_chain;
    5092              : 
    5093   1671668492 :   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      8414559 : maybe_push_cleanup_level (tree type)
    5102              : {
    5103      8414559 :   if (type != error_mark_node
    5104      8414367 :       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
    5105      8761917 :       && current_binding_level->more_cleanups_ok == 0)
    5106              :     {
    5107          378 :       begin_scope (sk_cleanup, NULL);
    5108          378 :       current_binding_level->statement_list = push_stmt_list ();
    5109              :     }
    5110      8414559 : }
    5111              : 
    5112              : /* Return true if we are in the global binding level.  */
    5113              : 
    5114              : bool
    5115       443764 : global_bindings_p (void)
    5116              : {
    5117       443764 :   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   1625836358 : toplevel_bindings_p (void)
    5128              : {
    5129   1625836358 :   cp_binding_level *b = innermost_nonclass_level ();
    5130              : 
    5131   1625836358 :   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     32831295 : namespace_bindings_p (void)
    5140              : {
    5141     32831295 :   cp_binding_level *b = innermost_nonclass_level ();
    5142              : 
    5143     32831295 :   return b->kind == sk_namespace;
    5144              : }
    5145              : 
    5146              : /* True if the innermost non-class scope is a block scope.  */
    5147              : 
    5148              : bool
    5149         1805 : local_bindings_p (void)
    5150              : {
    5151         1805 :   cp_binding_level *b = innermost_nonclass_level ();
    5152         1805 :   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    362896653 : kept_level_p (void)
    5159              : {
    5160    362896653 :   return (current_binding_level->blocks != NULL_TREE
    5161    325687236 :           || current_binding_level->keep
    5162    317851823 :           || current_binding_level->kind == sk_cleanup
    5163    317851823 :           || current_binding_level->names != NULL_TREE
    5164    644595884 :           || current_binding_level->using_directives);
    5165              : }
    5166              : 
    5167              : /* Returns the kind of the innermost scope.  */
    5168              : 
    5169              : scope_kind
    5170   2853890009 : innermost_scope_kind (void)
    5171              : {
    5172   2853890009 :   return current_binding_level->kind;
    5173              : }
    5174              : 
    5175              : /* Returns true if this scope was created to store template parameters.  */
    5176              : 
    5177              : bool
    5178    645988943 : template_parm_scope_p (void)
    5179              : {
    5180    645988943 :   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     13230524 : keep_next_level (bool keep)
    5189              : {
    5190     13230524 :   keep_next_level_flag = keep;
    5191     13230524 : }
    5192              : 
    5193              : /* Return the list of declarations of the current local scope.  */
    5194              : 
    5195              : tree
    5196    365218612 : get_local_decls (void)
    5197              : {
    5198    365218612 :   gcc_assert (current_binding_level->kind != sk_namespace
    5199              :               && current_binding_level->kind != sk_class);
    5200    365218612 :   return current_binding_level->names;
    5201              : }
    5202              : 
    5203              : /* Return how many function prototypes we are currently nested inside.  */
    5204              : 
    5205              : int
    5206    278297576 : function_parm_depth (void)
    5207              : {
    5208    278297576 :   int level = 0;
    5209    278297576 :   cp_binding_level *b;
    5210              : 
    5211    278297576 :   for (b = current_binding_level;
    5212    557324000 :        b->kind == sk_function_parms;
    5213    279026424 :        b = b->level_chain)
    5214    279026424 :     ++level;
    5215              : 
    5216    278297576 :   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    405751033 : set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
    5346              : {
    5347    405751033 :   if (b->kind == sk_namespace)
    5348              :     /* At namespace scope we should not see an identifier type value.  */
    5349     26030410 :     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    379720623 :       tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
    5356    379720623 :       b->type_shadowed = tree_cons (id, old, b->type_shadowed);
    5357    379720623 :       tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
    5358    379720623 :       TREE_TYPE (b->type_shadowed) = type;
    5359    379720623 :       SET_IDENTIFIER_TYPE_VALUE (id, type);
    5360              :     }
    5361    405751033 : }
    5362              : 
    5363              : /* As set_identifier_type_value_with_scope, but using
    5364              :    current_binding_level.  */
    5365              : 
    5366              : void
    5367    142981164 : set_identifier_type_value (tree id, tree decl)
    5368              : {
    5369    142981164 :   set_identifier_type_value_with_scope (id, decl, current_binding_level);
    5370    142981164 : }
    5371              : 
    5372              : /* Return the name for the constructor (or destructor) for the
    5373              :    specified class.  */
    5374              : 
    5375              : tree
    5376    382442542 : constructor_name (tree type)
    5377              : {
    5378    382442542 :   tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
    5379              : 
    5380    382442542 :   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    358448998 : constructor_name_p (tree name, tree type)
    5388              : {
    5389    358448998 :   gcc_assert (MAYBE_CLASS_TYPE_P (type));
    5390              : 
    5391              :   /* These don't have names.  */
    5392    358448998 :   if (TREE_CODE (type) == DECLTYPE_TYPE
    5393    358448995 :       || TREE_CODE (type) == TYPEOF_TYPE)
    5394              :     return false;
    5395              : 
    5396    358448995 :   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     64118951 : do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
    5419              : {
    5420     64118951 :   cp_binding_level *b;
    5421              : 
    5422     64118951 :   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     64118951 :       tree function_decl = current_function_decl;
    5433     64118951 :       if (level->kind == sk_namespace)
    5434     59713093 :         current_function_decl = NULL_TREE;
    5435     64118951 :       b = current_binding_level;
    5436     64118951 :       current_binding_level = level;
    5437     64118951 :       x = pushdecl (x, hiding);
    5438     64118951 :       current_binding_level = b;
    5439     64118951 :       current_function_decl = function_decl;
    5440              :     }
    5441     64118951 :   return x;
    5442              : }
    5443              : 
    5444              : /* Inject X into the local scope just before the function parms.  */
    5445              : 
    5446              : tree
    5447      2299332 : pushdecl_outermost_localscope (tree x)
    5448              : {
    5449      2299332 :   cp_binding_level *b = NULL;
    5450      2299332 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5451              : 
    5452              :   /* Find the block scope just inside the function parms.  */
    5453      2299332 :   cp_binding_level *n = current_binding_level;
    5454      2517123 :   while (n && n->kind != sk_block)
    5455       217791 :     n = n->level_chain;
    5456      6409662 :   for (; n && n->kind != sk_function_parms; n = b->level_chain)
    5457      4110330 :     b = n;
    5458              : 
    5459      2299332 :   return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
    5460      2299332 : }
    5461              : 
    5462              : /* Checks if BINDING is a binding that we can export.  */
    5463              : 
    5464              : static bool
    5465        84212 : check_can_export_using_decl (tree binding)
    5466              : {
    5467              :   /* Declarations in header units are always OK.  */
    5468        84212 :   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        72964 :   tree entity = binding;
    5474        72964 :   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        72449 :   linkage_kind linkage = decl_linkage (entity);
    5482        72449 :   tree not_tmpl = STRIP_TEMPLATE (entity);
    5483              : 
    5484              :   /* Attachment is determined by the owner of an enumerator.  */
    5485        72449 :   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        72449 :   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     19182491 : do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
    5543              :                          bool insert_p, tree *value_p, tree *type_p)
    5544              : {
    5545     19182491 :   tree value = *value_p;
    5546     19182491 :   tree type = *type_p;
    5547     19182491 :   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     19182491 :   if (value && DECL_IMPLICIT_TYPEDEF_P (strip_using_decl (value)))
    5552              :     {
    5553              :       type = value;
    5554              :       value = NULL_TREE;
    5555              :     }
    5556              : 
    5557     19182491 :   if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
    5558              :     {
    5559       175525 :       lookup.type = lookup.value;
    5560       175525 :       lookup.value = NULL_TREE;
    5561              :     }
    5562              : 
    5563              :   /* Only process exporting if we're going to be inserting.  */
    5564     19182491 :   bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
    5565              : 
    5566              :   /* First do the value binding.  */
    5567     19182491 :   if (!lookup.value)
    5568              :     /* Nothing (only implicit typedef found).  */
    5569       175703 :     gcc_checking_assert (lookup.type);
    5570     19006788 :   else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
    5571              :     {
    5572     15238475 :       for (lkp_iterator usings (lookup.value); usings; ++usings)
    5573              :         {
    5574      9947430 :           tree new_fn = *usings;
    5575      9947430 :           tree inner = STRIP_TEMPLATE (new_fn);
    5576      9947430 :           bool exporting_p = revealing_p && module_exporting_p ();
    5577        72798 :           if (exporting_p)
    5578        72798 :             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      9947430 :           bool found = false;
    5591    168906300 :           for (ovl_iterator old (value); !found && old; ++old)
    5592              :             {
    5593     79906805 :               tree old_fn = *old;
    5594              : 
    5595     79906805 :               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       329067 :                   found = true;
    5601       329067 :                   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       328338 :                   else if (!revealing_p)
    5606              :                     ;
    5607        57439 :                   else if (old.using_p ())
    5608              :                     {
    5609              :                       /* Update in place.  'tis ok.  */
    5610        45162 :                       OVL_PURVIEW_P (old.get_using ()) = true;
    5611        45162 :                       if (exporting_p)
    5612        45138 :                         OVL_EXPORT_P (old.get_using ()) = true;
    5613              :                     }
    5614        12277 :                   else if (!DECL_LANG_SPECIFIC (inner)
    5615        12277 :                            || !DECL_MODULE_PURVIEW_P (inner)
    5616        12292 :                            || (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     79577738 :               else if (old.using_p ())
    5625     74425441 :                 continue; /* This is a using decl. */
    5626      5152297 :               else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
    5627      2744312 :                 continue; /* This is an anticipated builtin.  */
    5628      2407985 :               else if (!matching_fn_p (new_fn, old_fn))
    5629      2407946 :                 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      9947430 :           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      9628879 :             value = ovl_insert (new_fn, value, 1 + revealing_p + exporting_p);
    5657              :         }
    5658      5291045 :     }
    5659     13715743 :   else if (value
    5660              :            /* Ignore anticipated builtins.  */
    5661        68645 :            && !anticipated_builtin_p (value)
    5662     13784384 :            && !decls_match (lookup.value, strip_using_decl (value)))
    5663              :     {
    5664           15 :       diagnose_name_conflict (lookup.value, value);
    5665           15 :       failed = true;
    5666              :     }
    5667     13715728 :   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     13714431 :       if (value != lookup.value)
    5677              :         {
    5678     13676429 :           value = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5679     13676429 :           USING_DECL_DECLS (value) = lookup.value;
    5680     13676429 :           USING_DECL_SCOPE (value) = CP_DECL_CONTEXT (lookup.value);
    5681     13676429 :           DECL_CONTEXT (value) = current_scope ();
    5682     13676429 :           DECL_MODULE_PURVIEW_P (value) = module_purview_p ();
    5683              :         }
    5684              :       else
    5685        38002 :         set_instantiating_module (value);
    5686              : 
    5687     13714431 :       if (revealing_p
    5688        10222 :           && module_exporting_p ()
    5689     13724518 :           && 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     19182491 :   if (lookup.type)
    5699              :     {
    5700       175722 :       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       175719 :       else if (insert_p)
    5706              :         {
    5707              :           /* As with revealing value bindings.  */
    5708       175541 :           if (type != lookup.type)
    5709              :             {
    5710       174666 :               type = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5711       174666 :               USING_DECL_DECLS (type) = lookup.type;
    5712       174666 :               USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (lookup.type);
    5713       174666 :               DECL_CONTEXT (type) = current_scope ();
    5714       174666 :               DECL_MODULE_PURVIEW_P (type) = module_purview_p ();
    5715              :             }
    5716              :           else
    5717          875 :             set_instantiating_module (type);
    5718              : 
    5719       175541 :           if (revealing_p
    5720         1346 :               && module_exporting_p ()
    5721       176868 :               && check_can_export_using_decl (lookup.type))
    5722         1312 :             DECL_MODULE_EXPORT_P (type) = true;
    5723              :         }
    5724              :     }
    5725              : 
    5726     19182313 :   if (insert_p)
    5727              :     {
    5728              :       /* If value is empty, shift any class or enumeration name back.  */
    5729     19178947 :       if (!value)
    5730              :         {
    5731       175516 :           value = type;
    5732       175516 :           type = NULL_TREE;
    5733              :         }
    5734     19178947 :       *value_p = value;
    5735     19178947 :       *type_p = type;
    5736              :     }
    5737              : 
    5738     19182491 :   return failed;
    5739              : }
    5740              : 
    5741              : /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
    5742              :    Both are namespaces.  */
    5743              : 
    5744              : bool
    5745    126992612 : is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
    5746              : {
    5747    126992612 :   int depth = SCOPE_DEPTH (ancestor);
    5748              : 
    5749    126992612 :   if (!depth && !inline_only)
    5750              :     /* The global namespace encloses everything.  */
    5751              :     return true;
    5752              : 
    5753    129104285 :   while (SCOPE_DEPTH (descendant) > depth
    5754    129104285 :          && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
    5755      2202341 :     descendant = CP_DECL_CONTEXT (descendant);
    5756              : 
    5757    126901944 :   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     95621648 : is_ancestor (tree root, tree child)
    5766              : {
    5767     95621648 :   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     95621648 :   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     95621648 :   if (root == global_namespace)
    5777              :     return true;
    5778              : 
    5779              :   /* Search CHILD until we reach namespace scope.  */
    5780    200865994 :   while (TREE_CODE (child) != NAMESPACE_DECL)
    5781              :     {
    5782              :       /* If we've reached the ROOT, it encloses CHILD.  */
    5783    105511355 :       if (root == child)
    5784              :         return true;
    5785              : 
    5786              :       /* Go out one level.  */
    5787    105511304 :       if (TYPE_P (child))
    5788    103367244 :         child = TYPE_NAME (child);
    5789    105511304 :       child = CP_DECL_CONTEXT (child);
    5790              :     }
    5791              : 
    5792     95354639 :   if (TREE_CODE (root) != NAMESPACE_DECL)
    5793              :     /* Failed to meet the non-namespace we were looking for.  */
    5794              :     return false;
    5795              : 
    5796     95354624 :   if (tree alias = DECL_NAMESPACE_ALIAS (child))
    5797            3 :     child = alias;
    5798              : 
    5799     95354624 :   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    341995369 : push_scope (tree t)
    5809              : {
    5810    341995369 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5811     82493766 :     push_decl_namespace (t);
    5812    259501603 :   else if (CLASS_TYPE_P (t))
    5813              :     {
    5814    195478583 :       if (!at_class_scope_p ()
    5815    195478583 :           || !same_type_p (current_class_type, t))
    5816     92556896 :         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    341995369 :   return t;
    5826              : }
    5827              : 
    5828              : /* Leave scope pushed by push_scope.  */
    5829              : 
    5830              : void
    5831    239074153 : pop_scope (tree t)
    5832              : {
    5833    239074153 :   if (t == NULL_TREE)
    5834              :     return;
    5835    239073679 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5836     82493763 :     pop_decl_namespace ();
    5837    156579916 :   else if CLASS_TYPE_P (t)
    5838     92556896 :     pop_nested_class ();
    5839              : }
    5840              : 
    5841              : /* Subroutine of push_inner_scope.  */
    5842              : 
    5843              : static void
    5844       262120 : push_inner_scope_r (tree outer, tree inner)
    5845              : {
    5846       262120 :   tree prev;
    5847              : 
    5848       262120 :   if (outer == inner
    5849       262120 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5850              :     return;
    5851              : 
    5852       262111 :   prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5853       262111 :   if (outer != prev)
    5854         1729 :     push_inner_scope_r (outer, prev);
    5855       262111 :   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       107489 :       while (current_binding_level->kind == sk_template_parms)
    5861              :         {
    5862        53677 :           cp_binding_level *b = current_binding_level;
    5863        53677 :           current_binding_level = b->level_chain;
    5864        53677 :           b->level_chain = save_template_parm;
    5865        53677 :           save_template_parm = b;
    5866              :         }
    5867              : 
    5868        53812 :       resume_scope (NAMESPACE_LEVEL (inner));
    5869        53812 :       current_namespace = inner;
    5870              : 
    5871              :       /* Restore template parameter scopes.  */
    5872       107489 :       while (save_template_parm)
    5873              :         {
    5874        53677 :           cp_binding_level *b = save_template_parm;
    5875        53677 :           save_template_parm = b->level_chain;
    5876        53677 :           b->level_chain = current_binding_level;
    5877        53677 :           current_binding_level = b;
    5878              :         }
    5879              :     }
    5880              :   else
    5881       208299 :     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       260391 : push_inner_scope (tree inner)
    5894              : {
    5895       260391 :   tree outer = current_scope ();
    5896       260391 :   if (!outer)
    5897            0 :     outer = current_namespace;
    5898              : 
    5899       260391 :   push_inner_scope_r (outer, inner);
    5900       260391 :   return outer;
    5901              : }
    5902              : 
    5903              : /* Exit the current scope INNER back to scope OUTER.  */
    5904              : 
    5905              : void
    5906       260391 : pop_inner_scope (tree outer, tree inner)
    5907              : {
    5908       260391 :   if (outer == inner
    5909       260391 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5910              :     return;
    5911              : 
    5912       522496 :   while (outer != inner)
    5913              :     {
    5914       262114 :       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       107489 :           while (current_binding_level->kind == sk_template_parms)
    5920              :             {
    5921        53677 :               cp_binding_level *b = current_binding_level;
    5922        53677 :               current_binding_level = b->level_chain;
    5923        53677 :               b->level_chain = save_template_parm;
    5924        53677 :               save_template_parm = b;
    5925              :             }
    5926              : 
    5927        53812 :           pop_namespace ();
    5928              : 
    5929              :           /* Restore template parameter scopes.  */
    5930       161301 :           while (save_template_parm)
    5931              :             {
    5932        53677 :               cp_binding_level *b = save_template_parm;
    5933        53677 :               save_template_parm = b->level_chain;
    5934        53677 :               b->level_chain = current_binding_level;
    5935        53677 :               current_binding_level = b;
    5936              :             }
    5937              :         }
    5938              :       else
    5939       208302 :         popclass ();
    5940              : 
    5941       262111 :       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    237936018 : pushlevel_class (void)
    5949              : {
    5950    237936018 :   class_binding_level = begin_scope (sk_class, current_class_type);
    5951    237936018 : }
    5952              : 
    5953              : /* ...and a poplevel for class declarations.  */
    5954              : 
    5955              : void
    5956    439596274 : poplevel_class (void)
    5957              : {
    5958    439596274 :   cp_binding_level *level = class_binding_level;
    5959    439596274 :   cp_class_binding *cb;
    5960    439596274 :   size_t i;
    5961    439596274 :   tree shadowed;
    5962              : 
    5963    439596274 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5964    439596274 :   gcc_assert (level != 0);
    5965              : 
    5966              :   /* If we're leaving a toplevel class, cache its binding level.  */
    5967    439596271 :   if (current_class_depth == 1)
    5968    274015358 :     previous_class_level = level;
    5969    439596271 :   for (shadowed = level->type_shadowed;
    5970   1413026221 :        shadowed;
    5971    973429950 :        shadowed = TREE_CHAIN (shadowed))
    5972    973429950 :     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
    5973              : 
    5974              :   /* Remove the bindings for all of the class-level declarations.  */
    5975    439596271 :   if (level->class_shadowed)
    5976              :     {
    5977    622818022 :       FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
    5978              :         {
    5979    475711989 :           IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
    5980    475711989 :           cxx_binding_free (cb->base);
    5981              :         }
    5982    147106033 :       ggc_free (level->class_shadowed);
    5983    147106033 :       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    439596271 :   gcc_assert (current_binding_level == level);
    5989    439596271 :   leave_scope ();
    5990    439596271 : }
    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    152670878 : set_inherited_value_binding_p (cxx_binding *binding, tree decl,
    5998              :                                tree class_type)
    5999              : {
    6000    152670878 :   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
    6001              :     {
    6002    152294543 :       tree context;
    6003              : 
    6004    152294543 :       if (is_overloaded_fn (decl))
    6005     43844169 :         context = ovl_scope (decl);
    6006              :       else
    6007              :         {
    6008    108450374 :           gcc_assert (DECL_P (decl));
    6009    108450374 :           context = context_for_name_lookup (decl);
    6010              :         }
    6011              : 
    6012    152294543 :       if (is_properly_derived_from (class_type, context))
    6013     18056114 :         INHERITED_VALUE_BINDING_P (binding) = 1;
    6014              :       else
    6015    134238429 :         INHERITED_VALUE_BINDING_P (binding) = 0;
    6016              :     }
    6017       376335 :   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       376335 :     INHERITED_VALUE_BINDING_P (binding) = 1;
    6022              :   else
    6023            0 :     INHERITED_VALUE_BINDING_P (binding) = 0;
    6024    152670878 : }
    6025              : 
    6026              : /* Make the declaration of X appear in CLASS scope.  */
    6027              : 
    6028              : bool
    6029    189535424 : pushdecl_class_level (tree x)
    6030              : {
    6031    189535424 :   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    189535424 :   if (current_class_type != class_binding_level->this_entity)
    6036              :     return true;
    6037              : 
    6038    189535424 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6039              :   /* Get the name of X.  */
    6040    379070848 :   tree name = OVL_NAME (x);
    6041              : 
    6042    189535424 :   if (name)
    6043              :     {
    6044    189140497 :       is_valid = push_class_level_binding (name, x);
    6045    189140497 :       if (TREE_CODE (x) == TYPE_DECL)
    6046    136953997 :         set_identifier_type_value (name, x);
    6047              :     }
    6048       394927 :   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       263128 :       location_t save_location = input_location;
    6054       263128 :       tree anon = TREE_TYPE (x);
    6055       263128 :       if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
    6056       409356 :         for (unsigned ix = member_vec->length (); ix--;)
    6057              :           {
    6058       380167 :             tree binding = (*member_vec)[ix];
    6059       380167 :             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       380167 :             if (!pushdecl_class_level (binding))
    6066            0 :               is_valid = false;
    6067              :         }
    6068              :       else
    6069      1022666 :         for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
    6070       788727 :           if (TREE_CODE (f) == FIELD_DECL)
    6071              :             {
    6072       480954 :               input_location = DECL_SOURCE_LOCATION (f);
    6073       480954 :               if (!pushdecl_class_level (f))
    6074       788727 :                 is_valid = false;
    6075              :             }
    6076       263128 :       input_location = save_location;
    6077              :     }
    6078    189535424 :   return is_valid;
    6079    189535424 : }
    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   2114414764 : get_class_binding (tree name, cp_binding_level *scope)
    6087              : {
    6088   2114414764 :   tree class_type;
    6089   2114414764 :   tree type_binding;
    6090   2114414764 :   tree value_binding;
    6091   2114414764 :   cxx_binding *binding;
    6092              : 
    6093   2114414764 :   class_type = scope->this_entity;
    6094              : 
    6095              :   /* Get the type binding.  */
    6096   2114414764 :   type_binding = lookup_member (class_type, name,
    6097              :                                 /*protect=*/2, /*want_type=*/true,
    6098              :                                 tf_warning_or_error);
    6099              :   /* Get the value binding.  */
    6100   2114414764 :   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   2114414764 :   if (type_binding || value_binding)
    6107              :     {
    6108    152670878 :       binding = new_class_binding (name,
    6109              :                                    value_binding,
    6110              :                                    type_binding,
    6111              :                                    scope);
    6112    152670878 :       set_inherited_value_binding_p (binding, value_binding, class_type);
    6113              :     }
    6114              :   else
    6115              :     binding = NULL;
    6116              : 
    6117   2114414764 :   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    463158950 : push_class_level_binding (tree name, tree x)
    6125              : {
    6126    463158950 :   cxx_binding *binding;
    6127    463158950 :   tree decl = x;
    6128    463158950 :   bool ok;
    6129              : 
    6130    463158950 :   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    463158950 :   if (!class_binding_level)
    6135              :     return true;
    6136              : 
    6137    463158950 :   if (name == error_mark_node)
    6138              :     return false;
    6139              : 
    6140              :   /* Can happen for an erroneous declaration (c++/60384).  */
    6141    463158950 :   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    463158968 :   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    463158947 :   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    463158947 :   if (TREE_CODE (decl) == TREE_LIST
    6158    463158947 :       && TREE_TYPE (decl) == error_mark_node)
    6159            0 :     decl = TREE_VALUE (decl);
    6160              : 
    6161    463158947 :   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    463158899 :   if ((VAR_P (x)
    6182    463158899 :        || TREE_CODE (x) == CONST_DECL
    6183    444885887 :        || (TREE_CODE (x) == TYPE_DECL
    6184    136953976 :            && !DECL_SELF_REFERENCE_P (x))
    6185              :        /* A data member of an anonymous union.  */
    6186    375680545 :        || (TREE_CODE (x) == FIELD_DECL
    6187     27389775 :            && DECL_CONTEXT (x) != current_class_type))
    6188    533150657 :       && 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    463158875 :   binding = IDENTIFIER_BINDING (name);
    6202    463158875 :   if (!binding || binding->scope != class_binding_level)
    6203              :     {
    6204    334604144 :       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    334604144 :       if (binding)
    6208              :         {
    6209     11560192 :           binding->previous = IDENTIFIER_BINDING (name);
    6210     11560192 :           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    140114923 :   if (binding && binding->value)
    6217              :     {
    6218    140114923 :       tree bval = binding->value;
    6219    140114923 :       tree old_decl = NULL_TREE;
    6220    140114923 :       tree target_decl = strip_using_decl (decl);
    6221    140114923 :       tree target_bval = strip_using_decl (bval);
    6222              : 
    6223    140114923 :       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     13875981 :           if (TREE_CODE (target_bval) == TYPE_DECL
    6230      6772734 :               && DECL_ARTIFICIAL (target_bval)
    6231     14809969 :               && !(TREE_CODE (target_decl) == TYPE_DECL
    6232       933978 :                    && DECL_ARTIFICIAL (target_decl)))
    6233              :             {
    6234        80925 :               old_decl = binding->type;
    6235        80925 :               binding->type = bval;
    6236        80925 :               binding->value = NULL_TREE;
    6237        80925 :               INHERITED_VALUE_BINDING_P (binding) = 0;
    6238              :             }
    6239              :           else
    6240              :             {
    6241     13795056 :               old_decl = bval;
    6242              :               /* Any inherited type declaration is hidden by the type
    6243              :                  declaration in the derived class.  */
    6244     13795056 :               if (TREE_CODE (target_decl) == TYPE_DECL
    6245     13795056 :                   && DECL_ARTIFICIAL (target_decl))
    6246       853572 :                 binding->type = NULL_TREE;
    6247              :             }
    6248              :         }
    6249    126238942 :       else if (TREE_CODE (decl) == USING_DECL
    6250        11064 :                && TREE_CODE (bval) == USING_DECL
    6251    126239048 :                && 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    126238909 :       else if (TREE_CODE (decl) == USING_DECL
    6257        11031 :                && TREE_CODE (bval) == USING_DECL
    6258           73 :                && DECL_DEPENDENT_P (decl)
    6259    126238928 :                && DECL_DEPENDENT_P (bval))
    6260              :         return true;
    6261    126238890 :       else if (TREE_CODE (decl) == USING_DECL
    6262        11012 :                && DECL_DEPENDENT_P (decl)
    6263    126248818 :                && OVL_P (target_bval))
    6264              :         /* The new dependent using beats an old overload.  */
    6265              :         old_decl = bval;
    6266    126228962 :       else if (TREE_CODE (bval) == USING_DECL
    6267       668435 :                && DECL_DEPENDENT_P (bval)
    6268    126614593 :                && OVL_P (target_decl))
    6269              :         /* The old dependent using beats a new overload.  */
    6270              :         return true;
    6271    125843343 :       else if (OVL_P (target_decl)
    6272    125631635 :                && OVL_P (target_bval))
    6273              :         /* The new overload set contains the old one.  */
    6274              :         old_decl = bval;
    6275              : 
    6276    139517520 :       if (old_decl && binding->scope == class_binding_level)
    6277              :         {
    6278    139517520 :           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    139517520 :           INHERITED_VALUE_BINDING_P (binding) = 0;
    6283    139517520 :           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    323255717 :   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    323255717 :   if (binding && binding->scope == class_binding_level)
    6296              :     /* Supplement the existing binding.  */
    6297       211765 :     ok = supplement_binding (binding, decl);
    6298              :   else
    6299              :     {
    6300              :       /* Create a new binding.  */
    6301    323043952 :       push_binding (name, decl, class_binding_level);
    6302    323043952 :       ok = true;
    6303              :     }
    6304              : 
    6305              :   return ok;
    6306    463158950 : }
    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     22282779 : lookup_using_decl (tree scope, name_lookup &lookup)
    6314              : {
    6315     22282779 :   tree current = current_scope ();
    6316     22282779 :   bool dependent_p = false;
    6317     22282779 :   tree binfo = NULL_TREE;
    6318     22282779 :   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     22282779 :   if (TREE_CODE (scope) == ENUMERAL_TYPE
    6326     12715371 :       && cxx_dialect < cxx20
    6327     12715371 :       && UNSCOPED_ENUM_P (scope)
    6328     22282789 :       && !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     22282779 :         scope = ctx;
    6336              :     }
    6337              : 
    6338              :   /* You cannot using-decl a destructor.  */
    6339     22282779 :   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     22282776 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    6347              :     {
    6348              :       /* Naming a namespace member.  */
    6349      6645586 :       qualified_namespace_lookup (scope, &lookup);
    6350              : 
    6351      6645586 :       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     15637190 :   else if (TREE_CODE (scope) == ENUMERAL_TYPE)
    6362              :     {
    6363              :       /* Naming an enumeration member.  */
    6364     12715361 :       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     12715361 :       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      2921829 :       tree npscope = scope;
    6376      2921829 :       if (PACK_EXPANSION_P (scope))
    6377         9147 :         npscope = PACK_EXPANSION_PATTERN (scope);
    6378              : 
    6379      2921829 :       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      2921820 :       if (lookup.name == TYPE_IDENTIFIER (npscope)
    6387      2921820 :           || constructor_name_p (lookup.name, npscope))
    6388              :         {
    6389       307188 :           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       307188 :           maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
    6396       307188 :           lookup.name = ctor_identifier;
    6397       307188 :           CLASSTYPE_NON_AGGREGATE (current) = true;
    6398              :         }
    6399              : 
    6400      2921820 :       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      2921817 :       bool depscope = dependent_scope_p (scope);
    6407              : 
    6408      2921817 :       if (depscope)
    6409              :         /* Leave binfo null.  */;
    6410      2050442 :       else if (TYPE_P (current))
    6411              :         {
    6412      2050425 :           binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
    6413      2050425 :           gcc_checking_assert (b_kind >= bk_not_base);
    6414              : 
    6415      2050425 :           if (b_kind == bk_not_base && any_dependent_bases_p ())
    6416              :             /* Treat as-if dependent.  */
    6417              :             depscope = true;
    6418      2050404 :           else if (lookup.name == ctor_identifier
    6419      2050404 :                    && (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      2050410 :           if (b_kind < bk_proper_base)
    6431           52 :             binfo = TYPE_BINFO (scope);
    6432              :         }
    6433              :       else
    6434           17 :         binfo = TYPE_BINFO (scope);
    6435              : 
    6436      4100827 :       dependent_p = (depscope
    6437      2050427 :                      || (IDENTIFIER_CONV_OP_P (lookup.name)
    6438       138072 :                          && dependent_type_p (TREE_TYPE (lookup.name))));
    6439              : 
    6440      2050400 :       if (!dependent_p)
    6441      2050400 :         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      2050400 :       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      2921802 :       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     15637136 :   if (dependent_p)
    6494              :     ;
    6495     21411311 :   else if (!lookup.value)
    6496              :     {
    6497           57 :       error ("%qD has not been declared in %qD", lookup.name, scope);
    6498           57 :       return NULL_TREE;
    6499              :     }
    6500     21411254 :   else if (TREE_CODE (lookup.value) == TREE_LIST
    6501              :            /* We can (independently) have ambiguous implicit typedefs.  */
    6502     21411254 :            || (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     21411251 :   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     22282653 :   if (TYPE_P (current))
    6518              :     {
    6519              :       /* In class scope.  */
    6520              : 
    6521              :       /* Cannot introduce a constructor name.  */
    6522      3103706 :       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      3103703 :       if (lookup.value && BASELINK_P (lookup.value))
    6530              :         /* The binfo from which the functions came does not matter.  */
    6531      1855541 :         lookup.value = BASELINK_FUNCTIONS (lookup.value);
    6532              :     }
    6533              : 
    6534     22282650 :   tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    6535     22282650 :   USING_DECL_SCOPE (using_decl) = scope;
    6536     22282650 :   USING_DECL_DECLS (using_decl) = lookup.value;
    6537     22282650 :   DECL_DEPENDENT_P (using_decl) = dependent_p;
    6538     22282650 :   DECL_CONTEXT (using_decl) = current;
    6539     22282650 :   if (TYPE_P (current) && b_kind == bk_not_base)
    6540      1053378 :     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      3103794 : do_class_using_decl (tree scope, tree name)
    6550              : {
    6551      3103794 :   if (name == error_mark_node
    6552      3103791 :       || scope == error_mark_node)
    6553              :     return NULL_TREE;
    6554              : 
    6555      3103788 :   name_lookup lookup (name);
    6556      3103788 :   return lookup_using_decl (scope, lookup);
    6557      3103788 : }
    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     12158506 : get_namespace_binding (tree ns, tree name)
    6567              : {
    6568     12158506 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6569     12158506 :   if (!ns)
    6570     12158506 :     ns = global_namespace;
    6571     12158506 :   gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
    6572     12158506 :   tree ret = NULL_TREE;
    6573              : 
    6574     12158506 :   if (tree *b = find_namespace_slot (ns, name))
    6575              :     {
    6576      5065429 :       ret = *b;
    6577              : 
    6578      5065429 :       if (TREE_CODE (ret) == BINDING_VECTOR)
    6579            4 :         ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
    6580            4 :       if (ret)
    6581      5065429 :         ret = strip_using_decl (MAYBE_STAT_DECL (ret));
    6582              :     }
    6583              : 
    6584     24317012 :   return ret;
    6585     12158506 : }
    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      3785825 : set_global_binding (tree decl)
    6593              : {
    6594      3785825 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6595              : 
    6596      3785825 :   tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
    6597              : 
    6598      3785825 :   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      3785825 :   *slot = decl;
    6604      3785825 : }
    6605              : 
    6606              : /* Set the context of a declaration to scope. Complain if we are not
    6607              :    outside scope.  */
    6608              : 
    6609              : void
    6610       240278 : set_decl_namespace (tree decl, tree scope, bool friendp)
    6611              : {
    6612              :   /* Get rid of namespace aliases.  */
    6613       240278 :   scope = ORIGINAL_NAMESPACE (scope);
    6614              : 
    6615              :   /* It is ok for friends to be qualified in parallel space.  */
    6616       240278 :   if (!friendp && !is_nested_namespace (current_namespace, scope))
    6617            6 :     error ("declaration of %qD not in a namespace surrounding %qD",
    6618              :            decl, scope);
    6619       240278 :   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    6620              : 
    6621              :   /* See whether this has been declared in the namespace or inline
    6622              :      children.  */
    6623       240278 :   tree old = NULL_TREE;
    6624       240278 :   {
    6625       240278 :     name_lookup lookup (DECL_NAME (decl),
    6626       240278 :                         LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
    6627       240278 :     if (!lookup.search_qualified (scope, /*usings=*/false))
    6628              :       /* No old declaration at all.  */
    6629           30 :       goto not_found;
    6630       240248 :     old = lookup.value;
    6631       240278 :   }
    6632              : 
    6633              :   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
    6634       240248 :   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       240239 :   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        14065 :       if (TREE_CODE (decl) == TREE_CODE (old))
    6652           35 :         DECL_CONTEXT (decl) = DECL_CONTEXT (old);
    6653              : 
    6654        14030 :     found:
    6655              :       /* Writing "N::i" to declare something directly in "N" is invalid.  */
    6656        56857 :       if (CP_DECL_CONTEXT (decl) == current_namespace
    6657        56857 :           && at_namespace_scope_p ())
    6658            3 :         error_at (DECL_SOURCE_LOCATION (decl),
    6659              :                   "explicit qualification in declaration of %qD", decl);
    6660        56857 :       return;
    6661              :     }
    6662              : 
    6663              :   /* Since decl is a function, old should contain a function decl.  */
    6664       226174 :   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       226165 :   if (processing_explicit_instantiation)
    6676              :     return;
    6677       225987 :   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       120333 :   if (friendp && DECL_USE_TEMPLATE (decl))
    6687              :     return;
    6688              : 
    6689        42816 :   tree found = NULL_TREE;
    6690        42816 :   bool hidden_p = false;
    6691        42816 :   bool saw_template = false;
    6692              : 
    6693       104490 :   for (lkp_iterator iter (old); iter; ++iter)
    6694              :     {
    6695        61680 :       if (iter.using_p ())
    6696            0 :         continue;
    6697              : 
    6698        61680 :       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        61680 :       DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
    6703        61680 :       if (decls_match (decl, ofn))
    6704              :         {
    6705        42804 :           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        42798 :           found = ofn;
    6714        42798 :           hidden_p = iter.hidden_p ();
    6715              :         }
    6716        18876 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    6717        18876 :                && TREE_CODE (ofn) == TEMPLATE_DECL)
    6718        61674 :         saw_template = true;
    6719              :     }
    6720              : 
    6721        42810 :   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        42798 :   if (found)
    6734              :     {
    6735        42792 :       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        42792 :       DECL_CONTEXT (decl) = DECL_CONTEXT (found);
    6744        42792 :       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   1485497104 : current_decl_namespace (void)
    6754              : {
    6755   1485497104 :   tree result;
    6756              :   /* If we have been pushed into a different namespace, use it.  */
    6757   1485497104 :   if (!vec_safe_is_empty (decl_namespace_list))
    6758     67273083 :     return decl_namespace_list->last ();
    6759              : 
    6760   1418224021 :   if (current_class_type)
    6761    668171659 :     result = decl_namespace_context (current_class_type);
    6762    750052362 :   else if (current_function_decl)
    6763    277049750 :     result = decl_namespace_context (current_function_decl);
    6764              :   else
    6765    473002612 :     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      6910219 : handle_namespace_attrs (tree ns, tree attributes)
    6774              : {
    6775      6910219 :   tree d;
    6776      6910219 :   bool saw_vis = false;
    6777              : 
    6778      6910219 :   if (attributes == error_mark_node)
    6779              :     return false;
    6780              : 
    6781      9871194 :   for (d = attributes; d; d = TREE_CHAIN (d))
    6782              :     {
    6783      2960975 :       tree name = get_attribute_name (d);
    6784      2960975 :       tree args = TREE_VALUE (d);
    6785              : 
    6786      2960975 :       if (is_attribute_p ("visibility", name)
    6787      2960975 :           && 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      2904603 :           tree x = args ? TREE_VALUE (args) : NULL_TREE;
    6793      2904603 :           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      2904603 :           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      2904603 :           push_visibility (TREE_STRING_POINTER (x), 1);
    6807      2904603 :           saw_vis = true;
    6808              :         }
    6809        56372 :       else if (is_attribute_p ("abi_tag", name)
    6810        56372 :                && is_attribute_namespace_p ("gnu", d))
    6811              :         {
    6812        46584 :           if (!DECL_NAME (ns))
    6813              :             {
    6814            3 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6815              :                        "namespace", name);
    6816            3 :               continue;
    6817              :             }
    6818        46581 :           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        46581 :           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        46581 :           if (check_abi_tag_args (args, name))
    6834        46581 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6835        46581 :                                               DECL_ATTRIBUTES (ns));
    6836              :         }
    6837         9788 :       else if (is_attribute_p ("deprecated", name)
    6838         9788 :                && is_attribute_namespace_p ("", d))
    6839              :         {
    6840         9672 :           if (!DECL_NAME (ns))
    6841              :             {
    6842            7 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6843              :                        "namespace", name);
    6844            7 :               continue;
    6845              :             }
    6846        19263 :           if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
    6847              :             {
    6848            0 :               error ("deprecated message is not a string");
    6849            0 :               continue;
    6850              :             }
    6851         9665 :           TREE_DEPRECATED (ns) = 1;
    6852         9665 :           if (args)
    6853         9598 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6854         9598 :                                               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     82680934 : push_decl_namespace (tree decl)
    6880              : {
    6881     82680934 :   if (TREE_CODE (decl) != NAMESPACE_DECL)
    6882            0 :     decl = decl_namespace_context (decl);
    6883     82680934 :   vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
    6884     82680934 : }
    6885              : 
    6886              : /* [namespace.memdef]/2 */
    6887              : 
    6888              : void
    6889     82680931 : pop_decl_namespace (void)
    6890              : {
    6891     82680931 :   decl_namespace_list->pop ();
    6892     82680931 : }
    6893              : 
    6894              : /* Process a namespace-alias declaration.  */
    6895              : 
    6896              : void
    6897       121379 : do_namespace_alias (location_t loc, tree alias, tree name_space)
    6898              : {
    6899       121379 :   if (name_space == error_mark_node)
    6900              :     return;
    6901              : 
    6902       121370 :   if (TREE_CODE (name_space) == NAMESPACE_DECL)
    6903       121369 :     name_space = ORIGINAL_NAMESPACE (name_space);
    6904              :   else
    6905            1 :     gcc_assert (TREE_CODE (name_space) == SPLICE_EXPR);
    6906              : 
    6907              :   /* Build the alias.  */
    6908       121370 :   alias = build_lang_decl_loc (loc, NAMESPACE_DECL, alias, void_type_node);
    6909       121370 :   DECL_NAMESPACE_ALIAS (alias) = name_space;
    6910       121370 :   DECL_EXTERNAL (alias) = 1;
    6911       121370 :   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
    6912       121370 :   TREE_PUBLIC (alias) = TREE_PUBLIC (CP_DECL_CONTEXT (alias));
    6913              : 
    6914       121370 :   alias = pushdecl (alias);
    6915              : 
    6916       242740 :   if (!DECL_P (alias) || !DECL_NAMESPACE_ALIAS (alias))
    6917              :     return;
    6918              : 
    6919       121367 :   set_originating_module (alias);
    6920       121367 :   check_module_decl_linkage (alias);
    6921              : 
    6922              :   /* Emit debug info for namespace alias.  */
    6923       121367 :   if (!building_stmt_list_p ())
    6924        12125 :     (*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     52953108 : pushdecl_namespace_level (tree decl, bool hiding)
    6932              : {
    6933     52953108 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6934     52953108 :   return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
    6935     52953108 :                                  hiding);
    6936     52953108 : }
    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     12999091 : push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
    6944              : {
    6945     12999091 :   tree type = NULL_TREE;
    6946              : 
    6947     12999091 :   cxx_binding *binding = find_local_binding (current_binding_level, name);
    6948     12999091 :   if (binding)
    6949              :     {
    6950           84 :       value = binding->value;
    6951           84 :       type = binding->type;
    6952              :     }
    6953              : 
    6954     12999091 :   if (lookup)
    6955     12739115 :     do_nonmember_using_decl (*lookup, true, true, &value, &type);
    6956              : 
    6957     12999091 :   if (!value)
    6958              :     ;
    6959     12999091 :   else if (binding && value == binding->value)
    6960              :     /* Redeclaration of this USING_DECL.  */;
    6961           48 :   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     12999019 :     push_local_binding (name, value, /*using=*/true);
    6970              : 
    6971     12999091 :   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     12999091 : }
    6981              : 
    6982              : /* Overload for push_using_decl_bindings that doesn't take a name_lookup.  */
    6983              : 
    6984              : void
    6985       259976 : push_using_decl_bindings (tree name, tree value)
    6986              : {
    6987       259976 :   push_using_decl_bindings (nullptr, name, value);
    6988       259976 : }
    6989              : 
    6990              : /* Process a using declaration in non-class scope.  */
    6991              : 
    6992              : void
    6993     19178991 : finish_nonmember_using_decl (tree scope, tree name)
    6994              : {
    6995     19178991 :   gcc_checking_assert (current_binding_level->kind != sk_class);
    6996              : 
    6997     19178991 :   if (scope == error_mark_node || name == error_mark_node)
    6998           44 :     return;
    6999              : 
    7000     19178991 :   name_lookup lookup (name);
    7001              : 
    7002     19178991 :   tree using_decl = lookup_using_decl (scope, lookup);
    7003     19178991 :   if (!using_decl)
    7004           44 :     return;
    7005              : 
    7006              :   /* Emit debug info.  */
    7007     19178947 :   if (!processing_template_decl)
    7008      6460038 :     cp_emit_debug_info_for_using (lookup.value,
    7009      6460038 :                                   current_binding_level->this_entity);
    7010              : 
    7011     19178947 :   if (current_binding_level->kind == sk_namespace)
    7012              :     {
    7013      6439832 :       tree *slot = find_namespace_slot (current_namespace, name, true);
    7014      6439832 :       tree *mslot = get_fixed_binding_slot (slot, name,
    7015              :                                             BINDING_SLOT_CURRENT, true);
    7016      6439832 :       bool failed = false;
    7017              : 
    7018      6439832 :       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      6439832 :           tree value = MAYBE_STAT_DECL (*mslot);
    7087      6439832 :           tree type = MAYBE_STAT_TYPE (*mslot);
    7088              : 
    7089      6439832 :           do_nonmember_using_decl (lookup, false, true, &value, &type);
    7090              : 
    7091              :           // FIXME: Partition mergeableness?
    7092      6439832 :           if (STAT_HACK_P (*mslot))
    7093              :             {
    7094         1223 :               STAT_DECL (*mslot) = value;
    7095         1223 :               STAT_TYPE (*mslot) = type;
    7096              :             }
    7097      6438609 :           else if (type)
    7098           22 :             *mslot = stat_hack (value, type);
    7099              :           else
    7100      6438587 :             *mslot = value;
    7101              :         }
    7102              :     }
    7103              :   else
    7104              :     {
    7105     12739115 :       add_decl_expr (using_decl);
    7106     12739115 :       if (DECL_DEPENDENT_P (using_decl))
    7107            7 :         lookup.value = using_decl;
    7108     12739115 :       push_using_decl_bindings (&lookup, name, NULL_TREE);
    7109              :     }
    7110     19178991 : }
    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   3544229968 : qualify_lookup (tree val, LOOK_want want)
    7125              : {
    7126   3544229968 :   if (val == NULL_TREE)
    7127              :     return false;
    7128              : 
    7129   3544229968 :   if (bool (want & LOOK_want::TYPE))
    7130              :     {
    7131     43147000 :       tree target_val = strip_using_decl (val);
    7132              : 
    7133     43147000 :       if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
    7134              :         return true;
    7135              :     }
    7136              : 
    7137   3501384022 :   if (bool (want & LOOK_want::TYPE_NAMESPACE))
    7138       510552 :     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         5377 : using_directives_contain_std_p (vec<tree, va_gc> *usings)
    7147              : {
    7148         5377 :   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         1717 : has_using_namespace_std_directive_p ()
    7164              : {
    7165         1717 :   for (cp_binding_level *level = current_binding_level;
    7166         7078 :        level;
    7167         5361 :        level = level->level_chain)
    7168         5377 :     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         1882 : namespace_hints::namespace_hints (location_t loc, tree name)
    7295         1882 : : m_loc(loc), m_name (name)
    7296              : {
    7297         1882 :   auto_vec<tree> worklist;
    7298              : 
    7299         1882 :   m_candidates = vNULL;
    7300         1882 :   m_limited = false;
    7301         1882 :   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         1882 :   worklist.safe_push (global_namespace);
    7306        16430 :   for (unsigned ix = 0; ix != worklist.length (); ix++)
    7307              :     {
    7308         6333 :       tree ns = worklist[ix];
    7309         6333 :       name_lookup lookup (name);
    7310              : 
    7311         6333 :       if (lookup.search_qualified (ns, false))
    7312          132 :         m_candidates.safe_push (lookup.value);
    7313              : 
    7314         6333 :       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         6321 :           auto_vec<tree> children;
    7320              : 
    7321         6321 :           for (tree decl = NAMESPACE_LEVEL (ns)->names;
    7322      5057567 :                decl; decl = TREE_CHAIN (decl))
    7323              :             {
    7324      5051246 :               if (TREE_CODE (decl) == NAMESPACE_DECL
    7325         4587 :                   && !DECL_NAMESPACE_ALIAS (decl)
    7326      5055826 :                   && !DECL_NAMESPACE_INLINE_P (decl))
    7327         4460 :                 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      5051246 :               if (TREE_CODE (decl) == TYPE_DECL)
    7333              :                 {
    7334        54489 :                   tree type = TREE_TYPE (decl);
    7335        54489 :                   if (SCOPED_ENUM_P (type))
    7336         1434 :                     maybe_add_candidate_for_scoped_enum (type, name);
    7337              :                 }
    7338              :             }
    7339              : 
    7340        17093 :           while (!m_limited && !children.is_empty ())
    7341              :             {
    7342         8908 :               if (worklist.length () == m_limit)
    7343            3 :                 m_limited = true;
    7344              :               else
    7345         4451 :                 worklist.safe_push (children.pop ());
    7346              :             }
    7347         6321 :         }
    7348         6333 :     }
    7349         1882 : }
    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         1882 : 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         1882 :   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         1772 :   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         1755 :   gcc_assert (m_candidates.length () == 0);
    7385         1755 :   gcc_assert (m_candidates == vNULL);
    7386              : 
    7387         1755 :   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         1882 : namespace_hints::maybe_decorate_with_limit (name_hint hint)
    7399              : {
    7400         1882 :   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         1879 :     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         1434 : namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
    7415              :                                                       tree name)
    7416              : {
    7417         1434 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    7418              : 
    7419         2390 :   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         1823 : suggest_alternatives_for (location_t location, tree name,
    7444              :                           bool suggest_misspellings)
    7445              : {
    7446              :   /* First, search for exact matches in other namespaces.  */
    7447         1823 :   namespace_hints ns_hints (location, name);
    7448         1823 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    7449              : 
    7450              :   /* Otherwise, try other approaches.  */
    7451         1823 :   if (!result)
    7452         1717 :     result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
    7453              : 
    7454         1823 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    7455         1823 : }
    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         1717 : 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         1717 :   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         1701 :   diagnostics::option_id option_id
    7480         1701 :     = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
    7481         1701 :   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         1695 :   if (!suggest_misspellings)
    7490            0 :     return name_hint ();
    7491              : 
    7492         1695 :   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         4695 : get_cxx_dialect_name (enum cxx_dialect dialect)
    7538              : {
    7539         4695 :   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         1469 :     case cxx17:
    7550         1469 :       return "C++17";
    7551         1602 :     case cxx20:
    7552         1602 :       return "C++20";
    7553           52 :     case cxx23:
    7554           52 :       return "C++23";
    7555         1564 :     case cxx26:
    7556         1564 :       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    542269628 : lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
    7702              : {
    7703    542269628 :   tree t = NULL_TREE;
    7704              : 
    7705    542269628 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    7706              :     {
    7707    406859859 :       name_lookup lookup (name, want);
    7708              : 
    7709    406859859 :       if (qualified_namespace_lookup (scope, &lookup))
    7710              :         {
    7711    398860909 :           t = lookup.value;
    7712              : 
    7713              :           /* If we have a known type overload, pull it out.  This can happen
    7714              :              for using decls.  */
    7715    398860909 :           if (TREE_CODE (t) == OVERLOAD
    7716    244588950 :               && TREE_TYPE (t) != unknown_type_node
    7717    399926884 :               && LIKELY (!cp_preserve_using_decl))
    7718      1065974 :             t = OVL_FUNCTION (t);
    7719              :         }
    7720    406859859 :     }
    7721    135409769 :   else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
    7722     14355796 :     t = lookup_enumerator (scope, name);
    7723    121053973 :   else if (is_class_type (scope, complain))
    7724    121032140 :     t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
    7725              :                        tf_warning_or_error);
    7726              : 
    7727    542247741 :   if (!t)
    7728      8138868 :     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       146375 : tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
    7736              : {
    7737       146375 :   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    413505445 : qualified_namespace_lookup (tree scope, name_lookup *lookup)
    7747              : {
    7748    413505445 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7749    413505445 :   query_oracle (lookup->name);
    7750    413505445 :   bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
    7751    827010890 :   return found;
    7752    413505445 : }
    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      3395785 : 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      3395785 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7799              :     return false;
    7800              : 
    7801      3394996 :   tree suggestion = DECL_NAME (decl);
    7802      3394996 :   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      3394996 :   if (IDENTIFIER_ANON_P (suggestion))
    7808              :     return false;
    7809              : 
    7810      3394996 :   vec.safe_push (suggestion);
    7811              : 
    7812      3394996 :   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      5870091 : maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
    7821              :                               lookup_name_fuzzy_kind kind)
    7822              : {
    7823      5870091 :   tree value = NULL_TREE;
    7824              : 
    7825      5870091 :   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      5869834 :   value = ovl_skip_hidden (value);
    7840      5869834 :   if (value)
    7841              :     {
    7842      5008151 :       value = OVL_FIRST (value);
    7843      5008151 :       if (kind != FUZZY_LOOKUP_TYPENAME
    7844      5008151 :           || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
    7845      3395528 :         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         7353 : 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         7353 :   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         7353 :   bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
    7875              : 
    7876         7353 :   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         2722 :       tree ns = lvl->this_entity;
    7910         2722 :       auto_vec<tree> vec;
    7911              : 
    7912         2722 :       hash_table<named_decl_hash>::iterator end
    7913         2722 :         (DECL_NAMESPACE_BINDINGS (ns)->end ());
    7914      5872903 :       for (hash_table<named_decl_hash>::iterator iter
    7915     11745806 :              (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
    7916              :         {
    7917      5870181 :           tree binding = *iter;
    7918              : 
    7919      5870181 :           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      5869830 :             maybe_add_fuzzy_binding (vec, binding, kind);
    7962              :         }
    7963              : 
    7964    178688152 :       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      3400440 :       for (unsigned ix = vec.length (); ix--;)
    7972              :         {
    7973      3394996 :           const char *str = IDENTIFIER_POINTER (vec[ix]);
    7974              : 
    7975              :           /* Ignore internal names with spaces in them.  */
    7976      3394996 :           if (strchr (str, ' '))
    7977        69985 :             continue;
    7978              : 
    7979              :           /* Don't suggest names that are reserved for use by the
    7980              :              implementation, unless NAME began with an underscore.  */
    7981      6330217 :           if (!consider_implementation_names
    7982      3325011 :               && name_reserved_for_implementation_p (str))
    7983      3005206 :             continue;
    7984              : 
    7985       319805 :           bm.consider (str);
    7986              :         }
    7987         2722 :     }
    7988         7353 : }
    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       476064 : suggest_rid_p  (enum rid rid)
    8048              : {
    8049       476064 :   switch (rid)
    8050              :     {
    8051              :     /* Support suggesting function-like keywords.  */
    8052              :     case RID_STATIC_ASSERT:
    8053              :       return true;
    8054              : 
    8055       471960 :     default:
    8056              :       /* Support suggesting the various decl-specifier words, to handle
    8057              :          e.g. "singed" vs "signed" typos.  */
    8058       471960 :       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         2369 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
    8076              : {
    8077         2369 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    8078              : 
    8079              :   /* Look up function-like macros first; maybe misusing them. */
    8080         4738 :   auto cpp_node = cpp_lookup (parse_in,
    8081         2369 :                               (const unsigned char*)IDENTIFIER_POINTER (name),
    8082         2369 :                               IDENTIFIER_LENGTH (name));
    8083         2369 :   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         2342 :   const char *header_hint
    8092         2342 :     = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
    8093         2342 :   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         2090 :   best_match <tree, const char *> bm (name);
    8101              : 
    8102         2090 :   cp_binding_level *lvl;
    8103         3152 :   for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
    8104         1062 :     consider_binding_level (name, bm, lvl, true, kind);
    8105              : 
    8106        10297 :   for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
    8107         6117 :     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         2090 :   best_macro_match bmm (name, bm.get_best_distance (), parse_in);
    8122         2090 :   cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
    8123              :   /* If a macro is the closest so far to NAME, consider it.  */
    8124         2090 :   if (best_macro)
    8125           27 :     bm.consider ((const char *)best_macro->ident.str);
    8126         2063 :   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       478116 :   for (unsigned i = 0; i < num_c_common_reswords; i++)
    8140              :     {
    8141       476064 :       const c_common_resword *resword = &c_common_reswords[i];
    8142              : 
    8143       476064 :       if (!suggest_rid_p (resword->rid))
    8144       350892 :         continue;
    8145              : 
    8146       125172 :       tree resword_identifier = ridpointers [resword->rid];
    8147       125172 :       if (!resword_identifier)
    8148            0 :         continue;
    8149       125172 :       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       125172 :       if (!IDENTIFIER_KEYWORD_P (resword_identifier))
    8154        11635 :         continue;
    8155              : 
    8156       113537 :       bm.consider (IDENTIFIER_POINTER (resword_identifier));
    8157              :     }
    8158              : 
    8159         2052 :   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    430174865 : binding_to_template_parms_of_scope_p (cxx_binding *binding,
    8173              :                                       cp_binding_level *scope)
    8174              : {
    8175    430174865 :   tree binding_value, tmpl, tinfo;
    8176    430174865 :   int level;
    8177              : 
    8178    430174865 :   if (!binding || !scope || !scope->this_entity)
    8179              :     return false;
    8180              : 
    8181    247863441 :   binding_value = binding->value ?  binding->value : binding->type;
    8182    247863441 :   tinfo = get_template_info (scope->this_entity);
    8183              : 
    8184              :   /* BINDING_VALUE must be a template parm.  */
    8185    247863441 :   if (binding_value == NULL_TREE
    8186    247863441 :       || (!DECL_P (binding_value)
    8187    247863441 :           || !DECL_TEMPLATE_PARM_P (binding_value)))
    8188              :     return false;
    8189              : 
    8190              :   /*  The level of BINDING_VALUE.  */
    8191    495726882 :   level =
    8192    247863441 :     template_type_parameter_p (binding_value)
    8193    247863441 :     ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
    8194              :                          (TREE_TYPE (binding_value)))
    8195     83500043 :     : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
    8196              : 
    8197              :   /* The template of the current scope, iff said scope is a primary
    8198              :      template.  */
    8199    247863441 :   tmpl = (tinfo
    8200    211152951 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
    8201    211152948 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
    8202    247863441 :           ? 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    182827964 :   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   6411715505 : outer_binding (tree name,
    8218              :                cxx_binding *binding,
    8219              :                bool class_p)
    8220              : {
    8221   6411715505 :   cxx_binding *outer;
    8222   6411715505 :   cp_binding_level *scope;
    8223   6411715505 :   cp_binding_level *outer_scope;
    8224              : 
    8225   6411715505 :   if (binding)
    8226              :     {
    8227      7880783 :       scope = binding->scope->level_chain;
    8228      7880783 :       outer = binding->previous;
    8229              :     }
    8230              :   else
    8231              :     {
    8232   6403834722 :       scope = current_binding_level;
    8233   6403834722 :       outer = IDENTIFIER_BINDING (name);
    8234              :     }
    8235   6411715505 :   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   6411715505 :   if (class_p)
    8242  16074557724 :     while (scope && scope != outer_scope && scope->kind != sk_namespace)
    8243              :       {
    8244  11883435236 :         if (scope->kind == sk_class)
    8245              :           {
    8246   1779810620 :             cxx_binding *class_binding;
    8247              : 
    8248   1779810620 :             class_binding = get_class_binding (name, scope);
    8249   1779810620 :             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    141110686 :                 if (BASELINK_P (class_binding->value))
    8255              :                   /* Don't put a BASELINK in IDENTIFIER_BINDING.  */
    8256     39716748 :                   class_binding->value
    8257     39716748 :                     = BASELINK_FUNCTIONS (class_binding->value);
    8258    141110686 :                 class_binding->previous = outer;
    8259    141110686 :                 if (binding)
    8260            3 :                   binding->previous = class_binding;
    8261              :                 else
    8262    141110683 :                   IDENTIFIER_BINDING (name) = class_binding;
    8263    141110686 :                 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   5408518901 :         if (outer_scope && outer_scope->kind == sk_template_parms
    8273  12172499415 :             && binding_to_template_parms_of_scope_p (outer, scope))
    8274              :           return outer;
    8275              : 
    8276  11574424844 :         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    925432939 : innermost_non_namespace_value (tree name)
    8287              : {
    8288    925432939 :   cxx_binding *binding;
    8289    925432939 :   binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
    8290    925432939 :   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   4877233047 : lookup_name (tree name, LOOK_where where, LOOK_want want)
    8340              : {
    8341   4877233047 :   tree val = NULL_TREE;
    8342              : 
    8343   4877233047 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8344              : 
    8345   4877233047 :   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   4877233047 :   gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
    8349              :                        || !bool (where & LOOK_where::NAMESPACE));
    8350   4877233047 :   query_oracle (name);
    8351              : 
    8352              :   /* Conversion operators are handled specially because ordinary
    8353              :      unqualified name lookup will not find template conversion
    8354              :      operators.  */
    8355   4877233047 :   if (IDENTIFIER_CONV_OP_P (name))
    8356              :     {
    8357       998606 :       cp_binding_level *level;
    8358              : 
    8359       998606 :       for (level = current_binding_level;
    8360      2778426 :            level && level->kind != sk_namespace;
    8361      1779820 :            level = level->level_chain)
    8362              :         {
    8363      1954476 :           tree class_type;
    8364      1954476 :           tree operators;
    8365              : 
    8366              :           /* A conversion operator can only be declared in a class
    8367              :              scope.  */
    8368      1954476 :           if (level->kind != sk_class)
    8369       822909 :             continue;
    8370              : 
    8371              :           /* Lookup the conversion operator in the class.  */
    8372      1131567 :           class_type = level->this_entity;
    8373      1131567 :           operators = lookup_fnfields (class_type, name, /*protect=*/0,
    8374              :                                        tf_warning_or_error);
    8375      1131567 :           if (operators)
    8376              :             return operators;
    8377              :         }
    8378              : 
    8379              :       return NULL_TREE;
    8380              :     }
    8381              : 
    8382              :   /* First, look in non-namespace scopes.  */
    8383              : 
    8384   4876234441 :   if (current_class_type == NULL_TREE)
    8385              :     /* Maybe avoid searching the binding stack at all.  */
    8386   1804692477 :     where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
    8387              : 
    8388   4876234441 :   if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
    8389              :     for (cxx_binding *iter = nullptr;
    8390   4883513199 :          (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
    8391              :       {
    8392              :         /* Skip entities we don't want.  */
    8393   4394883194 :         if (!bool (where & (LOCAL_BINDING_P (iter)
    8394              :                             ? LOOK_where::BLOCK : LOOK_where::CLASS)))
    8395        15389 :           continue;
    8396              : 
    8397              :         /* If this is the kind of thing we're looking for, we're done.  */
    8398   3548240183 :         if (iter->value)
    8399              :           {
    8400   3548240183 :             tree binding = NULL_TREE;
    8401              : 
    8402   3485321011 :             if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
    8403   3548240118 :                 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
    8404   3548008209 :                     || !is_lambda_ignored_entity (iter->value))
    8405   7088697349 :                 && qualify_lookup (iter->value, want))
    8406   3540374911 :               binding = iter->value;
    8407      7865272 :             else if (bool (want & LOOK_want::TYPE)
    8408        82231 :                      && !HIDDEN_TYPE_BINDING_P (iter)
    8409      7947488 :                      && iter->type)
    8410   3548240183 :               binding = iter->type;
    8411              : 
    8412   3548240183 :             if (binding)
    8413              :               {
    8414   3540374981 :                 val = strip_using_decl (binding);
    8415   3540374981 :                 break;
    8416              :               }
    8417              :           }
    8418              :       }
    8419              : 
    8420              :   /* Now lookup in namespace scopes.  */
    8421   4876234441 :   if (!val && bool (where & LOOK_where::NAMESPACE))
    8422              :     {
    8423   1335859244 :       name_lookup lookup (name, want);
    8424   1335859244 :       if (lookup.search_unqualified
    8425   1335859244 :           (current_decl_namespace (), current_binding_level))
    8426   1035981144 :         val = lookup.value;
    8427   1335859244 :     }
    8428              : 
    8429              :   /* If we have a known type overload, pull it out.  This can happen
    8430              :      for both using decls and unhidden functions.  */
    8431   4876234441 :   if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
    8432      2762499 :     val = OVL_FUNCTION (val);
    8433              : 
    8434              :   return val;
    8435   4877233047 : }
    8436              : 
    8437              : tree
    8438    308432011 : lookup_name (tree name)
    8439              : {
    8440    308432011 :   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     23583401 : lookup_elaborated_type (tree name, TAG_how how)
    8458              : {
    8459     23583401 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8460              : 
    8461     23583401 :   cp_binding_level *b = current_binding_level;
    8462              : 
    8463     23583401 :   if (b->kind != sk_namespace)
    8464              :     /* Look in non-namespace scopes.  */
    8465              :     for (cxx_binding *iter = NULL;
    8466     16478756 :          (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       938198 :         for (; b != iter->scope; b = b->level_chain)
    8471       612186 :           if (!(b->kind == sk_cleanup
    8472       451226 :                 || b->kind == sk_template_parms
    8473       160969 :                 || b->kind == sk_function_parms
    8474       160939 :                 || (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       486969 :         if (tree type = strip_using_decl (iter->type))
    8488              :           {
    8489        51412 :             if (qualify_lookup (type, LOOK_want::TYPE)
    8490        51412 :                 && (how != TAG_how::CURRENT_ONLY
    8491          121 :                     || LOCAL_BINDING_P (iter)
    8492          121 :                     || DECL_CONTEXT (type) == iter->scope->this_entity))
    8493              :               {
    8494        51363 :                 if (how != TAG_how::HIDDEN_FRIEND)
    8495              :                   /* It is no longer a hidden binding.  */
    8496           75 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8497              : 
    8498        51363 :                 return type;
    8499              :               }
    8500              :           }
    8501              :         else
    8502              :           {
    8503       435557 :             tree value = strip_using_decl (iter->value);
    8504       435557 :             if (qualify_lookup (value, LOOK_want::TYPE)
    8505       435557 :                 && (how != TAG_how::CURRENT_ONLY
    8506       102090 :                     || !INHERITED_VALUE_BINDING_P (iter)))
    8507              :               {
    8508       435425 :                 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
    8509              :                   /* It is no longer a hidden binding.  */
    8510       102096 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    8511              : 
    8512       435425 :                 return value;
    8513              :               }
    8514              :           }
    8515              :       }
    8516              : 
    8517              :   /* Now check if we can look in namespace scope.  */
    8518     36846244 :   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      3896347 :           || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    8523              :       return NULL_TREE;
    8524              : 
    8525              :   /* Look in the innermost namespace.  */
    8526     19551031 :   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     19551031 :   if (modules_p ())
    8532        84365 :     lazy_load_pendings (ns, name);
    8533              : 
    8534     19551031 :   if (tree *slot = find_namespace_slot (ns, name))
    8535              :     {
    8536      3286003 :       tree bind = *slot;
    8537      3286003 :       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      3285931 :           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      3285764 :           else if (tree decl = strip_using_decl (MAYBE_STAT_DECL (bind)))
    8552              :             {
    8553      3285764 :               if (qualify_lookup (decl, LOOK_want::TYPE))
    8554              :                 {
    8555      2935006 :                   if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
    8556      3410180 :                       && STAT_DECL_HIDDEN_P (bind))
    8557              :                     {
    8558       133628 :                       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       133628 :                           if (*slot == bind)
    8565       133625 :                             *slot = decl;
    8566              :                           else
    8567            3 :                             BINDING_VECTOR_CLUSTER (*slot, 0)
    8568            3 :                               .slots[BINDING_SLOT_CURRENT] = decl;
    8569              :                         }
    8570              :                     }
    8571      3276535 :                   return decl;
    8572              :                 }
    8573              :             }
    8574              :         }
    8575              : 
    8576         9301 :       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     23583401 : }
    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     22909033 : maybe_process_template_type_declaration (tree type, int is_friend,
    8673              :                                          cp_binding_level *b)
    8674              : {
    8675     22909033 :   tree decl = TYPE_NAME (type);
    8676              : 
    8677     22909033 :   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     22908883 :   else if (b->kind == sk_namespace
    8686      6759970 :            && 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     22786356 :       gcc_assert (MAYBE_CLASS_TYPE_P (type)
    8693              :                   || TREE_CODE (type) == ENUMERAL_TYPE);
    8694              : 
    8695     22786356 :       if (processing_template_decl)
    8696              :         {
    8697     13541041 :           decl = push_template_decl (decl, is_friend);
    8698     13541041 :           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     13541020 :           if (TREE_CODE (type) != ENUMERAL_TYPE
    8709     13200117 :               && !is_friend && b->kind == sk_template_parms
    8710     10483288 :               && b->level_chain->kind == sk_class)
    8711              :             {
    8712       698693 :               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
    8713              : 
    8714       698693 :               if (!COMPLETE_TYPE_P (current_class_type))
    8715       698672 :                 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     22909033 : pushtag (tree name, tree type, TAG_how how)
    8741              : {
    8742     22909033 :   tree decl;
    8743              : 
    8744     22909033 :   gcc_assert (identifier_p (name));
    8745              : 
    8746     22909033 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8747              : 
    8748     22909033 :   cp_binding_level *b = current_binding_level;
    8749     23041979 :   while (true)
    8750              :     {
    8751     23041979 :       if (/* Cleanup scopes are not scopes from the point of view of
    8752              :              the language.  */
    8753     23041979 :           b->kind == sk_cleanup
    8754              :           /* Neither are function parameter scopes.  */
    8755     23041973 :           || 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     23030024 :           || (b->kind == sk_template_parms
    8761     10964277 :               && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
    8762        12018 :         b = b->level_chain;
    8763     23029961 :       else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
    8764              :         {
    8765       120928 :           b = b->level_chain;
    8766       120928 :           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     22909033 :   if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
    8775              :     {
    8776     22909033 :       tree tdef;
    8777     22909033 :       tree context = TYPE_CONTEXT (type);
    8778              : 
    8779     22909033 :       if (! context)
    8780              :         {
    8781              :           cp_binding_level *cb = b;
    8782     36391140 :           while (cb->kind != sk_namespace
    8783     19846418 :                  && cb->kind != sk_class
    8784     52330916 :                  && (cb->kind != sk_function_parms
    8785      2106431 :                      || !cb->this_entity))
    8786     13833349 :             cb = cb->level_chain;
    8787     22557791 :           tree cs = cb->this_entity;
    8788              : 
    8789     22557791 :           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     22557791 :           if (how == TAG_how::CURRENT_ONLY
    8795       252078 :               || (cs && TREE_CODE (cs) == FUNCTION_DECL))
    8796              :             context = cs;
    8797       251985 :           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       129531 :             context = decl_function_context (get_type_decl (cs));
    8803              :         }
    8804     22557698 :       if (!context)
    8805       251982 :         context = current_namespace;
    8806              : 
    8807     22909033 :       tdef = create_implicit_typedef (name, type);
    8808     22909033 :       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
    8809     22909033 :       set_originating_module (tdef);
    8810              : 
    8811     22909033 :       decl = maybe_process_template_type_declaration
    8812     22909033 :         (type, how == TAG_how::HIDDEN_FRIEND, b);
    8813     22909033 :       if (decl == error_mark_node)
    8814              :         return decl;
    8815              : 
    8816     22909012 :       if (b->kind == sk_class)
    8817              :         {
    8818      3078302 :           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      3076413 :           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      3076070 :             finish_member_declaration (decl);
    8828              :           else
    8829          343 :             pushdecl_class_level (decl);
    8830              :         }
    8831     19830710 :       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     10964196 :           if (b->level_chain->kind != sk_class)
    8836      9784721 :             set_identifier_type_value_with_scope (name, tdef, b->level_chain);
    8837              :         }
    8838              :       else
    8839              :         {
    8840      8866514 :           decl = do_pushdecl_with_scope
    8841      8866514 :             (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
    8842      8866514 :           if (decl == error_mark_node)
    8843              :             return decl;
    8844              : 
    8845      8866466 :           if (DECL_CONTEXT (decl) == std_node
    8846      2328606 :               && init_list_identifier == DECL_NAME (TYPE_NAME (type))
    8847      8866472 :               && !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     22908958 :       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     22908958 :       if (TYPE_FUNCTION_SCOPE_P (type))
    8864              :         {
    8865      2106409 :           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      1198176 :               add_decl_expr (decl);
    8870              :             }
    8871              :           /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead.  */
    8872      1812690 :           else if (!LAMBDA_TYPE_P (type))
    8873       575169 :             determine_local_discriminator (TYPE_NAME (type));
    8874              :         }
    8875              :     }
    8876              : 
    8877     22908958 :   if (b->kind == sk_class
    8878     22908958 :       && !COMPLETE_TYPE_P (current_class_type))
    8879      3076449 :     maybe_add_class_template_decl_list (current_class_type,
    8880              :                                         type, /*friend_p=*/0);
    8881              : 
    8882     22908958 :   decl = TYPE_NAME (type);
    8883     22908958 :   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    8884              : 
    8885              :   /* Set type visibility now if this is a forward declaration.  */
    8886     22908958 :   TREE_PUBLIC (decl) = 1;
    8887     22908958 :   determine_visibility (decl);
    8888     22908958 :   check_module_decl_linkage (decl);
    8889              : 
    8890     22908958 :   return type;
    8891     22909033 : }
    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   2845753961 : store_binding_p (tree id)
    8904              : {
    8905   5686040318 :   if (!id || !IDENTIFIER_BINDING (id))
    8906              :     return false;
    8907              : 
    8908   2616565016 :   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   1076480469 : store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
    8919              : {
    8920   1076480469 :   cxx_saved_binding saved;
    8921              : 
    8922   1076480469 :   gcc_checking_assert (store_binding_p (id));
    8923              : 
    8924   1076480469 :   IDENTIFIER_MARKED (id) = 1;
    8925              : 
    8926   1076480469 :   saved.identifier = id;
    8927   1076480469 :   saved.binding = IDENTIFIER_BINDING (id);
    8928   1076480469 :   saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
    8929   1076480469 :   (*old_bindings)->quick_push (saved);
    8930   1076480469 :   IDENTIFIER_BINDING (id) = NULL;
    8931   1076480469 : }
    8932              : 
    8933              : static void
    8934    601278299 : store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
    8935              : {
    8936    601278299 :   static vec<tree> bindings_need_stored;
    8937    601278299 :   tree t, id;
    8938    601278299 :   size_t i;
    8939              : 
    8940    601278299 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8941   1682618871 :   for (t = names; t; t = TREE_CHAIN (t))
    8942              :     {
    8943    480062273 :       if (TREE_CODE (t) == TREE_LIST)
    8944     16178929 :         id = TREE_PURPOSE (t);
    8945              :       else
    8946    463883344 :         id = DECL_NAME (t);
    8947              : 
    8948    480062273 :       if (store_binding_p (id))
    8949    463604078 :         bindings_need_stored.safe_push (id);
    8950              :     }
    8951    601278299 :   if (!bindings_need_stored.is_empty ())
    8952              :     {
    8953    179896754 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8954    823397586 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8955              :         {
    8956              :           /* We can apparently have duplicates in NAMES.  */
    8957    463604078 :           if (store_binding_p (id))
    8958    463603734 :             store_binding (id, old_bindings);
    8959              :         }
    8960    179896754 :       bindings_need_stored.truncate (0);
    8961              :     }
    8962    601278299 : }
    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    381139843 : store_class_bindings (vec<cp_class_binding, va_gc> *names,
    8969              :                       vec<cxx_saved_binding, va_gc> **old_bindings)
    8970              : {
    8971    381139843 :   static vec<tree> bindings_need_stored;
    8972    381139843 :   size_t i;
    8973    381139843 :   cp_class_binding *cb;
    8974              : 
    8975   1206746984 :   for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
    8976    825607141 :     if (store_binding_p (cb->identifier))
    8977    612876735 :       bindings_need_stored.safe_push (cb->identifier);
    8978    381139843 :   if (!bindings_need_stored.is_empty ())
    8979              :     {
    8980     75301272 :       tree id;
    8981     75301272 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8982    763479279 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8983    612876735 :         store_binding (id, old_bindings);
    8984     75301272 :       bindings_need_stored.truncate (0);
    8985              :     }
    8986    381139843 : }
    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    501660264 : push_to_top_level (void)
    8997              : {
    8998    501660264 :   struct saved_scope *s;
    8999    501660264 :   cp_binding_level *b;
    9000    501660264 :   cxx_saved_binding *sb;
    9001    501660264 :   size_t i;
    9002    501660264 :   bool need_pop;
    9003              : 
    9004    501660264 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9005              : 
    9006              :   /* Reuse or create a new structure for this saved scope.  */
    9007    501660264 :   if (free_saved_scope != NULL)
    9008              :     {
    9009    500439414 :       s = free_saved_scope;
    9010    500439414 :       free_saved_scope = s->prev;
    9011              : 
    9012    500439414 :       vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
    9013    500439414 :       memset (s, 0, sizeof (*s));
    9014              :       /* Also reuse the structure's old_bindings vector.  */
    9015    500439414 :       vec_safe_truncate (old_bindings, 0);
    9016    500439414 :       s->old_bindings = old_bindings;
    9017              :     }
    9018              :   else
    9019      1220850 :     s = ggc_cleared_alloc<saved_scope> ();
    9020              : 
    9021    501660264 :   b = scope_chain ? current_binding_level : 0;
    9022              : 
    9023              :   /* If we're in the middle of some function, save our state.  */
    9024    501660264 :   if (cfun)
    9025              :     {
    9026     86500062 :       need_pop = true;
    9027     86500062 :       push_function_context ();
    9028              :     }
    9029              :   else
    9030              :     need_pop = false;
    9031              : 
    9032    501660264 :   if (scope_chain && previous_class_level)
    9033    139487820 :     store_class_bindings (previous_class_level->class_shadowed,
    9034              :                           &s->old_bindings);
    9035              : 
    9036              :   /* Save and clear any IDENTIFIER_BINDING from local scopes.  */
    9037   1102938563 :   for (; b; b = b->level_chain)
    9038              :     {
    9039   1102840167 :       tree t;
    9040              : 
    9041              :       /* We don't need to consider namespace scopes, they don't affect
    9042              :          IDENTIFIER_BINDING.  */
    9043   1102840167 :       if (b->kind == sk_namespace)
    9044              :         {
    9045              :           /* Jump straight to '::'.  */
    9046    501561868 :           b = NAMESPACE_LEVEL (global_namespace);
    9047    501561868 :           break;
    9048              :         }
    9049              : 
    9050    601278299 :       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    601278299 :       if (b->kind == sk_class)
    9054    241652023 :         store_class_bindings (b->class_shadowed, &s->old_bindings);
    9055              : 
    9056              :       /* Unwind type-value slots back to top level.  */
    9057   1007481200 :       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
    9058    406202901 :         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
    9059              :     }
    9060              : 
    9061   1578140733 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
    9062   1076480469 :     IDENTIFIER_MARKED (sb->identifier) = 0;
    9063              : 
    9064    501660264 :   s->prev = scope_chain;
    9065    501660264 :   s->bindings = b;
    9066    501660264 :   s->need_pop_function_context = need_pop;
    9067    501660264 :   s->function_decl = current_function_decl;
    9068    501660264 :   s->unevaluated_operand = cp_unevaluated_operand;
    9069    501660264 :   s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
    9070    501660264 :   s->suppress_location_wrappers = suppress_location_wrappers;
    9071    501660264 :   s->x_stmt_tree.stmts_are_full_exprs_p = true;
    9072              : 
    9073    501660264 :   scope_chain = s;
    9074    501660264 :   current_function_decl = NULL_TREE;
    9075    501660264 :   current_lang_base = NULL;
    9076    501660264 :   current_lang_name = lang_name_cplusplus;
    9077    501660264 :   current_namespace = global_namespace;
    9078    501660264 :   push_class_stack ();
    9079    501660264 :   cp_unevaluated_operand = 0;
    9080    501660264 :   c_inhibit_evaluation_warnings = 0;
    9081    501660264 :   suppress_location_wrappers = 0;
    9082    501660264 : }
    9083              : 
    9084              : void
    9085    501534778 : pop_from_top_level (void)
    9086              : {
    9087    501534778 :   struct saved_scope *s = scope_chain;
    9088    501534778 :   cxx_saved_binding *saved;
    9089    501534778 :   size_t i;
    9090              : 
    9091    501534778 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9092              : 
    9093    501534778 :   pop_class_stack ();
    9094              : 
    9095    501534778 :   release_tree_vector (current_lang_base);
    9096              : 
    9097    501534778 :   scope_chain = s->prev;
    9098   1578012433 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
    9099              :     {
    9100   1076477655 :       tree id = saved->identifier;
    9101              : 
    9102   1076477655 :       IDENTIFIER_BINDING (id) = saved->binding;
    9103   1076477655 :       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    501534778 :   if (s->need_pop_function_context)
    9109     86500041 :     pop_function_context ();
    9110    501534778 :   current_function_decl = s->function_decl;
    9111    501534778 :   cp_unevaluated_operand = s->unevaluated_operand;
    9112    501534778 :   c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
    9113    501534778 :   suppress_location_wrappers = s->suppress_location_wrappers;
    9114              : 
    9115              :   /* Make this saved_scope structure available for reuse by
    9116              :      push_to_top_level.  */
    9117    501534778 :   s->prev = free_saved_scope;
    9118    501534778 :   free_saved_scope = s;
    9119    501534778 : }
    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      1912899 :   save_and_clear ()
    9135              :   {
    9136      1912899 :     local_state_t s;
    9137      1912899 :     s.cp_unevaluated_operand = ::cp_unevaluated_operand;
    9138      1912899 :     ::cp_unevaluated_operand = 0;
    9139      1912899 :     s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
    9140      1912899 :     ::c_inhibit_evaluation_warnings = 0;
    9141      1912899 :     s.cp_noexcept_operand_ = ::cp_noexcept_operand;
    9142      1912899 :     ::cp_noexcept_operand = 0;
    9143      1912899 :     s.has_cfun = !!cfun;
    9144      1912899 :     if (s.has_cfun)
    9145      1532355 :       push_function_context ();
    9146      1912899 :     return s;
    9147              :   }
    9148              : 
    9149              :   void
    9150      1912899 :   restore () const
    9151              :   {
    9152      1912899 :     ::cp_unevaluated_operand = this->cp_unevaluated_operand;
    9153      1912899 :     ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
    9154      1912899 :     ::cp_noexcept_operand = this->cp_noexcept_operand_;
    9155      1912899 :     if (this->has_cfun)
    9156      1532355 :       pop_function_context ();
    9157      1912899 :   }
    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     72981743 : 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     72981743 :   const bool push_to_top
    9173     73962030 :     = (LAMBDA_FUNCTION_P (d)
    9174     72879158 :        || (TREE_CODE (d) == TYPE_DECL
    9175     33762891 :            && TREE_TYPE (d)
    9176     67128238 :            && LAMBDA_TYPE_P (TREE_TYPE (d)))
    9177     72116421 :        || !current_function_decl
    9178     96531260 :        || !decl_function_context (d));
    9179              : 
    9180     72981743 :   if (push_to_top)
    9181     71068844 :     push_to_top_level ();
    9182              :   else
    9183              :     {
    9184      1912899 :       gcc_assert (!processing_template_decl);
    9185      1912899 :       local_state_stack.safe_push (local_state_t::save_and_clear ());
    9186              :     }
    9187              : 
    9188     72981743 :   return push_to_top;
    9189              : }
    9190              : 
    9191              : /* Return from whatever maybe_push_to_top_level did.  */
    9192              : 
    9193              : void
    9194     72981734 : maybe_pop_from_top_level (bool push_to_top)
    9195              : {
    9196     72981734 :   if (push_to_top)
    9197     71068835 :     pop_from_top_level ();
    9198              :   else
    9199      1912899 :     local_state_stack.pop ().restore ();
    9200     72981734 : }
    9201              : 
    9202              : /* Push into the scope of the namespace NS, even if it is deeply
    9203              :    nested within another namespace.  */
    9204              : 
    9205              : void
    9206     89798106 : push_nested_namespace (tree ns)
    9207              : {
    9208     89798106 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9209     89798106 :   if (ns == global_namespace)
    9210     43862471 :     push_to_top_level ();
    9211              :   else
    9212              :     {
    9213     89002024 :       push_nested_namespace (CP_DECL_CONTEXT (ns));
    9214     45935635 :       resume_scope (NAMESPACE_LEVEL (ns));
    9215     45935635 :       current_namespace = ns;
    9216              :     }
    9217     89798106 : }
    9218              : 
    9219              : /* Pop back from the scope of the namespace NS, which was previously
    9220              :    entered with push_nested_namespace.  */
    9221              : 
    9222              : void
    9223     43851677 : pop_nested_namespace (tree ns)
    9224              : {
    9225     43851677 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9226    133638989 :   while (ns != global_namespace)
    9227              :     {
    9228     45935635 :       ns = CP_DECL_CONTEXT (ns);
    9229     45935635 :       current_namespace = ns;
    9230     45935635 :       leave_scope ();
    9231              :     }
    9232              : 
    9233     43851677 :   pop_from_top_level ();
    9234     43851677 : }
    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       195293 : add_using_namespace (vec<tree, va_gc> *&usings, tree target,
    9243              :                      bool imported = false)
    9244              : {
    9245              :   /* Find if this using already exists.  */
    9246       195293 :   tree old = NULL_TREE;
    9247       195293 :   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       195293 :   tree decl = old;
    9256       195293 :   if (!decl)
    9257              :     {
    9258       194764 :       decl = build_lang_decl (USING_DECL, NULL_TREE, NULL_TREE);
    9259       194764 :       USING_DECL_DECLS (decl) = target;
    9260       194764 :       DECL_MODULE_IMPORT_P (decl) = imported;
    9261              :     }
    9262              : 
    9263              :   /* Update module flags in case that has changed.  */
    9264       195293 :   if (modules_p ())
    9265              :     {
    9266         1148 :       if (module_purview_p ())
    9267          485 :         DECL_MODULE_PURVIEW_P (decl) = true;
    9268         1148 :       if (module_exporting_p ())
    9269          282 :         DECL_MODULE_EXPORT_P (decl) = true;
    9270         1148 :       if (!imported)
    9271          941 :         DECL_MODULE_IMPORT_P (decl) = false;
    9272              :     }
    9273              : 
    9274       195293 :   if (!old)
    9275       194764 :     vec_safe_push (usings, decl);
    9276       195293 : }
    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       208901 : emit_debug_info_using_namespace (tree from, tree target, bool implicit)
    9293              : {
    9294              :   /* Emit debugging info.  */
    9295       208901 :   tree context = from != global_namespace ? from : NULL_TREE;
    9296       208901 :   debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
    9297              :                                         implicit);
    9298       208901 : }
    9299              : 
    9300              : /* Process a using directive.  */
    9301              : 
    9302              : void
    9303       193750 : finish_using_directive (tree target, tree attribs)
    9304              : {
    9305       193750 :   if (target == error_mark_node)
    9306              :     return;
    9307              : 
    9308       193730 :   if (current_binding_level->kind != sk_namespace)
    9309       162824 :     add_stmt (build_stmt (input_location, USING_STMT, target));
    9310              :   else
    9311        61812 :     emit_debug_info_using_namespace (current_binding_level->this_entity,
    9312        30906 :                                      ORIGINAL_NAMESPACE (target), false);
    9313              : 
    9314       387460 :   add_using_namespace (current_binding_level->using_directives,
    9315       193730 :                        ORIGINAL_NAMESPACE (target));
    9316              : 
    9317       193730 :   bool diagnosed = false;
    9318       193730 :   if (attribs != error_mark_node)
    9319       193807 :     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       368276 : pushdecl_top_level (tree x)
    9364              : {
    9365       368276 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9366       368276 :   push_to_top_level ();
    9367       368276 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9368       368276 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9369       368276 :   x = pushdecl_namespace_level (x);
    9370       368276 :   pop_from_top_level ();
    9371       736552 :   return x;
    9372       368276 : }
    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      2253869 : pushdecl_top_level_and_finish (tree x, tree init)
    9379              : {
    9380      2253869 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9381      2253869 :   push_to_top_level ();
    9382      2253869 :   gcc_checking_assert (!DECL_CONTEXT (x));
    9383      2253869 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    9384      2253869 :   x = pushdecl_namespace_level (x);
    9385      2253869 :   cp_finish_decl (x, init, false, NULL_TREE, 0);
    9386      2253869 :   pop_from_top_level ();
    9387      4507738 :   return x;
    9388      2253869 : }
    9389              : 
    9390              : /* Enter the namespaces from current_namerspace to NS.  */
    9391              : 
    9392              : static int
    9393      6350754 : push_inline_namespaces (tree ns)
    9394              : {
    9395      6350754 :   int count = 0;
    9396      6350754 :   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      6350754 :   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       892376 : make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
    9435              : {
    9436              :   /* Create the namespace.  */
    9437       892376 :   tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
    9438       892376 :   DECL_SOURCE_LOCATION (ns) = loc;
    9439       892376 :   SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
    9440       892376 :   if (!SCOPE_DEPTH (ns))
    9441              :     /* We only allow depth 255. */
    9442            0 :     sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
    9443       892376 :   DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
    9444              : 
    9445       892376 :   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         1429 :     SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
    9451       890947 :   else if (TREE_PUBLIC (ctx))
    9452       890868 :     TREE_PUBLIC (ns) = true;
    9453              : 
    9454       892376 :   if (inline_p)
    9455       176581 :     DECL_NAMESPACE_INLINE_P (ns) = true;
    9456              : 
    9457       892376 :   return ns;
    9458              : }
    9459              : 
    9460              : /* NS was newly created, finish off making it.  */
    9461              : 
    9462              : static void
    9463       892394 : make_namespace_finish (tree ns, tree *slot, bool from_import = false)
    9464              : {
    9465       892394 :   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       892394 :   tree ctx = CP_DECL_CONTEXT (ns);
    9474       892394 :   cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
    9475       892394 :   scope->this_entity = ns;
    9476       892394 :   scope->more_cleanups_ok = true;
    9477       892394 :   scope->kind = sk_namespace;
    9478       892394 :   scope->level_chain = NAMESPACE_LEVEL (ctx);
    9479       892394 :   NAMESPACE_LEVEL (ns) = scope;
    9480              : 
    9481       892394 :   if (DECL_NAMESPACE_INLINE_P (ns))
    9482       176581 :     vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
    9483              : 
    9484       892394 :   if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
    9485       177995 :     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       892394 :   if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
    9490         1414 :     add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns,
    9491              :                          from_import);
    9492       892394 : }
    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        34433 : expose_existing_namespace (tree ns)
    9499              : {
    9500        34433 :   if (!modules_p ())
    9501              :     return;
    9502              : 
    9503        34433 :   tree bind = *find_namespace_slot (CP_DECL_CONTEXT (ns), DECL_NAME (ns));
    9504        34433 :   if (bind != ns)
    9505              :     {
    9506         1227 :       auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
    9507         1227 :       binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
    9508         1227 :       gcc_checking_assert (!(tree)slot || (tree)slot == ns);
    9509         1227 :       slot = ns;
    9510              :     }
    9511              : 
    9512        34433 :   if (module_purview_p ())
    9513        14561 :     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      7241359 : push_namespace (tree name, bool make_inline)
    9523              : {
    9524      7241359 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9525      7241359 :   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      7241359 :   gcc_checking_assert (global_namespace != NULL && name != global_identifier);
    9531              : 
    9532      7241359 :   tree ns = NULL_TREE;
    9533      7241359 :   {
    9534      7241359 :     name_lookup lookup (name);
    9535      7241359 :     if (!lookup.search_qualified (current_namespace, /*usings=*/false))
    9536              :       ;
    9537      6350751 :     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      6350748 :     else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    9572              :       ns = lookup.value;
    9573              : 
    9574      6350739 :     if (ns)
    9575      6350739 :       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      7241359 :   }
    9591              : 
    9592      7241359 :   if (ns)
    9593              :     {
    9594              :       /* DR2061.  NS might be a member of an inline namespace.  We
    9595              :          need to push into those namespaces.  */
    9596      6350736 :       if (modules_p ())
    9597        67277 :         for (tree ctx = ns; ctx != current_namespace;
    9598        26231 :              ctx = CP_DECL_CONTEXT (ctx))
    9599        26231 :           expose_existing_namespace (ctx);
    9600              : 
    9601      6350736 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    9602      6350736 :       if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
    9603              :         /* It's not builtin now.  */
    9604        34552 :         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       890623 :       tree *slot = find_namespace_slot (current_namespace, name, false);
    9611       890623 :       if (slot)
    9612           51 :         ns = reuse_namespace (slot, current_namespace, name);
    9613       890623 :       if (!ns)
    9614       890593 :         ns = make_namespace (current_namespace, name,
    9615              :                              input_location, make_inline);
    9616              : 
    9617       890623 :       if (pushdecl (ns) == error_mark_node)
    9618              :         ns = NULL_TREE;
    9619              :       else
    9620              :         {
    9621              :           /* Finish up making the namespace.  */
    9622       890611 :           add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
    9623       890611 :           if (!slot)
    9624              :             {
    9625       890572 :               slot = find_namespace_slot (current_namespace, name);
    9626              :               /* This should find the slot created by pushdecl.  */
    9627       890572 :               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       890611 :           make_namespace_finish (ns, slot);
    9637              :         }
    9638              :     }
    9639              : 
    9640      7241347 :   if (ns)
    9641              :     {
    9642              :       /* A public namespace is exported only if explicitly marked, or
    9643              :          it contains exported entities.  */
    9644      7241347 :       if (module_exporting_p ())
    9645              :         {
    9646        12099 :           if (TREE_PUBLIC (ns))
    9647        12056 :             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      7241347 :       if (module_purview_p ())
    9663        13357 :         DECL_MODULE_PURVIEW_P (ns) = true;
    9664              : 
    9665      7561248 :       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      7241347 :       resume_scope (NAMESPACE_LEVEL (ns));
    9673      7241347 :       current_namespace = ns;
    9674      7241347 :       count++;
    9675              :     }
    9676              : 
    9677     14482718 :   return count;
    9678      7241359 : }
    9679              : 
    9680              : /* Pop from the scope of the current namespace.  */
    9681              : 
    9682              : void
    9683      7295168 : pop_namespace (void)
    9684              : {
    9685      7295168 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    9686              : 
    9687      7295168 :   gcc_assert (current_namespace != global_namespace);
    9688      7295168 :   current_namespace = CP_DECL_CONTEXT (current_namespace);
    9689              :   /* The binding level is not popped, as it might be re-opened later.  */
    9690      7295168 :   leave_scope ();
    9691      7295168 : }
    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        96888 : pop_everything (void)
    9762              : {
    9763        96888 :   if (ENABLE_SCOPE_CHECKING)
    9764              :     verbatim ("XXX entering %<pop_everything ()%>");
    9765        96906 :   while (!namespace_bindings_p ())
    9766              :     {
    9767           18 :       if (current_binding_level->kind == sk_class)
    9768            0 :         pop_nested_class ();
    9769              :       else
    9770           18 :         poplevel (0, 0, 0);
    9771              :     }
    9772        96888 :   if (ENABLE_SCOPE_CHECKING)
    9773              :     verbatim ("XXX leaving %<pop_everything ()%>");
    9774        96888 : }
    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      8590255 : 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      8590255 :   if (seen_error ())
    9785              :     return;
    9786              : 
    9787              :   /* Do not supply context to imported_module_or_decl, if
    9788              :      it is a global namespace.  */
    9789      8586350 :   if (context == global_namespace)
    9790       176058 :     context = NULL_TREE;
    9791              : 
    9792      8586350 :   t = MAYBE_BASELINK_FUNCTIONS (t);
    9793              : 
    9794     18698313 :   for (lkp_iterator iter (t); iter; ++iter)
    9795              :     {
    9796     10111963 :       tree fn = *iter;
    9797              : 
    9798     10111963 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    9799              :         /* FIXME: Handle TEMPLATE_DECLs.  */
    9800       850666 :         continue;
    9801              : 
    9802              :       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
    9803              :          of a builtin function.  */
    9804     12072541 :       if (TREE_CODE (fn) == FUNCTION_DECL
    9805      7661246 :           && DECL_EXTERNAL (fn)
    9806     16913056 :           && fndecl_built_in_p (fn))
    9807      2811244 :         continue;
    9808              : 
    9809      6450053 :       if (building_stmt_list_p ())
    9810         1206 :         add_stmt (build_stmt (input_location, USING_STMT, fn));
    9811              :       else
    9812      6448847 :         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     60745647 : dependent_local_decl_p (tree d)
    9822              : {
    9823     60745647 :   if (!DECL_LOCAL_DECL_P (d))
    9824              :     return false;
    9825              : 
    9826        38957 :   cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
    9827        38957 :   cp_binding_level *l = b->scope;
    9828        98806 :   while (!l->this_entity)
    9829        59849 :     l = l->level_chain;
    9830        38957 :   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.