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