LCOV - code coverage report
Current view: top level - gcc/cp - name-lookup.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 3459 3686 93.8 %
Date: 2023-07-19 08:18:47 Functions: 217 226 96.0 %

          Line data    Source code
       1             : /* Definitions for C++ name lookup routines.
       2             :    Copyright (C) 2003-2023 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             : #define INCLUDE_MEMORY
      23             : #include "system.h"
      24             : #include "coretypes.h"
      25             : #include "cp-tree.h"
      26             : #include "timevar.h"
      27             : #include "stringpool.h"
      28             : #include "print-tree.h"
      29             : #include "attribs.h"
      30             : #include "debug.h"
      31             : #include "c-family/c-pragma.h"
      32             : #include "gcc-rich-location.h"
      33             : #include "spellcheck-tree.h"
      34             : #include "parser.h"
      35             : #include "c-family/name-hint.h"
      36             : #include "c-family/known-headers.h"
      37             : #include "c-family/c-spellcheck.h"
      38             : #include "bitmap.h"
      39             : 
      40             : static cxx_binding *cxx_binding_make (tree value, tree type);
      41             : static cp_binding_level *innermost_nonclass_level (void);
      42             : static void set_identifier_type_value_with_scope (tree id, tree decl,
      43             :                                                   cp_binding_level *b);
      44             : static name_hint maybe_suggest_missing_std_header (location_t location,
      45             :                                                    tree name);
      46             : static name_hint suggest_alternatives_for_1 (location_t location, tree name,
      47             :                                              bool suggest_misspellings);
      48             : 
      49             : /* Slots in BINDING_VECTOR.  */
      50             : enum binding_slots
      51             : {
      52             :  BINDING_SLOT_CURRENT,  /* Slot for current TU.  */
      53             :  BINDING_SLOT_GLOBAL,   /* Slot for merged global module. */
      54             :  BINDING_SLOT_PARTITION, /* Slot for merged partition entities
      55             :                             (optional).  */
      56             : 
      57             :  /* Number of always-allocated slots.  */
      58             :  BINDING_SLOTS_FIXED = BINDING_SLOT_GLOBAL + 1
      59             : };
      60             : 
      61             : /* Create an overload suitable for recording an artificial TYPE_DECL
      62             :    and another decl.  We use this machanism to implement the struct
      63             :    stat hack.  */
      64             : 
      65             : #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
      66             : #define STAT_TYPE_VISIBLE_P(N) TREE_USED (OVERLOAD_CHECK (N))
      67             : #define STAT_TYPE(N) TREE_TYPE (N)
      68             : #define STAT_DECL(N) OVL_FUNCTION (N)
      69             : #define STAT_VISIBLE(N) OVL_CHAIN (N)
      70             : #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
      71             : #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
      72             : 
      73             : /* When a STAT_HACK_P is true, OVL_USING_P and OVL_EXPORT_P are valid
      74             :    and apply to the hacked type.  */
      75             : 
      76             : /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
      77             :    But we also need to indicate hiddenness on implicit type decls
      78             :    (injected friend classes), and (coming soon) decls injected from
      79             :    block-scope externs.  It is too awkward to press the existing
      80             :    overload marking for that.  If we have a hidden non-function, we
      81             :    always create a STAT_HACK, and use these two markers as needed.  */
      82             : #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
      83             : #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
      84             : 
      85             : /* Create a STAT_HACK node with DECL as the value binding and TYPE as
      86             :    the type binding.  */
      87             : 
      88             : static tree
      89      414951 : stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
      90             : {
      91      414951 :   tree result = make_node (OVERLOAD);
      92             : 
      93             :   /* Mark this as a lookup, so we can tell this is a stat hack.  */
      94      414951 :   OVL_LOOKUP_P (result) = true;
      95      414951 :   STAT_DECL (result) = decl;
      96      414951 :   STAT_TYPE (result) = type;
      97      414951 :   return result;
      98             : }
      99             : 
     100             : /* Create a local binding level for NAME.  */
     101             : 
     102             : static cxx_binding *
     103   534259302 : create_local_binding (cp_binding_level *level, tree name)
     104             : {
     105   534259302 :   cxx_binding *binding = cxx_binding_make (NULL, NULL);
     106             : 
     107   534259302 :   LOCAL_BINDING_P (binding) = true;
     108   534259302 :   binding->scope = level;
     109   534259302 :   binding->previous = IDENTIFIER_BINDING (name);
     110             : 
     111   534259302 :   IDENTIFIER_BINDING (name) = binding;
     112             :   
     113   534259302 :   return binding;
     114             : }
     115             : 
     116             : /* Find the binding for NAME in namespace NS.  If CREATE_P is true,
     117             :    make an empty binding if there wasn't one.  */
     118             : 
     119             : static tree *
     120  5085426515 : find_namespace_slot (tree ns, tree name, bool create_p = false)
     121             : {
     122  5085426515 :   tree *slot = DECL_NAMESPACE_BINDINGS (ns)
     123  9862254844 :     ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
     124             :                            create_p ? INSERT : NO_INSERT);
     125  5085426515 :   return slot;
     126             : }
     127             : 
     128             : static tree
     129       25554 : find_namespace_value (tree ns, tree name)
     130             : {
     131       25554 :   tree *b = find_namespace_slot (ns, name);
     132             : 
     133       25554 :   return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
     134             : }
     135             : 
     136             : /* Look in *SLOT for a the binding of NAME in imported module IX.
     137             :    Returns pointer to binding's slot, or NULL if not found.  Does a
     138             :    binary search, as this is mainly used for random access during
     139             :    importing.  Do not use for the fixed slots.  */
     140             : 
     141             : static binding_slot *
     142      192945 : search_imported_binding_slot (tree *slot, unsigned ix)
     143             : {
     144      192945 :   gcc_assert (ix);
     145             : 
     146      192945 :   if (!*slot)
     147             :     return NULL;
     148             : 
     149      192945 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
     150             :     return NULL;
     151             :   
     152      192945 :   unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     153      192945 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
     154             : 
     155      192945 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     156             :     {
     157      192945 :       clusters--;
     158      192945 :       cluster++;
     159             :     }
     160             : 
     161      385929 :   while (clusters > 1)
     162             :     {
     163          39 :       unsigned half = clusters / 2;
     164          39 :       gcc_checking_assert (cluster[half].indices[0].span);
     165          39 :       if (cluster[half].indices[0].base > ix)
     166             :         clusters = half;
     167             :       else
     168             :         {
     169          30 :           clusters -= half;
     170          30 :           cluster += half;
     171             :         }
     172             :     }
     173             : 
     174      192945 :   if (clusters)
     175             :     /* Is it in this cluster?  */
     176      193744 :     for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     177             :       {
     178      193744 :         if (!cluster->indices[off].span)
     179             :           break;
     180      193744 :         if (cluster->indices[off].base > ix)
     181             :           break;
     182             : 
     183      193744 :         if (cluster->indices[off].base + cluster->indices[off].span > ix)
     184      192945 :           return &cluster->slots[off];
     185             :       }
     186             : 
     187             :   return NULL;
     188             : }
     189             : 
     190             : static void
     191      169317 : init_global_partition (binding_cluster *cluster, tree decl)
     192             : {
     193      169317 :   bool named = true;
     194             : 
     195      169317 :   if (header_module_p ())
     196             :     named = false;
     197      169232 :   else if (TREE_PUBLIC (decl)
     198      169232 :            && TREE_CODE (decl) == NAMESPACE_DECL
     199      170370 :            && !DECL_NAMESPACE_ALIAS (decl))
     200             :     named = false;
     201      168094 :   else if (!get_originating_module (decl))
     202             :     named = false;
     203             : 
     204           0 :   binding_slot *mslot;
     205           0 :   if (named)
     206           0 :     mslot = &cluster[BINDING_SLOT_PARTITION
     207             :                      / BINDING_VECTOR_SLOTS_PER_CLUSTER]
     208             :       .slots[BINDING_SLOT_PARTITION
     209             :              % BINDING_VECTOR_SLOTS_PER_CLUSTER];
     210             :   else
     211      169317 :     mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
     212             : 
     213      169317 :   if (*mslot)
     214       33896 :     decl = ovl_make (decl, *mslot);
     215      169317 :   *mslot = decl;
     216             : 
     217      169317 :   if (TREE_CODE (decl) == CONST_DECL)
     218             :     {
     219        6567 :       tree type = TREE_TYPE (decl);
     220        6567 :       if (TREE_CODE (type) == ENUMERAL_TYPE
     221        6567 :           && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type)))
     222       11161 :           && decl == TREE_VALUE (TYPE_VALUES (type)))
     223             :         /* Anonymous enums are keyed by their first enumerator, put
     224             :            the TYPE_DECL here too.  */
     225         352 :         *mslot = ovl_make (TYPE_NAME (type), *mslot);
     226             :     }
     227      169317 : }
     228             : 
     229             : /* Get the fixed binding slot IX.  Creating the vector if CREATE is
     230             :    non-zero.  If CREATE is < 0, make sure there is at least 1 spare
     231             :    slot for an import.  (It is an error for CREATE < 0 and the slot to
     232             :    already exist.)  */
     233             : 
     234             : static tree *
     235   305018964 : get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
     236             : {
     237   305018964 :   gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
     238             : 
     239             :   /* An assumption is that the fixed slots all reside in one cluster.  */
     240   305018964 :   gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
     241             : 
     242   305018964 :   if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
     243             :     {
     244   304801665 :       if (ix == BINDING_SLOT_CURRENT)
     245             :         /* The current TU can just use slot directly.  */
     246             :         return slot;
     247             : 
     248      246825 :       if (!create)
     249             :         return NULL;
     250             : 
     251             :       /* The partition slot is only needed when we're a named
     252             :          module.  */
     253      246825 :       bool partition_slot = named_module_p ();
     254      246825 :       unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
     255      246825 :                         + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
     256      246825 :                        / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     257      246825 :       tree new_vec = make_binding_vec (name, want);
     258      246825 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
     259      246825 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
     260             : 
     261             :       /* Initialize the fixed slots.  */
     262      246825 :       for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
     263             :         {
     264      493650 :           cluster[0].indices[jx].base = 0;
     265      493650 :           cluster[0].indices[jx].span = 1;
     266      740475 :           cluster[0].slots[jx] = NULL_TREE;
     267             :         }
     268             : 
     269      246825 :       if (partition_slot)
     270             :         {
     271       28138 :           unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     272       28138 :           unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
     273       28138 :           cluster[ind].indices[off].base = 0;
     274       28138 :           cluster[ind].indices[off].span = 1;
     275       28138 :           cluster[ind].slots[off] = NULL_TREE;
     276             :         }
     277             : 
     278      246825 :       if (tree orig = *slot)
     279             :         {
     280             :           /* Propagate existing value to current slot.  */
     281             : 
     282             :           /* Propagate global & module entities to the global and
     283             :              partition slots.  */
     284      135421 :           if (tree type = MAYBE_STAT_TYPE (orig))
     285          38 :             init_global_partition (cluster, type);
     286             : 
     287      609400 :           for (ovl_iterator iter (MAYBE_STAT_DECL (orig)); iter; ++iter)
     288             :             {
     289      169279 :               tree decl = *iter;
     290             : 
     291             :               /* Internal linkage entities are in deduplicateable.  */
     292      169279 :               init_global_partition (cluster, decl);
     293             :             }
     294             : 
     295      135421 :           if (cluster[0].slots[BINDING_SLOT_GLOBAL]
     296      135421 :               && !(TREE_CODE (orig) == NAMESPACE_DECL
     297      136584 :                    && !DECL_NAMESPACE_ALIAS (orig)))
     298             :             {
     299             :               /* Note that we had some GMF entries.  */
     300      134268 :               if (!STAT_HACK_P (orig))
     301      134224 :                 orig = stat_hack (orig);
     302             : 
     303      134268 :               MODULE_BINDING_GLOBAL_P (orig) = true;
     304             :             }
     305             : 
     306      135421 :           cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
     307             :         }
     308             : 
     309      246825 :       *slot = new_vec;
     310      246825 :     }
     311             :   else
     312      217299 :     gcc_checking_assert (create >= 0);
     313             : 
     314      464124 :   unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
     315      464124 :   binding_cluster &cluster
     316      464124 :     = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
     317             : 
     318             :   /* There must always be slots for these indices  */
     319      464124 :   gcc_checking_assert (cluster.indices[off].span == 1
     320             :                        && !cluster.indices[off].base
     321             :                        && !cluster.slots[off].is_lazy ());
     322             : 
     323      464124 :   return reinterpret_cast<tree *> (&cluster.slots[off]);
     324             : }
     325             : 
     326             : /* *SLOT is a namespace binding slot.  Append a slot for imported
     327             :    module IX.  */
     328             : 
     329             : static binding_slot *
     330      238595 : append_imported_binding_slot (tree *slot, tree name, unsigned ix)
     331             : {
     332      238595 :   gcc_checking_assert (ix);
     333             : 
     334      238595 :   if (!*slot ||  TREE_CODE (*slot) != BINDING_VECTOR)
     335             :     /* Make an initial module vector.  */
     336      230928 :     get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
     337        7667 :   else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
     338        7667 :            ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
     339             :     /* There is space in the last cluster.  */;
     340        7027 :   else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
     341        7027 :            != BINDING_VECTOR_ALLOC_CLUSTERS (*slot))
     342             :     /* There is space in the vector.  */
     343           0 :     BINDING_VECTOR_NUM_CLUSTERS (*slot)++;
     344             :   else
     345             :     {
     346             :       /* Extend the vector.  */
     347        7027 :       unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
     348        7027 :       unsigned want = (have * 3 + 1) / 2;
     349             : 
     350        7027 :       if (want > (unsigned short)~0)
     351           0 :         want = (unsigned short)~0;
     352             : 
     353        7027 :       tree new_vec = make_binding_vec (name, want);
     354        7027 :       BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
     355       14054 :       memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
     356       14054 :               BINDING_VECTOR_CLUSTER_BASE (*slot),
     357        7027 :               have * sizeof (binding_cluster));
     358        7027 :       *slot = new_vec;
     359             :     }
     360             : 
     361      238595 :   binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
     362      262892 :   for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
     363      262892 :     if (!last->indices[off].span)
     364             :       {
     365             :         /* Fill the free slot of the cluster.  */
     366      238595 :         last->indices[off].base = ix;
     367      238595 :         last->indices[off].span = 1;
     368      238595 :         last->slots[off] = NULL_TREE;
     369             :         /* Check monotonicity.  */
     370      477190 :         gcc_checking_assert (last[off ? 0 : -1]
     371             :                              .indices[off ? off - 1
     372             :                                       : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
     373             :                              .base < ix);
     374      238595 :         return &last->slots[off];
     375             :       }
     376             : 
     377           0 :   gcc_unreachable ();
     378             : }
     379             : 
     380             : /* Add DECL to the list of things declared in binding level B.  */
     381             : 
     382             : static void
     383   847771133 : add_decl_to_level (cp_binding_level *b, tree decl)
     384             : {
     385   847771133 :   gcc_assert (b->kind != sk_class);
     386             : 
     387             :   /* Make sure we don't create a circular list.  xref_tag can end
     388             :      up pushing the same artificial decl more than once.  We
     389             :      should have already detected that in update_binding.  (This isn't a
     390             :      complete verification of non-circularity.)  */
     391   847771133 :   gcc_assert (b->names != decl);
     392             : 
     393             :   /* We build up the list in reverse order, and reverse it later if
     394             :      necessary.  */
     395   847771133 :   TREE_CHAIN (decl) = b->names;
     396   847771133 :   b->names = decl;
     397             : 
     398             :   /* If appropriate, add decl to separate list of statics.  We include
     399             :      extern variables because they might turn out to be static later.
     400             :      It's OK for this list to contain a few false positives.  */
     401   847771133 :   if (b->kind == sk_namespace
     402   847771133 :       && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     403   293424855 :           || (TREE_CODE (decl) == FUNCTION_DECL
     404   251682536 :               && (!TREE_PUBLIC (decl)
     405   251300141 :                   || decl_internal_context_p (decl)
     406   251299036 :                   || DECL_DECLARED_INLINE_P (decl)))))
     407     9351168 :     vec_safe_push (static_decls, decl);
     408   847771133 : }
     409             : 
     410             : /* Find the binding for NAME in the local binding level B.  */
     411             : 
     412             : static cxx_binding *
     413   534944231 : find_local_binding (cp_binding_level *b, tree name)
     414             : {
     415   534944231 :   if (cxx_binding *binding = IDENTIFIER_BINDING (name))
     416           0 :     for (;; b = b->level_chain)
     417             :       {
     418     1036991 :         if (binding->scope == b)
     419             :           return binding;
     420             : 
     421             :         /* Cleanup contours are transparent to the language.  */
     422     1033790 :         if (b->kind != sk_cleanup)
     423             :           break;
     424             :       }
     425             :   return NULL;
     426             : }
     427             : 
     428             : class name_lookup
     429             : {
     430             : public:
     431             :   typedef std::pair<tree, tree> using_pair;
     432             :   typedef auto_vec<using_pair, 16> using_queue;
     433             : 
     434             : public:
     435             :   tree name;    /* The identifier being looked for.  */
     436             : 
     437             :   /* Usually we just add things to the VALUE binding, but we record
     438             :      (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
     439             :      using-decl resolution.  */
     440             :   tree value;   /* A (possibly ambiguous) set of things found.  */
     441             :   tree type;    /* A type that has been found.  */
     442             : 
     443             :   LOOK_want want;  /* What kind of entity we want.  */
     444             : 
     445             :   bool deduping; /* Full deduping is needed because using declarations
     446             :                     are in play.  */
     447             :   vec<tree, va_heap, vl_embed> *scopes;
     448             :   name_lookup *previous; /* Previously active lookup.  */
     449             : 
     450             : protected:
     451             :   /* Marked scope stack for outermost name lookup.  */
     452             :   static vec<tree, va_heap, vl_embed> *shared_scopes;
     453             :   /* Currently active lookup.  */
     454             :   static name_lookup *active;
     455             : 
     456             : public:
     457   924269050 :   name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
     458   924269050 :   : name (n), value (NULL_TREE), type (NULL_TREE),
     459   924269050 :     want (w),
     460   924269050 :     deduping (false), scopes (NULL), previous (NULL)
     461             :   {
     462   924269050 :     preserve_state ();
     463             :   }
     464   924269050 :   ~name_lookup ()
     465             :   {
     466   924269050 :     gcc_checking_assert (!deduping);
     467   924269050 :     restore_state ();
     468   924269050 :   }
     469             : 
     470             : private: /* Uncopyable, unmovable, unassignable. I am a rock. */
     471             :   name_lookup (const name_lookup &);
     472             :   name_lookup &operator= (const name_lookup &);
     473             : 
     474             :  public:
     475             :   /* Turn on or off deduping mode.  */
     476   945281772 :   void dedup (bool state)
     477             :   {
     478   945281772 :     if (deduping != state)
     479             :       {
     480    58789278 :         deduping = state;
     481    58789278 :         lookup_mark (value, state);
     482             :       }
     483   945281772 :   }
     484             : 
     485             : protected:
     486  9985833084 :   static bool seen_p (tree scope)
     487             :   {
     488  9985833084 :     return LOOKUP_SEEN_P (scope);
     489             :   }
     490     7830741 :   static bool found_p (tree scope)
     491             :   {
     492     7830741 :     return LOOKUP_FOUND_P (scope);
     493             :   }
     494             :   
     495             :   void mark_seen (tree scope); /* Mark and add to scope vector. */
     496   181703588 :   static void mark_found (tree scope)
     497             :   {
     498   181703588 :     gcc_checking_assert (seen_p (scope));
     499   181703588 :     LOOKUP_FOUND_P (scope) = true;
     500   181703588 :   }
     501  4856061767 :   bool see_and_mark (tree scope)
     502             :   {
     503  4856061767 :     bool ret = seen_p (scope);
     504  4856061767 :     if (!ret)
     505   864676050 :       mark_seen (scope);
     506  4775734995 :     return ret;
     507             :   }
     508             :   bool find_and_mark (tree scope);
     509             : 
     510             : private:
     511             :   void preserve_state ();
     512             :   void restore_state ();
     513             : 
     514             : private:
     515             :   static tree ambiguous (tree thing, tree current);
     516             :   void add_overload (tree fns);
     517             :   void add_value (tree new_val);
     518             :   void add_type (tree new_type);
     519             :   bool process_binding (tree val_bind, tree type_bind);
     520             :   unsigned process_module_binding (tree val_bind, tree type_bind, unsigned);
     521             :   /* Look in only namespace.  */
     522             :   bool search_namespace_only (tree scope);
     523             :   /* Look in namespace and its (recursive) inlines. Ignore using
     524             :      directives.  Return true if something found (inc dups). */
     525             :   bool search_namespace (tree scope);
     526             :   /* Look in the using directives of namespace + inlines using
     527             :      qualified lookup rules.  */
     528             :   bool search_usings (tree scope);
     529             : 
     530             : private:
     531             :   void queue_namespace (using_queue& queue, int depth, tree scope);
     532             :   void queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings);
     533             : 
     534             : private:
     535             :   void add_fns (tree);
     536             : 
     537             :  private:
     538             :   void adl_expr (tree);
     539             :   void adl_type (tree);
     540             :   void adl_template_arg (tree);
     541             :   void adl_class (tree);
     542             :   void adl_enum (tree);
     543             :   void adl_bases (tree);
     544             :   void adl_class_only (tree);
     545             :   void adl_namespace (tree);
     546             :   void adl_class_fns (tree);
     547             :   void adl_namespace_fns (tree, bitmap);
     548             : 
     549             : public:
     550             :   /* Search namespace + inlines + maybe usings as qualified lookup.  */
     551             :   bool search_qualified (tree scope, bool usings = true);
     552             : 
     553             :   /* Search namespace + inlines + usings as unqualified lookup.  */
     554             :   bool search_unqualified (tree scope, cp_binding_level *);
     555             : 
     556             :   /* ADL lookup of ARGS.  */
     557             :   tree search_adl (tree fns, vec<tree, va_gc> *args);
     558             : };
     559             : 
     560             : /* Scope stack shared by all outermost lookups.  This avoids us
     561             :    allocating and freeing on every single lookup.  */
     562             : vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
     563             : 
     564             : /* Currently active lookup.  */
     565             : name_lookup *name_lookup::active;
     566             : 
     567             : /* Name lookup is recursive, becase ADL can cause template
     568             :    instatiation.  This is of course a rare event, so we optimize for
     569             :    it not happening.  When we discover an active name-lookup, which
     570             :    must be an ADL lookup,  we need to unmark the marked scopes and also
     571             :    unmark the lookup we might have been accumulating.  */
     572             : 
     573             : void
     574   924269050 : name_lookup::preserve_state ()
     575             : {
     576   924269050 :   previous = active;
     577   924269050 :   if (previous)
     578             :     {
     579        8038 :       unsigned length = vec_safe_length (previous->scopes);
     580        8038 :       vec_safe_reserve (previous->scopes, length * 2);
     581       75325 :       for (unsigned ix = length; ix--;)
     582             :         {
     583       67287 :           tree decl = (*previous->scopes)[ix];
     584             : 
     585       67287 :           gcc_checking_assert (LOOKUP_SEEN_P (decl));
     586       67287 :           LOOKUP_SEEN_P (decl) = false;
     587             : 
     588             :           /* Preserve the FOUND_P state on the interrupted lookup's
     589             :              stack.  */
     590       67287 :           if (LOOKUP_FOUND_P (decl))
     591             :             {
     592        8063 :               LOOKUP_FOUND_P (decl) = false;
     593        8063 :               previous->scopes->quick_push (decl);
     594             :             }
     595             :         }
     596             : 
     597             :       /* Unmark the outer partial lookup.  */
     598        8038 :       if (previous->deduping)
     599           0 :         lookup_mark (previous->value, false);
     600             :     }
     601             :   else
     602   924261012 :     scopes = shared_scopes;
     603   924269050 :   active = this;
     604   924269050 : }
     605             : 
     606             : /* Restore the marking state of a lookup we interrupted.  */
     607             : 
     608             : void
     609   924269050 : name_lookup::restore_state ()
     610             : {
     611   924269050 :   gcc_checking_assert (!deduping);
     612             : 
     613             :   /* Unmark and empty this lookup's scope stack.  */
     614  6624273095 :   for (unsigned ix = vec_safe_length (scopes); ix--;)
     615             :     {
     616  4775734995 :       tree decl = scopes->pop ();
     617  4775734995 :       gcc_checking_assert (LOOKUP_SEEN_P (decl));
     618  4775734995 :       LOOKUP_SEEN_P (decl) = false;
     619  4775734995 :       LOOKUP_FOUND_P (decl) = false;
     620             :     }
     621             : 
     622   924269050 :   active = previous;
     623   924269050 :   if (previous)
     624             :     {
     625        8038 :       free (scopes);
     626             : 
     627        8038 :       unsigned length = vec_safe_length (previous->scopes);
     628       75325 :       for (unsigned ix = 0; ix != length; ix++)
     629             :         {
     630       74707 :           tree decl = (*previous->scopes)[ix];
     631       74707 :           if (LOOKUP_SEEN_P (decl))
     632             :             {
     633             :               /* The remainder of the scope stack must be recording
     634             :                  FOUND_P decls, which we want to pop off.  */
     635        8063 :               do
     636             :                 {
     637        8063 :                   tree decl = previous->scopes->pop ();
     638        8063 :                   gcc_checking_assert (LOOKUP_SEEN_P (decl)
     639             :                                        && !LOOKUP_FOUND_P (decl));
     640        8063 :                   LOOKUP_FOUND_P (decl) = true;
     641             :                 }
     642        8063 :               while (++ix != length);
     643             :               break;
     644             :             }
     645             : 
     646       67287 :           gcc_checking_assert (!LOOKUP_FOUND_P (decl));
     647       67287 :           LOOKUP_SEEN_P (decl) = true;
     648             :         }
     649             : 
     650             :       /* Remark the outer partial lookup.  */
     651        8038 :       if (previous->deduping)
     652           0 :         lookup_mark (previous->value, true);
     653             :     }
     654             :   else
     655   924261012 :     shared_scopes = scopes;
     656   924269050 : }
     657             : 
     658             : void
     659  4775734995 : name_lookup::mark_seen (tree scope)
     660             : {
     661  4775734995 :   gcc_checking_assert (!seen_p (scope));
     662  4775734995 :   LOOKUP_SEEN_P (scope) = true;
     663  4775734995 :   vec_safe_push (scopes, scope);
     664  4775734995 : }
     665             : 
     666             : bool
     667           0 : name_lookup::find_and_mark (tree scope)
     668             : {
     669           0 :   bool result = LOOKUP_FOUND_P (scope);
     670           0 :   if (!result)
     671             :     {
     672           0 :       LOOKUP_FOUND_P (scope) = true;
     673           0 :       if (!LOOKUP_SEEN_P (scope))
     674           0 :         vec_safe_push (scopes, scope);
     675             :     }
     676             : 
     677           0 :   return result;
     678             : }
     679             : 
     680             : /* THING and CURRENT are ambiguous, concatenate them.  */
     681             : 
     682             : tree
     683         248 : name_lookup::ambiguous (tree thing, tree current)
     684             : {
     685         248 :   if (TREE_CODE (current) != TREE_LIST)
     686             :     {
     687         210 :       current = build_tree_list (NULL_TREE, current);
     688         210 :       TREE_TYPE (current) = error_mark_node;
     689             :     }
     690         248 :   current = tree_cons (NULL_TREE, thing, current);
     691         248 :   TREE_TYPE (current) = error_mark_node;
     692             : 
     693         248 :   return current;
     694             : }
     695             : 
     696             : /* FNS is a new overload set to add to the exising set.  */
     697             : 
     698             : void
     699   301435782 : name_lookup::add_overload (tree fns)
     700             : {
     701   301435782 :   if (!deduping && TREE_CODE (fns) == OVERLOAD)
     702             :     {
     703   215555130 :       tree probe = fns;
     704   215555130 :       if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     705   215511204 :         probe = ovl_skip_hidden (probe);
     706   215555130 :       if (probe && TREE_CODE (probe) == OVERLOAD
     707   431110260 :           && OVL_DEDUP_P (probe))
     708             :         /* We're about to add something found by multiple paths, so need to
     709             :            engage deduping mode.  */
     710    21519798 :         dedup (true);
     711             :     }
     712             : 
     713   301435782 :   value = lookup_maybe_add (fns, value, deduping);
     714   301435782 : }
     715             : 
     716             : /* Add a NEW_VAL, a found value binding into the current value binding.  */
     717             : 
     718             : void
     719   739473417 : name_lookup::add_value (tree new_val)
     720             : {
     721   739473417 :   if (OVL_P (new_val) && (!value || OVL_P (value)))
     722   293387300 :     add_overload (new_val);
     723   446086117 :   else if (!value)
     724   445879558 :     value = new_val;
     725      206559 :   else if (value == new_val)
     726             :     ;
     727        9960 :   else if ((TREE_CODE (value) == TYPE_DECL
     728        9808 :             && TREE_CODE (new_val) == TYPE_DECL
     729       19768 :             && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
     730             :     /* Typedefs to the same type. */;
     731         264 :   else if (TREE_CODE (value) == NAMESPACE_DECL
     732          57 :            && TREE_CODE (new_val) == NAMESPACE_DECL
     733         378 :            && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
     734             :     /* Namespace (possibly aliased) to the same namespace.  Locate
     735             :        the namespace*/
     736          40 :     value = ORIGINAL_NAMESPACE (value);
     737             :   else
     738             :     {
     739             :       /* Disengage deduping mode.  */
     740         244 :       dedup (false);
     741         244 :       value = ambiguous (new_val, value);
     742             :     }
     743   739473417 : }
     744             : 
     745             : /* Add a NEW_TYPE, a found type binding into the current type binding.  */
     746             : 
     747             : void
     748     2942594 : name_lookup::add_type (tree new_type)
     749             : {
     750     2942594 :   if (!type)
     751     2942590 :     type = new_type;
     752           4 :   else if (TREE_CODE (type) == TREE_LIST
     753           4 :            || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
     754           4 :     type = ambiguous (new_type, type);
     755     2942594 : }
     756             : 
     757             : /* Process a found binding containing NEW_VAL and NEW_TYPE.  Returns
     758             :    true if we actually found something noteworthy.  Hiddenness has
     759             :    already been handled in the caller.  */
     760             : 
     761             : bool
     762   746156253 : name_lookup::process_binding (tree new_val, tree new_type)
     763             : {
     764             :   /* Did we really see a type? */
     765   746156253 :   if (new_type
     766   746156253 :       && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
     767             :     new_type = NULL_TREE;
     768             : 
     769             :   /* Do we really see a value? */
     770   746156253 :   if (new_val)
     771   739543059 :     switch (TREE_CODE (new_val))
     772             :       {
     773   130964170 :       case TEMPLATE_DECL:
     774             :         /* If we expect types or namespaces, and not templates,
     775             :            or this is not a template class.  */
     776   130964170 :         if (bool (want & LOOK_want::TYPE_NAMESPACE)
     777   130964170 :             && !DECL_TYPE_TEMPLATE_P (new_val))
     778             :           new_val = NULL_TREE;
     779             :         break;
     780   158654693 :       case TYPE_DECL:
     781   158654693 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
     782   158654693 :             || (new_type && bool (want & LOOK_want::TYPE)))
     783             :           new_val = NULL_TREE;
     784             :         break;
     785   122793219 :       case NAMESPACE_DECL:
     786   122793219 :         if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
     787             :           new_val = NULL_TREE;
     788             :         break;
     789   327130977 :       default:
     790   327130977 :         if (bool (want & LOOK_want::TYPE_NAMESPACE))
     791             :           new_val = NULL_TREE;
     792             :       }
     793             : 
     794             :   if (!new_val)
     795             :     {
     796     6714682 :       new_val = new_type;
     797     6714682 :       new_type = NULL_TREE;
     798             :     }
     799             : 
     800             :   /* Merge into the lookup  */
     801     6714682 :   if (new_val)
     802   739473417 :     add_value (new_val);
     803   746156253 :   if (new_type)
     804     2942594 :     add_type (new_type);
     805             : 
     806   746156253 :   return new_val != NULL_TREE;
     807             : }
     808             : 
     809             : /* If we're importing a module containing this binding, add it to the
     810             :    lookup set.  The trickiness is with namespaces, we only want to
     811             :    find it once.  */
     812             : 
     813             : unsigned
     814       29145 : name_lookup::process_module_binding (tree new_val, tree new_type,
     815             :                                      unsigned marker)
     816             : {
     817             :   /* Optimize for (re-)finding a public namespace.  We only need to
     818             :      look once.  */
     819       29145 :   if (new_val && !new_type
     820             :       && TREE_CODE (new_val) == NAMESPACE_DECL
     821       28364 :       && TREE_PUBLIC (new_val)
     822       37509 :       && !DECL_NAMESPACE_ALIAS (new_val))
     823             :     {
     824        8364 :       if (marker & 2)
     825             :         return marker;
     826        4396 :       marker |= 2;
     827             :     }
     828             : 
     829       25177 :   if (new_type || new_val)
     830       24449 :     marker |= process_binding (new_val, new_type);
     831             : 
     832             :   return marker;
     833             : }
     834             : 
     835             : /* Look in exactly namespace SCOPE.  */
     836             : 
     837             : bool
     838  4718865170 : name_lookup::search_namespace_only (tree scope)
     839             : {
     840  4718865170 :   bool found = false;
     841  4718865170 :   if (tree *binding = find_namespace_slot (scope, name))
     842             :     {
     843   746150586 :       tree val = *binding;
     844   746150586 :       if (TREE_CODE (val) == BINDING_VECTOR)
     845             :         {
     846             :           /* I presume the binding list is going to be sparser than
     847             :              the import bitmap.  Hence iterate over the former
     848             :              checking for bits set in the bitmap.  */
     849       18782 :           bitmap imports = get_import_bitmap ();
     850       18782 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
     851       18782 :           int marker = 0;
     852       18782 :           int dup_detect = 0;
     853             : 
     854       18782 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
     855             :             {
     856       11178 :               if (!deduping)
     857             :                 {
     858       10842 :                   if (named_module_purview_p ())
     859             :                     {
     860         237 :                       dup_detect |= 2;
     861             : 
     862         237 :                       if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
     863             :                         dup_detect |= 1;
     864             :                     }
     865             :                   else
     866             :                     dup_detect |= 1;
     867             :                 }
     868       11178 :               tree type = NULL_TREE;
     869       11178 :               tree value = bind;
     870             : 
     871       11178 :               if (STAT_HACK_P (bind))
     872             :                 {
     873        6754 :                   type = STAT_TYPE (bind);
     874        6754 :                   value = STAT_DECL (bind);
     875             : 
     876        6754 :                   if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     877             :                     {
     878        6738 :                       if (STAT_TYPE_HIDDEN_P (bind))
     879           0 :                         type = NULL_TREE;
     880        6738 :                       if (STAT_DECL_HIDDEN_P (bind))
     881             :                         value = NULL_TREE;
     882             :                       else
     883        6738 :                         value = ovl_skip_hidden (value);
     884             :                     }
     885             :                 }
     886        4424 :               else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     887        4424 :                 value = ovl_skip_hidden (value);
     888             : 
     889       11178 :               marker = process_module_binding (value, type, marker);
     890             :             }
     891             : 
     892             :           /* Scan the imported bindings.  */
     893       18782 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
     894       18782 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
     895             :             {
     896       18782 :               ix--;
     897       18782 :               cluster++;
     898             :             }
     899             : 
     900             :           /* Do this in forward order, so we load modules in an order
     901             :              the user expects.  */
     902       36671 :           for (; ix--; cluster++)
     903       53667 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
     904             :               {
     905             :                 /* Are we importing this module?  */
     906       35778 :                 if (unsigned base = cluster->indices[jx].base)
     907       18329 :                   if (unsigned span = cluster->indices[jx].span)
     908       18329 :                     do
     909       18329 :                       if (bitmap_bit_p (imports, base))
     910       17967 :                         goto found;
     911         362 :                     while (++base, --span);
     912       17811 :                 continue;
     913             : 
     914       17967 :               found:;
     915             :                 /* Is it loaded?  */
     916       17967 :                 if (cluster->slots[jx].is_lazy ())
     917             :                   {
     918        1509 :                     gcc_assert (cluster->indices[jx].span == 1);
     919        1509 :                     lazy_load_binding (cluster->indices[jx].base,
     920             :                                        scope, name, &cluster->slots[jx]);
     921             :                   }
     922       17967 :                 tree bind = cluster->slots[jx];
     923       17967 :                 if (!bind)
     924             :                   /* Load errors could mean there's nothing here.  */
     925           0 :                   continue;
     926             : 
     927             :                 /* Extract what we can see from here.  If there's no
     928             :                    stat_hack, then everything was exported.  */
     929       17967 :                 tree type = NULL_TREE;
     930             : 
     931             : 
     932             :                 /* If STAT_HACK_P is false, everything is visible, and
     933             :                    there's no duplication possibilities.  */
     934       17967 :                 if (STAT_HACK_P (bind))
     935             :                   {
     936        9068 :                     if (!deduping)
     937             :                       {
     938             :                         /* Do we need to engage deduplication?  */
     939        8723 :                         int dup = 0;
     940        8723 :                         if (MODULE_BINDING_GLOBAL_P (bind))
     941             :                           dup = 1;
     942        1579 :                         else if (MODULE_BINDING_PARTITION_P (bind))
     943        1135 :                           dup = 2;
     944        8723 :                         if (unsigned hit = dup_detect & dup)
     945             :                           {
     946        5481 :                             if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
     947        5917 :                                 || (hit & 2
     948         212 :                                     && BINDING_VECTOR_PARTITION_DUPS_P (val)))
     949        5379 :                               dedup (true);
     950             :                           }
     951        8723 :                         dup_detect |= dup;
     952             :                       }
     953             : 
     954        9068 :                     if (STAT_TYPE_VISIBLE_P (bind))
     955        1166 :                       type = STAT_TYPE (bind);
     956        9068 :                     bind = STAT_VISIBLE (bind);
     957             :                   }
     958             : 
     959             :                 /* And process it.  */
     960       17967 :                 marker = process_module_binding (bind, type, marker);
     961       17811 :               }
     962       18782 :           found |= marker & 1;
     963             :         }
     964             :       else
     965             :         {
     966             :           /* Only a current module binding, visible from the current module.  */
     967   746131804 :           tree bind = *binding;
     968   746131804 :           tree value = bind, type = NULL_TREE;
     969             : 
     970   746131804 :           if (STAT_HACK_P (bind))
     971             :             {
     972     2974937 :               type = STAT_TYPE (bind);
     973     2974937 :               value = STAT_DECL (bind);
     974             : 
     975     2974937 :               if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     976             :                 {
     977     2974867 :                   if (STAT_TYPE_HIDDEN_P (bind))
     978           4 :                     type = NULL_TREE;
     979     2974867 :                   if (STAT_DECL_HIDDEN_P (bind))
     980             :                     value = NULL_TREE;
     981             :                   else
     982     2974391 :                     value = ovl_skip_hidden (value);
     983             :                 }
     984             :             }
     985   743156867 :           else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
     986   742702055 :             value = ovl_skip_hidden (value);
     987             : 
     988   746131804 :           found |= process_binding (value, type);
     989             :         }
     990             :     }
     991             : 
     992  4718865170 :   return found;
     993             : }
     994             : 
     995             : /* Conditionally look in namespace SCOPE and inline children.  */
     996             : 
     997             : bool
     998   808346803 : name_lookup::search_namespace (tree scope)
     999             : {
    1000   808346803 :   if (see_and_mark (scope))
    1001             :     /* We've visited this scope before.  Return what we found then.  */
    1002           0 :     return found_p (scope);
    1003             : 
    1004             :   /* Look in exactly namespace. */
    1005   808346803 :   bool found = search_namespace_only (scope);
    1006             : 
    1007             :   /* Don't look into inline children, if we're looking for an
    1008             :      anonymous name -- it must be in the current scope, if anywhere.  */
    1009   808346803 :   if (name)
    1010             :     /* Recursively look in its inline children.  */
    1011   808345121 :     if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1012   879263996 :       for (unsigned ix = inlinees->length (); ix--;)
    1013   636014069 :         found |= search_namespace ((*inlinees)[ix]);
    1014             : 
    1015   808346803 :   if (found)
    1016   175165519 :     mark_found (scope);
    1017             : 
    1018             :   return found;
    1019             : }
    1020             : 
    1021             : /* Recursively follow using directives of SCOPE & its inline children.
    1022             :    Such following is essentially a flood-fill algorithm.  */
    1023             : 
    1024             : bool
    1025      205781 : name_lookup::search_usings (tree scope)
    1026             : {
    1027             :   /* We do not check seen_p here, as that was already set during the
    1028             :      namespace_only walk.  */
    1029      205781 :   if (found_p (scope))
    1030             :     return true;
    1031             : 
    1032      205781 :   bool found = false;
    1033      205781 :   if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
    1034       16511 :     for (unsigned ix = usings->length (); ix--;)
    1035        8484 :       found |= search_qualified ((*usings)[ix], true);
    1036             : 
    1037             :   /* Look in its inline children.  */
    1038      205781 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1039       49218 :     for (unsigned ix = inlinees->length (); ix--;)
    1040       25245 :       found |= search_usings ((*inlinees)[ix]);
    1041             : 
    1042      205781 :   if (found)
    1043        1656 :     mark_found (scope);
    1044             : 
    1045             :   return found;
    1046             : }
    1047             : 
    1048             : /* Qualified namespace lookup in SCOPE.
    1049             :    1) Look in SCOPE (+inlines).  If found, we're done.
    1050             :    2) Otherwise, if USINGS is true,
    1051             :       recurse for every using directive of SCOPE (+inlines).
    1052             : 
    1053             :    Trickiness is (a) loops and (b) multiple paths to same namespace.
    1054             :    In both cases we want to not repeat any lookups, and know whether
    1055             :    to stop the caller's step #2.  Do this via the FOUND_P marker.  */
    1056             : 
    1057             : bool
    1058   172332734 : name_lookup::search_qualified (tree scope, bool usings)
    1059             : {
    1060   172332734 :   bool found = false;
    1061             : 
    1062   172332734 :   if (seen_p (scope))
    1063           0 :     found = found_p (scope);
    1064             :   else 
    1065             :     {
    1066   172332734 :       found = search_namespace (scope);
    1067   172332734 :       if (!found && usings)
    1068      180536 :         found = search_usings (scope);
    1069             :     }
    1070             : 
    1071   172332734 :   dedup (false);
    1072             : 
    1073   172332734 :   return found;
    1074             : }
    1075             : 
    1076             : /* Add SCOPE to the unqualified search queue, recursively add its
    1077             :    inlines and those via using directives.  */
    1078             : 
    1079             : void
    1080  3951741068 : name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
    1081             : {
    1082  3951741068 :   if (see_and_mark (scope))
    1083  3951741068 :     return;
    1084             : 
    1085             :   /* Record it.  */
    1086             :   tree common = scope;
    1087  7947224811 :   while (SCOPE_DEPTH (common) > depth)
    1088  4036165866 :     common = CP_DECL_CONTEXT (common);
    1089  3911058945 :   queue.safe_push (using_pair (common, scope));
    1090             : 
    1091             :   /* Queue its inline children.  */
    1092  3911058945 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1093  3893520142 :     for (unsigned ix = inlinees->length (); ix--;)
    1094  2831815611 :       queue_namespace (queue, depth, (*inlinees)[ix]);
    1095             : 
    1096             :   /* Queue its using targets.  */
    1097  3911058945 :   queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
    1098             : }
    1099             : 
    1100             : /* Add the namespaces in USINGS to the unqualified search queue.  */
    1101             : 
    1102             : void
    1103  6117197155 : name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
    1104             : {
    1105  6117197155 :   if (usings)
    1106    11729988 :     for (unsigned ix = usings->length (); ix--;)
    1107     6041129 :       queue_namespace (queue, depth, (*usings)[ix]);
    1108  6117197155 : }
    1109             : 
    1110             : /* Unqualified namespace lookup in SCOPE.
    1111             :    1) add scope+inlins to worklist.
    1112             :    2) recursively add target of every using directive
    1113             :    3) for each worklist item where SCOPE is common ancestor, search it
    1114             :    4) if nothing find, scope=parent, goto 1.  */
    1115             : 
    1116             : bool
    1117   733187800 : name_lookup::search_unqualified (tree scope, cp_binding_level *level)
    1118             : {
    1119   733187800 :   using_queue queue;
    1120   733187800 :   bool found = false;
    1121             : 
    1122             :   /* Queue local using-directives.  */
    1123  2939326010 :   for (; level->kind != sk_namespace; level = level->level_chain)
    1124  2206138210 :     queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
    1125             : 
    1126  2213520564 :   for (; !found; scope = CP_DECL_CONTEXT (scope))
    1127             :     {
    1128  1113884328 :       gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
    1129  1113884328 :       int depth = SCOPE_DEPTH (scope);
    1130             : 
    1131             :       /* Queue namespaces reachable from SCOPE. */
    1132  1113884328 :       queue_namespace (queue, depth, scope);
    1133             : 
    1134             :       /* Search every queued namespace where SCOPE is the common
    1135             :          ancestor.  Adjust the others.  */
    1136  1113884328 :       unsigned ix = 0;
    1137  1115084287 :       do
    1138             :         {
    1139  1115084287 :           using_pair &pair = queue[ix];
    1140  5028178814 :           while (pair.first == scope)
    1141             :             {
    1142  3910518367 :               found |= search_namespace_only (pair.second);
    1143  3910518367 :               pair = queue.pop ();
    1144  3910518367 :               if (ix == queue.length ())
    1145  1112508127 :                 goto done;
    1146             :             }
    1147             :           /* The depth is the same as SCOPE, find the parent scope.  */
    1148     2576160 :           if (SCOPE_DEPTH (pair.first) == depth)
    1149     2571391 :             pair.first = CP_DECL_CONTEXT (pair.first);
    1150     2576160 :           ix++;
    1151             :         }
    1152     5152320 :       while (ix < queue.length ());
    1153     1376201 :     done:;
    1154  1113884328 :       if (scope == global_namespace)
    1155             :         break;
    1156             : 
    1157             :       /* If looking for hidden friends, we only look in the innermost
    1158             :          namespace scope.  [namespace.memdef]/3 If a friend
    1159             :          declaration in a non-local class first declares a class,
    1160             :          function, class template or function template the friend is a
    1161             :          member of the innermost enclosing namespace.  See also
    1162             :          [basic.lookup.unqual]/7 */
    1163   740559757 :       if (bool (want & LOOK_want::HIDDEN_FRIEND))
    1164             :         break;
    1165             :     }
    1166             : 
    1167   733187800 :   dedup (false);
    1168             : 
    1169   733187800 :   return found;
    1170   733187800 : }
    1171             : 
    1172             : /* FNS is a value binding.  If it is a (set of overloaded) functions,
    1173             :    add them into the current value.  */
    1174             : 
    1175             : void
    1176     8686956 : name_lookup::add_fns (tree fns)
    1177             : {
    1178     8686956 :   if (!fns)
    1179             :     return;
    1180     8050978 :   else if (TREE_CODE (fns) == OVERLOAD)
    1181             :     {
    1182     5926321 :       if (TREE_TYPE (fns) != unknown_type_node)
    1183       75080 :         fns = OVL_FUNCTION (fns);
    1184             :     }
    1185     2124657 :   else if (!DECL_DECLARES_FUNCTION_P (fns))
    1186             :     return;
    1187             : 
    1188     8048482 :   add_overload (fns);
    1189             : }
    1190             : 
    1191             : /* Add the overloaded fns of SCOPE.  */
    1192             : 
    1193             : void
    1194    41433896 : name_lookup::adl_namespace_fns (tree scope, bitmap imports)
    1195             : {
    1196    41433896 :   if (tree *binding = find_namespace_slot (scope, name))
    1197             :     {
    1198     7492473 :       tree val = *binding;
    1199     7492473 :       if (TREE_CODE (val) != BINDING_VECTOR)
    1200     7489192 :         add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
    1201             :       else
    1202             :         {
    1203             :           /* I presume the binding list is going to be sparser than
    1204             :              the import bitmap.  Hence iterate over the former
    1205             :              checking for bits set in the bitmap.  */
    1206        3281 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
    1207        3281 :           int dup_detect = 0;
    1208             : 
    1209        3281 :           if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    1210             :             {
    1211             :               /* The current TU's bindings must be visible, we don't
    1212             :                  need to check the bitmaps.  */
    1213             : 
    1214        2499 :               if (!deduping)
    1215             :                 {
    1216         466 :                   if (named_module_purview_p ())
    1217             :                     {
    1218           6 :                       dup_detect |= 2;
    1219             : 
    1220           6 :                       if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
    1221             :                         dup_detect |= 1;
    1222             :                     }
    1223             :                   else
    1224             :                     dup_detect |= 1;
    1225             :                 }
    1226             : 
    1227        2499 :               add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
    1228             :             }
    1229             : 
    1230             :           /* Scan the imported bindings.  */
    1231        3281 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
    1232        3281 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    1233             :             {
    1234        3281 :               ix--;
    1235        3281 :               cluster++;
    1236             :             }
    1237             : 
    1238             :           /* Do this in forward order, so we load modules in an order
    1239             :              the user expects.  */
    1240        5575 :           for (; ix--; cluster++)
    1241        6882 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    1242             :               {
    1243             :                 /* Functions are never on merged slots.  */
    1244        4588 :                 if (!cluster->indices[jx].base
    1245        2303 :                     || cluster->indices[jx].span != 1)
    1246        2285 :                   continue;
    1247             : 
    1248             :                 /* Is this slot visible?  */
    1249        2303 :                 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
    1250           0 :                   continue;
    1251             : 
    1252             :                 /* Is it loaded.  */
    1253        2303 :                 if (cluster->slots[jx].is_lazy ())
    1254          12 :                   lazy_load_binding (cluster->indices[jx].base,
    1255             :                                      scope, name, &cluster->slots[jx]);
    1256             : 
    1257        2303 :                 tree bind = cluster->slots[jx];
    1258        2303 :                 if (!bind)
    1259             :                   /* Load errors could mean there's nothing here.  */
    1260           0 :                   continue;
    1261             : 
    1262        2303 :                 if (STAT_HACK_P (bind))
    1263             :                   {
    1264        2249 :                     if (!deduping)
    1265             :                       {
    1266             :                         /* Do we need to engage deduplication?  */
    1267         820 :                         int dup = 0;
    1268         820 :                         if (MODULE_BINDING_GLOBAL_P (bind))
    1269             :                           dup = 1;
    1270          21 :                         else if (MODULE_BINDING_PARTITION_P (bind))
    1271          12 :                           dup = 2;
    1272         820 :                         if (unsigned hit = dup_detect & dup)
    1273         444 :                           if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
    1274         450 :                               || (hit & 2
    1275           6 :                                   && BINDING_VECTOR_PARTITION_DUPS_P (val)))
    1276         444 :                             dedup (true);
    1277         820 :                         dup_detect |= dup;
    1278             :                       }
    1279             : 
    1280        2249 :                     bind = STAT_VISIBLE (bind);
    1281             :                   }
    1282             : 
    1283        2303 :                 add_fns (bind);
    1284             :               }
    1285             :         }
    1286             :     }
    1287    41433896 : }
    1288             : 
    1289             : /* Add the hidden friends of SCOPE.  */
    1290             : 
    1291             : void
    1292     9833983 : name_lookup::adl_class_fns (tree type)
    1293             : {
    1294             :   /* Add friends.  */
    1295     9833983 :   for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
    1296    24773999 :        list; list = TREE_CHAIN (list))
    1297    14940016 :     if (name == FRIEND_NAME (list))
    1298             :       {
    1299      970913 :         tree context = NULL_TREE; /* Lazily computed.  */
    1300     2182240 :         for (tree friends = FRIEND_DECLS (list); friends;
    1301     1211327 :              friends = TREE_CHAIN (friends))
    1302             :           {
    1303     1211327 :             tree fn = TREE_VALUE (friends);
    1304             : 
    1305             :             /* Only interested in global functions with potentially hidden
    1306             :                (i.e. unqualified) declarations.  */
    1307     1211327 :             if (!context)
    1308      970913 :               context = decl_namespace_context (type);
    1309     1211327 :             if (CP_DECL_CONTEXT (fn) != context)
    1310       15434 :               continue;
    1311             : 
    1312     1195893 :             dedup (true);
    1313             : 
    1314             :             /* Template specializations are never found by name lookup.
    1315             :                (Templates themselves can be found, but not template
    1316             :                specializations.)  */
    1317     1195893 :             if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
    1318        2931 :               continue;
    1319             : 
    1320     1192962 :             add_fns (fn);
    1321             :           }
    1322             :       }
    1323     9833983 : }
    1324             : 
    1325             : /* Find the containing non-inlined namespace, add it and all its
    1326             :    inlinees.  */
    1327             : 
    1328             : void
    1329    45869124 : name_lookup::adl_namespace (tree scope)
    1330             : {
    1331    77453676 :   if (see_and_mark (scope))
    1332             :     return;
    1333             : 
    1334             :   /* Look down into inline namespaces.  */
    1335    41433896 :   if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
    1336    43750671 :     for (unsigned ix = inlinees->length (); ix--;)
    1337    31584552 :       adl_namespace ((*inlinees)[ix]);
    1338             : 
    1339    41433896 :   if (DECL_NAMESPACE_INLINE_P (scope))
    1340             :     /* Mark parent.  */
    1341    31584552 :     adl_namespace (CP_DECL_CONTEXT (scope));
    1342             : }
    1343             : 
    1344             : /* Adds the class and its friends to the lookup structure.  */
    1345             : 
    1346             : void
    1347     9912954 : name_lookup::adl_class_only (tree type)
    1348             : {
    1349             :   /* Backend-built structures, such as __builtin_va_list, aren't
    1350             :      affected by all this.  */
    1351     9912954 :   if (!CLASS_TYPE_P (type))
    1352             :     return;
    1353             : 
    1354     9912954 :   type = TYPE_MAIN_VARIANT (type);
    1355             : 
    1356     9912954 :   if (see_and_mark (type))
    1357             :     return;
    1358             : 
    1359     9833983 :   tree context = decl_namespace_context (type);
    1360     9833983 :   adl_namespace (context);
    1361             : }
    1362             : 
    1363             : /* Adds the class and its bases to the lookup structure.
    1364             :    Returns true on error.  */
    1365             : 
    1366             : void
    1367     8751639 : name_lookup::adl_bases (tree type)
    1368             : {
    1369     8751639 :   adl_class_only (type);
    1370             : 
    1371             :   /* Process baseclasses.  */
    1372     8751639 :   if (tree binfo = TYPE_BINFO (type))
    1373             :     {
    1374             :       tree base_binfo;
    1375             :       int i;
    1376             : 
    1377    10951665 :       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    1378     2215226 :         adl_bases (BINFO_TYPE (base_binfo));
    1379             :     }
    1380     8751639 : }
    1381             : 
    1382             : /* Adds everything associated with a class argument type to the lookup
    1383             :    structure.
    1384             : 
    1385             :    If T is a class type (including unions), its associated classes are: the
    1386             :    class itself; the class of which it is a member, if any; and its direct
    1387             :    and indirect base classes. Its associated namespaces are the namespaces
    1388             :    of which its associated classes are members. Furthermore, if T is a
    1389             :    class template specialization, its associated namespaces and classes
    1390             :    also include: the namespaces and classes associated with the types of
    1391             :    the template arguments provided for template type parameters (excluding
    1392             :    template template parameters); the namespaces of which any template
    1393             :    template arguments are members; and the classes of which any member
    1394             :    templates used as template template arguments are members. [ Note:
    1395             :    non-type template arguments do not contribute to the set of associated
    1396             :    namespaces.  --end note] */
    1397             : 
    1398             : void
    1399     7697660 : name_lookup::adl_class (tree type)
    1400             : {
    1401             :   /* Backend build structures, such as __builtin_va_list, aren't
    1402             :      affected by all this.  */
    1403     7697660 :   if (!CLASS_TYPE_P (type))
    1404             :     return;
    1405             : 
    1406     7624960 :   type = TYPE_MAIN_VARIANT (type);
    1407             : 
    1408             :   /* We don't set found here because we have to have set seen first,
    1409             :      which is done in the adl_bases walk.  */
    1410     7624960 :   if (found_p (type))
    1411             :     return;
    1412             : 
    1413     6536413 :   complete_type (type);
    1414     6536413 :   adl_bases (type);
    1415     6536413 :   mark_found (type);
    1416             : 
    1417     6536413 :   if (TYPE_CLASS_SCOPE_P (type))
    1418      548866 :     adl_class_only (TYPE_CONTEXT (type));
    1419             : 
    1420             :   /* Process template arguments.  */
    1421     6536413 :   if (CLASSTYPE_TEMPLATE_INFO (type)
    1422     6536413 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
    1423             :     {
    1424     2517664 :       tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1425     6660748 :       for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
    1426     4143084 :         adl_template_arg (TREE_VEC_ELT (list, i));
    1427             :     }
    1428             : }
    1429             : 
    1430             : void
    1431     8607266 : name_lookup::adl_enum (tree type)
    1432             : {
    1433     8607266 :   type = TYPE_MAIN_VARIANT (type);
    1434     8607266 :   if (see_and_mark (type))
    1435             :     return;
    1436             : 
    1437     5061368 :   if (TYPE_CLASS_SCOPE_P (type))
    1438      612441 :     adl_class_only (TYPE_CONTEXT (type));
    1439             :   else
    1440     4448927 :     adl_namespace (decl_namespace_context (type));
    1441             : }
    1442             : 
    1443             : void
    1444    31775692 : name_lookup::adl_expr (tree expr)
    1445             : {
    1446    31775692 :   if (!expr)
    1447             :     return;
    1448             : 
    1449    31775692 :   gcc_assert (!TYPE_P (expr));
    1450             : 
    1451    31775692 :   if (TREE_TYPE (expr) != unknown_type_node)
    1452             :     {
    1453    31772845 :       adl_type (unlowered_expr_type (expr));
    1454    31772845 :       return;
    1455             :     }
    1456             : 
    1457        2847 :   if (TREE_CODE (expr) == ADDR_EXPR)
    1458         413 :     expr = TREE_OPERAND (expr, 0);
    1459        2847 :   if (TREE_CODE (expr) == COMPONENT_REF
    1460        2847 :       || TREE_CODE (expr) == OFFSET_REF)
    1461         382 :     expr = TREE_OPERAND (expr, 1);
    1462        2847 :   expr = MAYBE_BASELINK_FUNCTIONS (expr);
    1463             : 
    1464        2847 :   if (OVL_P (expr))
    1465        8030 :     for (lkp_iterator iter (expr); iter; ++iter)
    1466        2924 :       adl_type (TREE_TYPE (*iter));
    1467         294 :   else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
    1468             :     {
    1469             :       /* The working paper doesn't currently say how to handle
    1470             :          template-id arguments.  The sensible thing would seem to be
    1471             :          to handle the list of template candidates like a normal
    1472             :          overload set, and handle the template arguments like we do
    1473             :          for class template specializations.  */
    1474             : 
    1475             :       /* First the templates.  */
    1476         294 :       adl_expr (TREE_OPERAND (expr, 0));
    1477             : 
    1478             :       /* Now the arguments.  */
    1479         294 :       if (tree args = TREE_OPERAND (expr, 1))
    1480         594 :         for (int ix = TREE_VEC_LENGTH (args); ix--;)
    1481         300 :           adl_template_arg (TREE_VEC_ELT (args, ix));
    1482             :     }
    1483             : }
    1484             : 
    1485             : void
    1486    35945875 : name_lookup::adl_type (tree type)
    1487             : {
    1488    40495848 :   if (!type)
    1489             :     return;
    1490             : 
    1491    40495848 :   if (TYPE_PTRDATAMEM_P (type))
    1492             :     {
    1493             :       /* Pointer to member: associate class type and value type.  */
    1494         684 :       adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
    1495         684 :       adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
    1496         684 :       return;
    1497             :     }
    1498             : 
    1499    40495164 :   switch (TREE_CODE (type))
    1500             :     {
    1501     6754987 :     case RECORD_TYPE:
    1502     6754987 :       if (TYPE_PTRMEMFUNC_P (type))
    1503             :         {
    1504       34933 :           adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
    1505       34933 :           return;
    1506             :         }
    1507             :       /* FALLTHRU */
    1508     7697660 :     case UNION_TYPE:
    1509     7697660 :       adl_class (type);
    1510     7697660 :       return;
    1511             : 
    1512       88380 :     case METHOD_TYPE:
    1513             :       /* The basetype is referenced in the first arg type, so just
    1514             :          fall through.  */
    1515       88380 :     case FUNCTION_TYPE:
    1516             :       /* Associate the parameter types.  */
    1517      408986 :       for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
    1518      320606 :         adl_type (TREE_VALUE (args));
    1519             :       /* FALLTHROUGH */
    1520             : 
    1521     4514353 :     case POINTER_TYPE:
    1522     4514353 :     case REFERENCE_TYPE:
    1523     4514353 :     case ARRAY_TYPE:
    1524     4514353 :       adl_type (TREE_TYPE (type));
    1525     4514353 :       return;
    1526             : 
    1527     8607266 :     case ENUMERAL_TYPE:
    1528     8607266 :       adl_enum (type);
    1529     8607266 :       return;
    1530             : 
    1531         528 :     case LANG_TYPE:
    1532         528 :       gcc_assert (type == unknown_type_node
    1533             :                   || type == init_list_type_node);
    1534             :       return;
    1535             : 
    1536           3 :     case TYPE_PACK_EXPANSION:
    1537           3 :       adl_type (PACK_EXPANSION_PATTERN (type));
    1538           3 :       return;
    1539             : 
    1540             :     default:
    1541             :       break;
    1542             :     }
    1543             : }
    1544             : 
    1545             : /* Adds everything associated with a template argument to the lookup
    1546             :    structure.  */
    1547             : 
    1548             : void
    1549     4183465 : name_lookup::adl_template_arg (tree arg)
    1550             : {
    1551             :   /* [basic.lookup.koenig]
    1552             : 
    1553             :      If T is a template-id, its associated namespaces and classes are
    1554             :      ... the namespaces and classes associated with the types of the
    1555             :      template arguments provided for template type parameters
    1556             :      (excluding template template parameters); the namespaces in which
    1557             :      any template template arguments are defined; and the classes in
    1558             :      which any member templates used as template template arguments
    1559             :      are defined.  [Note: non-type template arguments do not
    1560             :      contribute to the set of associated namespaces.  ]  */
    1561             : 
    1562             :   /* Consider first template template arguments.  */
    1563     4183465 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    1564     4183465 :       || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
    1565             :     ;
    1566     4183465 :   else if (TREE_CODE (arg) == TEMPLATE_DECL)
    1567             :     {
    1568        1670 :       tree ctx = CP_DECL_CONTEXT (arg);
    1569             : 
    1570             :       /* It's not a member template.  */
    1571        1670 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
    1572        1662 :         adl_namespace (ctx);
    1573             :       /* Otherwise, it must be member template.  */
    1574             :       else
    1575           8 :         adl_class_only (ctx);
    1576             :     }
    1577             :   /* It's an argument pack; handle it recursively.  */
    1578     4181795 :   else if (ARGUMENT_PACK_P (arg))
    1579             :     {
    1580        8107 :       tree args = ARGUMENT_PACK_ARGS (arg);
    1581        8107 :       int i, len = TREE_VEC_LENGTH (args);
    1582       48188 :       for (i = 0; i < len; ++i) 
    1583       40081 :         adl_template_arg (TREE_VEC_ELT (args, i));
    1584             :     }
    1585             :   /* It's not a template template argument, but it is a type template
    1586             :      argument.  */
    1587     4173688 :   else if (TYPE_P (arg))
    1588     3848716 :     adl_type (arg);
    1589     4183465 : }
    1590             : 
    1591             : /* Perform ADL lookup.  FNS is the existing lookup result and ARGS are
    1592             :    the call arguments.  */
    1593             : 
    1594             : tree
    1595    17211286 : name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
    1596             : {
    1597    17211286 :   gcc_checking_assert (!vec_safe_length (scopes));
    1598             :   
    1599             :   /* Gather each associated entity onto the lookup's scope list.  */
    1600    17211286 :   unsigned ix;
    1601    17211286 :   tree arg;
    1602             : 
    1603    48986784 :   FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
    1604             :     /* OMP reduction operators put an ADL-significant type as the
    1605             :        first arg. */
    1606    31775498 :     if (TYPE_P (arg))
    1607         100 :       adl_type (arg);
    1608             :     else
    1609    31775398 :       adl_expr (arg);
    1610             : 
    1611    17211286 :   if (vec_safe_length (scopes))
    1612             :     {
    1613             :       /* Now do the lookups.  */
    1614     9338320 :       value = fns;
    1615     9338320 :       if (fns)
    1616     7701160 :         dedup (true);
    1617             : 
    1618             :       /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL.  */
    1619     9338320 :       bitmap inst_path = NULL;
    1620             :       /* VISIBLE is the regular import bitmap.  */
    1621     9338320 :       bitmap visible = visible_instantiation_path (&inst_path);
    1622             : 
    1623    65667567 :       for (unsigned ix = scopes->length (); ix--;)
    1624             :         {
    1625    56329247 :           tree scope = (*scopes)[ix];
    1626    56329247 :           if (TREE_CODE (scope) == NAMESPACE_DECL)
    1627    41433896 :             adl_namespace_fns (scope, visible);
    1628             :           else
    1629             :             {
    1630    14895351 :               if (RECORD_OR_UNION_TYPE_P (scope))
    1631     9833983 :                 adl_class_fns (scope);
    1632             : 
    1633             :               /* During 2nd phase ADL: Any exported declaration D in N
    1634             :                  declared within the purview of a named module M
    1635             :                  (10.2) is visible if there is an associated entity
    1636             :                  attached to M with the same innermost enclosing
    1637             :                  non-inline namespace as D.
    1638             :                  [basic.lookup.argdep]/4.4 */ 
    1639             : 
    1640    14895351 :               if (!inst_path)
    1641             :                 /* Not 2nd phase.  */
    1642    14864363 :                 continue;
    1643             : 
    1644       30988 :               tree ctx = CP_DECL_CONTEXT (TYPE_NAME (scope));
    1645       30988 :               if (TREE_CODE (ctx) != NAMESPACE_DECL)
    1646             :                 /* Not namespace-scope class.  */
    1647        2877 :                 continue;
    1648             : 
    1649       28111 :               tree origin = get_originating_module_decl (TYPE_NAME (scope));
    1650       28111 :               tree not_tmpl = STRIP_TEMPLATE (origin);
    1651       28111 :               if (!DECL_LANG_SPECIFIC (not_tmpl)
    1652       48082 :                   || !DECL_MODULE_IMPORT_P (not_tmpl))
    1653             :                 /* Not imported.  */
    1654       25899 :                 continue;
    1655             : 
    1656        2212 :               unsigned module = get_importing_module (origin);
    1657             : 
    1658        2212 :               if (!bitmap_bit_p (inst_path, module))
    1659             :                 /* Not on path of instantiation.  */
    1660           3 :                 continue;
    1661             : 
    1662        2209 :               if (bitmap_bit_p (visible, module))
    1663             :                 /* If the module was in the visible set, we'll look at
    1664             :                    its namespace partition anyway.  */
    1665        2209 :                 continue;
    1666             : 
    1667           0 :               if (tree *slot = find_namespace_slot (ctx, name, false))
    1668           0 :                 if (binding_slot *mslot = search_imported_binding_slot (slot, module))
    1669             :                   {
    1670           0 :                     if (mslot->is_lazy ())
    1671           0 :                       lazy_load_binding (module, ctx, name, mslot);
    1672             : 
    1673           0 :                     if (tree bind = *mslot)
    1674             :                       {
    1675             :                         /* We must turn on deduping, because some other class
    1676             :                            from this module might also be in this namespace.  */
    1677           0 :                         dedup (true);
    1678             : 
    1679             :                         /* Add the exported fns  */
    1680           0 :                         if (STAT_HACK_P (bind))
    1681           0 :                           add_fns (STAT_VISIBLE (bind));
    1682             :                       }
    1683             :                   }
    1684             :             }
    1685             :         }
    1686             : 
    1687     9338320 :       fns = value;
    1688     9338320 :       dedup (false);
    1689             :     }
    1690             : 
    1691    17211286 :   return fns;
    1692             : }
    1693             : 
    1694             : static bool qualified_namespace_lookup (tree, name_lookup *);
    1695             : static void consider_binding_level (tree name,
    1696             :                                     best_match <tree, const char *> &bm,
    1697             :                                     cp_binding_level *lvl,
    1698             :                                     bool look_within_fields,
    1699             :                                     enum lookup_name_fuzzy_kind kind);
    1700             : 
    1701             : /* ADL lookup of NAME.  FNS is the result of regular lookup, and we
    1702             :    don't add duplicates to it.  ARGS is the vector of call
    1703             :    arguments (which will not be empty).  */
    1704             : 
    1705             : tree
    1706    17211286 : lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
    1707             : {
    1708    17211286 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    1709    17211286 :   name_lookup lookup (name);
    1710    17211286 :   return lookup.search_adl (fns, args);
    1711    17211286 : }
    1712             : 
    1713             : /* FNS is an overload set of conversion functions.  Return the
    1714             :    overloads converting to TYPE.  */
    1715             : 
    1716             : static tree
    1717      196573 : extract_conversion_operator (tree fns, tree type)
    1718             : {
    1719      196573 :   tree convs = NULL_TREE;
    1720      196573 :   tree tpls = NULL_TREE;
    1721             : 
    1722      876851 :   for (ovl_iterator iter (fns); iter; ++iter)
    1723             :     {
    1724      340139 :       if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
    1725      270773 :         convs = lookup_add (*iter, convs);
    1726             : 
    1727      340139 :       if (TREE_CODE (*iter) == TEMPLATE_DECL)
    1728       22495 :         tpls = lookup_add (*iter, tpls);
    1729             :     }
    1730             : 
    1731      196573 :   if (!convs)
    1732       24480 :     convs = tpls;
    1733             : 
    1734      196573 :   return convs;
    1735             : }
    1736             : 
    1737             : /* Binary search of (ordered) MEMBER_VEC for NAME.  */
    1738             : 
    1739             : static tree
    1740  1848160683 : member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
    1741             : {
    1742 11348689082 :   for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
    1743             :     {
    1744  8078727231 :       unsigned mid = (lo + hi) / 2;
    1745  8078727231 :       tree binding = (*member_vec)[mid];
    1746 16157454462 :       tree binding_name = OVL_NAME (binding);
    1747             : 
    1748  8078727231 :       if (binding_name > name)
    1749             :         hi = mid;
    1750  3847492359 :       else if (binding_name < name)
    1751  3421132844 :         lo = mid + 1;
    1752             :       else
    1753   426359515 :         return binding;
    1754             :     }
    1755             : 
    1756             :   return NULL_TREE;
    1757             : }
    1758             : 
    1759             : /* Linear search of (unordered) MEMBER_VEC for NAME.  */
    1760             : 
    1761             : static tree
    1762   425416837 : member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
    1763             : {
    1764  4199810272 :   for (int ix = member_vec->length (); ix--;)
    1765  3807301727 :     if (tree binding = (*member_vec)[ix])
    1766  3807301727 :       if (OVL_NAME (binding) == name)
    1767    32908292 :         return binding;
    1768             : 
    1769             :   return NULL_TREE;
    1770             : }
    1771             : 
    1772             : /* Linear search of (partially ordered) fields of KLASS for NAME.  */
    1773             : 
    1774             : static tree
    1775  1534517071 : fields_linear_search (tree klass, tree name, bool want_type)
    1776             : {
    1777 23465607040 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    1778             :     {
    1779 21963084829 :       tree decl = fields;
    1780             : 
    1781 21963084829 :       if (TREE_CODE (decl) == FIELD_DECL
    1782 21963084829 :           && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1783             :         {
    1784    20868901 :           if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
    1785        8793 :             return temp;
    1786             :         }
    1787             : 
    1788 21963076036 :       if (DECL_NAME (decl) != name)
    1789 21902231005 :         continue;
    1790             :       
    1791    60845031 :       if (TREE_CODE (decl) == USING_DECL)
    1792             :         {
    1793      336143 :           decl = strip_using_decl (decl);
    1794      336143 :           if (is_overloaded_fn (decl))
    1795      133750 :             continue;
    1796             :         }
    1797             : 
    1798    60711281 :       if (DECL_DECLARES_FUNCTION_P (decl))
    1799             :         /* Functions are found separately.  */
    1800    28396258 :         continue;
    1801             : 
    1802    32315023 :       if (!want_type || DECL_DECLARES_TYPE_P (decl))
    1803    31986067 :         return decl;
    1804             :     }
    1805             : 
    1806             :   return NULL_TREE;
    1807             : }
    1808             : 
    1809             : /* Look for NAME member inside of anonymous aggregate ANON.  Although
    1810             :    such things should only contain FIELD_DECLs, we check that too
    1811             :    late, and would give very confusing errors if we weren't
    1812             :    permissive here.  */
    1813             : 
    1814             : tree
    1815    20868901 : search_anon_aggr (tree anon, tree name, bool want_type)
    1816             : {
    1817    20868901 :   gcc_assert (COMPLETE_TYPE_P (anon));
    1818    20868901 :   tree ret = get_class_binding_direct (anon, name, want_type);
    1819    20868901 :   return ret;
    1820             : }
    1821             : 
    1822             : /* Look for NAME as an immediate member of KLASS (including
    1823             :    anon-members or unscoped enum member).  TYPE_OR_FNS is zero for
    1824             :    regular search.  >0 to get a type binding (if there is one) and <0
    1825             :    if you want (just) the member function binding.
    1826             : 
    1827             :    Use this if you do not want lazy member creation.  */
    1828             : 
    1829             : tree
    1830  3415195804 : get_class_binding_direct (tree klass, tree name, bool want_type)
    1831             : {
    1832  3415195804 :   gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
    1833             : 
    1834             :   /* Conversion operators can only be found by the marker conversion
    1835             :      operator name.  */
    1836  3415195804 :   bool conv_op = IDENTIFIER_CONV_OP_P (name);
    1837  3415195804 :   tree lookup = conv_op ? conv_op_identifier : name;
    1838  3415195804 :   tree val = NULL_TREE;
    1839  3415195804 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    1840             : 
    1841  3415195804 :   if (COMPLETE_TYPE_P (klass) && member_vec)
    1842             :     {
    1843  1847903506 :       val = member_vec_binary_search (member_vec, lookup);
    1844  1847903506 :       if (!val)
    1845             :         ;
    1846   426102848 :       else if (STAT_HACK_P (val))
    1847         216 :         val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
    1848   426102632 :       else if (want_type && !DECL_DECLARES_TYPE_P (val))
    1849             :         val = NULL_TREE;
    1850             :     }
    1851             :   else
    1852             :     {
    1853  1567292298 :       if (member_vec && !want_type)
    1854   425416837 :         val = member_vec_linear_search (member_vec, lookup);
    1855             : 
    1856   425416837 :       if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
    1857             :         /* Dependent using declarations are a 'field', make sure we
    1858             :            return that even if we saw an overload already.  */
    1859  1534514543 :         if (tree field_val = fields_linear_search (klass, lookup, want_type))
    1860             :           {
    1861    31992332 :             if (!val)
    1862             :               val = field_val;
    1863           0 :             else if (TREE_CODE (field_val) == USING_DECL)
    1864           0 :               val = ovl_make (field_val, val);
    1865             :           }
    1866             :     }
    1867             : 
    1868             :   /* Extract the conversion operators asked for, unless the general
    1869             :      conversion operator was requested.   */
    1870  3415195804 :   if (val && conv_op)
    1871             :     {
    1872     6218929 :       gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
    1873     6218929 :       val = OVL_CHAIN (val);
    1874     6218929 :       if (tree type = TREE_TYPE (name))
    1875      196573 :         val = extract_conversion_operator (val, type);
    1876             :     }
    1877             : 
    1878  3415195804 :   return val;
    1879             : }
    1880             : 
    1881             : /* We're about to lookup NAME in KLASS.  Make sure any lazily declared
    1882             :    members are now declared.  */
    1883             : 
    1884             : static void
    1885  1940356386 : maybe_lazily_declare (tree klass, tree name)
    1886             : {
    1887             :   /* See big comment anout module_state::write_pendings regarding adding a check
    1888             :      bit.  */
    1889  1940356386 :   if (modules_p ())
    1890     9214362 :     lazy_load_pendings (TYPE_NAME (klass));
    1891             : 
    1892             :   /* Lazily declare functions, if we're going to search these.  */
    1893  1940356386 :   if (IDENTIFIER_CTOR_P (name))
    1894             :     {
    1895    30695391 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
    1896     1216891 :         lazily_declare_fn (sfk_constructor, klass);
    1897    30695391 :       if (CLASSTYPE_LAZY_COPY_CTOR (klass))
    1898     2355523 :         lazily_declare_fn (sfk_copy_constructor, klass);
    1899    30695391 :       if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
    1900     1795517 :         lazily_declare_fn (sfk_move_constructor, klass);
    1901             :     }
    1902  1909660995 :   else if (IDENTIFIER_DTOR_P (name))
    1903             :     {
    1904    32961370 :       if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
    1905     2725039 :         lazily_declare_fn (sfk_destructor, klass);
    1906             :     }
    1907  1876699625 :   else if (name == assign_op_identifier)
    1908             :     {
    1909     7028498 :       if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
    1910      951862 :         lazily_declare_fn (sfk_copy_assignment, klass);
    1911     7028498 :       if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
    1912      547613 :         lazily_declare_fn (sfk_move_assignment, klass);
    1913             :     }
    1914  1940356386 : }
    1915             : 
    1916             : /* Look for NAME's binding in exactly KLASS.  See
    1917             :    get_class_binding_direct for argument description.  Does lazy
    1918             :    special function creation as necessary.  */
    1919             : 
    1920             : tree
    1921  3148190169 : get_class_binding (tree klass, tree name, bool want_type /*=false*/)
    1922             : {
    1923  3148190169 :   klass = complete_type (klass);
    1924             : 
    1925  3148190169 :   if (COMPLETE_TYPE_P (klass))
    1926  1940356386 :     maybe_lazily_declare (klass, name);
    1927             : 
    1928  3148190169 :   return get_class_binding_direct (klass, name, want_type);
    1929             : }
    1930             : 
    1931             : /* Find the slot containing overloads called 'NAME'.  If there is no
    1932             :    such slot and the class is complete, create an empty one, at the
    1933             :    correct point in the sorted member vector.  Otherwise return NULL.
    1934             :    Deals with conv_op marker handling.  */
    1935             : 
    1936             : tree *
    1937   184509651 : find_member_slot (tree klass, tree name)
    1938             : {
    1939   184509651 :   bool complete_p = COMPLETE_TYPE_P (klass);
    1940             : 
    1941   184509651 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    1942   184509651 :   if (!member_vec)
    1943             :     {
    1944    12993864 :       vec_alloc (member_vec, 8);
    1945    12993864 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    1946    12993864 :       if (complete_p)
    1947             :         /* If the class is complete but had no member_vec, we need to
    1948             :            add the TYPE_FIELDS into it.  We're also most likely to be
    1949             :            adding ctors & dtors, so ask for 6 spare slots (the
    1950             :            abstract cdtors and their clones).  */
    1951     1316636 :         member_vec = set_class_bindings (klass, 6);
    1952             :     }
    1953             : 
    1954   184509651 :   if (IDENTIFIER_CONV_OP_P (name))
    1955     1025975 :     name = conv_op_identifier;
    1956             : 
    1957   184509651 :   unsigned ix, length = member_vec->length ();
    1958  2264259810 :   for (ix = 0; ix < length; ix++)
    1959             :     {
    1960  2170793413 :       tree *slot = &(*member_vec)[ix];
    1961  4341586826 :       tree fn_name = OVL_NAME (*slot);
    1962             : 
    1963  2170793413 :       if (fn_name == name)
    1964             :         {
    1965             :           /* If we found an existing slot, it must be a function set.
    1966             :              Even with insertion after completion, because those only
    1967             :              happen with artificial fns that have unspellable names.
    1968             :              This means we do not have to deal with the stat hack
    1969             :              either.  */
    1970    86997614 :           gcc_checking_assert (OVL_P (*slot));
    1971    86997614 :           if (name == conv_op_identifier)
    1972             :             {
    1973      263111 :               gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
    1974             :               /* Skip the conv-op marker. */
    1975      263111 :               slot = &OVL_CHAIN (*slot);
    1976             :             }
    1977    86997614 :           return slot;
    1978             :         }
    1979             : 
    1980  2083795799 :       if (complete_p && fn_name > name)
    1981             :         break;
    1982             :     }
    1983             : 
    1984             :   /* No slot found, add one if the class is complete.  */
    1985    97512037 :   if (complete_p)
    1986             :     {
    1987             :       /* Do exact allocation, as we don't expect to add many.  */
    1988    12840119 :       gcc_assert (name != conv_op_identifier);
    1989    12840119 :       vec_safe_reserve_exact (member_vec, 1);
    1990    12840119 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    1991    12840119 :       member_vec->quick_insert (ix, NULL_TREE);
    1992    12840119 :       return &(*member_vec)[ix];
    1993             :     }
    1994             : 
    1995             :   return NULL;
    1996             : }
    1997             : 
    1998             : /* KLASS is an incomplete class to which we're adding a method NAME.
    1999             :    Add a slot and deal with conv_op marker handling.  */
    2000             : 
    2001             : tree *
    2002    84671898 : add_member_slot (tree klass, tree name)
    2003             : {
    2004    84671898 :   gcc_assert (!COMPLETE_TYPE_P (klass));
    2005             : 
    2006    84671898 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2007    84671898 :   vec_safe_push (member_vec, NULL_TREE);
    2008    84671898 :   CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2009             : 
    2010    84671898 :   tree *slot = &member_vec->last ();
    2011    84671898 :   if (IDENTIFIER_CONV_OP_P (name))
    2012             :     {
    2013             :       /* Install the marker prefix.  */
    2014      762864 :       *slot = ovl_make (conv_op_marker, NULL_TREE);
    2015      762864 :       slot = &OVL_CHAIN (*slot);
    2016             :     }
    2017             : 
    2018    84671898 :   return slot;
    2019             : }
    2020             : 
    2021             : /* Comparison function to compare two MEMBER_VEC entries by name.
    2022             :    Because we can have duplicates during insertion of TYPE_FIELDS, we
    2023             :    do extra checking so deduping doesn't have to deal with so many
    2024             :    cases.  */
    2025             : 
    2026             : static int
    2027  3445573706 : member_name_cmp (const void *a_p, const void *b_p)
    2028             : {
    2029  3445573706 :   tree a = *(const tree *)a_p;
    2030  3445573706 :   tree b = *(const tree *)b_p;
    2031  3445573706 :   tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
    2032  3445573706 :   tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
    2033             : 
    2034  3445573706 :   gcc_checking_assert (name_a && name_b);
    2035  3445573706 :   if (name_a != name_b)
    2036  5064091004 :     return name_a < name_b ? -1 : +1;
    2037             : 
    2038     9733421 :   if (name_a == conv_op_identifier)
    2039             :     {
    2040             :       /* Strip the conv-op markers. */
    2041      789876 :       gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
    2042             :                            && OVL_FUNCTION (b) == conv_op_marker);
    2043      394938 :       a = OVL_CHAIN (a);
    2044      394938 :       b = OVL_CHAIN (b);
    2045             :     }
    2046             : 
    2047     9733421 :   if (TREE_CODE (a) == OVERLOAD)
    2048     3890929 :     a = OVL_FUNCTION (a);
    2049     9733421 :   if (TREE_CODE (b) == OVERLOAD)
    2050     3527473 :     b = OVL_FUNCTION (b);
    2051             : 
    2052             :   /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
    2053     9733421 :   if (TREE_CODE (a) != TREE_CODE (b))
    2054             :     {
    2055             :       /* If one of them is a TYPE_DECL, it loses.  */
    2056     9286552 :       if (TREE_CODE (a) == TYPE_DECL)
    2057             :         return +1;
    2058     9286116 :       else if (TREE_CODE (b) == TYPE_DECL)
    2059             :         return -1;
    2060             : 
    2061             :       /* If one of them is a USING_DECL, it loses.  */
    2062     9285668 :       if (TREE_CODE (a) == USING_DECL)
    2063             :         return +1;
    2064     4858462 :       else if (TREE_CODE (b) == USING_DECL)
    2065             :         return -1;
    2066             : 
    2067             :       /* There are no other cases with different kinds of decls, as
    2068             :          duplicate detection should have kicked in earlier.  However,
    2069             :          some erroneous cases get though. */
    2070           0 :       gcc_assert (errorcount);
    2071             :     }
    2072             :   
    2073             :   /* Using source location would be the best thing here, but we can
    2074             :      get identically-located decls in the following circumstances:
    2075             : 
    2076             :      1) duplicate artificial type-decls for the same type.
    2077             : 
    2078             :      2) pack expansions of using-decls.
    2079             : 
    2080             :      We should not be doing #1, but in either case it doesn't matter
    2081             :      how we order these.  Use UID as a proxy for source ordering, so
    2082             :      that identically-located decls still have a well-defined stable
    2083             :      ordering.  */
    2084      446869 :   if (DECL_UID (a) != DECL_UID (b))
    2085      446851 :     return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
    2086          18 :   gcc_assert (a == b);
    2087             :   return 0;
    2088             : }
    2089             : 
    2090             : static struct {
    2091             :   gt_pointer_operator new_value;
    2092             :   void *cookie;
    2093             : } resort_data;
    2094             : 
    2095             : /* This routine compares two fields like member_name_cmp but using the
    2096             :    pointer operator in resort_field_decl_data.  We don't have to deal
    2097             :    with duplicates here.  */
    2098             : 
    2099             : static int
    2100     9421313 : resort_member_name_cmp (const void *a_p, const void *b_p)
    2101             : {
    2102     9421313 :   tree a = *(const tree *)a_p;
    2103     9421313 :   tree b = *(const tree *)b_p;
    2104    18842626 :   tree name_a = OVL_NAME (a);
    2105    18842626 :   tree name_b = OVL_NAME (b);
    2106             : 
    2107     9421313 :   resort_data.new_value (&name_a, &name_a, resort_data.cookie);
    2108     9421313 :   resort_data.new_value (&name_b, &name_b, resort_data.cookie);
    2109             : 
    2110     9421313 :   gcc_checking_assert (name_a != name_b);
    2111             : 
    2112     9421313 :   return name_a < name_b ? -1 : +1;
    2113             : }
    2114             : 
    2115             : /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered.  */
    2116             : 
    2117             : void
    2118       59372 : resort_type_member_vec (void *obj, void */*orig_obj*/,
    2119             :                         gt_pointer_operator new_value, void* cookie)
    2120             : {
    2121       59372 :   if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
    2122             :     {
    2123       59372 :       resort_data.new_value = new_value;
    2124       59372 :       resort_data.cookie = cookie;
    2125       59372 :       member_vec->qsort (resort_member_name_cmp);
    2126             :     }
    2127       59372 : }
    2128             : 
    2129             : /* Recursively count the number of fields in KLASS, including anonymous
    2130             :    union members.  */
    2131             : 
    2132             : static unsigned
    2133    38668504 : count_class_fields (tree klass)
    2134             : {
    2135    38668504 :   unsigned n_fields = 0;
    2136             : 
    2137   331316743 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2138   292648239 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2139             :       /* Functions are dealt with separately.  */;
    2140   136143614 :     else if (TREE_CODE (fields) == FIELD_DECL
    2141   136143614 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2142      107171 :       n_fields += count_class_fields (TREE_TYPE (fields));
    2143   136036443 :     else if (DECL_NAME (fields))
    2144   121427856 :       n_fields += 1;
    2145             : 
    2146    38668504 :   return n_fields;
    2147             : }
    2148             : 
    2149             : /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
    2150             :    Recurse for anonymous members.  MEMBER_VEC must have space.  */
    2151             : 
    2152             : static void
    2153    13441150 : member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
    2154             : {
    2155   246335293 :   for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
    2156   232894143 :     if (DECL_DECLARES_FUNCTION_P (fields))
    2157             :       /* Functions are handled separately.  */;
    2158    76389533 :     else if (TREE_CODE (fields) == FIELD_DECL
    2159    76389533 :              && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
    2160       99313 :       member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
    2161    76290220 :     else if (DECL_NAME (fields))
    2162             :       {
    2163    73069823 :         tree field = fields;
    2164             :         /* Mark a conv-op USING_DECL with the conv-op-marker.  */
    2165    73069823 :         if (TREE_CODE (field) == USING_DECL
    2166    73069823 :             && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
    2167       98718 :           field = ovl_make (conv_op_marker, field);
    2168    73069823 :         member_vec->quick_push (field);
    2169             :       }
    2170    13441150 : }
    2171             : 
    2172             : /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
    2173             :    MEMBER_VEC must have space.  */
    2174             : 
    2175             : static void
    2176          21 : member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
    2177             : {
    2178          21 :   for (tree values = TYPE_VALUES (enumtype);
    2179          51 :        values; values = TREE_CHAIN (values))
    2180          30 :     member_vec->quick_push (TREE_VALUE (values));
    2181          21 : }
    2182             : 
    2183             : /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
    2184             :    DeDup adjacent DECLS of the same name.  We already dealt with
    2185             :    conflict resolution when adding the fields or methods themselves.
    2186             :    There are three cases (which could all be combined):
    2187             :    1) a TYPE_DECL and non TYPE_DECL.  Deploy STAT_HACK as appropriate.
    2188             :    2) a USING_DECL and an overload.  If the USING_DECL is dependent,
    2189             :    it wins.  Otherwise the OVERLOAD does.
    2190             :    3) two USING_DECLS. ...
    2191             : 
    2192             :    member_name_cmp will have ordered duplicates as
    2193             :    <fns><using><type>  */
    2194             : 
    2195             : static void
    2196    13341858 : member_vec_dedup (vec<tree, va_gc> *member_vec)
    2197             : {
    2198    13341858 :   unsigned len = member_vec->length ();
    2199    13341858 :   unsigned store = 0;
    2200             : 
    2201    13341858 :   if (!len)
    2202             :     return;
    2203             : 
    2204    26683710 :   tree name = OVL_NAME ((*member_vec)[0]);
    2205   168677540 :   for (unsigned jx, ix = 0; ix < len; ix = jx)
    2206             :     {
    2207             :       tree current = NULL_TREE;
    2208             :       tree to_type = NULL_TREE;
    2209             :       tree to_using = NULL_TREE;
    2210             :       tree marker = NULL_TREE;
    2211             : 
    2212   313077550 :       for (jx = ix; jx < len; jx++)
    2213             :         {
    2214   299735695 :           tree next = (*member_vec)[jx];
    2215   299735695 :           if (jx != ix)
    2216             :             {
    2217   144400010 :               tree next_name = OVL_NAME (next);
    2218   144400010 :               if (next_name != name)
    2219             :                 {
    2220             :                   name = next_name;
    2221             :                   break;
    2222             :                 }
    2223             :             }
    2224             : 
    2225   157741865 :           if (IDENTIFIER_CONV_OP_P (name))
    2226             :             {
    2227      861582 :               marker = next;
    2228      861582 :               next = OVL_CHAIN (next);
    2229             :             }
    2230             : 
    2231   157741865 :           if (TREE_CODE (next) == USING_DECL)
    2232             :             {
    2233     6338812 :               if (IDENTIFIER_CTOR_P (name))
    2234             :                 /* Dependent inherited ctor. */
    2235      138065 :                 continue;
    2236             : 
    2237     6200747 :               next = strip_using_decl (next);
    2238     6200747 :               if (TREE_CODE (next) == USING_DECL)
    2239             :                 {
    2240     5192675 :                   to_using = next;
    2241     5192675 :                   continue;
    2242             :                 }
    2243             : 
    2244     1008072 :               if (is_overloaded_fn (next))
    2245      947871 :                 continue;
    2246             :             }
    2247             : 
    2248   151463254 :           if (DECL_DECLARES_TYPE_P (next))
    2249             :             {
    2250    45819708 :               to_type = next;
    2251    45819708 :               continue;
    2252             :             }
    2253             : 
    2254   105643546 :           if (!current)
    2255   105643528 :             current = next;
    2256             :         }
    2257             : 
    2258   155335685 :       if (to_using)
    2259             :         {
    2260     5181463 :           if (!current)
    2261             :             current = to_using;
    2262             :           else
    2263     1223782 :             current = ovl_make (to_using, current);
    2264             :         }
    2265             : 
    2266   155335685 :       if (to_type)
    2267             :         {
    2268    45720262 :           if (!current)
    2269             :             current = to_type;
    2270             :           else
    2271         208 :             current = stat_hack (current, to_type);
    2272             :         }
    2273             : 
    2274   155335685 :       if (current)
    2275             :         {
    2276   155321263 :           if (marker)
    2277             :             {
    2278      762864 :               OVL_CHAIN (marker) = current;
    2279      762864 :               current = marker;
    2280             :             }
    2281   155321263 :           (*member_vec)[store++] = current;
    2282             :         }
    2283             :     }
    2284             : 
    2285    15762457 :   while (store++ < len)
    2286     2420602 :     member_vec->pop ();
    2287             : }
    2288             : 
    2289             : /* Add the non-function members to CLASSTYPE_MEMBER_VEC.  If there is
    2290             :    no existing MEMBER_VEC and fewer than 8 fields, do nothing.  We
    2291             :    know there must be at least 1 field -- the self-reference
    2292             :    TYPE_DECL, except for anon aggregates, which will have at least
    2293             :    one field anyway.  If EXTRA < 0, always create the vector.  */
    2294             : 
    2295             : vec<tree, va_gc> *
    2296    38561312 : set_class_bindings (tree klass, int extra)
    2297             : {
    2298    38561312 :   unsigned n_fields = count_class_fields (klass);
    2299    38561312 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2300             : 
    2301    38561312 :   if (member_vec || n_fields >= 8 || extra < 0)
    2302             :     {
    2303             :       /* Append the new fields.  */
    2304    13341831 :       vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
    2305    13341831 :       member_vec_append_class_fields (member_vec, klass);
    2306             :     }
    2307             : 
    2308    38561312 :   if (member_vec)
    2309             :     {
    2310    13341831 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2311    13341831 :       member_vec->qsort (member_name_cmp);
    2312    13341831 :       member_vec_dedup (member_vec);
    2313             :     }
    2314             : 
    2315    38561312 :   return member_vec;
    2316             : }
    2317             : 
    2318             : /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case.  */
    2319             : 
    2320             : void
    2321          42 : insert_late_enum_def_bindings (tree klass, tree enumtype)
    2322             : {
    2323          42 :   int n_fields;
    2324          42 :   vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    2325             : 
    2326             :   /* The enum bindings will already be on the TYPE_FIELDS, so don't
    2327             :      count them twice.  */
    2328          42 :   if (!member_vec)
    2329          21 :     n_fields = count_class_fields (klass);
    2330             :   else
    2331          21 :     n_fields = list_length (TYPE_VALUES (enumtype));
    2332             : 
    2333          42 :   if (member_vec || n_fields >= 8)
    2334             :     {
    2335          27 :       vec_safe_reserve_exact (member_vec, n_fields);
    2336          27 :       if (CLASSTYPE_MEMBER_VEC (klass))
    2337          21 :         member_vec_append_enum_values (member_vec, enumtype);
    2338             :       else
    2339           6 :         member_vec_append_class_fields (member_vec, klass);
    2340          27 :       CLASSTYPE_MEMBER_VEC (klass) = member_vec;
    2341          27 :       member_vec->qsort (member_name_cmp);
    2342          27 :       member_vec_dedup (member_vec);
    2343             :     }
    2344          42 : }
    2345             : 
    2346             : /* The binding oracle; see cp-tree.h.  */
    2347             : 
    2348             : cp_binding_oracle_function *cp_binding_oracle;
    2349             : 
    2350             : /* If we have a binding oracle, ask it for all namespace-scoped
    2351             :    definitions of NAME.  */
    2352             : 
    2353             : static inline void
    2354  2892424688 : query_oracle (tree name)
    2355             : {
    2356  2892424688 :   if (!cp_binding_oracle)
    2357             :     return;
    2358             : 
    2359             :   /* LOOKED_UP holds the set of identifiers that we have already
    2360             :      looked up with the oracle.  */
    2361           0 :   static hash_set<tree> looked_up;
    2362           0 :   if (looked_up.add (name))
    2363             :     return;
    2364             : 
    2365           0 :   cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
    2366             : }
    2367             : 
    2368             : #ifndef ENABLE_SCOPE_CHECKING
    2369             : #  define ENABLE_SCOPE_CHECKING 0
    2370             : #else
    2371             : #  define ENABLE_SCOPE_CHECKING 1
    2372             : #endif
    2373             : 
    2374             : /* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
    2375             : 
    2376             : static GTY((deletable)) cxx_binding *free_bindings;
    2377             : 
    2378             : /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
    2379             :    field to NULL.  */
    2380             : 
    2381             : static inline void
    2382   805979302 : cxx_binding_init (cxx_binding *binding, tree value, tree type)
    2383             : {
    2384   805979302 :   binding->value = value;
    2385   805979302 :   binding->type = type;
    2386   805979302 :   binding->previous = NULL;
    2387             : }
    2388             : 
    2389             : /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
    2390             : 
    2391             : static cxx_binding *
    2392   805979302 : cxx_binding_make (tree value, tree type)
    2393             : {
    2394   805979302 :   cxx_binding *binding = free_bindings;
    2395             : 
    2396   805979302 :   if (binding)
    2397   798826145 :     free_bindings = binding->previous;
    2398             :   else
    2399     7153157 :     binding = ggc_alloc<cxx_binding> ();
    2400             : 
    2401             :   /* Clear flags by default.  */
    2402   805979302 :   LOCAL_BINDING_P (binding) = false;
    2403   805979302 :   INHERITED_VALUE_BINDING_P (binding) = false;
    2404   805979302 :   HIDDEN_TYPE_BINDING_P (binding) = false;
    2405             : 
    2406   805979302 :   cxx_binding_init (binding, value, type);
    2407             : 
    2408   805979302 :   return binding;
    2409             : }
    2410             : 
    2411             : /* Put BINDING back on the free list.  */
    2412             : 
    2413             : static inline void
    2414   805975499 : cxx_binding_free (cxx_binding *binding)
    2415             : {
    2416   805975499 :   binding->scope = NULL;
    2417   805975499 :   binding->previous = free_bindings;
    2418   805975499 :   free_bindings = binding;
    2419   534600134 : }
    2420             : 
    2421             : /* Create a new binding for NAME (with the indicated VALUE and TYPE
    2422             :    bindings) in the class scope indicated by SCOPE.  */
    2423             : 
    2424             : static cxx_binding *
    2425   271379140 : new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
    2426             : {
    2427   271379140 :   cp_class_binding cb = {cxx_binding_make (value, type), name};
    2428   271379140 :   cxx_binding *binding = cb.base;
    2429   271379140 :   vec_safe_push (scope->class_shadowed, cb);
    2430   271379140 :   binding->scope = scope;
    2431   271379140 :   return binding;
    2432             : }
    2433             : 
    2434             : /* Make DECL the innermost binding for ID.  The LEVEL is the binding
    2435             :    level at which this declaration is being bound.  */
    2436             : 
    2437             : void
    2438   179459466 : push_binding (tree id, tree decl, cp_binding_level* level)
    2439             : {
    2440   179459466 :   cxx_binding *binding;
    2441             : 
    2442   179459466 :   if (level != class_binding_level)
    2443             :     {
    2444      340860 :       binding = cxx_binding_make (decl, NULL_TREE);
    2445      340860 :       binding->scope = level;
    2446             :     }
    2447             :   else
    2448   179118606 :     binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
    2449             : 
    2450             :   /* Now, fill in the binding information.  */
    2451   179459466 :   binding->previous = IDENTIFIER_BINDING (id);
    2452   179459466 :   LOCAL_BINDING_P (binding) = (level != class_binding_level);
    2453             : 
    2454             :   /* And put it on the front of the list of bindings for ID.  */
    2455   179459466 :   IDENTIFIER_BINDING (id) = binding;
    2456   179459466 : }
    2457             : 
    2458             : /* Remove the binding for DECL which should be the innermost binding
    2459             :    for ID.  */
    2460             : 
    2461             : void
    2462   551581302 : pop_local_binding (tree id, tree decl)
    2463             : {
    2464  1086431492 :   if (!id || IDENTIFIER_ANON_P (id))
    2465             :     /* It's easiest to write the loops that call this function without
    2466             :        checking whether or not the entities involved have names.  We
    2467             :        get here for such an entity.  */
    2468             :     return;
    2469             : 
    2470             :   /* Get the innermost binding for ID.  */
    2471   534600320 :   cxx_binding *binding = IDENTIFIER_BINDING (id);
    2472             : 
    2473             :   /* The name should be bound.  */
    2474   534600320 :   gcc_assert (binding != NULL);
    2475             : 
    2476             :   /* The DECL will be either the ordinary binding or the type binding
    2477             :      for this identifier.  Remove that binding.  We don't have to
    2478             :      clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
    2479             :      away.  */
    2480   534600320 :   if (binding->value == decl)
    2481   534600134 :     binding->value = NULL_TREE;
    2482             :   else
    2483             :     {
    2484         186 :       gcc_checking_assert (binding->type == decl);
    2485         186 :       binding->type = NULL_TREE;
    2486             :     }
    2487             : 
    2488   534600320 :   if (!binding->value && !binding->type)
    2489             :     {
    2490             :       /* We're completely done with the innermost binding for this
    2491             :          identifier.  Unhook it from the list of bindings.  */
    2492   534600134 :       IDENTIFIER_BINDING (id) = binding->previous;
    2493             : 
    2494             :       /* Add it to the free list.  */
    2495   534600134 :       cxx_binding_free (binding);
    2496             :     }
    2497             : }
    2498             : 
    2499             : /* Remove the bindings for the decls of the current level and leave
    2500             :    the current scope.  */
    2501             : 
    2502             : void
    2503   126069604 : pop_bindings_and_leave_scope (void)
    2504             : {
    2505   290581392 :   for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
    2506             :     {
    2507   164511788 :       tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
    2508   329023576 :       tree name = OVL_NAME (decl);
    2509             : 
    2510   164511788 :       pop_local_binding (name, decl);
    2511             :     }
    2512             : 
    2513   126069604 :   leave_scope ();
    2514   126069604 : }
    2515             : 
    2516             : /* Strip non dependent using declarations. If DECL is dependent,
    2517             :    surreptitiously create a typename_type and return it.  */
    2518             : 
    2519             : tree
    2520  2731476364 : strip_using_decl (tree decl)
    2521             : {
    2522  2731476364 :   if (decl == NULL_TREE)
    2523             :     return NULL_TREE;
    2524             : 
    2525  2734089519 :   while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
    2526     2626942 :     decl = USING_DECL_DECLS (decl);
    2527             : 
    2528    13517667 :   if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
    2529  2744980244 :       && USING_DECL_TYPENAME_P (decl))
    2530             :     {
    2531             :       /* We have found a type introduced by a using
    2532             :          declaration at class scope that refers to a dependent
    2533             :          type.
    2534             :              
    2535             :          using typename :: [opt] nested-name-specifier unqualified-id ;
    2536             :       */
    2537      104333 :       decl = make_typename_type (USING_DECL_SCOPE (decl),
    2538      104333 :                                  DECL_NAME (decl),
    2539             :                                  typename_type, tf_error);
    2540      104333 :       if (decl != error_mark_node)
    2541      104333 :         decl = TYPE_NAME (decl);
    2542             :     }
    2543             : 
    2544             :   return decl;
    2545             : }
    2546             : 
    2547             : /* Return true if OVL is an overload for an anticipated builtin.  */
    2548             : 
    2549             : static bool
    2550    16505324 : anticipated_builtin_p (tree ovl)
    2551             : {
    2552    16505324 :   return (TREE_CODE (ovl) == OVERLOAD
    2553    15174115 :           && OVL_HIDDEN_P (ovl)
    2554    20864138 :           && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl)));
    2555             : }
    2556             : 
    2557             : /* BINDING records an existing declaration for a name in the current scope.
    2558             :    But, DECL is another declaration for that same identifier in the
    2559             :    same scope.  This is the `struct stat' hack whereby a non-typedef
    2560             :    class name or enum-name can be bound at the same level as some other
    2561             :    kind of entity.
    2562             :    3.3.7/1
    2563             : 
    2564             :      A class name (9.1) or enumeration name (7.2) can be hidden by the
    2565             :      name of an object, function, or enumerator declared in the same scope.
    2566             :      If a class or enumeration name and an object, function, or enumerator
    2567             :      are declared in the same scope (in any order) with the same name, the
    2568             :      class or enumeration name is hidden wherever the object, function, or
    2569             :      enumerator name is visible.
    2570             : 
    2571             :    It's the responsibility of the caller to check that
    2572             :    inserting this name is valid here.  Returns nonzero if the new binding
    2573             :    was successful.  */
    2574             : 
    2575             : static bool
    2576        7923 : supplement_binding (cxx_binding *binding, tree decl)
    2577             : {
    2578        7923 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    2579             : 
    2580        7923 :   tree bval = binding->value;
    2581        7923 :   bool ok = true;
    2582        7923 :   tree target_bval = strip_using_decl (bval);
    2583        7923 :   tree target_decl = strip_using_decl (decl);
    2584             : 
    2585        7614 :   if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
    2586        7414 :       && target_decl != target_bval
    2587       15325 :       && (TREE_CODE (target_bval) != TYPE_DECL
    2588             :           /* We allow pushing an enum multiple times in a class
    2589             :              template in order to handle late matching of underlying
    2590             :              type on an opaque-enum-declaration followed by an
    2591             :              enum-specifier.  */
    2592        7266 :           || (processing_template_decl
    2593         111 :               && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
    2594           0 :               && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
    2595           0 :               && (dependent_type_p (ENUM_UNDERLYING_TYPE
    2596             :                                     (TREE_TYPE (target_decl)))
    2597           0 :                   || dependent_type_p (ENUM_UNDERLYING_TYPE
    2598             :                                        (TREE_TYPE (target_bval)))))))
    2599             :     /* The new name is the type name.  */
    2600         136 :     binding->type = decl;
    2601        7787 :   else if (/* TARGET_BVAL is null when push_class_level_binding moves
    2602             :               an inherited type-binding out of the way to make room
    2603             :               for a new value binding.  */
    2604             :            !target_bval
    2605             :            /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
    2606             :               has been used in a non-class scope prior declaration.
    2607             :               In that case, we should have already issued a
    2608             :               diagnostic; for graceful error recovery purpose, pretend
    2609             :               this was the intended declaration for that name.  */
    2610        7787 :            || target_bval == error_mark_node
    2611             :            /* If TARGET_BVAL is anticipated but has not yet been
    2612             :               declared, pretend it is not there at all.  */
    2613       15574 :            || anticipated_builtin_p (target_bval))
    2614           0 :     binding->value = decl;
    2615        7787 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2616        7672 :            && DECL_ARTIFICIAL (target_bval)
    2617        7628 :            && target_decl != target_bval
    2618       15403 :            && (TREE_CODE (target_decl) != TYPE_DECL
    2619        7422 :                || same_type_p (TREE_TYPE (target_decl),
    2620             :                                TREE_TYPE (target_bval))))
    2621             :     {
    2622             :       /* The old binding was a type name.  It was placed in
    2623             :          VALUE field because it was thought, at the point it was
    2624             :          declared, to be the only entity with such a name.  Move the
    2625             :          type name into the type slot; it is now hidden by the new
    2626             :          binding.  */
    2627        7584 :       binding->type = bval;
    2628        7584 :       binding->value = decl;
    2629        7584 :       binding->value_is_inherited = false;
    2630             :     }
    2631         203 :   else if (TREE_CODE (target_bval) == TYPE_DECL
    2632          88 :            && TREE_CODE (target_decl) == TYPE_DECL
    2633          88 :            && DECL_NAME (target_decl) == DECL_NAME (target_bval)
    2634          88 :            && binding->scope->kind != sk_class
    2635         204 :            && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
    2636             :                /* If either type involves template parameters, we must
    2637             :                   wait until instantiation.  */
    2638           0 :                || uses_template_parms (TREE_TYPE (target_decl))
    2639           0 :                || uses_template_parms (TREE_TYPE (target_bval))))
    2640             :     /* We have two typedef-names, both naming the same type to have
    2641             :        the same name.  In general, this is OK because of:
    2642             : 
    2643             :          [dcl.typedef]
    2644             : 
    2645             :          In a given scope, a typedef specifier can be used to redefine
    2646             :          the name of any type declared in that scope to refer to the
    2647             :          type to which it already refers.
    2648             : 
    2649             :        However, in class scopes, this rule does not apply due to the
    2650             :        stricter language in [class.mem] prohibiting redeclarations of
    2651             :        members.  */
    2652             :     ok = false;
    2653             :   /* There can be two block-scope declarations of the same variable,
    2654             :      so long as they are `extern' declarations.  However, there cannot
    2655             :      be two declarations of the same static data member:
    2656             : 
    2657             :        [class.mem]
    2658             : 
    2659             :        A member shall not be declared twice in the
    2660             :        member-specification.  */
    2661         202 :   else if (VAR_P (target_decl)
    2662           8 :            && VAR_P (target_bval)
    2663           8 :            && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
    2664         210 :            && !DECL_CLASS_SCOPE_P (target_decl))
    2665             :     {
    2666           0 :       duplicate_decls (decl, binding->value);
    2667           0 :       ok = false;
    2668             :     }
    2669         202 :   else if (TREE_CODE (decl) == NAMESPACE_DECL
    2670           0 :            && TREE_CODE (bval) == NAMESPACE_DECL
    2671           0 :            && DECL_NAMESPACE_ALIAS (decl)
    2672           0 :            && DECL_NAMESPACE_ALIAS (bval)
    2673         202 :            && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
    2674             :     /* [namespace.alias]
    2675             : 
    2676             :       In a declarative region, a namespace-alias-definition can be
    2677             :       used to redefine a namespace-alias declared in that declarative
    2678             :       region to refer only to the namespace to which it already
    2679             :       refers.  */
    2680             :     ok = false;
    2681         202 :   else if (TREE_CODE (bval) == USING_DECL
    2682         202 :            && CONST_DECL_USING_P (decl))
    2683             :     /* Let the clone hide the using-decl that introduced it.  */
    2684          32 :     binding->value = decl;
    2685             :   else
    2686             :     {
    2687         170 :       if (!error_operand_p (bval))
    2688         170 :         diagnose_name_conflict (decl, bval);
    2689             :       ok = false;
    2690             :     }
    2691             : 
    2692       15846 :   return ok;
    2693        7923 : }
    2694             : 
    2695             : /* Diagnose a name conflict between DECL and BVAL.
    2696             : 
    2697             :    This is non-static so maybe_push_used_methods can use it and avoid changing
    2698             :    the diagnostic for inherit/using4.C; otherwise it should not be used from
    2699             :    outside this file.  */
    2700             : 
    2701             : void
    2702         287 : diagnose_name_conflict (tree decl, tree bval)
    2703             : {
    2704         287 :   if (TREE_CODE (decl) == TREE_CODE (bval)
    2705         209 :       && TREE_CODE (decl) != NAMESPACE_DECL
    2706         201 :       && !DECL_DECLARES_FUNCTION_P (decl)
    2707         137 :       && (TREE_CODE (decl) != TYPE_DECL
    2708          28 :           || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
    2709         420 :       && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
    2710             :     {
    2711         100 :       if (concept_definition_p (decl))
    2712           2 :         error ("redeclaration of %q#D with different template parameters",
    2713             :                decl);
    2714             :       else
    2715          98 :         error ("redeclaration of %q#D", decl);
    2716             :     }
    2717             :   else
    2718         187 :     error ("%q#D conflicts with a previous declaration", decl);
    2719             : 
    2720         287 :   inform (location_of (bval), "previous declaration %q#D", bval);
    2721         287 : }
    2722             : 
    2723             : /* Replace BINDING's current value on its scope's name list with
    2724             :    NEWVAL.  */
    2725             : 
    2726             : static void
    2727         106 : update_local_overload (cxx_binding *binding, tree newval)
    2728             : {
    2729         106 :   tree *d;
    2730             : 
    2731         124 :   for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
    2732         124 :     if (*d == binding->value)
    2733             :       {
    2734             :         /* Stitch new list node in.  */
    2735          61 :         *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
    2736          61 :         break;
    2737             :       }
    2738          63 :     else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
    2739             :       break;
    2740             : 
    2741         106 :   TREE_VALUE (*d) = newval;
    2742         106 : }
    2743             : 
    2744             : /* Compares the parameter-type-lists of ONE and TWO and
    2745             :    returns false if they are different.  If the DECLs are template
    2746             :    functions, the return types and the template parameter lists are
    2747             :    compared too (DR 565).  */
    2748             : 
    2749             : static bool
    2750     2375255 : matching_fn_p (tree one, tree two)
    2751             : {
    2752     2375255 :   if (TREE_CODE (one) != TREE_CODE (two))
    2753             :     return false;
    2754             : 
    2755     1047780 :   if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
    2756     1047780 :                   TYPE_ARG_TYPES (TREE_TYPE (two))))
    2757             :     return false;
    2758             : 
    2759         125 :   if (TREE_CODE (one) == TEMPLATE_DECL)
    2760             :     {
    2761             :       /* Compare template parms.  */
    2762          90 :       if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
    2763          45 :                                 DECL_TEMPLATE_PARMS (two)))
    2764             :         return false;
    2765             : 
    2766             :       /* And return type.  */
    2767          21 :       if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
    2768             :                         TREE_TYPE (TREE_TYPE (two))))
    2769             :         return false;
    2770             :     }
    2771             : 
    2772          89 :   if (!equivalently_constrained (one, two))
    2773             :     return false;
    2774             : 
    2775             :   return true;
    2776             : }
    2777             : 
    2778             : /* Push DECL into nonclass LEVEL BINDING or SLOT.  OLD is the current
    2779             :    binding value (possibly with anticipated builtins stripped).
    2780             :    Diagnose conflicts and return updated decl.  */
    2781             : 
    2782             : static tree
    2783   829103900 : update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
    2784             :                 tree old, tree decl, bool hiding = false)
    2785             : {
    2786   829103900 :   tree old_type = NULL_TREE;
    2787   829103900 :   bool hide_type = false;
    2788   829103900 :   bool hide_value = false;
    2789             : 
    2790   829103900 :   if (!slot)
    2791             :     {
    2792   534259529 :       old_type = binding->type;
    2793   534259529 :       hide_type = HIDDEN_TYPE_BINDING_P (binding);
    2794   534259529 :       if (!old_type)
    2795   534259525 :         hide_value = hide_type, hide_type = false;
    2796             :     }
    2797   294844371 :   else if (STAT_HACK_P (*slot))
    2798             :     {
    2799          50 :       old_type = STAT_TYPE (*slot);
    2800          50 :       hide_type = STAT_TYPE_HIDDEN_P (*slot);
    2801          50 :       hide_value = STAT_DECL_HIDDEN_P (*slot);
    2802             :     }
    2803             : 
    2804   829103900 :   tree to_val = decl;
    2805   829103900 :   tree to_type = old_type;
    2806   829103900 :   bool local_overload = false;
    2807             : 
    2808   829103900 :   gcc_assert (!level || level->kind == sk_namespace ? !binding
    2809             :               : level->kind != sk_class && !slot);
    2810             : 
    2811   829103900 :   if (old == error_mark_node)
    2812       89260 :     old = NULL_TREE;
    2813             : 
    2814   829103900 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    2815             :     {
    2816             :       /* Pushing an artificial decl.  We should not find another
    2817             :          artificial decl here already -- lookup_elaborated_type will
    2818             :          have already found it.  */
    2819     3350851 :       gcc_checking_assert (!to_type
    2820             :                            && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
    2821             : 
    2822             :       if (old)
    2823             :         {
    2824             :           /* Put DECL into the type slot.  */
    2825             :           gcc_checking_assert (!to_type);
    2826             :           hide_type = hiding;
    2827             :           to_type = decl;
    2828             :           to_val = old;
    2829             :         }
    2830             :       else
    2831             :         hide_value = hiding;
    2832             : 
    2833     3350851 :       goto done;
    2834             :     }
    2835             : 
    2836   825753049 :   if (old && DECL_IMPLICIT_TYPEDEF_P (old))
    2837             :     {
    2838             :       /* OLD is an implicit typedef.  Move it to to_type.  */
    2839       58872 :       gcc_checking_assert (!to_type);
    2840             : 
    2841             :       to_type = old;
    2842             :       hide_type = hide_value;
    2843             :       old = NULL_TREE;
    2844             :       hide_value = false;
    2845             :     }
    2846             : 
    2847   825753049 :   if (DECL_DECLARES_FUNCTION_P (decl))
    2848             :     {
    2849   272682248 :       if (!old)
    2850             :         ;
    2851    16325325 :       else if (OVL_P (old))
    2852             :         {
    2853   454304309 :           for (ovl_iterator iter (old); iter; ++iter)
    2854             :             {
    2855   218989500 :               tree fn = *iter;
    2856             : 
    2857   218989500 :               if (iter.using_p () && matching_fn_p (fn, decl))
    2858             :                 {
    2859          44 :                   gcc_checking_assert (!iter.hidden_p ());
    2860             :                   /* If a function declaration in namespace scope or
    2861             :                      block scope has the same name and the same
    2862             :                      parameter-type- list (8.3.5) as a function
    2863             :                      introduced by a using-declaration, and the
    2864             :                      declarations do not declare the same function,
    2865             :                      the program is ill-formed.  [namespace.udecl]/14 */
    2866          44 :                   if (tree match = duplicate_decls (decl, fn, hiding))
    2867           8 :                     return match;
    2868             :                   else
    2869             :                     /* FIXME: To preserve existing error behavior, we
    2870             :                        still push the decl.  This might change.  */
    2871          36 :                     diagnose_name_conflict (decl, fn);
    2872             :                 }
    2873             :             }
    2874    16325317 :         }
    2875             :       else
    2876           0 :         goto conflict;
    2877             : 
    2878   272682240 :       if (to_type != old_type
    2879       29611 :           && warn_shadow
    2880          16 :           && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
    2881   272682248 :           && !(DECL_IN_SYSTEM_HEADER (decl)
    2882           0 :                && DECL_IN_SYSTEM_HEADER (to_type)))
    2883           8 :         warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
    2884             :                  decl, to_type);
    2885             : 
    2886   272682240 :       local_overload = old && level && level->kind != sk_namespace;
    2887   272682240 :       to_val = ovl_insert (decl, old, -int (hiding));
    2888             :     }
    2889   553070801 :   else if (old)
    2890             :     {
    2891          27 :       if (TREE_CODE (old) != TREE_CODE (decl))
    2892             :         /* Different kinds of decls conflict.  */
    2893           0 :         goto conflict;
    2894          27 :       else if (TREE_CODE (old) == TYPE_DECL)
    2895             :         {
    2896           3 :           if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    2897             :             /* Two type decls to the same type.  Do nothing.  */
    2898             :             return old;
    2899             :           else
    2900           0 :             goto conflict;
    2901             :         }
    2902          24 :       else if (TREE_CODE (old) == NAMESPACE_DECL)
    2903             :         {
    2904             :           /* Two maybe-aliased namespaces.  If they're to the same target
    2905             :              namespace, that's ok.  */
    2906          24 :           if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
    2907           8 :             goto conflict;
    2908             : 
    2909             :           /* The new one must be an alias at this point.  */
    2910           4 :           gcc_assert (DECL_NAMESPACE_ALIAS (decl));
    2911             :           return old;
    2912             :         }
    2913          12 :       else if (TREE_CODE (old) == VAR_DECL)
    2914             :         {
    2915             :           /* There can be two block-scope declarations of the same
    2916             :              variable, so long as they are `extern' declarations.  */
    2917           7 :           if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
    2918           0 :             goto conflict;
    2919           7 :           else if (tree match = duplicate_decls (decl, old))
    2920             :             {
    2921           3 :               gcc_checking_assert (!hide_value && !hiding);
    2922             :               return match;
    2923             :             }
    2924             :           else
    2925           4 :             goto conflict;
    2926             :         }
    2927             :       else
    2928             :         {
    2929           5 :         conflict:
    2930          17 :           diagnose_name_conflict (decl, old);
    2931          17 :           to_val = NULL_TREE;
    2932             :         }
    2933             :     }
    2934   553070774 :   else if (hiding)
    2935       16703 :     hide_value = true;
    2936             : 
    2937   553054071 :  done:
    2938   829103882 :   if (to_val)
    2939             :     {
    2940   829103865 :       if (local_overload)
    2941             :         {
    2942          58 :           gcc_checking_assert (binding->value && OVL_P (binding->value));
    2943          58 :           update_local_overload (binding, to_val);
    2944             :         }
    2945   829103807 :       else if (level
    2946   829103807 :                && !(TREE_CODE (decl) == NAMESPACE_DECL
    2947      564386 :                     && !DECL_NAMESPACE_ALIAS (decl)))
    2948             :         /* Don't add namespaces here.  They're done in
    2949             :            push_namespace.  */
    2950   828569446 :         add_decl_to_level (level, decl);
    2951             : 
    2952   829103865 :       if (slot)
    2953             :         {
    2954   294844343 :           if (STAT_HACK_P (*slot))
    2955             :             {
    2956          50 :               STAT_TYPE (*slot) = to_type;
    2957          50 :               STAT_DECL (*slot) = to_val;
    2958          50 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    2959          50 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    2960             :             }
    2961   294844293 :           else if (to_type || hide_value)
    2962             :             {
    2963       88151 :               *slot = stat_hack (to_val, to_type);
    2964       88151 :               STAT_TYPE_HIDDEN_P (*slot) = hide_type;
    2965       88151 :               STAT_DECL_HIDDEN_P (*slot) = hide_value;
    2966             :             }
    2967             :           else
    2968             :             {
    2969   294756142 :               gcc_checking_assert (!hide_type);
    2970   294756142 :               *slot = to_val;
    2971             :             }
    2972             :         }
    2973             :       else
    2974             :         {
    2975   534259522 :           binding->type = to_type;
    2976   534259522 :           binding->value = to_val;
    2977   534259522 :           HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
    2978             :         }
    2979             :     }
    2980             : 
    2981             :   return decl;
    2982             : }
    2983             : 
    2984             : /* Table of identifiers to extern C declarations (or LISTS thereof).  */
    2985             : 
    2986             : static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
    2987             : 
    2988             : /* DECL has C linkage. If we have an existing instance, make sure the
    2989             :    new one is compatible.  Make sure it has the same exception
    2990             :    specification [7.5, 7.6].  Add DECL to the map.  */
    2991             : 
    2992             : static void
    2993   244919135 : check_extern_c_conflict (tree decl)
    2994             : {
    2995             :   /* Ignore artificial or system header decls.  */
    2996   244919135 :   if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
    2997   244812935 :     return;
    2998             : 
    2999             :   /* This only applies to decls at namespace scope.  */
    3000      106200 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
    3001             :     return;
    3002             : 
    3003      106186 :   if (!extern_c_decls)
    3004       32642 :     extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
    3005             : 
    3006      106186 :   tree *slot = extern_c_decls
    3007      106186 :     ->find_slot_with_hash (DECL_NAME (decl),
    3008      106186 :                            IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
    3009      106186 :   if (tree old = *slot)
    3010             :     {
    3011         418 :       if (TREE_CODE (old) == OVERLOAD)
    3012          23 :         old = OVL_FUNCTION (old);
    3013             : 
    3014         418 :       int mismatch = 0;
    3015         418 :       if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
    3016             :         ; /* If they're in the same context, we'll have already complained
    3017             :              about a (possible) mismatch, when inserting the decl.  */
    3018         398 :       else if (!decls_match (decl, old))
    3019             :         mismatch = 1;
    3020         370 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    3021         724 :                && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
    3022         354 :                                       TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
    3023             :                                       ce_normal))
    3024             :         mismatch = -1;
    3025         366 :       else if (DECL_ASSEMBLER_NAME_SET_P (old))
    3026          12 :         SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
    3027             : 
    3028          12 :       if (mismatch)
    3029             :         {
    3030          32 :           auto_diagnostic_group d;
    3031          32 :           pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3032             :                    "conflicting C language linkage declaration %q#D", decl);
    3033          32 :           inform (DECL_SOURCE_LOCATION (old),
    3034             :                   "previous declaration %q#D", old);
    3035          32 :           if (mismatch < 0)
    3036           4 :             inform (DECL_SOURCE_LOCATION (decl),
    3037             :                     "due to different exception specifications");
    3038          32 :         }
    3039             :       else
    3040             :         {
    3041         386 :           if (old == *slot)
    3042             :             /* The hash table expects OVERLOADS, so construct one with
    3043             :                OLD as both the function and the chain.  This allocate
    3044             :                an excess OVERLOAD node, but it's rare to have multiple
    3045             :                extern "C" decls of the same name.  And we save
    3046             :                complicating the hash table logic (which is used
    3047             :                elsewhere).  */
    3048         379 :             *slot = ovl_make (old, old);
    3049             : 
    3050         386 :           slot = &OVL_CHAIN (*slot);
    3051             : 
    3052             :           /* Chain it on for c_linkage_binding's use.  */
    3053         386 :           *slot = tree_cons (NULL_TREE, decl, *slot);
    3054             :         }
    3055             :     }
    3056             :   else
    3057      105768 :     *slot = decl;
    3058             : }
    3059             : 
    3060             : /* Returns a list of C-linkage decls with the name NAME.  Used in
    3061             :    c-family/c-pragma.cc to implement redefine_extname pragma.  */
    3062             : 
    3063             : tree
    3064          24 : c_linkage_bindings (tree name)
    3065             : {
    3066          24 :   if (extern_c_decls)
    3067          32 :     if (tree *slot = extern_c_decls
    3068          16 :         ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
    3069             :       {
    3070           8 :         tree result = *slot;
    3071           8 :         if (TREE_CODE (result) == OVERLOAD)
    3072           4 :           result = OVL_CHAIN (result);
    3073           8 :         return result;
    3074             :       }
    3075             : 
    3076             :   return NULL_TREE;
    3077             : }
    3078             : 
    3079             : /* Subroutine of check_local_shadow.  */
    3080             : 
    3081             : static void
    3082         210 : inform_shadowed (tree shadowed)
    3083             : {
    3084         210 :   inform (DECL_SOURCE_LOCATION (shadowed),
    3085             :           "shadowed declaration is here");
    3086         210 : }
    3087             : 
    3088             : /* DECL is being declared at a local scope.  Emit suitable shadow
    3089             :    warnings.  */
    3090             : 
    3091             : static void
    3092   534259529 : check_local_shadow (tree decl)
    3093             : {
    3094             :   /* Don't complain about the parms we push and then pop
    3095             :      while tentatively parsing a function declarator.  */
    3096   534259529 :   if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
    3097             :     return;
    3098             : 
    3099             :   /* External decls are something else.  */
    3100   383677370 :   if (DECL_EXTERNAL (decl))
    3101             :     return;
    3102             : 
    3103   379997375 :   tree old = NULL_TREE;
    3104   379997375 :   cp_binding_level *old_scope = NULL;
    3105   379997375 :   if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
    3106             :     {
    3107     1063154 :       old = binding->value;
    3108     1063154 :       old_scope = binding->scope;
    3109             :     }
    3110             : 
    3111     1063154 :   if (old
    3112     1063154 :       && (TREE_CODE (old) == PARM_DECL
    3113     1063154 :           || VAR_P (old)
    3114      128731 :           || (TREE_CODE (old) == TYPE_DECL
    3115       65338 :               && (!DECL_ARTIFICIAL (old)
    3116       42275 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3117      999647 :       && DECL_FUNCTION_SCOPE_P (old)
    3118     1998252 :       && (!DECL_ARTIFICIAL (decl)
    3119      607909 :           || is_capture_proxy (decl)
    3120      265271 :           || DECL_IMPLICIT_TYPEDEF_P (decl)
    3121      265225 :           || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
    3122             :     {
    3123             :       /* DECL shadows a local thing possibly of interest.  */
    3124             : 
    3125             :       /* DR 2211: check that captures and parameters
    3126             :          do not have the same name. */
    3127      669877 :       if (is_capture_proxy (decl))
    3128             :         {
    3129      342638 :           if (current_lambda_expr ()
    3130      342638 :               && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
    3131          15 :               && TREE_CODE (old) == PARM_DECL
    3132      342653 :               && DECL_NAME (decl) != this_identifier)
    3133             :             {
    3134          15 :               error_at (DECL_SOURCE_LOCATION (old),
    3135             :                         "lambda parameter %qD "
    3136             :                         "previously declared as a capture", old);
    3137             :             }
    3138      342638 :           return;
    3139             :         }
    3140             :       /* Don't complain if it's from an enclosing function.  */
    3141      327239 :       else if (DECL_CONTEXT (old) == current_function_decl
    3142      318115 :                && TREE_CODE (decl) != PARM_DECL
    3143      645354 :                && TREE_CODE (old) == PARM_DECL)
    3144             :         {
    3145             :           /* Go to where the parms should be and see if we find
    3146             :              them there.  */
    3147        7545 :           cp_binding_level *b = current_binding_level->level_chain;
    3148             : 
    3149       15090 :           if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
    3150             :             /* Skip the ctor/dtor cleanup level.  */
    3151           7 :             b = b->level_chain;
    3152             : 
    3153             :           /* [basic.scope.param] A parameter name shall not be redeclared
    3154             :              in the outermost block of the function definition.  */
    3155        7545 :           if (b->kind == sk_function_parms)
    3156             :             {
    3157          34 :               error_at (DECL_SOURCE_LOCATION (decl),
    3158             :                         "declaration of %q#D shadows a parameter", decl);
    3159          34 :               inform (DECL_SOURCE_LOCATION (old),
    3160             :                       "%q#D previously declared here", old);
    3161          34 :               return;
    3162             :             }
    3163             :         }
    3164             : 
    3165             :       /* The local structure or class can't use parameters of
    3166             :          the containing function anyway.  */
    3167      327205 :       if (DECL_CONTEXT (old) != current_function_decl)
    3168             :         {
    3169        9124 :           for (cp_binding_level *scope = current_binding_level;
    3170       22512 :                scope != old_scope; scope = scope->level_chain)
    3171       21430 :             if (scope->kind == sk_class
    3172       31690 :                 && !LAMBDA_TYPE_P (scope->this_entity))
    3173             :               return;
    3174             :         }
    3175             :       /* Error if redeclaring a local declared in a
    3176             :          init-statement or in the condition of an if or
    3177             :          switch statement when the new declaration is in the
    3178             :          outermost block of the controlled statement.
    3179             :          Redeclaring a variable from a for or while condition is
    3180             :          detected elsewhere.  */
    3181      318081 :       else if (VAR_P (old)
    3182      309878 :                && old_scope == current_binding_level->level_chain
    3183        1641 :                && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
    3184             :         {
    3185          42 :           auto_diagnostic_group d;
    3186          42 :           error_at (DECL_SOURCE_LOCATION (decl),
    3187             :                     "redeclaration of %q#D", decl);
    3188          42 :           inform (DECL_SOURCE_LOCATION (old),
    3189             :                   "%q#D previously declared here", old);
    3190          42 :           return;
    3191          42 :         }
    3192             :       /* C++11:
    3193             :          3.3.3/3:  The name declared in an exception-declaration (...)
    3194             :          shall not be redeclared in the outermost block of the handler.
    3195             :          3.3.3/2:  A parameter name shall not be redeclared (...) in
    3196             :          the outermost block of any handler associated with a
    3197             :          function-try-block.
    3198             :          3.4.1/15: The function parameter names shall not be redeclared
    3199             :          in the exception-declaration nor in the outermost block of a
    3200             :          handler for the function-try-block.  */
    3201      318039 :       else if ((TREE_CODE (old) == VAR_DECL
    3202      309836 :                 && old_scope == current_binding_level->level_chain
    3203        1599 :                 && old_scope->kind == sk_catch)
    3204      318031 :                || (TREE_CODE (old) == PARM_DECL
    3205        7511 :                    && (current_binding_level->kind == sk_catch
    3206        7503 :                        || current_binding_level->level_chain->kind == sk_catch)
    3207          20 :                    && in_function_try_handler))
    3208             :         {
    3209          20 :           auto_diagnostic_group d;
    3210          20 :           if (permerror (DECL_SOURCE_LOCATION (decl),
    3211             :                          "redeclaration of %q#D", decl))
    3212          20 :             inform (DECL_SOURCE_LOCATION (old),
    3213             :                     "%q#D previously declared here", old);
    3214          20 :           return;
    3215          20 :         }
    3216             : 
    3217             :       /* If '-Wshadow=compatible-local' is specified without other
    3218             :          -Wshadow= flags, we will warn only when the type of the
    3219             :          shadowing variable (DECL) can be converted to that of the
    3220             :          shadowed parameter (OLD_LOCAL). The reason why we only check
    3221             :          if DECL's type can be converted to OLD_LOCAL's type (but not the
    3222             :          other way around) is because when users accidentally shadow a
    3223             :          parameter, more than often they would use the variable
    3224             :          thinking (mistakenly) it's still the parameter. It would be
    3225             :          rare that users would use the variable in the place that
    3226             :          expects the parameter but thinking it's a new decl.
    3227             :          If either object is a TYPE_DECL, '-Wshadow=compatible-local'
    3228             :          warns regardless of whether one of the types involved
    3229             :          is a subclass of the other, since that is never okay.  */
    3230             : 
    3231      319101 :       enum opt_code warning_code;
    3232      319101 :       if (warn_shadow)
    3233             :         warning_code = OPT_Wshadow;
    3234      319029 :       else if ((TREE_CODE (decl) == TYPE_DECL)
    3235      319029 :                ^ (TREE_CODE (old) == TYPE_DECL))
    3236             :         /* If exactly one is a type, they aren't compatible.  */
    3237             :         warning_code = OPT_Wshadow_local;
    3238      319003 :       else if ((TREE_TYPE (old)
    3239      319003 :                 && TREE_TYPE (decl)
    3240      319000 :                 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
    3241       39553 :                || TREE_CODE (decl) == TYPE_DECL
    3242       39537 :                || TREE_CODE (old) == TYPE_DECL
    3243      358540 :                || (!dependent_type_p (TREE_TYPE (decl))
    3244       25344 :                    && !dependent_type_p (TREE_TYPE (old))
    3245             :                    /* If the new decl uses auto, we don't yet know
    3246             :                       its type (the old type cannot be using auto
    3247             :                       at this point, without also being
    3248             :                       dependent).  This is an indication we're
    3249             :                       (now) doing the shadow checking too
    3250             :                       early.  */
    3251       25329 :                    && !type_uses_auto (TREE_TYPE (decl))
    3252       25226 :                    && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
    3253             :                                        decl, LOOKUP_IMPLICIT, tf_none)))
    3254             :         warning_code = OPT_Wshadow_compatible_local;
    3255             :       else
    3256             :         warning_code = OPT_Wshadow_local;
    3257             : 
    3258      319101 :       const char *msg;
    3259      319101 :       if (TREE_CODE (old) == PARM_DECL)
    3260             :         msg = "declaration of %q#D shadows a parameter";
    3261      310978 :       else if (is_capture_proxy (old))
    3262             :         msg = "declaration of %qD shadows a lambda capture";
    3263             :       else
    3264      310801 :         msg = "declaration of %qD shadows a previous local";
    3265             : 
    3266      319101 :       auto_diagnostic_group d;
    3267      319101 :       if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
    3268         140 :         inform_shadowed (old);
    3269      319101 :       return;
    3270      319101 :     }
    3271             : 
    3272   379327498 :   if (!warn_shadow)
    3273             :     return;
    3274             : 
    3275             :   /* Don't warn for artificial things that are not implicit typedefs.  */
    3276         777 :   if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
    3277             :     return;
    3278             : 
    3279         400 :   if (nonlambda_method_basetype ())
    3280         200 :     if (tree member = lookup_member (current_nonlambda_class_type (),
    3281         200 :                                      DECL_NAME (decl), /*protect=*/0,
    3282             :                                      /*want_type=*/false, tf_warning_or_error))
    3283             :       {
    3284          62 :         member = MAYBE_BASELINK_FUNCTIONS (member);
    3285             : 
    3286             :         /* Warn if a variable shadows a non-function, or the variable
    3287             :            is a function or a pointer-to-function.  */
    3288          62 :         if ((!OVL_P (member)
    3289          12 :              || TREE_CODE (decl) == FUNCTION_DECL
    3290          12 :              || (TREE_TYPE (decl)
    3291           8 :                  && (TYPE_PTRFN_P (TREE_TYPE (decl))
    3292           8 :                      || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
    3293          66 :             && !warning_suppressed_p (decl, OPT_Wshadow))
    3294             :           {
    3295          30 :             auto_diagnostic_group d;
    3296          30 :             if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
    3297             :                             "declaration of %qD shadows a member of %qT",
    3298             :                             decl, current_nonlambda_class_type ())
    3299          30 :                 && DECL_P (member))
    3300             :               {
    3301          30 :                 inform_shadowed (member);
    3302          30 :                 suppress_warning (decl, OPT_Wshadow);
    3303             :               }
    3304          30 :           }
    3305          62 :         return;
    3306             :       }
    3307             : 
    3308             :   /* Now look for a namespace shadow.  */
    3309         338 :   old = find_namespace_value (current_namespace, DECL_NAME (decl));
    3310         338 :   if (old
    3311          88 :       && (VAR_P (old)
    3312          20 :           || (TREE_CODE (old) == TYPE_DECL
    3313          20 :               && (!DECL_ARTIFICIAL (old)
    3314          12 :                   || TREE_CODE (decl) == TYPE_DECL)))
    3315          80 :       && !instantiating_current_function_p ()
    3316         394 :       && !warning_suppressed_p (decl, OPT_Wshadow))
    3317             :     /* XXX shadow warnings in outer-more namespaces */
    3318             :     {
    3319          40 :       auto_diagnostic_group d;
    3320          40 :       if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
    3321             :                       "declaration of %qD shadows a global declaration",
    3322             :                       decl))
    3323             :         {
    3324          40 :           inform_shadowed (old);
    3325          40 :           suppress_warning (decl, OPT_Wshadow);
    3326             :         }
    3327          40 :       return;
    3328          40 :     }
    3329             : 
    3330             :   return;
    3331             : }
    3332             : 
    3333             : /* DECL is being pushed inside function CTX.  Set its context, if
    3334             :    needed.  */
    3335             : 
    3336             : static void
    3337   236963852 : set_decl_context_in_fn (tree ctx, tree decl)
    3338             : {
    3339   236963852 :   if (TREE_CODE (decl) == FUNCTION_DECL
    3340   236963852 :       || (VAR_P (decl) && DECL_EXTERNAL (decl)))
    3341             :     /* Make sure local externs are marked as such.  OMP UDRs really
    3342             :        are nested functions.  */
    3343       25794 :     gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
    3344             :                          && (DECL_NAMESPACE_SCOPE_P (decl)
    3345             :                              || (TREE_CODE (decl) == FUNCTION_DECL
    3346             :                                  && DECL_OMP_DECLARE_REDUCTION_P (decl))));
    3347             : 
    3348   236963852 :   if (!DECL_CONTEXT (decl)
    3349             :       /* When parsing the parameter list of a function declarator,
    3350             :          don't set DECL_CONTEXT to an enclosing function.  */
    3351   237366208 :       && !(TREE_CODE (decl) == PARM_DECL
    3352      402356 :            && parsing_function_declarator ()))
    3353    53484249 :     DECL_CONTEXT (decl) = ctx;
    3354   236963852 : }
    3355             : 
    3356             : /* DECL is a local extern decl.  Find or create the namespace-scope
    3357             :    decl that it aliases.  Also, determines the linkage of DECL.  */
    3358             : 
    3359             : void
    3360       25457 : push_local_extern_decl_alias (tree decl)
    3361             : {
    3362       25457 :   if (dependent_type_p (TREE_TYPE (decl))
    3363       25457 :       || (processing_template_decl
    3364        8736 :           && VAR_P (decl)
    3365          50 :           && CP_DECL_THREAD_LOCAL_P (decl)))
    3366             :     return;
    3367             :   /* EH specs were not part of the function type prior to c++17, but
    3368             :      we still can't go pushing dependent eh specs into the namespace.  */
    3369       25366 :   if (cxx_dialect < cxx17
    3370        2919 :       && TREE_CODE (decl) == FUNCTION_DECL
    3371       27708 :       && (value_dependent_expression_p
    3372        2342 :           (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
    3373             :     return;
    3374             : 
    3375       25365 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
    3376             :                        || !DECL_TEMPLATE_INFO (decl));
    3377       49480 :   if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
    3378             :     /* We're instantiating a non-dependent local decl, it already
    3379             :        knows the alias.  */
    3380             :     return;
    3381             : 
    3382       25220 :   tree alias = NULL_TREE;
    3383             : 
    3384       25220 :   if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
    3385             :     /* Do not let a VLA creep into a namespace.  Diagnostic will be
    3386             :        emitted in layout_var_decl later.  */
    3387           4 :     alias = error_mark_node;
    3388             :   else
    3389             :     {
    3390             :       /* First look for a decl that matches.  */
    3391       25216 :       tree ns = CP_DECL_CONTEXT (decl);
    3392       25216 :       tree binding = find_namespace_value (ns, DECL_NAME (decl));
    3393             : 
    3394       25216 :       if (binding && TREE_CODE (binding) != TREE_LIST)
    3395         626 :         for (ovl_iterator iter (binding); iter; ++iter)
    3396         424 :           if (decls_match (decl, *iter, /*record_versions*/false))
    3397             :             {
    3398         304 :               alias = *iter;
    3399         304 :               break;
    3400             :             }
    3401             : 
    3402         386 :       if (!alias)
    3403             :         {
    3404             :           /* No existing namespace-scope decl.  Make one.  */
    3405       24912 :           alias = copy_decl (decl);
    3406       24912 :           if (TREE_CODE (alias) == FUNCTION_DECL)
    3407             :             {
    3408             :               /* Recontextualize the parms.  */
    3409       23918 :               for (tree *chain = &DECL_ARGUMENTS (alias);
    3410       24447 :                    *chain; chain = &DECL_CHAIN (*chain))
    3411             :                 {
    3412         529 :                   *chain = copy_decl (*chain);
    3413         529 :                   DECL_CONTEXT (*chain) = alias;
    3414             :                 }
    3415             : 
    3416       23918 :               tree type = TREE_TYPE (alias);
    3417       23918 :               for (tree args = TYPE_ARG_TYPES (type);
    3418       48605 :                    args; args = TREE_CHAIN (args))
    3419       24732 :                 if (TREE_PURPOSE (args))
    3420             :                   {
    3421             :                     /* There are default args.  Lose them.  */
    3422          45 :                     tree nargs = NULL_TREE;
    3423          45 :                     tree *chain = &nargs;
    3424          45 :                     for (args = TYPE_ARG_TYPES (type);
    3425         111 :                          args; args = TREE_CHAIN (args))
    3426         111 :                       if (args == void_list_node)
    3427             :                         {
    3428          45 :                           *chain = args;
    3429          45 :                           break;
    3430             :                         }
    3431             :                       else
    3432             :                         {
    3433          66 :                           *chain
    3434          66 :                             = build_tree_list (NULL_TREE, TREE_VALUE (args));
    3435          66 :                           chain = &TREE_CHAIN (*chain);
    3436             :                         }
    3437             : 
    3438          45 :                     tree fn_type = build_function_type (TREE_TYPE (type), nargs);
    3439             : 
    3440          45 :                     fn_type = apply_memfn_quals
    3441          45 :                       (fn_type, type_memfn_quals (type));
    3442             : 
    3443          45 :                     fn_type = build_cp_fntype_variant
    3444          45 :                       (fn_type, type_memfn_rqual (type),
    3445          45 :                        TYPE_RAISES_EXCEPTIONS (type),
    3446          45 :                        TYPE_HAS_LATE_RETURN_TYPE (type));
    3447             : 
    3448          45 :                     TREE_TYPE (alias) = fn_type;
    3449          45 :                     break;
    3450             :                   }
    3451             :             }
    3452             : 
    3453             :           /* This is the real thing.  */
    3454       24912 :           DECL_LOCAL_DECL_P (alias) = false;
    3455             : 
    3456             :           /* Expected default linkage is from the namespace.  */
    3457       24912 :           TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
    3458       24912 :           push_nested_namespace (ns);
    3459       24912 :           alias = pushdecl (alias, /* hiding= */true);
    3460       24912 :           pop_nested_namespace (ns);
    3461       24912 :           if (VAR_P (decl)
    3462         994 :               && CP_DECL_THREAD_LOCAL_P (decl)
    3463       24935 :               && alias != error_mark_node)
    3464          20 :             set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
    3465             : 
    3466             :           /* Adjust visibility.  */
    3467       24912 :           determine_visibility (alias);
    3468             :         }
    3469             :     }
    3470             : 
    3471       25220 :   retrofit_lang_decl (decl);
    3472       49233 :   DECL_LOCAL_DECL_ALIAS (decl) = alias;
    3473             : }
    3474             : 
    3475             : /* If DECL has non-internal linkage, and we have a module vector,
    3476             :    record it in the appropriate slot.  We have already checked for
    3477             :    duplicates.  */
    3478             : 
    3479             : static void
    3480      235306 : maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
    3481             : {
    3482      235306 :   if (TREE_CODE (*slot) != BINDING_VECTOR)
    3483             :     return;
    3484             : 
    3485          39 :   if (!TREE_PUBLIC (CP_DECL_CONTEXT (decl)))
    3486             :     /* Member of internal namespace.  */
    3487             :     return;
    3488             : 
    3489          39 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    3490          39 :   if ((TREE_CODE (not_tmpl) == FUNCTION_DECL
    3491          39 :        || VAR_P (not_tmpl))
    3492          39 :       && DECL_THIS_STATIC (not_tmpl))
    3493             :     /* Internal linkage.  */
    3494             :     return;
    3495             : 
    3496          39 :   bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
    3497          78 :                       && DECL_MODULE_ATTACH_P (not_tmpl));
    3498             :   tree *gslot = get_fixed_binding_slot
    3499          39 :     (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    3500             :      true);
    3501             : 
    3502          39 :   if (!is_attached)
    3503             :     {
    3504          33 :       binding_slot &orig
    3505          33 :         = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
    3506             : 
    3507          33 :       if (!STAT_HACK_P (tree (orig)))
    3508           9 :         orig = stat_hack (tree (orig));
    3509             : 
    3510          33 :       MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
    3511             :     }
    3512             : 
    3513      235345 :   add_mergeable_namespace_entity (gslot, decl);
    3514             : }
    3515             : 
    3516             : /* DECL is being pushed.  Check whether it hides or ambiguates
    3517             :    something seen as an import.  This include decls seen in our own
    3518             :    interface, which is OK.  Also, check for merging a
    3519             :    global/partition decl.  */
    3520             : 
    3521             : static tree
    3522         313 : check_module_override (tree decl, tree mvec, bool hiding,
    3523             :                        tree scope, tree name)
    3524             : {
    3525         313 :   tree match = NULL_TREE;
    3526         313 :   bitmap imports = get_import_bitmap ();
    3527         313 :   binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
    3528         313 :   unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
    3529             : 
    3530         313 :   if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    3531             :     {
    3532         313 :       cluster++;
    3533         313 :       ix--;
    3534             :     }
    3535             : 
    3536         409 :   for (; ix--; cluster++)
    3537         579 :     for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    3538             :       {
    3539             :         /* Are we importing this module?  */
    3540         483 :         if (cluster->indices[jx].span != 1)
    3541          87 :           continue;
    3542         396 :         if (!cluster->indices[jx].base)
    3543         117 :           continue;
    3544         279 :         if (!bitmap_bit_p (imports, cluster->indices[jx].base))
    3545           9 :           continue;
    3546             :         /* Is it loaded? */
    3547         270 :         if (cluster->slots[jx].is_lazy ())
    3548             :           {
    3549           0 :             gcc_assert (cluster->indices[jx].span == 1);
    3550           0 :             lazy_load_binding (cluster->indices[jx].base,
    3551             :                                scope, name, &cluster->slots[jx]);
    3552             :           }
    3553         270 :         tree bind = cluster->slots[jx];
    3554         270 :         if (!bind)
    3555             :           /* Errors could cause there to be nothing.  */
    3556           0 :           continue;
    3557             : 
    3558         270 :         if (STAT_HACK_P (bind))
    3559             :           /* We do not have to check STAT_TYPE here, the xref_tag
    3560             :              machinery deals with that problem. */
    3561         249 :           bind = STAT_VISIBLE (bind);
    3562             : 
    3563         414 :         for (ovl_iterator iter (bind); iter; ++iter)
    3564         252 :           if (!iter.using_p ())
    3565             :             {
    3566         252 :               match = duplicate_decls (decl, *iter, hiding);
    3567         252 :               if (match)
    3568         180 :                 goto matched;
    3569             :             }
    3570             :       }
    3571             : 
    3572         133 :   if (TREE_PUBLIC (scope) && TREE_PUBLIC (STRIP_TEMPLATE (decl))
    3573             :       /* Namespaces are dealt with specially in
    3574             :          make_namespace_finish.  */
    3575         248 :       && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
    3576             :     {
    3577             :       /* Look in the appropriate mergeable decl slot.  */
    3578         106 :       tree mergeable = NULL_TREE;
    3579         106 :       if (named_module_p ())
    3580          12 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
    3581             :                                            / BINDING_VECTOR_SLOTS_PER_CLUSTER)
    3582           6 :           .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
    3583             :       else
    3584         100 :         mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
    3585             : 
    3586         532 :       for (ovl_iterator iter (mergeable); iter; ++iter)
    3587             :         {
    3588         249 :           match = duplicate_decls (decl, *iter, hiding);
    3589         249 :           if (match)
    3590          36 :             goto matched;
    3591             :         }
    3592             :     }
    3593             : 
    3594             :   return NULL_TREE;
    3595             : 
    3596         216 :  matched:
    3597         216 :   if (match != error_mark_node)
    3598             :     {
    3599         192 :       if (named_module_p ())
    3600         108 :         BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
    3601             :       else
    3602          84 :         BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
    3603             :     }
    3604             : 
    3605             :   return match;
    3606             : 
    3607             :   
    3608             : }
    3609             : 
    3610             : /* Record DECL as belonging to the current lexical scope.  Check for
    3611             :    errors (such as an incompatible declaration for the same name
    3612             :    already seen in the same scope).
    3613             : 
    3614             :    The new binding is hidden if HIDING is true (an anticipated builtin
    3615             :    or hidden friend).
    3616             : 
    3617             :    Returns either DECL or an old decl for the same name.  If an old
    3618             :    decl is returned, it may have been smashed to agree with what DECL
    3619             :    says.  */
    3620             : 
    3621             : tree
    3622   853137741 : pushdecl (tree decl, bool hiding)
    3623             : {
    3624   853137741 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    3625             : 
    3626   853137741 :   if (decl == error_mark_node)
    3627             :     return error_mark_node;
    3628             : 
    3629   853137733 :   if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
    3630   236963852 :     set_decl_context_in_fn (current_function_decl, decl);
    3631             : 
    3632             :   /* The binding level we will be pushing into.  During local class
    3633             :      pushing, we want to push to the containing scope.  */
    3634   853137733 :   cp_binding_level *level = current_binding_level;
    3635   853137733 :   while (level->kind == sk_class
    3636   853137808 :          || level->kind == sk_cleanup)
    3637          75 :     level = level->level_chain;
    3638             : 
    3639             :   /* An anonymous namespace has a NULL DECL_NAME, but we still want to
    3640             :      insert it.  Other NULL-named decls, not so much.  */
    3641   853137733 :   tree name = DECL_NAME (decl);
    3642  1688828878 :   if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
    3643             :     {
    3644   834898131 :       cxx_binding *binding = NULL; /* Local scope binding.  */
    3645   834898131 :       tree ns = NULL_TREE; /* Searched namespace.  */
    3646   834898131 :       tree *slot = NULL; /* Binding slot in namespace.  */
    3647   834898131 :       tree *mslot = NULL; /* Current module slot in namespace.  */
    3648   834898131 :       tree old = NULL_TREE;
    3649             : 
    3650   834898131 :       if (level->kind == sk_namespace)
    3651             :         {
    3652             :           /* We look in the decl's namespace for an existing
    3653             :              declaration, even though we push into the current
    3654             :              namespace.  */
    3655   601271490 :           ns = (DECL_NAMESPACE_SCOPE_P (decl)
    3656   601271490 :                 ? CP_DECL_CONTEXT (decl) : current_namespace);
    3657             :           /* Create the binding, if this is current namespace, because
    3658             :              that's where we'll be pushing anyway.  */
    3659   300635745 :           slot = find_namespace_slot (ns, name, ns == current_namespace);
    3660   300635745 :           if (slot)
    3661             :             {
    3662   601271306 :               mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
    3663   300635653 :                                               ns == current_namespace);
    3664   300635653 :               old = MAYBE_STAT_DECL (*mslot);
    3665             :             }
    3666             :         }
    3667             :       else
    3668             :         {
    3669   534262386 :           binding = find_local_binding (level, name);
    3670   534262386 :           if (binding)
    3671        3076 :             old = binding->value;
    3672             :         }
    3673             : 
    3674   834898131 :       if (old == error_mark_node)
    3675       89260 :         old = NULL_TREE;
    3676             : 
    3677  1285582125 :       for (ovl_iterator iter (old); iter; ++iter)
    3678   231136192 :         if (iter.using_p ())
    3679             :           ; /* Ignore using decls here.  */
    3680   228965168 :         else if (iter.hidden_p ()
    3681    39000388 :                  && TREE_CODE (*iter) == FUNCTION_DECL
    3682    25174704 :                  && DECL_LANG_SPECIFIC (*iter)
    3683   254139872 :                  && DECL_MODULE_IMPORT_P (*iter))
    3684             :           ; /* An undeclared builtin imported from elsewhere.  */
    3685   457930330 :         else if (tree match
    3686   228965165 :                  = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
    3687             :           {
    3688     5794195 :             if (match == error_mark_node)
    3689             :               ;
    3690     5793303 :             else if (TREE_CODE (match) == TYPE_DECL)
    3691        6551 :               gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
    3692             :                                    == (level->kind == sk_namespace
    3693             :                                        ? NULL_TREE : TREE_TYPE (match)));
    3694     5786752 :             else if (iter.hidden_p () && !hiding)
    3695             :               {
    3696             :                 /* Unhiding a previously hidden decl.  */
    3697     2765865 :                 tree head = iter.reveal_node (old);
    3698     2765865 :                 if (head != old)
    3699             :                   {
    3700       13934 :                     gcc_checking_assert (ns);
    3701       13934 :                     if (STAT_HACK_P (*slot))
    3702           0 :                       STAT_DECL (*slot) = head;
    3703             :                     else
    3704       13934 :                       *slot = head;
    3705             :                   }
    3706     2765865 :                 if (DECL_EXTERN_C_P (match))
    3707             :                   /* We need to check and register the decl now.  */
    3708     2592530 :                   check_extern_c_conflict (match);
    3709             :               }
    3710     3020887 :             else if (slot && !hiding
    3711     3020887 :                      && STAT_HACK_P (*slot) && STAT_DECL_HIDDEN_P (*slot))
    3712             :               {
    3713             :                 /* Unhide the non-function.  */
    3714          24 :                 gcc_checking_assert (old == match);
    3715          24 :                 if (!STAT_TYPE (*slot))
    3716          24 :                   *slot = match;
    3717             :                 else
    3718           0 :                   STAT_DECL (*slot) = match;
    3719             :               }
    3720     5794195 :             return match;
    3721             :           }
    3722             : 
    3723             :       /* Check for redeclaring an import.  */
    3724   829103936 :       if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
    3725         626 :         if (tree match
    3726         313 :             = check_module_override (decl, *slot, hiding, ns, name))
    3727             :           {
    3728         216 :             if (match == error_mark_node)
    3729             :               return match;
    3730             : 
    3731             :             /* We found a decl in an interface, push it into this
    3732             :                binding.  */
    3733         192 :             decl = update_binding (NULL, binding, mslot, old,
    3734             :                                    match, hiding);
    3735             : 
    3736         192 :             return decl;
    3737             :           }
    3738             : 
    3739             :       /* We are pushing a new decl.  */
    3740             : 
    3741             :       /* Skip a hidden builtin we failed to match already.  There can
    3742             :          only be one.  */
    3743   829103720 :       if (old && anticipated_builtin_p (old))
    3744      621102 :         old = OVL_CHAIN (old);
    3745             : 
    3746   829103720 :       check_template_shadow (decl);
    3747             : 
    3748   829103720 :       if (DECL_DECLARES_FUNCTION_P (decl))
    3749             :         {
    3750   272682107 :           check_default_args (decl);
    3751             : 
    3752   272682107 :           if (hiding)
    3753             :             {
    3754    65921986 :               if (level->kind != sk_namespace)
    3755             :                 {
    3756             :                   /* In a local class, a friend function declaration must
    3757             :                      find a matching decl in the innermost non-class scope.
    3758             :                      [class.friend/11] */
    3759          12 :                   error_at (DECL_SOURCE_LOCATION (decl),
    3760             :                             "friend declaration %qD in local class without "
    3761             :                             "prior local declaration", decl);
    3762             :                   /* Don't attempt to push it.  */
    3763          12 :                   return error_mark_node;
    3764             :                 }
    3765             :             }
    3766             :         }
    3767             : 
    3768   829103708 :       if (level->kind != sk_namespace)
    3769             :         {
    3770   534259529 :           check_local_shadow (decl);
    3771             : 
    3772   534259529 :           if (TREE_CODE (decl) == NAMESPACE_DECL)
    3773             :             /* A local namespace alias.  */
    3774       27814 :             set_identifier_type_value_with_scope (name, NULL_TREE, level);
    3775             : 
    3776   534259529 :           if (!binding)
    3777   534259302 :             binding = create_local_binding (level, name);
    3778             :         }
    3779   294844179 :       else if (!slot)
    3780             :         {
    3781          92 :           ns = current_namespace;
    3782          92 :           slot = find_namespace_slot (ns, name, true);
    3783          92 :           mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
    3784             :           /* Update OLD to reflect the namespace we're going to be
    3785             :              pushing into.  */
    3786          92 :           old = MAYBE_STAT_DECL (*mslot);
    3787             :         }
    3788             : 
    3789   829103708 :       old = update_binding (level, binding, mslot, old, decl, hiding);
    3790             : 
    3791   829103708 :       if (old != decl)
    3792             :         /* An existing decl matched, use it.  */
    3793             :         decl = old;
    3794             :       else
    3795             :         {
    3796   829103690 :           if (TREE_CODE (decl) == TYPE_DECL)
    3797             :             {
    3798   144413162 :               tree type = TREE_TYPE (decl);
    3799             : 
    3800   144413162 :               if (type != error_mark_node)
    3801             :                 {
    3802   144413093 :                   if (TYPE_NAME (type) != decl)
    3803    14130937 :                     set_underlying_type (decl);
    3804             : 
    3805   144413093 :                   set_identifier_type_value_with_scope (name, decl, level);
    3806             : 
    3807   144413093 :                   if (level->kind != sk_namespace
    3808   144413093 :                       && !instantiating_current_function_p ())
    3809             :                     /* This is a locally defined typedef in a function that
    3810             :                        is not a template instantation, record it to implement
    3811             :                        -Wunused-local-typedefs.  */
    3812   135014591 :                     record_locally_defined_typedef (decl);
    3813             :                 }
    3814             :             }
    3815   684690528 :           else if (VAR_OR_FUNCTION_DECL_P (decl))
    3816             :             {
    3817   303543715 :               if (DECL_EXTERN_C_P (decl))
    3818   242304095 :                 check_extern_c_conflict (decl);
    3819             : 
    3820   303543715 :               if (!DECL_LOCAL_DECL_P (decl)
    3821   303543715 :                   && VAR_P (decl))
    3822    48045963 :                 maybe_register_incomplete_var (decl);
    3823             : 
    3824   303543715 :               if (DECL_LOCAL_DECL_P (decl)
    3825   303543715 :                   && NAMESPACE_SCOPE_P (decl))
    3826       25449 :                 push_local_extern_decl_alias (decl);
    3827             :             }
    3828             : 
    3829   829103690 :           if (level->kind == sk_namespace
    3830   294844168 :               && TREE_PUBLIC (level->this_entity)
    3831  1123943816 :               && module_p ())
    3832      235306 :             maybe_record_mergeable_decl (slot, name, decl);
    3833             :         }
    3834             :     }
    3835             :   else
    3836    18239602 :     add_decl_to_level (level, decl);
    3837             : 
    3838             :   return decl;
    3839   853137741 : }
    3840             : 
    3841             : /* A mergeable entity is being loaded into namespace NS slot NAME.
    3842             :    Create and return the appropriate vector slot for that.  Either a
    3843             :    GMF slot or a module-specific one.  */
    3844             : 
    3845             : tree *
    3846      231263 : mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
    3847             : {
    3848      231263 :   tree *mslot = find_namespace_slot (ns, name, true);
    3849      231263 :   tree *vslot = get_fixed_binding_slot
    3850      462108 :     (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
    3851             :      true);
    3852             : 
    3853      231263 :   gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
    3854      231263 :   *vec = *mslot;
    3855             : 
    3856      231263 :   return vslot;
    3857             : }
    3858             : 
    3859             : /* DECL is a new mergeable namespace-scope decl.  Add it to the
    3860             :    mergeable entities on GSLOT.  */
    3861             : 
    3862             : void
    3863       85951 : add_mergeable_namespace_entity (tree *gslot, tree decl)
    3864             : {
    3865       85951 :   *gslot = ovl_make (decl, *gslot);
    3866          39 : }
    3867             : 
    3868             : /* A mergeable entity of KLASS called NAME is being loaded.  Return
    3869             :    the set of things it could be.  All such non-as_base classes have
    3870             :    been given a member vec.  */
    3871             : 
    3872             : tree
    3873      259705 : lookup_class_binding (tree klass, tree name)
    3874             : {
    3875      259705 :   tree found = NULL_TREE;
    3876             : 
    3877      259705 :   if (!COMPLETE_TYPE_P (klass))
    3878             :     ;
    3879      259705 :   else if (TYPE_LANG_SPECIFIC (klass))
    3880             :     {
    3881      257177 :       vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
    3882             : 
    3883      257177 :       found = member_vec_binary_search (member_vec, name);
    3884      257177 :       if (!found)
    3885             :         ;
    3886      256667 :       else if (STAT_HACK_P (found))
    3887             :         /* Rearrange the stat hack so that we don't need to expose that
    3888             :            internal detail.  */
    3889           6 :         found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
    3890      256661 :       else if (IDENTIFIER_CONV_OP_P (name))
    3891             :         {
    3892        1043 :           gcc_checking_assert (name == conv_op_identifier);
    3893        1043 :           found = OVL_CHAIN (found);
    3894             :         }
    3895             :     }
    3896             :   else
    3897             :     {
    3898        2528 :       gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
    3899             :                            || TYPE_PTRMEMFUNC_P (klass));
    3900        2528 :       found = fields_linear_search (klass, name, false);
    3901             :     }
    3902             : 
    3903      259705 :   return found;
    3904             : }
    3905             : 
    3906             : /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
    3907             :    for all decls of the current module.  When partitions are involved,
    3908             :    decls might be mentioned more than once.   Return the accumulation of
    3909             :    CALLBACK results.  */
    3910             : 
    3911             : unsigned
    3912     4349359 : walk_module_binding (tree binding, bitmap partitions,
    3913             :                      bool (*callback) (tree decl, WMB_Flags, void *data),
    3914             :                      void *data)
    3915             : {
    3916             :   // FIXME: We don't quite deal with using decls naming stat hack
    3917             :   // type.  Also using decls exporting something from the same scope.
    3918     4349359 :   tree current = binding;
    3919     4349359 :   unsigned count = 0;
    3920             : 
    3921     4349359 :   if (TREE_CODE (binding) == BINDING_VECTOR)
    3922       15720 :     current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
    3923             : 
    3924     4350461 :   bool decl_hidden = false;
    3925     4349359 :   if (tree type = MAYBE_STAT_TYPE (current))
    3926             :     {
    3927          77 :       WMB_Flags flags = WMB_None;
    3928          77 :       if (STAT_TYPE_HIDDEN_P (current))
    3929           0 :         flags = WMB_Flags (flags | WMB_Hidden);
    3930          77 :       count += callback (type, flags, data);
    3931          77 :       decl_hidden = STAT_DECL_HIDDEN_P (current);
    3932             :     }
    3933             : 
    3934    13096187 :   for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
    3935             :     {
    3936     4397469 :       if (iter.hidden_p ())
    3937             :         decl_hidden = true;
    3938     4397469 :       if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
    3939             :         {
    3940     3314550 :           WMB_Flags flags = WMB_None;
    3941     3314550 :           if (decl_hidden)
    3942        5758 :             flags = WMB_Flags (flags | WMB_Hidden);
    3943     3314550 :           if (iter.using_p ())
    3944             :             {
    3945       16043 :               flags = WMB_Flags (flags | WMB_Using);
    3946       16043 :               if (iter.exporting_p ())
    3947       12534 :                 flags = WMB_Flags (flags | WMB_Export);
    3948             :             }
    3949     3314550 :           count += callback (*iter, flags, data);
    3950             :         }
    3951     4397469 :       decl_hidden = false;
    3952             :     }
    3953             : 
    3954     4349359 :   if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
    3955             :     {
    3956             :       /* Process partition slots.  */
    3957          99 :       binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    3958          99 :       unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    3959          99 :       if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    3960             :         {
    3961          99 :           ix--;
    3962          99 :           cluster++;
    3963             :         }
    3964             : 
    3965          99 :       bool maybe_dups = BINDING_VECTOR_PARTITION_DUPS_P (binding);
    3966             : 
    3967         207 :       for (; ix--; cluster++)
    3968         324 :         for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    3969         216 :           if (!cluster->slots[jx].is_lazy ())
    3970         216 :             if (tree bind = cluster->slots[jx])
    3971             :               {
    3972         123 :                 if (TREE_CODE (bind) == NAMESPACE_DECL
    3973         123 :                     && !DECL_NAMESPACE_ALIAS (bind))
    3974             :                   {
    3975           9 :                     if (unsigned base = cluster->indices[jx].base)
    3976           9 :                       if (unsigned span = cluster->indices[jx].span)
    3977           9 :                         do
    3978           9 :                           if (bitmap_bit_p (partitions, base))
    3979           9 :                             goto found;
    3980           0 :                         while (++base, --span);
    3981             :                     /* Not a partition's namespace.  */
    3982           0 :                     continue;
    3983           9 :                   found:
    3984             : 
    3985           9 :                     WMB_Flags flags = WMB_None;
    3986           9 :                     if (maybe_dups)
    3987           0 :                       flags = WMB_Flags (flags | WMB_Dups);
    3988           9 :                     count += callback (bind, flags, data);
    3989           0 :                   }
    3990         114 :                 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
    3991             :                   {
    3992          60 :                     if (tree btype = STAT_TYPE (bind))
    3993             :                       {
    3994           0 :                         WMB_Flags flags = WMB_None;
    3995           0 :                         if (maybe_dups)
    3996           0 :                           flags = WMB_Flags (flags | WMB_Dups);
    3997           0 :                         if (STAT_TYPE_HIDDEN_P (bind))
    3998           0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    3999             : 
    4000           0 :                         count += callback (btype, flags, data);
    4001             :                       }
    4002          60 :                     bool hidden = STAT_DECL_HIDDEN_P (bind);
    4003          60 :                     for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
    4004         120 :                          iter; ++iter)
    4005             :                       {
    4006          60 :                         if (iter.hidden_p ())
    4007             :                           hidden = true;
    4008          60 :                         gcc_checking_assert
    4009             :                           (!(hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
    4010             : 
    4011          60 :                         WMB_Flags flags = WMB_None;
    4012          60 :                         if (maybe_dups)
    4013          18 :                           flags = WMB_Flags (flags | WMB_Dups);
    4014          60 :                         if (decl_hidden)
    4015           0 :                           flags = WMB_Flags (flags | WMB_Hidden);
    4016          60 :                         if (iter.using_p ())
    4017             :                           {
    4018           0 :                             flags = WMB_Flags (flags | WMB_Using);
    4019           0 :                             if (iter.exporting_p ())
    4020           0 :                               flags = WMB_Flags (flags | WMB_Export);
    4021             :                           }
    4022          60 :                         count += callback (*iter, flags, data);
    4023          60 :                         hidden = false;
    4024             :                       }
    4025             :                   }
    4026             :               }
    4027             :     }
    4028             : 
    4029     4349359 :   return count;
    4030             : }
    4031             : 
    4032             : /* Imported module MOD has a binding to NS::NAME, stored in section
    4033             :    SNUM.  */
    4034             : 
    4035             : bool
    4036      236013 : import_module_binding  (tree ns, tree name, unsigned mod, unsigned snum)
    4037             : {
    4038      236013 :   tree *slot = find_namespace_slot (ns, name, true);
    4039      236013 :   binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
    4040             : 
    4041      236013 :   if (mslot->is_lazy () || *mslot)
    4042             :     /* Oops, something was already there.  */
    4043             :     return false;
    4044             : 
    4045      236013 :   mslot->set_lazy (snum);
    4046      236013 :   return true;
    4047             : }
    4048             : 
    4049             : /* An import of MODULE is binding NS::NAME.  There should be no
    4050             :    existing binding for >= MODULE.  MOD_GLOB indicates whether MODULE
    4051             :    is a header_unit (-1) or part of the current module (+1).  VALUE
    4052             :    and TYPE are the value and type bindings. VISIBLE are the value
    4053             :    bindings being exported.  */
    4054             : 
    4055             : bool
    4056      192945 : set_module_binding (tree ns, tree name, unsigned mod, int mod_glob,
    4057             :                     tree value, tree type, tree visible)
    4058             : {
    4059      192945 :   if (!value)
    4060             :     /* Bogus BMIs could give rise to nothing to bind.  */
    4061             :     return false;
    4062             : 
    4063      192945 :   gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
    4064             :               || DECL_NAMESPACE_ALIAS (value));
    4065      192945 :   gcc_checking_assert (mod);
    4066             : 
    4067      192945 :   tree *slot = find_namespace_slot (ns, name, true);
    4068      192945 :   binding_slot *mslot = search_imported_binding_slot (slot, mod);
    4069             : 
    4070      192945 :   if (!mslot || !mslot->is_lazy ())
    4071             :     /* Again, bogus BMI could give find to missing or already loaded slot.  */
    4072             :     return false;
    4073             : 
    4074      192945 :   tree bind = value;
    4075      192945 :   if (type || visible != bind || mod_glob)
    4076             :     {
    4077      191956 :       bind = stat_hack (bind, type);
    4078      191956 :       STAT_VISIBLE (bind) = visible;
    4079         412 :       if ((mod_glob > 0 && TREE_PUBLIC (ns))
    4080      191956 :           || (type && DECL_MODULE_EXPORT_P (type)))
    4081         446 :         STAT_TYPE_VISIBLE_P (bind) = true;
    4082             :     }
    4083             : 
    4084             :   /* Note if this is this-module or global binding.  */
    4085      191956 :   if (mod_glob > 0)
    4086         412 :     MODULE_BINDING_PARTITION_P (bind) = true;
    4087      192533 :   else if (mod_glob < 0)
    4088      191423 :     MODULE_BINDING_GLOBAL_P (bind) = true;
    4089             : 
    4090      192945 :   *mslot = bind;
    4091             : 
    4092      192945 :   return true;
    4093             : }
    4094             : 
    4095             : void
    4096       87028 : add_module_namespace_decl (tree ns, tree decl)
    4097             : {
    4098       87028 :   gcc_assert (!DECL_CHAIN (decl));
    4099       87028 :   gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
    4100             :                          && DECL_LOCAL_DECL_P (decl)));
    4101       87028 :   if (CHECKING_P)
    4102             :     /* Expensive already-there? check.  */
    4103   151566874 :     for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
    4104   151479846 :          probe = DECL_CHAIN (probe))
    4105   151479846 :       gcc_assert (decl != probe);
    4106             : 
    4107       87028 :   add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
    4108             : 
    4109       87028 :   if (VAR_P (decl))
    4110        1782 :     maybe_register_incomplete_var (decl);
    4111             : 
    4112       87028 :   if (VAR_OR_FUNCTION_DECL_P (decl)
    4113       87028 :       && DECL_EXTERN_C_P (decl))
    4114       22510 :     check_extern_c_conflict (decl);
    4115       87028 : }
    4116             : 
    4117             : /* Enter DECL into the symbol table, if that's appropriate.  Returns
    4118             :    DECL, or a modified version thereof.  */
    4119             : 
    4120             : tree
    4121    86288764 : maybe_push_decl (tree decl)
    4122             : {
    4123    86288764 :   tree type = TREE_TYPE (decl);
    4124             : 
    4125             :   /* Add this decl to the current binding level, but not if it comes
    4126             :      from another scope, e.g. a static member variable.  TEM may equal
    4127             :      DECL or it may be a previous decl of the same name.  */
    4128    86288764 :   if (decl == error_mark_node
    4129    86288758 :       || (TREE_CODE (decl) != PARM_DECL
    4130    86288758 :           && DECL_CONTEXT (decl) != NULL_TREE
    4131             :           /* Definitions of namespace members outside their namespace are
    4132             :              possible.  */
    4133    33257446 :           && !DECL_NAMESPACE_SCOPE_P (decl))
    4134    85437828 :       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
    4135    85437828 :       || type == unknown_type_node
    4136             :       /* The declaration of a template specialization does not affect
    4137             :          the functions available for overload resolution, so we do not
    4138             :          call pushdecl.  */
    4139   171726592 :       || (TREE_CODE (decl) == FUNCTION_DECL
    4140    25383930 :           && DECL_TEMPLATE_SPECIALIZATION (decl)))
    4141             :     return decl;
    4142             :   else
    4143    85371395 :     return pushdecl (decl);
    4144             : }
    4145             : 
    4146             : /* Bind DECL to ID in the current_binding_level, assumed to be a local
    4147             :    binding level.  If IS_USING is true, DECL got here through a
    4148             :    using-declaration.  */
    4149             : 
    4150             : static void
    4151      340885 : push_local_binding (tree id, tree decl, bool is_using)
    4152             : {
    4153             :   /* Skip over any local classes.  This makes sense if we call
    4154             :      push_local_binding with a friend decl of a local class.  */
    4155      340885 :   cp_binding_level *b = innermost_nonclass_level ();
    4156             : 
    4157      340885 :   gcc_assert (b->kind != sk_namespace);
    4158      340885 :   if (find_local_binding (b, id))
    4159             :     {
    4160             :       /* Supplement the existing binding.  */
    4161          25 :       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
    4162             :         /* It didn't work.  Something else must be bound at this
    4163             :            level.  Do not add DECL to the list of things to pop
    4164             :            later.  */
    4165             :         return;
    4166             :     }
    4167             :   else
    4168             :     /* Create a new binding.  */
    4169      340860 :     push_binding (id, decl, b);
    4170             : 
    4171      340884 :   if (TREE_CODE (decl) == OVERLOAD || is_using)
    4172             :     /* We must put the OVERLOAD or using into a TREE_LIST since we
    4173             :        cannot use the decl's chain itself.  */
    4174      340884 :     decl = build_tree_list (id, decl);
    4175             : 
    4176             :   /* And put DECL on the list of things declared by the current
    4177             :      binding level.  */
    4178      340884 :   add_decl_to_level (b, decl);
    4179             : }
    4180             : 
    4181             : 
    4182             : /* true means unconditionally make a BLOCK for the next level pushed.  */
    4183             : 
    4184             : static bool keep_next_level_flag;
    4185             : 
    4186             : static int binding_depth = 0;
    4187             : 
    4188             : static void
    4189           0 : indent (int depth)
    4190             : {
    4191           0 :   int i;
    4192             : 
    4193           0 :   for (i = 0; i < depth * 2; i++)
    4194           0 :     putc (' ', stderr);
    4195           0 : }
    4196             : 
    4197             : /* Return a string describing the kind of SCOPE we have.  */
    4198             : static const char *
    4199           0 : cp_binding_level_descriptor (cp_binding_level *scope)
    4200             : {
    4201             :   /* The order of this table must match the "scope_kind"
    4202             :      enumerators.  */
    4203           0 :   static const char* scope_kind_names[] = {
    4204             :     "block-scope",
    4205             :     "cleanup-scope",
    4206             :     "try-scope",
    4207             :     "catch-scope",
    4208             :     "for-scope",
    4209             :     "function-parameter-scope",
    4210             :     "class-scope",
    4211             :     "namespace-scope",
    4212             :     "template-parameter-scope",
    4213             :     "template-explicit-spec-scope"
    4214             :   };
    4215           0 :   const scope_kind kind = scope->explicit_spec_p
    4216           0 :     ? sk_template_spec : scope->kind;
    4217             : 
    4218           0 :   return scope_kind_names[kind];
    4219             : }
    4220             : 
    4221             : /* Output a debugging information about SCOPE when performing
    4222             :    ACTION at LINE.  */
    4223             : static void
    4224           0 : cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
    4225             : {
    4226           0 :   const char *desc = cp_binding_level_descriptor (scope);
    4227           0 :   if (scope->this_entity)
    4228           0 :     verbatim ("%s %<%s(%E)%> %p %d", action, desc,
    4229             :               scope->this_entity, (void *) scope, line);
    4230             :   else
    4231           0 :     verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
    4232           0 : }
    4233             : 
    4234             : /* A chain of binding_level structures awaiting reuse.  */
    4235             : 
    4236             : static GTY((deletable)) cp_binding_level *free_binding_level;
    4237             : 
    4238             : /* Insert SCOPE as the innermost binding level.  */
    4239             : 
    4240             : void
    4241   718855075 : push_binding_level (cp_binding_level *scope)
    4242             : {
    4243             :   /* Add it to the front of currently active scopes stack.  */
    4244   718855075 :   scope->level_chain = current_binding_level;
    4245   718855075 :   current_binding_level = scope;
    4246   718855075 :   keep_next_level_flag = false;
    4247             : 
    4248   718855075 :   if (ENABLE_SCOPE_CHECKING)
    4249             :     {
    4250             :       scope->binding_depth = binding_depth;
    4251             :       indent (binding_depth);
    4252             :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    4253             :                               "push");
    4254             :       binding_depth++;
    4255             :     }
    4256   718855075 : }
    4257             : 
    4258             : /* Create a new KIND scope and make it the top of the active scopes stack.
    4259             :    ENTITY is the scope of the associated C++ entity (namespace, class,
    4260             :    function, C++0x enumeration); it is NULL otherwise.  */
    4261             : 
    4262             : cp_binding_level *
    4263   596757912 : begin_scope (scope_kind kind, tree entity)
    4264             : {
    4265   596757912 :   cp_binding_level *scope;
    4266             : 
    4267             :   /* Reuse or create a struct for this binding level.  */
    4268   596757912 :   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
    4269             :     {
    4270   579490110 :       scope = free_binding_level;
    4271   579490110 :       free_binding_level = scope->level_chain;
    4272   579490110 :       memset (scope, 0, sizeof (cp_binding_level));
    4273             :     }
    4274             :   else
    4275    17267802 :     scope = ggc_cleared_alloc<cp_binding_level> ();
    4276             : 
    4277   596757912 :   scope->this_entity = entity;
    4278   596757912 :   scope->more_cleanups_ok = true;
    4279   596757912 :   switch (kind)
    4280             :     {
    4281          89 :     case sk_cleanup:
    4282          89 :       scope->keep = true;
    4283          89 :       break;
    4284             : 
    4285     3556940 :     case sk_template_spec:
    4286     3556940 :       scope->explicit_spec_p = true;
    4287     3556940 :       kind = sk_template_parms;
    4288             :       /* Fall through.  */
    4289   379777532 :     case sk_template_parms:
    4290   379777532 :     case sk_block:
    4291   379777532 :     case sk_try:
    4292   379777532 :     case sk_catch:
    4293   379777532 :     case sk_for:
    4294   379777532 :     case sk_cond:
    4295   379777532 :     case sk_class:
    4296   379777532 :     case sk_scoped_enum:
    4297   379777532 :     case sk_transaction:
    4298   379777532 :     case sk_omp:
    4299   379777532 :     case sk_stmt_expr:
    4300   379777532 :       scope->keep = keep_next_level_flag;
    4301   379777532 :       break;
    4302             : 
    4303   216891031 :     case sk_function_parms:
    4304   216891031 :       scope->keep = keep_next_level_flag;
    4305   216891031 :       break;
    4306             : 
    4307       89260 :     case sk_namespace:
    4308       89260 :       NAMESPACE_LEVEL (entity) = scope;
    4309       89260 :       break;
    4310             : 
    4311           0 :     default:
    4312             :       /* Should not happen.  */
    4313           0 :       gcc_unreachable ();
    4314   596757912 :       break;
    4315             :     }
    4316   596757912 :   scope->kind = kind;
    4317             : 
    4318   596757912 :   push_binding_level (scope);
    4319             : 
    4320   596757912 :   return scope;
    4321             : }
    4322             : 
    4323             : /* We're about to leave current scope.  Pop the top of the stack of
    4324             :    currently active scopes.  Return the enclosing scope, now active.  */
    4325             : 
    4326             : cp_binding_level *
    4327   757200203 : leave_scope (void)
    4328             : {
    4329   757200203 :   cp_binding_level *scope = current_binding_level;
    4330             : 
    4331   757200203 :   if (scope->kind == sk_namespace && class_binding_level)
    4332           0 :     current_binding_level = class_binding_level;
    4333             : 
    4334             :   /* We cannot leave a scope, if there are none left.  */
    4335   757200203 :   if (NAMESPACE_LEVEL (global_namespace))
    4336   757200203 :     gcc_assert (!global_scope_p (scope));
    4337             : 
    4338   757200203 :   if (ENABLE_SCOPE_CHECKING)
    4339             :     {
    4340             :       indent (--binding_depth);
    4341             :       cp_binding_level_debug (scope, LOCATION_LINE (input_location),
    4342             :                               "leave");
    4343             :     }
    4344             : 
    4345             :   /* Move one nesting level up.  */
    4346   757200203 :   current_binding_level = scope->level_chain;
    4347             : 
    4348             :   /* Namespace-scopes are left most probably temporarily, not
    4349             :      completely; they can be reopened later, e.g. in namespace-extension
    4350             :      or any name binding activity that requires us to resume a
    4351             :      namespace.  For classes, we cache some binding levels.  For other
    4352             :      scopes, we just make the structure available for reuse.  */
    4353   757200203 :   if (scope->kind != sk_namespace
    4354   718744041 :       && scope != previous_class_level)
    4355             :     {
    4356   561623282 :       scope->level_chain = free_binding_level;
    4357   561623282 :       gcc_assert (!ENABLE_SCOPE_CHECKING
    4358             :                   || scope->binding_depth == binding_depth);
    4359   561623282 :       free_binding_level = scope;
    4360             :     }
    4361             : 
    4362   757200203 :   if (scope->kind == sk_class)
    4363             :     {
    4364             :       /* Reset DEFINING_CLASS_P to allow for reuse of a
    4365             :          class-defining scope in a non-defining context.  */
    4366   235250557 :       scope->defining_class_p = 0;
    4367             : 
    4368             :       /* Find the innermost enclosing class scope, and reset
    4369             :          CLASS_BINDING_LEVEL appropriately.  */
    4370   235250557 :       class_binding_level = NULL;
    4371   774868668 :       for (scope = current_binding_level; scope; scope = scope->level_chain)
    4372   581845398 :         if (scope->kind == sk_class)
    4373             :           {
    4374    42227287 :             class_binding_level = scope;
    4375    42227287 :             break;
    4376             :           }
    4377             :     }
    4378             : 
    4379   757200203 :   return current_binding_level;
    4380             : }
    4381             : 
    4382             : /* When we exit a toplevel class scope, we save its binding level so
    4383             :    that we can restore it quickly.  Here, we've entered some other
    4384             :    class, so we must invalidate our cache.  */
    4385             : 
    4386             : void
    4387    19085207 : invalidate_class_lookup_cache (void)
    4388             : {
    4389    19085207 :   previous_class_level->level_chain = free_binding_level;
    4390    19085207 :   free_binding_level = previous_class_level;
    4391    19085207 :   previous_class_level = NULL;
    4392    19085207 : }
    4393             : 
    4394             : static void
    4395    38456171 : resume_scope (cp_binding_level* b)
    4396             : {
    4397             :   /* Resuming binding levels is meant only for namespaces,
    4398             :      and those cannot nest into classes.  */
    4399    38456171 :   gcc_assert (!class_binding_level);
    4400             :   /* Also, resuming a non-directly nested namespace is a no-no.  */
    4401    38456171 :   gcc_assert (b->level_chain == current_binding_level);
    4402    38456171 :   current_binding_level = b;
    4403    38456171 :   if (ENABLE_SCOPE_CHECKING)
    4404             :     {
    4405             :       b->binding_depth = binding_depth;
    4406             :       indent (binding_depth);
    4407             :       cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
    4408             :       binding_depth++;
    4409             :     }
    4410    38456171 : }
    4411             : 
    4412             : /* Return the innermost binding level that is not for a class scope.  */
    4413             : 
    4414             : static cp_binding_level *
    4415   972751589 : innermost_nonclass_level (void)
    4416             : {
    4417   972751589 :   cp_binding_level *b;
    4418             : 
    4419   972751589 :   b = current_binding_level;
    4420  1119751896 :   while (b->kind == sk_class)
    4421   147000307 :     b = b->level_chain;
    4422             : 
    4423   972751589 :   return b;
    4424             : }
    4425             : 
    4426             : /* We're defining an object of type TYPE.  If it needs a cleanup, but
    4427             :    we're not allowed to add any more objects with cleanups to the current
    4428             :    scope, create a new binding level.  */
    4429             : 
    4430             : void
    4431     3333495 : maybe_push_cleanup_level (tree type)
    4432             : {
    4433     3333495 :   if (type != error_mark_node
    4434     3333283 :       && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
    4435     3540350 :       && current_binding_level->more_cleanups_ok == 0)
    4436             :     {
    4437          89 :       begin_scope (sk_cleanup, NULL);
    4438          89 :       current_binding_level->statement_list = push_stmt_list ();
    4439             :     }
    4440     3333495 : }
    4441             : 
    4442             : /* Return true if we are in the global binding level.  */
    4443             : 
    4444             : bool
    4445      217207 : global_bindings_p (void)
    4446             : {
    4447      217207 :   return global_scope_p (current_binding_level);
    4448             : }
    4449             : 
    4450             : /* True if we are currently in a toplevel binding level.  This
    4451             :    means either the global binding level or a namespace in a toplevel
    4452             :    binding level.  Since there are no non-toplevel namespace levels,
    4453             :    this really means any namespace or template parameter level.  We
    4454             :    also include a class whose context is toplevel.  */
    4455             : 
    4456             : bool
    4457   957618175 : toplevel_bindings_p (void)
    4458             : {
    4459   957618175 :   cp_binding_level *b = innermost_nonclass_level ();
    4460             : 
    4461   957618175 :   return b->kind == sk_namespace || b->kind == sk_template_parms;
    4462             : }
    4463             : 
    4464             : /* True if this is a namespace scope, or if we are defining a class
    4465             :    which is itself at namespace scope, or whose enclosing class is
    4466             :    such a class, etc.  */
    4467             : 
    4468             : bool
    4469    14790573 : namespace_bindings_p (void)
    4470             : {
    4471    14790573 :   cp_binding_level *b = innermost_nonclass_level ();
    4472             : 
    4473           0 :   return b->kind == sk_namespace;
    4474             : }
    4475             : 
    4476             : /* True if the innermost non-class scope is a block scope.  */
    4477             : 
    4478             : bool
    4479        1956 : local_bindings_p (void)
    4480             : {
    4481        1956 :   cp_binding_level *b = innermost_nonclass_level ();
    4482        1956 :   return b->kind < sk_function_parms || b->kind == sk_omp;
    4483             : }
    4484             : 
    4485             : /* True if the current level needs to have a BLOCK made.  */
    4486             : 
    4487             : bool
    4488   184847106 : kept_level_p (void)
    4489             : {
    4490   184847106 :   return (current_binding_level->blocks != NULL_TREE
    4491   165967635 :           || current_binding_level->keep
    4492   161702448 :           || current_binding_level->kind == sk_cleanup
    4493   161702448 :           || current_binding_level->names != NULL_TREE
    4494   327452638 :           || current_binding_level->using_directives);
    4495             : }
    4496             : 
    4497             : /* Returns the kind of the innermost scope.  */
    4498             : 
    4499             : scope_kind
    4500  1661105627 : innermost_scope_kind (void)
    4501             : {
    4502  1661105627 :   return current_binding_level->kind;
    4503             : }
    4504             : 
    4505             : /* Returns true if this scope was created to store template parameters.  */
    4506             : 
    4507             : bool
    4508   378256189 : template_parm_scope_p (void)
    4509             : {
    4510   378256189 :   return innermost_scope_kind () == sk_template_parms;
    4511             : }
    4512             : 
    4513             : /* If KEEP is true, make a BLOCK node for the next binding level,
    4514             :    unconditionally.  Otherwise, use the normal logic to decide whether
    4515             :    or not to create a BLOCK.  */
    4516             : 
    4517             : void
    4518     6345779 : keep_next_level (bool keep)
    4519             : {
    4520     6345779 :   keep_next_level_flag = keep;
    4521     6345779 : }
    4522             : 
    4523             : /* Return the list of declarations of the current local scope.  */
    4524             : 
    4525             : tree
    4526   217593328 : get_local_decls (void)
    4527             : {
    4528   217593328 :   gcc_assert (current_binding_level->kind != sk_namespace
    4529             :               && current_binding_level->kind != sk_class);
    4530   217593328 :   return current_binding_level->names;
    4531             : }
    4532             : 
    4533             : /* Return how many function prototypes we are currently nested inside.  */
    4534             : 
    4535             : int
    4536   179251632 : function_parm_depth (void)
    4537             : {
    4538   179251632 :   int level = 0;
    4539   179251632 :   cp_binding_level *b;
    4540             : 
    4541   179251632 :   for (b = current_binding_level;
    4542   359032707 :        b->kind == sk_function_parms;
    4543   179781075 :        b = b->level_chain)
    4544   179781075 :     ++level;
    4545             : 
    4546   179251632 :   return level;
    4547             : }
    4548             : 
    4549             : /* For debugging.  */
    4550             : static int no_print_functions = 0;
    4551             : static int no_print_builtins = 0;
    4552             : 
    4553             : static void
    4554           0 : print_binding_level (cp_binding_level* lvl)
    4555             : {
    4556           0 :   tree t;
    4557           0 :   int i = 0, len;
    4558           0 :   if (lvl->this_entity)
    4559           0 :     print_node_brief (stderr, "entity=", lvl->this_entity, 1);
    4560           0 :   fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
    4561           0 :   if (lvl->more_cleanups_ok)
    4562           0 :     fprintf (stderr, " more-cleanups-ok");
    4563           0 :   if (lvl->have_cleanups)
    4564           0 :     fprintf (stderr, " have-cleanups");
    4565           0 :   fprintf (stderr, "\n");
    4566           0 :   if (lvl->names)
    4567             :     {
    4568           0 :       fprintf (stderr, " names:\t");
    4569             :       /* We can probably fit 3 names to a line?  */
    4570           0 :       for (t = lvl->names; t; t = TREE_CHAIN (t))
    4571             :         {
    4572           0 :           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
    4573           0 :             continue;
    4574           0 :           if (no_print_builtins
    4575           0 :               && (TREE_CODE (t) == TYPE_DECL)
    4576           0 :               && DECL_IS_UNDECLARED_BUILTIN (t))
    4577           0 :             continue;
    4578             : 
    4579             :           /* Function decls tend to have longer names.  */
    4580           0 :           if (TREE_CODE (t) == FUNCTION_DECL)
    4581             :             len = 3;
    4582             :           else
    4583           0 :             len = 2;
    4584           0 :           i += len;
    4585           0 :           if (i > 6)
    4586             :             {
    4587           0 :               fprintf (stderr, "\n\t");
    4588           0 :               i = len;
    4589             :             }
    4590           0 :           print_node_brief (stderr, "", t, 0);
    4591           0 :           if (t == error_mark_node)
    4592             :             break;
    4593             :         }
    4594           0 :       if (i)
    4595           0 :         fprintf (stderr, "\n");
    4596             :     }
    4597           0 :   if (vec_safe_length (lvl->class_shadowed))
    4598             :     {
    4599           0 :       size_t i;
    4600           0 :       cp_class_binding *b;
    4601           0 :       fprintf (stderr, " class-shadowed:");
    4602           0 :       FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
    4603           0 :         fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
    4604           0 :       fprintf (stderr, "\n");
    4605             :     }
    4606           0 :   if (lvl->type_shadowed)
    4607             :     {
    4608           0 :       fprintf (stderr, " type-shadowed:");
    4609           0 :       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
    4610             :         {
    4611           0 :           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
    4612             :         }
    4613           0 :       fprintf (stderr, "\n");
    4614             :     }
    4615           0 : }
    4616             : 
    4617             : DEBUG_FUNCTION void
    4618           0 : debug (cp_binding_level &ref)
    4619             : {
    4620           0 :   print_binding_level (&ref);
    4621           0 : }
    4622             : 
    4623             : DEBUG_FUNCTION void
    4624           0 : debug (cp_binding_level *ptr)
    4625             : {
    4626           0 :   if (ptr)
    4627           0 :     debug (*ptr);
    4628             :   else
    4629           0 :     fprintf (stderr, "<nil>\n");
    4630           0 : }
    4631             : 
    4632             : static void
    4633           0 : print_other_binding_stack (cp_binding_level *stack)
    4634             : {
    4635           0 :   cp_binding_level *level;
    4636           0 :   for (level = stack; !global_scope_p (level); level = level->level_chain)
    4637             :     {
    4638           0 :       fprintf (stderr, "binding level %p\n", (void *) level);
    4639           0 :       print_binding_level (level);
    4640             :     }
    4641           0 : }
    4642             : 
    4643             : DEBUG_FUNCTION void
    4644           0 : print_binding_stack (void)
    4645             : {
    4646           0 :   cp_binding_level *b;
    4647           0 :   fprintf (stderr, "current_binding_level=%p\n"
    4648             :            "class_binding_level=%p\n"
    4649             :            "NAMESPACE_LEVEL (global_namespace)=%p\n",
    4650           0 :            (void *) current_binding_level, (void *) class_binding_level,
    4651           0 :            (void *) NAMESPACE_LEVEL (global_namespace));
    4652           0 :   if (class_binding_level)
    4653             :     {
    4654           0 :       for (b = class_binding_level; b; b = b->level_chain)
    4655           0 :         if (b == current_binding_level)
    4656             :           break;
    4657           0 :       if (b)
    4658             :         b = class_binding_level;
    4659             :       else
    4660           0 :         b = current_binding_level;
    4661             :     }
    4662             :   else
    4663           0 :     b = current_binding_level;
    4664           0 :   print_other_binding_stack (b);
    4665           0 :   fprintf (stderr, "global:\n");
    4666           0 :   print_binding_level (NAMESPACE_LEVEL (global_namespace));
    4667           0 : }
    4668             : 
    4669             : /* Push a definition of struct, union or enum tag named ID.  into
    4670             :    binding_level B.  DECL is a TYPE_DECL for the type.  DECL has
    4671             :    already been pushed into its binding level.  This is bookkeeping to
    4672             :    find it easily.  */
    4673             : 
    4674             : static void
    4675   230632112 : set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
    4676             : {
    4677   230632112 :   if (b->kind == sk_namespace)
    4678             :     /* At namespace scope we should not see an identifier type value.  */
    4679    16541164 :     gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
    4680             :                          /* We could be pushing a friend underneath a template
    4681             :                             parm (ill-formed).  */
    4682             :                          || (TEMPLATE_PARM_P
    4683             :                              (TYPE_NAME (REAL_IDENTIFIER_TYPE_VALUE (id)))));
    4684             :   else
    4685             :     {
    4686             :       /* Push the current type value, so we can restore it later  */
    4687   214090948 :       tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
    4688   214090948 :       b->type_shadowed = tree_cons (id, old, b->type_shadowed);
    4689   214090948 :       tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
    4690   214090948 :       TREE_TYPE (b->type_shadowed) = type;
    4691   214090948 :       SET_IDENTIFIER_TYPE_VALUE (id, type);
    4692             :     }
    4693   230632112 : }
    4694             : 
    4695             : /* As set_identifier_type_value_with_scope, but using
    4696             :    current_binding_level.  */
    4697             : 
    4698             : void
    4699    79695600 : set_identifier_type_value (tree id, tree decl)
    4700             : {
    4701    79695600 :   set_identifier_type_value_with_scope (id, decl, current_binding_level);
    4702    79695600 : }
    4703             : 
    4704             : /* Return the name for the constructor (or destructor) for the
    4705             :    specified class.  */
    4706             : 
    4707             : tree
    4708   221899746 : constructor_name (tree type)
    4709             : {
    4710   221899746 :   tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
    4711             : 
    4712   221899746 :   return decl ? DECL_NAME (decl) : NULL_TREE;
    4713             : }
    4714             : 
    4715             : /* Returns TRUE if NAME is the name for the constructor for TYPE,
    4716             :    which must be a class type.  */
    4717             : 
    4718             : bool
    4719   207785630 : constructor_name_p (tree name, tree type)
    4720             : {
    4721   207785630 :   gcc_assert (MAYBE_CLASS_TYPE_P (type));
    4722             : 
    4723             :   /* These don't have names.  */
    4724   207785630 :   if (TREE_CODE (type) == DECLTYPE_TYPE
    4725   207785627 :       || TREE_CODE (type) == TYPEOF_TYPE)
    4726             :     return false;
    4727             : 
    4728   207785627 :   if (name && name == constructor_name (type))
    4729    15409113 :     return true;
    4730             : 
    4731             :   return false;
    4732             : }
    4733             : 
    4734             : /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
    4735             :    caller to set DECL_CONTEXT properly.
    4736             : 
    4737             :    Warning: For class and block-scope this must only be used when X
    4738             :    will be the new innermost binding for its name, as we tack it onto
    4739             :    the front of IDENTIFIER_BINDING without checking to see if the
    4740             :    current IDENTIFIER_BINDING comes from a closer binding level than
    4741             :    LEVEL.
    4742             : 
    4743             :    Warning: For namespace scope, this will look in LEVEL for an
    4744             :    existing binding to match, but if not found will push the decl into
    4745             :    CURRENT_NAMESPACE.  Use push_nested_namespace/pushdecl/
    4746             :    pop_nested_namespace if you really need to push it into a foreign
    4747             :    namespace.  */
    4748             : 
    4749             : static tree
    4750    36283817 : do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
    4751             : {
    4752    36283817 :   cp_binding_level *b;
    4753             : 
    4754    36283817 :   if (level->kind == sk_class)
    4755             :     {
    4756           0 :       gcc_checking_assert (!hiding);
    4757           0 :       b = class_binding_level;
    4758           0 :       class_binding_level = level;
    4759           0 :       pushdecl_class_level (x);
    4760           0 :       class_binding_level = b;
    4761             :     }
    4762             :   else
    4763             :     {
    4764    36283817 :       tree function_decl = current_function_decl;
    4765    36283817 :       if (level->kind == sk_namespace)
    4766    35108209 :         current_function_decl = NULL_TREE;
    4767    36283817 :       b = current_binding_level;
    4768    36283817 :       current_binding_level = level;
    4769    36283817 :       x = pushdecl (x, hiding);
    4770    36283817 :       current_binding_level = b;
    4771    36283817 :       current_function_decl = function_decl;
    4772             :     }
    4773    36283817 :   return x;
    4774             : }
    4775             : 
    4776             : /* Inject X into the local scope just before the function parms.  */
    4777             : 
    4778             : tree
    4779      494086 : pushdecl_outermost_localscope (tree x)
    4780             : {
    4781      494086 :   cp_binding_level *b = NULL;
    4782      494086 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    4783             : 
    4784             :   /* Find the scope just inside the function parms.  */
    4785      494086 :   for (cp_binding_level *n = current_binding_level;
    4786     1305591 :        n->kind != sk_function_parms; n = b->level_chain)
    4787      811505 :     b = n;
    4788             : 
    4789      494086 :   return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
    4790      494086 : }
    4791             : 
    4792             : /* Process a local-scope or namespace-scope using declaration.  LOOKUP
    4793             :    is the result of qualified lookup (both value & type are
    4794             :    significant).  FN_SCOPE_P indicates if we're at function-scope (as
    4795             :    opposed to namespace-scope).  *VALUE_P and *TYPE_P are the current
    4796             :    bindings, which are altered to reflect the newly brought in
    4797             :    declarations.  */
    4798             : 
    4799             : static bool
    4800     4201366 : do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
    4801             :                          bool insert_p, tree *value_p, tree *type_p)
    4802             : {
    4803     4201366 :   tree value = *value_p;
    4804     4201366 :   tree type = *type_p;
    4805     4201366 :   bool failed = false;
    4806             : 
    4807             :   /* Shift the old and new bindings around so we're comparing class and
    4808             :      enumeration names to each other.  */
    4809     4201366 :   if (value && DECL_IMPLICIT_TYPEDEF_P (value))
    4810             :     {
    4811             :       type = value;
    4812             :       value = NULL_TREE;
    4813             :     }
    4814             : 
    4815     4201366 :   if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
    4816             :     {
    4817       69039 :       lookup.type = lookup.value;
    4818       69039 :       lookup.value = NULL_TREE;
    4819             :     }
    4820             : 
    4821             :   /* Only process exporting if we're going to be inserting.  */
    4822     4201366 :   bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
    4823             : 
    4824             :   /* First do the value binding.  */
    4825     4201366 :   if (!lookup.value)
    4826             :     /* Nothing (only implicit typedef found).  */
    4827       69039 :     gcc_checking_assert (lookup.type);
    4828     4132327 :   else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
    4829             :     {
    4830    12552920 :       for (lkp_iterator usings (lookup.value); usings; ++usings)
    4831             :         {
    4832     5511866 :           tree new_fn = *usings;
    4833     5511866 :           bool exporting = revealing_p && module_exporting_p ();
    4834       13118 :           if (exporting)
    4835             :             {
    4836             :               /* If the using decl is exported, the things it refers
    4837             :                  to must also be exported (or not habve module attachment).  */
    4838       13118 :               if (!DECL_MODULE_EXPORT_P (new_fn)
    4839       13118 :                   && (DECL_LANG_SPECIFIC (new_fn)
    4840           6 :                       && DECL_MODULE_ATTACH_P (new_fn)))
    4841             :                 {
    4842           6 :                   error ("%q#D does not have external linkage", new_fn);
    4843           6 :                   inform (DECL_SOURCE_LOCATION (new_fn),
    4844             :                           "%q#D declared here", new_fn);
    4845           6 :                   exporting = false;
    4846             :                 }
    4847             :             }
    4848             : 
    4849             :           /* [namespace.udecl]
    4850             : 
    4851             :              If a function declaration in namespace scope or block
    4852             :              scope has the same name and the same parameter types as a
    4853             :              function introduced by a using declaration the program is
    4854             :              ill-formed.  */
    4855             :           /* This seems overreaching, asking core -- why do we care
    4856             :              about decls in the namespace that we cannot name (because
    4857             :              they are not transitively imported.  We just check the
    4858             :              decls that are in this TU.  */
    4859     5511866 :           bool found = false;
    4860    69384146 :           for (ovl_iterator old (value); !found && old; ++old)
    4861             :             {
    4862    32133350 :               tree old_fn = *old;
    4863             : 
    4864    32133350 :               if (new_fn == old_fn)
    4865             :                 {
    4866             :                   /* The function already exists in the current
    4867             :                      namespace.  We will still want to insert it if
    4868             :                      it is revealing a not-revealed thing.  */
    4869      197166 :                   found = true;
    4870      197166 :                   if (!revealing_p)
    4871             :                     ;
    4872         587 :                   else if (old.using_p ())
    4873             :                     {
    4874         587 :                       if (exporting)
    4875             :                         /* Update in place.  'tis ok.  */
    4876         581 :                         OVL_EXPORT_P (old.get_using ()) = true;
    4877             :                       ;
    4878             :                     }
    4879           0 :                   else if (DECL_MODULE_EXPORT_P (new_fn))
    4880             :                     ;
    4881             :                   else
    4882             :                     {
    4883           0 :                       value = old.remove_node (value);
    4884           0 :                       found = false;
    4885             :                     }
    4886             :                   break;
    4887             :                 }
    4888    31936184 :               else if (old.using_p ())
    4889    29983796 :                 continue; /* This is a using decl. */
    4890     1952388 :               else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
    4891     1747914 :                 continue; /* This is an anticipated builtin.  */
    4892      204474 :               else if (!matching_fn_p (new_fn, old_fn))
    4893      204430 :                 continue; /* Parameters do not match.  */
    4894          44 :               else if (decls_match (new_fn, old_fn))
    4895             :                 {
    4896             :                   /* Extern "C" in different namespaces.  */
    4897             :                   found = true;
    4898             :                   break;
    4899             :                 }
    4900             :               else
    4901             :                 {
    4902          28 :                   diagnose_name_conflict (new_fn, old_fn);
    4903          28 :                   failed = true;
    4904          28 :                   found = true;
    4905          28 :                   break;
    4906             :                 }
    4907             :             }
    4908             : 
    4909     5511866 :           if (!found && insert_p)
    4910             :             /* Unlike the decl-pushing case we don't drop anticipated
    4911             :                builtins here.  They don't cause a problem, and we'd
    4912             :                like to match them with a future declaration.  */
    4913     5314656 :             value = ovl_insert (new_fn, value, 1 + exporting);
    4914             :         }
    4915     3520527 :     }
    4916      611800 :   else if (value
    4917             :            /* Ignore anticipated builtins.  */
    4918       24905 :            && !anticipated_builtin_p (value)
    4919      636701 :            && (fn_scope_p || !decls_match (lookup.value, value)))
    4920             :     {
    4921          24 :       diagnose_name_conflict (lookup.value, value);
    4922          24 :       failed = true;
    4923             :     }
    4924      611776 :   else if (insert_p)
    4925             :     // FIXME:what if we're newly exporting lookup.value
    4926      611776 :     value = lookup.value;
    4927             :   
    4928             :   /* Now the type binding.  */
    4929     4201366 :   if (lookup.type && lookup.type != type)
    4930             :     {
    4931             :       // FIXME: What if we're exporting lookup.type?
    4932       69052 :       if (type && !decls_match (lookup.type, type))
    4933             :         {
    4934           4 :           diagnose_name_conflict (lookup.type, type);
    4935           4 :           failed = true;
    4936             :         }
    4937       69048 :       else if (insert_p)
    4938       69048 :         type = lookup.type;
    4939             :     }
    4940             : 
    4941     4201366 :   if (insert_p)
    4942             :     {
    4943             :       /* If value is empty, shift any class or enumeration name back.  */
    4944     4201360 :       if (!value)
    4945             :         {
    4946       69027 :           value = type;
    4947       69027 :           type = NULL_TREE;
    4948             :         }
    4949     4201360 :       *value_p = value;
    4950     4201360 :       *type_p = type;
    4951             :     }
    4952             : 
    4953     4201366 :   return failed;
    4954             : }
    4955             : 
    4956             : /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
    4957             :    Both are namespaces.  */
    4958             : 
    4959             : bool
    4960    74383150 : is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
    4961             : {
    4962    74383150 :   int depth = SCOPE_DEPTH (ancestor);
    4963             : 
    4964    74383150 :   if (!depth && !inline_only)
    4965             :     /* The global namespace encloses everything.  */
    4966             :     return true;
    4967             : 
    4968    75725231 :   while (SCOPE_DEPTH (descendant) > depth
    4969    75725231 :          && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
    4970     1411786 :     descendant = CP_DECL_CONTEXT (descendant);
    4971             : 
    4972    74313445 :   return ancestor == descendant;
    4973             : }
    4974             : 
    4975             : /* Returns true if ROOT (a non-alias namespace, class, or function)
    4976             :    encloses CHILD.  CHILD may be either a class type or a namespace
    4977             :    (maybe alias).  */
    4978             : 
    4979             : bool
    4980    53897524 : is_ancestor (tree root, tree child)
    4981             : {
    4982    53897524 :   gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
    4983             :                         && !DECL_NAMESPACE_ALIAS (root))
    4984             :                        || TREE_CODE (root) == FUNCTION_DECL
    4985             :                        || CLASS_TYPE_P (root));
    4986    53897524 :   gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
    4987             :                        || CLASS_TYPE_P (child));
    4988             : 
    4989             :   /* The global namespace encloses everything.  Early-out for the
    4990             :      common case.  */
    4991    53897524 :   if (root == global_namespace)
    4992             :     return true;
    4993             : 
    4994             :   /* Search CHILD until we reach namespace scope.  */
    4995   110912504 :   while (TREE_CODE (child) != NAMESPACE_DECL)
    4996             :     {
    4997             :       /* If we've reached the ROOT, it encloses CHILD.  */
    4998    57232741 :       if (root == child)
    4999             :         return true;
    5000             : 
    5001             :       /* Go out one level.  */
    5002    57232676 :       if (TYPE_P (child))
    5003    56806209 :         child = TYPE_NAME (child);
    5004    57232676 :       child = CP_DECL_CONTEXT (child);
    5005             :     }
    5006             : 
    5007    53679763 :   if (TREE_CODE (root) != NAMESPACE_DECL)
    5008             :     /* Failed to meet the non-namespace we were looking for.  */
    5009             :     return false;
    5010             : 
    5011    53679745 :   if (tree alias = DECL_NAMESPACE_ALIAS (child))
    5012           4 :     child = alias;
    5013             : 
    5014    53679745 :   return is_nested_namespace (root, child);
    5015             : }
    5016             : 
    5017             : /* Enter the class or namespace scope indicated by T suitable for name
    5018             :    lookup.  T can be arbitrary scope, not necessary nested inside the
    5019             :    current scope.  Returns a non-null scope to pop iff pop_scope
    5020             :    should be called later to exit this scope.  */
    5021             : 
    5022             : tree
    5023   203759814 : push_scope (tree t)
    5024             : {
    5025   203759814 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5026    48140780 :     push_decl_namespace (t);
    5027   155619034 :   else if (CLASS_TYPE_P (t))
    5028             :     {
    5029   110486819 :       if (!at_class_scope_p ()
    5030   110486819 :           || !same_type_p (current_class_type, t))
    5031    47028416 :         push_nested_class (t);
    5032             :       else
    5033             :         /* T is the same as the current scope.  There is therefore no
    5034             :            need to re-enter the scope.  Since we are not actually
    5035             :            pushing a new scope, our caller should not call
    5036             :            pop_scope.  */
    5037             :         t = NULL_TREE;
    5038             :     }
    5039             : 
    5040   203759814 :   return t;
    5041             : }
    5042             : 
    5043             : /* Leave scope pushed by push_scope.  */
    5044             : 
    5045             : void
    5046   144684521 : pop_scope (tree t)
    5047             : {
    5048   144684521 :   if (t == NULL_TREE)
    5049             :     return;
    5050   140301408 :   if (TREE_CODE (t) == NAMESPACE_DECL)
    5051    48140777 :     pop_decl_namespace ();
    5052    92160631 :   else if CLASS_TYPE_P (t)
    5053    47028416 :     pop_nested_class ();
    5054             : }
    5055             : 
    5056             : /* Subroutine of push_inner_scope.  */
    5057             : 
    5058             : static void
    5059      153714 : push_inner_scope_r (tree outer, tree inner)
    5060             : {
    5061      153714 :   tree prev;
    5062             : 
    5063      153714 :   if (outer == inner
    5064      153714 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5065             :     return;
    5066             : 
    5067      153699 :   prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5068      153699 :   if (outer != prev)
    5069         269 :     push_inner_scope_r (outer, prev);
    5070      153699 :   if (TREE_CODE (inner) == NAMESPACE_DECL)
    5071             :     {
    5072             :       cp_binding_level *save_template_parm = 0;
    5073             :       /* Temporary take out template parameter scopes.  They are saved
    5074             :          in reversed order in save_template_parm.  */
    5075       33950 :       while (current_binding_level->kind == sk_template_parms)
    5076             :         {
    5077       16903 :           cp_binding_level *b = current_binding_level;
    5078       16903 :           current_binding_level = b->level_chain;
    5079       16903 :           b->level_chain = save_template_parm;
    5080       16903 :           save_template_parm = b;
    5081             :         }
    5082             : 
    5083       17047 :       resume_scope (NAMESPACE_LEVEL (inner));
    5084       17047 :       current_namespace = inner;
    5085             : 
    5086             :       /* Restore template parameter scopes.  */
    5087       33950 :       while (save_template_parm)
    5088             :         {
    5089       16903 :           cp_binding_level *b = save_template_parm;
    5090       16903 :           save_template_parm = b->level_chain;
    5091       16903 :           b->level_chain = current_binding_level;
    5092       16903 :           current_binding_level = b;
    5093             :         }
    5094             :     }
    5095             :   else
    5096      136652 :     pushclass (inner);
    5097             : }
    5098             : 
    5099             : /* Enter the scope INNER from current scope.  INNER must be a scope
    5100             :    nested inside current scope.  This works with both name lookup and
    5101             :    pushing name into scope.  In case a template parameter scope is present,
    5102             :    namespace is pushed under the template parameter scope according to
    5103             :    name lookup rule in 14.6.1/6.
    5104             : 
    5105             :    Return the former current scope suitable for pop_inner_scope.  */
    5106             : 
    5107             : tree
    5108      153445 : push_inner_scope (tree inner)
    5109             : {
    5110      153445 :   tree outer = current_scope ();
    5111      153445 :   if (!outer)
    5112           0 :     outer = current_namespace;
    5113             : 
    5114      153445 :   push_inner_scope_r (outer, inner);
    5115      153445 :   return outer;
    5116             : }
    5117             : 
    5118             : /* Exit the current scope INNER back to scope OUTER.  */
    5119             : 
    5120             : void
    5121      153445 : pop_inner_scope (tree outer, tree inner)
    5122             : {
    5123      153445 :   if (outer == inner
    5124      153445 :       || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
    5125             :     return;
    5126             : 
    5127      307132 :   while (outer != inner)
    5128             :     {
    5129      153702 :       if (TREE_CODE (inner) == NAMESPACE_DECL)
    5130             :         {
    5131             :           cp_binding_level *save_template_parm = 0;
    5132             :           /* Temporary take out template parameter scopes.  They are saved
    5133             :              in reversed order in save_template_parm.  */
    5134       33950 :           while (current_binding_level->kind == sk_template_parms)
    5135             :             {
    5136       16903 :               cp_binding_level *b = current_binding_level;
    5137       16903 :               current_binding_level = b->level_chain;
    5138       16903 :               b->level_chain = save_template_parm;
    5139       16903 :               save_template_parm = b;
    5140             :             }
    5141             : 
    5142       17047 :           pop_namespace ();
    5143             : 
    5144             :           /* Restore template parameter scopes.  */
    5145       50997 :           while (save_template_parm)
    5146             :             {
    5147       16903 :               cp_binding_level *b = save_template_parm;
    5148       16903 :               save_template_parm = b->level_chain;
    5149       16903 :               b->level_chain = current_binding_level;
    5150       16903 :               current_binding_level = b;
    5151             :             }
    5152             :         }
    5153             :       else
    5154      136655 :         popclass ();
    5155             : 
    5156      153699 :       inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
    5157             :     }
    5158             : }
    5159             : 
    5160             : /* Do a pushlevel for class declarations.  */
    5161             : 
    5162             : void
    5163   113175079 : pushlevel_class (void)
    5164             : {
    5165   113175079 :   class_binding_level = begin_scope (sk_class, current_class_type);
    5166   113175079 : }
    5167             : 
    5168             : /* ...and a poplevel for class declarations.  */
    5169             : 
    5170             : void
    5171   235250560 : poplevel_class (void)
    5172             : {
    5173   235250560 :   cp_binding_level *level = class_binding_level;
    5174   235250560 :   cp_class_binding *cb;
    5175   235250560 :   size_t i;
    5176   235250560 :   tree shadowed;
    5177             : 
    5178   235250560 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5179   235250560 :   gcc_assert (level != 0);
    5180             : 
    5181             :   /* If we're leaving a toplevel class, cache its binding level.  */
    5182   235250557 :   if (current_class_depth == 1)
    5183   157120759 :     previous_class_level = level;
    5184   235250557 :   for (shadowed = level->type_shadowed;
    5185   857549900 :        shadowed;
    5186   622299343 :        shadowed = TREE_CHAIN (shadowed))
    5187   622299343 :     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
    5188             : 
    5189             :   /* Remove the bindings for all of the class-level declarations.  */
    5190   235250557 :   if (level->class_shadowed)
    5191             :     {
    5192   355318845 :       FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
    5193             :         {
    5194   271375365 :           IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
    5195   271375365 :           cxx_binding_free (cb->base);
    5196             :         }
    5197    83943480 :       ggc_free (level->class_shadowed);
    5198    83943480 :       level->class_shadowed = NULL;
    5199             :     }
    5200             : 
    5201             :   /* Now, pop out of the binding level which we created up in the
    5202             :      `pushlevel_class' routine.  */
    5203   235250557 :   gcc_assert (current_binding_level == level);
    5204   235250557 :   leave_scope ();
    5205   235250557 : }
    5206             : 
    5207             : /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
    5208             :    appropriate.  DECL is the value to which a name has just been
    5209             :    bound.  CLASS_TYPE is the class in which the lookup occurred.  */
    5210             : 
    5211             : static void
    5212    92260534 : set_inherited_value_binding_p (cxx_binding *binding, tree decl,
    5213             :                                tree class_type)
    5214             : {
    5215    92260534 :   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
    5216             :     {
    5217    91856610 :       tree context;
    5218             : 
    5219    91856610 :       if (is_overloaded_fn (decl))
    5220    26951393 :         context = ovl_scope (decl);
    5221             :       else
    5222             :         {
    5223    64905217 :           gcc_assert (DECL_P (decl));
    5224    64905217 :           context = context_for_name_lookup (decl);
    5225             :         }
    5226             : 
    5227    91856610 :       if (is_properly_derived_from (class_type, context))
    5228    11742044 :         INHERITED_VALUE_BINDING_P (binding) = 1;
    5229             :       else
    5230    80114566 :         INHERITED_VALUE_BINDING_P (binding) = 0;
    5231             :     }
    5232      403924 :   else if (binding->value == decl)
    5233             :     /* We only encounter a TREE_LIST when there is an ambiguity in the
    5234             :        base classes.  Such an ambiguity can be overridden by a
    5235             :        definition in this class.  */
    5236      403924 :     INHERITED_VALUE_BINDING_P (binding) = 1;
    5237             :   else
    5238           0 :     INHERITED_VALUE_BINDING_P (binding) = 0;
    5239    92260534 : }
    5240             : 
    5241             : /* Make the declaration of X appear in CLASS scope.  */
    5242             : 
    5243             : bool
    5244   104892803 : pushdecl_class_level (tree x)
    5245             : {
    5246   104892803 :   bool is_valid = true;
    5247             : 
    5248             :   /* Do nothing if we're adding to an outer lambda closure type,
    5249             :      outer_binding will add it later if it's needed.  */
    5250   104892803 :   if (current_class_type != class_binding_level->this_entity)
    5251             :     return true;
    5252             : 
    5253   209785606 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5254             :   /* Get the name of X.  */
    5255   104892803 :   tree name = OVL_NAME (x);
    5256             : 
    5257   104892803 :   if (name)
    5258             :     {
    5259   104691362 :       is_valid = push_class_level_binding (name, x);
    5260   104691362 :       if (TREE_CODE (x) == TYPE_DECL)
    5261    76514596 :         set_identifier_type_value (name, x);
    5262             :     }
    5263      201441 :   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
    5264             :     {
    5265             :       /* If X is an anonymous aggregate, all of its members are
    5266             :          treated as if they were members of the class containing the
    5267             :          aggregate, for naming purposes.  */
    5268      100123 :       location_t save_location = input_location;
    5269      100123 :       tree anon = TREE_TYPE (x);
    5270      100123 :       if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
    5271       62292 :         for (unsigned ix = member_vec->length (); ix--;)
    5272             :           {
    5273       55051 :             tree binding = (*member_vec)[ix];
    5274       55051 :             if (STAT_HACK_P (binding))
    5275             :               {
    5276           0 :                 if (!pushdecl_class_level (STAT_TYPE (binding)))
    5277           0 :                   is_valid = false;
    5278           0 :                 binding = STAT_DECL (binding);
    5279             :               }
    5280       55051 :             if (!pushdecl_class_level (binding))
    5281           0 :               is_valid = false;
    5282             :         }
    5283             :       else
    5284      405285 :         for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
    5285      312403 :           if (TREE_CODE (f) == FIELD_DECL)
    5286             :             {
    5287      200836 :               input_location = DECL_SOURCE_LOCATION (f);
    5288      200836 :               if (!pushdecl_class_level (f))
    5289      312403 :                 is_valid = false;
    5290             :             }
    5291      100123 :       input_location = save_location;
    5292             :     }
    5293   104892803 :   return is_valid;
    5294   104892803 : }
    5295             : 
    5296             : /* Return the BINDING (if any) for NAME in SCOPE, which is a class
    5297             :    scope.  If the value returned is non-NULL, and the PREVIOUS field
    5298             :    is not set, callers must set the PREVIOUS field explicitly.  */
    5299             : 
    5300             : static cxx_binding *
    5301  1141354144 : get_class_binding (tree name, cp_binding_level *scope)
    5302             : {
    5303  1141354144 :   tree class_type;
    5304  1141354144 :   tree type_binding;
    5305  1141354144 :   tree value_binding;
    5306  1141354144 :   cxx_binding *binding;
    5307             : 
    5308  1141354144 :   class_type = scope->this_entity;
    5309             : 
    5310             :   /* Get the type binding.  */
    5311  1141354144 :   type_binding = lookup_member (class_type, name,
    5312             :                                 /*protect=*/2, /*want_type=*/true,
    5313             :                                 tf_warning_or_error);
    5314             :   /* Get the value binding.  */
    5315  1141354144 :   value_binding = lookup_member (class_type, name,
    5316             :                                  /*protect=*/2, /*want_type=*/false,
    5317             :                                  tf_warning_or_error);
    5318             : 
    5319             :   /* If we found either a type binding or a value binding, create a
    5320             :      new binding object.  */
    5321  1141354144 :   if (type_binding || value_binding)
    5322             :     {
    5323    92260534 :       binding = new_class_binding (name,
    5324             :                                    value_binding,
    5325             :                                    type_binding,
    5326             :                                    scope);
    5327    92260534 :       set_inherited_value_binding_p (binding, value_binding, class_type);
    5328             :     }
    5329             :   else
    5330             :     binding = NULL;
    5331             : 
    5332  1141354144 :   return binding;
    5333             : }
    5334             : 
    5335             : /* Make the declaration(s) of X appear in CLASS scope under the name
    5336             :    NAME.  Returns true if the binding is valid.  */
    5337             : 
    5338             : bool
    5339   262251240 : push_class_level_binding (tree name, tree x)
    5340             : {
    5341   262251240 :   cxx_binding *binding;
    5342   262251240 :   tree decl = x;
    5343   262251240 :   bool ok;
    5344             : 
    5345   262251240 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5346             : 
    5347             :   /* The class_binding_level will be NULL if x is a template
    5348             :      parameter name in a member template.  */
    5349   262251240 :   if (!class_binding_level)
    5350             :     return true;
    5351             : 
    5352   262251240 :   if (name == error_mark_node)
    5353             :     return false;
    5354             : 
    5355             :   /* Can happen for an erroneous declaration (c++/60384).  */
    5356   262251240 :   if (!identifier_p (name))
    5357             :     {
    5358           3 :       gcc_assert (errorcount || sorrycount);
    5359             :       return false;
    5360             :     }
    5361             : 
    5362             :   /* Check for invalid member names.  But don't worry about a default
    5363             :      argument-scope lambda being pushed after the class is complete.  */
    5364   262251252 :   gcc_assert (TYPE_BEING_DEFINED (current_class_type)
    5365             :               || LAMBDA_TYPE_P (TREE_TYPE (decl)));
    5366             :   /* Check that we're pushing into the right binding level.  */
    5367   262251237 :   gcc_assert (current_class_type == class_binding_level->this_entity);
    5368             : 
    5369             :   /* We could have been passed a tree list if this is an ambiguous
    5370             :      declaration. If so, pull the declaration out because
    5371             :      check_template_shadow will not handle a TREE_LIST.  */
    5372   262251237 :   if (TREE_CODE (decl) == TREE_LIST
    5373   262251237 :       && TREE_TYPE (decl) == error_mark_node)
    5374           0 :     decl = TREE_VALUE (decl);
    5375             : 
    5376   262251237 :   if (!check_template_shadow (decl))
    5377             :     return false;
    5378             : 
    5379             :   /* [class.mem]
    5380             : 
    5381             :      If T is the name of a class, then each of the following shall
    5382             :      have a name different from T:
    5383             : 
    5384             :      -- every static data member of class T;
    5385             : 
    5386             :      -- every member of class T that is itself a type;
    5387             : 
    5388             :      -- every enumerator of every member of class T that is an
    5389             :         enumerated type;
    5390             : 
    5391             :      -- every member of every anonymous union that is a member of
    5392             :         class T.
    5393             : 
    5394             :      (Non-static data members were also forbidden to have the same
    5395             :      name as T until TC1.)  */
    5396   262251185 :   if ((VAR_P (x)
    5397   262251185 :        || TREE_CODE (x) == CONST_DECL
    5398   253090099 :        || (TREE_CODE (x) == TYPE_DECL
    5399    76514577 :            && !DECL_SELF_REFERENCE_P (x))
    5400             :        /* A data member of an anonymous union.  */
    5401   213509109 :        || (TREE_CODE (x) == FIELD_DECL
    5402    14947198 :            && DECL_CONTEXT (x) != current_class_type))
    5403   302066835 :       && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
    5404             :     {
    5405          31 :       tree scope = context_for_name_lookup (x);
    5406          31 :       if (TYPE_P (scope) && same_type_p (scope, current_class_type))
    5407             :         {
    5408          31 :           error_at (DECL_SOURCE_LOCATION (x),
    5409             :                     "%qD has the same name as the class in which it is "
    5410             :                     "declared", x);
    5411          31 :           return false;
    5412             :         }
    5413             :     }
    5414             : 
    5415             :   /* Get the current binding for NAME in this class, if any.  */
    5416   262251154 :   binding = IDENTIFIER_BINDING (name);
    5417   262251154 :   if (!binding || binding->scope != class_binding_level)
    5418             :     {
    5419   186933338 :       binding = get_class_binding (name, class_binding_level);
    5420             :       /* If a new binding was created, put it at the front of the
    5421             :          IDENTIFIER_BINDING list.  */
    5422   186933338 :       if (binding)
    5423             :         {
    5424     7814732 :           binding->previous = IDENTIFIER_BINDING (name);
    5425     7814732 :           IDENTIFIER_BINDING (name) = binding;
    5426             :         }
    5427             :     }
    5428             : 
    5429             :   /* If there is already a binding, then we may need to update the
    5430             :      current value.  */
    5431    83132548 :   if (binding && binding->value)
    5432             :     {
    5433    83132548 :       tree bval = binding->value;
    5434    83132548 :       tree old_decl = NULL_TREE;
    5435    83132548 :       tree target_decl = strip_using_decl (decl);
    5436    83132548 :       tree target_bval = strip_using_decl (bval);
    5437             : 
    5438    83132548 :       if (INHERITED_VALUE_BINDING_P (binding))
    5439             :         {
    5440             :           /* If the old binding was from a base class, and was for a
    5441             :              tag name, slide it over to make room for the new binding.
    5442             :              The old binding is still visible if explicitly qualified
    5443             :              with a class-key.  */
    5444     9308139 :           if (TREE_CODE (target_bval) == TYPE_DECL
    5445     4852297 :               && DECL_ARTIFICIAL (target_bval)
    5446     9643415 :               && !(TREE_CODE (target_decl) == TYPE_DECL
    5447      335264 :                    && DECL_ARTIFICIAL (target_decl)))
    5448             :             {
    5449       44693 :               old_decl = binding->type;
    5450       44693 :               binding->type = bval;
    5451       44693 :               binding->value = NULL_TREE;
    5452       44693 :               INHERITED_VALUE_BINDING_P (binding) = 0;
    5453             :             }
    5454             :           else
    5455             :             {
    5456     9263446 :               old_decl = bval;
    5457             :               /* Any inherited type declaration is hidden by the type
    5458             :                  declaration in the derived class.  */
    5459     9263446 :               if (TREE_CODE (target_decl) == TYPE_DECL
    5460     9263446 :                   && DECL_ARTIFICIAL (target_decl))
    5461      291097 :                 binding->type = NULL_TREE;
    5462             :             }
    5463             :         }
    5464    73824409 :       else if (TREE_CODE (decl) == USING_DECL
    5465         271 :                && TREE_CODE (bval) == USING_DECL
    5466    73824499 :                && same_type_p (USING_DECL_SCOPE (decl),
    5467             :                                USING_DECL_SCOPE (bval)))
    5468             :         /* This is a using redeclaration that will be diagnosed later
    5469             :            in supplement_binding */
    5470             :         ;
    5471    73824368 :       else if (TREE_CODE (decl) == USING_DECL
    5472         230 :                && TREE_CODE (bval) == USING_DECL
    5473          49 :                && DECL_DEPENDENT_P (decl)
    5474    73824379 :                && DECL_DEPENDENT_P (bval))
    5475             :         return true;
    5476    73824357 :       else if (TREE_CODE (decl) == USING_DECL
    5477         219 :                && DECL_DEPENDENT_P (decl)
    5478    73824509 :                && OVL_P (target_bval))
    5479             :         /* The new dependent using beats an old overload.  */
    5480             :         old_decl = bval;
    5481    73824205 :       else if (TREE_CODE (bval) == USING_DECL
    5482      307533 :                && DECL_DEPENDENT_P (bval)
    5483    74100559 :                && OVL_P (target_decl))
    5484             :         /* The old dependent using beats a new overload.  */
    5485             :         return true;
    5486    73547867 :       else if (OVL_P (target_decl)
    5487    73540042 :                && OVL_P (target_bval))
    5488             :         /* The new overload set contains the old one.  */
    5489             :         old_decl = bval;
    5490             : 
    5491    82848301 :       if (old_decl && binding->scope == class_binding_level)
    5492             :         {
    5493    82848301 :           binding->value = x;
    5494             :           /* It is always safe to clear INHERITED_VALUE_BINDING_P
    5495             :              here.  This function is only used to register bindings
    5496             :              from with the class definition itself.  */
    5497    82848301 :           INHERITED_VALUE_BINDING_P (binding) = 0;
    5498    82848301 :           return true;
    5499             :         }
    5500             :     }
    5501             : 
    5502             :   /* Note that we declared this value so that we can issue an error if
    5503             :      this is an invalid redeclaration of a name already used for some
    5504             :      other purpose.  */
    5505   179126504 :   note_name_declared_in_class (name, decl);
    5506             : 
    5507             :   /* If we didn't replace an existing binding, put the binding on the
    5508             :      stack of bindings for the identifier, and update the shadowed
    5509             :      list.  */
    5510   179126504 :   if (binding && binding->scope == class_binding_level)
    5511             :     /* Supplement the existing binding.  */
    5512        7898 :     ok = supplement_binding (binding, decl);
    5513             :   else
    5514             :     {
    5515             :       /* Create a new binding.  */
    5516   179118606 :       push_binding (name, decl, class_binding_level);
    5517   179118606 :       ok = true;
    5518             :     }
    5519             : 
    5520             :   return ok;
    5521   262251240 : }
    5522             : 
    5523             : /* Process and lookup a using decl SCOPE::lookup.name, filling in
    5524             :    lookup.values & lookup.type.  Return a USING_DECL, or NULL_TREE on
    5525             :    failure.  */
    5526             : 
    5527             : static tree
    5528     5710579 : lookup_using_decl (tree scope, name_lookup &lookup)
    5529             : {
    5530     5710579 :   tree current = current_scope ();
    5531     5710579 :   bool dependent_p = false;
    5532     5710579 :   tree binfo = NULL_TREE;
    5533     5710579 :   base_kind b_kind = bk_not_base;
    5534             : 
    5535             :   /* Because C++20 breaks the invariant that only member using-decls
    5536             :      refer to members and only non-member using-decls refer to
    5537             :      non-members, we first do the lookups, and then do validation that
    5538             :      what we found is ok.  */
    5539             : 
    5540     5710579 :   if (TREE_CODE (scope) == ENUMERAL_TYPE
    5541       36575 :       && cxx_dialect < cxx20
    5542       36575 :       && UNSCOPED_ENUM_P (scope)
    5543     5710595 :       && !TYPE_FUNCTION_SCOPE_P (scope))
    5544             :     {
    5545             :       /* PR c++/60265 argued that since C++11 added explicit enum scope, we
    5546             :          should allow it as meaning the enclosing scope.  I don't see any
    5547             :          justification for this in C++11, but let's keep allowing it.  */
    5548          14 :       tree ctx = CP_TYPE_CONTEXT (scope);
    5549          36 :       if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
    5550     5710579 :         scope = ctx;
    5551             :     }
    5552             : 
    5553             :   /* You cannot using-decl a destructor.  */
    5554     5710579 :   if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
    5555             :     {
    5556           6 :       error ("%<%T%s%D%> names destructor", scope,
    5557           3 :              &"::"[scope == global_namespace ? 2 : 0], lookup.name);
    5558           3 :       return NULL_TREE;
    5559             :     }
    5560             : 
    5561     5710576 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    5562             :     {
    5563             :       /* Naming a namespace member.  */
    5564     4164865 :       qualified_namespace_lookup (scope, &lookup);
    5565             : 
    5566     4164865 :       if (TYPE_P (current)
    5567           4 :           && (!lookup.value
    5568           0 :               || lookup.type
    5569           0 :               || cxx_dialect < cxx20
    5570           0 :               || TREE_CODE (lookup.value) != CONST_DECL))
    5571             :         {
    5572           4 :           error ("using-declaration for non-member at class scope");
    5573           4 :           return NULL_TREE;
    5574             :         }
    5575             :     }
    5576     1545711 :   else if (TREE_CODE (scope) == ENUMERAL_TYPE)
    5577             :     {
    5578             :       /* Naming an enumeration member.  */
    5579       36562 :       if (cxx_dialect < cxx20)
    5580          12 :         error ("%<using%> with enumeration scope %q#T "
    5581             :                "only available with %<-std=c++20%> or %<-std=gnu++20%>",
    5582             :                scope);
    5583       36562 :       lookup.value = lookup_enumerator (scope, lookup.name);
    5584             :     }
    5585             :   else
    5586             :     {
    5587             :       /* Naming a class member.  This is awkward in C++20, because we
    5588             :          might be naming an enumerator of an unrelated class.  */
    5589             : 
    5590     1509149 :       tree npscope = scope;
    5591     1509149 :       if (PACK_EXPANSION_P (scope))
    5592        6912 :         npscope = PACK_EXPANSION_PATTERN (scope);
    5593             : 
    5594     2996618 :       if (!MAYBE_CLASS_TYPE_P (npscope))
    5595             :         {
    5596          11 :           error ("%qT is not a class, namespace, or enumeration", npscope);
    5597          11 :           return NULL_TREE;
    5598             :         }
    5599             : 
    5600             :       /* Using T::T declares inheriting ctors, even if T is a typedef.  */
    5601     1509138 :       if (lookup.name == TYPE_IDENTIFIER (npscope)
    5602     1509138 :           || constructor_name_p (lookup.name, npscope))
    5603             :         {
    5604      149514 :           if (!TYPE_P (current))
    5605             :             {
    5606           0 :               error ("non-member using-declaration names constructor of %qT",
    5607             :                      npscope);
    5608           0 :               return NULL_TREE;
    5609             :             }
    5610      149514 :           maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
    5611      149514 :           lookup.name = ctor_identifier;
    5612      149514 :           CLASSTYPE_NON_AGGREGATE (current) = true;
    5613             :         }
    5614             : 
    5615     1509138 :       if (!TYPE_P (current) && cxx_dialect < cxx20)
    5616             :         {
    5617           6 :           error ("using-declaration for member at non-class scope");
    5618           6 :           return NULL_TREE;
    5619             :         }
    5620             : 
    5621     1509132 :       bool depscope = dependent_scope_p (scope);
    5622             : 
    5623     1509132 :       if (depscope)
    5624             :         /* Leave binfo null.  */;
    5625     1095986 :       else if (TYPE_P (current))
    5626             :         {
    5627     1095979 :           binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
    5628     1095979 :           gcc_checking_assert (b_kind >= bk_not_base);
    5629             : 
    5630     1095979 :           if (b_kind == bk_not_base && any_dependent_bases_p ())
    5631             :             /* Treat as-if dependent.  */
    5632             :             depscope = true;
    5633     1095957 :           else if (lookup.name == ctor_identifier
    5634     1095957 :                    && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
    5635             :             {
    5636          15 :               if (any_dependent_bases_p ())
    5637             :                 depscope = true;
    5638             :               else
    5639             :                 {
    5640          15 :                   error ("%qT is not a direct base of %qT", scope, current);
    5641          15 :                   return NULL_TREE;
    5642             :                 }
    5643             :             }
    5644             : 
    5645     1095964 :           if (b_kind < bk_proper_base)
    5646          56 :             binfo = TYPE_BINFO (scope);
    5647             :         }
    5648             :       else
    5649           7 :         binfo = TYPE_BINFO (scope);
    5650             : 
    5651     2191913 :       dependent_p = (depscope
    5652     1095971 :                      || (IDENTIFIER_CONV_OP_P (lookup.name)
    5653       98729 :                          && dependent_type_p (TREE_TYPE (lookup.name))));
    5654             : 
    5655     1095942 :       if (!dependent_p)
    5656     1095942 :         lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
    5657             :                                       /*want_type=*/false, tf_none);
    5658             : 
    5659             :       /* If the lookup in the base contains a dependent using, this
    5660             :          using is also dependent.  */
    5661     1095942 :       if (!dependent_p && lookup.value && dependent_type_p (scope))
    5662             :         {
    5663          36 :           tree val = lookup.value;
    5664          36 :           if (tree fns = maybe_get_fns (val))
    5665          12 :             val = fns;
    5666         100 :           for (tree f: lkp_range (val))
    5667          36 :             if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
    5668             :               {
    5669             :                 dependent_p = true;
    5670             :                 break;
    5671             :               }
    5672             :         }
    5673             : 
    5674     1509117 :       if (!depscope && b_kind < bk_proper_base)
    5675             :         {
    5676          41 :           if (cxx_dialect >= cxx20 && lookup.value
    5677          15 :               && TREE_CODE (lookup.value) == CONST_DECL)
    5678             :             {
    5679             :               /* Using an unrelated enum; check access here rather
    5680             :                  than separately for class and non-class using.  */
    5681           9 :               perform_or_defer_access_check
    5682           9 :                 (binfo, lookup.value, lookup.value, tf_warning_or_error);
    5683             :               /* And then if this is a copy from handle_using_decl, look
    5684             :                  through to the original enumerator.  */
    5685           9 :               if (CONST_DECL_USING_P (lookup.value))
    5686           4 :                 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
    5687             :             }
    5688          32 :           else if (!TYPE_P (current))
    5689             :             {
    5690           1 :               error ("using-declaration for member at non-class scope");
    5691           1 :               return NULL_TREE;
    5692             :             }
    5693             :           else
    5694             :             {
    5695          31 :               auto_diagnostic_group g;
    5696          31 :               error_not_base_type (scope, current);
    5697          19 :               if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
    5698          32 :                   && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
    5699           1 :                 inform (input_location,
    5700             :                         "did you mean %<using enum %T::%D%>?",
    5701             :                         scope, lookup.name);
    5702          31 :               return NULL_TREE;
    5703          31 :             }
    5704             :         }
    5705             :     }
    5706             : 
    5707             :   /* Did we find anything sane?  */
    5708     1545647 :   if (dependent_p)
    5709             :     ;
    5710     5297325 :   else if (!lookup.value)
    5711             :     {
    5712          67 :       error ("%qD has not been declared in %qD", lookup.name, scope);
    5713          67 :       return NULL_TREE;
    5714             :     }
    5715     5297258 :   else if (TREE_CODE (lookup.value) == TREE_LIST
    5716             :            /* We can (independently) have ambiguous implicit typedefs.  */
    5717     5297258 :            || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
    5718             :     {
    5719           4 :       error ("reference to %qD is ambiguous", lookup.name);
    5720           4 :       print_candidates (TREE_CODE (lookup.value) == TREE_LIST
    5721             :                         ? lookup.value : lookup.type);
    5722           4 :       return NULL_TREE;
    5723             :     }
    5724     5297254 :   else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    5725             :     {
    5726           8 :       error ("using-declaration may not name namespace %qD", lookup.value);
    5727           8 :       return NULL_TREE;
    5728             :     }
    5729             : 
    5730     5710429 :   if (TYPE_P (current))
    5731             :     {
    5732             :       /* In class scope.  */
    5733             : 
    5734             :       /* Cannot introduce a constructor name.  */
    5735     1509069 :       if (constructor_name_p (lookup.name, current))
    5736             :         {
    5737           4 :           error ("%<%T::%D%> names constructor in %qT",
    5738             :                  scope, lookup.name, current);
    5739           4 :           return NULL_TREE;
    5740             :         }
    5741             : 
    5742     1509065 :       if (lookup.value && BASELINK_P (lookup.value))
    5743             :         /* The binfo from which the functions came does not matter.  */
    5744      979580 :         lookup.value = BASELINK_FUNCTIONS (lookup.value);
    5745             :     }
    5746             : 
    5747     5710425 :   tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
    5748     5710425 :   USING_DECL_SCOPE (using_decl) = scope;
    5749     5710425 :   USING_DECL_DECLS (using_decl) = lookup.value;
    5750     5710425 :   DECL_DEPENDENT_P (using_decl) = dependent_p;
    5751     5710425 :   DECL_CONTEXT (using_decl) = current;
    5752     5710425 :   if (TYPE_P (current) && b_kind == bk_not_base)
    5753      413201 :     USING_DECL_UNRELATED_P (using_decl) = true;
    5754             : 
    5755             :   return using_decl;
    5756             : }
    5757             : 
    5758             : /* Process "using SCOPE::NAME" in a class scope.  Return the
    5759             :    USING_DECL created.  */
    5760             : 
    5761             : tree
    5762     1509178 : do_class_using_decl (tree scope, tree name)
    5763             : {
    5764     1509178 :   if (name == error_mark_node
    5765     1509174 :       || scope == error_mark_node)
    5766             :     return NULL_TREE;
    5767             : 
    5768     1509170 :   name_lookup lookup (name);
    5769     1509170 :   return lookup_using_decl (scope, lookup);
    5770     1509170 : }
    5771             : 
    5772             : 
    5773             : /* Return the binding for NAME in NS in the current TU.  If NS is
    5774             :    NULL, look in global_namespace.  We will not find declarations
    5775             :    from imports.  Users of this who, having found nothing, push a new
    5776             :    decl must be prepared for that pushing to match an existing decl.  */
    5777             : 
    5778             : tree
    5779     3961445 : get_namespace_binding (tree ns, tree name)
    5780             : {
    5781     3961445 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5782     3961445 :   if (!ns)
    5783     3961445 :     ns = global_namespace;
    5784     3961445 :   gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
    5785     3961445 :   tree ret = NULL_TREE;
    5786             : 
    5787     3961445 :   if (tree *b = find_namespace_slot (ns, name))
    5788             :     {
    5789     3223142 :       ret = *b;
    5790             : 
    5791     3223142 :       if (TREE_CODE (ret) == BINDING_VECTOR)
    5792           0 :         ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
    5793           0 :       if (ret)
    5794     3223142 :         ret = MAYBE_STAT_DECL (ret);
    5795             :     }
    5796             : 
    5797     7922890 :   return ret;
    5798     3961445 : }
    5799             : 
    5800             : /* Push internal DECL into the global namespace.  Does not do the
    5801             :    full overload fn handling and does not add it to the list of things
    5802             :    in the namespace.  */
    5803             : 
    5804             : void
    5805     3389643 : set_global_binding (tree decl)
    5806             : {
    5807     3389643 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    5808             : 
    5809     3389643 :   tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
    5810             : 
    5811     3389643 :   if (*slot)
    5812             :     /* The user's placed something in the implementor's namespace.  */
    5813           0 :     diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
    5814             : 
    5815             :   /* Force the binding, so compiler internals continue to work.  */
    5816     3389643 :   *slot = decl;
    5817     3389643 : }
    5818             : 
    5819             : /* Set the context of a declaration to scope. Complain if we are not
    5820             :    outside scope.  */
    5821             : 
    5822             : void
    5823       60976 : set_decl_namespace (tree decl, tree scope, bool friendp)
    5824             : {
    5825             :   /* Get rid of namespace aliases.  */
    5826       60976 :   scope = ORIGINAL_NAMESPACE (scope);
    5827             : 
    5828             :   /* It is ok for friends to be qualified in parallel space.  */
    5829       60976 :   if (!friendp && !is_nested_namespace (current_namespace, scope))
    5830           8 :     error ("declaration of %qD not in a namespace surrounding %qD",
    5831             :            decl, scope);
    5832       60976 :   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    5833             : 
    5834             :   /* See whether this has been declared in the namespace or inline
    5835             :      children.  */
    5836       60976 :   tree old = NULL_TREE;
    5837       60976 :   {
    5838       60976 :     name_lookup lookup (DECL_NAME (decl),
    5839       60976 :                         LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
    5840       60976 :     if (!lookup.search_qualified (scope, /*usings=*/false))
    5841             :       /* No old declaration at all.  */
    5842          36 :       goto not_found;
    5843       60940 :     old = lookup.value;
    5844       60976 :   }
    5845             : 
    5846             :   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
    5847       60940 :   if (TREE_CODE (old) == TREE_LIST)
    5848             :     {
    5849           7 :     ambiguous:
    5850          13 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    5851          13 :       error ("reference to %qD is ambiguous", decl);
    5852          13 :       print_candidates (old);
    5853          13 :       return;
    5854             :     }
    5855             : 
    5856       60933 :   if (!DECL_DECLARES_FUNCTION_P (decl))
    5857             :     {
    5858             :       /* Don't compare non-function decls with decls_match here, since
    5859             :          it can't check for the correct constness at this
    5860             :          point.  pushdecl will find those errors later.  */
    5861             : 
    5862             :       /* We might have found it in an inline namespace child of SCOPE.  */
    5863          79 :       if (TREE_CODE (decl) == TREE_CODE (old))
    5864          41 :         DECL_CONTEXT (decl) = DECL_CONTEXT (old);
    5865             : 
    5866          38 :     found:
    5867             :       /* Writing "N::i" to declare something directly in "N" is invalid.  */
    5868       31613 :       if (CP_DECL_CONTEXT (decl) == current_namespace
    5869       31613 :           && at_namespace_scope_p ())
    5870           3 :         error_at (DECL_SOURCE_LOCATION (decl),
    5871             :                   "explicit qualification in declaration of %qD", decl);
    5872       31613 :       return;
    5873             :     }
    5874             : 
    5875             :   /* Since decl is a function, old should contain a function decl.  */
    5876       60854 :   if (!OVL_P (old))
    5877             :     {
    5878          10 :     not_found:
    5879             :       /* It didn't work, go back to the explicit scope.  */
    5880          54 :       DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    5881          54 :       error ("%qD should have been declared inside %qD", decl, scope);
    5882             : 
    5883          54 :       return;
    5884             :     }
    5885             :   
    5886             :   /* We handle these in check_explicit_instantiation_namespace.  */
    5887       60844 :   if (processing_explicit_instantiation)
    5888             :     return;
    5889       60759 :   if (processing_template_decl || processing_specialization)
    5890             :     /* We have not yet called push_template_decl to turn a
    5891             :        FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
    5892             :        match.  But, we'll check later, when we construct the
    5893             :        template.  */
    5894             :     return;
    5895             : 
    5896             :   /* Instantiations or specializations of templates may be declared as
    5897             :      friends in any namespace.  */
    5898       31571 :   if (friendp && DECL_USE_TEMPLATE (decl))
    5899             :     return;
    5900             : 
    5901       31563 :   tree found = NULL_TREE;
    5902       31563 :   bool hidden_p = false;
    5903       31563 :   bool saw_template = false;
    5904             : 
    5905      109096 :   for (lkp_iterator iter (old); iter; ++iter)
    5906             :     {
    5907       45976 :       if (iter.using_p ())
    5908           0 :         continue;
    5909             : 
    5910       45976 :       tree ofn = *iter;
    5911             : 
    5912             :       /* Adjust DECL_CONTEXT first so decls_match will return true
    5913             :          if DECL will match a declaration in an inline namespace.  */
    5914       45976 :       DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
    5915       45976 :       if (decls_match (decl, ofn))
    5916             :         {
    5917       31546 :           if (found)
    5918             :             {
    5919             :               /* We found more than one matching declaration.  This
    5920             :                  can happen if we have two inline namespace children,
    5921             :                  each containing a suitable declaration.  */
    5922           6 :               DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
    5923           6 :               goto ambiguous;
    5924             :             }
    5925       31540 :           found = ofn;
    5926       31540 :           hidden_p = iter.hidden_p ();
    5927             :         }
    5928       14430 :       else if (TREE_CODE (decl) == FUNCTION_DECL
    5929       14430 :                && TREE_CODE (ofn) == TEMPLATE_DECL)
    5930       45970 :         saw_template = true;
    5931             :     }
    5932             : 
    5933       31557 :   if (!found && friendp && saw_template)
    5934             :     {
    5935             :       /* "[if no non-template match is found,] each remaining function template
    5936             :          is replaced with the specialization chosen by deduction from the
    5937             :          friend declaration or discarded if deduction fails."
    5938             : 
    5939             :          So tell check_explicit_specialization to look for a match.  */
    5940          15 :       SET_DECL_IMPLICIT_INSTANTIATION (decl);
    5941          15 :       DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
    5942          15 :       return;
    5943             :     }
    5944             : 
    5945       31542 :   if (found)
    5946             :     {
    5947       31534 :       if (hidden_p)
    5948             :         {
    5949           4 :           pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    5950             :                    "%qD has not been declared within %qD", decl, scope);
    5951           4 :           inform (DECL_SOURCE_LOCATION (found),
    5952             :                   "only here as a %<friend%>");
    5953             :         }
    5954       31534 :       DECL_CONTEXT (decl) = DECL_CONTEXT (found);
    5955       31534 :       goto found;
    5956             :     }
    5957             : 
    5958           8 :   goto not_found;
    5959             : }
    5960             : 
    5961             : /* Return the namespace where the current declaration is declared.  */
    5962             : 
    5963             : tree
    5964   833433789 : current_decl_namespace (void)
    5965             : {
    5966   833433789 :   tree result;
    5967             :   /* If we have been pushed into a different namespace, use it.  */
    5968   833433789 :   if (!vec_safe_is_empty (decl_namespace_list))
    5969    39278320 :     return decl_namespace_list->last ();
    5970             : 
    5971   794155469 :   if (current_class_type)
    5972   309954364 :     result = decl_namespace_context (current_class_type);
    5973   484201105 :   else if (current_function_decl)
    5974   175735114 :     result = decl_namespace_context (current_function_decl);
    5975             :   else
    5976   308465991 :     result = current_namespace;
    5977             :   return result;
    5978             : }
    5979             : 
    5980             : /* Process any ATTRIBUTES on a namespace definition.  Returns true if
    5981             :    attribute visibility is seen.  */
    5982             : 
    5983             : bool
    5984     3429464 : handle_namespace_attrs (tree ns, tree attributes)
    5985             : {
    5986     3429464 :   tree d;
    5987     3429464 :   bool saw_vis = false;
    5988             : 
    5989     3429464 :   if (attributes == error_mark_node)
    5990             :     return false;
    5991             : 
    5992     5283355 :   for (d = attributes; d; d = TREE_CHAIN (d))
    5993             :     {
    5994     1853891 :       tree name = get_attribute_name (d);
    5995     1853891 :       tree args = TREE_VALUE (d);
    5996             : 
    5997     1853891 :       if (is_attribute_p ("visibility", name))
    5998             :         {
    5999             :           /* attribute visibility is a property of the syntactic block
    6000             :              rather than the namespace as a whole, so we don't touch the
    6001             :              NAMESPACE_DECL at all.  */
    6002     1818250 :           tree x = args ? TREE_VALUE (args) : NULL_TREE;
    6003     1818250 :           if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
    6004             :             {
    6005           0 :               warning (OPT_Wattributes,
    6006             :                        "%qD attribute requires a single NTBS argument",
    6007             :                        name);
    6008           0 :               continue;
    6009             :             }
    6010             : 
    6011     1818250 :           if (!TREE_PUBLIC (ns))
    6012           0 :             warning (OPT_Wattributes,
    6013             :                      "%qD attribute is meaningless since members of the "
    6014             :                      "anonymous namespace get local symbols", name);
    6015             : 
    6016     1818250 :           push_visibility (TREE_STRING_POINTER (x), 1);
    6017     1818250 :           saw_vis = true;
    6018             :         }
    6019       35641 :       else if (is_attribute_p ("abi_tag", name))
    6020             :         {
    6021       35578 :           if (!DECL_NAME (ns))
    6022             :             {
    6023           3 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6024             :                        "namespace", name);
    6025           3 :               continue;
    6026             :             }
    6027       35575 :           if (!DECL_NAMESPACE_INLINE_P (ns))
    6028             :             {
    6029           0 :               warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
    6030             :                        "namespace", name);
    6031           0 :               continue;
    6032             :             }
    6033       35575 :           if (!args)
    6034             :             {
    6035          20 :               tree dn = DECL_NAME (ns);
    6036          20 :               args = build_string (IDENTIFIER_LENGTH (dn) + 1,
    6037          20 :                                    IDENTIFIER_POINTER (dn));
    6038          20 :               TREE_TYPE (args) = char_array_type_node;
    6039          20 :               args = fix_string_type (args);
    6040          20 :               args = build_tree_list (NULL_TREE, args);
    6041             :             }
    6042       35575 :           if (check_abi_tag_args (args, name))
    6043       35575 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6044       35575 :                                               DECL_ATTRIBUTES (ns));
    6045             :         }
    6046          63 :       else if (is_attribute_p ("deprecated", name))
    6047             :         {
    6048          63 :           if (!DECL_NAME (ns))
    6049             :             {
    6050           3 :               warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
    6051             :                        "namespace", name);
    6052           3 :               continue;
    6053             :             }
    6054          66 :           if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
    6055             :             {
    6056           0 :               error ("deprecated message is not a string");
    6057           0 :               continue;
    6058             :             }
    6059          60 :           TREE_DEPRECATED (ns) = 1;
    6060          60 :           if (args)
    6061           6 :             DECL_ATTRIBUTES (ns) = tree_cons (name, args,
    6062           6 :                                               DECL_ATTRIBUTES (ns));
    6063             :         }
    6064             :       else
    6065             :         {
    6066           0 :           warning (OPT_Wattributes, "%qD attribute directive ignored",
    6067             :                    name);
    6068           0 :           continue;
    6069             :         }
    6070             :     }
    6071             : 
    6072             :   return saw_vis;
    6073             : }
    6074             : 
    6075             : /* Temporarily set the namespace for the current declaration.  */
    6076             : 
    6077             : void
    6078    48147568 : push_decl_namespace (tree decl)
    6079             : {
    6080    48147568 :   if (TREE_CODE (decl) != NAMESPACE_DECL)
    6081           0 :     decl = decl_namespace_context (decl);
    6082    48147568 :   vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
    6083    48147568 : }
    6084             : 
    6085             : /* [namespace.memdef]/2 */
    6086             : 
    6087             : void
    6088    48147565 : pop_decl_namespace (void)
    6089             : {
    6090    48147565 :   decl_namespace_list->pop ();
    6091    48140777 : }
    6092             : 
    6093             : /* Process a namespace-alias declaration.  */
    6094             : 
    6095             : void
    6096       30249 : do_namespace_alias (tree alias, tree name_space)
    6097             : {
    6098       30249 :   if (name_space == error_mark_node)
    6099             :     return;
    6100             : 
    6101       30237 :   gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
    6102             : 
    6103       30237 :   name_space = ORIGINAL_NAMESPACE (name_space);
    6104             : 
    6105             :   /* Build the alias.  */
    6106       30237 :   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
    6107       30237 :   DECL_NAMESPACE_ALIAS (alias) = name_space;
    6108       30237 :   DECL_EXTERNAL (alias) = 1;
    6109       30237 :   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
    6110       30237 :   set_originating_module (alias);
    6111             : 
    6112       30237 :   pushdecl (alias);
    6113             : 
    6114             :   /* Emit debug info for namespace alias.  */
    6115       30237 :   if (!building_stmt_list_p ())
    6116        2423 :     (*debug_hooks->early_global_decl) (alias);
    6117             : }
    6118             : 
    6119             : /* Like pushdecl, only it places DECL in the current namespace,
    6120             :    if appropriate.  */
    6121             : 
    6122             : tree
    6123    31644850 : pushdecl_namespace_level (tree decl, bool hiding)
    6124             : {
    6125    31644850 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    6126    31644850 :   return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
    6127    31644850 :                                  hiding);
    6128    31644850 : }
    6129             : 
    6130             : /* Wrapper around push_local_binding to push the bindings for
    6131             :    a non-member USING_DECL with NAME and VALUE.  LOOKUP, if non-null,
    6132             :    is the result of name lookup during template parsing.  */
    6133             : 
    6134             : static void
    6135      340960 : push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
    6136             : {
    6137      340960 :   tree type = NULL_TREE;
    6138             : 
    6139      340960 :   cxx_binding *binding = find_local_binding (current_binding_level, name);
    6140      340960 :   if (binding)
    6141             :     {
    6142         100 :       value = binding->value;
    6143         100 :       type = binding->type;
    6144             :     }
    6145             : 
    6146             :   /* DR 36 questions why using-decls at function scope may not be
    6147             :      duplicates.  Disallow it, as C++11 claimed and PR 20420
    6148             :      implemented.  */
    6149      340960 :   if (lookup)
    6150      281896 :     do_nonmember_using_decl (*lookup, true, true, &value, &type);
    6151             : 
    6152      340960 :   if (!value)
    6153             :     ;
    6154      340960 :   else if (binding && value == binding->value)
    6155             :     /* Redeclaration of this USING_DECL.  */;
    6156          52 :   else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
    6157             :     {
    6158             :       /* We already have this binding, so replace it.  */
    6159          48 :       update_local_overload (IDENTIFIER_BINDING (name), value);
    6160          48 :       IDENTIFIER_BINDING (name)->value = value;
    6161             :     }
    6162             :   else
    6163             :     /* Install the new binding.  */
    6164      340864 :     push_local_binding (name, value, /*using=*/true);
    6165             : 
    6166      340960 :   if (!type)
    6167             :     ;
    6168          29 :   else if (binding && type == binding->type)
    6169             :     ;
    6170             :   else
    6171             :     {
    6172          21 :       push_local_binding (name, type, /*using=*/true);
    6173          21 :       set_identifier_type_value (name, type);
    6174             :     }
    6175      340960 : }
    6176             : 
    6177             : /* Overload for push_using_decl_bindings that doesn't take a name_lookup.  */
    6178             : 
    6179             : void
    6180       59064 : push_using_decl_bindings (tree name, tree value)
    6181             : {
    6182       59064 :   push_using_decl_bindings (nullptr, name, value);
    6183       59064 : }
    6184             : 
    6185             : /* Process a using declaration in non-class scope.  */
    6186             : 
    6187             : void
    6188     4201409 : finish_nonmember_using_decl (tree scope, tree name)
    6189             : {
    6190     4201409 :   gcc_checking_assert (current_binding_level->kind != sk_class);
    6191             : 
    6192     4201409 :   if (scope == error_mark_node || name == error_mark_node)
    6193          49 :     return;
    6194             : 
    6195     4201409 :   name_lookup lookup (name);
    6196             : 
    6197     4201409 :   tree using_decl = lookup_using_decl (scope, lookup);
    6198     4201409 :   if (!using_decl)
    6199          49 :     return;
    6200             : 
    6201             :   /* Emit debug info.  */
    6202     4201360 :   if (!processing_template_decl)
    6203     7855454 :     cp_emit_debug_info_for_using (lookup.value,
    6204     3927727 :                                   current_binding_level->this_entity);
    6205             : 
    6206     4201360 :   if (current_binding_level->kind == sk_namespace)
    6207             :     {
    6208     3919464 :       tree *slot = find_namespace_slot (current_namespace, name, true);
    6209     3919464 :       tree *mslot = get_fixed_binding_slot (slot, name,
    6210             :                                             BINDING_SLOT_CURRENT, true);
    6211     3919464 :       bool failed = false;
    6212             : 
    6213     3919464 :       if (mslot != slot)
    6214             :         {
    6215             :           /* A module vector.  I presume the binding list is going to
    6216             :              be sparser than the import bitmap.  Hence iterate over
    6217             :              the former checking for bits set in the bitmap.  */
    6218           6 :           bitmap imports = get_import_bitmap ();
    6219           6 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    6220             : 
    6221             :           /* Scan the imported bindings.  */
    6222           6 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    6223           6 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    6224             :             {
    6225           6 :               ix--;
    6226           6 :               cluster++;
    6227             :             }
    6228             : 
    6229             :           /* Do this in forward order, so we load modules in an order
    6230             :              the user expects.  */
    6231          12 :           for (; ix--; cluster++)
    6232          18 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    6233             :               {
    6234             :                 /* Are we importing this module?  */
    6235          12 :                 if (unsigned base = cluster->indices[jx].base)
    6236           6 :                   if (unsigned span = cluster->indices[jx].span)
    6237           6 :                     do
    6238           6 :                       if (bitmap_bit_p (imports, base))
    6239           6 :                         goto found;
    6240           0 :                     while (++base, --span);
    6241           6 :                 continue;
    6242             : 
    6243           6 :               found:;
    6244             :                 /* Is it loaded?  */
    6245           6 :                 if (cluster->slots[jx].is_lazy ())
    6246             :                   {
    6247           0 :                     gcc_assert (cluster->indices[jx].span == 1);
    6248           0 :                     lazy_load_binding (cluster->indices[jx].base,
    6249             :                                        scope, name, &cluster->slots[jx]);
    6250             :                   }
    6251             : 
    6252           6 :                 tree value = cluster->slots[jx];
    6253           6 :                 if (!value)
    6254             :                   /* Load errors could mean there's nothing here.  */
    6255           0 :                   continue;
    6256             : 
    6257             :                 /* Extract what we can see from here.  If there's no
    6258             :                    stat_hack, then everything was exported.  */
    6259           6 :                 tree type = NULL_TREE;
    6260             : 
    6261             :                 /* If no stat hack, everything is visible.  */
    6262           6 :                 if (STAT_HACK_P (value))
    6263             :                   {
    6264           0 :                     if (STAT_TYPE_VISIBLE_P (value))
    6265           0 :                       type = STAT_TYPE (value);
    6266           0 :                     value = STAT_VISIBLE (value);
    6267             :                   }
    6268             : 
    6269           6 :                 if (do_nonmember_using_decl (lookup, false, false,
    6270             :                                              &value, &type))
    6271             :                   {
    6272           0 :                     failed = true;
    6273           0 :                     break;
    6274             :                   }
    6275           6 :               }
    6276             :         }
    6277             : 
    6278           6 :       if (!failed)
    6279             :         {
    6280             :           /* Now do the current slot.  */
    6281     3919464 :           tree value = MAYBE_STAT_DECL (*mslot);
    6282     3919464 :           tree type = MAYBE_STAT_TYPE (*mslot);
    6283             : 
    6284     3919464 :           do_nonmember_using_decl (lookup, false, true, &value, &type);
    6285             : 
    6286             :           // FIXME: Partition mergeableness?
    6287     3919464 :           if (STAT_HACK_P (*mslot))
    6288             :             {
    6289           0 :               STAT_DECL (*mslot) = value;
    6290           0 :               STAT_TYPE (*mslot) = type;
    6291             :             }
    6292     3919464 :           else if (type)
    6293          22 :             *mslot = stat_hack (value, type);
    6294             :           else
    6295     3919442 :             *mslot = value;
    6296             :         }
    6297             :     }
    6298             :   else
    6299             :     {
    6300      281896 :       add_decl_expr (using_decl);
    6301      281896 :       if (DECL_DEPENDENT_P (using_decl))
    6302           3 :         lookup.value = using_decl;
    6303      281896 :       push_using_decl_bindings (&lookup, name, NULL_TREE);
    6304             :     }
    6305     4201409 : }
    6306             : 
    6307             : /* Return the declarations that are members of the namespace NS.  */
    6308             : 
    6309             : tree
    6310          12 : cp_namespace_decls (tree ns)
    6311             : {
    6312          12 :   return NAMESPACE_LEVEL (ns)->names;
    6313             : }
    6314             : 
    6315             : /* Given a lookup that returned VAL, use FLAGS to decide if we want to
    6316             :    ignore it or not.  Subroutine of lookup_name_1 and lookup_type_scope.  */
    6317             : 
    6318             : static bool
    6319  1992267980 : qualify_lookup (tree val, LOOK_want want)
    6320             : {
    6321  1992267980 :   if (val == NULL_TREE)
    6322             :     return false;
    6323             : 
    6324  1992267980 :   if (bool (want & LOOK_want::TYPE))
    6325             :     {
    6326    27129942 :       tree target_val = strip_using_decl (val);
    6327             : 
    6328    27129942 :       if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
    6329             :         return true;
    6330             :     }
    6331             : 
    6332  1965290158 :   if (bool (want & LOOK_want::TYPE_NAMESPACE))
    6333      249318 :     return TREE_CODE (val) == NAMESPACE_DECL;
    6334             : 
    6335             :   return true;
    6336             : }
    6337             : 
    6338             : /* Is there a "using namespace std;" directive within USINGS?  */
    6339             : 
    6340             : static bool
    6341        5878 : using_directives_contain_std_p (vec<tree, va_gc> *usings)
    6342             : {
    6343        5878 :   if (!usings)
    6344             :     return false;
    6345             : 
    6346          53 :   for (unsigned ix = usings->length (); ix--;)
    6347          37 :     if ((*usings)[ix] == std_node)
    6348             :       return true;
    6349             : 
    6350             :   return false;
    6351             : }
    6352             : 
    6353             : /* Is there a "using namespace std;" directive within the current
    6354             :    namespace (or its ancestors)?
    6355             :    Compare with name_lookup::search_unqualified.  */
    6356             : 
    6357             : static bool
    6358        1857 : has_using_namespace_std_directive_p ()
    6359             : {
    6360        1857 :   for (cp_binding_level *level = current_binding_level;
    6361        7714 :        level;
    6362        5857 :        level = level->level_chain)
    6363        5878 :     if (using_directives_contain_std_p (level->using_directives))
    6364             :       return true;
    6365             : 
    6366             :   return false;
    6367             : }
    6368             : 
    6369             : /* Subclass of deferred_diagnostic, for issuing a note when
    6370             :    --param cxx-max-namespaces-for-diagnostic-help is reached.
    6371             : 
    6372             :    The note should be issued after the error, but before any other
    6373             :    deferred diagnostics.  This is handled by decorating a wrapped
    6374             :    deferred_diagnostic, and emitting a note before that wrapped note is
    6375             :    deleted.  */
    6376             : 
    6377             : class namespace_limit_reached : public deferred_diagnostic
    6378             : {
    6379             :  public:
    6380           3 :   namespace_limit_reached (location_t loc, unsigned limit, tree name,
    6381             :                            std::unique_ptr<deferred_diagnostic> wrapped)
    6382           3 :   : deferred_diagnostic (loc),
    6383           3 :     m_limit (limit), m_name (name),
    6384           3 :     m_wrapped (std::move (wrapped))
    6385             :   {
    6386             :   }
    6387             : 
    6388           6 :   ~namespace_limit_reached ()
    6389           3 :   {
    6390             :     /* Unconditionally warn that the search was truncated.  */
    6391           3 :     inform (get_location (),
    6392             :             "maximum limit of %d namespaces searched for %qE",
    6393             :             m_limit, m_name);
    6394             :     /* m_wrapped will be implicitly deleted after this, emitting any followup
    6395             :        diagnostic after the above note.  */
    6396           6 :   }
    6397             : 
    6398             :  private:
    6399             :   unsigned m_limit;
    6400             :   tree m_name;
    6401             :   std::unique_ptr<deferred_diagnostic> m_wrapped;
    6402             : };
    6403             : 
    6404             : /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
    6405             :    Emit a note showing the location of the declaration of the suggestion.  */
    6406             : 
    6407             : class show_candidate_location : public deferred_diagnostic
    6408             : {
    6409             :  public:
    6410         129 :   show_candidate_location (location_t loc, tree candidate)
    6411         129 :   : deferred_diagnostic (loc),
    6412         129 :     m_candidate (candidate)
    6413             :   {
    6414             :   }
    6415             : 
    6416         258 :   ~show_candidate_location ()
    6417         129 :   {
    6418         129 :     inform (location_of (m_candidate), "%qE declared here", m_candidate);
    6419         258 :   }
    6420             : 
    6421             :  private:
    6422             :   tree m_candidate;
    6423             : };
    6424             : 
    6425             : /* Subclass of deferred_diagnostic, for use when there are multiple candidates
    6426             :    to be suggested by suggest_alternatives_for.
    6427             : 
    6428             :    Emit a series of notes showing the various suggestions.  */
    6429             : 
    6430             : class suggest_alternatives : public deferred_diagnostic
    6431             : {
    6432             :  public:
    6433          22 :   suggest_alternatives (location_t loc, vec<tree> candidates)
    6434          22 :   : deferred_diagnostic (loc),
    6435          22 :     m_candidates (candidates)
    6436             :   {
    6437             :   }
    6438             : 
    6439          44 :   ~suggest_alternatives ()
    6440          22 :   {
    6441          22 :     if (m_candidates.length ())
    6442             :       {
    6443          22 :         inform_n (get_location (), m_candidates.length (),
    6444             :                   "suggested alternative:",
    6445             :                   "suggested alternatives:");
    6446         140 :         for (unsigned ix = 0; ix != m_candidates.length (); ix++)
    6447             :           {
    6448          48 :             tree val = m_candidates[ix];
    6449             : 
    6450          48 :             inform (location_of (val), "  %qE", val);
    6451             :           }
    6452             :       }
    6453          22 :     m_candidates.release ();
    6454          44 :   }
    6455             : 
    6456             :  private:
    6457             :   vec<tree> m_candidates;
    6458             : };
    6459             : 
    6460             : /* A class for encapsulating the result of a search across
    6461             :    multiple namespaces (and scoped enums within them) for an
    6462             :    unrecognized name seen at a given source location.  */
    6463             : 
    6464             : class namespace_hints
    6465             : {
    6466             :  public:
    6467             :   namespace_hints (location_t loc, tree name);
    6468             : 
    6469             :   name_hint convert_candidates_to_name_hint ();
    6470             :   name_hint maybe_decorate_with_limit (name_hint);
    6471             : 
    6472             :  private:
    6473             :   void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
    6474             : 
    6475             :   location_t m_loc;
    6476             :   tree m_name;
    6477             :   vec<tree> m_candidates;
    6478             : 
    6479             :   /* Value of "--param cxx-max-namespaces-for-diagnostic-help".  */
    6480             :   unsigned m_limit;
    6481             : 
    6482             :   /* Was the limit reached?  */
    6483             :   bool m_limited;
    6484             : };
    6485             : 
    6486             : /* Constructor for namespace_hints.  Search namespaces and scoped enums,
    6487             :    looking for an exact match for unrecognized NAME seen at LOC.  */
    6488             : 
    6489        2031 : namespace_hints::namespace_hints (location_t loc, tree name)
    6490        2031 : : m_loc(loc), m_name (name)
    6491             : {
    6492        2031 :   auto_vec<tree> worklist;
    6493             : 
    6494        2031 :   m_candidates = vNULL;
    6495        2031 :   m_limited = false;
    6496        2031 :   m_limit = param_cxx_max_namespaces_for_diagnostic_help;
    6497             : 
    6498             :   /* Breadth-first search of namespaces.  Up to limit namespaces
    6499             :      searched (limit zero == unlimited).  */
    6500        2031 :   worklist.safe_push (global_namespace);
    6501       17718 :   for (unsigned ix = 0; ix != worklist.length (); ix++)
    6502             :     {
    6503        6828 :       tree ns = worklist[ix];
    6504        6828 :       name_lookup lookup (name);
    6505             : 
    6506        6828 :       if (lookup.search_qualified (ns, false))
    6507         162 :         m_candidates.safe_push (lookup.value);
    6508             : 
    6509        6828 :       if (!m_limited)
    6510             :         {
    6511             :           /* Look for child namespaces.  We have to do this
    6512             :              indirectly because they are chained in reverse order,
    6513             :              which is confusing to the user.  */
    6514        6816 :           auto_vec<tree> children;
    6515             : 
    6516        6816 :           for (tree decl = NAMESPACE_LEVEL (ns)->names;
    6517     5179112 :                decl; decl = TREE_CHAIN (decl))
    6518             :             {
    6519     5172296 :               if (TREE_CODE (decl) == NAMESPACE_DECL
    6520        4915 :                   && !DECL_NAMESPACE_ALIAS (decl)
    6521     5177203 :                   && !DECL_NAMESPACE_INLINE_P (decl))
    6522        4806 :                 children.safe_push (decl);
    6523             : 
    6524             :               /* Look for exact matches for NAME within scoped enums.
    6525             :                  These aren't added to the worklist, and so don't count
    6526             :                  against the search limit.  */
    6527     5172296 :               if (TREE_CODE (decl) == TYPE_DECL)
    6528             :                 {
    6529       59093 :                   tree type = TREE_TYPE (decl);
    6530       59093 :                   if (SCOPED_ENUM_P (type))
    6531        1194 :                     maybe_add_candidate_for_scoped_enum (type, name);
    6532             :                 }
    6533             :             }
    6534             : 
    6535       18429 :           while (!m_limited && !children.is_empty ())
    6536             :             {
    6537        9600 :               if (worklist.length () == m_limit)
    6538           3 :                 m_limited = true;
    6539             :               else
    6540        4797 :                 worklist.safe_push (children.pop ());
    6541             :             }
    6542        6816 :         }
    6543        6828 :     }
    6544        2031 : }
    6545             : 
    6546             : /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
    6547             :    for m_name, an IDENTIFIER_NODE for which name lookup failed.
    6548             : 
    6549             :    If m_candidates is non-empty, use it to generate a suggestion and/or
    6550             :    a deferred diagnostic that lists the possible candidate(s).
    6551             : */
    6552             : 
    6553             : name_hint
    6554        2031 : namespace_hints::convert_candidates_to_name_hint ()
    6555             : {
    6556             :   /* How many candidates do we have?  */
    6557             : 
    6558             :   /* If we have just one candidate, issue a name_hint with it as a suggestion
    6559             :      (so that consumers are able to suggest it within the error message and emit
    6560             :      it as a fix-it hint), and with a note showing the candidate's location.  */
    6561        2031 :   if (m_candidates.length () == 1)
    6562             :     {
    6563         129 :       tree candidate = m_candidates[0];
    6564             :       /* Clean up CANDIDATES.  */
    6565         129 :       m_candidates.release ();
    6566         129 :       return name_hint (expr_to_string (candidate),
    6567         129 :                         new show_candidate_location (m_loc, candidate));
    6568             :     }
    6569        1902 :   else if (m_candidates.length () > 1)
    6570             :     /* If we have more than one candidate, issue a name_hint without a single
    6571             :        "suggestion", but with a deferred diagnostic that lists the
    6572             :        various candidates.  This takes ownership of m_candidates.  */
    6573          22 :     return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
    6574             : 
    6575             :   /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary.  */
    6576        1880 :   gcc_assert (m_candidates.length () == 0);
    6577        1880 :   gcc_assert (m_candidates == vNULL);
    6578             : 
    6579        1880 :   return name_hint ();
    6580             : }
    6581             : 
    6582             : /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
    6583             :    then we want to emit a note about after the error, but before
    6584             :    any other deferred diagnostics.
    6585             : 
    6586             :    Handle this by figuring out what hint is needed, then optionally
    6587             :    decorating HINT with a namespace_limit_reached wrapper.  */
    6588             : 
    6589             : name_hint
    6590        2031 : namespace_hints::maybe_decorate_with_limit (name_hint hint)
    6591             : {
    6592        2031 :   if (m_limited)
    6593           3 :     return name_hint (hint.suggestion (),
    6594             :                       new namespace_limit_reached (m_loc, m_limit,
    6595             :                                                    m_name,
    6596           3 :                                                    hint.take_deferred ()));
    6597             :   else
    6598        2028 :     return hint;
    6599             : }
    6600             : 
    6601             : /* Look inside SCOPED_ENUM for exact matches for NAME.
    6602             :    If one is found, add its CONST_DECL to m_candidates.  */
    6603             : 
    6604             : void
    6605        1194 : namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
    6606             :                                                       tree name)
    6607             : {
    6608        1194 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    6609             : 
    6610        2172 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    6611             :     {
    6612         993 :       tree id = TREE_PURPOSE (iter);
    6613         993 :       if (id == name)
    6614             :         {
    6615          15 :           m_candidates.safe_push (TREE_VALUE (iter));
    6616          15 :           return;
    6617             :         }
    6618             :     }
    6619             : }
    6620             : 
    6621             : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    6622             :    name lookup failed.
    6623             : 
    6624             :    Search through all available namespaces and any scoped enums within them
    6625             :    and generate a suggestion and/or a deferred diagnostic that lists possible
    6626             :    candidate(s).
    6627             : 
    6628             :    If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
    6629             :    look for near-matches and suggest the best near-match, if there is one.
    6630             : 
    6631             :    If nothing is found, then an empty name_hint is returned.  */
    6632             : 
    6633             : name_hint
    6634        1981 : suggest_alternatives_for (location_t location, tree name,
    6635             :                           bool suggest_misspellings)
    6636             : {
    6637             :   /* First, search for exact matches in other namespaces.  */
    6638        1981 :   namespace_hints ns_hints (location, name);
    6639        1981 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    6640             : 
    6641             :   /* Otherwise, try other approaches.  */
    6642        1981 :   if (!result)
    6643        1857 :     result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
    6644             : 
    6645        1981 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    6646        1981 : }
    6647             : 
    6648             : /* The second half of suggest_alternatives_for, for when no exact matches
    6649             :    were found in other namespaces.  */
    6650             : 
    6651             : static name_hint
    6652        1857 : suggest_alternatives_for_1 (location_t location, tree name,
    6653             :                             bool suggest_misspellings)
    6654             : {
    6655             :   /* No candidates were found in the available namespaces.  */
    6656             : 
    6657             :   /* If there's a "using namespace std;" active, and this
    6658             :      is one of the most common "std::" names, then it's probably a
    6659             :      missing #include.  */
    6660        1857 :   if (has_using_namespace_std_directive_p ())
    6661             :     {
    6662          21 :       name_hint hint = maybe_suggest_missing_std_header (location, name);
    6663          21 :       if (hint)
    6664          21 :         return hint;
    6665          21 :     }
    6666             : 
    6667             :   /* Otherwise, consider misspellings.  */
    6668        1836 :   if (!suggest_misspellings)
    6669           0 :     return name_hint ();
    6670             : 
    6671        1836 :   return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
    6672             : }
    6673             : 
    6674             : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
    6675             :    name lookup failed.
    6676             : 
    6677             :    Search through all available namespaces and generate a suggestion and/or
    6678             :    a deferred diagnostic that lists possible candidate(s).
    6679             : 
    6680             :    This is similiar to suggest_alternatives_for, but doesn't fallback to
    6681             :    the other approaches used by that function.  */
    6682             : 
    6683             : name_hint
    6684          50 : suggest_alternatives_in_other_namespaces (location_t location, tree name)
    6685             : {
    6686          50 :   namespace_hints ns_hints (location, name);
    6687             : 
    6688          50 :   name_hint result = ns_hints.convert_candidates_to_name_hint ();
    6689             : 
    6690          50 :   return ns_hints.maybe_decorate_with_limit (std::move (result));
    6691          50 : }
    6692             : 
    6693             : /* A well-known name within the C++ standard library, returned by
    6694             :    get_std_name_hint.
    6695             : 
    6696             :    The gperf-generated file contains the definition of the class
    6697             :    "std_name_hint_lookup" with a static member function which
    6698             :    returns the pointer to a structure "std_name_hint" which
    6699             :    is also defined in that file.  */
    6700             : 
    6701             : #include "std-name-hint.h"
    6702             : 
    6703             : /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
    6704             :    for some of the most common names within "std::".
    6705             :    Given non-NULL NAME, return the std_name_hint for it, or NULL.  */
    6706             : 
    6707             : static const std_name_hint *
    6708         185 : get_std_name_hint (const char *name)
    6709             : {
    6710         185 :   return std_name_hint_lookup::find(name, strlen(name));
    6711             : }
    6712             : 
    6713             : /* Describe DIALECT.  */
    6714             : 
    6715             : const char *
    6716        2825 : get_cxx_dialect_name (enum cxx_dialect dialect)
    6717             : {
    6718        2825 :   switch (dialect)
    6719             :     {
    6720           0 :     default:
    6721           0 :       gcc_unreachable ();
    6722             :     case cxx98:
    6723             :       return "C++98";
    6724             :     case cxx11:
    6725             :       return "C++11";
    6726             :     case cxx14:
    6727             :       return "C++14";
    6728             :     case cxx17:
    6729             :       return "C++17";
    6730             :     case cxx20:
    6731             :       return "C++20";
    6732             :     case cxx23:
    6733             :       return "C++23";
    6734             :     case cxx26:
    6735             :       return "C++26";
    6736             :     }
    6737             : }
    6738             : 
    6739             : /* Subclass of deferred_diagnostic for use for names in the "std" namespace
    6740             :    that weren't recognized, but for which we know which header it ought to be
    6741             :    in.
    6742             : 
    6743             :    Emit a note either suggesting the header to be included, or noting that
    6744             :    the current dialect is too early for the given name.  */
    6745             : 
    6746             : class missing_std_header : public deferred_diagnostic
    6747             : {
    6748             :  public:
    6749         163 :   missing_std_header (location_t loc,
    6750             :                       const char *name_str,
    6751             :                       const std_name_hint *header_hint)
    6752         163 :   : deferred_diagnostic (loc),
    6753         163 :     m_name_str (name_str),
    6754         163 :     m_header_hint (header_hint)
    6755             :   {}
    6756         326 :   ~missing_std_header ()
    6757         163 :   {
    6758         163 :     gcc_rich_location richloc (get_location ());
    6759         163 :     if (cxx_dialect >= m_header_hint->min_dialect)
    6760             :       {
    6761         154 :         const char *header = m_header_hint->header;
    6762         154 :         maybe_add_include_fixit (&richloc, header, true);
    6763         154 :         inform (&richloc,
    6764             :                 "%<std::%s%> is defined in header %qs;"
    6765             :                 " this is probably fixable by adding %<#include %s%>",
    6766             :                 m_name_str, header, header);
    6767             :       }
    6768             :     else
    6769           9 :       inform (&richloc,
    6770             :               "%<std::%s%> is only available from %s onwards",
    6771             :               m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
    6772         326 :   }
    6773             : 
    6774             : private:
    6775             :   const char *m_name_str;
    6776             :   const std_name_hint *m_header_hint;
    6777             : };
    6778             : 
    6779             : /* Attempt to generate a name_hint that suggests pertinent header files
    6780             :    for NAME at LOCATION, for common names within the "std" namespace,
    6781             :    or an empty name_hint if this isn't applicable.  */
    6782             : 
    6783             : static name_hint
    6784         185 : maybe_suggest_missing_std_header (location_t location, tree name)
    6785             : {
    6786         185 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    6787             : 
    6788         185 :   const char *name_str = IDENTIFIER_POINTER (name);
    6789         185 :   const std_name_hint *header_hint = get_std_name_hint (name_str);
    6790         185 :   if (!header_hint)
    6791          22 :     return name_hint ();
    6792             : 
    6793         163 :   return name_hint (NULL, new missing_std_header (location, name_str,
    6794         163 :                                                   header_hint));
    6795             : }
    6796             : 
    6797             : /* Attempt to generate a name_hint that suggests a missing header file
    6798             :    for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
    6799             :    applicable.  */
    6800             : 
    6801             : name_hint
    6802         508 : maybe_suggest_missing_header (location_t location, tree name, tree scope)
    6803             : {
    6804         508 :   if (scope == NULL_TREE)
    6805           0 :     return name_hint ();
    6806         508 :   if (TREE_CODE (scope) != NAMESPACE_DECL)
    6807          43 :     return name_hint ();
    6808             :   /* We only offer suggestions for the "std" namespace.  */
    6809         465 :   if (scope != std_node)
    6810         301 :     return name_hint ();
    6811         164 :   return maybe_suggest_missing_std_header (location, name);
    6812             : }
    6813             : 
    6814             : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
    6815             :    lookup failed within the explicitly provided SCOPE.
    6816             : 
    6817             :    Suggest the best meaningful candidates (if any), otherwise
    6818             :    an empty name_hint is returned.  */
    6819             : 
    6820             : name_hint
    6821         299 : suggest_alternative_in_explicit_scope (location_t location, tree name,
    6822             :                                        tree scope)
    6823             : {
    6824             :   /* Something went very wrong; don't suggest anything.  */
    6825         299 :   if (name == error_mark_node)
    6826           3 :     return name_hint ();
    6827             : 
    6828             :   /* Resolve any namespace aliases.  */
    6829         296 :   scope = ORIGINAL_NAMESPACE (scope);
    6830             : 
    6831         296 :   name_hint hint = maybe_suggest_missing_header (location, name, scope);
    6832         296 :   if (hint)
    6833         127 :     return hint;
    6834             : 
    6835         169 :   cp_binding_level *level = NAMESPACE_LEVEL (scope);
    6836             : 
    6837         169 :   best_match <tree, const char *> bm (name);
    6838         169 :   consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
    6839             : 
    6840             :   /* See if we have a good suggesion for the user.  */
    6841         169 :   const char *fuzzy_name = bm.get_best_meaningful_candidate ();
    6842         169 :   if (fuzzy_name)
    6843          53 :     return name_hint (fuzzy_name, NULL);
    6844             : 
    6845         296 :   return name_hint ();
    6846         299 : }
    6847             : 
    6848             : /* Given NAME, look within SCOPED_ENUM for possible spell-correction
    6849             :    candidates.  */
    6850             : 
    6851             : name_hint
    6852          15 : suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
    6853             : {
    6854          15 :   gcc_assert (SCOPED_ENUM_P (scoped_enum));
    6855             : 
    6856          15 :   best_match <tree, const char *> bm (name);
    6857          48 :   for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
    6858             :     {
    6859          33 :       tree id = TREE_PURPOSE (iter);
    6860          33 :       bm.consider (IDENTIFIER_POINTER (id));
    6861             :     }
    6862          15 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    6863             : }
    6864             : 
    6865             : /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
    6866             :    or a class TYPE).
    6867             : 
    6868             :    WANT as for lookup_name_1.
    6869             : 
    6870             :    Returns a DECL (or OVERLOAD, or BASELINK) representing the
    6871             :    declaration found.  If no suitable declaration can be found,
    6872             :    ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
    6873             :    neither a class-type nor a namespace a diagnostic is issued.  */
    6874             : 
    6875             : tree
    6876   228216087 : lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
    6877             : {
    6878   228216087 :   tree t = NULL_TREE;
    6879             : 
    6880   228216087 :   if (TREE_CODE (scope) == NAMESPACE_DECL)
    6881             :     {
    6882   164437169 :       name_lookup lookup (name, want);
    6883             : 
    6884   164437169 :       if (qualified_namespace_lookup (scope, &lookup))
    6885             :         {
    6886   164265063 :           t = lookup.value;
    6887             : 
    6888             :           /* If we have a known type overload, pull it out.  This can happen
    6889             :              for using decls.  */
    6890   164265063 :           if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
    6891     1074431 :             t = OVL_FUNCTION (t);
    6892             :         }
    6893   164437169 :     }
    6894    63778918 :   else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
    6895     3763552 :     t = lookup_enumerator (scope, name);
    6896    60015366 :   else if (is_class_type (scope, complain))
    6897    59974763 :     t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
    6898             :                        tf_warning_or_error);
    6899             : 
    6900   228175420 :   if (!t)
    6901      222643 :     return error_mark_node;
    6902             :   return t;
    6903             : }
    6904             : 
    6905             : /* Wrapper for the above that takes a string argument.  The function name is
    6906             :    not at the beginning of the line to keep this wrapper out of etags.  */
    6907             : 
    6908      119962 : tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
    6909             : {
    6910      119962 :   return lookup_qualified_name (t, get_identifier (p), w, c);
    6911             : }
    6912             : 
    6913             : /* [namespace.qual]
    6914             :    Accepts the NAME to lookup and its qualifying SCOPE.
    6915             :    Returns the name/type pair found into the cxx_binding *RESULT,
    6916             :    or false on error.  */
    6917             : 
    6918             : static bool
    6919   168602034 : qualified_namespace_lookup (tree scope, name_lookup *lookup)
    6920             : {
    6921   168602034 :   timevar_start (TV_NAME_LOOKUP);
    6922   168602034 :   query_oracle (lookup->name);
    6923   168602034 :   bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
    6924   168602034 :   timevar_stop (TV_NAME_LOOKUP);
    6925   168602034 :   return found;
    6926             : }
    6927             : 
    6928             : /* If DECL is suitably visible to the user, consider its name for
    6929             :    spelling correction.  */
    6930             : 
    6931             : static void
    6932        2151 : consider_decl (tree decl,  best_match <tree, const char *> &bm,
    6933             :                bool consider_impl_names)
    6934             : {
    6935             :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    6936             :      within range for).  */
    6937        2151 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    6938             :     return;
    6939             : 
    6940        2084 :   tree suggestion = DECL_NAME (decl);
    6941        2084 :   if (!suggestion)
    6942             :     return;
    6943             : 
    6944             :   /* Don't suggest names that are for anonymous aggregate types, as
    6945             :      they are an implementation detail generated by the compiler.  */
    6946        1949 :   if (IDENTIFIER_ANON_P (suggestion))
    6947             :     return;
    6948             : 
    6949        1859 :   const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
    6950             : 
    6951             :   /* Ignore internal names with spaces in them.  */
    6952        1859 :   if (strchr (suggestion_str, ' '))
    6953             :     return;
    6954             : 
    6955             :   /* Don't suggest names that are reserved for use by the
    6956             :      implementation, unless NAME began with an underscore.  */
    6957        1859 :   if (!consider_impl_names
    6958        1859 :       && name_reserved_for_implementation_p (suggestion_str))
    6959             :     return;
    6960             : 
    6961        1830 :   bm.consider (suggestion_str);
    6962             : }
    6963             : 
    6964             : /* If DECL is suitably visible to the user, add its name to VEC and
    6965             :    return true.  Otherwise return false.  */
    6966             : 
    6967             : static bool
    6968     3425263 : maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
    6969             : {
    6970             :   /* Skip compiler-generated variables (e.g. __for_begin/__for_end
    6971             :      within range for).  */
    6972     3425263 :   if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    6973             :     return false;
    6974             : 
    6975     3424164 :   tree suggestion = DECL_NAME (decl);
    6976     3424164 :   if (!suggestion)
    6977             :     return false;
    6978             : 
    6979             :   /* Don't suggest names that are for anonymous aggregate types, as
    6980             :      they are an implementation detail generated by the compiler.  */
    6981     3424164 :   if (IDENTIFIER_ANON_P (suggestion))
    6982             :     return false;
    6983             : 
    6984     3424164 :   vec.safe_push (suggestion);
    6985             : 
    6986     3424164 :   return true;
    6987             : }
    6988             : 
    6989             : /* Examing the namespace binding BINDING, and add at most one instance
    6990             :    of the name, if it contains a visible entity of interest.  Return
    6991             :    true if we added something.  */
    6992             : 
    6993             : bool
    6994     5391163 : maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
    6995             :                               lookup_name_fuzzy_kind kind)
    6996             : {
    6997     5391163 :   tree value = NULL_TREE;
    6998             : 
    6999     5391163 :   if (STAT_HACK_P (binding))
    7000             :     {
    7001         184 :       if (!STAT_TYPE_HIDDEN_P (binding)
    7002         184 :           && STAT_TYPE (binding))
    7003             :         {
    7004         106 :           if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
    7005             :             return true;
    7006             :         }
    7007          78 :       else if (!STAT_DECL_HIDDEN_P (binding))
    7008          42 :         value = STAT_DECL (binding);
    7009             :     }
    7010             :   else
    7011             :     value = binding;
    7012             : 
    7013     5391057 :   value = ovl_skip_hidden (value);
    7014     5391057 :   if (value)
    7015             :     {
    7016     4548542 :       value = OVL_FIRST (value);
    7017     4548542 :       if (kind != FUZZY_LOOKUP_TYPENAME
    7018     4548542 :           || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
    7019     3425157 :         if (maybe_add_fuzzy_decl (vec, value))
    7020             :           return true;
    7021             :     }
    7022             : 
    7023             :   /* Nothing found.  */
    7024             :   return false;
    7025             : }
    7026             : 
    7027             : /* Helper function for lookup_name_fuzzy.
    7028             :    Traverse binding level LVL, looking for good name matches for NAME
    7029             :    (and BM).  */
    7030             : static void
    7031        7163 : consider_binding_level (tree name, best_match <tree, const char *> &bm,
    7032             :                         cp_binding_level *lvl, bool look_within_fields,
    7033             :                         enum lookup_name_fuzzy_kind kind)
    7034             : {
    7035        7163 :   if (look_within_fields)
    7036        1036 :     if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
    7037             :       {
    7038         425 :         tree type = lvl->this_entity;
    7039         425 :         bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
    7040         425 :         tree best_matching_field
    7041         425 :           = lookup_member_fuzzy (type, name, want_type_p);
    7042         425 :         if (best_matching_field)
    7043           8 :           bm.consider (IDENTIFIER_POINTER (best_matching_field));
    7044             :       }
    7045             : 
    7046             :   /* Only suggest names reserved for the implementation if NAME begins
    7047             :      with an underscore.  */
    7048        7163 :   bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
    7049             : 
    7050        7163 :   if (lvl->kind != sk_namespace)
    7051        7060 :     for (tree t = lvl->names; t; t = TREE_CHAIN (t))
    7052             :       {
    7053        2544 :         tree d = t;
    7054             : 
    7055             :         /* OVERLOADs or decls from using declaration are wrapped into
    7056             :            TREE_LIST.  */
    7057        2544 :         if (TREE_CODE (d) == TREE_LIST)
    7058          36 :           d = OVL_FIRST (TREE_VALUE (d));
    7059             : 
    7060             :         /* Don't use bindings from implicitly declared functions,
    7061             :            as they were likely misspellings themselves.  */
    7062        2544 :         if (TREE_TYPE (d) == error_mark_node)
    7063         329 :           continue;
    7064             : 
    7065             :         /* If we want a typename, ignore non-types.  */
    7066        2279 :         if (kind == FUZZY_LOOKUP_TYPENAME
    7067        2215 :             && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
    7068          64 :           continue;
    7069             : 
    7070        2151 :         consider_decl (d, bm, consider_implementation_names);
    7071             :       }
    7072             :   else
    7073             :     {
    7074             :       /* We need to iterate over the namespace hash table, in order to
    7075             :          not mention hidden entities.  But hash table iteration is
    7076             :          (essentially) unpredictable, our correction-distance measure
    7077             :          is very granular, and we pick the first of equal distances.
    7078             :          Hence, we need to call the distance-measurer in a predictable
    7079             :          order.  So, iterate over the namespace hash, inserting
    7080             :          visible names into a vector.  Then sort the vector.  Then
    7081             :          determine spelling distance.  */
    7082             :       
    7083        2647 :       tree ns = lvl->this_entity;
    7084        2647 :       auto_vec<tree> vec;
    7085             : 
    7086        2647 :       hash_table<named_decl_hash>::iterator end
    7087        2647 :         (DECL_NAMESPACE_BINDINGS (ns)->end ());
    7088     5393822 :       for (hash_table<named_decl_hash>::iterator iter
    7089     5396469 :              (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
    7090             :         {
    7091     5391175 :           tree binding = *iter;
    7092             : 
    7093     5391175 :           if (TREE_CODE (binding) == BINDING_VECTOR)
    7094             :             {
    7095          99 :               bitmap imports = get_import_bitmap ();
    7096          99 :               binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
    7097             : 
    7098          99 :               if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
    7099          15 :                 if (maybe_add_fuzzy_binding (vec, bind, kind))
    7100           9 :                   continue;
    7101             : 
    7102             :               /* Scan the imported bindings.  */
    7103          90 :               unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
    7104          90 :               if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    7105             :                 {
    7106          90 :                   ix--;
    7107          90 :                   cluster++;
    7108             :                 }
    7109             : 
    7110         180 :               for (; ix--; cluster++)
    7111         162 :                 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
    7112             :                      jx++)
    7113             :                   {
    7114             :                     /* Are we importing this module?  */
    7115         135 :                     if (unsigned base = cluster->indices[jx].base)
    7116          90 :                       if (unsigned span = cluster->indices[jx].span)
    7117          90 :                         do
    7118          90 :                           if (bitmap_bit_p (imports, base))
    7119          87 :                             goto found;
    7120           3 :                         while (++base, --span);
    7121          48 :                     continue;
    7122             : 
    7123          87 :                   found:;
    7124             :                     /* Is it loaded?  */
    7125          87 :                     if (cluster->slots[jx].is_lazy ())
    7126             :                       /* Let's not read in everything on the first
    7127             :                          spello! **/
    7128          15 :                       continue;
    7129          72 :                     if (tree bind = cluster->slots[jx])
    7130          72 :                       if (maybe_add_fuzzy_binding (vec, bind, kind))
    7131             :                         break;
    7132          48 :                   }
    7133             :             }
    7134             :           else
    7135     5391076 :             maybe_add_fuzzy_binding (vec, binding, kind);
    7136             :         }
    7137             :         
    7138   179657252 :       vec.qsort ([] (const void *a_, const void *b_)
    7139             :                  {
    7140             :                    return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
    7141             :                                   IDENTIFIER_POINTER (*(const tree *)b_));
    7142             :                  });
    7143             : 
    7144             :       /* Examine longest to shortest.  */
    7145     3429458 :       for (unsigned ix = vec.length (); ix--;)
    7146             :         {
    7147     3424164 :           const char *str = IDENTIFIER_POINTER (vec[ix]);
    7148             : 
    7149             :           /* Ignore internal names with spaces in them.  */
    7150     3424164 :           if (strchr (str, ' '))
    7151       62590 :             continue;
    7152             :           
    7153             :           /* Don't suggest names that are reserved for use by the
    7154             :              implementation, unless NAME began with an underscore.  */
    7155     6436148 :           if (!consider_implementation_names
    7156     3361574 :               && name_reserved_for_implementation_p (str))
    7157     3074574 :             continue;
    7158             : 
    7159      287000 :           bm.consider (str);
    7160             :         }
    7161        2647 :     }
    7162        7163 : }
    7163             : 
    7164             : /* Subclass of deferred_diagnostic.  Notify the user that the
    7165             :    given macro was used before it was defined.
    7166             :    This can be done in the C++ frontend since tokenization happens
    7167             :    upfront.  */
    7168             : 
    7169             : class macro_use_before_def : public deferred_diagnostic
    7170             : {
    7171             :  public:
    7172             :   /* Factory function.  Return a new macro_use_before_def instance if
    7173             :      appropriate, or return NULL. */
    7174             :   static macro_use_before_def *
    7175          57 :   maybe_make (location_t use_loc, cpp_hashnode *macro)
    7176             :   {
    7177          85 :     location_t def_loc = cpp_macro_definition_location (macro);
    7178          57 :     if (def_loc == UNKNOWN_LOCATION)
    7179             :       return NULL;
    7180             : 
    7181             :     /* We only want to issue a note if the macro was used *before* it was
    7182             :        defined.
    7183             :        We don't want to issue a note for cases where a macro was incorrectly
    7184             :        used, leaving it unexpanded (e.g. by using the wrong argument
    7185             :        count).  */
    7186          57 :     if (!linemap_location_before_p (line_table, use_loc, def_loc))
    7187             :       return NULL;
    7188             : 
    7189          29 :     return new macro_use_before_def (use_loc, macro);
    7190             :   }
    7191             : 
    7192             :  private:
    7193             :   /* Ctor.  LOC is the location of the usage.  MACRO is the
    7194             :      macro that was used.  */
    7195          29 :   macro_use_before_def (location_t loc, cpp_hashnode *macro)
    7196          29 :   : deferred_diagnostic (loc), m_macro (macro)
    7197             :   {
    7198          29 :     gcc_assert (macro);
    7199          29 :   }
    7200             : 
    7201          58 :   ~macro_use_before_def ()
    7202          29 :   {
    7203          29 :     if (is_suppressed_p ())
    7204           0 :       return;
    7205             : 
    7206          29 :     inform (get_location (), "the macro %qs had not yet been defined",
    7207          29 :             (const char *)m_macro->ident.str);
    7208          58 :     inform (cpp_macro_definition_location (m_macro),
    7209             :             "it was later defined here");
    7210          58 :   }
    7211             : 
    7212             :  private:
    7213             :   cpp_hashnode *m_macro;
    7214             : };
    7215             : 
    7216             : /* Determine if it can ever make sense to offer RID as a suggestion for
    7217             :    a misspelling.
    7218             : 
    7219             :    Subroutine of lookup_name_fuzzy.  */
    7220             : 
    7221             : static bool
    7222      488064 : suggest_rid_p  (enum rid rid)
    7223             : {
    7224      488064 :   switch (rid)
    7225             :     {
    7226             :     /* Support suggesting function-like keywords.  */
    7227             :     case RID_STATIC_ASSERT:
    7228             :       return true;
    7229             : 
    7230      484128 :     default:
    7231             :       /* Support suggesting the various decl-specifier words, to handle
    7232             :          e.g. "singed" vs "signed" typos.  */
    7233      484128 :       if (cp_keyword_starts_decl_specifier_p (rid))
    7234             :         return true;
    7235             : 
    7236             :       /* Otherwise, don't offer it.  This avoids suggesting e.g. "if"
    7237             :          and "do" for short misspellings, which are likely to lead to
    7238             :          nonsensical results.  */
    7239             :       return false;
    7240             :     }
    7241             : }
    7242             : 
    7243             : /* Search for near-matches for NAME within the current bindings, and within
    7244             :    macro names, returning the best match as a const char *, or NULL if
    7245             :    no reasonable match is found.
    7246             : 
    7247             :    Use LOC for any deferred diagnostics.  */
    7248             : 
    7249             : name_hint
    7250        2329 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
    7251             : {
    7252        2329 :   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
    7253             : 
    7254             :   /* First, try some well-known names in the C++ standard library, in case
    7255             :      the user forgot a #include.  */
    7256        2329 :   const char *header_hint
    7257        2329 :     = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
    7258        2329 :   if (header_hint)
    7259         304 :     return name_hint (NULL,
    7260             :                       new suggest_missing_header (loc,
    7261         304 :                                                   IDENTIFIER_POINTER (name),
    7262         304 :                                                   header_hint));
    7263             : 
    7264        2025 :   best_match <tree, const char *> bm (name);
    7265             : 
    7266        2025 :   cp_binding_level *lvl;
    7267        3061 :   for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
    7268        1036 :     consider_binding_level (name, bm, lvl, true, kind);
    7269             : 
    7270        7983 :   for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
    7271        5958 :     consider_binding_level (name, bm, lvl, false, kind);
    7272             : 
    7273             :   /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
    7274             :      as:
    7275             :        x = SOME_OTHER_MACRO (y);
    7276             :      then "SOME_OTHER_MACRO" will survive to the frontend and show up
    7277             :      as a misspelled identifier.
    7278             : 
    7279             :      Use the best distance so far so that a candidate is only set if
    7280             :      a macro is better than anything so far.  This allows early rejection
    7281             :      (without calculating the edit distance) of macro names that must have
    7282             :      distance >= bm.get_best_distance (), and means that we only get a
    7283             :      non-NULL result for best_macro_match if it's better than any of
    7284             :      the identifiers already checked.  */
    7285        2025 :   best_macro_match bmm (name, bm.get_best_distance (), parse_in);
    7286        2025 :   cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
    7287             :   /* If a macro is the closest so far to NAME, consider it.  */
    7288        2025 :   if (best_macro)
    7289          32 :     bm.consider ((const char *)best_macro->ident.str);
    7290        1993 :   else if (bmm.get_best_distance () == 0)
    7291             :     {
    7292             :       /* If we have an exact match for a macro name, then either the
    7293             :          macro was used with the wrong argument count, or the macro
    7294             :          has been used before it was defined.  */
    7295          89 :       if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
    7296          61 :         if (cpp_user_macro_p (macro))
    7297          57 :           return name_hint (NULL,
    7298          57 :                             macro_use_before_def::maybe_make (loc, macro));
    7299             :     }
    7300             : 
    7301             :   /* Try the "starts_decl_specifier_p" keywords to detect
    7302             :      "singed" vs "signed" typos.  */
    7303      490032 :   for (unsigned i = 0; i < num_c_common_reswords; i++)
    7304             :     {
    7305      488064 :       const c_common_resword *resword = &c_common_reswords[i];
    7306             : 
    7307      488064 :       if (!suggest_rid_p (resword->rid))
    7308      354240 :         continue;
    7309             : 
    7310      133824 :       tree resword_identifier = ridpointers [resword->rid];
    7311      133824 :       if (!resword_identifier)
    7312           0 :         continue;
    7313      133824 :       gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
    7314             : 
    7315             :       /* Only consider reserved words that survived the
    7316             :          filtering in init_reswords (e.g. for -std).  */
    7317      133824 :       if (!IDENTIFIER_KEYWORD_P (resword_identifier))
    7318       13501 :         continue;
    7319             : 
    7320      120323 :       bm.consider (IDENTIFIER_POINTER (resword_identifier));
    7321             :     }
    7322             : 
    7323        1968 :   return name_hint (bm.get_best_meaningful_candidate (), NULL);
    7324             : }
    7325             : 
    7326             : /* Subroutine of outer_binding.
    7327             : 
    7328             :    Returns TRUE if BINDING is a binding to a template parameter of
    7329             :    SCOPE.  In that case SCOPE is the scope of a primary template
    7330             :    parameter -- in the sense of G++, i.e, a template that has its own
    7331             :    template header.
    7332             : 
    7333             :    Returns FALSE otherwise.  */
    7334             : 
    7335             : static bool
    7336   214244434 : binding_to_template_parms_of_scope_p (cxx_binding *binding,
    7337             :                                       cp_binding_level *scope)
    7338             : {
    7339   214244434 :   tree binding_value, tmpl, tinfo;
    7340   214244434 :   int level;
    7341             : 
    7342   214244434 :   if (!binding || !scope || !scope->this_entity)
    7343             :     return false;
    7344             : 
    7345   127966154 :   binding_value = binding->value ?  binding->value : binding->type;
    7346   127966154 :   tinfo = get_template_info (scope->this_entity);
    7347             : 
    7348             :   /* BINDING_VALUE must be a template parm.  */
    7349   127966154 :   if (binding_value == NULL_TREE
    7350   127966154 :       || (!DECL_P (binding_value)
    7351   127966154 :           || !DECL_TEMPLATE_PARM_P (binding_value)))
    7352             :     return false;
    7353             : 
    7354             :   /*  The level of BINDING_VALUE.  */
    7355   255932308 :   level =
    7356   127966154 :     template_type_parameter_p (binding_value)
    7357   127966154 :     ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
    7358             :                          (TREE_TYPE (binding_value)))
    7359    50346758 :     : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
    7360             : 
    7361             :   /* The template of the current scope, iff said scope is a primary
    7362             :      template.  */
    7363   444882531 :   tmpl = (tinfo
    7364   119351507 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
    7365   127966154 :           ? TI_TEMPLATE (tinfo)
    7366             :           : NULL_TREE);
    7367             : 
    7368             :   /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
    7369             :      then BINDING_VALUE is a parameter of TMPL.  */
    7370   102671943 :   return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
    7371             : }
    7372             : 
    7373             : /* Return the innermost non-namespace binding for NAME from a scope
    7374             :    containing BINDING, or, if BINDING is NULL, the current scope.
    7375             :    Please note that for a given template, the template parameters are
    7376             :    considered to be in the scope containing the current scope.
    7377             :    If CLASS_P is false, then class bindings are ignored.  */
    7378             : 
    7379             : cxx_binding *
    7380  3660642371 : outer_binding (tree name,
    7381             :                cxx_binding *binding,
    7382             :                bool class_p)
    7383             : {
    7384  3660642371 :   cxx_binding *outer;
    7385  3660642371 :   cp_binding_level *scope;
    7386  3660642371 :   cp_binding_level *outer_scope;
    7387             : 
    7388  3660642371 :   if (binding)
    7389             :     {
    7390     2568047 :       scope = binding->scope->level_chain;
    7391     2568047 :       outer = binding->previous;
    7392             :     }
    7393             :   else
    7394             :     {
    7395  3658074324 :       scope = current_binding_level;
    7396  3658074324 :       outer = IDENTIFIER_BINDING (name);
    7397             :     }
    7398  3660642371 :   outer_scope = outer ? outer->scope : NULL;
    7399             : 
    7400             :   /* Because we create class bindings lazily, we might be missing a
    7401             :      class binding for NAME.  If there are any class binding levels
    7402             :      between the LAST_BINDING_LEVEL and the scope in which OUTER was
    7403             :      declared, we must lookup NAME in those class scopes.  */
    7404  3660642371 :   if (class_p)
    7405  8320476124 :     while (scope && scope != outer_scope && scope->kind != sk_namespace)
    7406             :       {
    7407  6032587148 :         if (scope->kind == sk_class)
    7408             :           {
    7409   954420806 :             cxx_binding *class_binding;
    7410             : 
    7411   954420806 :             class_binding = get_class_binding (name, scope);
    7412   954420806 :             if (class_binding)
    7413             :               {
    7414             :                 /* Thread this new class-scope binding onto the
    7415             :                    IDENTIFIER_BINDING list so that future lookups
    7416             :                    find it quickly.  */
    7417    84445802 :                 if (BASELINK_P (class_binding->value))
    7418             :                   /* Don't put a BASELINK in IDENTIFIER_BINDING.  */
    7419    24698849 :                   class_binding->value
    7420    24698849 :                     = BASELINK_FUNCTIONS (class_binding->value);
    7421    84445802 :                 class_binding->previous = outer;
    7422    84445802 :                 if (binding)
    7423           3 :                   binding->previous = class_binding;
    7424             :                 else
    7425    84445799 :                   IDENTIFIER_BINDING (name) = class_binding;
    7426    84445802 :                 return class_binding;
    7427             :               }
    7428             :           }
    7429             :         /* If we are in a member template, the template parms of the member
    7430             :            template are considered to be inside the scope of the containing
    7431             :            class, but within G++ the class bindings are all pushed between the
    7432             :            template parms and the function body.  So if the outer binding is
    7433             :            a template parm for the current scope, return it now rather than
    7434             :            look for a class binding.  */
    7435  2525947220 :         if (outer_scope && outer_scope->kind == sk_template_parms
    7436  6162385780 :             && binding_to_template_parms_of_scope_p (outer, scope))
    7437             :           return outer;
    7438             : 
    7439  5853189689 :         scope = scope->level_chain;
    7440             :       }
    7441             : 
    7442             :   return outer;
    7443             : }
    7444             : 
    7445             : /* Return the innermost block-scope or class-scope value binding for
    7446             :    NAME, or NULL_TREE if there is no such binding.  */
    7447             : 
    7448             : tree
    7449   544674691 : innermost_non_namespace_value (tree name)
    7450             : {
    7451   544674691 :   cxx_binding *binding;
    7452   544674691 :   binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
    7453   544674691 :   return binding ? binding->value : NULL_TREE;
    7454             : }
    7455             : 
    7456             : /* True iff current_binding_level is within the potential scope of local
    7457             :    variable DECL. */
    7458             : 
    7459             : bool
    7460         106 : decl_in_scope_p (tree decl)
    7461             : {
    7462         106 :   gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
    7463             : 
    7464         106 :   tree name = DECL_NAME (decl);
    7465             : 
    7466         106 :   for (cxx_binding *iter = NULL;
    7467         114 :        (iter = outer_binding (name, iter, /*class_p=*/false)); )
    7468             :     {
    7469          72 :       if (!LOCAL_BINDING_P (iter))
    7470             :         return false;
    7471          72 :       if (iter->value == decl)
    7472             :         return true;
    7473             :     }
    7474             : 
    7475             :   return false;
    7476             : }
    7477             : 
    7478             : /* Look up NAME in the current binding level and its superiors in the
    7479             :    namespace of variables, functions and typedefs.  Return a ..._DECL
    7480             :    node of some kind representing its definition if there is only one
    7481             :    such declaration, or return a TREE_LIST with all the overloaded
    7482             :    definitions if there are many, or return NULL_TREE if it is undefined.
    7483             :    Hidden name, either friend declaration or built-in function, are
    7484             :    not ignored.
    7485             : 
    7486             :    WHERE controls which scopes are considered.  It is a bit mask of
    7487             :    LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
    7488             :    (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
    7489             :    scopes).  It is an error for no bits to be set.  These scopes are
    7490             :    searched from innermost to outermost.
    7491             : 
    7492             :    WANT controls what kind of entity we'd happy with.
    7493             :    LOOK_want::NORMAL for normal lookup (implicit typedefs can be
    7494             :    hidden).  LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
    7495             :    for only NAMESPACE_DECLS.  These two can be bit-ored to find
    7496             :    namespace or type.
    7497             : 
    7498             :    WANT can also have LOOK_want::HIDDEN_FRIEND or
    7499             :    LOOK_want::HIDDEN_LAMBDa added to it.  */
    7500             : 
    7501             : tree
    7502  2723822654 : lookup_name (tree name, LOOK_where where, LOOK_want want)
    7503             : {
    7504  2723822654 :   tree val = NULL_TREE;
    7505             : 
    7506  2723822654 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7507             : 
    7508  2723822654 :   gcc_checking_assert (unsigned (where) != 0);
    7509             :   /* If we're looking for hidden lambda things, we shouldn't be
    7510             :      looking in namespace scope.  */
    7511  2723822654 :   gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
    7512             :                        || !bool (where & LOOK_where::NAMESPACE));
    7513  2723822654 :   query_oracle (name);
    7514             : 
    7515             :   /* Conversion operators are handled specially because ordinary
    7516             :      unqualified name lookup will not find template conversion
    7517             :      operators.  */
    7518  2723822654 :   if (IDENTIFIER_CONV_OP_P (name))
    7519             :     {
    7520      312950 :       cp_binding_level *level;
    7521             : 
    7522      312950 :       for (level = current_binding_level;
    7523      845366 :            level && level->kind != sk_namespace;
    7524      532416 :            level = level->level_chain)
    7525             :         {
    7526      581887 :           tree class_type;
    7527      581887 :           tree operators;
    7528             : 
    7529             :           /* A conversion operator can only be declared in a class
    7530             :              scope.  */
    7531      581887 :           if (level->kind != sk_class)
    7532      230157 :             continue;
    7533             : 
    7534             :           /* Lookup the conversion operator in the class.  */
    7535      351730 :           class_type = level->this_entity;
    7536      351730 :           operators = lookup_fnfields (class_type, name, /*protect=*/0,
    7537             :                                        tf_warning_or_error);
    7538      351730 :           if (operators)
    7539       49471 :             return operators;
    7540             :         }
    7541             : 
    7542             :       return NULL_TREE;
    7543             :     }
    7544             : 
    7545             :   /* First, look in non-namespace scopes.  */
    7546             : 
    7547  2723509704 :   if (current_class_type == NULL_TREE)
    7548             :     /* Maybe avoid searching the binding stack at all.  */
    7549  1149166736 :     where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
    7550             : 
    7551  2723509704 :   if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
    7552             :     for (cxx_binding *iter = nullptr;
    7553  2725684029 :          (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
    7554             :       {
    7555             :         /* Skip entities we don't want.  */
    7556  2481599967 :         if (!bool (where & (LOCAL_BINDING_P (iter)
    7557             :                             ? LOOK_where::BLOCK : LOOK_where::CLASS)))
    7558        4964 :           continue;
    7559             : 
    7560             :         /* If this is the kind of thing we're looking for, we're done.  */
    7561  1992884755 :         if (iter->value)
    7562             :           {
    7563  1992884755 :             tree binding = NULL_TREE;
    7564             : 
    7565  1951524454 :             if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
    7566  1992884671 :                 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
    7567  1992850646 :                     || !is_lambda_ignored_entity (iter->value))
    7568  3983261203 :                 && qualify_lookup (iter->value, want))
    7569  1990321822 :               binding = iter->value;
    7570     2562933 :             else if (bool (want & LOOK_want::TYPE)
    7571       54602 :                      && !HIDDEN_TYPE_BINDING_P (iter)
    7572     2617515 :                      && iter->type)
    7573  1992884755 :               binding = iter->type;
    7574             : 
    7575  1992884755 :             if (binding)
    7576             :               {
    7577             :                 val = binding;
    7578             :                 break;
    7579             :               }
    7580             :           }
    7581             :       }
    7582             : 
    7583             :   /* Now lookup in namespace scopes.  */
    7584  2723509704 :   if (!val && bool (where & LOOK_where::NAMESPACE))
    7585             :     {
    7586   733187800 :       name_lookup lookup (name, want);
    7587   733187800 :       if (lookup.search_unqualified
    7588   733187800 :           (current_decl_namespace (), current_binding_level))
    7589   563678161 :         val = lookup.value;
    7590   733187800 :     }
    7591             : 
    7592             :   /* If we have a known type overload, pull it out.  This can happen
    7593             :      for both using decls and unhidden functions.  */
    7594  2723509704 :   if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
    7595     2044105 :     val = OVL_FUNCTION (val);
    7596             : 
    7597             :   return val;
    7598  2723822654 : }
    7599             : 
    7600             : tree
    7601   187626121 : lookup_name (tree name)
    7602             : {
    7603   187626121 :   return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
    7604             : }
    7605             : 
    7606             : /* Look up NAME for type used in elaborated name specifier in
    7607             :    the scopes given by HOW.
    7608             : 
    7609             :    Unlike lookup_name_1, we make sure that NAME is actually
    7610             :    declared in the desired scope, not from inheritance, nor using
    7611             :    directive.  For using declaration, there is DR138 still waiting
    7612             :    to be resolved.  Hidden name coming from an earlier friend
    7613             :    declaration is also returned, and will be made visible unless HOW
    7614             :    is TAG_how::HIDDEN_FRIEND.
    7615             : 
    7616             :    A TYPE_DECL best matching the NAME is returned.  Catching error
    7617             :    and issuing diagnostics are caller's responsibility.  */
    7618             : 
    7619             : tree
    7620    13789237 : lookup_elaborated_type (tree name, TAG_how how)
    7621             : {
    7622    13789237 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7623             : 
    7624    13789237 :   cp_binding_level *b = current_binding_level;
    7625             : 
    7626    13789237 :   if (b->kind != sk_namespace)
    7627             :     /* Look in non-namespace scopes.  */
    7628             :     for (cxx_binding *iter = NULL;
    7629    10286162 :          (iter = outer_binding (name, iter, /*class_p=*/ true)); )
    7630             :       {
    7631             :         /* First check we're supposed to be looking in this scope --
    7632             :            if we're not, we're done.  */
    7633      327717 :         for (; b != iter->scope; b = b->level_chain)
    7634      197152 :           if (!(b->kind == sk_cleanup
    7635      139830 :                 || b->kind == sk_template_parms
    7636       57335 :                 || b->kind == sk_function_parms
    7637       57295 :                 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    7638             :             return NULL_TREE;
    7639             : 
    7640             :         /* Check if this is the kind of thing we're looking for.  If
    7641             :            HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
    7642             :            come from base class.  For ITER->VALUE, we can simply use
    7643             :            INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
    7644             :            our own check.
    7645             : 
    7646             :            We check ITER->TYPE before ITER->VALUE in order to handle
    7647             :              typedef struct C {} C;
    7648             :            correctly.  */
    7649             : 
    7650      187884 :         if (tree type = iter->type)
    7651             :           {
    7652       31800 :             if (qualify_lookup (type, LOOK_want::TYPE)
    7653       31800 :                 && (how != TAG_how::CURRENT_ONLY
    7654         132 :                     || LOCAL_BINDING_P (iter)
    7655         132 :                     || DECL_CONTEXT (type) == iter->scope->this_entity))
    7656             :               {
    7657       31736 :                 if (how != TAG_how::HIDDEN_FRIEND)
    7658             :                   /* It is no longer a hidden binding.  */
    7659          72 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    7660             : 
    7661       31736 :                 return type;
    7662             :               }
    7663             :           }
    7664             :         else
    7665             :           {
    7666      156084 :             if (qualify_lookup (iter->value, LOOK_want::TYPE)
    7667      156084 :                 && (how != TAG_how::CURRENT_ONLY
    7668       23191 :                     || !INHERITED_VALUE_BINDING_P (iter)))
    7669             :               {
    7670      155924 :                 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
    7671             :                   /* It is no longer a hidden binding.  */
    7672       23199 :                   HIDDEN_TYPE_BINDING_P (iter) = false;
    7673             : 
    7674      155924 :                 return iter->value;
    7675             :               }
    7676             :           }
    7677             :       }
    7678             : 
    7679             :   /* Now check if we can look in namespace scope.  */
    7680    22312827 :   for (; b->kind != sk_namespace; b = b->level_chain)
    7681    13309023 :     if (!(b->kind == sk_cleanup
    7682    10869787 :           || b->kind == sk_template_parms
    7683     2440772 :           || b->kind == sk_function_parms
    7684     2328849 :           || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
    7685             :       return NULL_TREE;
    7686             : 
    7687             :   /* Look in the innermost namespace.  */
    7688    11443036 :   tree ns = b->this_entity;
    7689    11443036 :   if (tree *slot = find_namespace_slot (ns, name))
    7690             :     {
    7691     1703800 :       tree bind = *slot;
    7692     1703800 :       if (TREE_CODE (bind) == BINDING_VECTOR)
    7693          72 :         bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
    7694             : 
    7695          72 :       if (bind)
    7696             :         {
    7697             :           /* If this is the kind of thing we're looking for, we're done.  */
    7698     1703755 :           if (tree type = MAYBE_STAT_TYPE (bind))
    7699             :             {
    7700         146 :               if (how != TAG_how::HIDDEN_FRIEND)
    7701             :                 /* No longer hidden.  */
    7702         146 :                 STAT_TYPE_HIDDEN_P (*slot) = false;
    7703             :               
    7704         146 :               return type;
    7705             :             }
    7706     1703609 :           else if (tree decl = MAYBE_STAT_DECL (bind))
    7707             :             {
    7708     1703609 :               if (qualify_lookup (decl, LOOK_want::TYPE))
    7709             :                 {
    7710     1522757 :                   if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
    7711     1730482 :                       && STAT_DECL_HIDDEN_P (bind))
    7712             :                     {
    7713       27054 :                       if (STAT_TYPE (bind))
    7714           0 :                         STAT_DECL_HIDDEN_P (bind) = false;
    7715             :                       else
    7716             :                         {
    7717             :                           /* There is no type, just remove the stat
    7718             :                              hack.  */
    7719       27054 :                           if (*slot == bind)
    7720       27051 :                             *slot = decl;
    7721             :                           else
    7722           3 :                             BINDING_VECTOR_CLUSTER (*slot, 0)
    7723           3 :                               .slots[BINDING_SLOT_CURRENT] = decl;
    7724             :                         }
    7725             :                     }
    7726     1703409 :                   return decl;
    7727             :                 }
    7728             :             }
    7729             :         }
    7730             : 
    7731         245 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    7732             :         {
    7733             :           /* We could be redeclaring a global module entity, (from GMF
    7734             :              or header unit), or from another partition, or
    7735             :              specializing an imported template.  */
    7736          45 :           bitmap imports = get_import_bitmap ();
    7737          45 :           binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
    7738             : 
    7739             :           /* Scan the imported bindings.  */
    7740          45 :           unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
    7741          45 :           if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
    7742             :             {
    7743          45 :               ix--;
    7744          45 :               cluster++;
    7745             :             }
    7746             : 
    7747             :           /* Do this in forward order, so we load modules in an order
    7748             :              the user expects.  */
    7749          48 :           for (; ix--; cluster++)
    7750          66 :             for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
    7751             :               {
    7752             :                 /* Are we importing this module?  */
    7753          63 :                 if (unsigned base = cluster->indices[jx].base)
    7754          39 :                   if (unsigned span = cluster->indices[jx].span)
    7755          39 :                     do
    7756          39 :                       if (bitmap_bit_p (imports, base))
    7757          39 :                         goto found;
    7758           0 :                     while (++base, --span);
    7759          24 :                 continue;
    7760             : 
    7761          39 :               found:;
    7762             :                 /* Is it loaded?  */
    7763          39 :                 if (cluster->slots[jx].is_lazy ())
    7764             :                   {
    7765          24 :                     gcc_assert (cluster->indices[jx].span == 1);
    7766          24 :                     lazy_load_binding (cluster->indices[jx].base,
    7767             :                                        ns, name, &cluster->slots[jx]);
    7768             :                   }
    7769          39 :                 tree bind = cluster->slots[jx];
    7770          39 :                 if (!bind)
    7771             :                   /* Load errors could mean there's nothing here.  */
    7772           0 :                   continue;
    7773             : 
    7774             :                 /* Extract what we can see from here.  If there's no
    7775             :                    stat_hack, then everything was exported.  */
    7776          39 :                 tree type = NULL_TREE;
    7777             : 
    7778             :                 /* If no stat hack, everything is visible.  */
    7779          39 :                 if (STAT_HACK_P (bind))
    7780             :                   {
    7781          36 :                     if (STAT_TYPE_VISIBLE_P (bind))
    7782          18 :                       type = STAT_TYPE (bind);
    7783          36 :                     bind = STAT_VISIBLE (bind);
    7784             :                   }
    7785             : 
    7786          36 :                 if (type && qualify_lookup (type, LOOK_want::TYPE))
    7787           3 :                   return type;
    7788             : 
    7789          36 :                 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
    7790          36 :                   return bind;
    7791          24 :               }
    7792             : 
    7793             :           if (!module_purview_p ())
    7794             :             {
    7795             :               /* We're in the global module, perhaps there's a tag
    7796             :                  there?  */
    7797             :               // FIXME: This isn't quite right, if we find something
    7798             :               // here, from the language PoV we're not supposed to
    7799             :               // know it?
    7800             :             }
    7801             :         }
    7802             :     }
    7803             : 
    7804             :   return NULL_TREE;
    7805    13789237 : }
    7806             : 
    7807             : /* The type TYPE is being declared.  If it is a class template, or a
    7808             :    specialization of a class template, do any processing required and
    7809             :    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
    7810             :    being declared a friend.  B is the binding level at which this TYPE
    7811             :    should be bound.
    7812             : 
    7813             :    Returns the TYPE_DECL for TYPE, which may have been altered by this
    7814             :    processing.  */
    7815             : 
    7816             : static tree
    7817    13114821 : maybe_process_template_type_declaration (tree type, int is_friend,
    7818             :                                          cp_binding_level *b)
    7819             : {
    7820    13114821 :   tree decl = TYPE_NAME (type);
    7821             : 
    7822    13114821 :   if (processing_template_parmlist)
    7823             :     /* You can't declare a new template type in a template parameter
    7824             :        list.  But, you can declare a non-template type:
    7825             : 
    7826             :          template <class A*> struct S;
    7827             : 
    7828             :        is a forward-declaration of `A'.  */
    7829             :     ;
    7830    13114768 :   else if (b->kind == sk_namespace
    7831     3463339 :            && current_binding_level->kind != sk_namespace)
    7832             :     /* If this new type is being injected into a containing scope,
    7833             :        then it's not a template type.  */
    7834             :     ;
    7835             :   else
    7836             :     {
    7837    13088166 :       gcc_assert (MAYBE_CLASS_TYPE_P (type)
    7838             :                   || TREE_CODE (type) == ENUMERAL_TYPE);
    7839             : 
    7840    13088166 :       if (processing_template_decl)
    7841             :         {
    7842     8191710 :           decl = push_template_decl (decl, is_friend);
    7843     8191710 :           if (decl == error_mark_node)
    7844             :             return error_mark_node;
    7845             : 
    7846             :           /* If the current binding level is the binding level for the
    7847             :              template parameters (see the comment in
    7848             :              begin_template_parm_list) and the enclosing level is a class
    7849             :              scope, and we're not looking at a friend, push the
    7850             :              declaration of the member class into the class scope.  In the
    7851             :              friend case, push_template_decl will already have put the
    7852             :              friend into global scope, if appropriate.  */
    7853     8191679 :           if (TREE_CODE (type) != ENUMERAL_TYPE
    7854     7961339 :               && !is_friend && b->kind == sk_template_parms
    7855     6931751 :               && b->level_chain->kind == sk_class)
    7856             :             {
    7857      436179 :               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
    7858             : 
    7859      436179 :               if (!COMPLETE_TYPE_P (current_class_type))
    7860      436164 :                 maybe_add_class_template_decl_list (current_class_type,
    7861             :                                                     type, /*friend_p=*/0);
    7862             :             }
    7863             :         }
    7864             :     }
    7865             : 
    7866             :   return decl;
    7867             : }
    7868             : 
    7869             : /* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
    7870             :    that the NAME is a class template, the tag is processed but not pushed.
    7871             : 
    7872             :    The pushed scope depend on the SCOPE parameter:
    7873             :    - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
    7874             :      scope.
    7875             :    - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
    7876             :      non-template-parameter scope.  This case is needed for forward
    7877             :      declarations.
    7878             :    - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
    7879             :      TS_GLOBAL case except that names within template-parameter scopes
    7880             :      are not pushed at all.
    7881             : 
    7882             :    Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
    7883             : 
    7884             : tree
    7885    13114821 : pushtag (tree name, tree type, TAG_how how)
    7886             : {
    7887    13114821 :   tree decl;
    7888             : 
    7889    13114821 :   gcc_assert (identifier_p (name));
    7890             : 
    7891    13114821 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    7892             : 
    7893    13114821 :   cp_binding_level *b = current_binding_level;
    7894    13141862 :   while (true)
    7895             :     {
    7896    13141862 :       if (/* Cleanup scopes are not scopes from the point of view of
    7897             :              the language.  */
    7898    13141862 :           b->kind == sk_cleanup
    7899             :           /* Neither are function parameter scopes.  */
    7900    13141862 :           || b->kind == sk_function_parms
    7901             :           /* Neither are the scopes used to hold template parameters
    7902             :              for an explicit specialization.  For an ordinary template
    7903             :              declaration, these scopes are not scopes from the point of
    7904             :              view of the language.  */
    7905    13140101 :           || (b->kind == sk_template_parms
    7906     7153098 :               && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
    7907        1831 :         b = b->level_chain;
    7908    13140031 :       else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
    7909             :         {
    7910       25210 :           b = b->level_chain;
    7911       25210 :           if (b->kind == sk_template_parms)
    7912         475 :             b = b->level_chain;
    7913             :         }
    7914             :       else
    7915             :         break;
    7916             :     }
    7917             : 
    7918             :   /* Do C++ gratuitous typedefing.  */
    7919    13114821 :   if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
    7920             :     {
    7921    13114821 :       tree tdef;
    7922    13114821 :       tree context = TYPE_CONTEXT (type);
    7923             : 
    7924    13114821 :       if (! context)
    7925             :         {
    7926             :           cp_binding_level *cb = b;
    7927    20967078 :           while (cb->kind != sk_namespace
    7928    20967078 :                  && cb->kind != sk_class
    7929    20967078 :                  && (cb->kind != sk_function_parms
    7930      681530 :                      || !cb->this_entity))
    7931     8006831 :             cb = cb->level_chain;
    7932    12960247 :           tree cs = cb->this_entity;
    7933             : 
    7934    12960247 :           gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
    7935             :                                ? cs == current_function_decl
    7936             :                                : TYPE_P (cs) ? cs == current_class_type
    7937             :                                : cs == current_namespace);
    7938             : 
    7939    12960247 :           if (how == TAG_how::CURRENT_ONLY
    7940       92996 :               || (cs && TREE_CODE (cs) == FUNCTION_DECL))
    7941             :             context = cs;
    7942       92881 :           else if (cs && TYPE_P (cs))
    7943             :             /* When declaring a friend class of a local class, we want
    7944             :                to inject the newly named class into the scope
    7945             :                containing the local class, not the namespace
    7946             :                scope.  */
    7947       66647 :             context = decl_function_context (get_type_decl (cs));
    7948             :         }
    7949    12960132 :       if (!context)
    7950       92881 :         context = current_namespace;
    7951             : 
    7952    13114821 :       tdef = create_implicit_typedef (name, type);
    7953    13114821 :       DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
    7954    13114821 :       set_originating_module (tdef);
    7955             : 
    7956    13114821 :       decl = maybe_process_template_type_declaration
    7957    13114821 :         (type, how == TAG_how::HIDDEN_FRIEND, b);
    7958    13114821 :       if (decl == error_mark_node)
    7959             :         return decl;
    7960             : 
    7961    13114790 :       if (b->kind == sk_class)
    7962             :         {
    7963     1816900 :           if (!TYPE_BEING_DEFINED (current_class_type))
    7964             :             /* Don't push anywhere if the class is complete; a lambda in an
    7965             :                NSDMI is not a member of the class.  */
    7966             :             ;
    7967     1816554 :           else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
    7968             :             /* Put this TYPE_DECL on the TYPE_FIELDS list for the
    7969             :                class.  But if it's a member template class, we want
    7970             :                the TEMPLATE_DECL, not the TYPE_DECL, so this is done
    7971             :                later.  */
    7972     1816546 :             finish_member_declaration (decl);
    7973             :           else
    7974           8 :             pushdecl_class_level (decl);
    7975             :         }
    7976    11297890 :       else if (b->kind == sk_template_parms)
    7977             :         {
    7978             :           /* Do not push the tag here -- we'll want to push the
    7979             :              TEMPLATE_DECL.  */
    7980     7153005 :           if (b->level_chain->kind != sk_class)
    7981     6495605 :             set_identifier_type_value_with_scope (name, tdef, b->level_chain);
    7982             :         }
    7983             :       else
    7984             :         {
    7985     4144885 :           decl = do_pushdecl_with_scope
    7986     4144885 :             (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
    7987     4144885 :           if (decl == error_mark_node)
    7988             :             return decl;
    7989             : 
    7990     4144832 :           if (DECL_CONTEXT (decl) == std_node
    7991     1574435 :               && init_list_identifier == DECL_NAME (TYPE_NAME (type))
    7992     4144838 :               && !CLASSTYPE_TEMPLATE_INFO (type))
    7993             :             {
    7994           6 :               error ("declaration of %<std::initializer_list%> does not match "
    7995             :                      "%<#include <initializer_list>%>, isn%'t a template");
    7996           6 :               return error_mark_node;
    7997             :             }
    7998             :         }
    7999             : 
    8000    13114731 :       TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
    8001             : 
    8002             :       /* If this is a local class, keep track of it.  We need this
    8003             :          information for name-mangling, and so that it is possible to
    8004             :          find all function definitions in a translation unit in a
    8005             :          convenient way.  (It's otherwise tricky to find a member
    8006             :          function definition it's only pointed to from within a local
    8007             :          class.)  */
    8008    13114731 :       if (TYPE_FUNCTION_SCOPE_P (type))
    8009             :         {
    8010      681500 :           if (processing_template_decl)
    8011             :             {
    8012             :               /* Push a DECL_EXPR so we call pushtag at the right time in
    8013             :                  template instantiation rather than in some nested context.  */
    8014      312414 :               add_decl_expr (decl);
    8015             :             }
    8016             :           /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead.  */
    8017      734873 :           else if (!LAMBDA_TYPE_P (type))
    8018      327038 :             determine_local_discriminator (TYPE_NAME (type));
    8019             :         }
    8020             :     }
    8021             : 
    8022    13114731 :   if (b->kind == sk_class
    8023    13114731 :       && !COMPLETE_TYPE_P (current_class_type))
    8024     1816561 :     maybe_add_class_template_decl_list (current_class_type,
    8025             :                                         type, /*friend_p=*/0);
    8026             : 
    8027    13114731 :   decl = TYPE_NAME (type);
    8028    13114731 :   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    8029             : 
    8030             :   /* Set type visibility now if this is a forward declaration.  */
    8031    13114731 :   TREE_PUBLIC (decl) = 1;
    8032    13114731 :   determine_visibility (decl);
    8033             : 
    8034    13114731 :   return type;
    8035    13114821 : }
    8036             : 
    8037             : /* Subroutines for reverting temporarily to top-level for instantiation
    8038             :    of templates and such.  We actually need to clear out the class- and
    8039             :    local-value slots of all identifiers, so that only the global values
    8040             :    are at all visible.  Simply setting current_binding_level to the global
    8041             :    scope isn't enough, because more binding levels may be pushed.  */
    8042             : struct saved_scope *scope_chain;
    8043             : 
    8044             : /* Return true if ID has not already been marked.  */
    8045             : 
    8046             : static inline bool
    8047 89250486444 : store_binding_p (tree id)
    8048             : {
    8049 >17849*10^7 :   if (!id || !IDENTIFIER_BINDING (id))
    8050             :     return false;
    8051             : 
    8052  1001297038 :   if (IDENTIFIER_MARKED (id))
    8053           0 :     return false;
    8054             : 
    8055             :   return true;
    8056             : }
    8057             : 
    8058             : /* Add an appropriate binding to *OLD_BINDINGS which needs to already
    8059             :    have enough space reserved.  */
    8060             : 
    8061             : static void
    8062   413018848 : store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
    8063             : {
    8064   413018848 :   cxx_saved_binding saved;
    8065             : 
    8066   413018848 :   gcc_checking_assert (store_binding_p (id));
    8067             : 
    8068   413018848 :   IDENTIFIER_MARKED (id) = 1;
    8069             : 
    8070   413018848 :   saved.identifier = id;
    8071   413018848 :   saved.binding = IDENTIFIER_BINDING (id);
    8072   413018848 :   saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
    8073   413018848 :   (*old_bindings)->quick_push (saved);
    8074   413018848 :   IDENTIFIER_BINDING (id) = NULL;
    8075   413018848 : }
    8076             : 
    8077             : static void
    8078   247849106 : store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
    8079             : {
    8080   247849106 :   static vec<tree> bindings_need_stored;
    8081   247849106 :   tree t, id;
    8082   247849106 :   size_t i;
    8083             : 
    8084   247849106 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8085 88825208828 :   for (t = names; t; t = TREE_CHAIN (t))
    8086             :     {
    8087 88329510616 :       if (TREE_CODE (t) == TREE_LIST)
    8088      956137 :         id = TREE_PURPOSE (t);
    8089             :       else
    8090 88328554479 :         id = DECL_NAME (t);
    8091             : 
    8092 88329510616 :       if (store_binding_p (id))
    8093   175259342 :         bindings_need_stored.safe_push (id);
    8094             :     }
    8095   247849106 :   if (!bindings_need_stored.is_empty ())
    8096             :     {
    8097    72615103 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8098   320489548 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8099             :         {
    8100             :           /* We can apparently have duplicates in NAMES.  */
    8101   175259342 :           if (store_binding_p (id))
    8102   175259315 :             store_binding (id, old_bindings);
    8103             :         }
    8104    72615103 :       bindings_need_stored.truncate (0);
    8105             :     }
    8106   247849106 : }
    8107             : 
    8108             : /* Like store_bindings, but NAMES is a vector of cp_class_binding
    8109             :    objects, rather than a TREE_LIST.  */
    8110             : 
    8111             : static void
    8112   122025219 : store_class_bindings (vec<cp_class_binding, va_gc> *names,
    8113             :                       vec<cxx_saved_binding, va_gc> **old_bindings)
    8114             : {
    8115   122025219 :   static vec<tree> bindings_need_stored;
    8116   122025219 :   size_t i;
    8117   122025219 :   cp_class_binding *cb;
    8118             : 
    8119   454722857 :   for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
    8120   332697638 :     if (store_binding_p (cb->identifier))
    8121   237759533 :       bindings_need_stored.safe_push (cb->identifier);
    8122   122025219 :   if (!bindings_need_stored.is_empty ())
    8123             :     {
    8124    29221965 :       tree id;
    8125    29221965 :       vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
    8126   296203463 :       for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
    8127   237759533 :         store_binding (id, old_bindings);
    8128    29221965 :       bindings_need_stored.truncate (0);
    8129             :     }
    8130   122025219 : }
    8131             : 
    8132             : /* A chain of saved_scope structures awaiting reuse.  */
    8133             : 
    8134             : static GTY((deletable)) struct saved_scope *free_saved_scope;
    8135             : 
    8136             : void
    8137   174665668 : push_to_top_level (void)
    8138             : {
    8139   174665668 :   struct saved_scope *s;
    8140   174665668 :   cp_binding_level *b;
    8141   174665668 :   cxx_saved_binding *sb;
    8142   174665668 :   size_t i;
    8143   174665668 :   bool need_pop;
    8144             : 
    8145   174665668 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8146             : 
    8147             :   /* Reuse or create a new structure for this saved scope.  */
    8148   174665668 :   if (free_saved_scope != NULL)
    8149             :     {
    8150   173805375 :       s = free_saved_scope;
    8151   173805375 :       free_saved_scope = s->prev;
    8152             : 
    8153   173805375 :       vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
    8154   173805375 :       memset (s, 0, sizeof (*s));
    8155             :       /* Also reuse the structure's old_bindings vector.  */
    8156   173805375 :       vec_safe_truncate (old_bindings, 0);
    8157   173805375 :       s->old_bindings = old_bindings;
    8158             :     }
    8159             :   else
    8160      860293 :     s = ggc_cleared_alloc<saved_scope> ();
    8161             : 
    8162   174665668 :   b = scope_chain ? current_binding_level : 0;
    8163             : 
    8164             :   /* If we're in the middle of some function, save our state.  */
    8165   174665668 :   if (cfun)
    8166             :     {
    8167    31355017 :       need_pop = true;
    8168    31355017 :       push_function_context ();
    8169             :     }
    8170             :   else
    8171             :     need_pop = false;
    8172             : 
    8173   174665668 :   if (scope_chain && previous_class_level)
    8174    52017036 :     store_class_bindings (previous_class_level->class_shadowed,
    8175             :                           &s->old_bindings);
    8176             : 
    8177             :   /* Have to include the global scope, because class-scope decls
    8178             :      aren't listed anywhere useful.  */
    8179   422514774 :   for (; b; b = b->level_chain)
    8180             :     {
    8181   422425514 :       tree t;
    8182             : 
    8183             :       /* Template IDs are inserted into the global level. If they were
    8184             :          inserted into namespace level, finish_file wouldn't find them
    8185             :          when doing pending instantiations. Therefore, don't stop at
    8186             :          namespace level, but continue until :: .  */
    8187   422425514 :       if (global_scope_p (b))
    8188             :         break;
    8189             : 
    8190   247849106 :       store_bindings (b->names, &s->old_bindings);
    8191             :       /* We also need to check class_shadowed to save class-level type
    8192             :          bindings, since pushclass doesn't fill in b->names.  */
    8193   247849106 :       if (b->kind == sk_class)
    8194    70008183 :         store_class_bindings (b->class_shadowed, &s->old_bindings);
    8195             : 
    8196             :       /* Unwind type-value slots back to top level.  */
    8197   428531357 :       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
    8198   180682251 :         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
    8199             :     }
    8200             : 
    8201   587684516 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
    8202   413018848 :     IDENTIFIER_MARKED (sb->identifier) = 0;
    8203             : 
    8204   174665668 :   s->prev = scope_chain;
    8205   174665668 :   s->bindings = b;
    8206   174665668 :   s->need_pop_function_context = need_pop;
    8207   174665668 :   s->function_decl = current_function_decl;
    8208   174665668 :   s->unevaluated_operand = cp_unevaluated_operand;
    8209   174665668 :   s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
    8210   174665668 :   s->suppress_location_wrappers = suppress_location_wrappers;
    8211   174665668 :   s->x_stmt_tree.stmts_are_full_exprs_p = true;
    8212             : 
    8213   174665668 :   scope_chain = s;
    8214   174665668 :   current_function_decl = NULL_TREE;
    8215   174665668 :   current_lang_base = NULL;
    8216   174665668 :   current_lang_name = lang_name_cplusplus;
    8217   174665668 :   current_namespace = global_namespace;
    8218   174665668 :   push_class_stack ();
    8219   174665668 :   cp_unevaluated_operand = 0;
    8220   174665668 :   c_inhibit_evaluation_warnings = 0;
    8221   174665668 :   suppress_location_wrappers = 0;
    8222   174665668 : }
    8223             : 
    8224             : void
    8225   174548401 : pop_from_top_level (void)
    8226             : {
    8227   174548401 :   struct saved_scope *s = scope_chain;
    8228   174548401 :   cxx_saved_binding *saved;
    8229   174548401 :   size_t i;
    8230             : 
    8231   174548401 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8232             : 
    8233   174548401 :   pop_class_stack ();
    8234             : 
    8235   174548401 :   release_tree_vector (current_lang_base);
    8236             : 
    8237   174548401 :   scope_chain = s->prev;
    8238   587563504 :   FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
    8239             :     {
    8240   413015103 :       tree id = saved->identifier;
    8241             : 
    8242   413015103 :       IDENTIFIER_BINDING (id) = saved->binding;
    8243   413015103 :       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
    8244             :     }
    8245             : 
    8246             :   /* If we were in the middle of compiling a function, restore our
    8247             :      state.  */
    8248   174548401 :   if (s->need_pop_function_context)
    8249    31354997 :     pop_function_context ();
    8250   174548401 :   current_function_decl = s->function_decl;
    8251   174548401 :   cp_unevaluated_operand = s->unevaluated_operand;
    8252   174548401 :   c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
    8253   174548401 :   suppress_location_wrappers = s->suppress_location_wrappers;
    8254             : 
    8255             :   /* Make this saved_scope structure available for reuse by
    8256             :      push_to_top_level.  */
    8257   174548401 :   s->prev = free_saved_scope;
    8258   174548401 :   free_saved_scope = s;
    8259   174548401 : }
    8260             : 
    8261             : /* Like push_to_top_level, but not if D is function-local.  Returns whether we
    8262             :    did push to top.  */
    8263             : 
    8264             : bool
    8265    11420069 : maybe_push_to_top_level (tree d)
    8266             : {
    8267             :   /* Push if D isn't function-local, or is a lambda function, for which name
    8268             :      resolution is already done.  */
    8269    11420069 :   bool push_to_top
    8270    11420069 :     = !(current_function_decl
    8271     1608480 :         && !LAMBDA_FUNCTION_P (d)
    8272     1576917 :         && decl_function_context (d) == current_function_decl);
    8273             : 
    8274    11420069 :   if (push_to_top)
    8275    10777742 :     push_to_top_level ();
    8276             :   else
    8277             :     {
    8278      642327 :       gcc_assert (!processing_template_decl);
    8279      642327 :       push_function_context ();
    8280      642327 :       cp_unevaluated_operand = 0;
    8281      642327 :       c_inhibit_evaluation_warnings = 0;
    8282             :     }
    8283             : 
    8284    11420069 :   return push_to_top;
    8285             : }
    8286             : 
    8287             : /* Return from whatever maybe_push_to_top_level did.  */
    8288             : 
    8289             : void
    8290    11420058 : maybe_pop_from_top_level (bool push_to_top)
    8291             : {
    8292    11420058 :   if (push_to_top)
    8293    10777731 :     pop_from_top_level ();
    8294             :   else
    8295      642327 :     pop_function_context ();
    8296    11420058 : }
    8297             : 
    8298             : /* Push into the scope of the namespace NS, even if it is deeply
    8299             :    nested within another namespace.  */
    8300             : 
    8301             : void
    8302    69676298 : push_nested_namespace (tree ns)
    8303             : {
    8304    69676298 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8305    69676298 :   if (ns == global_namespace)
    8306    34891586 :     push_to_top_level ();
    8307             :   else
    8308             :     {
    8309    69046421 :       push_nested_namespace (CP_DECL_CONTEXT (ns));
    8310    34784712 :       resume_scope (NAMESPACE_LEVEL (ns));
    8311    34784712 :       current_namespace = ns;
    8312             :     }
    8313    69676298 : }
    8314             : 
    8315             : /* Pop back from the scope of the namespace NS, which was previously
    8316             :    entered with push_nested_namespace.  */
    8317             : 
    8318             : void
    8319    34877194 : pop_nested_namespace (tree ns)
    8320             : {
    8321    34877194 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8322    69661906 :   while (ns != global_namespace)
    8323             :     {
    8324    34784712 :       ns = CP_DECL_CONTEXT (ns);
    8325    34784712 :       current_namespace = ns;
    8326    34784712 :       leave_scope ();
    8327             :     }
    8328             : 
    8329    34877194 :   pop_from_top_level ();
    8330    34877194 : }
    8331             : 
    8332             : /* Add TARGET to USINGS, if it does not already exist there.  We used
    8333             :    to build the complete graph of usings at this point, from the POV
    8334             :    of the source namespaces.  Now we build that as we perform the
    8335             :    unqualified search.  */
    8336             : 
    8337             : static void
    8338       46757 : add_using_namespace (vec<tree, va_gc> *&usings, tree target)
    8339             : {
    8340       46757 :   if (usings)
    8341        1325 :     for (unsigned ix = usings->length (); ix--;)
    8342         888 :       if ((*usings)[ix] == target)
    8343             :         return;
    8344             : 
    8345       46320 :   vec_safe_push (usings, target);
    8346             : }
    8347             : 
    8348             : /* Tell the debug system of a using directive.  */
    8349             : 
    8350             : static void
    8351      128202 : emit_debug_info_using_namespace (tree from, tree target, bool implicit)
    8352             : {
    8353             :   /* Emit debugging info.  */
    8354      128202 :   tree context = from != global_namespace ? from : NULL_TREE;
    8355      128202 :   debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
    8356             :                                         implicit);
    8357      128202 : }
    8358             : 
    8359             : /* Process a using directive.  */
    8360             : 
    8361             : void
    8362       45624 : finish_using_directive (tree target, tree attribs)
    8363             : {
    8364       45624 :   if (target == error_mark_node)
    8365             :     return;
    8366             : 
    8367       45603 :   if (current_binding_level->kind != sk_namespace)
    8368       21260 :     add_stmt (build_stmt (input_location, USING_STMT, target));
    8369             :   else
    8370       48686 :     emit_debug_info_using_namespace (current_binding_level->this_entity,
    8371       24343 :                                      ORIGINAL_NAMESPACE (target), false);
    8372             : 
    8373       91206 :   add_using_namespace (current_binding_level->using_directives,
    8374       45603 :                        ORIGINAL_NAMESPACE (target));
    8375             : 
    8376       45603 :   bool diagnosed = false;
    8377       45603 :   if (attribs != error_mark_node)
    8378       45624 :     for (tree a = attribs; a; a = TREE_CHAIN (a))
    8379             :       {
    8380          21 :         tree name = get_attribute_name (a);
    8381          21 :         if (current_binding_level->kind == sk_namespace
    8382          21 :             && is_attribute_p ("strong", name))
    8383             :           {
    8384          12 :             if (warning (0, "%<strong%> using directive no longer supported")
    8385          12 :                 && CP_DECL_CONTEXT (target) == current_namespace)
    8386           6 :               inform (DECL_SOURCE_LOCATION (target),
    8387             :                       "you can use an inline namespace instead");
    8388             :           }
    8389           9 :         else if ((flag_openmp || flag_openmp_simd)
    8390           3 :                  && get_attribute_namespace (a) == omp_identifier
    8391          12 :                  && (is_attribute_p ("directive", name)
    8392           0 :                      || is_attribute_p ("sequence", name)))
    8393             :           {
    8394           3 :             if (!diagnosed)
    8395           3 :               error ("%<omp::%E%> not allowed to be specified in this "
    8396             :                      "context", name);
    8397             :             diagnosed = true;
    8398             :           }
    8399             :         else
    8400           6 :           warning (OPT_Wattributes, "%qD attribute directive ignored", name);
    8401             :       }
    8402             : }
    8403             : 
    8404             : /* Pushes X into the global namespace.  */
    8405             : 
    8406             : tree
    8407      321624 : pushdecl_top_level (tree x)
    8408             : {
    8409      321624 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8410      321624 :   push_to_top_level ();
    8411      321624 :   gcc_checking_assert (!DECL_CONTEXT (x));
    8412      321624 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    8413      321624 :   x = pushdecl_namespace_level (x);
    8414      321624 :   pop_from_top_level ();
    8415      643248 :   return x;
    8416      321624 : }
    8417             : 
    8418             : /* Pushes X into the global namespace and calls cp_finish_decl to
    8419             :    register the variable, initializing it with INIT.  */
    8420             : 
    8421             : tree
    8422     1464982 : pushdecl_top_level_and_finish (tree x, tree init)
    8423             : {
    8424     1464982 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8425     1464982 :   push_to_top_level ();
    8426     1464982 :   gcc_checking_assert (!DECL_CONTEXT (x));
    8427     1464982 :   DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
    8428     1464982 :   x = pushdecl_namespace_level (x);
    8429     1464982 :   cp_finish_decl (x, init, false, NULL_TREE, 0);
    8430     1464982 :   pop_from_top_level ();
    8431     2929964 :   return x;
    8432     1464982 : }
    8433             : 
    8434             : /* Enter the namespaces from current_namerspace to NS.  */
    8435             : 
    8436             : static int
    8437     3120239 : push_inline_namespaces (tree ns)
    8438             : {
    8439     3120239 :   int count = 0;
    8440     3120239 :   if (ns != current_namespace)
    8441             :     {
    8442          16 :       gcc_assert (ns != global_namespace);
    8443          25 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    8444          16 :       resume_scope (NAMESPACE_LEVEL (ns));
    8445          16 :       current_namespace = ns;
    8446          16 :       count++;
    8447             :     }
    8448     3120239 :   return count;
    8449             : }
    8450             : 
    8451             : /* SLOT is the (possibly empty) binding slot for NAME in CTX.
    8452             :    Reuse or create a namespace NAME.  NAME is null for the anonymous
    8453             :    namespace.  */
    8454             : 
    8455             : static tree
    8456        2689 : reuse_namespace (tree *slot, tree ctx, tree name)
    8457             : {
    8458        2689 :   if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
    8459             :     {
    8460             :       /* Public namespace.  Shared.  */
    8461        1246 :       tree *global_slot = slot;
    8462        1246 :       if (TREE_CODE (*slot) == BINDING_VECTOR)
    8463          90 :         global_slot = get_fixed_binding_slot (slot, name,
    8464             :                                               BINDING_SLOT_GLOBAL, false);
    8465             : 
    8466        1252 :       for (ovl_iterator iter (*global_slot); iter; ++iter)
    8467             :         {
    8468        1246 :           tree decl = *iter;
    8469             : 
    8470        1246 :           if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
    8471        1243 :             return decl;
    8472             :         }
    8473             :     }
    8474             :   return NULL_TREE;
    8475             : }
    8476             : 
    8477             : static tree
    8478      535606 : make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
    8479             : {
    8480             :   /* Create the namespace.  */
    8481      535606 :   tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
    8482      535606 :   DECL_SOURCE_LOCATION (ns) = loc;
    8483      535606 :   SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
    8484      535606 :   if (!SCOPE_DEPTH (ns))
    8485             :     /* We only allow depth 255. */
    8486           0 :     sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
    8487      535606 :   DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
    8488             : 
    8489      535606 :   if (!name)
    8490             :     /* Anon-namespaces in different header-unit imports are distinct.
    8491             :        But that's ok as their contents all have internal linkage.
    8492             :        (This is different to how they'd behave as textual includes,
    8493             :        but doing this at all is really odd source.)  */
    8494        1167 :     SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
    8495      534439 :   else if (TREE_PUBLIC (ctx))
    8496      534395 :     TREE_PUBLIC (ns) = true;
    8497             : 
    8498      535606 :   if (inline_p)
    8499      102705 :     DECL_NAMESPACE_INLINE_P (ns) = true;
    8500             : 
    8501      535606 :   return ns;
    8502             : }
    8503             : 
    8504             : /* NS was newly created, finish off making it.  */
    8505             : 
    8506             : static void
    8507      535599 : make_namespace_finish (tree ns, tree *slot, bool from_import = false)
    8508             : {
    8509      535599 :   if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
    8510             :     {
    8511             :       /* Merge into global slot.  */
    8512        1435 :       tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
    8513             :                                             BINDING_SLOT_GLOBAL, true);
    8514        1435 :       *gslot = ns;
    8515             :     }
    8516             : 
    8517      535599 :   tree ctx = CP_DECL_CONTEXT (ns);
    8518      535599 :   cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
    8519      535599 :   scope->this_entity = ns;
    8520      535599 :   scope->more_cleanups_ok = true;
    8521      535599 :   scope->kind = sk_namespace;
    8522      535599 :   scope->level_chain = NAMESPACE_LEVEL (ctx);
    8523      535599 :   NAMESPACE_LEVEL (ns) = scope;
    8524             : 
    8525      535599 :   if (DECL_NAMESPACE_INLINE_P (ns))
    8526      102705 :     vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
    8527             : 
    8528      535599 :   if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
    8529      103859 :     emit_debug_info_using_namespace (ctx, ns, true);
    8530      535599 : }
    8531             : 
    8532             : /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE,
    8533             :    then we enter an anonymous namespace.  If MAKE_INLINE is true, then
    8534             :    we create an inline namespace (it is up to the caller to check upon
    8535             :    redefinition). Return the number of namespaces entered.  */
    8536             : 
    8537             : int
    8538     3654412 : push_namespace (tree name, bool make_inline)
    8539             : {
    8540     3654412 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8541     3654412 :   int count = 0;
    8542             : 
    8543             :   /* We should not get here if the global_namespace is not yet constructed
    8544             :      nor if NAME designates the global namespace:  The global scope is
    8545             :      constructed elsewhere.  */
    8546     3654412 :   gcc_checking_assert (global_namespace != NULL && name != global_identifier);
    8547             : 
    8548     3654412 :   tree ns = NULL_TREE;
    8549     3654412 :   {
    8550     3654412 :     name_lookup lookup (name);
    8551     3654412 :     if (!lookup.search_qualified (current_namespace, /*usings=*/false))
    8552             :       ;
    8553     3120243 :     else if (TREE_CODE (lookup.value) == TREE_LIST)
    8554             :       {
    8555             :         /* An ambiguous lookup.  If exactly one is a namespace, we
    8556             :            want that.  If more than one is a namespace, error, but
    8557             :            pick one of them.  */
    8558             :         /* DR2061 can cause us to find multiple namespaces of the same
    8559             :            name.  We must treat that carefully and avoid thinking we
    8560             :            need to push a new (possibly) duplicate namespace.  Hey,
    8561             :            if you want to use the same identifier within an inline
    8562             :            nest, knock yourself out.  */
    8563           9 :         for (tree *chain = &lookup.value, next; (next = *chain);)
    8564             :           {
    8565           6 :             tree decl = TREE_VALUE (next);
    8566           6 :             if (TREE_CODE (decl) == NAMESPACE_DECL)
    8567             :               {
    8568           6 :                 if (!ns)
    8569             :                   ns = decl;
    8570           3 :                 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
    8571           6 :                   ns = decl;
    8572             : 
    8573             :                 /* Advance.  */
    8574           6 :                 chain = &TREE_CHAIN (next);
    8575             :               }
    8576             :             else
    8577             :               /* Stitch out.  */
    8578           0 :               *chain = TREE_CHAIN (next);
    8579             :           }
    8580             : 
    8581           3 :         if (TREE_CHAIN (lookup.value))
    8582             :           {
    8583           3 :             error ("%<namespace %E%> is ambiguous", name);
    8584           3 :             print_candidates (lookup.value);
    8585             :           }
    8586             :       }
    8587     3120240 :     else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
    8588             :       ns = lookup.value;
    8589             : 
    8590     3120227 :     if (ns)
    8591     3120227 :       if (tree dna = DECL_NAMESPACE_ALIAS (ns))
    8592             :         {
    8593             :           /* A namespace alias is not allowed here, but if the alias
    8594             :              is for a namespace also inside the current scope,
    8595             :              accept it with a diagnostic.  That's better than dying
    8596             :              horribly.  */
    8597          12 :           if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
    8598             :             {
    8599           8 :               error ("namespace alias %qD not allowed here, "
    8600             :                      "assuming %qD", ns, dna);
    8601           8 :               ns = dna;
    8602             :             }
    8603             :           else
    8604             :             ns = NULL_TREE;
    8605             :         }
    8606     3654412 :   }
    8607             : 
    8608     3654412 :   if (ns)
    8609             :     {
    8610             :       /* DR2061.  NS might be a member of an inline namespace.  We
    8611             :          need to push into those namespaces.  */
    8612     3120223 :       if (modules_p ())
    8613             :         {
    8614       42454 :           for (tree parent, ctx = ns; ctx != current_namespace;
    8615             :                ctx = parent)
    8616             :             {
    8617       21227 :               parent = CP_DECL_CONTEXT (ctx);
    8618             : 
    8619       21227 :               tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
    8620       21227 :               if (bind != ctx)
    8621             :                 {
    8622         208 :                   auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
    8623         208 :                   binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
    8624         208 :                   gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
    8625       42662 :                   slot = ctx;
    8626             :                 }
    8627             :             }
    8628             :         }
    8629             : 
    8630     3120223 :       count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
    8631     3120223 :       if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
    8632             :         /* It's not builtin now.  */
    8633       25133 :         DECL_SOURCE_LOCATION (ns) = input_location;
    8634             :     }
    8635             :   else
    8636             :     {
    8637             :       /* Before making a new namespace, see if we already have one in
    8638             :          the existing partitions of the current namespace.  */
    8639      534189 :       tree *slot = find_namespace_slot (current_namespace, name, false);
    8640      534189 :       if (slot)
    8641          29 :         ns = reuse_namespace (slot, current_namespace, name);
    8642      534189 :       if (!ns)
    8643      534180 :         ns = make_namespace (current_namespace, name,
    8644             :                              input_location, make_inline);
    8645             : 
    8646      534189 :       if (pushdecl (ns) == error_mark_node)
    8647             :         ns = NULL_TREE;
    8648             :       else
    8649             :         {
    8650             :           /* Finish up making the namespace.  */
    8651      534173 :           add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
    8652      534173 :           if (!slot)
    8653             :             {
    8654      534160 :               slot = find_namespace_slot (current_namespace, name);
    8655             :               /* This should find the slot created by pushdecl.  */
    8656      534160 :               gcc_checking_assert (slot && *slot == ns);
    8657             :             }
    8658             :           else
    8659             :             {
    8660             :               /* pushdecl could have expanded the hash table, so
    8661             :                  slot might be invalid.  */
    8662          13 :               slot = find_namespace_slot (current_namespace, name);
    8663          13 :               gcc_checking_assert (slot);
    8664             :             }
    8665      534173 :           make_namespace_finish (ns, slot);
    8666             : 
    8667             :           /* Add the anon using-directive here, we don't do it in
    8668             :              make_namespace_finish.  */
    8669      534173 :           if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
    8670        1154 :             add_using_namespace (current_binding_level->using_directives, ns);
    8671             :         }
    8672             :     }
    8673             : 
    8674     3654396 :   if (ns)
    8675             :     {
    8676             :       /* A public namespace is exported only if explicitly marked, or
    8677             :          it contains exported entities.  */
    8678     3654396 :       if (TREE_PUBLIC (ns) && module_exporting_p ())
    8679       12057 :         DECL_MODULE_EXPORT_P (ns) = true;
    8680     3654396 :       if (module_purview_p ())
    8681       12261 :         DECL_MODULE_PURVIEW_P (ns) = true;
    8682             : 
    8683     3805716 :       if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
    8684             :         {
    8685           3 :           error_at (input_location,
    8686             :                     "inline namespace must be specified at initial definition");
    8687           3 :           inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
    8688             :         }
    8689     3654396 :       resume_scope (NAMESPACE_LEVEL (ns));
    8690     3654396 :       current_namespace = ns;
    8691     3654396 :       count++;
    8692             :     }
    8693             : 
    8694     7308824 :   return count;
    8695     3654412 : }
    8696             : 
    8697             : /* Pop from the scope of the current namespace.  */
    8698             : 
    8699             : void
    8700     3671450 : pop_namespace (void)
    8701             : {
    8702     3671450 :   auto_cond_timevar tv (TV_NAME_LOOKUP);
    8703             : 
    8704     3671450 :   gcc_assert (current_namespace != global_namespace);
    8705     3671450 :   current_namespace = CP_DECL_CONTEXT (current_namespace);
    8706             :   /* The binding level is not popped, as it might be re-opened later.  */
    8707     3671450 :   leave_scope ();
    8708     3671450 : }
    8709             : 
    8710             : /* An IMPORT is an import that is defining namespace NAME inside CTX.  Find or
    8711             :    create that namespace and add it to the container's binding-vector.   */
    8712             : 
    8713             : tree
    8714        2660 : add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import, 
    8715             :                         bool inline_p, bool visible_p)
    8716             : {
    8717             :   // FIXME: Something is not correct about the VISIBLE_P handling.  We
    8718             :   // need to insert this namespace into
    8719             :   // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
    8720             :   // (b) The importing module's slot (always)
    8721             :   // (c) Do we need to put it in the CURRENT slot?  This is the
    8722             :   // confused piece.
    8723             : 
    8724        2660 :   tree *slot = find_namespace_slot (ctx, name, true);
    8725        2660 :   tree decl = reuse_namespace (slot, ctx, name);
    8726             : 
    8727             :   /* Creating and binding.  */
    8728        2660 :   if (!decl)
    8729             :     {
    8730        1426 :       decl = make_namespace (ctx, name, loc, inline_p);
    8731        1426 :       DECL_MODULE_IMPORT_P (decl) = true;
    8732        1426 :       make_namespace_finish (decl, slot, true);
    8733             :     }
    8734        1234 :   else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
    8735             :     {
    8736           0 :       error_at (loc, "%s namespace %qD conflicts with reachable definition",
    8737             :                 inline_p ? "inline" : "non-inline", decl);
    8738           0 :       inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
    8739             :               inline_p ? "non-inline" : "inline");
    8740             :     }
    8741             : 
    8742        2660 :   if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
    8743             :     {
    8744             :       /* See if we can extend the final slot.  */
    8745        1507 :       binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
    8746        1507 :       gcc_checking_assert (last->indices[0].span);
    8747             :       unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
    8748             : 
    8749        1979 :       while (--jx)
    8750        1507 :         if (last->indices[jx].span)
    8751             :           break;
    8752        1507 :       tree final = last->slots[jx];
    8753        1507 :       if (visible_p == !STAT_HACK_P (final)
    8754        1224 :           && MAYBE_STAT_DECL (final) == decl
    8755         914 :           && last->indices[jx].base + last->indices[jx].span == import
    8756        2398 :           && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
    8757             :               || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
    8758             :                   && jx >= BINDING_SLOTS_FIXED)))
    8759             :         {
    8760          78 :           last->indices[jx].span++;
    8761          78 :           return decl;
    8762             :         }
    8763             :     }
    8764             : 
    8765             :   /* Append a new slot.  */
    8766        2582 :   tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
    8767             : 
    8768        2582 :   gcc_assert (!*mslot);
    8769        2582 :   *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
    8770             : 
    8771        2582 :   return decl;
    8772             : }
    8773             : 
    8774             : /* Pop off extraneous binding levels left over due to syntax errors.
    8775             :    We don't pop past namespaces, as they might be valid.  */
    8776             : 
    8777             : void
    8778       88402 : pop_everything (void)
    8779             : {
    8780       88402 :   if (ENABLE_SCOPE_CHECKING)
    8781             :     verbatim ("XXX entering %<pop_everything ()%>");
    8782       88402 :   while (!namespace_bindings_p ())
    8783             :     {
    8784           0 :       if (current_binding_level->kind == sk_class)
    8785           0 :         pop_nested_class ();
    8786             :       else
    8787           0 :         poplevel (0, 0, 0);
    8788             :     }
    8789       88402 :   if (ENABLE_SCOPE_CHECKING)
    8790             :     verbatim ("XXX leaving %<pop_everything ()%>");
    8791       88402 : }
    8792             : 
    8793             : /* Emit debugging information for using declarations and directives.
    8794             :    If input tree is overloaded fn then emit debug info for all
    8795             :    candidates.  */
    8796             : 
    8797             : void
    8798     4991695 : cp_emit_debug_info_for_using (tree t, tree context)
    8799             : {
    8800             :   /* Don't try to emit any debug information if we have errors.  */
    8801     4991695 :   if (seen_error ())
    8802             :     return;
    8803             : 
    8804             :   /* Do not supply context to imported_module_or_decl, if
    8805             :      it is a global namespace.  */
    8806     4989592 :   if (context == global_namespace)
    8807       64890 :     context = NULL_TREE;
    8808             : 
    8809     4989592 :   t = MAYBE_BASELINK_FUNCTIONS (t);
    8810             : 
    8811    15781538 :   for (lkp_iterator iter (t); iter; ++iter)
    8812             :     {
    8813     5802354 :       tree fn = *iter;
    8814             : 
    8815     5802354 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    8816             :         /* FIXME: Handle TEMPLATE_DECLs.  */
    8817      387857 :         continue;
    8818             :       
    8819             :       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
    8820             :          of a builtin function.  */
    8821     7219783 :       if (TREE_CODE (fn) == FUNCTION_DECL
    8822     4822078 :           && DECL_EXTERNAL (fn)
    8823    10236272 :           && fndecl_built_in_p (fn))
    8824     1805286 :         continue;
    8825             : 
    8826     3609211 :       if (building_stmt_list_p ())
    8827         605 :         add_stmt (build_stmt (input_location, USING_STMT, fn));
    8828             :       else
    8829     3608606 :         debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
    8830             :                                               false, false);
    8831             :     }
    8832             : }
    8833             : 
    8834             : /* True if D is a local declaration in dependent scope.  Assumes that it is
    8835             :    (part of) the current lookup result for its name.  */
    8836             : 
    8837             : bool
    8838    11798066 : dependent_local_decl_p (tree d)
    8839             : {
    8840    11798066 :   if (!DECL_LOCAL_DECL_P (d))
    8841             :     return false;
    8842             : 
    8843        8649 :   cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
    8844        8649 :   cp_binding_level *l = b->scope;
    8845       25910 :   while (!l->this_entity)
    8846       17261 :     l = l->level_chain;
    8847        8649 :   return uses_template_parms (l->this_entity);
    8848             : }
    8849             : 
    8850             : 
    8851             : 
    8852             : #include "gt-cp-name-lookup.h"

Generated by: LCOV version 1.16