libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / cp / name-lookup.cc
blob609bd6e8c9b50d1afeb0b59d2548de78411c3cc2
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
35 #include "c-family/name-hint.h"
36 #include "c-family/known-headers.h"
37 #include "c-family/c-spellcheck.h"
38 #include "bitmap.h"
40 static cxx_binding *cxx_binding_make (tree value, tree type);
41 static cp_binding_level *innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id, tree decl,
43 cp_binding_level *b);
44 static name_hint maybe_suggest_missing_std_header (location_t location,
45 tree name);
46 static name_hint suggest_alternatives_for_1 (location_t location, tree name,
47 bool suggest_misspellings);
49 /* Slots in BINDING_VECTOR. */
50 enum binding_slots
52 BINDING_SLOT_CURRENT, /* Slot for current TU. */
53 BINDING_SLOT_GLOBAL, /* Slot for merged global module. */
54 BINDING_SLOT_PARTITION, /* Slot for merged partition entities or
55 imported friends. */
57 /* Number of always-allocated slots. */
58 BINDING_SLOTS_FIXED = BINDING_SLOT_GLOBAL + 1
61 /* Create an overload suitable for recording an artificial TYPE_DECL
62 and another decl. We use this machanism to implement the struct
63 stat hack. */
65 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
66 #define STAT_TYPE_VISIBLE_P(N) TREE_USED (OVERLOAD_CHECK (N))
67 #define STAT_TYPE(N) TREE_TYPE (N)
68 #define STAT_DECL(N) OVL_FUNCTION (N)
69 #define STAT_VISIBLE(N) OVL_CHAIN (N)
70 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
71 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
73 /* When a STAT_HACK_P is true, OVL_USING_P and OVL_EXPORT_P are valid
74 and apply to the hacked type. */
76 /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
77 But we also need to indicate hiddenness on implicit type decls
78 (injected friend classes), and (coming soon) decls injected from
79 block-scope externs. It is too awkward to press the existing
80 overload marking for that. If we have a hidden non-function, we
81 always create a STAT_HACK, and use these two markers as needed. */
82 #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
83 #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
85 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
86 the type binding. */
88 static tree
89 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
91 tree result = make_node (OVERLOAD);
93 /* Mark this as a lookup, so we can tell this is a stat hack. */
94 OVL_LOOKUP_P (result) = true;
95 STAT_DECL (result) = decl;
96 STAT_TYPE (result) = type;
97 return result;
100 /* Create a local binding level for NAME. */
102 static cxx_binding *
103 create_local_binding (cp_binding_level *level, tree name)
105 cxx_binding *binding = cxx_binding_make (NULL, NULL);
107 LOCAL_BINDING_P (binding) = true;
108 binding->scope = level;
109 binding->previous = IDENTIFIER_BINDING (name);
111 IDENTIFIER_BINDING (name) = binding;
113 return binding;
116 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
117 make an empty binding if there wasn't one. */
119 static tree *
120 find_namespace_slot (tree ns, tree name, bool create_p = false)
122 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
123 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
124 create_p ? INSERT : NO_INSERT);
125 return slot;
128 static tree
129 find_namespace_value (tree ns, tree name)
131 tree *b = find_namespace_slot (ns, name);
133 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
136 /* Look in *SLOT for a the binding of NAME in imported module IX.
137 Returns pointer to binding's slot, or NULL if not found. Does a
138 binary search, as this is mainly used for random access during
139 importing. Do not use for the fixed slots. */
141 static binding_slot *
142 search_imported_binding_slot (tree *slot, unsigned ix)
144 gcc_assert (ix);
146 if (!*slot)
147 return NULL;
149 if (TREE_CODE (*slot) != BINDING_VECTOR)
150 return NULL;
152 unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
153 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
155 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
157 clusters--;
158 cluster++;
161 while (clusters > 1)
163 unsigned half = clusters / 2;
164 gcc_checking_assert (cluster[half].indices[0].span);
165 if (cluster[half].indices[0].base > ix)
166 clusters = half;
167 else
169 clusters -= half;
170 cluster += half;
174 if (clusters)
175 /* Is it in this cluster? */
176 for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
178 if (!cluster->indices[off].span)
179 break;
180 if (cluster->indices[off].base > ix)
181 break;
183 if (cluster->indices[off].base + cluster->indices[off].span > ix)
184 return &cluster->slots[off];
187 return NULL;
190 static void
191 init_global_partition (binding_cluster *cluster, tree decl)
193 bool named = true;
195 if (header_module_p ())
196 named = false;
197 else if (TREE_PUBLIC (decl)
198 && TREE_CODE (decl) == NAMESPACE_DECL
199 && !DECL_NAMESPACE_ALIAS (decl))
200 named = false;
201 else if (!get_originating_module (decl))
202 named = false;
204 binding_slot *mslot;
205 if (named)
206 mslot = &cluster[BINDING_SLOT_PARTITION
207 / BINDING_VECTOR_SLOTS_PER_CLUSTER]
208 .slots[BINDING_SLOT_PARTITION
209 % BINDING_VECTOR_SLOTS_PER_CLUSTER];
210 else
211 mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
213 if (*mslot)
214 decl = ovl_make (decl, *mslot);
215 *mslot = decl;
217 if (TREE_CODE (decl) == CONST_DECL)
219 tree type = TREE_TYPE (decl);
220 if (TREE_CODE (type) == ENUMERAL_TYPE
221 && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type)))
222 && decl == TREE_VALUE (TYPE_VALUES (type)))
223 /* Anonymous enums are keyed by their first enumerator, put
224 the TYPE_DECL here too. */
225 *mslot = ovl_make (TYPE_NAME (type), *mslot);
229 /* Get the fixed binding slot IX. Creating the vector if CREATE is
230 non-zero. If CREATE is < 0, make sure there is at least 1 spare
231 slot for an import. (It is an error for CREATE < 0 and the slot to
232 already exist.) */
234 static tree *
235 get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
237 gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
239 /* An assumption is that the fixed slots all reside in one cluster. */
240 gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
242 if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
244 if (ix == BINDING_SLOT_CURRENT)
245 /* The current TU can just use slot directly. */
246 return slot;
248 if (!create)
249 return NULL;
251 /* The partition slot is always needed, in case we have imported
252 temploid friends with attachment different from the module we
253 imported them from. */
254 bool partition_slot = true;
255 unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
256 + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
257 / BINDING_VECTOR_SLOTS_PER_CLUSTER);
258 tree new_vec = make_binding_vec (name, want);
259 BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
260 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
262 /* Initialize the fixed slots. */
263 for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
265 cluster[0].indices[jx].base = 0;
266 cluster[0].indices[jx].span = 1;
267 cluster[0].slots[jx] = NULL_TREE;
270 if (partition_slot)
272 unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
273 unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
274 cluster[ind].indices[off].base = 0;
275 cluster[ind].indices[off].span = 1;
276 cluster[ind].slots[off] = NULL_TREE;
279 if (tree orig = *slot)
281 /* Propagate existing value to current slot. */
283 /* Propagate global & module entities to the global and
284 partition slots. */
285 if (tree type = strip_using_decl (MAYBE_STAT_TYPE (orig)))
286 init_global_partition (cluster, type);
288 for (ovl_iterator iter (strip_using_decl (MAYBE_STAT_DECL (orig)));
289 iter; ++iter)
291 tree decl = *iter;
293 /* Internal linkage entities are in deduplicateable. */
294 init_global_partition (cluster, decl);
297 if (cluster[0].slots[BINDING_SLOT_GLOBAL]
298 && !(TREE_CODE (orig) == NAMESPACE_DECL
299 && !DECL_NAMESPACE_ALIAS (orig)))
301 /* Note that we had some GMF entries. */
302 if (!STAT_HACK_P (orig))
303 orig = stat_hack (orig);
305 MODULE_BINDING_GLOBAL_P (orig) = true;
308 cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
311 *slot = new_vec;
313 else
314 gcc_checking_assert (create >= 0);
316 unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
317 binding_cluster &cluster
318 = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
320 /* There must always be slots for these indices */
321 gcc_checking_assert (cluster.indices[off].span == 1
322 && !cluster.indices[off].base
323 && !cluster.slots[off].is_lazy ());
325 return reinterpret_cast<tree *> (&cluster.slots[off]);
328 /* *SLOT is a namespace binding slot. Append a slot for imported
329 module IX. */
331 static binding_slot *
332 append_imported_binding_slot (tree *slot, tree name, unsigned ix)
334 gcc_checking_assert (ix);
336 if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
337 /* Make an initial module vector. */
338 get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
339 else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
340 ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
341 /* There is space in the last cluster. */;
342 else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
343 != BINDING_VECTOR_ALLOC_CLUSTERS (*slot))
344 /* There is space in the vector. */
345 BINDING_VECTOR_NUM_CLUSTERS (*slot)++;
346 else
348 /* Extend the vector. */
349 unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
350 unsigned want = (have * 3 + 1) / 2;
352 if (want > (unsigned short)~0)
353 want = (unsigned short)~0;
355 tree new_vec = make_binding_vec (name, want);
356 BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
357 BINDING_VECTOR_GLOBAL_DUPS_P (new_vec)
358 = BINDING_VECTOR_GLOBAL_DUPS_P (*slot);
359 BINDING_VECTOR_PARTITION_DUPS_P (new_vec)
360 = BINDING_VECTOR_PARTITION_DUPS_P (*slot);
361 memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
362 BINDING_VECTOR_CLUSTER_BASE (*slot),
363 have * sizeof (binding_cluster));
364 *slot = new_vec;
367 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
368 for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
369 if (!last->indices[off].span)
371 /* Fill the free slot of the cluster. */
372 last->indices[off].base = ix;
373 last->indices[off].span = 1;
374 last->slots[off] = NULL_TREE;
375 /* Check monotonicity. */
376 gcc_checking_assert (last[off ? 0 : -1]
377 .indices[off ? off - 1
378 : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
379 .base < ix);
380 return &last->slots[off];
383 gcc_unreachable ();
386 /* Add DECL to the list of things declared in binding level B. */
388 static void
389 add_decl_to_level (cp_binding_level *b, tree decl)
391 gcc_assert (b->kind != sk_class);
393 /* Make sure we don't create a circular list. xref_tag can end
394 up pushing the same artificial decl more than once. We
395 should have already detected that in update_binding. (This isn't a
396 complete verification of non-circularity.) */
397 gcc_assert (b->names != decl);
399 /* We build up the list in reverse order, and reverse it later if
400 necessary. */
401 TREE_CHAIN (decl) = b->names;
402 b->names = decl;
404 /* If appropriate, add decl to separate list of statics. We include
405 extern variables because they might turn out to be static later.
406 It's OK for this list to contain a few false positives. */
407 if (b->kind == sk_namespace
408 && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
409 || (TREE_CODE (decl) == FUNCTION_DECL
410 && (!TREE_PUBLIC (decl)
411 || decl_internal_context_p (decl)
412 || DECL_DECLARED_INLINE_P (decl)))))
413 vec_safe_push (static_decls, decl);
416 /* Find the binding for NAME in the local binding level B. */
418 static cxx_binding *
419 find_local_binding (cp_binding_level *b, tree name)
421 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
422 for (;; b = b->level_chain)
424 if (binding->scope == b)
425 return binding;
427 /* Cleanup contours are transparent to the language. */
428 if (b->kind != sk_cleanup)
429 break;
431 return NULL;
434 class name_lookup
436 public:
437 typedef std::pair<tree, tree> using_pair;
438 typedef auto_vec<using_pair, 16> using_queue;
440 public:
441 tree name; /* The identifier being looked for. */
443 /* Usually we just add things to the VALUE binding, but we record
444 (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
445 using-decl resolution. */
446 tree value; /* A (possibly ambiguous) set of things found. */
447 tree type; /* A type that has been found. */
449 LOOK_want want; /* What kind of entity we want. */
451 bool deduping; /* Full deduping is needed because using declarations
452 are in play. */
453 vec<tree, va_heap, vl_embed> *scopes;
454 name_lookup *previous; /* Previously active lookup. */
456 protected:
457 /* Marked scope stack for outermost name lookup. */
458 static vec<tree, va_heap, vl_embed> *shared_scopes;
459 /* Currently active lookup. */
460 static name_lookup *active;
462 public:
463 name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
464 : name (n), value (NULL_TREE), type (NULL_TREE),
465 want (w),
466 deduping (false), scopes (NULL), previous (NULL)
468 preserve_state ();
470 ~name_lookup ()
472 gcc_checking_assert (!deduping);
473 restore_state ();
476 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
477 name_lookup (const name_lookup &);
478 name_lookup &operator= (const name_lookup &);
480 public:
481 /* Turn on or off deduping mode. */
482 void dedup (bool state)
484 if (deduping != state)
486 deduping = state;
487 lookup_mark (value, state);
491 protected:
492 static bool seen_p (tree scope)
494 return LOOKUP_SEEN_P (scope);
496 static bool found_p (tree scope)
498 return LOOKUP_FOUND_P (scope);
501 void mark_seen (tree scope); /* Mark and add to scope vector. */
502 static void mark_found (tree scope)
504 gcc_checking_assert (seen_p (scope));
505 LOOKUP_FOUND_P (scope) = true;
507 bool see_and_mark (tree scope)
509 bool ret = seen_p (scope);
510 if (!ret)
511 mark_seen (scope);
512 return ret;
514 bool find_and_mark (tree scope);
516 private:
517 void preserve_state ();
518 void restore_state ();
520 public:
521 static tree ambiguous (tree thing, tree current);
522 void add_value (tree new_val);
523 private:
524 void add_overload (tree fns);
525 void add_type (tree new_type);
526 bool process_binding (tree val_bind, tree type_bind);
527 unsigned process_module_binding (tree val_bind, tree type_bind, unsigned);
528 /* Look in only namespace. */
529 bool search_namespace_only (tree scope);
530 /* Look in namespace and its (recursive) inlines. Ignore using
531 directives. Return true if something found (inc dups). */
532 bool search_namespace (tree scope);
533 /* Look in the using directives of namespace + inlines using
534 qualified lookup rules. */
535 bool search_usings (tree scope);
537 private:
538 void queue_namespace (using_queue& queue, int depth, tree scope);
539 void queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings);
541 private:
542 void add_fns (tree);
544 private:
545 void adl_expr (tree);
546 void adl_type (tree);
547 void adl_template_arg (tree);
548 void adl_class (tree);
549 void adl_enum (tree);
550 void adl_bases (tree);
551 void adl_class_only (tree);
552 void adl_namespace (tree);
553 void adl_class_fns (tree);
554 void adl_namespace_fns (tree, bitmap);
556 public:
557 /* Search namespace + inlines + maybe usings as qualified lookup. */
558 bool search_qualified (tree scope, bool usings = true);
560 /* Search namespace + inlines + usings as unqualified lookup. */
561 bool search_unqualified (tree scope, cp_binding_level *);
563 /* ADL lookup of ARGS. */
564 tree search_adl (tree fns, vec<tree, va_gc> *args);
567 /* Scope stack shared by all outermost lookups. This avoids us
568 allocating and freeing on every single lookup. */
569 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
571 /* Currently active lookup. */
572 name_lookup *name_lookup::active;
574 /* Name lookup is recursive, becase ADL can cause template
575 instatiation. This is of course a rare event, so we optimize for
576 it not happening. When we discover an active name-lookup, which
577 must be an ADL lookup, we need to unmark the marked scopes and also
578 unmark the lookup we might have been accumulating. */
580 void
581 name_lookup::preserve_state ()
583 previous = active;
584 if (previous)
586 unsigned length = vec_safe_length (previous->scopes);
587 vec_safe_reserve (previous->scopes, length * 2);
588 for (unsigned ix = length; ix--;)
590 tree decl = (*previous->scopes)[ix];
592 gcc_checking_assert (LOOKUP_SEEN_P (decl));
593 LOOKUP_SEEN_P (decl) = false;
595 /* Preserve the FOUND_P state on the interrupted lookup's
596 stack. */
597 if (LOOKUP_FOUND_P (decl))
599 LOOKUP_FOUND_P (decl) = false;
600 previous->scopes->quick_push (decl);
604 /* Unmark the outer partial lookup. */
605 if (previous->deduping)
606 lookup_mark (previous->value, false);
608 else
609 scopes = shared_scopes;
610 active = this;
613 /* Restore the marking state of a lookup we interrupted. */
615 void
616 name_lookup::restore_state ()
618 gcc_checking_assert (!deduping);
620 /* Unmark and empty this lookup's scope stack. */
621 for (unsigned ix = vec_safe_length (scopes); ix--;)
623 tree decl = scopes->pop ();
624 gcc_checking_assert (LOOKUP_SEEN_P (decl));
625 LOOKUP_SEEN_P (decl) = false;
626 LOOKUP_FOUND_P (decl) = false;
629 active = previous;
630 if (previous)
632 free (scopes);
634 unsigned length = vec_safe_length (previous->scopes);
635 for (unsigned ix = 0; ix != length; ix++)
637 tree decl = (*previous->scopes)[ix];
638 if (LOOKUP_SEEN_P (decl))
640 /* The remainder of the scope stack must be recording
641 FOUND_P decls, which we want to pop off. */
644 tree decl = previous->scopes->pop ();
645 gcc_checking_assert (LOOKUP_SEEN_P (decl)
646 && !LOOKUP_FOUND_P (decl));
647 LOOKUP_FOUND_P (decl) = true;
649 while (++ix != length);
650 break;
653 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
654 LOOKUP_SEEN_P (decl) = true;
657 /* Remark the outer partial lookup. */
658 if (previous->deduping)
659 lookup_mark (previous->value, true);
661 else
662 shared_scopes = scopes;
665 void
666 name_lookup::mark_seen (tree scope)
668 gcc_checking_assert (!seen_p (scope));
669 LOOKUP_SEEN_P (scope) = true;
670 vec_safe_push (scopes, scope);
673 bool
674 name_lookup::find_and_mark (tree scope)
676 bool result = LOOKUP_FOUND_P (scope);
677 if (!result)
679 LOOKUP_FOUND_P (scope) = true;
680 if (!LOOKUP_SEEN_P (scope))
681 vec_safe_push (scopes, scope);
684 return result;
687 /* THING and CURRENT are ambiguous, concatenate them. */
689 tree
690 name_lookup::ambiguous (tree thing, tree current)
692 if (TREE_CODE (current) != TREE_LIST)
694 current = build_tree_list (NULL_TREE, current);
695 TREE_TYPE (current) = error_mark_node;
697 current = tree_cons (NULL_TREE, thing, current);
698 TREE_TYPE (current) = error_mark_node;
700 return current;
703 /* FNS is a new overload set to add to the exising set. */
705 void
706 name_lookup::add_overload (tree fns)
708 if (!deduping && TREE_CODE (fns) == OVERLOAD)
710 tree probe = fns;
711 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
712 probe = ovl_skip_hidden (probe);
713 if (probe && TREE_CODE (probe) == OVERLOAD
714 && OVL_DEDUP_P (probe))
715 /* We're about to add something found by multiple paths, so need to
716 engage deduping mode. */
717 dedup (true);
720 value = lookup_maybe_add (fns, value, deduping);
723 /* Add a NEW_VAL, a found value binding into the current value binding. */
725 void
726 name_lookup::add_value (tree new_val)
728 if (OVL_P (new_val) && (!value || OVL_P (value)))
729 add_overload (new_val);
730 else if (!value)
731 value = new_val;
732 else if (value == new_val)
734 else if ((TREE_CODE (value) == TYPE_DECL
735 && TREE_CODE (new_val) == TYPE_DECL
736 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
737 /* Typedefs to the same type. */;
738 else if (TREE_CODE (value) == NAMESPACE_DECL
739 && TREE_CODE (new_val) == NAMESPACE_DECL
740 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
741 /* Namespace (possibly aliased) to the same namespace. Locate
742 the namespace*/
743 value = ORIGINAL_NAMESPACE (value);
744 else
746 /* Disengage deduping mode. */
747 dedup (false);
748 value = ambiguous (new_val, value);
752 /* Add a NEW_TYPE, a found type binding into the current type binding. */
754 void
755 name_lookup::add_type (tree new_type)
757 if (!type)
758 type = new_type;
759 else if (TREE_CODE (type) == TREE_LIST
760 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
761 type = ambiguous (new_type, type);
764 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
765 true if we actually found something noteworthy. Hiddenness has
766 already been handled in the caller. */
768 bool
769 name_lookup::process_binding (tree new_val, tree new_type)
771 /* Did we really see a type? */
772 if (new_type
773 && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
774 new_type = NULL_TREE;
776 new_val = strip_using_decl (new_val);
777 new_type = strip_using_decl (new_type);
779 /* Do we really see a value? */
780 if (new_val)
781 switch (TREE_CODE (new_val))
783 case TEMPLATE_DECL:
784 /* If we expect types or namespaces, and not templates,
785 or this is not a template class. */
786 if (bool (want & LOOK_want::TYPE_NAMESPACE)
787 && !DECL_TYPE_TEMPLATE_P (new_val))
788 new_val = NULL_TREE;
789 break;
790 case TYPE_DECL:
791 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
792 || (new_type && bool (want & LOOK_want::TYPE)))
793 new_val = NULL_TREE;
794 break;
795 case NAMESPACE_DECL:
796 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
797 new_val = NULL_TREE;
798 break;
799 default:
800 if (bool (want & LOOK_want::TYPE_NAMESPACE))
801 new_val = NULL_TREE;
804 if (!new_val)
806 new_val = new_type;
807 new_type = NULL_TREE;
810 /* Merge into the lookup */
811 if (new_val)
812 add_value (new_val);
813 if (new_type)
814 add_type (new_type);
816 return new_val != NULL_TREE;
819 /* If we're importing a module containing this binding, add it to the
820 lookup set. The trickiness is with namespaces, we only want to
821 find it once. */
823 unsigned
824 name_lookup::process_module_binding (tree new_val, tree new_type,
825 unsigned marker)
827 /* Optimize for (re-)finding a public namespace. We only need to
828 look once. */
829 if (new_val && !new_type
830 && TREE_CODE (new_val) == NAMESPACE_DECL
831 && TREE_PUBLIC (new_val)
832 && !DECL_NAMESPACE_ALIAS (new_val))
834 if (marker & 2)
835 return marker;
836 marker |= 2;
839 if (new_type || new_val)
840 marker |= process_binding (new_val, new_type);
842 return marker;
845 /* Look in exactly namespace SCOPE. */
847 bool
848 name_lookup::search_namespace_only (tree scope)
850 bool found = false;
851 if (tree *binding = find_namespace_slot (scope, name))
853 tree val = *binding;
854 if (TREE_CODE (val) == BINDING_VECTOR)
856 /* I presume the binding list is going to be sparser than
857 the import bitmap. Hence iterate over the former
858 checking for bits set in the bitmap. */
859 bitmap imports = get_import_bitmap ();
860 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
861 int marker = 0;
862 int dup_detect = 0;
864 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
866 if (!deduping)
868 if (named_module_purview_p ())
870 dup_detect |= 2;
872 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
873 dup_detect |= 1;
875 else
876 dup_detect |= 1;
878 tree type = NULL_TREE;
879 tree value = bind;
881 if (STAT_HACK_P (bind))
883 type = STAT_TYPE (bind);
884 value = STAT_DECL (bind);
886 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
888 if (STAT_TYPE_HIDDEN_P (bind))
889 type = NULL_TREE;
890 if (STAT_DECL_HIDDEN_P (bind))
891 value = NULL_TREE;
892 else
893 value = ovl_skip_hidden (value);
896 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
897 value = ovl_skip_hidden (value);
899 marker = process_module_binding (value, type, marker);
902 /* Scan the imported bindings. */
903 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
904 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
906 ix--;
907 cluster++;
910 /* Do this in forward order, so we load modules in an order
911 the user expects. */
912 for (; ix--; cluster++)
913 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
915 /* Are we importing this module? */
916 if (unsigned base = cluster->indices[jx].base)
917 if (unsigned span = cluster->indices[jx].span)
919 if (bool (want & LOOK_want::ANY_REACHABLE)
920 || bitmap_bit_p (imports, base))
921 goto found;
922 while (++base, --span);
923 continue;
925 found:;
926 /* Is it loaded? */
927 if (cluster->slots[jx].is_lazy ())
929 gcc_assert (cluster->indices[jx].span == 1);
930 lazy_load_binding (cluster->indices[jx].base,
931 scope, name, &cluster->slots[jx]);
933 tree bind = cluster->slots[jx];
934 if (!bind)
935 /* Load errors could mean there's nothing here. */
936 continue;
938 /* Extract what we can see from here. If there's no
939 stat_hack, then everything was exported. */
940 tree type = NULL_TREE;
942 /* If STAT_HACK_P is false, everything is visible, and
943 there's no duplication possibilities. */
944 if (STAT_HACK_P (bind))
946 if (!deduping)
948 /* Do we need to engage deduplication? */
949 int dup = 0;
950 if (MODULE_BINDING_GLOBAL_P (bind))
951 dup |= 1;
952 if (MODULE_BINDING_PARTITION_P (bind))
953 dup |= 2;
954 if (unsigned hit = dup_detect & dup)
956 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
957 || (hit & 2
958 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
959 dedup (true);
961 dup_detect |= dup;
964 if (bool (want & LOOK_want::ANY_REACHABLE))
966 type = STAT_TYPE (bind);
967 bind = STAT_DECL (bind);
969 else
971 if (STAT_TYPE_VISIBLE_P (bind))
972 type = STAT_TYPE (bind);
973 bind = STAT_VISIBLE (bind);
977 /* And process it. */
978 marker = process_module_binding (bind, type, marker);
980 found |= marker & 1;
982 else
984 /* Only a current module binding, visible from the current module. */
985 tree bind = *binding;
986 tree value = bind, type = NULL_TREE;
988 if (STAT_HACK_P (bind))
990 type = STAT_TYPE (bind);
991 value = STAT_DECL (bind);
993 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
995 if (STAT_TYPE_HIDDEN_P (bind))
996 type = NULL_TREE;
997 if (STAT_DECL_HIDDEN_P (bind))
998 value = NULL_TREE;
999 else
1000 value = ovl_skip_hidden (value);
1003 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
1004 value = ovl_skip_hidden (value);
1006 found |= process_binding (value, type);
1010 return found;
1013 /* Conditionally look in namespace SCOPE and inline children. */
1015 bool
1016 name_lookup::search_namespace (tree scope)
1018 if (see_and_mark (scope))
1019 /* We've visited this scope before. Return what we found then. */
1020 return found_p (scope);
1022 /* Look in exactly namespace. */
1023 bool found = search_namespace_only (scope);
1025 /* Don't look into inline children, if we're looking for an
1026 anonymous name -- it must be in the current scope, if anywhere. */
1027 if (name)
1028 /* Recursively look in its inline children. */
1029 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1030 for (unsigned ix = inlinees->length (); ix--;)
1031 found |= search_namespace ((*inlinees)[ix]);
1033 if (found)
1034 mark_found (scope);
1036 return found;
1039 /* Recursively follow using directives of SCOPE & its inline children.
1040 Such following is essentially a flood-fill algorithm. */
1042 bool
1043 name_lookup::search_usings (tree scope)
1045 /* We do not check seen_p here, as that was already set during the
1046 namespace_only walk. */
1047 if (found_p (scope))
1048 return true;
1050 bool found = false;
1051 if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
1052 for (unsigned ix = usings->length (); ix--;)
1053 found |= search_qualified ((*usings)[ix], true);
1055 /* Look in its inline children. */
1056 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1057 for (unsigned ix = inlinees->length (); ix--;)
1058 found |= search_usings ((*inlinees)[ix]);
1060 if (found)
1061 mark_found (scope);
1063 return found;
1066 /* Qualified namespace lookup in SCOPE.
1067 1) Look in SCOPE (+inlines). If found, we're done.
1068 2) Otherwise, if USINGS is true,
1069 recurse for every using directive of SCOPE (+inlines).
1071 Trickiness is (a) loops and (b) multiple paths to same namespace.
1072 In both cases we want to not repeat any lookups, and know whether
1073 to stop the caller's step #2. Do this via the FOUND_P marker. */
1075 bool
1076 name_lookup::search_qualified (tree scope, bool usings)
1078 bool found = false;
1080 if (seen_p (scope))
1081 found = found_p (scope);
1082 else
1084 found = search_namespace (scope);
1085 if (!found && usings)
1086 found = search_usings (scope);
1089 dedup (false);
1091 return found;
1094 /* Add SCOPE to the unqualified search queue, recursively add its
1095 inlines and those via using directives. */
1097 void
1098 name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
1100 if (see_and_mark (scope))
1101 return;
1103 /* Record it. */
1104 tree common = scope;
1105 while (SCOPE_DEPTH (common) > depth)
1106 common = CP_DECL_CONTEXT (common);
1107 queue.safe_push (using_pair (common, scope));
1109 /* Queue its inline children. */
1110 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1111 for (unsigned ix = inlinees->length (); ix--;)
1112 queue_namespace (queue, depth, (*inlinees)[ix]);
1114 /* Queue its using targets. */
1115 queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
1118 /* Add the namespaces in USINGS to the unqualified search queue. */
1120 void
1121 name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
1123 if (usings)
1124 for (unsigned ix = usings->length (); ix--;)
1125 queue_namespace (queue, depth, (*usings)[ix]);
1128 /* Unqualified namespace lookup in SCOPE.
1129 1) add scope+inlins to worklist.
1130 2) recursively add target of every using directive
1131 3) for each worklist item where SCOPE is common ancestor, search it
1132 4) if nothing find, scope=parent, goto 1. */
1134 bool
1135 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
1137 using_queue queue;
1138 bool found = false;
1140 /* Queue local using-directives. */
1141 for (; level->kind != sk_namespace; level = level->level_chain)
1142 queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
1144 for (; !found; scope = CP_DECL_CONTEXT (scope))
1146 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
1147 int depth = SCOPE_DEPTH (scope);
1149 /* Queue namespaces reachable from SCOPE. */
1150 queue_namespace (queue, depth, scope);
1152 /* Search every queued namespace where SCOPE is the common
1153 ancestor. Adjust the others. */
1154 unsigned ix = 0;
1157 using_pair &pair = queue[ix];
1158 while (pair.first == scope)
1160 found |= search_namespace_only (pair.second);
1161 pair = queue.pop ();
1162 if (ix == queue.length ())
1163 goto done;
1165 /* The depth is the same as SCOPE, find the parent scope. */
1166 if (SCOPE_DEPTH (pair.first) == depth)
1167 pair.first = CP_DECL_CONTEXT (pair.first);
1168 ix++;
1170 while (ix < queue.length ());
1171 done:;
1172 if (scope == global_namespace)
1173 break;
1175 /* If looking for hidden friends, we only look in the innermost
1176 namespace scope. [namespace.memdef]/3 If a friend
1177 declaration in a non-local class first declares a class,
1178 function, class template or function template the friend is a
1179 member of the innermost enclosing namespace. See also
1180 [basic.lookup.unqual]/7 */
1181 if (bool (want & LOOK_want::HIDDEN_FRIEND))
1182 break;
1185 dedup (false);
1187 return found;
1190 /* FNS is a value binding. If it is a (set of overloaded) functions,
1191 add them into the current value. */
1193 void
1194 name_lookup::add_fns (tree fns)
1196 if (!fns)
1197 return;
1198 else if (TREE_CODE (fns) == OVERLOAD)
1200 if (TREE_TYPE (fns) != unknown_type_node)
1201 fns = OVL_FUNCTION (fns);
1203 else if (!DECL_DECLARES_FUNCTION_P (fns))
1204 return;
1206 add_overload (fns);
1209 /* Add the overloaded fns of SCOPE. */
1211 void
1212 name_lookup::adl_namespace_fns (tree scope, bitmap imports)
1214 if (tree *binding = find_namespace_slot (scope, name))
1216 tree val = *binding;
1217 if (TREE_CODE (val) != BINDING_VECTOR)
1218 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
1219 else
1221 /* I presume the binding list is going to be sparser than
1222 the import bitmap. Hence iterate over the former
1223 checking for bits set in the bitmap. */
1224 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
1225 int dup_detect = 0;
1227 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
1229 /* The current TU's bindings must be visible, we don't
1230 need to check the bitmaps. */
1232 if (!deduping)
1234 if (named_module_purview_p ())
1236 dup_detect |= 2;
1238 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
1239 dup_detect |= 1;
1241 else
1242 dup_detect |= 1;
1245 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
1248 /* Scan the imported bindings. */
1249 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
1250 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
1252 ix--;
1253 cluster++;
1256 /* Do this in forward order, so we load modules in an order
1257 the user expects. */
1258 for (; ix--; cluster++)
1259 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
1261 /* Functions are never on merged slots. */
1262 if (!cluster->indices[jx].base
1263 || cluster->indices[jx].span != 1)
1264 continue;
1266 /* Is this slot visible? */
1267 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
1268 continue;
1270 /* Is it loaded. */
1271 if (cluster->slots[jx].is_lazy ())
1272 lazy_load_binding (cluster->indices[jx].base,
1273 scope, name, &cluster->slots[jx]);
1275 tree bind = cluster->slots[jx];
1276 if (!bind)
1277 /* Load errors could mean there's nothing here. */
1278 continue;
1280 if (STAT_HACK_P (bind))
1282 if (!deduping)
1284 /* Do we need to engage deduplication? */
1285 int dup = 0;
1286 if (MODULE_BINDING_GLOBAL_P (bind))
1287 dup |= 1;
1288 if (MODULE_BINDING_PARTITION_P (bind))
1289 dup |= 2;
1290 if (unsigned hit = dup_detect & dup)
1291 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
1292 || (hit & 2
1293 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
1294 dedup (true);
1295 dup_detect |= dup;
1298 bind = STAT_VISIBLE (bind);
1301 add_fns (bind);
1307 /* Add the hidden friends of SCOPE. */
1309 void
1310 name_lookup::adl_class_fns (tree type)
1312 /* Add friends. */
1313 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
1314 list; list = TREE_CHAIN (list))
1315 if (name == FRIEND_NAME (list))
1317 tree context = NULL_TREE; /* Lazily computed. */
1318 for (tree friends = FRIEND_DECLS (list); friends;
1319 friends = TREE_CHAIN (friends))
1321 tree fn = TREE_VALUE (friends);
1323 /* Only interested in global functions with potentially hidden
1324 (i.e. unqualified) declarations. */
1325 if (!context)
1326 context = decl_namespace_context (type);
1327 if (CP_DECL_CONTEXT (fn) != context)
1328 continue;
1330 dedup (true);
1332 /* Template specializations are never found by name lookup.
1333 (Templates themselves can be found, but not template
1334 specializations.) */
1335 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
1336 continue;
1338 add_fns (fn);
1343 /* Find the containing non-inlined namespace, add it and all its
1344 inlinees. */
1346 void
1347 name_lookup::adl_namespace (tree scope)
1349 if (see_and_mark (scope))
1350 return;
1352 /* Look down into inline namespaces. */
1353 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1354 for (unsigned ix = inlinees->length (); ix--;)
1355 adl_namespace ((*inlinees)[ix]);
1357 if (DECL_NAMESPACE_INLINE_P (scope))
1358 /* Mark parent. */
1359 adl_namespace (CP_DECL_CONTEXT (scope));
1362 /* Adds the class and its friends to the lookup structure. */
1364 void
1365 name_lookup::adl_class_only (tree type)
1367 /* Backend-built structures, such as __builtin_va_list, aren't
1368 affected by all this. */
1369 if (!CLASS_TYPE_P (type))
1370 return;
1372 type = TYPE_MAIN_VARIANT (type);
1374 if (see_and_mark (type))
1375 return;
1377 tree context = decl_namespace_context (type);
1378 adl_namespace (context);
1381 /* Adds the class and its bases to the lookup structure.
1382 Returns true on error. */
1384 void
1385 name_lookup::adl_bases (tree type)
1387 adl_class_only (type);
1389 /* Process baseclasses. */
1390 if (tree binfo = TYPE_BINFO (type))
1392 tree base_binfo;
1393 int i;
1395 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1396 adl_bases (BINFO_TYPE (base_binfo));
1400 /* Adds everything associated with a class argument type to the lookup
1401 structure.
1403 If T is a class type (including unions), its associated classes are: the
1404 class itself; the class of which it is a member, if any; and its direct
1405 and indirect base classes. Its associated namespaces are the namespaces
1406 of which its associated classes are members. Furthermore, if T is a
1407 class template specialization, its associated namespaces and classes
1408 also include: the namespaces and classes associated with the types of
1409 the template arguments provided for template type parameters (excluding
1410 template template parameters); the namespaces of which any template
1411 template arguments are members; and the classes of which any member
1412 templates used as template template arguments are members. [ Note:
1413 non-type template arguments do not contribute to the set of associated
1414 namespaces. --end note] */
1416 void
1417 name_lookup::adl_class (tree type)
1419 /* Backend build structures, such as __builtin_va_list, aren't
1420 affected by all this. */
1421 if (!CLASS_TYPE_P (type))
1422 return;
1424 type = TYPE_MAIN_VARIANT (type);
1426 /* We don't set found here because we have to have set seen first,
1427 which is done in the adl_bases walk. */
1428 if (found_p (type))
1429 return;
1431 complete_type (type);
1432 adl_bases (type);
1433 mark_found (type);
1435 if (TYPE_CLASS_SCOPE_P (type))
1436 adl_class_only (TYPE_CONTEXT (type));
1438 /* Process template arguments. */
1439 if (CLASSTYPE_TEMPLATE_INFO (type)
1440 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
1442 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1443 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
1444 adl_template_arg (TREE_VEC_ELT (list, i));
1448 void
1449 name_lookup::adl_enum (tree type)
1451 type = TYPE_MAIN_VARIANT (type);
1452 if (see_and_mark (type))
1453 return;
1455 if (TYPE_CLASS_SCOPE_P (type))
1456 adl_class_only (TYPE_CONTEXT (type));
1457 else
1458 adl_namespace (decl_namespace_context (type));
1461 void
1462 name_lookup::adl_expr (tree expr)
1464 if (!expr)
1465 return;
1467 gcc_assert (!TYPE_P (expr));
1469 if (TREE_TYPE (expr) != unknown_type_node)
1471 adl_type (unlowered_expr_type (expr));
1472 return;
1475 if (TREE_CODE (expr) == ADDR_EXPR)
1476 expr = TREE_OPERAND (expr, 0);
1477 if (TREE_CODE (expr) == COMPONENT_REF
1478 || TREE_CODE (expr) == OFFSET_REF)
1479 expr = TREE_OPERAND (expr, 1);
1480 expr = MAYBE_BASELINK_FUNCTIONS (expr);
1482 if (OVL_P (expr))
1483 for (lkp_iterator iter (expr); iter; ++iter)
1484 adl_type (TREE_TYPE (*iter));
1485 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
1487 /* The working paper doesn't currently say how to handle
1488 template-id arguments. The sensible thing would seem to be
1489 to handle the list of template candidates like a normal
1490 overload set, and handle the template arguments like we do
1491 for class template specializations. */
1493 /* First the templates. */
1494 adl_expr (TREE_OPERAND (expr, 0));
1496 /* Now the arguments. */
1497 if (tree args = TREE_OPERAND (expr, 1))
1498 for (int ix = TREE_VEC_LENGTH (args); ix--;)
1499 adl_template_arg (TREE_VEC_ELT (args, ix));
1503 void
1504 name_lookup::adl_type (tree type)
1506 if (!type)
1507 return;
1509 if (TYPE_PTRDATAMEM_P (type))
1511 /* Pointer to member: associate class type and value type. */
1512 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
1513 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1514 return;
1517 switch (TREE_CODE (type))
1519 case RECORD_TYPE:
1520 if (TYPE_PTRMEMFUNC_P (type))
1522 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
1523 return;
1525 /* FALLTHRU */
1526 case UNION_TYPE:
1527 adl_class (type);
1528 return;
1530 case METHOD_TYPE:
1531 /* The basetype is referenced in the first arg type, so just
1532 fall through. */
1533 case FUNCTION_TYPE:
1534 /* Associate the parameter types. */
1535 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
1536 adl_type (TREE_VALUE (args));
1537 /* FALLTHROUGH */
1539 case POINTER_TYPE:
1540 case REFERENCE_TYPE:
1541 case ARRAY_TYPE:
1542 adl_type (TREE_TYPE (type));
1543 return;
1545 case ENUMERAL_TYPE:
1546 adl_enum (type);
1547 return;
1549 case LANG_TYPE:
1550 gcc_assert (type == unknown_type_node
1551 || type == init_list_type_node);
1552 return;
1554 case TYPE_PACK_EXPANSION:
1555 adl_type (PACK_EXPANSION_PATTERN (type));
1556 return;
1558 default:
1559 break;
1563 /* Adds everything associated with a template argument to the lookup
1564 structure. */
1566 void
1567 name_lookup::adl_template_arg (tree arg)
1569 /* [basic.lookup.koenig]
1571 If T is a template-id, its associated namespaces and classes are
1572 ... the namespaces and classes associated with the types of the
1573 template arguments provided for template type parameters
1574 (excluding template template parameters); the namespaces in which
1575 any template template arguments are defined; and the classes in
1576 which any member templates used as template template arguments
1577 are defined. [Note: non-type template arguments do not
1578 contribute to the set of associated namespaces. ] */
1580 /* Consider first template template arguments. */
1581 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1582 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1584 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1586 tree ctx = CP_DECL_CONTEXT (arg);
1588 /* It's not a member template. */
1589 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1590 adl_namespace (ctx);
1591 /* Otherwise, it must be member template. */
1592 else
1593 adl_class_only (ctx);
1595 /* It's an argument pack; handle it recursively. */
1596 else if (ARGUMENT_PACK_P (arg))
1598 tree args = ARGUMENT_PACK_ARGS (arg);
1599 int i, len = TREE_VEC_LENGTH (args);
1600 for (i = 0; i < len; ++i)
1601 adl_template_arg (TREE_VEC_ELT (args, i));
1603 /* It's not a template template argument, but it is a type template
1604 argument. */
1605 else if (TYPE_P (arg))
1606 adl_type (arg);
1609 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1610 the call arguments. */
1612 tree
1613 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1615 gcc_checking_assert (!vec_safe_length (scopes));
1617 /* Gather each associated entity onto the lookup's scope list. */
1618 unsigned ix;
1619 tree arg;
1621 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1622 /* OMP reduction operators put an ADL-significant type as the
1623 first arg. */
1624 if (TYPE_P (arg))
1625 adl_type (arg);
1626 else
1627 adl_expr (arg);
1629 if (vec_safe_length (scopes))
1631 /* Now do the lookups. */
1632 value = fns;
1633 if (fns)
1634 dedup (true);
1636 /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1637 bitmap inst_path = NULL;
1638 /* VISIBLE is the regular import bitmap. */
1639 bitmap visible = visible_instantiation_path (&inst_path);
1641 for (unsigned ix = scopes->length (); ix--;)
1643 tree scope = (*scopes)[ix];
1644 if (TREE_CODE (scope) == NAMESPACE_DECL)
1645 adl_namespace_fns (scope, visible);
1646 else
1648 if (RECORD_OR_UNION_TYPE_P (scope))
1649 adl_class_fns (scope);
1651 /* During 2nd phase ADL: Any exported declaration D in N
1652 declared within the purview of a named module M
1653 (10.2) is visible if there is an associated entity
1654 attached to M with the same innermost enclosing
1655 non-inline namespace as D.
1656 [basic.lookup.argdep]/4.4 */
1658 if (!inst_path)
1659 /* Not 2nd phase. */
1660 continue;
1662 tree ctx = CP_DECL_CONTEXT (TYPE_NAME (scope));
1663 if (TREE_CODE (ctx) != NAMESPACE_DECL)
1664 /* Not namespace-scope class. */
1665 continue;
1667 tree origin = get_originating_module_decl (TYPE_NAME (scope));
1668 tree not_tmpl = STRIP_TEMPLATE (origin);
1669 if (!DECL_LANG_SPECIFIC (not_tmpl)
1670 || !DECL_MODULE_IMPORT_P (not_tmpl))
1671 /* Not imported. */
1672 continue;
1674 unsigned module = get_importing_module (origin);
1676 if (!bitmap_bit_p (inst_path, module))
1677 /* Not on path of instantiation. */
1678 continue;
1680 if (bitmap_bit_p (visible, module))
1681 /* If the module was in the visible set, we'll look at
1682 its namespace partition anyway. */
1683 continue;
1685 if (tree *slot = find_namespace_slot (ctx, name, false))
1686 if (binding_slot *mslot = search_imported_binding_slot (slot, module))
1688 if (mslot->is_lazy ())
1689 lazy_load_binding (module, ctx, name, mslot);
1691 if (tree bind = *mslot)
1693 /* We must turn on deduping, because some other class
1694 from this module might also be in this namespace. */
1695 dedup (true);
1697 /* Add the exported fns */
1698 if (STAT_HACK_P (bind))
1699 add_fns (STAT_VISIBLE (bind));
1705 fns = value;
1706 dedup (false);
1709 return fns;
1712 static bool qualified_namespace_lookup (tree, name_lookup *);
1713 static void consider_binding_level (tree name,
1714 best_match <tree, const char *> &bm,
1715 cp_binding_level *lvl,
1716 bool look_within_fields,
1717 enum lookup_name_fuzzy_kind kind);
1719 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1720 don't add duplicates to it. ARGS is the vector of call
1721 arguments (which will not be empty). */
1723 tree
1724 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1726 auto_cond_timevar tv (TV_NAME_LOOKUP);
1727 name_lookup lookup (name);
1728 return lookup.search_adl (fns, args);
1731 /* FNS is an overload set of conversion functions. Return the
1732 overloads converting to TYPE. */
1734 static tree
1735 extract_conversion_operator (tree fns, tree type)
1737 tree convs = NULL_TREE;
1738 tree tpls = NULL_TREE;
1740 for (ovl_iterator iter (fns); iter; ++iter)
1742 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1743 convs = lookup_add (*iter, convs);
1745 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1746 tpls = lookup_add (*iter, tpls);
1749 if (!convs)
1750 convs = tpls;
1752 return convs;
1755 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1757 static tree
1758 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1760 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1762 unsigned mid = (lo + hi) / 2;
1763 tree binding = (*member_vec)[mid];
1764 tree binding_name = OVL_NAME (binding);
1766 if (binding_name > name)
1767 hi = mid;
1768 else if (binding_name < name)
1769 lo = mid + 1;
1770 else
1771 return binding;
1774 return NULL_TREE;
1777 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1779 static tree
1780 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1782 for (int ix = member_vec->length (); ix--;)
1783 if (tree binding = (*member_vec)[ix])
1784 if (OVL_NAME (binding) == name)
1785 return binding;
1787 return NULL_TREE;
1790 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1792 static tree
1793 fields_linear_search (tree klass, tree name, bool want_type)
1795 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1797 tree decl = fields;
1799 if (TREE_CODE (decl) == FIELD_DECL
1800 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1802 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1803 return temp;
1806 if (DECL_NAME (decl) != name)
1807 continue;
1809 if (TREE_CODE (decl) == USING_DECL)
1811 decl = strip_using_decl (decl);
1812 if (is_overloaded_fn (decl))
1813 continue;
1816 if (DECL_DECLARES_FUNCTION_P (decl))
1817 /* Functions are found separately. */
1818 continue;
1820 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1821 return decl;
1824 return NULL_TREE;
1827 /* Like fields_linear_search, but specific for "_" name. There can be multiple
1828 name-independent non-static data members and in that case a TREE_LIST with the
1829 ambiguous decls should be returned. */
1831 static tree
1832 name_independent_linear_search (tree val, tree klass, tree name)
1834 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1836 tree decl = fields;
1838 if (TREE_CODE (decl) == FIELD_DECL
1839 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1841 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, false))
1843 decl = temp;
1844 goto add;
1848 if (DECL_NAME (decl) != name)
1849 continue;
1851 if (TREE_CODE (decl) == USING_DECL)
1853 decl = strip_using_decl (decl);
1854 if (is_overloaded_fn (decl))
1855 continue;
1858 if (DECL_DECLARES_FUNCTION_P (decl))
1859 /* Functions are found separately. */
1860 continue;
1862 add:
1863 if (val == NULL_TREE)
1864 val = decl;
1865 else
1867 if (TREE_CODE (val) != TREE_LIST)
1869 if (TREE_CODE (val) == OVERLOAD
1870 && OVL_DEDUP_P (val)
1871 && TREE_CODE (decl) == USING_DECL)
1873 val = ovl_make (decl, val);
1874 continue;
1876 val = tree_cons (NULL_TREE, val, NULL_TREE);
1877 TREE_TYPE (val) = error_mark_node;
1879 if (TREE_CODE (decl) == TREE_LIST)
1880 val = chainon (decl, val);
1881 else
1883 val = tree_cons (NULL_TREE, decl, val);
1884 TREE_TYPE (val) = error_mark_node;
1889 return val;
1892 /* Look for NAME member inside of anonymous aggregate ANON. Although
1893 such things should only contain FIELD_DECLs, we check that too
1894 late, and would give very confusing errors if we weren't
1895 permissive here. */
1897 tree
1898 search_anon_aggr (tree anon, tree name, bool want_type)
1900 gcc_assert (COMPLETE_TYPE_P (anon));
1901 tree ret = get_class_binding_direct (anon, name, want_type);
1902 return ret;
1905 /* Look for NAME as an immediate member of KLASS (including
1906 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1907 regular search. >0 to get a type binding (if there is one) and <0
1908 if you want (just) the member function binding.
1910 Use this if you do not want lazy member creation. */
1912 tree
1913 get_class_binding_direct (tree klass, tree name, bool want_type)
1915 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1917 /* Conversion operators can only be found by the marker conversion
1918 operator name. */
1919 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1920 tree lookup = conv_op ? conv_op_identifier : name;
1921 tree val = NULL_TREE;
1922 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1924 if (COMPLETE_TYPE_P (klass) && member_vec)
1926 val = member_vec_binary_search (member_vec, lookup);
1927 if (!val)
1929 else if (TREE_CODE (val) == OVERLOAD
1930 && OVL_NAME_INDEPENDENT_DECL_P (val))
1932 if (want_type)
1934 while (TREE_CODE (val) == OVERLOAD
1935 && OVL_NAME_INDEPENDENT_DECL_P (val))
1936 val = OVL_CHAIN (val);
1937 if (STAT_HACK_P (val))
1938 val = STAT_TYPE (val);
1939 else if (!DECL_DECLARES_TYPE_P (val))
1940 val = NULL_TREE;
1942 else
1944 /* OVERLOAD with a special OVL_NAME_INDEPENDENT_DECL_P
1945 flag is used under the hood to represent lookup
1946 results which include name-independent declarations,
1947 and get_class_binding_direct is turning that into
1948 TREE_LIST representation (which the callers expect for
1949 ambiguous lookups) instead.
1950 There are 2 reasons for that:
1951 1) in order to keep the member_vec binary search fast, I
1952 think it is better to keep OVL_NAME usable on all elements
1953 because having to special case TREE_LIST would slow
1954 everything down;
1955 2) the callers need to be able to chain the results anyway
1956 and so need an unshared TREE_LIST they can tweak/destroy. */
1957 tree ovl = val;
1958 val = NULL_TREE;
1959 while (TREE_CODE (ovl) == OVERLOAD
1960 && OVL_NAME_INDEPENDENT_DECL_P (ovl))
1962 val = tree_cons (NULL_TREE, OVL_FUNCTION (ovl), val);
1963 TREE_TYPE (val) = error_mark_node;
1964 ovl = OVL_CHAIN (ovl);
1966 if (STAT_HACK_P (ovl))
1967 val = tree_cons (NULL_TREE, STAT_DECL (ovl), val);
1968 else
1969 val = tree_cons (NULL_TREE, ovl, val);
1970 TREE_TYPE (val) = error_mark_node;
1973 else if (STAT_HACK_P (val))
1974 val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
1975 else if (want_type && !DECL_DECLARES_TYPE_P (val))
1976 val = NULL_TREE;
1978 else
1980 if (member_vec && !want_type)
1981 val = member_vec_linear_search (member_vec, lookup);
1983 if (id_equal (lookup, "_") && !want_type)
1984 val = name_independent_linear_search (val, klass, lookup);
1985 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
1986 /* Dependent using declarations are a 'field', make sure we
1987 return that even if we saw an overload already. */
1988 if (tree field_val = fields_linear_search (klass, lookup, want_type))
1990 if (!val)
1991 val = field_val;
1992 else if (TREE_CODE (field_val) == USING_DECL)
1993 val = ovl_make (field_val, val);
1997 /* Extract the conversion operators asked for, unless the general
1998 conversion operator was requested. */
1999 if (val && conv_op)
2001 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
2002 val = OVL_CHAIN (val);
2003 if (tree type = TREE_TYPE (name))
2004 val = extract_conversion_operator (val, type);
2007 return val;
2010 /* We're about to lookup NAME in KLASS. Make sure any lazily declared
2011 members are now declared. */
2013 static void
2014 maybe_lazily_declare (tree klass, tree name)
2016 /* See big comment anout module_state::write_pendings regarding adding a check
2017 bit. */
2018 if (modules_p ())
2019 lazy_load_pendings (TYPE_NAME (klass));
2021 /* Lazily declare functions, if we're going to search these. */
2022 if (IDENTIFIER_CTOR_P (name))
2024 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
2025 lazily_declare_fn (sfk_constructor, klass);
2026 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
2027 lazily_declare_fn (sfk_copy_constructor, klass);
2028 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
2029 lazily_declare_fn (sfk_move_constructor, klass);
2031 else if (IDENTIFIER_DTOR_P (name))
2033 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
2034 lazily_declare_fn (sfk_destructor, klass);
2036 else if (name == assign_op_identifier)
2038 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
2039 lazily_declare_fn (sfk_copy_assignment, klass);
2040 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
2041 lazily_declare_fn (sfk_move_assignment, klass);
2045 /* Look for NAME's binding in exactly KLASS. See
2046 get_class_binding_direct for argument description. Does lazy
2047 special function creation as necessary. */
2049 tree
2050 get_class_binding (tree klass, tree name, bool want_type /*=false*/)
2052 klass = complete_type (klass);
2054 if (COMPLETE_TYPE_P (klass))
2055 maybe_lazily_declare (klass, name);
2057 return get_class_binding_direct (klass, name, want_type);
2060 /* Find the slot containing overloads called 'NAME'. If there is no
2061 such slot and the class is complete, create an empty one, at the
2062 correct point in the sorted member vector. Otherwise return NULL.
2063 Deals with conv_op marker handling. */
2065 tree *
2066 find_member_slot (tree klass, tree name)
2068 bool complete_p = COMPLETE_TYPE_P (klass);
2070 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2071 if (!member_vec)
2073 vec_alloc (member_vec, 8);
2074 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2075 if (complete_p)
2076 /* If the class is complete but had no member_vec, we need to
2077 add the TYPE_FIELDS into it. We're also most likely to be
2078 adding ctors & dtors, so ask for 6 spare slots (the
2079 abstract cdtors and their clones). */
2080 member_vec = set_class_bindings (klass, 6);
2083 if (IDENTIFIER_CONV_OP_P (name))
2084 name = conv_op_identifier;
2086 unsigned ix, length = member_vec->length ();
2087 for (ix = 0; ix < length; ix++)
2089 tree *slot = &(*member_vec)[ix];
2090 tree fn_name = OVL_NAME (*slot);
2092 if (fn_name == name)
2094 /* If we found an existing slot, it must be a function set.
2095 Even with insertion after completion, because those only
2096 happen with artificial fns that have unspellable names.
2097 This means we do not have to deal with the stat hack
2098 either. */
2099 gcc_checking_assert (OVL_P (*slot));
2100 if (name == conv_op_identifier)
2102 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
2103 /* Skip the conv-op marker. */
2104 slot = &OVL_CHAIN (*slot);
2106 return slot;
2109 if (complete_p && fn_name > name)
2110 break;
2113 /* No slot found, add one if the class is complete. */
2114 if (complete_p)
2116 /* Do exact allocation, as we don't expect to add many. */
2117 gcc_assert (name != conv_op_identifier);
2118 vec_safe_reserve_exact (member_vec, 1);
2119 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2120 member_vec->quick_insert (ix, NULL_TREE);
2121 return &(*member_vec)[ix];
2124 return NULL;
2127 /* KLASS is an incomplete class to which we're adding a method NAME.
2128 Add a slot and deal with conv_op marker handling. */
2130 tree *
2131 add_member_slot (tree klass, tree name)
2133 gcc_assert (!COMPLETE_TYPE_P (klass));
2135 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2136 vec_safe_push (member_vec, NULL_TREE);
2137 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2139 tree *slot = &member_vec->last ();
2140 if (IDENTIFIER_CONV_OP_P (name))
2142 /* Install the marker prefix. */
2143 *slot = ovl_make (conv_op_marker, NULL_TREE);
2144 slot = &OVL_CHAIN (*slot);
2147 return slot;
2150 /* Comparison function to compare two MEMBER_VEC entries by name.
2151 Because we can have duplicates during insertion of TYPE_FIELDS, we
2152 do extra checking so deduping doesn't have to deal with so many
2153 cases. */
2155 static int
2156 member_name_cmp (const void *a_p, const void *b_p)
2158 tree a = *(const tree *)a_p;
2159 tree b = *(const tree *)b_p;
2160 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
2161 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
2163 gcc_checking_assert (name_a && name_b);
2164 if (name_a != name_b)
2165 return name_a < name_b ? -1 : +1;
2167 if (name_a == conv_op_identifier)
2169 /* Strip the conv-op markers. */
2170 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
2171 && OVL_FUNCTION (b) == conv_op_marker);
2172 a = OVL_CHAIN (a);
2173 b = OVL_CHAIN (b);
2176 if (TREE_CODE (a) == OVERLOAD)
2177 a = OVL_FUNCTION (a);
2178 if (TREE_CODE (b) == OVERLOAD)
2179 b = OVL_FUNCTION (b);
2181 if (id_equal (name_a, "_"))
2183 /* Sort name-independent members first. */
2184 if (name_independent_decl_p (a))
2186 if (name_independent_decl_p (b))
2188 if (DECL_UID (a) != DECL_UID (b))
2189 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
2190 gcc_assert (a == b);
2191 return 0;
2193 else
2194 return -1;
2196 else if (name_independent_decl_p (b))
2197 return +1;
2200 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
2201 if (TREE_CODE (a) != TREE_CODE (b))
2203 /* If one of them is a TYPE_DECL, it loses. */
2204 if (TREE_CODE (a) == TYPE_DECL)
2205 return +1;
2206 else if (TREE_CODE (b) == TYPE_DECL)
2207 return -1;
2209 /* If one of them is a USING_DECL, it loses. */
2210 if (TREE_CODE (a) == USING_DECL)
2211 return +1;
2212 else if (TREE_CODE (b) == USING_DECL)
2213 return -1;
2215 /* There are no other cases with different kinds of decls, as
2216 duplicate detection should have kicked in earlier. However,
2217 some erroneous cases get though. */
2218 gcc_assert (errorcount);
2221 /* Using source location would be the best thing here, but we can
2222 get identically-located decls in the following circumstances:
2224 1) duplicate artificial type-decls for the same type.
2226 2) pack expansions of using-decls.
2228 We should not be doing #1, but in either case it doesn't matter
2229 how we order these. Use UID as a proxy for source ordering, so
2230 that identically-located decls still have a well-defined stable
2231 ordering. */
2232 if (DECL_UID (a) != DECL_UID (b))
2233 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
2234 gcc_assert (a == b);
2235 return 0;
2238 static struct {
2239 gt_pointer_operator new_value;
2240 void *cookie;
2241 } resort_data;
2243 /* This routine compares two fields like member_name_cmp but using the
2244 pointer operator in resort_field_decl_data. We don't have to deal
2245 with duplicates here. */
2247 static int
2248 resort_member_name_cmp (const void *a_p, const void *b_p)
2250 tree a = *(const tree *)a_p;
2251 tree b = *(const tree *)b_p;
2252 tree name_a = OVL_NAME (a);
2253 tree name_b = OVL_NAME (b);
2255 resort_data.new_value (&name_a, &name_a, resort_data.cookie);
2256 resort_data.new_value (&name_b, &name_b, resort_data.cookie);
2258 gcc_checking_assert (name_a != name_b);
2260 return name_a < name_b ? -1 : +1;
2263 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2265 void
2266 resort_type_member_vec (void *obj, void */*orig_obj*/,
2267 gt_pointer_operator new_value, void* cookie)
2269 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
2271 resort_data.new_value = new_value;
2272 resort_data.cookie = cookie;
2273 member_vec->qsort (resort_member_name_cmp);
2277 /* Recursively count the number of fields in KLASS, including anonymous
2278 union members. */
2280 static unsigned
2281 count_class_fields (tree klass)
2283 unsigned n_fields = 0;
2285 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2286 if (DECL_DECLARES_FUNCTION_P (fields))
2287 /* Functions are dealt with separately. */;
2288 else if (TREE_CODE (fields) == FIELD_DECL
2289 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2290 n_fields += count_class_fields (TREE_TYPE (fields));
2291 else if (DECL_NAME (fields))
2292 n_fields += 1;
2294 return n_fields;
2297 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
2298 Recurse for anonymous members. MEMBER_VEC must have space. */
2300 static void
2301 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
2303 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2304 if (DECL_DECLARES_FUNCTION_P (fields))
2305 /* Functions are handled separately. */;
2306 else if (TREE_CODE (fields) == FIELD_DECL
2307 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2308 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
2309 else if (DECL_NAME (fields))
2311 tree field = fields;
2312 /* Mark a conv-op USING_DECL with the conv-op-marker. */
2313 if (TREE_CODE (field) == USING_DECL
2314 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
2315 field = ovl_make (conv_op_marker, field);
2316 member_vec->quick_push (field);
2320 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
2321 MEMBER_VEC must have space. */
2323 static void
2324 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
2326 for (tree values = TYPE_VALUES (enumtype);
2327 values; values = TREE_CHAIN (values))
2328 member_vec->quick_push (TREE_VALUE (values));
2331 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
2332 DeDup adjacent DECLS of the same name. We already dealt with
2333 conflict resolution when adding the fields or methods themselves.
2334 There are four cases (which could all be combined):
2335 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
2336 2) a USING_DECL and an overload. If the USING_DECL is dependent,
2337 it wins. Otherwise the OVERLOAD does.
2338 3) two USING_DECLS.
2339 4) name-independent members plus others. ...
2341 member_name_cmp will have ordered duplicates as
2342 <name_independent><fns><using><type> */
2344 static void
2345 member_vec_dedup (vec<tree, va_gc> *member_vec)
2347 unsigned len = member_vec->length ();
2348 unsigned store = 0;
2350 if (!len)
2351 return;
2353 tree name = OVL_NAME ((*member_vec)[0]);
2354 for (unsigned jx, ix = 0; ix < len; ix = jx)
2356 tree current = NULL_TREE;
2357 tree to_type = NULL_TREE;
2358 tree to_using = NULL_TREE;
2359 tree marker = NULL_TREE;
2360 unsigned name_independent = ix;
2362 for (jx = ix; jx < len; jx++)
2364 tree next = (*member_vec)[jx];
2365 if (jx != ix)
2367 tree next_name = OVL_NAME (next);
2368 if (next_name != name)
2370 name = next_name;
2371 break;
2375 if (IDENTIFIER_CONV_OP_P (name))
2377 marker = next;
2378 next = OVL_CHAIN (next);
2381 if (TREE_CODE (next) == USING_DECL)
2383 if (IDENTIFIER_CTOR_P (name))
2384 /* Dependent inherited ctor. */
2385 continue;
2387 next = strip_using_decl (next);
2388 if (TREE_CODE (next) == USING_DECL)
2390 to_using = next;
2391 continue;
2394 if (is_overloaded_fn (next))
2395 continue;
2398 if (DECL_DECLARES_TYPE_P (next))
2400 to_type = next;
2401 continue;
2404 if (name_independent_decl_p (next))
2405 name_independent = jx + 1;
2406 else if (!current)
2407 current = next;
2410 if (to_using)
2412 if (!current)
2413 current = to_using;
2414 else
2415 current = ovl_make (to_using, current);
2418 if (to_type)
2420 if (!current)
2421 current = to_type;
2422 else
2423 current = stat_hack (current, to_type);
2426 for (unsigned kx = name_independent; kx > ix; --kx)
2427 if (!current)
2428 current = (*member_vec)[kx - 1];
2429 else if (current == to_type)
2430 current = stat_hack ((*member_vec)[kx - 1], to_type);
2431 else
2433 current = ovl_make ((*member_vec)[kx - 1], current);
2434 OVL_NAME_INDEPENDENT_DECL_P (current) = 1;
2437 if (current)
2439 if (marker)
2441 OVL_CHAIN (marker) = current;
2442 current = marker;
2444 (*member_vec)[store++] = current;
2448 while (store++ < len)
2449 member_vec->pop ();
2452 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
2453 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
2454 know there must be at least 1 field -- the self-reference
2455 TYPE_DECL, except for anon aggregates, which will have at least
2456 one field anyway. If EXTRA < 0, always create the vector. */
2458 vec<tree, va_gc> *
2459 set_class_bindings (tree klass, int extra)
2461 unsigned n_fields = count_class_fields (klass);
2462 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2464 if (member_vec || n_fields >= 8 || extra < 0)
2466 /* Append the new fields. */
2467 vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
2468 member_vec_append_class_fields (member_vec, klass);
2471 if (member_vec)
2473 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2474 member_vec->qsort (member_name_cmp);
2475 member_vec_dedup (member_vec);
2478 return member_vec;
2481 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
2483 void
2484 insert_late_enum_def_bindings (tree klass, tree enumtype)
2486 int n_fields;
2487 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2489 /* The enum bindings will already be on the TYPE_FIELDS, so don't
2490 count them twice. */
2491 if (!member_vec)
2492 n_fields = count_class_fields (klass);
2493 else
2494 n_fields = list_length (TYPE_VALUES (enumtype));
2496 if (member_vec || n_fields >= 8)
2498 vec_safe_reserve_exact (member_vec, n_fields);
2499 if (CLASSTYPE_MEMBER_VEC (klass))
2500 member_vec_append_enum_values (member_vec, enumtype);
2501 else
2502 member_vec_append_class_fields (member_vec, klass);
2503 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2504 member_vec->qsort (member_name_cmp);
2505 member_vec_dedup (member_vec);
2509 /* The binding oracle; see cp-tree.h. */
2511 cp_binding_oracle_function *cp_binding_oracle;
2513 /* If we have a binding oracle, ask it for all namespace-scoped
2514 definitions of NAME. */
2516 static inline void
2517 query_oracle (tree name)
2519 if (!cp_binding_oracle)
2520 return;
2522 /* LOOKED_UP holds the set of identifiers that we have already
2523 looked up with the oracle. */
2524 static hash_set<tree> looked_up;
2525 if (looked_up.add (name))
2526 return;
2528 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
2531 #ifndef ENABLE_SCOPE_CHECKING
2532 # define ENABLE_SCOPE_CHECKING 0
2533 #else
2534 # define ENABLE_SCOPE_CHECKING 1
2535 #endif
2537 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
2539 static GTY((deletable)) cxx_binding *free_bindings;
2541 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
2542 field to NULL. */
2544 static inline void
2545 cxx_binding_init (cxx_binding *binding, tree value, tree type)
2547 binding->value = value;
2548 binding->type = type;
2549 binding->previous = NULL;
2552 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2554 static cxx_binding *
2555 cxx_binding_make (tree value, tree type)
2557 cxx_binding *binding = free_bindings;
2559 if (binding)
2560 free_bindings = binding->previous;
2561 else
2562 binding = ggc_alloc<cxx_binding> ();
2564 /* Clear flags by default. */
2565 LOCAL_BINDING_P (binding) = false;
2566 INHERITED_VALUE_BINDING_P (binding) = false;
2567 HIDDEN_TYPE_BINDING_P (binding) = false;
2569 cxx_binding_init (binding, value, type);
2571 return binding;
2574 /* Put BINDING back on the free list. */
2576 static inline void
2577 cxx_binding_free (cxx_binding *binding)
2579 binding->scope = NULL;
2580 binding->previous = free_bindings;
2581 free_bindings = binding;
2584 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2585 bindings) in the class scope indicated by SCOPE. */
2587 static cxx_binding *
2588 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2590 cp_class_binding cb = {cxx_binding_make (value, type), name};
2591 cxx_binding *binding = cb.base;
2592 vec_safe_push (scope->class_shadowed, cb);
2593 binding->scope = scope;
2594 return binding;
2597 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2598 level at which this declaration is being bound. */
2600 void
2601 push_binding (tree id, tree decl, cp_binding_level* level)
2603 cxx_binding *binding;
2605 if (level != class_binding_level)
2607 binding = cxx_binding_make (decl, NULL_TREE);
2608 binding->scope = level;
2610 else
2611 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2613 /* Now, fill in the binding information. */
2614 binding->previous = IDENTIFIER_BINDING (id);
2615 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2617 /* And put it on the front of the list of bindings for ID. */
2618 IDENTIFIER_BINDING (id) = binding;
2621 /* Remove the binding for DECL which should be the innermost binding
2622 for ID. */
2624 void
2625 pop_local_binding (tree id, tree decl)
2627 if (!id || IDENTIFIER_ANON_P (id))
2628 /* It's easiest to write the loops that call this function without
2629 checking whether or not the entities involved have names. We
2630 get here for such an entity. */
2631 return;
2633 /* Get the innermost binding for ID. */
2634 cxx_binding *binding = IDENTIFIER_BINDING (id);
2636 /* The name should be bound. */
2637 gcc_assert (binding != NULL);
2639 /* The DECL will be either the ordinary binding or the type binding
2640 for this identifier. Remove that binding. We don't have to
2641 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2642 away. */
2643 if (binding->value == decl)
2644 binding->value = NULL_TREE;
2645 else if (binding->type == decl)
2646 binding->type = NULL_TREE;
2647 else
2649 /* Name-independent variable was found after at least one declaration
2650 with the same name. */
2651 gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
2652 if (TREE_VALUE (binding->value) != decl)
2654 binding->value = nreverse (binding->value);
2655 /* Skip over TREE_LISTs added in pushdecl for check_local_shadow
2656 detected declarations, formerly at the tail, now at the start
2657 of the list. */
2658 while (TREE_PURPOSE (binding->value) == error_mark_node)
2659 binding->value = TREE_CHAIN (binding->value);
2661 gcc_assert (TREE_VALUE (binding->value) == decl);
2662 binding->value = TREE_CHAIN (binding->value);
2663 while (binding->value
2664 && TREE_PURPOSE (binding->value) == error_mark_node)
2665 binding->value = TREE_CHAIN (binding->value);
2668 if (!binding->value && !binding->type)
2670 /* We're completely done with the innermost binding for this
2671 identifier. Unhook it from the list of bindings. */
2672 IDENTIFIER_BINDING (id) = binding->previous;
2674 /* Add it to the free list. */
2675 cxx_binding_free (binding);
2679 /* Remove the bindings for the decls of the current level and leave
2680 the current scope. */
2682 void
2683 pop_bindings_and_leave_scope (void)
2685 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2687 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2688 tree name = OVL_NAME (decl);
2690 pop_local_binding (name, decl);
2693 leave_scope ();
2696 /* Strip non dependent using declarations. If DECL is dependent,
2697 surreptitiously create a typename_type and return it. */
2699 tree
2700 strip_using_decl (tree decl)
2702 if (decl == NULL_TREE)
2703 return NULL_TREE;
2705 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2706 decl = USING_DECL_DECLS (decl);
2708 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2709 && USING_DECL_TYPENAME_P (decl))
2711 /* We have found a type introduced by a using
2712 declaration at class scope that refers to a dependent
2713 type.
2715 using typename :: [opt] nested-name-specifier unqualified-id ;
2717 decl = make_typename_type (USING_DECL_SCOPE (decl),
2718 DECL_NAME (decl),
2719 typename_type, tf_error);
2720 if (decl != error_mark_node)
2721 decl = TYPE_NAME (decl);
2724 return decl;
2727 /* Return true if OVL is an overload for an anticipated builtin. */
2729 static bool
2730 anticipated_builtin_p (tree ovl)
2732 return (TREE_CODE (ovl) == OVERLOAD
2733 && OVL_HIDDEN_P (ovl)
2734 && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl)));
2737 /* BINDING records an existing declaration for a name in the current scope.
2738 But, DECL is another declaration for that same identifier in the
2739 same scope. This is the `struct stat' hack whereby a non-typedef
2740 class name or enum-name can be bound at the same level as some other
2741 kind of entity.
2742 3.3.7/1
2744 A class name (9.1) or enumeration name (7.2) can be hidden by the
2745 name of an object, function, or enumerator declared in the same scope.
2746 If a class or enumeration name and an object, function, or enumerator
2747 are declared in the same scope (in any order) with the same name, the
2748 class or enumeration name is hidden wherever the object, function, or
2749 enumerator name is visible.
2751 It's the responsibility of the caller to check that
2752 inserting this name is valid here. Returns nonzero if the new binding
2753 was successful. */
2755 static bool
2756 supplement_binding (cxx_binding *binding, tree decl)
2758 auto_cond_timevar tv (TV_NAME_LOOKUP);
2760 tree bval = binding->value;
2761 bool ok = true;
2762 if (bval
2763 && TREE_CODE (bval) == TREE_LIST
2764 && name_independent_decl_p (TREE_VALUE (bval)))
2765 bval = TREE_VALUE (bval);
2766 tree target_bval = strip_using_decl (bval);
2767 tree target_decl = strip_using_decl (decl);
2769 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2770 && target_decl != target_bval
2771 && (TREE_CODE (target_bval) != TYPE_DECL
2772 /* We allow pushing an enum multiple times in a class
2773 template in order to handle late matching of underlying
2774 type on an opaque-enum-declaration followed by an
2775 enum-specifier. */
2776 || (processing_template_decl
2777 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2778 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2779 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2780 (TREE_TYPE (target_decl)))
2781 || dependent_type_p (ENUM_UNDERLYING_TYPE
2782 (TREE_TYPE (target_bval)))))))
2783 /* The new name is the type name. */
2784 binding->type = decl;
2785 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2786 an inherited type-binding out of the way to make room
2787 for a new value binding. */
2788 !target_bval
2789 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2790 has been used in a non-class scope prior declaration.
2791 In that case, we should have already issued a
2792 diagnostic; for graceful error recovery purpose, pretend
2793 this was the intended declaration for that name. */
2794 || target_bval == error_mark_node
2795 /* If TARGET_BVAL is anticipated but has not yet been
2796 declared, pretend it is not there at all. */
2797 || anticipated_builtin_p (target_bval))
2798 binding->value = decl;
2799 else if (TREE_CODE (target_bval) == TYPE_DECL
2800 && DECL_ARTIFICIAL (target_bval)
2801 && target_decl != target_bval
2802 && (TREE_CODE (target_decl) != TYPE_DECL
2803 || same_type_p (TREE_TYPE (target_decl),
2804 TREE_TYPE (target_bval))))
2806 /* The old binding was a type name. It was placed in
2807 VALUE field because it was thought, at the point it was
2808 declared, to be the only entity with such a name. Move the
2809 type name into the type slot; it is now hidden by the new
2810 binding. */
2811 binding->type = bval;
2812 binding->value = decl;
2813 binding->value_is_inherited = false;
2815 else if (TREE_CODE (target_bval) == TYPE_DECL
2816 && TREE_CODE (target_decl) == TYPE_DECL
2817 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2818 && binding->scope->kind != sk_class
2819 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2820 /* If either type involves template parameters, we must
2821 wait until instantiation. */
2822 || uses_template_parms (TREE_TYPE (target_decl))
2823 || uses_template_parms (TREE_TYPE (target_bval))))
2824 /* We have two typedef-names, both naming the same type to have
2825 the same name. In general, this is OK because of:
2827 [dcl.typedef]
2829 In a given scope, a typedef specifier can be used to redefine
2830 the name of any type declared in that scope to refer to the
2831 type to which it already refers.
2833 However, in class scopes, this rule does not apply due to the
2834 stricter language in [class.mem] prohibiting redeclarations of
2835 members. */
2836 ok = false;
2837 /* There can be two block-scope declarations of the same variable,
2838 so long as they are `extern' declarations. However, there cannot
2839 be two declarations of the same static data member:
2841 [class.mem]
2843 A member shall not be declared twice in the
2844 member-specification. */
2845 else if (VAR_P (target_decl)
2846 && VAR_P (target_bval)
2847 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2848 && !DECL_CLASS_SCOPE_P (target_decl))
2850 duplicate_decls (decl, binding->value);
2851 ok = false;
2853 else if (TREE_CODE (decl) == NAMESPACE_DECL
2854 && TREE_CODE (bval) == NAMESPACE_DECL
2855 && DECL_NAMESPACE_ALIAS (decl)
2856 && DECL_NAMESPACE_ALIAS (bval)
2857 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2858 /* [namespace.alias]
2860 In a declarative region, a namespace-alias-definition can be
2861 used to redefine a namespace-alias declared in that declarative
2862 region to refer only to the namespace to which it already
2863 refers. */
2864 ok = false;
2865 else if (TREE_CODE (bval) == USING_DECL
2866 && CONST_DECL_USING_P (decl))
2867 /* Let the clone hide the using-decl that introduced it. */
2868 binding->value = decl;
2869 else if (name_independent_decl_p (decl))
2871 if (cxx_dialect < cxx26)
2872 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
2873 "name-independent declarations only available with "
2874 "%<-std=c++2c%> or %<-std=gnu++2c%>");
2875 binding->value = name_lookup::ambiguous (decl, binding->value);
2877 else if (binding->scope->kind != sk_class
2878 && TREE_CODE (decl) == USING_DECL
2879 && decls_match (target_bval, target_decl))
2880 /* Since P1787 (DR 36) it is OK to redeclare entities via using-decl,
2881 except in class scopes. */
2882 ok = false;
2883 else
2885 if (!error_operand_p (bval))
2886 diagnose_name_conflict (decl, bval);
2887 ok = false;
2890 return ok;
2893 /* Diagnose a name conflict between DECL and BVAL.
2895 This is non-static so maybe_push_used_methods can use it and avoid changing
2896 the diagnostic for inherit/using4.C; otherwise it should not be used from
2897 outside this file. */
2899 void
2900 diagnose_name_conflict (tree decl, tree bval)
2902 auto_diagnostic_group d;
2903 if (TREE_CODE (decl) == TREE_CODE (bval)
2904 && TREE_CODE (decl) != NAMESPACE_DECL
2905 && !DECL_DECLARES_FUNCTION_P (decl)
2906 && (TREE_CODE (decl) != TYPE_DECL
2907 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2908 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2910 if (concept_definition_p (decl))
2911 error ("redeclaration of %q#D with different template parameters",
2912 decl);
2913 else
2914 error ("redeclaration of %q#D", decl);
2916 else
2917 error ("%q#D conflicts with a previous declaration", decl);
2919 inform (location_of (bval), "previous declaration %q#D", bval);
2922 /* Replace BINDING's current value on its scope's name list with
2923 NEWVAL. */
2925 static void
2926 update_local_overload (cxx_binding *binding, tree newval)
2928 tree *d;
2930 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2931 if (*d == binding->value)
2933 /* Stitch new list node in. */
2934 *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
2935 break;
2937 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2938 break;
2940 TREE_VALUE (*d) = newval;
2943 /* Compares the parameter-type-lists of ONE and TWO and
2944 returns false if they are different. If the DECLs are template
2945 functions, the return types and the template parameter lists are
2946 compared too (DR 565). */
2948 static bool
2949 matching_fn_p (tree one, tree two)
2951 if (TREE_CODE (one) != TREE_CODE (two))
2952 return false;
2954 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2955 TYPE_ARG_TYPES (TREE_TYPE (two))))
2956 return false;
2958 if (TREE_CODE (one) == TEMPLATE_DECL)
2960 /* Compare template parms. */
2961 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2962 DECL_TEMPLATE_PARMS (two)))
2963 return false;
2965 /* And return type. */
2966 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2967 TREE_TYPE (TREE_TYPE (two))))
2968 return false;
2971 if (!equivalently_constrained (one, two))
2972 return false;
2974 return true;
2977 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2978 binding value (possibly with anticipated builtins stripped).
2979 Diagnose conflicts and return updated decl. */
2981 static tree
2982 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2983 tree old, tree decl, bool hiding = false)
2985 tree old_type = NULL_TREE;
2986 bool hide_type = false;
2987 bool hide_value = false;
2988 bool name_independent_p = false;
2990 if (!slot)
2992 old_type = binding->type;
2993 hide_type = HIDDEN_TYPE_BINDING_P (binding);
2994 if (!old_type)
2995 hide_value = hide_type, hide_type = false;
2996 name_independent_p = name_independent_decl_p (decl);
2998 else if (STAT_HACK_P (*slot))
3000 old_type = STAT_TYPE (*slot);
3001 hide_type = STAT_TYPE_HIDDEN_P (*slot);
3002 hide_value = STAT_DECL_HIDDEN_P (*slot);
3005 tree to_val = decl;
3006 tree to_type = old_type;
3007 bool local_overload = false;
3009 gcc_assert (!level || level->kind == sk_namespace ? !binding
3010 : level->kind != sk_class && !slot);
3012 if (old == error_mark_node)
3013 old = NULL_TREE;
3015 tree old_bval = old;
3016 old = strip_using_decl (old);
3018 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3020 /* Pushing an artificial decl. We should not find another
3021 artificial decl here already -- lookup_elaborated_type will
3022 have already found it. */
3023 gcc_checking_assert (!to_type
3024 && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
3026 if (old)
3028 /* Put DECL into the type slot. */
3029 gcc_checking_assert (!to_type);
3030 hide_type = hiding;
3031 to_type = decl;
3032 to_val = old_bval;
3034 else
3035 hide_value = hiding;
3037 goto done;
3040 if (old && DECL_IMPLICIT_TYPEDEF_P (old))
3042 /* OLD is an implicit typedef. Move it to to_type. */
3043 gcc_checking_assert (!to_type);
3045 to_type = old_bval;
3046 hide_type = hide_value;
3047 old = NULL_TREE;
3048 hide_value = false;
3051 if (DECL_DECLARES_FUNCTION_P (decl))
3053 if (!old)
3055 else if (OVL_P (old))
3057 for (ovl_iterator iter (old); iter; ++iter)
3059 tree fn = *iter;
3061 if (iter.using_p () && matching_fn_p (fn, decl))
3063 gcc_checking_assert (!iter.hidden_p ());
3064 /* If a function declaration in namespace scope or
3065 block scope has the same name and the same
3066 parameter-type- list (8.3.5) as a function
3067 introduced by a using-declaration, and the
3068 declarations do not declare the same function,
3069 the program is ill-formed. [namespace.udecl]/14 */
3070 if (tree match = duplicate_decls (decl, fn, hiding))
3071 return match;
3072 else
3073 /* FIXME: To preserve existing error behavior, we
3074 still push the decl. This might change. */
3075 diagnose_name_conflict (decl, fn);
3079 else
3080 goto conflict;
3082 if (to_type != old_type
3083 && warn_shadow
3084 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
3085 && !(DECL_IN_SYSTEM_HEADER (decl)
3086 && DECL_IN_SYSTEM_HEADER (to_type)))
3087 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
3088 decl, to_type);
3090 local_overload = old && level && level->kind != sk_namespace;
3091 to_val = ovl_insert (decl, old, -int (hiding));
3093 else if (old)
3095 if (name_independent_p)
3096 to_val = name_lookup::ambiguous (decl, old);
3097 else if (TREE_CODE (old) != TREE_CODE (decl))
3098 /* Different kinds of decls conflict. */
3099 goto conflict;
3100 else if (TREE_CODE (old) == TYPE_DECL)
3102 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3103 /* Two type decls to the same type. Do nothing. */
3104 return old;
3105 else
3106 goto conflict;
3108 else if (TREE_CODE (old) == NAMESPACE_DECL)
3110 /* Two maybe-aliased namespaces. If they're to the same target
3111 namespace, that's ok. */
3112 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
3113 goto conflict;
3115 /* The new one must be an alias at this point. */
3116 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
3117 return old;
3119 else if (TREE_CODE (old) == VAR_DECL)
3121 if (tree match = duplicate_decls (decl, old))
3123 gcc_checking_assert (!hide_value && !hiding);
3124 return match;
3126 else
3127 goto conflict;
3129 else
3131 conflict:
3132 diagnose_name_conflict (decl, old_bval);
3133 to_val = NULL_TREE;
3136 else if (hiding)
3137 hide_value = true;
3139 done:
3140 if (to_val)
3142 if (local_overload)
3144 gcc_checking_assert (binding->value && OVL_P (binding->value));
3145 update_local_overload (binding, to_val);
3147 else if (level
3148 && !(TREE_CODE (decl) == NAMESPACE_DECL
3149 && !DECL_NAMESPACE_ALIAS (decl)))
3150 /* Don't add namespaces here. They're done in
3151 push_namespace. */
3152 add_decl_to_level (level, decl);
3154 if (slot)
3156 if (STAT_HACK_P (*slot))
3158 STAT_TYPE (*slot) = to_type;
3159 STAT_DECL (*slot) = to_val;
3160 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3161 STAT_DECL_HIDDEN_P (*slot) = hide_value;
3163 else if (to_type || hide_value)
3165 *slot = stat_hack (to_val, to_type);
3166 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3167 STAT_DECL_HIDDEN_P (*slot) = hide_value;
3169 else
3171 gcc_checking_assert (!hide_type);
3172 *slot = to_val;
3175 else
3177 binding->type = to_type;
3178 binding->value = to_val;
3179 HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
3183 return decl;
3186 /* Table of identifiers to extern C declarations (or LISTS thereof). */
3188 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
3190 /* DECL has C linkage. If we have an existing instance, make sure the
3191 new one is compatible. Make sure it has the same exception
3192 specification [7.5, 7.6]. Add DECL to the map. */
3194 static void
3195 check_extern_c_conflict (tree decl)
3197 /* Ignore artificial or system header decls. */
3198 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
3199 return;
3201 /* This only applies to decls at namespace scope. */
3202 if (!DECL_NAMESPACE_SCOPE_P (decl))
3203 return;
3205 if (!extern_c_decls)
3206 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
3208 tree *slot = extern_c_decls
3209 ->find_slot_with_hash (DECL_NAME (decl),
3210 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
3211 if (tree old = *slot)
3213 if (TREE_CODE (old) == OVERLOAD)
3214 old = OVL_FUNCTION (old);
3216 int mismatch = 0;
3217 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
3218 ; /* If they're in the same context, we'll have already complained
3219 about a (possible) mismatch, when inserting the decl. */
3220 else if (!decls_match (decl, old))
3221 mismatch = 1;
3222 else if (TREE_CODE (decl) == FUNCTION_DECL
3223 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
3224 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3225 ce_normal))
3226 mismatch = -1;
3227 else if (DECL_ASSEMBLER_NAME_SET_P (old))
3228 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
3230 if (mismatch)
3232 auto_diagnostic_group d;
3233 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3234 "conflicting C language linkage declaration %q#D", decl);
3235 inform (DECL_SOURCE_LOCATION (old),
3236 "previous declaration %q#D", old);
3237 if (mismatch < 0)
3238 inform (DECL_SOURCE_LOCATION (decl),
3239 "due to different exception specifications");
3241 else
3243 if (old == *slot)
3244 /* The hash table expects OVERLOADS, so construct one with
3245 OLD as both the function and the chain. This allocate
3246 an excess OVERLOAD node, but it's rare to have multiple
3247 extern "C" decls of the same name. And we save
3248 complicating the hash table logic (which is used
3249 elsewhere). */
3250 *slot = ovl_make (old, old);
3252 slot = &OVL_CHAIN (*slot);
3254 /* Chain it on for c_linkage_binding's use. */
3255 *slot = tree_cons (NULL_TREE, decl, *slot);
3258 else
3259 *slot = decl;
3262 /* Returns a list of C-linkage decls with the name NAME. Used in
3263 c-family/c-pragma.cc to implement redefine_extname pragma. */
3265 tree
3266 c_linkage_bindings (tree name)
3268 if (extern_c_decls)
3269 if (tree *slot = extern_c_decls
3270 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
3272 tree result = *slot;
3273 if (TREE_CODE (result) == OVERLOAD)
3274 result = OVL_CHAIN (result);
3275 return result;
3278 return NULL_TREE;
3281 /* Subroutine of check_local_shadow. */
3283 static void
3284 inform_shadowed (tree shadowed)
3286 inform (DECL_SOURCE_LOCATION (shadowed),
3287 "shadowed declaration is here");
3290 /* DECL is being declared at a local scope. Emit suitable shadow
3291 warnings. */
3293 static tree
3294 check_local_shadow (tree decl)
3296 /* Don't complain about the parms we push and then pop
3297 while tentatively parsing a function declarator. */
3298 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
3299 return NULL_TREE;
3301 if (DECL_FUNCTION_SCOPE_P (decl))
3303 tree ctx = DECL_CONTEXT (decl);
3304 if (DECL_CLONED_FUNCTION_P (ctx)
3305 || DECL_TEMPLATE_INSTANTIATED (ctx)
3306 || (DECL_LANG_SPECIFIC (ctx)
3307 && DECL_DEFAULTED_FN (ctx))
3308 || (LAMBDA_FUNCTION_P (ctx)
3309 && LAMBDA_EXPR_REGEN_INFO (CLASSTYPE_LAMBDA_EXPR
3310 (DECL_CONTEXT (ctx)))))
3311 /* It suffices to check shadowing only when actually parsing.
3312 So punt for clones, instantiations, defaulted functions and
3313 regenerated lambdas. This optimization helps reduce lazy
3314 loading cascades with modules. */
3315 return NULL_TREE;
3318 tree old = NULL_TREE;
3319 cp_binding_level *old_scope = NULL;
3320 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
3322 old = binding->value;
3323 old_scope = binding->scope;
3326 if (old
3327 && (TREE_CODE (old) == PARM_DECL
3328 || VAR_P (old)
3329 || (TREE_CODE (old) == TYPE_DECL
3330 && (!DECL_ARTIFICIAL (old)
3331 || TREE_CODE (decl) == TYPE_DECL)))
3332 && DECL_FUNCTION_SCOPE_P (old)
3333 && (!DECL_ARTIFICIAL (decl)
3334 || is_capture_proxy (decl)
3335 || DECL_IMPLICIT_TYPEDEF_P (decl)
3336 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
3338 /* DECL shadows a local thing possibly of interest. */
3340 /* DR 2211: check that captures and parameters
3341 do not have the same name. */
3342 if (is_capture_proxy (decl))
3344 if (current_lambda_expr ()
3345 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
3346 && TREE_CODE (old) == PARM_DECL
3347 && DECL_NAME (decl) != this_identifier)
3348 error_at (DECL_SOURCE_LOCATION (old),
3349 "lambda parameter %qD "
3350 "previously declared as a capture", old);
3351 return NULL_TREE;
3353 /* Don't complain if it's from an enclosing function. */
3354 else if (DECL_CONTEXT (old) == current_function_decl
3355 && TREE_CODE (decl) != PARM_DECL
3356 && TREE_CODE (old) == PARM_DECL)
3358 /* Go to where the parms should be and see if we find
3359 them there. */
3360 cp_binding_level *b = current_binding_level->level_chain;
3362 if (in_function_try_handler && b->kind == sk_catch)
3363 b = b->level_chain;
3365 /* Skip artificially added scopes which aren't present
3366 in the C++ standard, e.g. for function-try-block or
3367 ctor/dtor cleanups. */
3368 while (b->artificial)
3369 b = b->level_chain;
3371 /* [basic.scope.param] A parameter name shall not be redeclared
3372 in the outermost block of the function definition. */
3373 if (b->kind == sk_function_parms)
3375 if (name_independent_decl_p (decl))
3376 return old;
3378 auto_diagnostic_group d;
3379 bool emit = true;
3380 if (DECL_EXTERNAL (decl))
3381 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3382 "declaration of %q#D shadows a parameter",
3383 decl);
3384 else
3385 error_at (DECL_SOURCE_LOCATION (decl),
3386 "declaration of %q#D shadows a parameter", decl);
3387 if (emit)
3388 inform (DECL_SOURCE_LOCATION (old),
3389 "%q#D previously declared here", old);
3390 return NULL_TREE;
3394 /* The local structure or class can't use parameters of
3395 the containing function anyway. */
3396 if (DECL_CONTEXT (old) != current_function_decl)
3398 for (cp_binding_level *scope = current_binding_level;
3399 scope != old_scope; scope = scope->level_chain)
3400 if (scope->kind == sk_class
3401 && !LAMBDA_TYPE_P (scope->this_entity))
3402 return NULL_TREE;
3404 /* Error if redeclaring a local declared in a
3405 init-statement or in the condition of an if or
3406 switch statement when the new declaration is in the
3407 outermost block of the controlled statement.
3408 Redeclaring a variable from a for or while condition is
3409 detected elsewhere. */
3410 else if (VAR_P (old)
3411 && old_scope == current_binding_level->level_chain
3412 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
3414 if (name_independent_decl_p (decl))
3415 return old;
3417 auto_diagnostic_group d;
3418 bool emit = true;
3419 if (DECL_EXTERNAL (decl))
3420 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3421 "redeclaration of %q#D", decl);
3422 else
3423 error_at (DECL_SOURCE_LOCATION (decl),
3424 "redeclaration of %q#D", decl);
3425 if (emit)
3426 inform (DECL_SOURCE_LOCATION (old),
3427 "%q#D previously declared here", old);
3428 return NULL_TREE;
3430 /* C++11:
3431 3.3.3/3: The name declared in an exception-declaration (...)
3432 shall not be redeclared in the outermost block of the handler.
3433 3.3.3/2: A parameter name shall not be redeclared (...) in
3434 the outermost block of any handler associated with a
3435 function-try-block. */
3436 else if (TREE_CODE (old) == VAR_DECL
3437 && old_scope == current_binding_level->level_chain
3438 && old_scope->kind == sk_catch)
3440 if (name_independent_decl_p (decl))
3441 return old;
3443 auto_diagnostic_group d;
3444 bool emit;
3445 if (DECL_EXTERNAL (decl))
3446 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3447 "redeclaration of %q#D", decl);
3448 else
3449 emit = permerror (DECL_SOURCE_LOCATION (decl),
3450 "redeclaration of %q#D", decl);
3451 if (emit)
3452 inform (DECL_SOURCE_LOCATION (old),
3453 "%q#D previously declared here", old);
3454 return NULL_TREE;
3457 /* Don't emit -Wshadow* warnings for name-independent decls. */
3458 if (name_independent_decl_p (decl) || name_independent_decl_p (old))
3459 return NULL_TREE;
3461 /* If '-Wshadow=compatible-local' is specified without other
3462 -Wshadow= flags, we will warn only when the type of the
3463 shadowing variable (DECL) can be converted to that of the
3464 shadowed parameter (OLD_LOCAL). The reason why we only check
3465 if DECL's type can be converted to OLD_LOCAL's type (but not the
3466 other way around) is because when users accidentally shadow a
3467 parameter, more than often they would use the variable
3468 thinking (mistakenly) it's still the parameter. It would be
3469 rare that users would use the variable in the place that
3470 expects the parameter but thinking it's a new decl.
3471 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3472 warns regardless of whether one of the types involved
3473 is a subclass of the other, since that is never okay. */
3475 enum opt_code warning_code;
3476 if (warn_shadow)
3477 warning_code = OPT_Wshadow;
3478 else if ((TREE_CODE (decl) == TYPE_DECL)
3479 ^ (TREE_CODE (old) == TYPE_DECL))
3480 /* If exactly one is a type, they aren't compatible. */
3481 warning_code = OPT_Wshadow_local;
3482 else if ((TREE_TYPE (old)
3483 && TREE_TYPE (decl)
3484 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3485 || TREE_CODE (decl) == TYPE_DECL
3486 || TREE_CODE (old) == TYPE_DECL
3487 || (!dependent_type_p (TREE_TYPE (decl))
3488 && !dependent_type_p (TREE_TYPE (old))
3489 /* If the new decl uses auto, we don't yet know
3490 its type (the old type cannot be using auto
3491 at this point, without also being
3492 dependent). This is an indication we're
3493 (now) doing the shadow checking too
3494 early. */
3495 && !type_uses_auto (TREE_TYPE (decl))
3496 && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
3497 decl, LOOKUP_IMPLICIT, tf_none)))
3498 warning_code = OPT_Wshadow_compatible_local;
3499 else
3500 warning_code = OPT_Wshadow_local;
3502 const char *msg;
3503 if (TREE_CODE (old) == PARM_DECL)
3504 msg = "declaration of %q#D shadows a parameter";
3505 else if (is_capture_proxy (old))
3506 msg = "declaration of %qD shadows a lambda capture";
3507 else
3508 msg = "declaration of %qD shadows a previous local";
3510 auto_diagnostic_group d;
3511 if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
3512 inform_shadowed (old);
3513 return NULL_TREE;
3516 if (!warn_shadow)
3517 return NULL_TREE;
3519 /* Don't emit -Wshadow for name-independent decls. */
3520 if (name_independent_decl_p (decl))
3521 return NULL_TREE;
3523 /* Don't warn for artificial things that are not implicit typedefs. */
3524 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
3525 return NULL_TREE;
3527 if (nonlambda_method_basetype ())
3528 if (tree member = lookup_member (current_nonlambda_class_type (),
3529 DECL_NAME (decl), /*protect=*/0,
3530 /*want_type=*/false, tf_warning_or_error))
3532 member = MAYBE_BASELINK_FUNCTIONS (member);
3534 /* Warn if a variable shadows a non-function, or the variable
3535 is a function or a pointer-to-function. */
3536 if ((!OVL_P (member)
3537 || TREE_CODE (decl) == FUNCTION_DECL
3538 || (TREE_TYPE (decl)
3539 && (TYPE_PTRFN_P (TREE_TYPE (decl))
3540 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
3541 && !warning_suppressed_p (decl, OPT_Wshadow))
3543 auto_diagnostic_group d;
3544 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3545 "declaration of %qD shadows a member of %qT",
3546 decl, current_nonlambda_class_type ())
3547 && DECL_P (member))
3549 inform_shadowed (member);
3550 suppress_warning (decl, OPT_Wshadow);
3553 return NULL_TREE;
3556 /* Now look for a namespace shadow. */
3557 old = find_namespace_value (current_namespace, DECL_NAME (decl));
3558 if (old
3559 && (VAR_P (old)
3560 || (TREE_CODE (old) == TYPE_DECL
3561 && (!DECL_ARTIFICIAL (old)
3562 || TREE_CODE (decl) == TYPE_DECL)))
3563 && !DECL_EXTERNAL (decl)
3564 && !instantiating_current_function_p ()
3565 && !warning_suppressed_p (decl, OPT_Wshadow))
3566 /* XXX shadow warnings in outer-more namespaces */
3568 auto_diagnostic_group d;
3569 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3570 "declaration of %qD shadows a global declaration",
3571 decl))
3573 inform_shadowed (old);
3574 suppress_warning (decl, OPT_Wshadow);
3576 return NULL_TREE;
3579 return NULL_TREE;
3582 /* DECL is being pushed inside function CTX. Set its context, if
3583 needed. */
3585 static void
3586 set_decl_context_in_fn (tree ctx, tree decl)
3588 if (TREE_CODE (decl) == FUNCTION_DECL
3589 || (VAR_P (decl) && DECL_EXTERNAL (decl)))
3590 /* Make sure local externs are marked as such. OMP UDRs really
3591 are nested functions. */
3592 gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
3593 && (DECL_NAMESPACE_SCOPE_P (decl)
3594 || (TREE_CODE (decl) == FUNCTION_DECL
3595 && DECL_OMP_DECLARE_REDUCTION_P (decl))));
3597 if (!DECL_CONTEXT (decl)
3598 /* When parsing the parameter list of a function declarator,
3599 don't set DECL_CONTEXT to an enclosing function. */
3600 && !(TREE_CODE (decl) == PARM_DECL
3601 && parsing_function_declarator ()))
3602 DECL_CONTEXT (decl) = ctx;
3605 /* DECL is a local extern decl. Find or create the namespace-scope
3606 decl that it aliases. Also, determines the linkage of DECL. */
3608 void
3609 push_local_extern_decl_alias (tree decl)
3611 if (dependent_type_p (TREE_TYPE (decl))
3612 || (processing_template_decl
3613 && VAR_P (decl)
3614 && CP_DECL_THREAD_LOCAL_P (decl)))
3615 return;
3616 /* EH specs were not part of the function type prior to c++17, but
3617 we still can't go pushing dependent eh specs into the namespace. */
3618 if (cxx_dialect < cxx17
3619 && TREE_CODE (decl) == FUNCTION_DECL
3620 && (value_dependent_expression_p
3621 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
3622 return;
3624 gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
3625 || !DECL_TEMPLATE_INFO (decl));
3626 if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
3627 /* We're instantiating a non-dependent local decl, it already
3628 knows the alias. */
3629 return;
3631 tree alias = NULL_TREE;
3633 if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
3634 /* Do not let a VLA creep into a namespace. Diagnostic will be
3635 emitted in layout_var_decl later. */
3636 alias = error_mark_node;
3637 else
3639 /* First look for a decl that matches. */
3640 tree ns = CP_DECL_CONTEXT (decl);
3641 tree binding = find_namespace_value (ns, DECL_NAME (decl));
3643 if (binding && TREE_CODE (binding) != TREE_LIST)
3644 for (ovl_iterator iter (binding); iter; ++iter)
3645 if (decls_match (decl, *iter, /*record_versions*/false))
3647 alias = *iter;
3648 if (!validate_constexpr_redeclaration (alias, decl))
3649 return;
3650 break;
3653 if (!alias)
3655 /* No existing namespace-scope decl. Make one. */
3656 alias = copy_decl (decl);
3657 if (TREE_CODE (alias) == FUNCTION_DECL)
3659 /* Recontextualize the parms. */
3660 for (tree *chain = &DECL_ARGUMENTS (alias);
3661 *chain; chain = &DECL_CHAIN (*chain))
3663 *chain = copy_decl (*chain);
3664 DECL_CONTEXT (*chain) = alias;
3667 tree type = TREE_TYPE (alias);
3668 for (tree args = TYPE_ARG_TYPES (type);
3669 args; args = TREE_CHAIN (args))
3670 if (TREE_PURPOSE (args))
3672 /* There are default args. Lose them. */
3673 tree nargs = NULL_TREE;
3674 tree *chain = &nargs;
3675 for (args = TYPE_ARG_TYPES (type);
3676 args; args = TREE_CHAIN (args))
3677 if (args == void_list_node)
3679 *chain = args;
3680 break;
3682 else
3684 *chain
3685 = build_tree_list (NULL_TREE, TREE_VALUE (args));
3686 chain = &TREE_CHAIN (*chain);
3689 tree fn_type = build_function_type (TREE_TYPE (type), nargs);
3691 fn_type = apply_memfn_quals
3692 (fn_type, type_memfn_quals (type));
3694 fn_type = build_cp_fntype_variant
3695 (fn_type, type_memfn_rqual (type),
3696 TYPE_RAISES_EXCEPTIONS (type),
3697 TYPE_HAS_LATE_RETURN_TYPE (type));
3699 TREE_TYPE (alias) = fn_type;
3700 break;
3704 /* This is the real thing. */
3705 DECL_LOCAL_DECL_P (alias) = false;
3707 /* Expected default linkage is from the namespace. */
3708 TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
3709 push_nested_namespace (ns);
3710 alias = pushdecl (alias, /* hiding= */true);
3711 pop_nested_namespace (ns);
3712 if (VAR_P (decl)
3713 && CP_DECL_THREAD_LOCAL_P (decl)
3714 && alias != error_mark_node)
3715 set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
3717 /* Adjust visibility. */
3718 determine_visibility (alias);
3722 retrofit_lang_decl (decl);
3723 DECL_LOCAL_DECL_ALIAS (decl) = alias;
3726 /* If DECL has non-internal linkage, and we have a module vector,
3727 record it in the appropriate slot. We have already checked for
3728 duplicates. */
3730 static void
3731 maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
3733 if (TREE_CODE (*slot) != BINDING_VECTOR)
3734 return;
3736 if (decl_linkage (decl) == lk_internal)
3737 return;
3739 tree not_tmpl = STRIP_TEMPLATE (decl);
3740 bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
3741 && DECL_MODULE_ATTACH_P (not_tmpl));
3742 tree *gslot = get_fixed_binding_slot
3743 (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
3744 true);
3746 if (!is_attached)
3748 binding_slot &orig
3749 = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
3751 if (!STAT_HACK_P (tree (orig)))
3752 orig = stat_hack (tree (orig));
3754 MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
3757 add_mergeable_namespace_entity (gslot, decl);
3760 /* DECL is being pushed. Check whether it hides or ambiguates
3761 something seen as an import. This include decls seen in our own
3762 interface, which is OK. Also, check for merging a
3763 global/partition decl. */
3765 static tree
3766 check_module_override (tree decl, tree mvec, bool hiding,
3767 tree scope, tree name)
3769 tree match = NULL_TREE;
3770 bitmap imports = get_import_bitmap ();
3771 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
3772 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
3774 tree nontmpl = STRIP_TEMPLATE (decl);
3775 bool attached = DECL_LANG_SPECIFIC (nontmpl) && DECL_MODULE_ATTACH_P (nontmpl);
3777 /* For deduction guides we don't do normal name lookup, but rather consider
3778 any reachable declaration, so we should check for overriding here too. */
3779 bool any_reachable = deduction_guide_p (decl);
3781 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
3783 cluster++;
3784 ix--;
3787 for (; ix--; cluster++)
3788 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
3790 /* Are we importing this module? */
3791 if (cluster->indices[jx].span != 1)
3792 continue;
3793 if (!cluster->indices[jx].base)
3794 continue;
3795 if (!any_reachable
3796 && !bitmap_bit_p (imports, cluster->indices[jx].base))
3797 continue;
3798 /* Is it loaded? */
3799 if (cluster->slots[jx].is_lazy ())
3801 gcc_assert (cluster->indices[jx].span == 1);
3802 lazy_load_binding (cluster->indices[jx].base,
3803 scope, name, &cluster->slots[jx]);
3805 tree bind = cluster->slots[jx];
3806 if (!bind)
3807 /* Errors could cause there to be nothing. */
3808 continue;
3810 tree type = NULL_TREE;
3811 if (STAT_HACK_P (bind))
3813 /* If there was a matching STAT_TYPE here then xref_tag
3814 should have found it, but we need to check anyway because
3815 a conflicting using-declaration may exist. */
3816 if (any_reachable)
3818 type = STAT_TYPE (bind);
3819 bind = STAT_DECL (bind);
3821 else
3823 if (STAT_TYPE_VISIBLE_P (bind))
3824 type = STAT_TYPE (bind);
3825 bind = STAT_VISIBLE (bind);
3829 if (type)
3831 match = duplicate_decls (decl, strip_using_decl (type), hiding);
3832 if (match)
3833 goto matched;
3836 for (ovl_iterator iter (strip_using_decl (bind)); iter; ++iter)
3838 match = duplicate_decls (decl, *iter, hiding);
3839 if (match)
3840 goto matched;
3844 if (TREE_PUBLIC (scope) && TREE_PUBLIC (STRIP_TEMPLATE (decl))
3845 /* Namespaces are dealt with specially in
3846 make_namespace_finish. */
3847 && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
3849 /* Look in the appropriate mergeable decl slot. */
3850 tree mergeable = NULL_TREE;
3851 if (attached)
3852 mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
3853 / BINDING_VECTOR_SLOTS_PER_CLUSTER)
3854 .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
3855 else
3856 mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
3858 for (ovl_iterator iter (mergeable); iter; ++iter)
3860 match = duplicate_decls (decl, *iter, hiding);
3861 if (match)
3862 goto matched;
3866 return NULL_TREE;
3868 matched:
3869 if (match != error_mark_node)
3871 if (attached)
3872 BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
3873 else
3874 BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
3877 return match;
3880 /* Record DECL as belonging to the current lexical scope. Check for
3881 errors (such as an incompatible declaration for the same name
3882 already seen in the same scope).
3884 The new binding is hidden if HIDING is true (an anticipated builtin
3885 or hidden friend).
3887 Returns either DECL or an old decl for the same name. If an old
3888 decl is returned, it may have been smashed to agree with what DECL
3889 says. */
3891 tree
3892 pushdecl (tree decl, bool hiding)
3894 auto_cond_timevar tv (TV_NAME_LOOKUP);
3896 if (decl == error_mark_node)
3897 return error_mark_node;
3899 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
3900 set_decl_context_in_fn (current_function_decl, decl);
3902 /* The binding level we will be pushing into. During local class
3903 pushing, we want to push to the containing scope. */
3904 cp_binding_level *level = current_binding_level;
3905 while (level->kind == sk_class
3906 || level->kind == sk_cleanup)
3907 level = level->level_chain;
3909 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3910 insert it. Other NULL-named decls, not so much. */
3911 tree name = DECL_NAME (decl);
3912 if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
3914 cxx_binding *binding = NULL; /* Local scope binding. */
3915 tree ns = NULL_TREE; /* Searched namespace. */
3916 tree *slot = NULL; /* Binding slot in namespace. */
3917 tree *mslot = NULL; /* Current module slot in namespace. */
3918 tree old = NULL_TREE;
3919 bool name_independent_p = false;
3920 bool name_independent_diagnosed_p = false;
3922 if (level->kind == sk_namespace)
3924 /* We look in the decl's namespace for an existing
3925 declaration, even though we push into the current
3926 namespace. */
3927 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3928 ? CP_DECL_CONTEXT (decl) : current_namespace);
3929 /* Create the binding, if this is current namespace, because
3930 that's where we'll be pushing anyway. */
3931 slot = find_namespace_slot (ns, name, ns == current_namespace);
3932 if (slot)
3934 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
3935 ns == current_namespace);
3936 old = MAYBE_STAT_DECL (*mslot);
3939 else
3941 binding = find_local_binding (level, name);
3942 if (binding)
3943 old = binding->value;
3944 name_independent_p = name_independent_decl_p (decl);
3947 if (old == error_mark_node)
3948 old = NULL_TREE;
3950 tree oldi, oldn;
3951 for (oldi = old; oldi; oldi = oldn)
3953 if (TREE_CODE (oldi) == TREE_LIST)
3955 gcc_checking_assert (level->kind != sk_namespace
3956 && name_independent_decl_p
3957 (TREE_VALUE (old)));
3958 oldn = TREE_CHAIN (oldi);
3959 oldi = TREE_VALUE (oldi);
3961 else
3962 oldn = NULL_TREE;
3963 for (ovl_iterator iter (oldi); iter; ++iter)
3964 if (iter.using_p ())
3965 ; /* Ignore using decls here. */
3966 else if (iter.hidden_p ()
3967 && TREE_CODE (*iter) == FUNCTION_DECL
3968 && DECL_LANG_SPECIFIC (*iter)
3969 && DECL_MODULE_IMPORT_P (*iter))
3970 ; /* An undeclared builtin imported from elsewhere. */
3971 else if (name_independent_p)
3973 /* Ignore name-independent declarations. */
3974 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
3975 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
3976 "name-independent declarations only available with "
3977 "%<-std=c++2c%> or %<-std=gnu++2c%>");
3978 name_independent_diagnosed_p = true;
3980 else if (tree match
3981 = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
3983 if (match == error_mark_node)
3985 else if (TREE_CODE (match) == TYPE_DECL)
3986 gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
3987 == (level->kind == sk_namespace
3988 ? NULL_TREE : TREE_TYPE (match)));
3989 else if (iter.hidden_p () && !hiding)
3991 /* Unhiding a previously hidden decl. */
3992 tree head = iter.reveal_node (oldi);
3993 if (head != oldi)
3995 gcc_checking_assert (ns);
3996 if (STAT_HACK_P (*slot))
3997 STAT_DECL (*slot) = head;
3998 else
3999 *slot = head;
4001 if (DECL_EXTERN_C_P (match))
4002 /* We need to check and register the decl now. */
4003 check_extern_c_conflict (match);
4005 else if (slot
4006 && !hiding
4007 && STAT_HACK_P (*slot)
4008 && STAT_DECL_HIDDEN_P (*slot))
4010 /* Unhide the non-function. */
4011 gcc_checking_assert (oldi == match);
4012 if (!STAT_TYPE (*slot))
4013 *slot = match;
4014 else
4015 STAT_DECL (*slot) = match;
4017 return match;
4021 /* Check for redeclaring an import. */
4022 if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
4023 if (tree match
4024 = check_module_override (decl, *slot, hiding, ns, name))
4026 if (match == error_mark_node)
4027 return match;
4029 /* We found a decl in an interface, push it into this
4030 binding. */
4031 decl = update_binding (NULL, binding, mslot, old,
4032 match, hiding);
4034 return decl;
4037 /* We are pushing a new decl. */
4039 /* Skip a hidden builtin we failed to match already. There can
4040 only be one. */
4041 if (old && anticipated_builtin_p (old))
4042 old = OVL_CHAIN (old);
4044 check_template_shadow (decl);
4046 if (DECL_DECLARES_FUNCTION_P (decl))
4048 check_default_args (decl);
4050 if (hiding)
4052 if (level->kind != sk_namespace)
4054 /* In a local class, a friend function declaration must
4055 find a matching decl in the innermost non-class scope.
4056 [class.friend/11] */
4057 error_at (DECL_SOURCE_LOCATION (decl),
4058 "friend declaration %qD in local class without "
4059 "prior local declaration", decl);
4060 /* Don't attempt to push it. */
4061 return error_mark_node;
4066 if (level->kind != sk_namespace)
4068 tree local_shadow = check_local_shadow (decl);
4069 if (name_independent_p && local_shadow)
4071 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
4072 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
4073 "name-independent declarations only available with "
4074 "%<-std=c++2c%> or %<-std=gnu++2c%>");
4075 name_independent_diagnosed_p = true;
4076 /* When a name-independent declaration is pushed into a scope
4077 which itself does not contain a _ named declaration yet (so
4078 _ name lookups wouldn't be normally ambiguous), but it
4079 shadows a _ declaration in some outer scope in cases
4080 described in [basic.scope.block]/2 where if the names of
4081 the shadowed and shadowing declarations were different it
4082 would be ill-formed program, arrange for _ name lookups
4083 in this scope to be ambiguous. */
4084 if (old == NULL_TREE)
4086 old = build_tree_list (error_mark_node, local_shadow);
4087 TREE_TYPE (old) = error_mark_node;
4091 if (TREE_CODE (decl) == NAMESPACE_DECL)
4092 /* A local namespace alias. */
4093 set_identifier_type_value_with_scope (name, NULL_TREE, level);
4095 if (!binding)
4096 binding = create_local_binding (level, name);
4098 else if (!slot)
4100 ns = current_namespace;
4101 slot = find_namespace_slot (ns, name, true);
4102 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
4103 /* Update OLD to reflect the namespace we're going to be
4104 pushing into. */
4105 old = MAYBE_STAT_DECL (*mslot);
4108 old = update_binding (level, binding, mslot, old, decl, hiding);
4110 if (old != decl)
4111 /* An existing decl matched, use it. */
4112 decl = old;
4113 else
4115 if (TREE_CODE (decl) == TYPE_DECL)
4117 tree type = TREE_TYPE (decl);
4119 if (type != error_mark_node)
4121 if (TYPE_NAME (type) != decl)
4122 set_underlying_type (decl);
4124 set_identifier_type_value_with_scope (name, decl, level);
4126 if (level->kind != sk_namespace
4127 && !instantiating_current_function_p ())
4128 /* This is a locally defined typedef in a function that
4129 is not a template instantation, record it to implement
4130 -Wunused-local-typedefs. */
4131 record_locally_defined_typedef (decl);
4134 else if (VAR_OR_FUNCTION_DECL_P (decl))
4136 if (DECL_EXTERN_C_P (decl))
4137 check_extern_c_conflict (decl);
4139 if (!DECL_LOCAL_DECL_P (decl)
4140 && VAR_P (decl))
4141 maybe_register_incomplete_var (decl);
4143 if (DECL_LOCAL_DECL_P (decl)
4144 && NAMESPACE_SCOPE_P (decl))
4145 push_local_extern_decl_alias (decl);
4148 if (level->kind == sk_namespace
4149 && TREE_PUBLIC (level->this_entity)
4150 && module_maybe_has_cmi_p ())
4151 maybe_record_mergeable_decl (slot, name, decl);
4154 else
4155 add_decl_to_level (level, decl);
4157 return decl;
4160 /* A mergeable entity is being loaded into namespace NS slot NAME.
4161 Create and return the appropriate vector slot for that. Either a
4162 GMF slot or a module-specific one. */
4164 tree *
4165 mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
4167 tree *mslot = find_namespace_slot (ns, name, true);
4168 tree *vslot = get_fixed_binding_slot
4169 (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
4170 true);
4172 gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
4173 *vec = *mslot;
4175 return vslot;
4178 /* Retrieve the bindings for an existing mergeable entity in namespace
4179 NS slot NAME. Returns NULL if no such bindings exists. */
4181 static tree
4182 get_mergeable_namespace_binding (tree ns, tree name, bool is_attached)
4184 tree *mslot = find_namespace_slot (ns, name, false);
4185 if (!mslot || !*mslot || TREE_CODE (*mslot) != BINDING_VECTOR)
4186 return NULL_TREE;
4188 tree *vslot = get_fixed_binding_slot
4189 (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
4190 false);
4191 return vslot ? *vslot : NULL_TREE;
4194 /* DECL is a new mergeable namespace-scope decl. Add it to the
4195 mergeable entities on GSLOT. */
4197 void
4198 add_mergeable_namespace_entity (tree *gslot, tree decl)
4200 *gslot = ovl_make (decl, *gslot);
4203 /* A mergeable entity of KLASS called NAME is being loaded. Return
4204 the set of things it could be. All such non-as_base classes have
4205 been given a member vec. */
4207 tree
4208 lookup_class_binding (tree klass, tree name)
4210 tree found = NULL_TREE;
4212 if (!COMPLETE_TYPE_P (klass))
4214 else if (TYPE_LANG_SPECIFIC (klass))
4216 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
4218 found = member_vec_binary_search (member_vec, name);
4219 if (!found)
4221 else if (STAT_HACK_P (found))
4222 /* Rearrange the stat hack so that we don't need to expose that
4223 internal detail. */
4224 found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
4225 else if (IDENTIFIER_CONV_OP_P (name))
4227 gcc_checking_assert (name == conv_op_identifier);
4228 found = OVL_CHAIN (found);
4231 else
4233 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
4234 || TYPE_PTRMEMFUNC_P (klass));
4235 found = fields_linear_search (klass, name, false);
4238 return found;
4241 /* Whether this using is declared in the module purview. */
4243 bool
4244 ovl_iterator::purview_p () const
4246 gcc_checking_assert (using_p ());
4247 if (TREE_CODE (ovl) == USING_DECL)
4248 return DECL_MODULE_PURVIEW_P (ovl);
4249 return OVL_PURVIEW_P (ovl);
4252 /* Whether this using is exported from this module. */
4254 bool
4255 ovl_iterator::exporting_p () const
4257 gcc_checking_assert (using_p ());
4258 if (TREE_CODE (ovl) == USING_DECL)
4259 return DECL_MODULE_EXPORT_P (ovl);
4260 return OVL_EXPORT_P (ovl);
4263 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
4264 for all decls of the current module. When partitions are involved,
4265 decls might be mentioned more than once. Return the accumulation of
4266 CALLBACK results. */
4268 unsigned
4269 walk_module_binding (tree binding, bitmap partitions,
4270 bool (*callback) (tree decl, WMB_Flags, void *data),
4271 void *data)
4273 tree current = binding;
4274 unsigned count = 0;
4276 if (TREE_CODE (binding) == BINDING_VECTOR)
4277 current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
4279 bool decl_hidden = false;
4280 if (tree type = MAYBE_STAT_TYPE (current))
4282 WMB_Flags flags = WMB_None;
4283 if (STAT_TYPE_HIDDEN_P (current))
4284 flags = WMB_Flags (flags | WMB_Hidden);
4285 if (TREE_CODE (type) == USING_DECL)
4287 flags = WMB_Flags (flags | WMB_Using);
4288 if (DECL_MODULE_PURVIEW_P (type))
4289 flags = WMB_Flags (flags | WMB_Purview);
4290 if (DECL_MODULE_EXPORT_P (type))
4291 flags = WMB_Flags (flags | WMB_Export);
4293 count += callback (type, flags, data);
4294 decl_hidden = STAT_DECL_HIDDEN_P (current);
4297 for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
4299 if (iter.hidden_p ())
4300 decl_hidden = true;
4301 if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
4303 WMB_Flags flags = WMB_None;
4304 if (decl_hidden)
4305 flags = WMB_Flags (flags | WMB_Hidden);
4306 if (iter.using_p ())
4308 flags = WMB_Flags (flags | WMB_Using);
4309 if (iter.purview_p ())
4310 flags = WMB_Flags (flags | WMB_Purview);
4311 if (iter.exporting_p ())
4312 flags = WMB_Flags (flags | WMB_Export);
4314 count += callback (*iter, flags, data);
4316 decl_hidden = false;
4319 if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
4321 /* Process partition slots. */
4322 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
4323 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
4324 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
4326 ix--;
4327 cluster++;
4330 /* There could be duplicate module or GMF entries. */
4331 bool maybe_dups = (BINDING_VECTOR_PARTITION_DUPS_P (binding)
4332 || BINDING_VECTOR_GLOBAL_DUPS_P (binding));
4334 for (; ix--; cluster++)
4335 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
4336 if (!cluster->slots[jx].is_lazy ())
4337 if (tree bind = cluster->slots[jx])
4339 if (TREE_CODE (bind) == NAMESPACE_DECL
4340 && !DECL_NAMESPACE_ALIAS (bind))
4342 if (unsigned base = cluster->indices[jx].base)
4343 if (unsigned span = cluster->indices[jx].span)
4345 if (bitmap_bit_p (partitions, base))
4346 goto found;
4347 while (++base, --span);
4348 /* Not a partition's namespace. */
4349 continue;
4350 found:
4352 WMB_Flags flags = WMB_None;
4353 if (maybe_dups)
4354 flags = WMB_Flags (flags | WMB_Dups);
4355 count += callback (bind, flags, data);
4357 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
4359 if (tree btype = STAT_TYPE (bind))
4361 WMB_Flags flags = WMB_None;
4362 if (maybe_dups)
4363 flags = WMB_Flags (flags | WMB_Dups);
4364 if (STAT_TYPE_HIDDEN_P (bind))
4365 flags = WMB_Flags (flags | WMB_Hidden);
4366 if (TREE_CODE (btype) == USING_DECL)
4368 flags = WMB_Flags (flags | WMB_Using);
4369 if (DECL_MODULE_PURVIEW_P (btype))
4370 flags = WMB_Flags (flags | WMB_Purview);
4371 if (DECL_MODULE_EXPORT_P (btype))
4372 flags = WMB_Flags (flags | WMB_Export);
4374 count += callback (btype, flags, data);
4376 bool part_hidden = STAT_DECL_HIDDEN_P (bind);
4377 for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
4378 iter; ++iter)
4380 if (iter.hidden_p ())
4381 part_hidden = true;
4382 gcc_checking_assert
4383 (!(part_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
4385 WMB_Flags flags = WMB_None;
4386 if (maybe_dups)
4387 flags = WMB_Flags (flags | WMB_Dups);
4388 if (part_hidden)
4389 flags = WMB_Flags (flags | WMB_Hidden);
4390 if (iter.using_p ())
4392 flags = WMB_Flags (flags | WMB_Using);
4393 if (iter.purview_p ())
4394 flags = WMB_Flags (flags | WMB_Purview);
4395 if (iter.exporting_p ())
4396 flags = WMB_Flags (flags | WMB_Export);
4398 count += callback (*iter, flags, data);
4399 part_hidden = false;
4405 return count;
4408 /* Imported module MOD has a binding to NS::NAME, stored in section
4409 SNUM. */
4411 bool
4412 import_module_binding (tree ns, tree name, unsigned mod, unsigned snum)
4414 tree *slot = find_namespace_slot (ns, name, true);
4415 binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
4417 if (mslot->is_lazy () || *mslot)
4418 /* Oops, something was already there. */
4419 return false;
4421 mslot->set_lazy (snum);
4422 return true;
4425 /* An import of MODULE is binding NS::NAME. There should be no
4426 existing binding for >= MODULE. GLOBAL_P indicates whether the
4427 bindings include global module entities. PARTITION_P is true if
4428 it is part of the current module. VALUE and TYPE are the value
4429 and type bindings. VISIBLE are the value bindings being exported. */
4431 bool
4432 set_module_binding (tree ns, tree name, unsigned mod, bool global_p,
4433 bool partition_p, tree value, tree type, tree visible)
4435 if (!value)
4436 /* Bogus BMIs could give rise to nothing to bind. */
4437 return false;
4439 gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
4440 || DECL_NAMESPACE_ALIAS (value));
4441 gcc_checking_assert (mod);
4443 tree *slot = find_namespace_slot (ns, name, true);
4444 binding_slot *mslot = search_imported_binding_slot (slot, mod);
4446 if (!mslot || !mslot->is_lazy ())
4447 /* Again, bogus BMI could give find to missing or already loaded slot. */
4448 return false;
4450 tree bind = value;
4451 if (type || visible != bind || partition_p || global_p)
4453 bind = stat_hack (bind, type);
4454 STAT_VISIBLE (bind) = visible;
4455 if ((partition_p && TREE_PUBLIC (ns))
4456 || (type && DECL_MODULE_EXPORT_P (type)))
4457 STAT_TYPE_VISIBLE_P (bind) = true;
4460 /* Note if this is this-module and/or global binding. */
4461 if (partition_p)
4462 MODULE_BINDING_PARTITION_P (bind) = true;
4463 if (global_p)
4464 MODULE_BINDING_GLOBAL_P (bind) = true;
4466 *mslot = bind;
4468 return true;
4471 void
4472 add_module_namespace_decl (tree ns, tree decl)
4474 gcc_assert (!DECL_CHAIN (decl));
4475 gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
4476 && DECL_LOCAL_DECL_P (decl)));
4477 if (CHECKING_P)
4478 /* Expensive already-there? check. */
4479 for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
4480 probe = DECL_CHAIN (probe))
4481 gcc_assert (decl != probe);
4483 add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
4485 if (VAR_P (decl))
4486 maybe_register_incomplete_var (decl);
4488 if (VAR_OR_FUNCTION_DECL_P (decl)
4489 && DECL_EXTERN_C_P (decl))
4490 check_extern_c_conflict (decl);
4493 /* Enter DECL into the symbol table, if that's appropriate. Returns
4494 DECL, or a modified version thereof. */
4496 tree
4497 maybe_push_decl (tree decl)
4499 tree type = TREE_TYPE (decl);
4501 /* Add this decl to the current binding level, but not if it comes
4502 from another scope, e.g. a static member variable. TEM may equal
4503 DECL or it may be a previous decl of the same name. */
4504 if (decl == error_mark_node
4505 || (TREE_CODE (decl) != PARM_DECL
4506 && DECL_CONTEXT (decl) != NULL_TREE
4507 /* Definitions of namespace members outside their namespace are
4508 possible. */
4509 && !DECL_NAMESPACE_SCOPE_P (decl))
4510 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4511 || type == unknown_type_node
4512 /* The declaration of a template specialization does not affect
4513 the functions available for overload resolution, so we do not
4514 call pushdecl. */
4515 || (TREE_CODE (decl) == FUNCTION_DECL
4516 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4517 return decl;
4518 else
4519 return pushdecl (decl);
4522 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4523 binding level. If IS_USING is true, DECL got here through a
4524 using-declaration. */
4526 static void
4527 push_local_binding (tree id, tree decl, bool is_using)
4529 /* Skip over any local classes. This makes sense if we call
4530 push_local_binding with a friend decl of a local class. */
4531 cp_binding_level *b = innermost_nonclass_level ();
4533 gcc_assert (b->kind != sk_namespace);
4534 if (find_local_binding (b, id))
4536 /* Supplement the existing binding. */
4537 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
4538 /* It didn't work. Something else must be bound at this
4539 level. Do not add DECL to the list of things to pop
4540 later. */
4541 return;
4543 else
4544 /* Create a new binding. */
4545 push_binding (id, decl, b);
4547 if (TREE_CODE (decl) == OVERLOAD || is_using)
4548 /* We must put the OVERLOAD or using into a TREE_LIST since we
4549 cannot use the decl's chain itself. */
4550 decl = build_tree_list (id, decl);
4552 /* And put DECL on the list of things declared by the current
4553 binding level. */
4554 add_decl_to_level (b, decl);
4557 /* Lookup the FRIEND_TMPL within all merged module imports. Used to dedup
4558 instantiations of temploid hidden friends from imported modules. */
4560 tree
4561 lookup_imported_hidden_friend (tree friend_tmpl)
4563 /* For a class-scope friend class it should have been found by regular
4564 name lookup. Otherwise we're looking in the current namespace. */
4565 gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
4567 tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
4568 if (!DECL_LANG_SPECIFIC (inner)
4569 || !DECL_MODULE_IMPORT_P (inner))
4570 return NULL_TREE;
4572 tree bind = get_mergeable_namespace_binding
4573 (current_namespace, DECL_NAME (inner), DECL_MODULE_ATTACH_P (inner));
4574 if (!bind)
4575 return NULL_TREE;
4577 /* We're only interested in declarations coming from the same module
4578 of the friend class we're attempting to instantiate. */
4579 int m = get_originating_module (friend_tmpl);
4580 gcc_assert (m != 0);
4582 /* There should be at most one class template from the module we're
4583 looking for, return it. */
4584 for (ovl_iterator iter (bind); iter; ++iter)
4585 if (DECL_CLASS_TEMPLATE_P (*iter)
4586 && get_originating_module (*iter) == m)
4587 return *iter;
4589 return NULL_TREE;
4593 /* true means unconditionally make a BLOCK for the next level pushed. */
4595 static bool keep_next_level_flag;
4597 static int binding_depth = 0;
4599 static void
4600 indent (int depth)
4602 int i;
4604 for (i = 0; i < depth * 2; i++)
4605 putc (' ', stderr);
4608 /* Return a string describing the kind of SCOPE we have. */
4609 static const char *
4610 cp_binding_level_descriptor (cp_binding_level *scope)
4612 /* The order of this table must match the "scope_kind"
4613 enumerators. */
4614 static const char* scope_kind_names[] = {
4615 "block-scope",
4616 "cleanup-scope",
4617 "try-scope",
4618 "catch-scope",
4619 "for-scope",
4620 "cond-init-scope",
4621 "stmt-expr-scope",
4622 "function-parameter-scope",
4623 "class-scope",
4624 "enum-scope",
4625 "namespace-scope",
4626 "template-parameter-scope",
4627 "template-explicit-spec-scope",
4628 "transaction-scope",
4629 "openmp-scope"
4631 static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
4632 "must keep names aligned with scope_kind enum");
4634 scope_kind kind = scope->kind;
4635 if (kind == sk_template_parms && scope->explicit_spec_p)
4636 kind = sk_template_spec;
4638 return scope_kind_names[kind];
4641 /* Output a debugging information about SCOPE when performing
4642 ACTION at LINE. */
4643 static void
4644 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
4646 const char *desc = cp_binding_level_descriptor (scope);
4647 if (scope->this_entity)
4648 verbatim ("%s %<%s(%E)%> %p %d", action, desc,
4649 scope->this_entity, (void *) scope, line);
4650 else
4651 verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
4654 /* A chain of binding_level structures awaiting reuse. */
4656 static GTY((deletable)) cp_binding_level *free_binding_level;
4658 /* Insert SCOPE as the innermost binding level. */
4660 void
4661 push_binding_level (cp_binding_level *scope)
4663 /* Add it to the front of currently active scopes stack. */
4664 scope->level_chain = current_binding_level;
4665 current_binding_level = scope;
4666 keep_next_level_flag = false;
4668 if (ENABLE_SCOPE_CHECKING)
4670 scope->binding_depth = binding_depth;
4671 indent (binding_depth);
4672 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4673 "push");
4674 binding_depth++;
4678 /* Create a new KIND scope and make it the top of the active scopes stack.
4679 ENTITY is the scope of the associated C++ entity (namespace, class,
4680 function, C++0x enumeration); it is NULL otherwise. */
4682 cp_binding_level *
4683 begin_scope (scope_kind kind, tree entity)
4685 cp_binding_level *scope;
4687 /* Reuse or create a struct for this binding level. */
4688 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
4690 scope = free_binding_level;
4691 free_binding_level = scope->level_chain;
4692 memset (scope, 0, sizeof (cp_binding_level));
4694 else
4695 scope = ggc_cleared_alloc<cp_binding_level> ();
4697 scope->this_entity = entity;
4698 scope->more_cleanups_ok = true;
4699 switch (kind)
4701 case sk_cleanup:
4702 scope->keep = true;
4703 break;
4705 case sk_template_spec:
4706 scope->explicit_spec_p = true;
4707 kind = sk_template_parms;
4708 /* Fall through. */
4709 case sk_template_parms:
4710 case sk_block:
4711 case sk_try:
4712 case sk_catch:
4713 case sk_for:
4714 case sk_cond:
4715 case sk_class:
4716 case sk_scoped_enum:
4717 case sk_transaction:
4718 case sk_omp:
4719 case sk_stmt_expr:
4720 scope->keep = keep_next_level_flag;
4721 break;
4723 case sk_function_parms:
4724 scope->keep = keep_next_level_flag;
4725 break;
4727 case sk_namespace:
4728 NAMESPACE_LEVEL (entity) = scope;
4729 break;
4731 default:
4732 /* Should not happen. */
4733 gcc_unreachable ();
4734 break;
4736 scope->kind = kind;
4738 push_binding_level (scope);
4740 return scope;
4743 /* We're about to leave current scope. Pop the top of the stack of
4744 currently active scopes. Return the enclosing scope, now active. */
4746 cp_binding_level *
4747 leave_scope (void)
4749 cp_binding_level *scope = current_binding_level;
4751 if (scope->kind == sk_namespace && class_binding_level)
4752 current_binding_level = class_binding_level;
4754 /* We cannot leave a scope, if there are none left. */
4755 if (NAMESPACE_LEVEL (global_namespace))
4756 gcc_assert (!global_scope_p (scope));
4758 if (ENABLE_SCOPE_CHECKING)
4760 indent (--binding_depth);
4761 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4762 "leave");
4765 /* Move one nesting level up. */
4766 current_binding_level = scope->level_chain;
4768 /* Namespace-scopes are left most probably temporarily, not
4769 completely; they can be reopened later, e.g. in namespace-extension
4770 or any name binding activity that requires us to resume a
4771 namespace. For classes, we cache some binding levels. For other
4772 scopes, we just make the structure available for reuse. */
4773 if (scope->kind != sk_namespace
4774 && scope != previous_class_level)
4776 scope->level_chain = free_binding_level;
4777 gcc_assert (!ENABLE_SCOPE_CHECKING
4778 || scope->binding_depth == binding_depth);
4779 free_binding_level = scope;
4782 if (scope->kind == sk_class)
4784 /* Reset DEFINING_CLASS_P to allow for reuse of a
4785 class-defining scope in a non-defining context. */
4786 scope->defining_class_p = 0;
4788 /* Find the innermost enclosing class scope, and reset
4789 CLASS_BINDING_LEVEL appropriately. */
4790 class_binding_level = NULL;
4791 for (scope = current_binding_level; scope; scope = scope->level_chain)
4792 if (scope->kind == sk_class)
4794 class_binding_level = scope;
4795 break;
4799 return current_binding_level;
4802 /* When we exit a toplevel class scope, we save its binding level so
4803 that we can restore it quickly. Here, we've entered some other
4804 class, so we must invalidate our cache. */
4806 void
4807 invalidate_class_lookup_cache (void)
4809 previous_class_level->level_chain = free_binding_level;
4810 free_binding_level = previous_class_level;
4811 previous_class_level = NULL;
4814 static void
4815 resume_scope (cp_binding_level* b)
4817 /* Resuming binding levels is meant only for namespaces,
4818 and those cannot nest into classes. */
4819 gcc_assert (!class_binding_level);
4820 /* Also, resuming a non-directly nested namespace is a no-no. */
4821 gcc_assert (b->level_chain == current_binding_level);
4822 current_binding_level = b;
4823 if (ENABLE_SCOPE_CHECKING)
4825 b->binding_depth = binding_depth;
4826 indent (binding_depth);
4827 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
4828 binding_depth++;
4832 /* Return the innermost binding level that is not for a class scope. */
4834 static cp_binding_level *
4835 innermost_nonclass_level (void)
4837 cp_binding_level *b;
4839 b = current_binding_level;
4840 while (b->kind == sk_class)
4841 b = b->level_chain;
4843 return b;
4846 /* We're defining an object of type TYPE. If it needs a cleanup, but
4847 we're not allowed to add any more objects with cleanups to the current
4848 scope, create a new binding level. */
4850 void
4851 maybe_push_cleanup_level (tree type)
4853 if (type != error_mark_node
4854 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4855 && current_binding_level->more_cleanups_ok == 0)
4857 begin_scope (sk_cleanup, NULL);
4858 current_binding_level->statement_list = push_stmt_list ();
4862 /* Return true if we are in the global binding level. */
4864 bool
4865 global_bindings_p (void)
4867 return global_scope_p (current_binding_level);
4870 /* True if we are currently in a toplevel binding level. This
4871 means either the global binding level or a namespace in a toplevel
4872 binding level. Since there are no non-toplevel namespace levels,
4873 this really means any namespace or template parameter level. We
4874 also include a class whose context is toplevel. */
4876 bool
4877 toplevel_bindings_p (void)
4879 cp_binding_level *b = innermost_nonclass_level ();
4881 return b->kind == sk_namespace || b->kind == sk_template_parms;
4884 /* True if this is a namespace scope, or if we are defining a class
4885 which is itself at namespace scope, or whose enclosing class is
4886 such a class, etc. */
4888 bool
4889 namespace_bindings_p (void)
4891 cp_binding_level *b = innermost_nonclass_level ();
4893 return b->kind == sk_namespace;
4896 /* True if the innermost non-class scope is a block scope. */
4898 bool
4899 local_bindings_p (void)
4901 cp_binding_level *b = innermost_nonclass_level ();
4902 return b->kind < sk_function_parms || b->kind == sk_omp;
4905 /* True if the current level needs to have a BLOCK made. */
4907 bool
4908 kept_level_p (void)
4910 return (current_binding_level->blocks != NULL_TREE
4911 || current_binding_level->keep
4912 || current_binding_level->kind == sk_cleanup
4913 || current_binding_level->names != NULL_TREE
4914 || current_binding_level->using_directives);
4917 /* Returns the kind of the innermost scope. */
4919 scope_kind
4920 innermost_scope_kind (void)
4922 return current_binding_level->kind;
4925 /* Returns true if this scope was created to store template parameters. */
4927 bool
4928 template_parm_scope_p (void)
4930 return innermost_scope_kind () == sk_template_parms;
4933 /* If KEEP is true, make a BLOCK node for the next binding level,
4934 unconditionally. Otherwise, use the normal logic to decide whether
4935 or not to create a BLOCK. */
4937 void
4938 keep_next_level (bool keep)
4940 keep_next_level_flag = keep;
4943 /* Return the list of declarations of the current local scope. */
4945 tree
4946 get_local_decls (void)
4948 gcc_assert (current_binding_level->kind != sk_namespace
4949 && current_binding_level->kind != sk_class);
4950 return current_binding_level->names;
4953 /* Return how many function prototypes we are currently nested inside. */
4956 function_parm_depth (void)
4958 int level = 0;
4959 cp_binding_level *b;
4961 for (b = current_binding_level;
4962 b->kind == sk_function_parms;
4963 b = b->level_chain)
4964 ++level;
4966 return level;
4969 /* For debugging. */
4970 static int no_print_functions = 0;
4971 static int no_print_builtins = 0;
4973 static void
4974 print_binding_level (cp_binding_level* lvl)
4976 tree t;
4977 int i = 0, len;
4978 if (lvl->this_entity)
4979 print_node_brief (stderr, "entity=", lvl->this_entity, 1);
4980 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
4981 if (lvl->more_cleanups_ok)
4982 fprintf (stderr, " more-cleanups-ok");
4983 if (lvl->have_cleanups)
4984 fprintf (stderr, " have-cleanups");
4985 fprintf (stderr, "\n");
4986 if (lvl->names)
4988 fprintf (stderr, " names:\t");
4989 /* We can probably fit 3 names to a line? */
4990 for (t = lvl->names; t; t = TREE_CHAIN (t))
4992 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
4993 continue;
4994 if (no_print_builtins
4995 && (TREE_CODE (t) == TYPE_DECL)
4996 && DECL_IS_UNDECLARED_BUILTIN (t))
4997 continue;
4999 /* Function decls tend to have longer names. */
5000 if (TREE_CODE (t) == FUNCTION_DECL)
5001 len = 3;
5002 else
5003 len = 2;
5004 i += len;
5005 if (i > 6)
5007 fprintf (stderr, "\n\t");
5008 i = len;
5010 print_node_brief (stderr, "", t, 0);
5011 if (t == error_mark_node)
5012 break;
5014 if (i)
5015 fprintf (stderr, "\n");
5017 if (vec_safe_length (lvl->class_shadowed))
5019 size_t i;
5020 cp_class_binding *b;
5021 fprintf (stderr, " class-shadowed:");
5022 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
5023 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
5024 fprintf (stderr, "\n");
5026 if (lvl->type_shadowed)
5028 fprintf (stderr, " type-shadowed:");
5029 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
5031 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
5033 fprintf (stderr, "\n");
5037 DEBUG_FUNCTION void
5038 debug (cp_binding_level &ref)
5040 print_binding_level (&ref);
5043 DEBUG_FUNCTION void
5044 debug (cp_binding_level *ptr)
5046 if (ptr)
5047 debug (*ptr);
5048 else
5049 fprintf (stderr, "<nil>\n");
5052 static void
5053 print_other_binding_stack (cp_binding_level *stack)
5055 cp_binding_level *level;
5056 for (level = stack; !global_scope_p (level); level = level->level_chain)
5058 fprintf (stderr, "binding level %p\n", (void *) level);
5059 print_binding_level (level);
5063 DEBUG_FUNCTION void
5064 print_binding_stack (void)
5066 cp_binding_level *b;
5067 fprintf (stderr, "current_binding_level=%p\n"
5068 "class_binding_level=%p\n"
5069 "NAMESPACE_LEVEL (global_namespace)=%p\n",
5070 (void *) current_binding_level, (void *) class_binding_level,
5071 (void *) NAMESPACE_LEVEL (global_namespace));
5072 if (class_binding_level)
5074 for (b = class_binding_level; b; b = b->level_chain)
5075 if (b == current_binding_level)
5076 break;
5077 if (b)
5078 b = class_binding_level;
5079 else
5080 b = current_binding_level;
5082 else
5083 b = current_binding_level;
5084 print_other_binding_stack (b);
5085 fprintf (stderr, "global:\n");
5086 print_binding_level (NAMESPACE_LEVEL (global_namespace));
5089 /* Push a definition of struct, union or enum tag named ID. into
5090 binding_level B. DECL is a TYPE_DECL for the type. DECL has
5091 already been pushed into its binding level. This is bookkeeping to
5092 find it easily. */
5094 static void
5095 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
5097 if (b->kind == sk_namespace)
5098 /* At namespace scope we should not see an identifier type value. */
5099 gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
5100 /* We could be pushing a friend underneath a template
5101 parm (ill-formed). */
5102 || (TEMPLATE_PARM_P
5103 (TYPE_NAME (REAL_IDENTIFIER_TYPE_VALUE (id)))));
5104 else
5106 /* Push the current type value, so we can restore it later */
5107 tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
5108 b->type_shadowed = tree_cons (id, old, b->type_shadowed);
5109 tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
5110 TREE_TYPE (b->type_shadowed) = type;
5111 SET_IDENTIFIER_TYPE_VALUE (id, type);
5115 /* As set_identifier_type_value_with_scope, but using
5116 current_binding_level. */
5118 void
5119 set_identifier_type_value (tree id, tree decl)
5121 set_identifier_type_value_with_scope (id, decl, current_binding_level);
5124 /* Return the name for the constructor (or destructor) for the
5125 specified class. */
5127 tree
5128 constructor_name (tree type)
5130 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
5132 return decl ? DECL_NAME (decl) : NULL_TREE;
5135 /* Returns TRUE if NAME is the name for the constructor for TYPE,
5136 which must be a class type. */
5138 bool
5139 constructor_name_p (tree name, tree type)
5141 gcc_assert (MAYBE_CLASS_TYPE_P (type));
5143 /* These don't have names. */
5144 if (TREE_CODE (type) == DECLTYPE_TYPE
5145 || TREE_CODE (type) == TYPEOF_TYPE)
5146 return false;
5148 if (name && name == constructor_name (type))
5149 return true;
5151 return false;
5154 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
5155 caller to set DECL_CONTEXT properly.
5157 Warning: For class and block-scope this must only be used when X
5158 will be the new innermost binding for its name, as we tack it onto
5159 the front of IDENTIFIER_BINDING without checking to see if the
5160 current IDENTIFIER_BINDING comes from a closer binding level than
5161 LEVEL.
5163 Warning: For namespace scope, this will look in LEVEL for an
5164 existing binding to match, but if not found will push the decl into
5165 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
5166 pop_nested_namespace if you really need to push it into a foreign
5167 namespace. */
5169 static tree
5170 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
5172 cp_binding_level *b;
5174 if (level->kind == sk_class)
5176 gcc_checking_assert (!hiding);
5177 b = class_binding_level;
5178 class_binding_level = level;
5179 pushdecl_class_level (x);
5180 class_binding_level = b;
5182 else
5184 tree function_decl = current_function_decl;
5185 if (level->kind == sk_namespace)
5186 current_function_decl = NULL_TREE;
5187 b = current_binding_level;
5188 current_binding_level = level;
5189 x = pushdecl (x, hiding);
5190 current_binding_level = b;
5191 current_function_decl = function_decl;
5193 return x;
5196 /* Inject X into the local scope just before the function parms. */
5198 tree
5199 pushdecl_outermost_localscope (tree x)
5201 cp_binding_level *b = NULL;
5202 auto_cond_timevar tv (TV_NAME_LOOKUP);
5204 /* Find the scope just inside the function parms. */
5205 for (cp_binding_level *n = current_binding_level;
5206 n->kind != sk_function_parms; n = b->level_chain)
5207 b = n;
5209 return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
5212 /* Checks if BINDING is a binding that we can export. */
5214 static bool
5215 check_can_export_using_decl (tree binding)
5217 /* Declarations in header units are always OK. */
5218 if (header_module_p ())
5219 return true;
5221 /* We want the linkage of the underlying entity, so strip typedefs.
5222 If the underlying entity is a builtin type then we're OK. */
5223 tree entity = binding;
5224 if (TREE_CODE (entity) == TYPE_DECL)
5226 entity = TYPE_MAIN_DECL (TREE_TYPE (entity));
5227 if (!entity)
5228 return true;
5231 linkage_kind linkage = decl_linkage (entity);
5232 tree not_tmpl = STRIP_TEMPLATE (entity);
5234 /* Attachment is determined by the owner of an enumerator. */
5235 if (TREE_CODE (not_tmpl) == CONST_DECL)
5236 not_tmpl = TYPE_NAME (DECL_CONTEXT (not_tmpl));
5238 /* If the using decl is exported, the things it refers to must
5239 have external linkage. decl_linkage returns lk_external for
5240 module linkage so also check for attachment. */
5241 if (linkage != lk_external
5242 || (DECL_LANG_SPECIFIC (not_tmpl)
5243 && DECL_MODULE_ATTACH_P (not_tmpl)
5244 && !DECL_MODULE_EXPORT_P (not_tmpl)))
5246 auto_diagnostic_group d;
5247 error ("exporting %q#D that does not have external linkage",
5248 binding);
5249 if (linkage == lk_none)
5250 inform (DECL_SOURCE_LOCATION (entity),
5251 "%q#D declared here with no linkage", entity);
5252 else if (linkage == lk_internal)
5253 inform (DECL_SOURCE_LOCATION (entity),
5254 "%q#D declared here with internal linkage", entity);
5255 else
5256 inform (DECL_SOURCE_LOCATION (entity),
5257 "%q#D declared here with module linkage", entity);
5258 return false;
5261 return true;
5264 /* Process a local-scope or namespace-scope using declaration. LOOKUP
5265 is the result of qualified lookup (both value & type are
5266 significant). FN_SCOPE_P indicates if we're at function-scope (as
5267 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
5268 bindings, which are altered to reflect the newly brought in
5269 declarations. */
5271 static bool
5272 do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
5273 bool insert_p, tree *value_p, tree *type_p)
5275 tree value = *value_p;
5276 tree type = *type_p;
5277 bool failed = false;
5279 /* Shift the old and new bindings around so we're comparing class and
5280 enumeration names to each other. */
5281 if (value && DECL_IMPLICIT_TYPEDEF_P (strip_using_decl (value)))
5283 type = value;
5284 value = NULL_TREE;
5287 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
5289 lookup.type = lookup.value;
5290 lookup.value = NULL_TREE;
5293 /* Only process exporting if we're going to be inserting. */
5294 bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
5296 /* First do the value binding. */
5297 if (!lookup.value)
5298 /* Nothing (only implicit typedef found). */
5299 gcc_checking_assert (lookup.type);
5300 else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
5302 for (lkp_iterator usings (lookup.value); usings; ++usings)
5304 tree new_fn = *usings;
5305 tree inner = STRIP_TEMPLATE (new_fn);
5306 bool exporting_p = revealing_p && module_exporting_p ();
5307 if (exporting_p)
5308 exporting_p = check_can_export_using_decl (new_fn);
5310 /* [namespace.udecl]
5312 If a function declaration in namespace scope or block
5313 scope has the same name and the same parameter types as a
5314 function introduced by a using declaration the program is
5315 ill-formed. */
5316 /* This seems overreaching, asking core -- why do we care
5317 about decls in the namespace that we cannot name (because
5318 they are not transitively imported. We just check the
5319 decls that are in this TU. */
5320 bool found = false;
5321 for (ovl_iterator old (value); !found && old; ++old)
5323 tree old_fn = *old;
5325 if (new_fn == old_fn)
5327 /* The function already exists in the current
5328 namespace. We will still want to insert it if
5329 it is revealing a not-revealed thing. */
5330 found = true;
5331 if (!revealing_p)
5333 else if (old.using_p ())
5335 /* Update in place. 'tis ok. */
5336 OVL_PURVIEW_P (old.get_using ()) = true;
5337 if (exporting_p)
5338 OVL_EXPORT_P (old.get_using ()) = true;
5340 else if (!DECL_LANG_SPECIFIC (inner)
5341 || !DECL_MODULE_PURVIEW_P (inner))
5342 /* We need to re-insert this function as a revealed
5343 (possibly exported) declaration. We can't remove
5344 the existing decl because that will change any
5345 overloads cached in template functions. */
5346 found = false;
5347 break;
5349 else if (old.using_p ())
5350 continue; /* This is a using decl. */
5351 else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
5352 continue; /* This is an anticipated builtin. */
5353 else if (!matching_fn_p (new_fn, old_fn))
5354 continue; /* Parameters do not match. */
5355 else if (decls_match (new_fn, old_fn))
5357 /* Extern "C" in different namespaces. But similarly
5358 to above, if revealing a not-revealed thing we may
5359 need to reinsert. */
5360 found = true;
5361 if (revealing_p
5362 && (!DECL_LANG_SPECIFIC (inner)
5363 || !DECL_MODULE_PURVIEW_P (inner)))
5364 found = false;
5365 break;
5367 else
5369 diagnose_name_conflict (new_fn, old_fn);
5370 failed = true;
5371 found = true;
5372 break;
5376 if (!found && insert_p)
5377 /* Unlike the decl-pushing case we don't drop anticipated
5378 builtins here. They don't cause a problem, and we'd
5379 like to match them with a future declaration. */
5380 value = ovl_insert (new_fn, value, 1 + revealing_p + exporting_p);
5383 else if (value
5384 /* Ignore anticipated builtins. */
5385 && !anticipated_builtin_p (value)
5386 && !decls_match (lookup.value, strip_using_decl (value)))
5388 diagnose_name_conflict (lookup.value, value);
5389 failed = true;
5391 else if (insert_p)
5393 /* A using-decl does not necessarily have the same purview-ness or
5394 exporting as the declaration it reveals, so build a USING_DECL
5395 that we can attach this information to. This also gives us a
5396 location for the using-decl that we can use in diagnostics.
5398 But this is unnecessary if we're just redeclaring the same decl;
5399 in that case we can just mark it purview or exported directly. */
5400 if (value != lookup.value)
5402 value = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5403 USING_DECL_DECLS (value) = lookup.value;
5404 USING_DECL_SCOPE (value) = CP_DECL_CONTEXT (lookup.value);
5405 DECL_CONTEXT (value) = current_scope ();
5406 DECL_MODULE_PURVIEW_P (value) = module_purview_p ();
5408 else
5409 set_instantiating_module (value);
5411 if (revealing_p
5412 && module_exporting_p ()
5413 && check_can_export_using_decl (lookup.value))
5415 if (TREE_CODE (value) == TEMPLATE_DECL)
5416 DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (value)) = true;
5417 DECL_MODULE_EXPORT_P (value) = true;
5421 /* Now the type binding. */
5422 if (lookup.type)
5424 if (type && !decls_match (lookup.type, strip_using_decl (type)))
5426 diagnose_name_conflict (lookup.type, type);
5427 failed = true;
5429 else if (insert_p)
5431 /* As with revealing value bindings. */
5432 if (type != lookup.type)
5434 type = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5435 USING_DECL_DECLS (type) = lookup.type;
5436 USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (lookup.type);
5437 DECL_CONTEXT (type) = current_scope ();
5438 DECL_MODULE_PURVIEW_P (type) = module_purview_p ();
5440 else
5441 set_instantiating_module (type);
5443 if (revealing_p
5444 && module_exporting_p ()
5445 && check_can_export_using_decl (lookup.type))
5446 DECL_MODULE_EXPORT_P (type) = true;
5450 if (insert_p)
5452 /* If value is empty, shift any class or enumeration name back. */
5453 if (!value)
5455 value = type;
5456 type = NULL_TREE;
5458 *value_p = value;
5459 *type_p = type;
5462 return failed;
5465 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5466 Both are namespaces. */
5468 bool
5469 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
5471 int depth = SCOPE_DEPTH (ancestor);
5473 if (!depth && !inline_only)
5474 /* The global namespace encloses everything. */
5475 return true;
5477 while (SCOPE_DEPTH (descendant) > depth
5478 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
5479 descendant = CP_DECL_CONTEXT (descendant);
5481 return ancestor == descendant;
5484 /* Returns true if ROOT (a non-alias namespace, class, or function)
5485 encloses CHILD. CHILD may be either a class type or a namespace
5486 (maybe alias). */
5488 bool
5489 is_ancestor (tree root, tree child)
5491 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
5492 && !DECL_NAMESPACE_ALIAS (root))
5493 || TREE_CODE (root) == FUNCTION_DECL
5494 || CLASS_TYPE_P (root));
5495 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
5496 || CLASS_TYPE_P (child));
5498 /* The global namespace encloses everything. Early-out for the
5499 common case. */
5500 if (root == global_namespace)
5501 return true;
5503 /* Search CHILD until we reach namespace scope. */
5504 while (TREE_CODE (child) != NAMESPACE_DECL)
5506 /* If we've reached the ROOT, it encloses CHILD. */
5507 if (root == child)
5508 return true;
5510 /* Go out one level. */
5511 if (TYPE_P (child))
5512 child = TYPE_NAME (child);
5513 child = CP_DECL_CONTEXT (child);
5516 if (TREE_CODE (root) != NAMESPACE_DECL)
5517 /* Failed to meet the non-namespace we were looking for. */
5518 return false;
5520 if (tree alias = DECL_NAMESPACE_ALIAS (child))
5521 child = alias;
5523 return is_nested_namespace (root, child);
5526 /* Enter the class or namespace scope indicated by T suitable for name
5527 lookup. T can be arbitrary scope, not necessary nested inside the
5528 current scope. Returns a non-null scope to pop iff pop_scope
5529 should be called later to exit this scope. */
5531 tree
5532 push_scope (tree t)
5534 if (TREE_CODE (t) == NAMESPACE_DECL)
5535 push_decl_namespace (t);
5536 else if (CLASS_TYPE_P (t))
5538 if (!at_class_scope_p ()
5539 || !same_type_p (current_class_type, t))
5540 push_nested_class (t);
5541 else
5542 /* T is the same as the current scope. There is therefore no
5543 need to re-enter the scope. Since we are not actually
5544 pushing a new scope, our caller should not call
5545 pop_scope. */
5546 t = NULL_TREE;
5549 return t;
5552 /* Leave scope pushed by push_scope. */
5554 void
5555 pop_scope (tree t)
5557 if (t == NULL_TREE)
5558 return;
5559 if (TREE_CODE (t) == NAMESPACE_DECL)
5560 pop_decl_namespace ();
5561 else if CLASS_TYPE_P (t)
5562 pop_nested_class ();
5565 /* Subroutine of push_inner_scope. */
5567 static void
5568 push_inner_scope_r (tree outer, tree inner)
5570 tree prev;
5572 if (outer == inner
5573 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5574 return;
5576 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5577 if (outer != prev)
5578 push_inner_scope_r (outer, prev);
5579 if (TREE_CODE (inner) == NAMESPACE_DECL)
5581 cp_binding_level *save_template_parm = 0;
5582 /* Temporary take out template parameter scopes. They are saved
5583 in reversed order in save_template_parm. */
5584 while (current_binding_level->kind == sk_template_parms)
5586 cp_binding_level *b = current_binding_level;
5587 current_binding_level = b->level_chain;
5588 b->level_chain = save_template_parm;
5589 save_template_parm = b;
5592 resume_scope (NAMESPACE_LEVEL (inner));
5593 current_namespace = inner;
5595 /* Restore template parameter scopes. */
5596 while (save_template_parm)
5598 cp_binding_level *b = save_template_parm;
5599 save_template_parm = b->level_chain;
5600 b->level_chain = current_binding_level;
5601 current_binding_level = b;
5604 else
5605 pushclass (inner);
5608 /* Enter the scope INNER from current scope. INNER must be a scope
5609 nested inside current scope. This works with both name lookup and
5610 pushing name into scope. In case a template parameter scope is present,
5611 namespace is pushed under the template parameter scope according to
5612 name lookup rule in 14.6.1/6.
5614 Return the former current scope suitable for pop_inner_scope. */
5616 tree
5617 push_inner_scope (tree inner)
5619 tree outer = current_scope ();
5620 if (!outer)
5621 outer = current_namespace;
5623 push_inner_scope_r (outer, inner);
5624 return outer;
5627 /* Exit the current scope INNER back to scope OUTER. */
5629 void
5630 pop_inner_scope (tree outer, tree inner)
5632 if (outer == inner
5633 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5634 return;
5636 while (outer != inner)
5638 if (TREE_CODE (inner) == NAMESPACE_DECL)
5640 cp_binding_level *save_template_parm = 0;
5641 /* Temporary take out template parameter scopes. They are saved
5642 in reversed order in save_template_parm. */
5643 while (current_binding_level->kind == sk_template_parms)
5645 cp_binding_level *b = current_binding_level;
5646 current_binding_level = b->level_chain;
5647 b->level_chain = save_template_parm;
5648 save_template_parm = b;
5651 pop_namespace ();
5653 /* Restore template parameter scopes. */
5654 while (save_template_parm)
5656 cp_binding_level *b = save_template_parm;
5657 save_template_parm = b->level_chain;
5658 b->level_chain = current_binding_level;
5659 current_binding_level = b;
5662 else
5663 popclass ();
5665 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5669 /* Do a pushlevel for class declarations. */
5671 void
5672 pushlevel_class (void)
5674 class_binding_level = begin_scope (sk_class, current_class_type);
5677 /* ...and a poplevel for class declarations. */
5679 void
5680 poplevel_class (void)
5682 cp_binding_level *level = class_binding_level;
5683 cp_class_binding *cb;
5684 size_t i;
5685 tree shadowed;
5687 auto_cond_timevar tv (TV_NAME_LOOKUP);
5688 gcc_assert (level != 0);
5690 /* If we're leaving a toplevel class, cache its binding level. */
5691 if (current_class_depth == 1)
5692 previous_class_level = level;
5693 for (shadowed = level->type_shadowed;
5694 shadowed;
5695 shadowed = TREE_CHAIN (shadowed))
5696 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
5698 /* Remove the bindings for all of the class-level declarations. */
5699 if (level->class_shadowed)
5701 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
5703 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
5704 cxx_binding_free (cb->base);
5706 ggc_free (level->class_shadowed);
5707 level->class_shadowed = NULL;
5710 /* Now, pop out of the binding level which we created up in the
5711 `pushlevel_class' routine. */
5712 gcc_assert (current_binding_level == level);
5713 leave_scope ();
5716 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5717 appropriate. DECL is the value to which a name has just been
5718 bound. CLASS_TYPE is the class in which the lookup occurred. */
5720 static void
5721 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
5722 tree class_type)
5724 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
5726 tree context;
5728 if (is_overloaded_fn (decl))
5729 context = ovl_scope (decl);
5730 else
5732 gcc_assert (DECL_P (decl));
5733 context = context_for_name_lookup (decl);
5736 if (is_properly_derived_from (class_type, context))
5737 INHERITED_VALUE_BINDING_P (binding) = 1;
5738 else
5739 INHERITED_VALUE_BINDING_P (binding) = 0;
5741 else if (binding->value == decl)
5742 /* We only encounter a TREE_LIST when there is an ambiguity in the
5743 base classes. Such an ambiguity can be overridden by a
5744 definition in this class. */
5745 INHERITED_VALUE_BINDING_P (binding) = 1;
5746 else
5747 INHERITED_VALUE_BINDING_P (binding) = 0;
5750 /* Make the declaration of X appear in CLASS scope. */
5752 bool
5753 pushdecl_class_level (tree x)
5755 bool is_valid = true;
5757 /* Do nothing if we're adding to an outer lambda closure type,
5758 outer_binding will add it later if it's needed. */
5759 if (current_class_type != class_binding_level->this_entity)
5760 return true;
5762 auto_cond_timevar tv (TV_NAME_LOOKUP);
5763 /* Get the name of X. */
5764 tree name = OVL_NAME (x);
5766 if (name)
5768 is_valid = push_class_level_binding (name, x);
5769 if (TREE_CODE (x) == TYPE_DECL)
5770 set_identifier_type_value (name, x);
5772 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
5774 /* If X is an anonymous aggregate, all of its members are
5775 treated as if they were members of the class containing the
5776 aggregate, for naming purposes. */
5777 location_t save_location = input_location;
5778 tree anon = TREE_TYPE (x);
5779 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
5780 for (unsigned ix = member_vec->length (); ix--;)
5782 tree binding = (*member_vec)[ix];
5783 if (STAT_HACK_P (binding))
5785 if (!pushdecl_class_level (STAT_TYPE (binding)))
5786 is_valid = false;
5787 binding = STAT_DECL (binding);
5789 if (!pushdecl_class_level (binding))
5790 is_valid = false;
5792 else
5793 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
5794 if (TREE_CODE (f) == FIELD_DECL)
5796 input_location = DECL_SOURCE_LOCATION (f);
5797 if (!pushdecl_class_level (f))
5798 is_valid = false;
5800 input_location = save_location;
5802 return is_valid;
5805 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5806 scope. If the value returned is non-NULL, and the PREVIOUS field
5807 is not set, callers must set the PREVIOUS field explicitly. */
5809 static cxx_binding *
5810 get_class_binding (tree name, cp_binding_level *scope)
5812 tree class_type;
5813 tree type_binding;
5814 tree value_binding;
5815 cxx_binding *binding;
5817 class_type = scope->this_entity;
5819 /* Get the type binding. */
5820 type_binding = lookup_member (class_type, name,
5821 /*protect=*/2, /*want_type=*/true,
5822 tf_warning_or_error);
5823 /* Get the value binding. */
5824 value_binding = lookup_member (class_type, name,
5825 /*protect=*/2, /*want_type=*/false,
5826 tf_warning_or_error);
5828 /* If we found either a type binding or a value binding, create a
5829 new binding object. */
5830 if (type_binding || value_binding)
5832 binding = new_class_binding (name,
5833 value_binding,
5834 type_binding,
5835 scope);
5836 set_inherited_value_binding_p (binding, value_binding, class_type);
5838 else
5839 binding = NULL;
5841 return binding;
5844 /* Make the declaration(s) of X appear in CLASS scope under the name
5845 NAME. Returns true if the binding is valid. */
5847 bool
5848 push_class_level_binding (tree name, tree x)
5850 cxx_binding *binding;
5851 tree decl = x;
5852 bool ok;
5854 auto_cond_timevar tv (TV_NAME_LOOKUP);
5856 /* The class_binding_level will be NULL if x is a template
5857 parameter name in a member template. */
5858 if (!class_binding_level)
5859 return true;
5861 if (name == error_mark_node)
5862 return false;
5864 /* Can happen for an erroneous declaration (c++/60384). */
5865 if (!identifier_p (name))
5867 gcc_assert (errorcount || sorrycount);
5868 return false;
5871 /* Check for invalid member names. But don't worry about a default
5872 argument-scope lambda being pushed after the class is complete. */
5873 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
5874 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
5875 /* Check that we're pushing into the right binding level. */
5876 gcc_assert (current_class_type == class_binding_level->this_entity);
5878 /* We could have been passed a tree list if this is an ambiguous
5879 declaration. If so, pull the declaration out because
5880 check_template_shadow will not handle a TREE_LIST. */
5881 if (TREE_CODE (decl) == TREE_LIST
5882 && TREE_TYPE (decl) == error_mark_node)
5883 decl = TREE_VALUE (decl);
5885 if (!check_template_shadow (decl))
5886 return false;
5888 /* [class.mem]
5890 If T is the name of a class, then each of the following shall
5891 have a name different from T:
5893 -- every static data member of class T;
5895 -- every member of class T that is itself a type;
5897 -- every enumerator of every member of class T that is an
5898 enumerated type;
5900 -- every member of every anonymous union that is a member of
5901 class T.
5903 (Non-static data members were also forbidden to have the same
5904 name as T until TC1.) */
5905 if ((VAR_P (x)
5906 || TREE_CODE (x) == CONST_DECL
5907 || (TREE_CODE (x) == TYPE_DECL
5908 && !DECL_SELF_REFERENCE_P (x))
5909 /* A data member of an anonymous union. */
5910 || (TREE_CODE (x) == FIELD_DECL
5911 && DECL_CONTEXT (x) != current_class_type))
5912 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
5914 tree scope = context_for_name_lookup (x);
5915 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
5917 error_at (DECL_SOURCE_LOCATION (x),
5918 "%qD has the same name as the class in which it is "
5919 "declared", x);
5920 return false;
5924 /* Get the current binding for NAME in this class, if any. */
5925 binding = IDENTIFIER_BINDING (name);
5926 if (!binding || binding->scope != class_binding_level)
5928 binding = get_class_binding (name, class_binding_level);
5929 /* If a new binding was created, put it at the front of the
5930 IDENTIFIER_BINDING list. */
5931 if (binding)
5933 binding->previous = IDENTIFIER_BINDING (name);
5934 IDENTIFIER_BINDING (name) = binding;
5938 /* If there is already a binding, then we may need to update the
5939 current value. */
5940 if (binding && binding->value)
5942 tree bval = binding->value;
5943 tree old_decl = NULL_TREE;
5944 tree target_decl = strip_using_decl (decl);
5945 tree target_bval = strip_using_decl (bval);
5947 if (INHERITED_VALUE_BINDING_P (binding))
5949 /* If the old binding was from a base class, and was for a
5950 tag name, slide it over to make room for the new binding.
5951 The old binding is still visible if explicitly qualified
5952 with a class-key. */
5953 if (TREE_CODE (target_bval) == TYPE_DECL
5954 && DECL_ARTIFICIAL (target_bval)
5955 && !(TREE_CODE (target_decl) == TYPE_DECL
5956 && DECL_ARTIFICIAL (target_decl)))
5958 old_decl = binding->type;
5959 binding->type = bval;
5960 binding->value = NULL_TREE;
5961 INHERITED_VALUE_BINDING_P (binding) = 0;
5963 else
5965 old_decl = bval;
5966 /* Any inherited type declaration is hidden by the type
5967 declaration in the derived class. */
5968 if (TREE_CODE (target_decl) == TYPE_DECL
5969 && DECL_ARTIFICIAL (target_decl))
5970 binding->type = NULL_TREE;
5973 else if (TREE_CODE (decl) == USING_DECL
5974 && TREE_CODE (bval) == USING_DECL
5975 && same_type_p (USING_DECL_SCOPE (decl),
5976 USING_DECL_SCOPE (bval)))
5977 /* This is a using redeclaration that will be diagnosed later
5978 in supplement_binding */
5980 else if (TREE_CODE (decl) == USING_DECL
5981 && TREE_CODE (bval) == USING_DECL
5982 && DECL_DEPENDENT_P (decl)
5983 && DECL_DEPENDENT_P (bval))
5984 return true;
5985 else if (TREE_CODE (decl) == USING_DECL
5986 && DECL_DEPENDENT_P (decl)
5987 && OVL_P (target_bval))
5988 /* The new dependent using beats an old overload. */
5989 old_decl = bval;
5990 else if (TREE_CODE (bval) == USING_DECL
5991 && DECL_DEPENDENT_P (bval)
5992 && OVL_P (target_decl))
5993 /* The old dependent using beats a new overload. */
5994 return true;
5995 else if (OVL_P (target_decl)
5996 && OVL_P (target_bval))
5997 /* The new overload set contains the old one. */
5998 old_decl = bval;
6000 if (old_decl && binding->scope == class_binding_level)
6002 binding->value = x;
6003 /* It is always safe to clear INHERITED_VALUE_BINDING_P
6004 here. This function is only used to register bindings
6005 from with the class definition itself. */
6006 INHERITED_VALUE_BINDING_P (binding) = 0;
6007 return true;
6011 /* Note that we declared this value so that we can issue an error if
6012 this is an invalid redeclaration of a name already used for some
6013 other purpose. */
6014 note_name_declared_in_class (name, decl);
6016 /* If we didn't replace an existing binding, put the binding on the
6017 stack of bindings for the identifier, and update the shadowed
6018 list. */
6019 if (binding && binding->scope == class_binding_level)
6020 /* Supplement the existing binding. */
6021 ok = supplement_binding (binding, decl);
6022 else
6024 /* Create a new binding. */
6025 push_binding (name, decl, class_binding_level);
6026 ok = true;
6029 return ok;
6032 /* Process and lookup a using decl SCOPE::lookup.name, filling in
6033 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
6034 failure. */
6036 static tree
6037 lookup_using_decl (tree scope, name_lookup &lookup)
6039 tree current = current_scope ();
6040 bool dependent_p = false;
6041 tree binfo = NULL_TREE;
6042 base_kind b_kind = bk_not_base;
6044 /* Because C++20 breaks the invariant that only member using-decls
6045 refer to members and only non-member using-decls refer to
6046 non-members, we first do the lookups, and then do validation that
6047 what we found is ok. */
6049 if (TREE_CODE (scope) == ENUMERAL_TYPE
6050 && cxx_dialect < cxx20
6051 && UNSCOPED_ENUM_P (scope)
6052 && !TYPE_FUNCTION_SCOPE_P (scope))
6054 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
6055 should allow it as meaning the enclosing scope. I don't see any
6056 justification for this in C++11, but let's keep allowing it. */
6057 tree ctx = CP_TYPE_CONTEXT (scope);
6058 if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
6059 scope = ctx;
6062 /* You cannot using-decl a destructor. */
6063 if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
6065 error ("%<%T%s%D%> names destructor", scope,
6066 &"::"[scope == global_namespace ? 2 : 0], lookup.name);
6067 return NULL_TREE;
6070 if (TREE_CODE (scope) == NAMESPACE_DECL)
6072 /* Naming a namespace member. */
6073 qualified_namespace_lookup (scope, &lookup);
6075 if (TYPE_P (current)
6076 && (!lookup.value
6077 || lookup.type
6078 || cxx_dialect < cxx20
6079 || TREE_CODE (lookup.value) != CONST_DECL))
6081 error ("using-declaration for non-member at class scope");
6082 return NULL_TREE;
6085 else if (TREE_CODE (scope) == ENUMERAL_TYPE)
6087 /* Naming an enumeration member. */
6088 if (cxx_dialect < cxx20)
6089 error ("%<using%> with enumeration scope %q#T "
6090 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
6091 scope);
6092 lookup.value = lookup_enumerator (scope, lookup.name);
6094 else
6096 /* Naming a class member. This is awkward in C++20, because we
6097 might be naming an enumerator of an unrelated class. */
6099 tree npscope = scope;
6100 if (PACK_EXPANSION_P (scope))
6101 npscope = PACK_EXPANSION_PATTERN (scope);
6103 if (!MAYBE_CLASS_TYPE_P (npscope))
6105 error ("%qT is not a class, namespace, or enumeration", npscope);
6106 return NULL_TREE;
6109 /* Using T::T declares inheriting ctors, even if T is a typedef. */
6110 if (lookup.name == TYPE_IDENTIFIER (npscope)
6111 || constructor_name_p (lookup.name, npscope))
6113 if (!TYPE_P (current))
6115 error ("non-member using-declaration names constructor of %qT",
6116 npscope);
6117 return NULL_TREE;
6119 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
6120 lookup.name = ctor_identifier;
6121 CLASSTYPE_NON_AGGREGATE (current) = true;
6124 if (!TYPE_P (current) && cxx_dialect < cxx20)
6126 error ("using-declaration for member at non-class scope");
6127 return NULL_TREE;
6130 bool depscope = dependent_scope_p (scope);
6132 if (depscope)
6133 /* Leave binfo null. */;
6134 else if (TYPE_P (current))
6136 binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
6137 gcc_checking_assert (b_kind >= bk_not_base);
6139 if (b_kind == bk_not_base && any_dependent_bases_p ())
6140 /* Treat as-if dependent. */
6141 depscope = true;
6142 else if (lookup.name == ctor_identifier
6143 && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
6145 if (any_dependent_bases_p ())
6146 depscope = true;
6147 else
6149 error ("%qT is not a direct base of %qT", scope, current);
6150 return NULL_TREE;
6154 if (b_kind < bk_proper_base)
6155 binfo = TYPE_BINFO (scope);
6157 else
6158 binfo = TYPE_BINFO (scope);
6160 dependent_p = (depscope
6161 || (IDENTIFIER_CONV_OP_P (lookup.name)
6162 && dependent_type_p (TREE_TYPE (lookup.name))));
6164 if (!dependent_p)
6165 lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
6166 /*want_type=*/false, tf_none);
6168 /* If the lookup in the base contains a dependent using, this
6169 using is also dependent. */
6170 if (!dependent_p && lookup.value && dependent_type_p (scope))
6172 tree val = lookup.value;
6173 if (tree fns = maybe_get_fns (val))
6174 val = fns;
6175 for (tree f: lkp_range (val))
6176 if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
6178 dependent_p = true;
6179 break;
6183 if (!depscope && b_kind < bk_proper_base)
6185 if (cxx_dialect >= cxx20 && lookup.value
6186 && TREE_CODE (lookup.value) == CONST_DECL)
6188 /* Using an unrelated enum; check access here rather
6189 than separately for class and non-class using. */
6190 perform_or_defer_access_check
6191 (binfo, lookup.value, lookup.value, tf_warning_or_error);
6192 /* And then if this is a copy from handle_using_decl, look
6193 through to the original enumerator. */
6194 if (CONST_DECL_USING_P (lookup.value))
6195 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
6197 else if (!TYPE_P (current))
6199 error ("using-declaration for member at non-class scope");
6200 return NULL_TREE;
6202 else
6204 auto_diagnostic_group g;
6205 error_not_base_type (scope, current);
6206 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
6207 && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
6208 inform (input_location,
6209 "did you mean %<using enum %T::%D%>?",
6210 scope, lookup.name);
6211 return NULL_TREE;
6216 /* Did we find anything sane? */
6217 if (dependent_p)
6219 else if (!lookup.value)
6221 error ("%qD has not been declared in %qD", lookup.name, scope);
6222 return NULL_TREE;
6224 else if (TREE_CODE (lookup.value) == TREE_LIST
6225 /* We can (independently) have ambiguous implicit typedefs. */
6226 || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
6228 auto_diagnostic_group d;
6229 error ("reference to %qD is ambiguous", lookup.name);
6230 print_candidates (TREE_CODE (lookup.value) == TREE_LIST
6231 ? lookup.value : lookup.type);
6232 return NULL_TREE;
6234 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
6236 error ("using-declaration may not name namespace %qD", lookup.value);
6237 return NULL_TREE;
6240 if (TYPE_P (current))
6242 /* In class scope. */
6244 /* Cannot introduce a constructor name. */
6245 if (constructor_name_p (lookup.name, current))
6247 error ("%<%T::%D%> names constructor in %qT",
6248 scope, lookup.name, current);
6249 return NULL_TREE;
6252 if (lookup.value && BASELINK_P (lookup.value))
6253 /* The binfo from which the functions came does not matter. */
6254 lookup.value = BASELINK_FUNCTIONS (lookup.value);
6257 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
6258 USING_DECL_SCOPE (using_decl) = scope;
6259 USING_DECL_DECLS (using_decl) = lookup.value;
6260 DECL_DEPENDENT_P (using_decl) = dependent_p;
6261 DECL_CONTEXT (using_decl) = current;
6262 if (TYPE_P (current) && b_kind == bk_not_base)
6263 USING_DECL_UNRELATED_P (using_decl) = true;
6265 return using_decl;
6268 /* Process "using SCOPE::NAME" in a class scope. Return the
6269 USING_DECL created. */
6271 tree
6272 do_class_using_decl (tree scope, tree name)
6274 if (name == error_mark_node
6275 || scope == error_mark_node)
6276 return NULL_TREE;
6278 name_lookup lookup (name);
6279 return lookup_using_decl (scope, lookup);
6283 /* Return the binding for NAME in NS in the current TU. If NS is
6284 NULL, look in global_namespace. We will not find declarations
6285 from imports. Users of this who, having found nothing, push a new
6286 decl must be prepared for that pushing to match an existing decl. */
6288 tree
6289 get_namespace_binding (tree ns, tree name)
6291 auto_cond_timevar tv (TV_NAME_LOOKUP);
6292 if (!ns)
6293 ns = global_namespace;
6294 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
6295 tree ret = NULL_TREE;
6297 if (tree *b = find_namespace_slot (ns, name))
6299 ret = *b;
6301 if (TREE_CODE (ret) == BINDING_VECTOR)
6302 ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
6303 if (ret)
6304 ret = strip_using_decl (MAYBE_STAT_DECL (ret));
6307 return ret;
6310 /* Push internal DECL into the global namespace. Does not do the
6311 full overload fn handling and does not add it to the list of things
6312 in the namespace. */
6314 void
6315 set_global_binding (tree decl)
6317 auto_cond_timevar tv (TV_NAME_LOOKUP);
6319 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
6321 if (*slot)
6322 /* The user's placed something in the implementor's namespace. */
6323 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
6325 /* Force the binding, so compiler internals continue to work. */
6326 *slot = decl;
6329 /* Set the context of a declaration to scope. Complain if we are not
6330 outside scope. */
6332 void
6333 set_decl_namespace (tree decl, tree scope, bool friendp)
6335 /* Get rid of namespace aliases. */
6336 scope = ORIGINAL_NAMESPACE (scope);
6338 /* It is ok for friends to be qualified in parallel space. */
6339 if (!friendp && !is_nested_namespace (current_namespace, scope))
6340 error ("declaration of %qD not in a namespace surrounding %qD",
6341 decl, scope);
6342 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6344 /* See whether this has been declared in the namespace or inline
6345 children. */
6346 tree old = NULL_TREE;
6348 name_lookup lookup (DECL_NAME (decl),
6349 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
6350 if (!lookup.search_qualified (scope, /*usings=*/false))
6351 /* No old declaration at all. */
6352 goto not_found;
6353 old = lookup.value;
6356 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
6357 if (TREE_CODE (old) == TREE_LIST)
6359 ambiguous:
6360 auto_diagnostic_group d;
6361 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6362 error ("reference to %qD is ambiguous", decl);
6363 print_candidates (old);
6364 return;
6367 if (!DECL_DECLARES_FUNCTION_P (decl))
6369 /* Don't compare non-function decls with decls_match here, since
6370 it can't check for the correct constness at this
6371 point. pushdecl will find those errors later. */
6373 /* We might have found it in an inline namespace child of SCOPE. */
6374 if (TREE_CODE (decl) == TREE_CODE (old))
6375 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
6377 found:
6378 /* Writing "N::i" to declare something directly in "N" is invalid. */
6379 if (CP_DECL_CONTEXT (decl) == current_namespace
6380 && at_namespace_scope_p ())
6381 error_at (DECL_SOURCE_LOCATION (decl),
6382 "explicit qualification in declaration of %qD", decl);
6383 return;
6386 /* Since decl is a function, old should contain a function decl. */
6387 if (!OVL_P (old))
6389 not_found:
6390 /* It didn't work, go back to the explicit scope. */
6391 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6392 error ("%qD should have been declared inside %qD", decl, scope);
6394 return;
6397 /* We handle these in check_explicit_instantiation_namespace. */
6398 if (processing_explicit_instantiation)
6399 return;
6400 if (processing_template_decl || processing_specialization)
6401 /* We have not yet called push_template_decl to turn a
6402 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6403 match. But, we'll check later, when we construct the
6404 template. */
6405 return;
6407 /* Instantiations or specializations of templates may be declared as
6408 friends in any namespace. */
6409 if (friendp && DECL_USE_TEMPLATE (decl))
6410 return;
6412 tree found = NULL_TREE;
6413 bool hidden_p = false;
6414 bool saw_template = false;
6416 for (lkp_iterator iter (old); iter; ++iter)
6418 if (iter.using_p ())
6419 continue;
6421 tree ofn = *iter;
6423 /* Adjust DECL_CONTEXT first so decls_match will return true
6424 if DECL will match a declaration in an inline namespace. */
6425 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
6426 if (decls_match (decl, ofn))
6428 if (found)
6430 /* We found more than one matching declaration. This
6431 can happen if we have two inline namespace children,
6432 each containing a suitable declaration. */
6433 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6434 goto ambiguous;
6436 found = ofn;
6437 hidden_p = iter.hidden_p ();
6439 else if (TREE_CODE (decl) == FUNCTION_DECL
6440 && TREE_CODE (ofn) == TEMPLATE_DECL)
6441 saw_template = true;
6444 if (!found && friendp && saw_template)
6446 /* "[if no non-template match is found,] each remaining function template
6447 is replaced with the specialization chosen by deduction from the
6448 friend declaration or discarded if deduction fails."
6450 So tell check_explicit_specialization to look for a match. */
6451 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6452 DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
6453 return;
6456 if (found)
6458 if (hidden_p)
6460 auto_diagnostic_group d;
6461 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
6462 "%qD has not been declared within %qD", decl, scope);
6463 inform (DECL_SOURCE_LOCATION (found),
6464 "only here as a %<friend%>");
6466 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
6467 goto found;
6470 goto not_found;
6473 /* Return the namespace where the current declaration is declared. */
6475 tree
6476 current_decl_namespace (void)
6478 tree result;
6479 /* If we have been pushed into a different namespace, use it. */
6480 if (!vec_safe_is_empty (decl_namespace_list))
6481 return decl_namespace_list->last ();
6483 if (current_class_type)
6484 result = decl_namespace_context (current_class_type);
6485 else if (current_function_decl)
6486 result = decl_namespace_context (current_function_decl);
6487 else
6488 result = current_namespace;
6489 return result;
6492 /* Process any ATTRIBUTES on a namespace definition. Returns true if
6493 attribute visibility is seen. */
6495 bool
6496 handle_namespace_attrs (tree ns, tree attributes)
6498 tree d;
6499 bool saw_vis = false;
6501 if (attributes == error_mark_node)
6502 return false;
6504 for (d = attributes; d; d = TREE_CHAIN (d))
6506 tree name = get_attribute_name (d);
6507 tree args = TREE_VALUE (d);
6509 if (is_attribute_p ("visibility", name))
6511 /* attribute visibility is a property of the syntactic block
6512 rather than the namespace as a whole, so we don't touch the
6513 NAMESPACE_DECL at all. */
6514 tree x = args ? TREE_VALUE (args) : NULL_TREE;
6515 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
6517 warning (OPT_Wattributes,
6518 "%qD attribute requires a single NTBS argument",
6519 name);
6520 continue;
6523 if (!TREE_PUBLIC (ns))
6524 warning (OPT_Wattributes,
6525 "%qD attribute is meaningless since members of the "
6526 "anonymous namespace get local symbols", name);
6528 push_visibility (TREE_STRING_POINTER (x), 1);
6529 saw_vis = true;
6531 else if (is_attribute_p ("abi_tag", name))
6533 if (!DECL_NAME (ns))
6535 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6536 "namespace", name);
6537 continue;
6539 if (!DECL_NAMESPACE_INLINE_P (ns))
6541 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
6542 "namespace", name);
6543 continue;
6545 if (!args)
6547 tree dn = DECL_NAME (ns);
6548 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
6549 IDENTIFIER_POINTER (dn));
6550 TREE_TYPE (args) = char_array_type_node;
6551 args = fix_string_type (args);
6552 args = build_tree_list (NULL_TREE, args);
6554 if (check_abi_tag_args (args, name))
6555 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6556 DECL_ATTRIBUTES (ns));
6558 else if (is_attribute_p ("deprecated", name))
6560 if (!DECL_NAME (ns))
6562 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6563 "namespace", name);
6564 continue;
6566 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6568 error ("deprecated message is not a string");
6569 continue;
6571 TREE_DEPRECATED (ns) = 1;
6572 if (args)
6573 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6574 DECL_ATTRIBUTES (ns));
6576 else if (!attribute_ignored_p (d))
6578 warning (OPT_Wattributes, "%qD attribute directive ignored",
6579 name);
6580 continue;
6584 return saw_vis;
6587 /* Temporarily set the namespace for the current declaration. */
6589 void
6590 push_decl_namespace (tree decl)
6592 if (TREE_CODE (decl) != NAMESPACE_DECL)
6593 decl = decl_namespace_context (decl);
6594 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
6597 /* [namespace.memdef]/2 */
6599 void
6600 pop_decl_namespace (void)
6602 decl_namespace_list->pop ();
6605 /* Process a namespace-alias declaration. */
6607 void
6608 do_namespace_alias (tree alias, tree name_space)
6610 if (name_space == error_mark_node)
6611 return;
6613 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
6615 name_space = ORIGINAL_NAMESPACE (name_space);
6617 /* Build the alias. */
6618 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
6619 DECL_NAMESPACE_ALIAS (alias) = name_space;
6620 DECL_EXTERNAL (alias) = 1;
6621 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
6622 TREE_PUBLIC (alias) = TREE_PUBLIC (DECL_CONTEXT (alias));
6623 set_originating_module (alias);
6625 pushdecl (alias);
6627 /* Emit debug info for namespace alias. */
6628 if (!building_stmt_list_p ())
6629 (*debug_hooks->early_global_decl) (alias);
6632 /* Like pushdecl, only it places DECL in the current namespace,
6633 if appropriate. */
6635 tree
6636 pushdecl_namespace_level (tree decl, bool hiding)
6638 auto_cond_timevar tv (TV_NAME_LOOKUP);
6639 return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
6640 hiding);
6643 /* Wrapper around push_local_binding to push the bindings for
6644 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6645 is the result of name lookup during template parsing. */
6647 static void
6648 push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
6650 tree type = NULL_TREE;
6652 cxx_binding *binding = find_local_binding (current_binding_level, name);
6653 if (binding)
6655 value = binding->value;
6656 type = binding->type;
6659 if (lookup)
6660 do_nonmember_using_decl (*lookup, true, true, &value, &type);
6662 if (!value)
6664 else if (binding && value == binding->value)
6665 /* Redeclaration of this USING_DECL. */;
6666 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
6668 /* We already have this binding, so replace it. */
6669 update_local_overload (IDENTIFIER_BINDING (name), value);
6670 IDENTIFIER_BINDING (name)->value = value;
6672 else
6673 /* Install the new binding. */
6674 push_local_binding (name, value, /*using=*/true);
6676 if (!type)
6678 else if (binding && type == binding->type)
6680 else
6682 push_local_binding (name, type, /*using=*/true);
6683 set_identifier_type_value (name, type);
6687 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6689 void
6690 push_using_decl_bindings (tree name, tree value)
6692 push_using_decl_bindings (nullptr, name, value);
6695 /* Process a using declaration in non-class scope. */
6697 void
6698 finish_nonmember_using_decl (tree scope, tree name)
6700 gcc_checking_assert (current_binding_level->kind != sk_class);
6702 if (scope == error_mark_node || name == error_mark_node)
6703 return;
6705 name_lookup lookup (name);
6707 tree using_decl = lookup_using_decl (scope, lookup);
6708 if (!using_decl)
6709 return;
6711 /* Emit debug info. */
6712 if (!processing_template_decl)
6713 cp_emit_debug_info_for_using (lookup.value,
6714 current_binding_level->this_entity);
6716 if (current_binding_level->kind == sk_namespace)
6718 tree *slot = find_namespace_slot (current_namespace, name, true);
6719 tree *mslot = get_fixed_binding_slot (slot, name,
6720 BINDING_SLOT_CURRENT, true);
6721 bool failed = false;
6723 if (mslot != slot)
6725 /* A module vector. I presume the binding list is going to
6726 be sparser than the import bitmap. Hence iterate over
6727 the former checking for bits set in the bitmap. */
6728 bitmap imports = get_import_bitmap ();
6729 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
6731 /* Scan the imported bindings. */
6732 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
6733 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
6735 ix--;
6736 cluster++;
6739 /* Do this in forward order, so we load modules in an order
6740 the user expects. */
6741 for (; ix--; cluster++)
6742 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
6744 /* Are we importing this module? */
6745 if (unsigned base = cluster->indices[jx].base)
6746 if (unsigned span = cluster->indices[jx].span)
6748 if (bitmap_bit_p (imports, base))
6749 goto found;
6750 while (++base, --span);
6751 continue;
6753 found:;
6754 /* Is it loaded? */
6755 if (cluster->slots[jx].is_lazy ())
6757 gcc_assert (cluster->indices[jx].span == 1);
6758 lazy_load_binding (cluster->indices[jx].base,
6759 scope, name, &cluster->slots[jx]);
6762 tree value = cluster->slots[jx];
6763 if (!value)
6764 /* Load errors could mean there's nothing here. */
6765 continue;
6767 /* Extract what we can see from here. If there's no
6768 stat_hack, then everything was exported. */
6769 tree type = NULL_TREE;
6771 /* If no stat hack, everything is visible. */
6772 if (STAT_HACK_P (value))
6774 if (STAT_TYPE_VISIBLE_P (value))
6775 type = STAT_TYPE (value);
6776 value = STAT_VISIBLE (value);
6779 if (do_nonmember_using_decl (lookup, false, false,
6780 &value, &type))
6782 failed = true;
6783 break;
6788 if (!failed)
6790 /* Now do the current slot. */
6791 tree value = MAYBE_STAT_DECL (*mslot);
6792 tree type = MAYBE_STAT_TYPE (*mslot);
6794 do_nonmember_using_decl (lookup, false, true, &value, &type);
6796 // FIXME: Partition mergeableness?
6797 if (STAT_HACK_P (*mslot))
6799 STAT_DECL (*mslot) = value;
6800 STAT_TYPE (*mslot) = type;
6802 else if (type)
6803 *mslot = stat_hack (value, type);
6804 else
6805 *mslot = value;
6808 else
6810 add_decl_expr (using_decl);
6811 if (DECL_DEPENDENT_P (using_decl))
6812 lookup.value = using_decl;
6813 push_using_decl_bindings (&lookup, name, NULL_TREE);
6817 /* Return the declarations that are members of the namespace NS. */
6819 tree
6820 cp_namespace_decls (tree ns)
6822 return NAMESPACE_LEVEL (ns)->names;
6825 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6826 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6828 static bool
6829 qualify_lookup (tree val, LOOK_want want)
6831 if (val == NULL_TREE)
6832 return false;
6834 if (bool (want & LOOK_want::TYPE))
6836 tree target_val = strip_using_decl (val);
6838 if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
6839 return true;
6842 if (bool (want & LOOK_want::TYPE_NAMESPACE))
6843 return TREE_CODE (val) == NAMESPACE_DECL;
6845 return true;
6848 /* Is there a "using namespace std;" directive within USINGS? */
6850 static bool
6851 using_directives_contain_std_p (vec<tree, va_gc> *usings)
6853 if (!usings)
6854 return false;
6856 for (unsigned ix = usings->length (); ix--;)
6857 if ((*usings)[ix] == std_node)
6858 return true;
6860 return false;
6863 /* Is there a "using namespace std;" directive within the current
6864 namespace (or its ancestors)?
6865 Compare with name_lookup::search_unqualified. */
6867 static bool
6868 has_using_namespace_std_directive_p ()
6870 for (cp_binding_level *level = current_binding_level;
6871 level;
6872 level = level->level_chain)
6873 if (using_directives_contain_std_p (level->using_directives))
6874 return true;
6876 return false;
6879 /* Subclass of deferred_diagnostic, for issuing a note when
6880 --param cxx-max-namespaces-for-diagnostic-help is reached.
6882 The note should be issued after the error, but before any other
6883 deferred diagnostics. This is handled by decorating a wrapped
6884 deferred_diagnostic, and emitting a note before that wrapped note is
6885 deleted. */
6887 class namespace_limit_reached : public deferred_diagnostic
6889 public:
6890 namespace_limit_reached (location_t loc, unsigned limit, tree name,
6891 std::unique_ptr<deferred_diagnostic> wrapped)
6892 : deferred_diagnostic (loc),
6893 m_limit (limit), m_name (name),
6894 m_wrapped (std::move (wrapped))
6898 ~namespace_limit_reached ()
6900 /* Unconditionally warn that the search was truncated. */
6901 inform (get_location (),
6902 "maximum limit of %d namespaces searched for %qE",
6903 m_limit, m_name);
6904 /* m_wrapped will be implicitly deleted after this, emitting any followup
6905 diagnostic after the above note. */
6908 private:
6909 unsigned m_limit;
6910 tree m_name;
6911 std::unique_ptr<deferred_diagnostic> m_wrapped;
6914 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6915 Emit a note showing the location of the declaration of the suggestion. */
6917 class show_candidate_location : public deferred_diagnostic
6919 public:
6920 show_candidate_location (location_t loc, tree candidate)
6921 : deferred_diagnostic (loc),
6922 m_candidate (candidate)
6926 ~show_candidate_location ()
6928 inform (location_of (m_candidate), "%qE declared here", m_candidate);
6931 private:
6932 tree m_candidate;
6935 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6936 to be suggested by suggest_alternatives_for.
6938 Emit a series of notes showing the various suggestions. */
6940 class suggest_alternatives : public deferred_diagnostic
6942 public:
6943 suggest_alternatives (location_t loc, vec<tree> candidates)
6944 : deferred_diagnostic (loc),
6945 m_candidates (candidates)
6949 ~suggest_alternatives ()
6951 if (m_candidates.length ())
6953 inform_n (get_location (), m_candidates.length (),
6954 "suggested alternative:",
6955 "suggested alternatives:");
6956 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
6958 tree val = m_candidates[ix];
6960 inform (location_of (val), " %qE", val);
6963 m_candidates.release ();
6966 private:
6967 vec<tree> m_candidates;
6970 /* A class for encapsulating the result of a search across
6971 multiple namespaces (and scoped enums within them) for an
6972 unrecognized name seen at a given source location. */
6974 class namespace_hints
6976 public:
6977 namespace_hints (location_t loc, tree name);
6979 name_hint convert_candidates_to_name_hint ();
6980 name_hint maybe_decorate_with_limit (name_hint);
6982 private:
6983 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
6985 location_t m_loc;
6986 tree m_name;
6987 vec<tree> m_candidates;
6989 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6990 unsigned m_limit;
6992 /* Was the limit reached? */
6993 bool m_limited;
6996 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6997 looking for an exact match for unrecognized NAME seen at LOC. */
6999 namespace_hints::namespace_hints (location_t loc, tree name)
7000 : m_loc(loc), m_name (name)
7002 auto_vec<tree> worklist;
7004 m_candidates = vNULL;
7005 m_limited = false;
7006 m_limit = param_cxx_max_namespaces_for_diagnostic_help;
7008 /* Breadth-first search of namespaces. Up to limit namespaces
7009 searched (limit zero == unlimited). */
7010 worklist.safe_push (global_namespace);
7011 for (unsigned ix = 0; ix != worklist.length (); ix++)
7013 tree ns = worklist[ix];
7014 name_lookup lookup (name);
7016 if (lookup.search_qualified (ns, false))
7017 m_candidates.safe_push (lookup.value);
7019 if (!m_limited)
7021 /* Look for child namespaces. We have to do this
7022 indirectly because they are chained in reverse order,
7023 which is confusing to the user. */
7024 auto_vec<tree> children;
7026 for (tree decl = NAMESPACE_LEVEL (ns)->names;
7027 decl; decl = TREE_CHAIN (decl))
7029 if (TREE_CODE (decl) == NAMESPACE_DECL
7030 && !DECL_NAMESPACE_ALIAS (decl)
7031 && !DECL_NAMESPACE_INLINE_P (decl))
7032 children.safe_push (decl);
7034 /* Look for exact matches for NAME within scoped enums.
7035 These aren't added to the worklist, and so don't count
7036 against the search limit. */
7037 if (TREE_CODE (decl) == TYPE_DECL)
7039 tree type = TREE_TYPE (decl);
7040 if (SCOPED_ENUM_P (type))
7041 maybe_add_candidate_for_scoped_enum (type, name);
7045 while (!m_limited && !children.is_empty ())
7047 if (worklist.length () == m_limit)
7048 m_limited = true;
7049 else
7050 worklist.safe_push (children.pop ());
7056 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
7057 for m_name, an IDENTIFIER_NODE for which name lookup failed.
7059 If m_candidates is non-empty, use it to generate a suggestion and/or
7060 a deferred diagnostic that lists the possible candidate(s).
7063 name_hint
7064 namespace_hints::convert_candidates_to_name_hint ()
7066 /* How many candidates do we have? */
7068 /* If we have just one candidate, issue a name_hint with it as a suggestion
7069 (so that consumers are able to suggest it within the error message and emit
7070 it as a fix-it hint), and with a note showing the candidate's location. */
7071 if (m_candidates.length () == 1)
7073 tree candidate = m_candidates[0];
7074 /* Clean up CANDIDATES. */
7075 m_candidates.release ();
7076 return name_hint (expr_to_string (candidate),
7077 new show_candidate_location (m_loc, candidate));
7079 else if (m_candidates.length () > 1)
7080 /* If we have more than one candidate, issue a name_hint without a single
7081 "suggestion", but with a deferred diagnostic that lists the
7082 various candidates. This takes ownership of m_candidates. */
7083 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
7085 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
7086 gcc_assert (m_candidates.length () == 0);
7087 gcc_assert (m_candidates == vNULL);
7089 return name_hint ();
7092 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
7093 then we want to emit a note about after the error, but before
7094 any other deferred diagnostics.
7096 Handle this by figuring out what hint is needed, then optionally
7097 decorating HINT with a namespace_limit_reached wrapper. */
7099 name_hint
7100 namespace_hints::maybe_decorate_with_limit (name_hint hint)
7102 if (m_limited)
7103 return name_hint (hint.suggestion (),
7104 new namespace_limit_reached (m_loc, m_limit,
7105 m_name,
7106 hint.take_deferred ()));
7107 else
7108 return hint;
7111 /* Look inside SCOPED_ENUM for exact matches for NAME.
7112 If one is found, add its CONST_DECL to m_candidates. */
7114 void
7115 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
7116 tree name)
7118 gcc_assert (SCOPED_ENUM_P (scoped_enum));
7120 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7122 tree id = TREE_PURPOSE (iter);
7123 if (id == name)
7125 m_candidates.safe_push (TREE_VALUE (iter));
7126 return;
7131 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7132 name lookup failed.
7134 Search through all available namespaces and any scoped enums within them
7135 and generate a suggestion and/or a deferred diagnostic that lists possible
7136 candidate(s).
7138 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
7139 look for near-matches and suggest the best near-match, if there is one.
7141 If nothing is found, then an empty name_hint is returned. */
7143 name_hint
7144 suggest_alternatives_for (location_t location, tree name,
7145 bool suggest_misspellings)
7147 /* First, search for exact matches in other namespaces. */
7148 namespace_hints ns_hints (location, name);
7149 name_hint result = ns_hints.convert_candidates_to_name_hint ();
7151 /* Otherwise, try other approaches. */
7152 if (!result)
7153 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
7155 return ns_hints.maybe_decorate_with_limit (std::move (result));
7158 /* The second half of suggest_alternatives_for, for when no exact matches
7159 were found in other namespaces. */
7161 static name_hint
7162 suggest_alternatives_for_1 (location_t location, tree name,
7163 bool suggest_misspellings)
7165 /* No candidates were found in the available namespaces. */
7167 /* If there's a "using namespace std;" active, and this
7168 is one of the most common "std::" names, then it's probably a
7169 missing #include. */
7170 if (has_using_namespace_std_directive_p ())
7172 name_hint hint = maybe_suggest_missing_std_header (location, name);
7173 if (hint)
7174 return hint;
7177 /* Otherwise, consider misspellings. */
7178 if (!suggest_misspellings)
7179 return name_hint ();
7181 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
7184 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7185 name lookup failed.
7187 Search through all available namespaces and generate a suggestion and/or
7188 a deferred diagnostic that lists possible candidate(s).
7190 This is similiar to suggest_alternatives_for, but doesn't fallback to
7191 the other approaches used by that function. */
7193 name_hint
7194 suggest_alternatives_in_other_namespaces (location_t location, tree name)
7196 namespace_hints ns_hints (location, name);
7198 name_hint result = ns_hints.convert_candidates_to_name_hint ();
7200 return ns_hints.maybe_decorate_with_limit (std::move (result));
7203 /* A well-known name within the C++ standard library, returned by
7204 get_std_name_hint.
7206 The gperf-generated file contains the definition of the class
7207 "std_name_hint_lookup" with a static member function which
7208 returns the pointer to a structure "std_name_hint" which
7209 is also defined in that file. */
7211 #include "std-name-hint.h"
7213 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
7214 for some of the most common names within "std::".
7215 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
7217 static const std_name_hint *
7218 get_std_name_hint (const char *name)
7220 return std_name_hint_lookup::find(name, strlen(name));
7223 /* Describe DIALECT. */
7225 const char *
7226 get_cxx_dialect_name (enum cxx_dialect dialect)
7228 switch (dialect)
7230 default:
7231 gcc_unreachable ();
7232 case cxx98:
7233 return "C++98";
7234 case cxx11:
7235 return "C++11";
7236 case cxx14:
7237 return "C++14";
7238 case cxx17:
7239 return "C++17";
7240 case cxx20:
7241 return "C++20";
7242 case cxx23:
7243 return "C++23";
7244 case cxx26:
7245 return "C++26";
7249 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7250 that weren't recognized, but for which we know which header it ought to be
7253 Emit a note either suggesting the header to be included, or noting that
7254 the current dialect is too early for the given name. */
7256 class missing_std_header : public deferred_diagnostic
7258 public:
7259 missing_std_header (location_t loc,
7260 const char *name_str,
7261 const std_name_hint *header_hint)
7262 : deferred_diagnostic (loc),
7263 m_name_str (name_str),
7264 m_header_hint (header_hint)
7266 ~missing_std_header ()
7268 gcc_rich_location richloc (get_location ());
7269 if (cxx_dialect >= m_header_hint->min_dialect)
7271 const char *header = m_header_hint->header;
7272 maybe_add_include_fixit (&richloc, header, true);
7273 inform (&richloc,
7274 "%<std::%s%> is defined in header %qs;"
7275 " this is probably fixable by adding %<#include %s%>",
7276 m_name_str, header, header);
7278 else
7279 inform (&richloc,
7280 "%<std::%s%> is only available from %s onwards",
7281 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
7284 private:
7285 const char *m_name_str;
7286 const std_name_hint *m_header_hint;
7289 /* Attempt to generate a name_hint that suggests pertinent header files
7290 for NAME at LOCATION, for common names within the "std" namespace,
7291 or an empty name_hint if this isn't applicable. */
7293 static name_hint
7294 maybe_suggest_missing_std_header (location_t location, tree name)
7296 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7298 const char *name_str = IDENTIFIER_POINTER (name);
7299 const std_name_hint *header_hint = get_std_name_hint (name_str);
7300 if (!header_hint)
7301 return name_hint ();
7303 return name_hint (NULL, new missing_std_header (location, name_str,
7304 header_hint));
7307 /* Attempt to generate a name_hint that suggests a missing header file
7308 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7309 applicable. */
7311 name_hint
7312 maybe_suggest_missing_header (location_t location, tree name, tree scope)
7314 if (scope == NULL_TREE)
7315 return name_hint ();
7316 if (TREE_CODE (scope) != NAMESPACE_DECL)
7317 return name_hint ();
7318 /* We only offer suggestions for the "std" namespace. */
7319 if (scope != std_node)
7320 return name_hint ();
7321 return maybe_suggest_missing_std_header (location, name);
7324 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7325 lookup failed within the explicitly provided SCOPE.
7327 Suggest the best meaningful candidates (if any), otherwise
7328 an empty name_hint is returned. */
7330 name_hint
7331 suggest_alternative_in_explicit_scope (location_t location, tree name,
7332 tree scope)
7334 /* Something went very wrong; don't suggest anything. */
7335 if (name == error_mark_node)
7336 return name_hint ();
7338 /* Resolve any namespace aliases. */
7339 scope = ORIGINAL_NAMESPACE (scope);
7341 name_hint hint = maybe_suggest_missing_header (location, name, scope);
7342 if (hint)
7343 return hint;
7345 cp_binding_level *level = NAMESPACE_LEVEL (scope);
7347 best_match <tree, const char *> bm (name);
7348 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
7350 /* See if we have a good suggesion for the user. */
7351 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
7352 if (fuzzy_name)
7353 return name_hint (fuzzy_name, NULL);
7355 return name_hint ();
7358 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7359 candidates. */
7361 name_hint
7362 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
7364 gcc_assert (SCOPED_ENUM_P (scoped_enum));
7366 best_match <tree, const char *> bm (name);
7367 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7369 tree id = TREE_PURPOSE (iter);
7370 bm.consider (IDENTIFIER_POINTER (id));
7372 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7375 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7376 or a class TYPE).
7378 WANT as for lookup_name_1.
7380 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7381 declaration found. If no suitable declaration can be found,
7382 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7383 neither a class-type nor a namespace a diagnostic is issued. */
7385 tree
7386 lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
7388 tree t = NULL_TREE;
7390 if (TREE_CODE (scope) == NAMESPACE_DECL)
7392 name_lookup lookup (name, want);
7394 if (qualified_namespace_lookup (scope, &lookup))
7396 t = lookup.value;
7398 /* If we have a known type overload, pull it out. This can happen
7399 for using decls. */
7400 if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
7401 t = OVL_FUNCTION (t);
7404 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
7405 t = lookup_enumerator (scope, name);
7406 else if (is_class_type (scope, complain))
7407 t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
7408 tf_warning_or_error);
7410 if (!t)
7411 return error_mark_node;
7412 return t;
7415 /* Wrapper for the above that takes a string argument. The function name is
7416 not at the beginning of the line to keep this wrapper out of etags. */
7418 tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
7420 return lookup_qualified_name (t, get_identifier (p), w, c);
7423 /* [namespace.qual]
7424 Accepts the NAME to lookup and its qualifying SCOPE.
7425 Returns the name/type pair found into the cxx_binding *RESULT,
7426 or false on error. */
7428 static bool
7429 qualified_namespace_lookup (tree scope, name_lookup *lookup)
7431 auto_cond_timevar tv (TV_NAME_LOOKUP);
7432 query_oracle (lookup->name);
7433 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
7434 return found;
7437 /* If DECL is suitably visible to the user, consider its name for
7438 spelling correction. */
7440 static void
7441 consider_decl (tree decl, best_match <tree, const char *> &bm,
7442 bool consider_impl_names)
7444 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7445 within range for). */
7446 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7447 return;
7449 tree suggestion = DECL_NAME (decl);
7450 if (!suggestion)
7451 return;
7453 /* Don't suggest names that are for anonymous aggregate types, as
7454 they are an implementation detail generated by the compiler. */
7455 if (IDENTIFIER_ANON_P (suggestion))
7456 return;
7458 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
7460 /* Ignore internal names with spaces in them. */
7461 if (strchr (suggestion_str, ' '))
7462 return;
7464 /* Don't suggest names that are reserved for use by the
7465 implementation, unless NAME began with an underscore. */
7466 if (!consider_impl_names
7467 && name_reserved_for_implementation_p (suggestion_str))
7468 return;
7470 bm.consider (suggestion_str);
7473 /* If DECL is suitably visible to the user, add its name to VEC and
7474 return true. Otherwise return false. */
7476 static bool
7477 maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
7479 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7480 within range for). */
7481 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7482 return false;
7484 tree suggestion = DECL_NAME (decl);
7485 if (!suggestion)
7486 return false;
7488 /* Don't suggest names that are for anonymous aggregate types, as
7489 they are an implementation detail generated by the compiler. */
7490 if (IDENTIFIER_ANON_P (suggestion))
7491 return false;
7493 vec.safe_push (suggestion);
7495 return true;
7498 /* Examing the namespace binding BINDING, and add at most one instance
7499 of the name, if it contains a visible entity of interest. Return
7500 true if we added something. */
7502 bool
7503 maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
7504 lookup_name_fuzzy_kind kind)
7506 tree value = NULL_TREE;
7508 if (STAT_HACK_P (binding))
7510 if (!STAT_TYPE_HIDDEN_P (binding)
7511 && STAT_TYPE (binding))
7513 if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
7514 return true;
7516 else if (!STAT_DECL_HIDDEN_P (binding))
7517 value = STAT_DECL (binding);
7519 else
7520 value = binding;
7522 value = ovl_skip_hidden (value);
7523 if (value)
7525 value = OVL_FIRST (value);
7526 if (kind != FUZZY_LOOKUP_TYPENAME
7527 || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
7528 if (maybe_add_fuzzy_decl (vec, value))
7529 return true;
7532 /* Nothing found. */
7533 return false;
7536 /* Helper function for lookup_name_fuzzy.
7537 Traverse binding level LVL, looking for good name matches for NAME
7538 (and BM). */
7539 static void
7540 consider_binding_level (tree name, best_match <tree, const char *> &bm,
7541 cp_binding_level *lvl, bool look_within_fields,
7542 enum lookup_name_fuzzy_kind kind)
7544 if (look_within_fields)
7545 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
7547 tree type = lvl->this_entity;
7548 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
7549 tree best_matching_field
7550 = lookup_member_fuzzy (type, name, want_type_p);
7551 if (best_matching_field)
7552 bm.consider (IDENTIFIER_POINTER (best_matching_field));
7555 /* Only suggest names reserved for the implementation if NAME begins
7556 with an underscore. */
7557 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
7559 if (lvl->kind != sk_namespace)
7560 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
7562 tree d = t;
7564 /* OVERLOADs or decls from using declaration are wrapped into
7565 TREE_LIST. */
7566 if (TREE_CODE (d) == TREE_LIST)
7567 d = OVL_FIRST (TREE_VALUE (d));
7569 /* Don't use bindings from implicitly declared functions,
7570 as they were likely misspellings themselves. */
7571 if (TREE_TYPE (d) == error_mark_node)
7572 continue;
7574 /* If we want a typename, ignore non-types. */
7575 if (kind == FUZZY_LOOKUP_TYPENAME
7576 && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
7577 continue;
7579 consider_decl (d, bm, consider_implementation_names);
7581 else
7583 /* We need to iterate over the namespace hash table, in order to
7584 not mention hidden entities. But hash table iteration is
7585 (essentially) unpredictable, our correction-distance measure
7586 is very granular, and we pick the first of equal distances.
7587 Hence, we need to call the distance-measurer in a predictable
7588 order. So, iterate over the namespace hash, inserting
7589 visible names into a vector. Then sort the vector. Then
7590 determine spelling distance. */
7592 tree ns = lvl->this_entity;
7593 auto_vec<tree> vec;
7595 hash_table<named_decl_hash>::iterator end
7596 (DECL_NAMESPACE_BINDINGS (ns)->end ());
7597 for (hash_table<named_decl_hash>::iterator iter
7598 (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
7600 tree binding = *iter;
7602 if (TREE_CODE (binding) == BINDING_VECTOR)
7604 bitmap imports = get_import_bitmap ();
7605 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
7607 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
7608 if (maybe_add_fuzzy_binding (vec, bind, kind))
7609 continue;
7611 /* Scan the imported bindings. */
7612 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
7613 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7615 ix--;
7616 cluster++;
7619 for (; ix--; cluster++)
7620 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
7621 jx++)
7623 /* Are we importing this module? */
7624 if (unsigned base = cluster->indices[jx].base)
7625 if (unsigned span = cluster->indices[jx].span)
7627 if (bitmap_bit_p (imports, base))
7628 goto found;
7629 while (++base, --span);
7630 continue;
7632 found:;
7633 /* Is it loaded? */
7634 if (cluster->slots[jx].is_lazy ())
7635 /* Let's not read in everything on the first
7636 spello! **/
7637 continue;
7638 if (tree bind = cluster->slots[jx])
7639 if (maybe_add_fuzzy_binding (vec, bind, kind))
7640 break;
7643 else
7644 maybe_add_fuzzy_binding (vec, binding, kind);
7647 vec.qsort ([] (const void *a_, const void *b_)
7649 return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
7650 IDENTIFIER_POINTER (*(const tree *)b_));
7653 /* Examine longest to shortest. */
7654 for (unsigned ix = vec.length (); ix--;)
7656 const char *str = IDENTIFIER_POINTER (vec[ix]);
7658 /* Ignore internal names with spaces in them. */
7659 if (strchr (str, ' '))
7660 continue;
7662 /* Don't suggest names that are reserved for use by the
7663 implementation, unless NAME began with an underscore. */
7664 if (!consider_implementation_names
7665 && name_reserved_for_implementation_p (str))
7666 continue;
7668 bm.consider (str);
7673 /* Subclass of deferred_diagnostic. Notify the user that the
7674 given macro was used before it was defined.
7675 This can be done in the C++ frontend since tokenization happens
7676 upfront. */
7678 class macro_use_before_def : public deferred_diagnostic
7680 public:
7681 /* Factory function. Return a new macro_use_before_def instance if
7682 appropriate, or return NULL. */
7683 static macro_use_before_def *
7684 maybe_make (location_t use_loc, cpp_hashnode *macro)
7686 location_t def_loc = cpp_macro_definition_location (macro);
7687 if (def_loc == UNKNOWN_LOCATION)
7688 return NULL;
7690 /* We only want to issue a note if the macro was used *before* it was
7691 defined.
7692 We don't want to issue a note for cases where a macro was incorrectly
7693 used, leaving it unexpanded (e.g. by using the wrong argument
7694 count). */
7695 if (!linemap_location_before_p (line_table, use_loc, def_loc))
7696 return NULL;
7698 return new macro_use_before_def (use_loc, macro);
7701 private:
7702 /* Ctor. LOC is the location of the usage. MACRO is the
7703 macro that was used. */
7704 macro_use_before_def (location_t loc, cpp_hashnode *macro)
7705 : deferred_diagnostic (loc), m_macro (macro)
7707 gcc_assert (macro);
7710 ~macro_use_before_def ()
7712 if (is_suppressed_p ())
7713 return;
7715 inform (get_location (), "the macro %qs had not yet been defined",
7716 (const char *)m_macro->ident.str);
7717 inform (cpp_macro_definition_location (m_macro),
7718 "it was later defined here");
7721 private:
7722 cpp_hashnode *m_macro;
7725 /* Determine if it can ever make sense to offer RID as a suggestion for
7726 a misspelling.
7728 Subroutine of lookup_name_fuzzy. */
7730 static bool
7731 suggest_rid_p (enum rid rid)
7733 switch (rid)
7735 /* Support suggesting function-like keywords. */
7736 case RID_STATIC_ASSERT:
7737 return true;
7739 default:
7740 /* Support suggesting the various decl-specifier words, to handle
7741 e.g. "singed" vs "signed" typos. */
7742 if (cp_keyword_starts_decl_specifier_p (rid))
7743 return true;
7745 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7746 and "do" for short misspellings, which are likely to lead to
7747 nonsensical results. */
7748 return false;
7752 /* Search for near-matches for NAME within the current bindings, and within
7753 macro names, returning the best match as a const char *, or NULL if
7754 no reasonable match is found.
7756 Use LOC for any deferred diagnostics. */
7758 name_hint
7759 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
7761 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7763 /* First, try some well-known names in the C++ standard library, in case
7764 the user forgot a #include. */
7765 const char *header_hint
7766 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
7767 if (header_hint)
7768 return name_hint (NULL,
7769 new suggest_missing_header (loc,
7770 IDENTIFIER_POINTER (name),
7771 header_hint));
7773 best_match <tree, const char *> bm (name);
7775 cp_binding_level *lvl;
7776 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
7777 consider_binding_level (name, bm, lvl, true, kind);
7779 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
7780 consider_binding_level (name, bm, lvl, false, kind);
7782 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7784 x = SOME_OTHER_MACRO (y);
7785 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7786 as a misspelled identifier.
7788 Use the best distance so far so that a candidate is only set if
7789 a macro is better than anything so far. This allows early rejection
7790 (without calculating the edit distance) of macro names that must have
7791 distance >= bm.get_best_distance (), and means that we only get a
7792 non-NULL result for best_macro_match if it's better than any of
7793 the identifiers already checked. */
7794 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
7795 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
7796 /* If a macro is the closest so far to NAME, consider it. */
7797 if (best_macro)
7798 bm.consider ((const char *)best_macro->ident.str);
7799 else if (bmm.get_best_distance () == 0)
7801 /* If we have an exact match for a macro name, then either the
7802 macro was used with the wrong argument count, or the macro
7803 has been used before it was defined. */
7804 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
7805 if (cpp_user_macro_p (macro))
7806 return name_hint (NULL,
7807 macro_use_before_def::maybe_make (loc, macro));
7810 /* Try the "starts_decl_specifier_p" keywords to detect
7811 "singed" vs "signed" typos. */
7812 for (unsigned i = 0; i < num_c_common_reswords; i++)
7814 const c_common_resword *resword = &c_common_reswords[i];
7816 if (!suggest_rid_p (resword->rid))
7817 continue;
7819 tree resword_identifier = ridpointers [resword->rid];
7820 if (!resword_identifier)
7821 continue;
7822 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
7824 /* Only consider reserved words that survived the
7825 filtering in init_reswords (e.g. for -std). */
7826 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
7827 continue;
7829 bm.consider (IDENTIFIER_POINTER (resword_identifier));
7832 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7835 /* Subroutine of outer_binding.
7837 Returns TRUE if BINDING is a binding to a template parameter of
7838 SCOPE. In that case SCOPE is the scope of a primary template
7839 parameter -- in the sense of G++, i.e, a template that has its own
7840 template header.
7842 Returns FALSE otherwise. */
7844 static bool
7845 binding_to_template_parms_of_scope_p (cxx_binding *binding,
7846 cp_binding_level *scope)
7848 tree binding_value, tmpl, tinfo;
7849 int level;
7851 if (!binding || !scope || !scope->this_entity)
7852 return false;
7854 binding_value = binding->value ? binding->value : binding->type;
7855 tinfo = get_template_info (scope->this_entity);
7857 /* BINDING_VALUE must be a template parm. */
7858 if (binding_value == NULL_TREE
7859 || (!DECL_P (binding_value)
7860 || !DECL_TEMPLATE_PARM_P (binding_value)))
7861 return false;
7863 /* The level of BINDING_VALUE. */
7864 level =
7865 template_type_parameter_p (binding_value)
7866 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7867 (TREE_TYPE (binding_value)))
7868 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
7870 /* The template of the current scope, iff said scope is a primary
7871 template. */
7872 tmpl = (tinfo
7873 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
7874 ? TI_TEMPLATE (tinfo)
7875 : NULL_TREE);
7877 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7878 then BINDING_VALUE is a parameter of TMPL. */
7879 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
7882 /* Return the innermost non-namespace binding for NAME from a scope
7883 containing BINDING, or, if BINDING is NULL, the current scope.
7884 Please note that for a given template, the template parameters are
7885 considered to be in the scope containing the current scope.
7886 If CLASS_P is false, then class bindings are ignored. */
7888 cxx_binding *
7889 outer_binding (tree name,
7890 cxx_binding *binding,
7891 bool class_p)
7893 cxx_binding *outer;
7894 cp_binding_level *scope;
7895 cp_binding_level *outer_scope;
7897 if (binding)
7899 scope = binding->scope->level_chain;
7900 outer = binding->previous;
7902 else
7904 scope = current_binding_level;
7905 outer = IDENTIFIER_BINDING (name);
7907 outer_scope = outer ? outer->scope : NULL;
7909 /* Because we create class bindings lazily, we might be missing a
7910 class binding for NAME. If there are any class binding levels
7911 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7912 declared, we must lookup NAME in those class scopes. */
7913 if (class_p)
7914 while (scope && scope != outer_scope && scope->kind != sk_namespace)
7916 if (scope->kind == sk_class)
7918 cxx_binding *class_binding;
7920 class_binding = get_class_binding (name, scope);
7921 if (class_binding)
7923 /* Thread this new class-scope binding onto the
7924 IDENTIFIER_BINDING list so that future lookups
7925 find it quickly. */
7926 if (BASELINK_P (class_binding->value))
7927 /* Don't put a BASELINK in IDENTIFIER_BINDING. */
7928 class_binding->value
7929 = BASELINK_FUNCTIONS (class_binding->value);
7930 class_binding->previous = outer;
7931 if (binding)
7932 binding->previous = class_binding;
7933 else
7934 IDENTIFIER_BINDING (name) = class_binding;
7935 return class_binding;
7938 /* If we are in a member template, the template parms of the member
7939 template are considered to be inside the scope of the containing
7940 class, but within G++ the class bindings are all pushed between the
7941 template parms and the function body. So if the outer binding is
7942 a template parm for the current scope, return it now rather than
7943 look for a class binding. */
7944 if (outer_scope && outer_scope->kind == sk_template_parms
7945 && binding_to_template_parms_of_scope_p (outer, scope))
7946 return outer;
7948 scope = scope->level_chain;
7951 return outer;
7954 /* Return the innermost block-scope or class-scope value binding for
7955 NAME, or NULL_TREE if there is no such binding. */
7957 tree
7958 innermost_non_namespace_value (tree name)
7960 cxx_binding *binding;
7961 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
7962 return binding ? binding->value : NULL_TREE;
7965 /* True iff current_binding_level is within the potential scope of local
7966 variable DECL. */
7968 bool
7969 decl_in_scope_p (tree decl)
7971 gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
7973 tree name = DECL_NAME (decl);
7975 for (cxx_binding *iter = NULL;
7976 (iter = outer_binding (name, iter, /*class_p=*/false)); )
7978 if (!LOCAL_BINDING_P (iter))
7979 return false;
7980 if (iter->value == decl)
7981 return true;
7984 return false;
7987 /* Look up NAME in the current binding level and its superiors in the
7988 namespace of variables, functions and typedefs. Return a ..._DECL
7989 node of some kind representing its definition if there is only one
7990 such declaration, or return a TREE_LIST with all the overloaded
7991 definitions if there are many, or return NULL_TREE if it is undefined.
7992 Hidden name, either friend declaration or built-in function, are
7993 not ignored.
7995 WHERE controls which scopes are considered. It is a bit mask of
7996 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7997 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7998 scopes). It is an error for no bits to be set. These scopes are
7999 searched from innermost to outermost.
8001 WANT controls what kind of entity we'd happy with.
8002 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
8003 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
8004 for only NAMESPACE_DECLS. These two can be bit-ored to find
8005 namespace or type.
8007 WANT can also have LOOK_want::HIDDEN_FRIEND or
8008 LOOK_want::HIDDEN_LAMBDa added to it. */
8010 tree
8011 lookup_name (tree name, LOOK_where where, LOOK_want want)
8013 tree val = NULL_TREE;
8015 auto_cond_timevar tv (TV_NAME_LOOKUP);
8017 gcc_checking_assert (unsigned (where) != 0);
8018 /* If we're looking for hidden lambda things, we shouldn't be
8019 looking in namespace scope. */
8020 gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
8021 || !bool (where & LOOK_where::NAMESPACE));
8022 query_oracle (name);
8024 /* Conversion operators are handled specially because ordinary
8025 unqualified name lookup will not find template conversion
8026 operators. */
8027 if (IDENTIFIER_CONV_OP_P (name))
8029 cp_binding_level *level;
8031 for (level = current_binding_level;
8032 level && level->kind != sk_namespace;
8033 level = level->level_chain)
8035 tree class_type;
8036 tree operators;
8038 /* A conversion operator can only be declared in a class
8039 scope. */
8040 if (level->kind != sk_class)
8041 continue;
8043 /* Lookup the conversion operator in the class. */
8044 class_type = level->this_entity;
8045 operators = lookup_fnfields (class_type, name, /*protect=*/0,
8046 tf_warning_or_error);
8047 if (operators)
8048 return operators;
8051 return NULL_TREE;
8054 /* First, look in non-namespace scopes. */
8056 if (current_class_type == NULL_TREE)
8057 /* Maybe avoid searching the binding stack at all. */
8058 where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
8060 if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
8061 for (cxx_binding *iter = nullptr;
8062 (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
8064 /* Skip entities we don't want. */
8065 if (!bool (where & (LOCAL_BINDING_P (iter)
8066 ? LOOK_where::BLOCK : LOOK_where::CLASS)))
8067 continue;
8069 /* If this is the kind of thing we're looking for, we're done. */
8070 if (iter->value)
8072 tree binding = NULL_TREE;
8074 if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
8075 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
8076 || !is_lambda_ignored_entity (iter->value))
8077 && qualify_lookup (iter->value, want))
8078 binding = iter->value;
8079 else if (bool (want & LOOK_want::TYPE)
8080 && !HIDDEN_TYPE_BINDING_P (iter)
8081 && iter->type)
8082 binding = iter->type;
8084 if (binding)
8086 val = strip_using_decl (binding);
8087 break;
8092 /* Now lookup in namespace scopes. */
8093 if (!val && bool (where & LOOK_where::NAMESPACE))
8095 name_lookup lookup (name, want);
8096 if (lookup.search_unqualified
8097 (current_decl_namespace (), current_binding_level))
8098 val = lookup.value;
8101 /* If we have a known type overload, pull it out. This can happen
8102 for both using decls and unhidden functions. */
8103 if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
8104 val = OVL_FUNCTION (val);
8106 return val;
8109 tree
8110 lookup_name (tree name)
8112 return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
8115 /* Look up NAME for type used in elaborated name specifier in
8116 the scopes given by HOW.
8118 Unlike lookup_name_1, we make sure that NAME is actually
8119 declared in the desired scope, not from inheritance, nor using
8120 directive. For using declaration, there is DR138 still waiting
8121 to be resolved. Hidden name coming from an earlier friend
8122 declaration is also returned, and will be made visible unless HOW
8123 is TAG_how::HIDDEN_FRIEND.
8125 A TYPE_DECL best matching the NAME is returned. Catching error
8126 and issuing diagnostics are caller's responsibility. */
8128 tree
8129 lookup_elaborated_type (tree name, TAG_how how)
8131 auto_cond_timevar tv (TV_NAME_LOOKUP);
8133 cp_binding_level *b = current_binding_level;
8135 if (b->kind != sk_namespace)
8136 /* Look in non-namespace scopes. */
8137 for (cxx_binding *iter = NULL;
8138 (iter = outer_binding (name, iter, /*class_p=*/ true)); )
8140 /* First check we're supposed to be looking in this scope --
8141 if we're not, we're done. */
8142 for (; b != iter->scope; b = b->level_chain)
8143 if (!(b->kind == sk_cleanup
8144 || b->kind == sk_template_parms
8145 || b->kind == sk_function_parms
8146 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8147 return NULL_TREE;
8149 /* Check if this is the kind of thing we're looking for. If
8150 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
8151 come from base class. For ITER->VALUE, we can simply use
8152 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
8153 our own check.
8155 We check ITER->TYPE before ITER->VALUE in order to handle
8156 typedef struct C {} C;
8157 correctly. */
8159 if (tree type = strip_using_decl (iter->type))
8161 if (qualify_lookup (type, LOOK_want::TYPE)
8162 && (how != TAG_how::CURRENT_ONLY
8163 || LOCAL_BINDING_P (iter)
8164 || DECL_CONTEXT (type) == iter->scope->this_entity))
8166 if (how != TAG_how::HIDDEN_FRIEND)
8167 /* It is no longer a hidden binding. */
8168 HIDDEN_TYPE_BINDING_P (iter) = false;
8170 return type;
8173 else
8175 tree value = strip_using_decl (iter->value);
8176 if (qualify_lookup (value, LOOK_want::TYPE)
8177 && (how != TAG_how::CURRENT_ONLY
8178 || !INHERITED_VALUE_BINDING_P (iter)))
8180 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
8181 /* It is no longer a hidden binding. */
8182 HIDDEN_TYPE_BINDING_P (iter) = false;
8184 return value;
8189 /* Now check if we can look in namespace scope. */
8190 for (; b->kind != sk_namespace; b = b->level_chain)
8191 if (!(b->kind == sk_cleanup
8192 || b->kind == sk_template_parms
8193 || b->kind == sk_function_parms
8194 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8195 return NULL_TREE;
8197 /* Look in the innermost namespace. */
8198 tree ns = b->this_entity;
8199 if (tree *slot = find_namespace_slot (ns, name))
8201 tree bind = *slot;
8202 if (TREE_CODE (bind) == BINDING_VECTOR)
8203 bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
8205 if (bind)
8207 /* If this is the kind of thing we're looking for, we're done. */
8208 if (tree type = strip_using_decl (MAYBE_STAT_TYPE (bind)))
8210 if (how != TAG_how::HIDDEN_FRIEND)
8211 /* No longer hidden. */
8212 STAT_TYPE_HIDDEN_P (*slot) = false;
8214 return type;
8216 else if (tree decl = strip_using_decl (MAYBE_STAT_DECL (bind)))
8218 if (qualify_lookup (decl, LOOK_want::TYPE))
8220 if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
8221 && STAT_DECL_HIDDEN_P (bind))
8223 if (STAT_TYPE (bind))
8224 STAT_DECL_HIDDEN_P (bind) = false;
8225 else
8227 /* There is no type, just remove the stat
8228 hack. */
8229 if (*slot == bind)
8230 *slot = decl;
8231 else
8232 BINDING_VECTOR_CLUSTER (*slot, 0)
8233 .slots[BINDING_SLOT_CURRENT] = decl;
8236 return decl;
8241 if (TREE_CODE (*slot) == BINDING_VECTOR)
8243 /* We could be redeclaring a global module entity, (from GMF
8244 or header unit), or from another partition, or
8245 specializing an imported template. */
8246 bitmap imports = get_import_bitmap ();
8247 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
8249 /* Scan the imported bindings. */
8250 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
8251 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
8253 ix--;
8254 cluster++;
8257 /* Do this in forward order, so we load modules in an order
8258 the user expects. */
8259 for (; ix--; cluster++)
8260 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
8262 /* Are we importing this module? */
8263 if (unsigned base = cluster->indices[jx].base)
8264 if (unsigned span = cluster->indices[jx].span)
8266 if (bitmap_bit_p (imports, base))
8267 goto found;
8268 while (++base, --span);
8269 continue;
8271 found:;
8272 /* Is it loaded? */
8273 if (cluster->slots[jx].is_lazy ())
8275 gcc_assert (cluster->indices[jx].span == 1);
8276 lazy_load_binding (cluster->indices[jx].base,
8277 ns, name, &cluster->slots[jx]);
8279 tree bind = cluster->slots[jx];
8280 if (!bind)
8281 /* Load errors could mean there's nothing here. */
8282 continue;
8284 /* Extract what we can see from here. If there's no
8285 stat_hack, then everything was exported. */
8286 tree type = NULL_TREE;
8288 /* If no stat hack, everything is visible. */
8289 if (STAT_HACK_P (bind))
8291 if (STAT_TYPE_VISIBLE_P (bind))
8292 type = STAT_TYPE (bind);
8293 bind = STAT_VISIBLE (bind);
8296 if (type && qualify_lookup (type, LOOK_want::TYPE))
8297 return strip_using_decl (type);
8299 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
8300 return strip_using_decl (bind);
8303 if (!module_purview_p ())
8305 /* We're in the global module, perhaps there's a tag
8306 there? */
8308 /* FIXME: In general we should probably merge global module
8309 classes in check_module_override rather than here, but for
8310 GCC14 let's just fix lazy declarations of __class_type_info in
8311 build_dynamic_cast_1. */
8312 if (current_namespace == abi_node)
8314 tree g = (BINDING_VECTOR_CLUSTER (*slot, 0)
8315 .slots[BINDING_SLOT_GLOBAL]);
8316 for (ovl_iterator iter (g); iter; ++iter)
8317 if (qualify_lookup (*iter, LOOK_want::TYPE))
8318 return *iter;
8324 return NULL_TREE;
8327 /* The type TYPE is being declared. If it is a class template, or a
8328 specialization of a class template, do any processing required and
8329 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8330 being declared a friend. B is the binding level at which this TYPE
8331 should be bound.
8333 Returns the TYPE_DECL for TYPE, which may have been altered by this
8334 processing. */
8336 static tree
8337 maybe_process_template_type_declaration (tree type, int is_friend,
8338 cp_binding_level *b)
8340 tree decl = TYPE_NAME (type);
8342 if (processing_template_parmlist)
8343 /* You can't declare a new template type in a template parameter
8344 list. But, you can declare a non-template type:
8346 template <class A*> struct S;
8348 is a forward-declaration of `A'. */
8350 else if (b->kind == sk_namespace
8351 && current_binding_level->kind != sk_namespace)
8352 /* If this new type is being injected into a containing scope,
8353 then it's not a template type. */
8355 else
8357 gcc_assert (MAYBE_CLASS_TYPE_P (type)
8358 || TREE_CODE (type) == ENUMERAL_TYPE);
8360 if (processing_template_decl)
8362 decl = push_template_decl (decl, is_friend);
8363 if (decl == error_mark_node)
8364 return error_mark_node;
8366 /* If the current binding level is the binding level for the
8367 template parameters (see the comment in
8368 begin_template_parm_list) and the enclosing level is a class
8369 scope, and we're not looking at a friend, push the
8370 declaration of the member class into the class scope. In the
8371 friend case, push_template_decl will already have put the
8372 friend into global scope, if appropriate. */
8373 if (TREE_CODE (type) != ENUMERAL_TYPE
8374 && !is_friend && b->kind == sk_template_parms
8375 && b->level_chain->kind == sk_class)
8377 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
8379 if (!COMPLETE_TYPE_P (current_class_type))
8380 maybe_add_class_template_decl_list (current_class_type,
8381 type, /*friend_p=*/0);
8386 return decl;
8389 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8390 that the NAME is a class template, the tag is processed but not pushed.
8392 The pushed scope depend on the SCOPE parameter:
8393 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8394 scope.
8395 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8396 non-template-parameter scope. This case is needed for forward
8397 declarations.
8398 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8399 TS_GLOBAL case except that names within template-parameter scopes
8400 are not pushed at all.
8402 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8404 tree
8405 pushtag (tree name, tree type, TAG_how how)
8407 tree decl;
8409 gcc_assert (identifier_p (name));
8411 auto_cond_timevar tv (TV_NAME_LOOKUP);
8413 cp_binding_level *b = current_binding_level;
8414 while (true)
8416 if (/* Cleanup scopes are not scopes from the point of view of
8417 the language. */
8418 b->kind == sk_cleanup
8419 /* Neither are function parameter scopes. */
8420 || b->kind == sk_function_parms
8421 /* Neither are the scopes used to hold template parameters
8422 for an explicit specialization. For an ordinary template
8423 declaration, these scopes are not scopes from the point of
8424 view of the language. */
8425 || (b->kind == sk_template_parms
8426 && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
8427 b = b->level_chain;
8428 else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
8430 b = b->level_chain;
8431 if (b->kind == sk_template_parms)
8432 b = b->level_chain;
8434 else
8435 break;
8438 /* Do C++ gratuitous typedefing. */
8439 if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
8441 tree tdef;
8442 tree context = TYPE_CONTEXT (type);
8444 if (! context)
8446 cp_binding_level *cb = b;
8447 while (cb->kind != sk_namespace
8448 && cb->kind != sk_class
8449 && (cb->kind != sk_function_parms
8450 || !cb->this_entity))
8451 cb = cb->level_chain;
8452 tree cs = cb->this_entity;
8454 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
8455 ? cs == current_function_decl
8456 : TYPE_P (cs) ? cs == current_class_type
8457 : cs == current_namespace);
8459 if (how == TAG_how::CURRENT_ONLY
8460 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
8461 context = cs;
8462 else if (cs && TYPE_P (cs))
8463 /* When declaring a friend class of a local class, we want
8464 to inject the newly named class into the scope
8465 containing the local class, not the namespace
8466 scope. */
8467 context = decl_function_context (get_type_decl (cs));
8469 if (!context)
8470 context = current_namespace;
8472 tdef = create_implicit_typedef (name, type);
8473 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
8474 set_originating_module (tdef);
8476 decl = maybe_process_template_type_declaration
8477 (type, how == TAG_how::HIDDEN_FRIEND, b);
8478 if (decl == error_mark_node)
8479 return decl;
8481 if (b->kind == sk_class)
8483 if (!TYPE_BEING_DEFINED (current_class_type))
8484 /* Don't push anywhere if the class is complete; a lambda in an
8485 NSDMI is not a member of the class. */
8487 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8488 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8489 class. But if it's a member template class, we want
8490 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8491 later. */
8492 finish_member_declaration (decl);
8493 else
8494 pushdecl_class_level (decl);
8496 else if (b->kind == sk_template_parms)
8498 /* Do not push the tag here -- we'll want to push the
8499 TEMPLATE_DECL. */
8500 if (b->level_chain->kind != sk_class)
8501 set_identifier_type_value_with_scope (name, tdef, b->level_chain);
8503 else
8505 decl = do_pushdecl_with_scope
8506 (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
8507 if (decl == error_mark_node)
8508 return decl;
8510 if (DECL_CONTEXT (decl) == std_node
8511 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
8512 && !CLASSTYPE_TEMPLATE_INFO (type))
8514 error ("declaration of %<std::initializer_list%> does not match "
8515 "%<#include <initializer_list>%>, isn%'t a template");
8516 return error_mark_node;
8520 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
8522 /* If this is a local class, keep track of it. We need this
8523 information for name-mangling, and so that it is possible to
8524 find all function definitions in a translation unit in a
8525 convenient way. (It's otherwise tricky to find a member
8526 function definition it's only pointed to from within a local
8527 class.) */
8528 if (TYPE_FUNCTION_SCOPE_P (type))
8530 if (processing_template_decl)
8532 /* Push a DECL_EXPR so we call pushtag at the right time in
8533 template instantiation rather than in some nested context. */
8534 add_decl_expr (decl);
8536 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8537 else if (!LAMBDA_TYPE_P (type))
8538 determine_local_discriminator (TYPE_NAME (type));
8542 if (b->kind == sk_class
8543 && !COMPLETE_TYPE_P (current_class_type))
8544 maybe_add_class_template_decl_list (current_class_type,
8545 type, /*friend_p=*/0);
8547 decl = TYPE_NAME (type);
8548 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
8550 /* Set type visibility now if this is a forward declaration. */
8551 TREE_PUBLIC (decl) = 1;
8552 determine_visibility (decl);
8554 return type;
8557 /* Subroutines for reverting temporarily to top-level for instantiation
8558 of templates and such. We actually need to clear out the class- and
8559 local-value slots of all identifiers, so that only the global values
8560 are at all visible. Simply setting current_binding_level to the global
8561 scope isn't enough, because more binding levels may be pushed. */
8562 struct saved_scope *scope_chain;
8564 /* Return true if ID has not already been marked. */
8566 static inline bool
8567 store_binding_p (tree id)
8569 if (!id || !IDENTIFIER_BINDING (id))
8570 return false;
8572 if (IDENTIFIER_MARKED (id))
8573 return false;
8575 return true;
8578 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8579 have enough space reserved. */
8581 static void
8582 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
8584 cxx_saved_binding saved;
8586 gcc_checking_assert (store_binding_p (id));
8588 IDENTIFIER_MARKED (id) = 1;
8590 saved.identifier = id;
8591 saved.binding = IDENTIFIER_BINDING (id);
8592 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8593 (*old_bindings)->quick_push (saved);
8594 IDENTIFIER_BINDING (id) = NULL;
8597 static void
8598 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
8600 static vec<tree> bindings_need_stored;
8601 tree t, id;
8602 size_t i;
8604 auto_cond_timevar tv (TV_NAME_LOOKUP);
8605 for (t = names; t; t = TREE_CHAIN (t))
8607 if (TREE_CODE (t) == TREE_LIST)
8608 id = TREE_PURPOSE (t);
8609 else
8610 id = DECL_NAME (t);
8612 if (store_binding_p (id))
8613 bindings_need_stored.safe_push (id);
8615 if (!bindings_need_stored.is_empty ())
8617 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8618 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8620 /* We can apparently have duplicates in NAMES. */
8621 if (store_binding_p (id))
8622 store_binding (id, old_bindings);
8624 bindings_need_stored.truncate (0);
8628 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8629 objects, rather than a TREE_LIST. */
8631 static void
8632 store_class_bindings (vec<cp_class_binding, va_gc> *names,
8633 vec<cxx_saved_binding, va_gc> **old_bindings)
8635 static vec<tree> bindings_need_stored;
8636 size_t i;
8637 cp_class_binding *cb;
8639 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
8640 if (store_binding_p (cb->identifier))
8641 bindings_need_stored.safe_push (cb->identifier);
8642 if (!bindings_need_stored.is_empty ())
8644 tree id;
8645 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8646 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8647 store_binding (id, old_bindings);
8648 bindings_need_stored.truncate (0);
8652 /* A chain of saved_scope structures awaiting reuse. */
8654 static GTY((deletable)) struct saved_scope *free_saved_scope;
8656 void
8657 push_to_top_level (void)
8659 struct saved_scope *s;
8660 cp_binding_level *b;
8661 cxx_saved_binding *sb;
8662 size_t i;
8663 bool need_pop;
8665 auto_cond_timevar tv (TV_NAME_LOOKUP);
8667 /* Reuse or create a new structure for this saved scope. */
8668 if (free_saved_scope != NULL)
8670 s = free_saved_scope;
8671 free_saved_scope = s->prev;
8673 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
8674 memset (s, 0, sizeof (*s));
8675 /* Also reuse the structure's old_bindings vector. */
8676 vec_safe_truncate (old_bindings, 0);
8677 s->old_bindings = old_bindings;
8679 else
8680 s = ggc_cleared_alloc<saved_scope> ();
8682 b = scope_chain ? current_binding_level : 0;
8684 /* If we're in the middle of some function, save our state. */
8685 if (cfun)
8687 need_pop = true;
8688 push_function_context ();
8690 else
8691 need_pop = false;
8693 if (scope_chain && previous_class_level)
8694 store_class_bindings (previous_class_level->class_shadowed,
8695 &s->old_bindings);
8697 /* Have to include the global scope, because class-scope decls
8698 aren't listed anywhere useful. */
8699 for (; b; b = b->level_chain)
8701 tree t;
8703 /* Template IDs are inserted into the global level. If they were
8704 inserted into namespace level, finish_file wouldn't find them
8705 when doing pending instantiations. Therefore, don't stop at
8706 namespace level, but continue until :: . */
8707 if (global_scope_p (b))
8708 break;
8710 store_bindings (b->names, &s->old_bindings);
8711 /* We also need to check class_shadowed to save class-level type
8712 bindings, since pushclass doesn't fill in b->names. */
8713 if (b->kind == sk_class)
8714 store_class_bindings (b->class_shadowed, &s->old_bindings);
8716 /* Unwind type-value slots back to top level. */
8717 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
8718 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
8721 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
8722 IDENTIFIER_MARKED (sb->identifier) = 0;
8724 s->prev = scope_chain;
8725 s->bindings = b;
8726 s->need_pop_function_context = need_pop;
8727 s->function_decl = current_function_decl;
8728 s->unevaluated_operand = cp_unevaluated_operand;
8729 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8730 s->suppress_location_wrappers = suppress_location_wrappers;
8731 s->x_stmt_tree.stmts_are_full_exprs_p = true;
8733 scope_chain = s;
8734 current_function_decl = NULL_TREE;
8735 current_lang_base = NULL;
8736 current_lang_name = lang_name_cplusplus;
8737 current_namespace = global_namespace;
8738 push_class_stack ();
8739 cp_unevaluated_operand = 0;
8740 c_inhibit_evaluation_warnings = 0;
8741 suppress_location_wrappers = 0;
8744 void
8745 pop_from_top_level (void)
8747 struct saved_scope *s = scope_chain;
8748 cxx_saved_binding *saved;
8749 size_t i;
8751 auto_cond_timevar tv (TV_NAME_LOOKUP);
8753 pop_class_stack ();
8755 release_tree_vector (current_lang_base);
8757 scope_chain = s->prev;
8758 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
8760 tree id = saved->identifier;
8762 IDENTIFIER_BINDING (id) = saved->binding;
8763 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8766 /* If we were in the middle of compiling a function, restore our
8767 state. */
8768 if (s->need_pop_function_context)
8769 pop_function_context ();
8770 current_function_decl = s->function_decl;
8771 cp_unevaluated_operand = s->unevaluated_operand;
8772 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
8773 suppress_location_wrappers = s->suppress_location_wrappers;
8775 /* Make this saved_scope structure available for reuse by
8776 push_to_top_level. */
8777 s->prev = free_saved_scope;
8778 free_saved_scope = s;
8781 namespace {
8783 /* Helper class for saving/restoring relevant global flags for the
8784 function-local case of maybe_push_to_top_level. */
8786 struct local_state_t
8788 int cp_unevaluated_operand;
8789 int c_inhibit_evaluation_warnings;
8790 int cp_noexcept_operand_;
8792 static local_state_t
8793 save_and_clear ()
8795 local_state_t s;
8796 s.cp_unevaluated_operand = ::cp_unevaluated_operand;
8797 ::cp_unevaluated_operand = 0;
8798 s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
8799 ::c_inhibit_evaluation_warnings = 0;
8800 s.cp_noexcept_operand_ = ::cp_noexcept_operand;
8801 ::cp_noexcept_operand = 0;
8802 return s;
8805 void
8806 restore () const
8808 ::cp_unevaluated_operand = this->cp_unevaluated_operand;
8809 ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
8810 ::cp_noexcept_operand = this->cp_noexcept_operand_;
8814 vec<local_state_t> local_state_stack;
8816 } // anon namespace
8818 /* Like push_to_top_level, but not if D is function-local. Returns whether we
8819 did push to top. */
8821 bool
8822 maybe_push_to_top_level (tree d)
8824 /* Push if D isn't function-local, or is a lambda function, for which name
8825 resolution is already done. */
8826 const bool push_to_top
8827 = (LAMBDA_FUNCTION_P (d)
8828 || (TREE_CODE (d) == TYPE_DECL
8829 && TREE_TYPE (d)
8830 && LAMBDA_TYPE_P (TREE_TYPE (d)))
8831 || !current_function_decl
8832 || !decl_function_context (d));
8834 if (push_to_top)
8835 push_to_top_level ();
8836 else
8838 gcc_assert (!processing_template_decl);
8839 push_function_context ();
8840 local_state_stack.safe_push (local_state_t::save_and_clear ());
8843 return push_to_top;
8846 /* Return from whatever maybe_push_to_top_level did. */
8848 void
8849 maybe_pop_from_top_level (bool push_to_top)
8851 if (push_to_top)
8852 pop_from_top_level ();
8853 else
8855 local_state_stack.pop ().restore ();
8856 pop_function_context ();
8860 /* Push into the scope of the namespace NS, even if it is deeply
8861 nested within another namespace. */
8863 void
8864 push_nested_namespace (tree ns)
8866 auto_cond_timevar tv (TV_NAME_LOOKUP);
8867 if (ns == global_namespace)
8868 push_to_top_level ();
8869 else
8871 push_nested_namespace (CP_DECL_CONTEXT (ns));
8872 resume_scope (NAMESPACE_LEVEL (ns));
8873 current_namespace = ns;
8877 /* Pop back from the scope of the namespace NS, which was previously
8878 entered with push_nested_namespace. */
8880 void
8881 pop_nested_namespace (tree ns)
8883 auto_cond_timevar tv (TV_NAME_LOOKUP);
8884 while (ns != global_namespace)
8886 ns = CP_DECL_CONTEXT (ns);
8887 current_namespace = ns;
8888 leave_scope ();
8891 pop_from_top_level ();
8894 /* Add TARGET to USINGS, if it does not already exist there. We used
8895 to build the complete graph of usings at this point, from the POV
8896 of the source namespaces. Now we build that as we perform the
8897 unqualified search. */
8899 static void
8900 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
8902 if (usings)
8903 for (unsigned ix = usings->length (); ix--;)
8904 if ((*usings)[ix] == target)
8905 return;
8907 vec_safe_push (usings, target);
8910 /* Tell the debug system of a using directive. */
8912 static void
8913 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
8915 /* Emit debugging info. */
8916 tree context = from != global_namespace ? from : NULL_TREE;
8917 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
8918 implicit);
8921 /* Process a using directive. */
8923 void
8924 finish_using_directive (tree target, tree attribs)
8926 if (target == error_mark_node)
8927 return;
8929 if (current_binding_level->kind != sk_namespace)
8930 add_stmt (build_stmt (input_location, USING_STMT, target));
8931 else
8932 emit_debug_info_using_namespace (current_binding_level->this_entity,
8933 ORIGINAL_NAMESPACE (target), false);
8935 add_using_namespace (current_binding_level->using_directives,
8936 ORIGINAL_NAMESPACE (target));
8938 bool diagnosed = false;
8939 if (attribs != error_mark_node)
8940 for (tree a = attribs; a; a = TREE_CHAIN (a))
8942 tree name = get_attribute_name (a);
8943 if (current_binding_level->kind == sk_namespace
8944 && is_attribute_p ("strong", name))
8946 auto_diagnostic_group d;
8947 if (warning (0, "%<strong%> using directive no longer supported")
8948 && CP_DECL_CONTEXT (target) == current_namespace)
8949 inform (DECL_SOURCE_LOCATION (target),
8950 "you can use an inline namespace instead");
8952 else if ((flag_openmp || flag_openmp_simd)
8953 && get_attribute_namespace (a) == omp_identifier
8954 && (is_attribute_p ("directive", name)
8955 || is_attribute_p ("sequence", name)
8956 || is_attribute_p ("decl", name)))
8958 if (!diagnosed)
8960 if (tree ar = TREE_VALUE (a))
8962 tree d = TREE_VALUE (ar);
8963 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
8964 error ("%<omp::%s%> not allowed to be specified in "
8965 "this context",
8966 TREE_PUBLIC (d) ? "decl" : "directive");
8968 else
8969 error ("%<omp::%E%> not allowed to be specified in this "
8970 "context", name);
8971 diagnosed = true;
8974 else if (!attribute_ignored_p (a))
8975 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
8979 /* Pushes X into the global namespace. */
8981 tree
8982 pushdecl_top_level (tree x)
8984 auto_cond_timevar tv (TV_NAME_LOOKUP);
8985 push_to_top_level ();
8986 gcc_checking_assert (!DECL_CONTEXT (x));
8987 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8988 x = pushdecl_namespace_level (x);
8989 pop_from_top_level ();
8990 return x;
8993 /* Pushes X into the global namespace and calls cp_finish_decl to
8994 register the variable, initializing it with INIT. */
8996 tree
8997 pushdecl_top_level_and_finish (tree x, tree init)
8999 auto_cond_timevar tv (TV_NAME_LOOKUP);
9000 push_to_top_level ();
9001 gcc_checking_assert (!DECL_CONTEXT (x));
9002 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
9003 x = pushdecl_namespace_level (x);
9004 cp_finish_decl (x, init, false, NULL_TREE, 0);
9005 pop_from_top_level ();
9006 return x;
9009 /* Enter the namespaces from current_namerspace to NS. */
9011 static int
9012 push_inline_namespaces (tree ns)
9014 int count = 0;
9015 if (ns != current_namespace)
9017 gcc_assert (ns != global_namespace);
9018 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
9019 resume_scope (NAMESPACE_LEVEL (ns));
9020 current_namespace = ns;
9021 count++;
9023 return count;
9026 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
9027 Reuse or create a namespace NAME. NAME is null for the anonymous
9028 namespace. */
9030 static tree
9031 reuse_namespace (tree *slot, tree ctx, tree name)
9033 if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
9035 /* Public namespace. Shared. */
9036 tree *global_slot = slot;
9037 if (TREE_CODE (*slot) == BINDING_VECTOR)
9038 global_slot = get_fixed_binding_slot (slot, name,
9039 BINDING_SLOT_GLOBAL, false);
9041 for (ovl_iterator iter (*global_slot); iter; ++iter)
9043 tree decl = *iter;
9045 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
9046 return decl;
9049 return NULL_TREE;
9052 static tree
9053 make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
9055 /* Create the namespace. */
9056 tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
9057 DECL_SOURCE_LOCATION (ns) = loc;
9058 SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
9059 if (!SCOPE_DEPTH (ns))
9060 /* We only allow depth 255. */
9061 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
9062 DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
9064 if (!name)
9065 /* Anon-namespaces in different header-unit imports are distinct.
9066 But that's ok as their contents all have internal linkage.
9067 (This is different to how they'd behave as textual includes,
9068 but doing this at all is really odd source.) */
9069 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
9070 else if (TREE_PUBLIC (ctx))
9071 TREE_PUBLIC (ns) = true;
9073 if (inline_p)
9074 DECL_NAMESPACE_INLINE_P (ns) = true;
9076 return ns;
9079 /* NS was newly created, finish off making it. */
9081 static void
9082 make_namespace_finish (tree ns, tree *slot, bool from_import = false)
9084 if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
9086 /* Merge into global slot. */
9087 tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
9088 BINDING_SLOT_GLOBAL, true);
9089 *gslot = ns;
9092 tree ctx = CP_DECL_CONTEXT (ns);
9093 cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
9094 scope->this_entity = ns;
9095 scope->more_cleanups_ok = true;
9096 scope->kind = sk_namespace;
9097 scope->level_chain = NAMESPACE_LEVEL (ctx);
9098 NAMESPACE_LEVEL (ns) = scope;
9100 if (DECL_NAMESPACE_INLINE_P (ns))
9101 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
9103 if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
9104 emit_debug_info_using_namespace (ctx, ns, true);
9107 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
9108 then we enter an anonymous namespace. If MAKE_INLINE is true, then
9109 we create an inline namespace (it is up to the caller to check upon
9110 redefinition). Return the number of namespaces entered. */
9113 push_namespace (tree name, bool make_inline)
9115 auto_cond_timevar tv (TV_NAME_LOOKUP);
9116 int count = 0;
9118 /* We should not get here if the global_namespace is not yet constructed
9119 nor if NAME designates the global namespace: The global scope is
9120 constructed elsewhere. */
9121 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
9123 tree ns = NULL_TREE;
9125 name_lookup lookup (name);
9126 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
9128 else if (TREE_CODE (lookup.value) == TREE_LIST)
9130 /* An ambiguous lookup. If exactly one is a namespace, we
9131 want that. If more than one is a namespace, error, but
9132 pick one of them. */
9133 /* DR2061 can cause us to find multiple namespaces of the same
9134 name. We must treat that carefully and avoid thinking we
9135 need to push a new (possibly) duplicate namespace. Hey,
9136 if you want to use the same identifier within an inline
9137 nest, knock yourself out. */
9138 for (tree *chain = &lookup.value, next; (next = *chain);)
9140 tree decl = TREE_VALUE (next);
9141 if (TREE_CODE (decl) == NAMESPACE_DECL)
9143 if (!ns)
9144 ns = decl;
9145 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
9146 ns = decl;
9148 /* Advance. */
9149 chain = &TREE_CHAIN (next);
9151 else
9152 /* Stitch out. */
9153 *chain = TREE_CHAIN (next);
9156 if (TREE_CHAIN (lookup.value))
9158 error ("%<namespace %E%> is ambiguous", name);
9159 print_candidates (lookup.value);
9162 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
9163 ns = lookup.value;
9165 if (ns)
9166 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
9168 /* A namespace alias is not allowed here, but if the alias
9169 is for a namespace also inside the current scope,
9170 accept it with a diagnostic. That's better than dying
9171 horribly. */
9172 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
9174 error ("namespace alias %qD not allowed here, "
9175 "assuming %qD", ns, dna);
9176 ns = dna;
9178 else
9179 ns = NULL_TREE;
9183 if (ns)
9185 /* DR2061. NS might be a member of an inline namespace. We
9186 need to push into those namespaces. */
9187 if (modules_p ())
9189 for (tree parent, ctx = ns; ctx != current_namespace;
9190 ctx = parent)
9192 parent = CP_DECL_CONTEXT (ctx);
9194 tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
9195 if (bind != ctx)
9197 auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
9198 binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
9199 gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
9200 slot = ctx;
9205 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
9206 if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
9207 /* It's not builtin now. */
9208 DECL_SOURCE_LOCATION (ns) = input_location;
9210 else
9212 /* Before making a new namespace, see if we already have one in
9213 the existing partitions of the current namespace. */
9214 tree *slot = find_namespace_slot (current_namespace, name, false);
9215 if (slot)
9216 ns = reuse_namespace (slot, current_namespace, name);
9217 if (!ns)
9218 ns = make_namespace (current_namespace, name,
9219 input_location, make_inline);
9221 if (pushdecl (ns) == error_mark_node)
9222 ns = NULL_TREE;
9223 else
9225 /* Finish up making the namespace. */
9226 add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
9227 if (!slot)
9229 slot = find_namespace_slot (current_namespace, name);
9230 /* This should find the slot created by pushdecl. */
9231 gcc_checking_assert (slot && *slot == ns);
9233 else
9235 /* pushdecl could have expanded the hash table, so
9236 slot might be invalid. */
9237 slot = find_namespace_slot (current_namespace, name);
9238 gcc_checking_assert (slot);
9240 make_namespace_finish (ns, slot);
9242 /* Add the anon using-directive here, we don't do it in
9243 make_namespace_finish. */
9244 if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
9245 add_using_namespace (current_binding_level->using_directives, ns);
9249 if (ns)
9251 /* A public namespace is exported only if explicitly marked, or
9252 it contains exported entities. */
9253 if (module_exporting_p ())
9255 if (TREE_PUBLIC (ns))
9256 DECL_MODULE_EXPORT_P (ns) = true;
9257 else if (!header_module_p ())
9258 error_at (input_location,
9259 "exporting namespace with internal linkage");
9261 if (module_purview_p ())
9262 DECL_MODULE_PURVIEW_P (ns) = true;
9264 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
9266 auto_diagnostic_group d;
9267 error_at (input_location,
9268 "inline namespace must be specified at initial definition");
9269 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
9271 resume_scope (NAMESPACE_LEVEL (ns));
9272 current_namespace = ns;
9273 count++;
9276 return count;
9279 /* Pop from the scope of the current namespace. */
9281 void
9282 pop_namespace (void)
9284 auto_cond_timevar tv (TV_NAME_LOOKUP);
9286 gcc_assert (current_namespace != global_namespace);
9287 current_namespace = CP_DECL_CONTEXT (current_namespace);
9288 /* The binding level is not popped, as it might be re-opened later. */
9289 leave_scope ();
9292 /* An IMPORT is an import that is defining namespace NAME inside CTX. Find or
9293 create that namespace and add it to the container's binding-vector. */
9295 tree
9296 add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
9297 bool inline_p, bool visible_p)
9299 // FIXME: Something is not correct about the VISIBLE_P handling. We
9300 // need to insert this namespace into
9301 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9302 // (b) The importing module's slot (always)
9303 // (c) Do we need to put it in the CURRENT slot? This is the
9304 // confused piece.
9306 tree *slot = find_namespace_slot (ctx, name, true);
9307 tree decl = reuse_namespace (slot, ctx, name);
9309 /* Creating and binding. */
9310 if (!decl)
9312 decl = make_namespace (ctx, name, loc, inline_p);
9313 make_namespace_finish (decl, slot, true);
9315 else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
9317 auto_diagnostic_group d;
9318 error_at (loc, "%s namespace %qD conflicts with reachable definition",
9319 inline_p ? "inline" : "non-inline", decl);
9320 inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
9321 inline_p ? "non-inline" : "inline");
9324 if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
9326 /* See if we can extend the final slot. */
9327 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
9328 gcc_checking_assert (last->indices[0].span);
9329 unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
9331 while (--jx)
9332 if (last->indices[jx].span)
9333 break;
9334 tree final = last->slots[jx];
9335 if (visible_p == !STAT_HACK_P (final)
9336 && MAYBE_STAT_DECL (final) == decl
9337 && last->indices[jx].base + last->indices[jx].span == import
9338 && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
9339 || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
9340 && jx >= BINDING_SLOTS_FIXED)))
9342 last->indices[jx].span++;
9343 return decl;
9347 /* Append a new slot. */
9348 tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
9350 gcc_assert (!*mslot);
9351 *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
9353 return decl;
9356 /* Pop off extraneous binding levels left over due to syntax errors.
9357 We don't pop past namespaces, as they might be valid. */
9359 void
9360 pop_everything (void)
9362 if (ENABLE_SCOPE_CHECKING)
9363 verbatim ("XXX entering %<pop_everything ()%>");
9364 while (!namespace_bindings_p ())
9366 if (current_binding_level->kind == sk_class)
9367 pop_nested_class ();
9368 else
9369 poplevel (0, 0, 0);
9371 if (ENABLE_SCOPE_CHECKING)
9372 verbatim ("XXX leaving %<pop_everything ()%>");
9375 /* Emit debugging information for using declarations and directives.
9376 If input tree is overloaded fn then emit debug info for all
9377 candidates. */
9379 void
9380 cp_emit_debug_info_for_using (tree t, tree context)
9382 /* Don't try to emit any debug information if we have errors. */
9383 if (seen_error ())
9384 return;
9386 /* Do not supply context to imported_module_or_decl, if
9387 it is a global namespace. */
9388 if (context == global_namespace)
9389 context = NULL_TREE;
9391 t = MAYBE_BASELINK_FUNCTIONS (t);
9393 for (lkp_iterator iter (t); iter; ++iter)
9395 tree fn = *iter;
9397 if (TREE_CODE (fn) == TEMPLATE_DECL)
9398 /* FIXME: Handle TEMPLATE_DECLs. */
9399 continue;
9401 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9402 of a builtin function. */
9403 if (TREE_CODE (fn) == FUNCTION_DECL
9404 && DECL_EXTERNAL (fn)
9405 && fndecl_built_in_p (fn))
9406 continue;
9408 if (building_stmt_list_p ())
9409 add_stmt (build_stmt (input_location, USING_STMT, fn));
9410 else
9411 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
9412 false, false);
9416 /* True if D is a local declaration in dependent scope. Assumes that it is
9417 (part of) the current lookup result for its name. */
9419 bool
9420 dependent_local_decl_p (tree d)
9422 if (!DECL_LOCAL_DECL_P (d))
9423 return false;
9425 cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
9426 cp_binding_level *l = b->scope;
9427 while (!l->this_entity)
9428 l = l->level_chain;
9429 return uses_template_parms (l->this_entity);
9434 #include "gt-cp-name-lookup.h"