c++: Implement P2615 'Meaningful Exports' [PR107688]
[official-gcc.git] / gcc / cp / name-lookup.cc
blob5d2319db43d43c2d6b13ff09d9a448e126af418f
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
55 (optional). */
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 only needed when we're a named
252 module. */
253 bool partition_slot = named_module_p ();
254 unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
255 + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
256 / BINDING_VECTOR_SLOTS_PER_CLUSTER);
257 tree new_vec = make_binding_vec (name, want);
258 BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
259 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
261 /* Initialize the fixed slots. */
262 for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
264 cluster[0].indices[jx].base = 0;
265 cluster[0].indices[jx].span = 1;
266 cluster[0].slots[jx] = NULL_TREE;
269 if (partition_slot)
271 unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
272 unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
273 cluster[ind].indices[off].base = 0;
274 cluster[ind].indices[off].span = 1;
275 cluster[ind].slots[off] = NULL_TREE;
278 if (tree orig = *slot)
280 /* Propagate existing value to current slot. */
282 /* Propagate global & module entities to the global and
283 partition slots. */
284 if (tree type = MAYBE_STAT_TYPE (orig))
285 init_global_partition (cluster, type);
287 for (ovl_iterator iter (MAYBE_STAT_DECL (orig)); iter; ++iter)
289 tree decl = *iter;
291 /* Internal linkage entities are in deduplicateable. */
292 init_global_partition (cluster, decl);
295 if (cluster[0].slots[BINDING_SLOT_GLOBAL]
296 && !(TREE_CODE (orig) == NAMESPACE_DECL
297 && !DECL_NAMESPACE_ALIAS (orig)))
299 /* Note that we had some GMF entries. */
300 if (!STAT_HACK_P (orig))
301 orig = stat_hack (orig);
303 MODULE_BINDING_GLOBAL_P (orig) = true;
306 cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
309 *slot = new_vec;
311 else
312 gcc_checking_assert (create >= 0);
314 unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
315 binding_cluster &cluster
316 = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
318 /* There must always be slots for these indices */
319 gcc_checking_assert (cluster.indices[off].span == 1
320 && !cluster.indices[off].base
321 && !cluster.slots[off].is_lazy ());
323 return reinterpret_cast<tree *> (&cluster.slots[off]);
326 /* *SLOT is a namespace binding slot. Append a slot for imported
327 module IX. */
329 static binding_slot *
330 append_imported_binding_slot (tree *slot, tree name, unsigned ix)
332 gcc_checking_assert (ix);
334 if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
335 /* Make an initial module vector. */
336 get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
337 else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
338 ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
339 /* There is space in the last cluster. */;
340 else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
341 != BINDING_VECTOR_ALLOC_CLUSTERS (*slot))
342 /* There is space in the vector. */
343 BINDING_VECTOR_NUM_CLUSTERS (*slot)++;
344 else
346 /* Extend the vector. */
347 unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
348 unsigned want = (have * 3 + 1) / 2;
350 if (want > (unsigned short)~0)
351 want = (unsigned short)~0;
353 tree new_vec = make_binding_vec (name, want);
354 BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
355 memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
356 BINDING_VECTOR_CLUSTER_BASE (*slot),
357 have * sizeof (binding_cluster));
358 *slot = new_vec;
361 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
362 for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
363 if (!last->indices[off].span)
365 /* Fill the free slot of the cluster. */
366 last->indices[off].base = ix;
367 last->indices[off].span = 1;
368 last->slots[off] = NULL_TREE;
369 /* Check monotonicity. */
370 gcc_checking_assert (last[off ? 0 : -1]
371 .indices[off ? off - 1
372 : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
373 .base < ix);
374 return &last->slots[off];
377 gcc_unreachable ();
380 /* Add DECL to the list of things declared in binding level B. */
382 static void
383 add_decl_to_level (cp_binding_level *b, tree decl)
385 gcc_assert (b->kind != sk_class);
387 /* Make sure we don't create a circular list. xref_tag can end
388 up pushing the same artificial decl more than once. We
389 should have already detected that in update_binding. (This isn't a
390 complete verification of non-circularity.) */
391 gcc_assert (b->names != decl);
393 /* We build up the list in reverse order, and reverse it later if
394 necessary. */
395 TREE_CHAIN (decl) = b->names;
396 b->names = decl;
398 /* If appropriate, add decl to separate list of statics. We include
399 extern variables because they might turn out to be static later.
400 It's OK for this list to contain a few false positives. */
401 if (b->kind == sk_namespace
402 && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
403 || (TREE_CODE (decl) == FUNCTION_DECL
404 && (!TREE_PUBLIC (decl)
405 || decl_internal_context_p (decl)
406 || DECL_DECLARED_INLINE_P (decl)))))
407 vec_safe_push (static_decls, decl);
410 /* Find the binding for NAME in the local binding level B. */
412 static cxx_binding *
413 find_local_binding (cp_binding_level *b, tree name)
415 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
416 for (;; b = b->level_chain)
418 if (binding->scope == b)
419 return binding;
421 /* Cleanup contours are transparent to the language. */
422 if (b->kind != sk_cleanup)
423 break;
425 return NULL;
428 class name_lookup
430 public:
431 typedef std::pair<tree, tree> using_pair;
432 typedef auto_vec<using_pair, 16> using_queue;
434 public:
435 tree name; /* The identifier being looked for. */
437 /* Usually we just add things to the VALUE binding, but we record
438 (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
439 using-decl resolution. */
440 tree value; /* A (possibly ambiguous) set of things found. */
441 tree type; /* A type that has been found. */
443 LOOK_want want; /* What kind of entity we want. */
445 bool deduping; /* Full deduping is needed because using declarations
446 are in play. */
447 vec<tree, va_heap, vl_embed> *scopes;
448 name_lookup *previous; /* Previously active lookup. */
450 protected:
451 /* Marked scope stack for outermost name lookup. */
452 static vec<tree, va_heap, vl_embed> *shared_scopes;
453 /* Currently active lookup. */
454 static name_lookup *active;
456 public:
457 name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
458 : name (n), value (NULL_TREE), type (NULL_TREE),
459 want (w),
460 deduping (false), scopes (NULL), previous (NULL)
462 preserve_state ();
464 ~name_lookup ()
466 gcc_checking_assert (!deduping);
467 restore_state ();
470 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
471 name_lookup (const name_lookup &);
472 name_lookup &operator= (const name_lookup &);
474 public:
475 /* Turn on or off deduping mode. */
476 void dedup (bool state)
478 if (deduping != state)
480 deduping = state;
481 lookup_mark (value, state);
485 protected:
486 static bool seen_p (tree scope)
488 return LOOKUP_SEEN_P (scope);
490 static bool found_p (tree scope)
492 return LOOKUP_FOUND_P (scope);
495 void mark_seen (tree scope); /* Mark and add to scope vector. */
496 static void mark_found (tree scope)
498 gcc_checking_assert (seen_p (scope));
499 LOOKUP_FOUND_P (scope) = true;
501 bool see_and_mark (tree scope)
503 bool ret = seen_p (scope);
504 if (!ret)
505 mark_seen (scope);
506 return ret;
508 bool find_and_mark (tree scope);
510 private:
511 void preserve_state ();
512 void restore_state ();
514 public:
515 static tree ambiguous (tree thing, tree current);
516 void add_value (tree new_val);
517 private:
518 void add_overload (tree fns);
519 void add_type (tree new_type);
520 bool process_binding (tree val_bind, tree type_bind);
521 unsigned process_module_binding (tree val_bind, tree type_bind, unsigned);
522 /* Look in only namespace. */
523 bool search_namespace_only (tree scope);
524 /* Look in namespace and its (recursive) inlines. Ignore using
525 directives. Return true if something found (inc dups). */
526 bool search_namespace (tree scope);
527 /* Look in the using directives of namespace + inlines using
528 qualified lookup rules. */
529 bool search_usings (tree scope);
531 private:
532 void queue_namespace (using_queue& queue, int depth, tree scope);
533 void queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings);
535 private:
536 void add_fns (tree);
538 private:
539 void adl_expr (tree);
540 void adl_type (tree);
541 void adl_template_arg (tree);
542 void adl_class (tree);
543 void adl_enum (tree);
544 void adl_bases (tree);
545 void adl_class_only (tree);
546 void adl_namespace (tree);
547 void adl_class_fns (tree);
548 void adl_namespace_fns (tree, bitmap);
550 public:
551 /* Search namespace + inlines + maybe usings as qualified lookup. */
552 bool search_qualified (tree scope, bool usings = true);
554 /* Search namespace + inlines + usings as unqualified lookup. */
555 bool search_unqualified (tree scope, cp_binding_level *);
557 /* ADL lookup of ARGS. */
558 tree search_adl (tree fns, vec<tree, va_gc> *args);
561 /* Scope stack shared by all outermost lookups. This avoids us
562 allocating and freeing on every single lookup. */
563 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
565 /* Currently active lookup. */
566 name_lookup *name_lookup::active;
568 /* Name lookup is recursive, becase ADL can cause template
569 instatiation. This is of course a rare event, so we optimize for
570 it not happening. When we discover an active name-lookup, which
571 must be an ADL lookup, we need to unmark the marked scopes and also
572 unmark the lookup we might have been accumulating. */
574 void
575 name_lookup::preserve_state ()
577 previous = active;
578 if (previous)
580 unsigned length = vec_safe_length (previous->scopes);
581 vec_safe_reserve (previous->scopes, length * 2);
582 for (unsigned ix = length; ix--;)
584 tree decl = (*previous->scopes)[ix];
586 gcc_checking_assert (LOOKUP_SEEN_P (decl));
587 LOOKUP_SEEN_P (decl) = false;
589 /* Preserve the FOUND_P state on the interrupted lookup's
590 stack. */
591 if (LOOKUP_FOUND_P (decl))
593 LOOKUP_FOUND_P (decl) = false;
594 previous->scopes->quick_push (decl);
598 /* Unmark the outer partial lookup. */
599 if (previous->deduping)
600 lookup_mark (previous->value, false);
602 else
603 scopes = shared_scopes;
604 active = this;
607 /* Restore the marking state of a lookup we interrupted. */
609 void
610 name_lookup::restore_state ()
612 gcc_checking_assert (!deduping);
614 /* Unmark and empty this lookup's scope stack. */
615 for (unsigned ix = vec_safe_length (scopes); ix--;)
617 tree decl = scopes->pop ();
618 gcc_checking_assert (LOOKUP_SEEN_P (decl));
619 LOOKUP_SEEN_P (decl) = false;
620 LOOKUP_FOUND_P (decl) = false;
623 active = previous;
624 if (previous)
626 free (scopes);
628 unsigned length = vec_safe_length (previous->scopes);
629 for (unsigned ix = 0; ix != length; ix++)
631 tree decl = (*previous->scopes)[ix];
632 if (LOOKUP_SEEN_P (decl))
634 /* The remainder of the scope stack must be recording
635 FOUND_P decls, which we want to pop off. */
638 tree decl = previous->scopes->pop ();
639 gcc_checking_assert (LOOKUP_SEEN_P (decl)
640 && !LOOKUP_FOUND_P (decl));
641 LOOKUP_FOUND_P (decl) = true;
643 while (++ix != length);
644 break;
647 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
648 LOOKUP_SEEN_P (decl) = true;
651 /* Remark the outer partial lookup. */
652 if (previous->deduping)
653 lookup_mark (previous->value, true);
655 else
656 shared_scopes = scopes;
659 void
660 name_lookup::mark_seen (tree scope)
662 gcc_checking_assert (!seen_p (scope));
663 LOOKUP_SEEN_P (scope) = true;
664 vec_safe_push (scopes, scope);
667 bool
668 name_lookup::find_and_mark (tree scope)
670 bool result = LOOKUP_FOUND_P (scope);
671 if (!result)
673 LOOKUP_FOUND_P (scope) = true;
674 if (!LOOKUP_SEEN_P (scope))
675 vec_safe_push (scopes, scope);
678 return result;
681 /* THING and CURRENT are ambiguous, concatenate them. */
683 tree
684 name_lookup::ambiguous (tree thing, tree current)
686 if (TREE_CODE (current) != TREE_LIST)
688 current = build_tree_list (NULL_TREE, current);
689 TREE_TYPE (current) = error_mark_node;
691 current = tree_cons (NULL_TREE, thing, current);
692 TREE_TYPE (current) = error_mark_node;
694 return current;
697 /* FNS is a new overload set to add to the exising set. */
699 void
700 name_lookup::add_overload (tree fns)
702 if (!deduping && TREE_CODE (fns) == OVERLOAD)
704 tree probe = fns;
705 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
706 probe = ovl_skip_hidden (probe);
707 if (probe && TREE_CODE (probe) == OVERLOAD
708 && OVL_DEDUP_P (probe))
709 /* We're about to add something found by multiple paths, so need to
710 engage deduping mode. */
711 dedup (true);
714 value = lookup_maybe_add (fns, value, deduping);
717 /* Add a NEW_VAL, a found value binding into the current value binding. */
719 void
720 name_lookup::add_value (tree new_val)
722 if (OVL_P (new_val) && (!value || OVL_P (value)))
723 add_overload (new_val);
724 else if (!value)
725 value = new_val;
726 else if (value == new_val)
728 else if ((TREE_CODE (value) == TYPE_DECL
729 && TREE_CODE (new_val) == TYPE_DECL
730 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
731 /* Typedefs to the same type. */;
732 else if (TREE_CODE (value) == NAMESPACE_DECL
733 && TREE_CODE (new_val) == NAMESPACE_DECL
734 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
735 /* Namespace (possibly aliased) to the same namespace. Locate
736 the namespace*/
737 value = ORIGINAL_NAMESPACE (value);
738 else
740 /* Disengage deduping mode. */
741 dedup (false);
742 value = ambiguous (new_val, value);
746 /* Add a NEW_TYPE, a found type binding into the current type binding. */
748 void
749 name_lookup::add_type (tree new_type)
751 if (!type)
752 type = new_type;
753 else if (TREE_CODE (type) == TREE_LIST
754 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
755 type = ambiguous (new_type, type);
758 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
759 true if we actually found something noteworthy. Hiddenness has
760 already been handled in the caller. */
762 bool
763 name_lookup::process_binding (tree new_val, tree new_type)
765 /* Did we really see a type? */
766 if (new_type
767 && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
768 new_type = NULL_TREE;
770 /* Do we really see a value? */
771 if (new_val)
772 switch (TREE_CODE (new_val))
774 case TEMPLATE_DECL:
775 /* If we expect types or namespaces, and not templates,
776 or this is not a template class. */
777 if (bool (want & LOOK_want::TYPE_NAMESPACE)
778 && !DECL_TYPE_TEMPLATE_P (new_val))
779 new_val = NULL_TREE;
780 break;
781 case TYPE_DECL:
782 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
783 || (new_type && bool (want & LOOK_want::TYPE)))
784 new_val = NULL_TREE;
785 break;
786 case NAMESPACE_DECL:
787 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
788 new_val = NULL_TREE;
789 break;
790 default:
791 if (bool (want & LOOK_want::TYPE_NAMESPACE))
792 new_val = NULL_TREE;
795 if (!new_val)
797 new_val = new_type;
798 new_type = NULL_TREE;
801 /* Merge into the lookup */
802 if (new_val)
803 add_value (new_val);
804 if (new_type)
805 add_type (new_type);
807 return new_val != NULL_TREE;
810 /* If we're importing a module containing this binding, add it to the
811 lookup set. The trickiness is with namespaces, we only want to
812 find it once. */
814 unsigned
815 name_lookup::process_module_binding (tree new_val, tree new_type,
816 unsigned marker)
818 /* Optimize for (re-)finding a public namespace. We only need to
819 look once. */
820 if (new_val && !new_type
821 && TREE_CODE (new_val) == NAMESPACE_DECL
822 && TREE_PUBLIC (new_val)
823 && !DECL_NAMESPACE_ALIAS (new_val))
825 if (marker & 2)
826 return marker;
827 marker |= 2;
830 if (new_type || new_val)
831 marker |= process_binding (new_val, new_type);
833 return marker;
836 /* Look in exactly namespace SCOPE. */
838 bool
839 name_lookup::search_namespace_only (tree scope)
841 bool found = false;
842 if (tree *binding = find_namespace_slot (scope, name))
844 tree val = *binding;
845 if (TREE_CODE (val) == BINDING_VECTOR)
847 /* I presume the binding list is going to be sparser than
848 the import bitmap. Hence iterate over the former
849 checking for bits set in the bitmap. */
850 bitmap imports = get_import_bitmap ();
851 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
852 int marker = 0;
853 int dup_detect = 0;
855 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
857 if (!deduping)
859 if (named_module_purview_p ())
861 dup_detect |= 2;
863 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
864 dup_detect |= 1;
866 else
867 dup_detect |= 1;
869 tree type = NULL_TREE;
870 tree value = bind;
872 if (STAT_HACK_P (bind))
874 type = STAT_TYPE (bind);
875 value = STAT_DECL (bind);
877 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
879 if (STAT_TYPE_HIDDEN_P (bind))
880 type = NULL_TREE;
881 if (STAT_DECL_HIDDEN_P (bind))
882 value = NULL_TREE;
883 else
884 value = ovl_skip_hidden (value);
887 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
888 value = ovl_skip_hidden (value);
890 marker = process_module_binding (value, type, marker);
893 /* Scan the imported bindings. */
894 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
895 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
897 ix--;
898 cluster++;
901 /* Do this in forward order, so we load modules in an order
902 the user expects. */
903 for (; ix--; cluster++)
904 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
906 /* Are we importing this module? */
907 if (unsigned base = cluster->indices[jx].base)
908 if (unsigned span = cluster->indices[jx].span)
910 if (bitmap_bit_p (imports, base))
911 goto found;
912 while (++base, --span);
913 continue;
915 found:;
916 /* Is it loaded? */
917 if (cluster->slots[jx].is_lazy ())
919 gcc_assert (cluster->indices[jx].span == 1);
920 lazy_load_binding (cluster->indices[jx].base,
921 scope, name, &cluster->slots[jx]);
923 tree bind = cluster->slots[jx];
924 if (!bind)
925 /* Load errors could mean there's nothing here. */
926 continue;
928 /* Extract what we can see from here. If there's no
929 stat_hack, then everything was exported. */
930 tree type = NULL_TREE;
933 /* If STAT_HACK_P is false, everything is visible, and
934 there's no duplication possibilities. */
935 if (STAT_HACK_P (bind))
937 if (!deduping)
939 /* Do we need to engage deduplication? */
940 int dup = 0;
941 if (MODULE_BINDING_GLOBAL_P (bind))
942 dup = 1;
943 else if (MODULE_BINDING_PARTITION_P (bind))
944 dup = 2;
945 if (unsigned hit = dup_detect & dup)
947 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
948 || (hit & 2
949 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
950 dedup (true);
952 dup_detect |= dup;
955 if (STAT_TYPE_VISIBLE_P (bind))
956 type = STAT_TYPE (bind);
957 bind = STAT_VISIBLE (bind);
960 /* And process it. */
961 marker = process_module_binding (bind, type, marker);
963 found |= marker & 1;
965 else
967 /* Only a current module binding, visible from the current module. */
968 tree bind = *binding;
969 tree value = bind, type = NULL_TREE;
971 if (STAT_HACK_P (bind))
973 type = STAT_TYPE (bind);
974 value = STAT_DECL (bind);
976 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
978 if (STAT_TYPE_HIDDEN_P (bind))
979 type = NULL_TREE;
980 if (STAT_DECL_HIDDEN_P (bind))
981 value = NULL_TREE;
982 else
983 value = ovl_skip_hidden (value);
986 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
987 value = ovl_skip_hidden (value);
989 found |= process_binding (value, type);
993 return found;
996 /* Conditionally look in namespace SCOPE and inline children. */
998 bool
999 name_lookup::search_namespace (tree scope)
1001 if (see_and_mark (scope))
1002 /* We've visited this scope before. Return what we found then. */
1003 return found_p (scope);
1005 /* Look in exactly namespace. */
1006 bool found = search_namespace_only (scope);
1008 /* Don't look into inline children, if we're looking for an
1009 anonymous name -- it must be in the current scope, if anywhere. */
1010 if (name)
1011 /* Recursively look in its inline children. */
1012 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1013 for (unsigned ix = inlinees->length (); ix--;)
1014 found |= search_namespace ((*inlinees)[ix]);
1016 if (found)
1017 mark_found (scope);
1019 return found;
1022 /* Recursively follow using directives of SCOPE & its inline children.
1023 Such following is essentially a flood-fill algorithm. */
1025 bool
1026 name_lookup::search_usings (tree scope)
1028 /* We do not check seen_p here, as that was already set during the
1029 namespace_only walk. */
1030 if (found_p (scope))
1031 return true;
1033 bool found = false;
1034 if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
1035 for (unsigned ix = usings->length (); ix--;)
1036 found |= search_qualified ((*usings)[ix], true);
1038 /* Look in its inline children. */
1039 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1040 for (unsigned ix = inlinees->length (); ix--;)
1041 found |= search_usings ((*inlinees)[ix]);
1043 if (found)
1044 mark_found (scope);
1046 return found;
1049 /* Qualified namespace lookup in SCOPE.
1050 1) Look in SCOPE (+inlines). If found, we're done.
1051 2) Otherwise, if USINGS is true,
1052 recurse for every using directive of SCOPE (+inlines).
1054 Trickiness is (a) loops and (b) multiple paths to same namespace.
1055 In both cases we want to not repeat any lookups, and know whether
1056 to stop the caller's step #2. Do this via the FOUND_P marker. */
1058 bool
1059 name_lookup::search_qualified (tree scope, bool usings)
1061 bool found = false;
1063 if (seen_p (scope))
1064 found = found_p (scope);
1065 else
1067 found = search_namespace (scope);
1068 if (!found && usings)
1069 found = search_usings (scope);
1072 dedup (false);
1074 return found;
1077 /* Add SCOPE to the unqualified search queue, recursively add its
1078 inlines and those via using directives. */
1080 void
1081 name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
1083 if (see_and_mark (scope))
1084 return;
1086 /* Record it. */
1087 tree common = scope;
1088 while (SCOPE_DEPTH (common) > depth)
1089 common = CP_DECL_CONTEXT (common);
1090 queue.safe_push (using_pair (common, scope));
1092 /* Queue its inline children. */
1093 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1094 for (unsigned ix = inlinees->length (); ix--;)
1095 queue_namespace (queue, depth, (*inlinees)[ix]);
1097 /* Queue its using targets. */
1098 queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
1101 /* Add the namespaces in USINGS to the unqualified search queue. */
1103 void
1104 name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
1106 if (usings)
1107 for (unsigned ix = usings->length (); ix--;)
1108 queue_namespace (queue, depth, (*usings)[ix]);
1111 /* Unqualified namespace lookup in SCOPE.
1112 1) add scope+inlins to worklist.
1113 2) recursively add target of every using directive
1114 3) for each worklist item where SCOPE is common ancestor, search it
1115 4) if nothing find, scope=parent, goto 1. */
1117 bool
1118 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
1120 using_queue queue;
1121 bool found = false;
1123 /* Queue local using-directives. */
1124 for (; level->kind != sk_namespace; level = level->level_chain)
1125 queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
1127 for (; !found; scope = CP_DECL_CONTEXT (scope))
1129 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
1130 int depth = SCOPE_DEPTH (scope);
1132 /* Queue namespaces reachable from SCOPE. */
1133 queue_namespace (queue, depth, scope);
1135 /* Search every queued namespace where SCOPE is the common
1136 ancestor. Adjust the others. */
1137 unsigned ix = 0;
1140 using_pair &pair = queue[ix];
1141 while (pair.first == scope)
1143 found |= search_namespace_only (pair.second);
1144 pair = queue.pop ();
1145 if (ix == queue.length ())
1146 goto done;
1148 /* The depth is the same as SCOPE, find the parent scope. */
1149 if (SCOPE_DEPTH (pair.first) == depth)
1150 pair.first = CP_DECL_CONTEXT (pair.first);
1151 ix++;
1153 while (ix < queue.length ());
1154 done:;
1155 if (scope == global_namespace)
1156 break;
1158 /* If looking for hidden friends, we only look in the innermost
1159 namespace scope. [namespace.memdef]/3 If a friend
1160 declaration in a non-local class first declares a class,
1161 function, class template or function template the friend is a
1162 member of the innermost enclosing namespace. See also
1163 [basic.lookup.unqual]/7 */
1164 if (bool (want & LOOK_want::HIDDEN_FRIEND))
1165 break;
1168 dedup (false);
1170 return found;
1173 /* FNS is a value binding. If it is a (set of overloaded) functions,
1174 add them into the current value. */
1176 void
1177 name_lookup::add_fns (tree fns)
1179 if (!fns)
1180 return;
1181 else if (TREE_CODE (fns) == OVERLOAD)
1183 if (TREE_TYPE (fns) != unknown_type_node)
1184 fns = OVL_FUNCTION (fns);
1186 else if (!DECL_DECLARES_FUNCTION_P (fns))
1187 return;
1189 add_overload (fns);
1192 /* Add the overloaded fns of SCOPE. */
1194 void
1195 name_lookup::adl_namespace_fns (tree scope, bitmap imports)
1197 if (tree *binding = find_namespace_slot (scope, name))
1199 tree val = *binding;
1200 if (TREE_CODE (val) != BINDING_VECTOR)
1201 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
1202 else
1204 /* I presume the binding list is going to be sparser than
1205 the import bitmap. Hence iterate over the former
1206 checking for bits set in the bitmap. */
1207 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
1208 int dup_detect = 0;
1210 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
1212 /* The current TU's bindings must be visible, we don't
1213 need to check the bitmaps. */
1215 if (!deduping)
1217 if (named_module_purview_p ())
1219 dup_detect |= 2;
1221 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
1222 dup_detect |= 1;
1224 else
1225 dup_detect |= 1;
1228 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
1231 /* Scan the imported bindings. */
1232 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
1233 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
1235 ix--;
1236 cluster++;
1239 /* Do this in forward order, so we load modules in an order
1240 the user expects. */
1241 for (; ix--; cluster++)
1242 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
1244 /* Functions are never on merged slots. */
1245 if (!cluster->indices[jx].base
1246 || cluster->indices[jx].span != 1)
1247 continue;
1249 /* Is this slot visible? */
1250 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
1251 continue;
1253 /* Is it loaded. */
1254 if (cluster->slots[jx].is_lazy ())
1255 lazy_load_binding (cluster->indices[jx].base,
1256 scope, name, &cluster->slots[jx]);
1258 tree bind = cluster->slots[jx];
1259 if (!bind)
1260 /* Load errors could mean there's nothing here. */
1261 continue;
1263 if (STAT_HACK_P (bind))
1265 if (!deduping)
1267 /* Do we need to engage deduplication? */
1268 int dup = 0;
1269 if (MODULE_BINDING_GLOBAL_P (bind))
1270 dup = 1;
1271 else if (MODULE_BINDING_PARTITION_P (bind))
1272 dup = 2;
1273 if (unsigned hit = dup_detect & dup)
1274 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
1275 || (hit & 2
1276 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
1277 dedup (true);
1278 dup_detect |= dup;
1281 bind = STAT_VISIBLE (bind);
1284 add_fns (bind);
1290 /* Add the hidden friends of SCOPE. */
1292 void
1293 name_lookup::adl_class_fns (tree type)
1295 /* Add friends. */
1296 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
1297 list; list = TREE_CHAIN (list))
1298 if (name == FRIEND_NAME (list))
1300 tree context = NULL_TREE; /* Lazily computed. */
1301 for (tree friends = FRIEND_DECLS (list); friends;
1302 friends = TREE_CHAIN (friends))
1304 tree fn = TREE_VALUE (friends);
1306 /* Only interested in global functions with potentially hidden
1307 (i.e. unqualified) declarations. */
1308 if (!context)
1309 context = decl_namespace_context (type);
1310 if (CP_DECL_CONTEXT (fn) != context)
1311 continue;
1313 dedup (true);
1315 /* Template specializations are never found by name lookup.
1316 (Templates themselves can be found, but not template
1317 specializations.) */
1318 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
1319 continue;
1321 add_fns (fn);
1326 /* Find the containing non-inlined namespace, add it and all its
1327 inlinees. */
1329 void
1330 name_lookup::adl_namespace (tree scope)
1332 if (see_and_mark (scope))
1333 return;
1335 /* Look down into inline namespaces. */
1336 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1337 for (unsigned ix = inlinees->length (); ix--;)
1338 adl_namespace ((*inlinees)[ix]);
1340 if (DECL_NAMESPACE_INLINE_P (scope))
1341 /* Mark parent. */
1342 adl_namespace (CP_DECL_CONTEXT (scope));
1345 /* Adds the class and its friends to the lookup structure. */
1347 void
1348 name_lookup::adl_class_only (tree type)
1350 /* Backend-built structures, such as __builtin_va_list, aren't
1351 affected by all this. */
1352 if (!CLASS_TYPE_P (type))
1353 return;
1355 type = TYPE_MAIN_VARIANT (type);
1357 if (see_and_mark (type))
1358 return;
1360 tree context = decl_namespace_context (type);
1361 adl_namespace (context);
1364 /* Adds the class and its bases to the lookup structure.
1365 Returns true on error. */
1367 void
1368 name_lookup::adl_bases (tree type)
1370 adl_class_only (type);
1372 /* Process baseclasses. */
1373 if (tree binfo = TYPE_BINFO (type))
1375 tree base_binfo;
1376 int i;
1378 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1379 adl_bases (BINFO_TYPE (base_binfo));
1383 /* Adds everything associated with a class argument type to the lookup
1384 structure.
1386 If T is a class type (including unions), its associated classes are: the
1387 class itself; the class of which it is a member, if any; and its direct
1388 and indirect base classes. Its associated namespaces are the namespaces
1389 of which its associated classes are members. Furthermore, if T is a
1390 class template specialization, its associated namespaces and classes
1391 also include: the namespaces and classes associated with the types of
1392 the template arguments provided for template type parameters (excluding
1393 template template parameters); the namespaces of which any template
1394 template arguments are members; and the classes of which any member
1395 templates used as template template arguments are members. [ Note:
1396 non-type template arguments do not contribute to the set of associated
1397 namespaces. --end note] */
1399 void
1400 name_lookup::adl_class (tree type)
1402 /* Backend build structures, such as __builtin_va_list, aren't
1403 affected by all this. */
1404 if (!CLASS_TYPE_P (type))
1405 return;
1407 type = TYPE_MAIN_VARIANT (type);
1409 /* We don't set found here because we have to have set seen first,
1410 which is done in the adl_bases walk. */
1411 if (found_p (type))
1412 return;
1414 complete_type (type);
1415 adl_bases (type);
1416 mark_found (type);
1418 if (TYPE_CLASS_SCOPE_P (type))
1419 adl_class_only (TYPE_CONTEXT (type));
1421 /* Process template arguments. */
1422 if (CLASSTYPE_TEMPLATE_INFO (type)
1423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
1425 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1426 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
1427 adl_template_arg (TREE_VEC_ELT (list, i));
1431 void
1432 name_lookup::adl_enum (tree type)
1434 type = TYPE_MAIN_VARIANT (type);
1435 if (see_and_mark (type))
1436 return;
1438 if (TYPE_CLASS_SCOPE_P (type))
1439 adl_class_only (TYPE_CONTEXT (type));
1440 else
1441 adl_namespace (decl_namespace_context (type));
1444 void
1445 name_lookup::adl_expr (tree expr)
1447 if (!expr)
1448 return;
1450 gcc_assert (!TYPE_P (expr));
1452 if (TREE_TYPE (expr) != unknown_type_node)
1454 adl_type (unlowered_expr_type (expr));
1455 return;
1458 if (TREE_CODE (expr) == ADDR_EXPR)
1459 expr = TREE_OPERAND (expr, 0);
1460 if (TREE_CODE (expr) == COMPONENT_REF
1461 || TREE_CODE (expr) == OFFSET_REF)
1462 expr = TREE_OPERAND (expr, 1);
1463 expr = MAYBE_BASELINK_FUNCTIONS (expr);
1465 if (OVL_P (expr))
1466 for (lkp_iterator iter (expr); iter; ++iter)
1467 adl_type (TREE_TYPE (*iter));
1468 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
1470 /* The working paper doesn't currently say how to handle
1471 template-id arguments. The sensible thing would seem to be
1472 to handle the list of template candidates like a normal
1473 overload set, and handle the template arguments like we do
1474 for class template specializations. */
1476 /* First the templates. */
1477 adl_expr (TREE_OPERAND (expr, 0));
1479 /* Now the arguments. */
1480 if (tree args = TREE_OPERAND (expr, 1))
1481 for (int ix = TREE_VEC_LENGTH (args); ix--;)
1482 adl_template_arg (TREE_VEC_ELT (args, ix));
1486 void
1487 name_lookup::adl_type (tree type)
1489 if (!type)
1490 return;
1492 if (TYPE_PTRDATAMEM_P (type))
1494 /* Pointer to member: associate class type and value type. */
1495 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
1496 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1497 return;
1500 switch (TREE_CODE (type))
1502 case RECORD_TYPE:
1503 if (TYPE_PTRMEMFUNC_P (type))
1505 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
1506 return;
1508 /* FALLTHRU */
1509 case UNION_TYPE:
1510 adl_class (type);
1511 return;
1513 case METHOD_TYPE:
1514 /* The basetype is referenced in the first arg type, so just
1515 fall through. */
1516 case FUNCTION_TYPE:
1517 /* Associate the parameter types. */
1518 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
1519 adl_type (TREE_VALUE (args));
1520 /* FALLTHROUGH */
1522 case POINTER_TYPE:
1523 case REFERENCE_TYPE:
1524 case ARRAY_TYPE:
1525 adl_type (TREE_TYPE (type));
1526 return;
1528 case ENUMERAL_TYPE:
1529 adl_enum (type);
1530 return;
1532 case LANG_TYPE:
1533 gcc_assert (type == unknown_type_node
1534 || type == init_list_type_node);
1535 return;
1537 case TYPE_PACK_EXPANSION:
1538 adl_type (PACK_EXPANSION_PATTERN (type));
1539 return;
1541 default:
1542 break;
1546 /* Adds everything associated with a template argument to the lookup
1547 structure. */
1549 void
1550 name_lookup::adl_template_arg (tree arg)
1552 /* [basic.lookup.koenig]
1554 If T is a template-id, its associated namespaces and classes are
1555 ... the namespaces and classes associated with the types of the
1556 template arguments provided for template type parameters
1557 (excluding template template parameters); the namespaces in which
1558 any template template arguments are defined; and the classes in
1559 which any member templates used as template template arguments
1560 are defined. [Note: non-type template arguments do not
1561 contribute to the set of associated namespaces. ] */
1563 /* Consider first template template arguments. */
1564 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1565 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1567 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1569 tree ctx = CP_DECL_CONTEXT (arg);
1571 /* It's not a member template. */
1572 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1573 adl_namespace (ctx);
1574 /* Otherwise, it must be member template. */
1575 else
1576 adl_class_only (ctx);
1578 /* It's an argument pack; handle it recursively. */
1579 else if (ARGUMENT_PACK_P (arg))
1581 tree args = ARGUMENT_PACK_ARGS (arg);
1582 int i, len = TREE_VEC_LENGTH (args);
1583 for (i = 0; i < len; ++i)
1584 adl_template_arg (TREE_VEC_ELT (args, i));
1586 /* It's not a template template argument, but it is a type template
1587 argument. */
1588 else if (TYPE_P (arg))
1589 adl_type (arg);
1592 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1593 the call arguments. */
1595 tree
1596 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1598 gcc_checking_assert (!vec_safe_length (scopes));
1600 /* Gather each associated entity onto the lookup's scope list. */
1601 unsigned ix;
1602 tree arg;
1604 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1605 /* OMP reduction operators put an ADL-significant type as the
1606 first arg. */
1607 if (TYPE_P (arg))
1608 adl_type (arg);
1609 else
1610 adl_expr (arg);
1612 if (vec_safe_length (scopes))
1614 /* Now do the lookups. */
1615 value = fns;
1616 if (fns)
1617 dedup (true);
1619 /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1620 bitmap inst_path = NULL;
1621 /* VISIBLE is the regular import bitmap. */
1622 bitmap visible = visible_instantiation_path (&inst_path);
1624 for (unsigned ix = scopes->length (); ix--;)
1626 tree scope = (*scopes)[ix];
1627 if (TREE_CODE (scope) == NAMESPACE_DECL)
1628 adl_namespace_fns (scope, visible);
1629 else
1631 if (RECORD_OR_UNION_TYPE_P (scope))
1632 adl_class_fns (scope);
1634 /* During 2nd phase ADL: Any exported declaration D in N
1635 declared within the purview of a named module M
1636 (10.2) is visible if there is an associated entity
1637 attached to M with the same innermost enclosing
1638 non-inline namespace as D.
1639 [basic.lookup.argdep]/4.4 */
1641 if (!inst_path)
1642 /* Not 2nd phase. */
1643 continue;
1645 tree ctx = CP_DECL_CONTEXT (TYPE_NAME (scope));
1646 if (TREE_CODE (ctx) != NAMESPACE_DECL)
1647 /* Not namespace-scope class. */
1648 continue;
1650 tree origin = get_originating_module_decl (TYPE_NAME (scope));
1651 tree not_tmpl = STRIP_TEMPLATE (origin);
1652 if (!DECL_LANG_SPECIFIC (not_tmpl)
1653 || !DECL_MODULE_IMPORT_P (not_tmpl))
1654 /* Not imported. */
1655 continue;
1657 unsigned module = get_importing_module (origin);
1659 if (!bitmap_bit_p (inst_path, module))
1660 /* Not on path of instantiation. */
1661 continue;
1663 if (bitmap_bit_p (visible, module))
1664 /* If the module was in the visible set, we'll look at
1665 its namespace partition anyway. */
1666 continue;
1668 if (tree *slot = find_namespace_slot (ctx, name, false))
1669 if (binding_slot *mslot = search_imported_binding_slot (slot, module))
1671 if (mslot->is_lazy ())
1672 lazy_load_binding (module, ctx, name, mslot);
1674 if (tree bind = *mslot)
1676 /* We must turn on deduping, because some other class
1677 from this module might also be in this namespace. */
1678 dedup (true);
1680 /* Add the exported fns */
1681 if (STAT_HACK_P (bind))
1682 add_fns (STAT_VISIBLE (bind));
1688 fns = value;
1689 dedup (false);
1692 return fns;
1695 static bool qualified_namespace_lookup (tree, name_lookup *);
1696 static void consider_binding_level (tree name,
1697 best_match <tree, const char *> &bm,
1698 cp_binding_level *lvl,
1699 bool look_within_fields,
1700 enum lookup_name_fuzzy_kind kind);
1702 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1703 don't add duplicates to it. ARGS is the vector of call
1704 arguments (which will not be empty). */
1706 tree
1707 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1709 auto_cond_timevar tv (TV_NAME_LOOKUP);
1710 name_lookup lookup (name);
1711 return lookup.search_adl (fns, args);
1714 /* FNS is an overload set of conversion functions. Return the
1715 overloads converting to TYPE. */
1717 static tree
1718 extract_conversion_operator (tree fns, tree type)
1720 tree convs = NULL_TREE;
1721 tree tpls = NULL_TREE;
1723 for (ovl_iterator iter (fns); iter; ++iter)
1725 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1726 convs = lookup_add (*iter, convs);
1728 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1729 tpls = lookup_add (*iter, tpls);
1732 if (!convs)
1733 convs = tpls;
1735 return convs;
1738 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1740 static tree
1741 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1743 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1745 unsigned mid = (lo + hi) / 2;
1746 tree binding = (*member_vec)[mid];
1747 tree binding_name = OVL_NAME (binding);
1749 if (binding_name > name)
1750 hi = mid;
1751 else if (binding_name < name)
1752 lo = mid + 1;
1753 else
1754 return binding;
1757 return NULL_TREE;
1760 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1762 static tree
1763 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1765 for (int ix = member_vec->length (); ix--;)
1766 if (tree binding = (*member_vec)[ix])
1767 if (OVL_NAME (binding) == name)
1768 return binding;
1770 return NULL_TREE;
1773 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1775 static tree
1776 fields_linear_search (tree klass, tree name, bool want_type)
1778 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1780 tree decl = fields;
1782 if (TREE_CODE (decl) == FIELD_DECL
1783 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1785 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1786 return temp;
1789 if (DECL_NAME (decl) != name)
1790 continue;
1792 if (TREE_CODE (decl) == USING_DECL)
1794 decl = strip_using_decl (decl);
1795 if (is_overloaded_fn (decl))
1796 continue;
1799 if (DECL_DECLARES_FUNCTION_P (decl))
1800 /* Functions are found separately. */
1801 continue;
1803 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1804 return decl;
1807 return NULL_TREE;
1810 /* Like fields_linear_search, but specific for "_" name. There can be multiple
1811 name-independent non-static data members and in that case a TREE_LIST with the
1812 ambiguous decls should be returned. */
1814 static tree
1815 name_independent_linear_search (tree val, tree klass, tree name)
1817 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1819 tree decl = fields;
1821 if (TREE_CODE (decl) == FIELD_DECL
1822 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1824 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, false))
1826 decl = temp;
1827 goto add;
1831 if (DECL_NAME (decl) != name)
1832 continue;
1834 if (TREE_CODE (decl) == USING_DECL)
1836 decl = strip_using_decl (decl);
1837 if (is_overloaded_fn (decl))
1838 continue;
1841 if (DECL_DECLARES_FUNCTION_P (decl))
1842 /* Functions are found separately. */
1843 continue;
1845 add:
1846 if (val == NULL_TREE)
1847 val = decl;
1848 else
1850 if (TREE_CODE (val) != TREE_LIST)
1852 if (TREE_CODE (val) == OVERLOAD
1853 && OVL_DEDUP_P (val)
1854 && TREE_CODE (decl) == USING_DECL)
1856 val = ovl_make (decl, val);
1857 continue;
1859 val = tree_cons (NULL_TREE, val, NULL_TREE);
1860 TREE_TYPE (val) = error_mark_node;
1862 if (TREE_CODE (decl) == TREE_LIST)
1863 val = chainon (decl, val);
1864 else
1866 val = tree_cons (NULL_TREE, decl, val);
1867 TREE_TYPE (val) = error_mark_node;
1872 return val;
1875 /* Look for NAME member inside of anonymous aggregate ANON. Although
1876 such things should only contain FIELD_DECLs, we check that too
1877 late, and would give very confusing errors if we weren't
1878 permissive here. */
1880 tree
1881 search_anon_aggr (tree anon, tree name, bool want_type)
1883 gcc_assert (COMPLETE_TYPE_P (anon));
1884 tree ret = get_class_binding_direct (anon, name, want_type);
1885 return ret;
1888 /* Look for NAME as an immediate member of KLASS (including
1889 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1890 regular search. >0 to get a type binding (if there is one) and <0
1891 if you want (just) the member function binding.
1893 Use this if you do not want lazy member creation. */
1895 tree
1896 get_class_binding_direct (tree klass, tree name, bool want_type)
1898 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1900 /* Conversion operators can only be found by the marker conversion
1901 operator name. */
1902 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1903 tree lookup = conv_op ? conv_op_identifier : name;
1904 tree val = NULL_TREE;
1905 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1907 if (COMPLETE_TYPE_P (klass) && member_vec)
1909 val = member_vec_binary_search (member_vec, lookup);
1910 if (!val)
1912 else if (TREE_CODE (val) == OVERLOAD
1913 && OVL_NAME_INDEPENDENT_DECL_P (val))
1915 if (want_type)
1917 while (TREE_CODE (val) == OVERLOAD
1918 && OVL_NAME_INDEPENDENT_DECL_P (val))
1919 val = OVL_CHAIN (val);
1920 if (STAT_HACK_P (val))
1921 val = STAT_TYPE (val);
1922 else if (!DECL_DECLARES_TYPE_P (val))
1923 val = NULL_TREE;
1925 else
1927 /* OVERLOAD with a special OVL_NAME_INDEPENDENT_DECL_P
1928 flag is used under the hood to represent lookup
1929 results which include name-independent declarations,
1930 and get_class_binding_direct is turning that into
1931 TREE_LIST representation (which the callers expect for
1932 ambiguous lookups) instead.
1933 There are 2 reasons for that:
1934 1) in order to keep the member_vec binary search fast, I
1935 think it is better to keep OVL_NAME usable on all elements
1936 because having to special case TREE_LIST would slow
1937 everything down;
1938 2) the callers need to be able to chain the results anyway
1939 and so need an unshared TREE_LIST they can tweak/destroy. */
1940 tree ovl = val;
1941 val = NULL_TREE;
1942 while (TREE_CODE (ovl) == OVERLOAD
1943 && OVL_NAME_INDEPENDENT_DECL_P (ovl))
1945 val = tree_cons (NULL_TREE, OVL_FUNCTION (ovl), val);
1946 TREE_TYPE (val) = error_mark_node;
1947 ovl = OVL_CHAIN (ovl);
1949 if (STAT_HACK_P (ovl))
1950 val = tree_cons (NULL_TREE, STAT_DECL (ovl), val);
1951 else
1952 val = tree_cons (NULL_TREE, ovl, val);
1953 TREE_TYPE (val) = error_mark_node;
1956 else if (STAT_HACK_P (val))
1957 val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
1958 else if (want_type && !DECL_DECLARES_TYPE_P (val))
1959 val = NULL_TREE;
1961 else
1963 if (member_vec && !want_type)
1964 val = member_vec_linear_search (member_vec, lookup);
1966 if (id_equal (lookup, "_") && !want_type)
1967 val = name_independent_linear_search (val, klass, lookup);
1968 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
1969 /* Dependent using declarations are a 'field', make sure we
1970 return that even if we saw an overload already. */
1971 if (tree field_val = fields_linear_search (klass, lookup, want_type))
1973 if (!val)
1974 val = field_val;
1975 else if (TREE_CODE (field_val) == USING_DECL)
1976 val = ovl_make (field_val, val);
1980 /* Extract the conversion operators asked for, unless the general
1981 conversion operator was requested. */
1982 if (val && conv_op)
1984 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1985 val = OVL_CHAIN (val);
1986 if (tree type = TREE_TYPE (name))
1987 val = extract_conversion_operator (val, type);
1990 return val;
1993 /* We're about to lookup NAME in KLASS. Make sure any lazily declared
1994 members are now declared. */
1996 static void
1997 maybe_lazily_declare (tree klass, tree name)
1999 /* See big comment anout module_state::write_pendings regarding adding a check
2000 bit. */
2001 if (modules_p ())
2002 lazy_load_pendings (TYPE_NAME (klass));
2004 /* Lazily declare functions, if we're going to search these. */
2005 if (IDENTIFIER_CTOR_P (name))
2007 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
2008 lazily_declare_fn (sfk_constructor, klass);
2009 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
2010 lazily_declare_fn (sfk_copy_constructor, klass);
2011 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
2012 lazily_declare_fn (sfk_move_constructor, klass);
2014 else if (IDENTIFIER_DTOR_P (name))
2016 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
2017 lazily_declare_fn (sfk_destructor, klass);
2019 else if (name == assign_op_identifier)
2021 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
2022 lazily_declare_fn (sfk_copy_assignment, klass);
2023 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
2024 lazily_declare_fn (sfk_move_assignment, klass);
2028 /* Look for NAME's binding in exactly KLASS. See
2029 get_class_binding_direct for argument description. Does lazy
2030 special function creation as necessary. */
2032 tree
2033 get_class_binding (tree klass, tree name, bool want_type /*=false*/)
2035 klass = complete_type (klass);
2037 if (COMPLETE_TYPE_P (klass))
2038 maybe_lazily_declare (klass, name);
2040 return get_class_binding_direct (klass, name, want_type);
2043 /* Find the slot containing overloads called 'NAME'. If there is no
2044 such slot and the class is complete, create an empty one, at the
2045 correct point in the sorted member vector. Otherwise return NULL.
2046 Deals with conv_op marker handling. */
2048 tree *
2049 find_member_slot (tree klass, tree name)
2051 bool complete_p = COMPLETE_TYPE_P (klass);
2053 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2054 if (!member_vec)
2056 vec_alloc (member_vec, 8);
2057 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2058 if (complete_p)
2059 /* If the class is complete but had no member_vec, we need to
2060 add the TYPE_FIELDS into it. We're also most likely to be
2061 adding ctors & dtors, so ask for 6 spare slots (the
2062 abstract cdtors and their clones). */
2063 member_vec = set_class_bindings (klass, 6);
2066 if (IDENTIFIER_CONV_OP_P (name))
2067 name = conv_op_identifier;
2069 unsigned ix, length = member_vec->length ();
2070 for (ix = 0; ix < length; ix++)
2072 tree *slot = &(*member_vec)[ix];
2073 tree fn_name = OVL_NAME (*slot);
2075 if (fn_name == name)
2077 /* If we found an existing slot, it must be a function set.
2078 Even with insertion after completion, because those only
2079 happen with artificial fns that have unspellable names.
2080 This means we do not have to deal with the stat hack
2081 either. */
2082 gcc_checking_assert (OVL_P (*slot));
2083 if (name == conv_op_identifier)
2085 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
2086 /* Skip the conv-op marker. */
2087 slot = &OVL_CHAIN (*slot);
2089 return slot;
2092 if (complete_p && fn_name > name)
2093 break;
2096 /* No slot found, add one if the class is complete. */
2097 if (complete_p)
2099 /* Do exact allocation, as we don't expect to add many. */
2100 gcc_assert (name != conv_op_identifier);
2101 vec_safe_reserve_exact (member_vec, 1);
2102 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2103 member_vec->quick_insert (ix, NULL_TREE);
2104 return &(*member_vec)[ix];
2107 return NULL;
2110 /* KLASS is an incomplete class to which we're adding a method NAME.
2111 Add a slot and deal with conv_op marker handling. */
2113 tree *
2114 add_member_slot (tree klass, tree name)
2116 gcc_assert (!COMPLETE_TYPE_P (klass));
2118 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2119 vec_safe_push (member_vec, NULL_TREE);
2120 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2122 tree *slot = &member_vec->last ();
2123 if (IDENTIFIER_CONV_OP_P (name))
2125 /* Install the marker prefix. */
2126 *slot = ovl_make (conv_op_marker, NULL_TREE);
2127 slot = &OVL_CHAIN (*slot);
2130 return slot;
2133 /* Comparison function to compare two MEMBER_VEC entries by name.
2134 Because we can have duplicates during insertion of TYPE_FIELDS, we
2135 do extra checking so deduping doesn't have to deal with so many
2136 cases. */
2138 static int
2139 member_name_cmp (const void *a_p, const void *b_p)
2141 tree a = *(const tree *)a_p;
2142 tree b = *(const tree *)b_p;
2143 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
2144 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
2146 gcc_checking_assert (name_a && name_b);
2147 if (name_a != name_b)
2148 return name_a < name_b ? -1 : +1;
2150 if (name_a == conv_op_identifier)
2152 /* Strip the conv-op markers. */
2153 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
2154 && OVL_FUNCTION (b) == conv_op_marker);
2155 a = OVL_CHAIN (a);
2156 b = OVL_CHAIN (b);
2159 if (TREE_CODE (a) == OVERLOAD)
2160 a = OVL_FUNCTION (a);
2161 if (TREE_CODE (b) == OVERLOAD)
2162 b = OVL_FUNCTION (b);
2164 if (id_equal (name_a, "_"))
2166 /* Sort name-independent members first. */
2167 if (name_independent_decl_p (a))
2169 if (name_independent_decl_p (b))
2171 if (DECL_UID (a) != DECL_UID (b))
2172 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
2173 gcc_assert (a == b);
2174 return 0;
2176 else
2177 return -1;
2179 else if (name_independent_decl_p (b))
2180 return +1;
2183 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
2184 if (TREE_CODE (a) != TREE_CODE (b))
2186 /* If one of them is a TYPE_DECL, it loses. */
2187 if (TREE_CODE (a) == TYPE_DECL)
2188 return +1;
2189 else if (TREE_CODE (b) == TYPE_DECL)
2190 return -1;
2192 /* If one of them is a USING_DECL, it loses. */
2193 if (TREE_CODE (a) == USING_DECL)
2194 return +1;
2195 else if (TREE_CODE (b) == USING_DECL)
2196 return -1;
2198 /* There are no other cases with different kinds of decls, as
2199 duplicate detection should have kicked in earlier. However,
2200 some erroneous cases get though. */
2201 gcc_assert (errorcount);
2204 /* Using source location would be the best thing here, but we can
2205 get identically-located decls in the following circumstances:
2207 1) duplicate artificial type-decls for the same type.
2209 2) pack expansions of using-decls.
2211 We should not be doing #1, but in either case it doesn't matter
2212 how we order these. Use UID as a proxy for source ordering, so
2213 that identically-located decls still have a well-defined stable
2214 ordering. */
2215 if (DECL_UID (a) != DECL_UID (b))
2216 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
2217 gcc_assert (a == b);
2218 return 0;
2221 static struct {
2222 gt_pointer_operator new_value;
2223 void *cookie;
2224 } resort_data;
2226 /* This routine compares two fields like member_name_cmp but using the
2227 pointer operator in resort_field_decl_data. We don't have to deal
2228 with duplicates here. */
2230 static int
2231 resort_member_name_cmp (const void *a_p, const void *b_p)
2233 tree a = *(const tree *)a_p;
2234 tree b = *(const tree *)b_p;
2235 tree name_a = OVL_NAME (a);
2236 tree name_b = OVL_NAME (b);
2238 resort_data.new_value (&name_a, &name_a, resort_data.cookie);
2239 resort_data.new_value (&name_b, &name_b, resort_data.cookie);
2241 gcc_checking_assert (name_a != name_b);
2243 return name_a < name_b ? -1 : +1;
2246 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2248 void
2249 resort_type_member_vec (void *obj, void */*orig_obj*/,
2250 gt_pointer_operator new_value, void* cookie)
2252 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
2254 resort_data.new_value = new_value;
2255 resort_data.cookie = cookie;
2256 member_vec->qsort (resort_member_name_cmp);
2260 /* Recursively count the number of fields in KLASS, including anonymous
2261 union members. */
2263 static unsigned
2264 count_class_fields (tree klass)
2266 unsigned n_fields = 0;
2268 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2269 if (DECL_DECLARES_FUNCTION_P (fields))
2270 /* Functions are dealt with separately. */;
2271 else if (TREE_CODE (fields) == FIELD_DECL
2272 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2273 n_fields += count_class_fields (TREE_TYPE (fields));
2274 else if (DECL_NAME (fields))
2275 n_fields += 1;
2277 return n_fields;
2280 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
2281 Recurse for anonymous members. MEMBER_VEC must have space. */
2283 static void
2284 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
2286 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2287 if (DECL_DECLARES_FUNCTION_P (fields))
2288 /* Functions are handled separately. */;
2289 else if (TREE_CODE (fields) == FIELD_DECL
2290 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2291 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
2292 else if (DECL_NAME (fields))
2294 tree field = fields;
2295 /* Mark a conv-op USING_DECL with the conv-op-marker. */
2296 if (TREE_CODE (field) == USING_DECL
2297 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
2298 field = ovl_make (conv_op_marker, field);
2299 member_vec->quick_push (field);
2303 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
2304 MEMBER_VEC must have space. */
2306 static void
2307 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
2309 for (tree values = TYPE_VALUES (enumtype);
2310 values; values = TREE_CHAIN (values))
2311 member_vec->quick_push (TREE_VALUE (values));
2314 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
2315 DeDup adjacent DECLS of the same name. We already dealt with
2316 conflict resolution when adding the fields or methods themselves.
2317 There are four cases (which could all be combined):
2318 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
2319 2) a USING_DECL and an overload. If the USING_DECL is dependent,
2320 it wins. Otherwise the OVERLOAD does.
2321 3) two USING_DECLS.
2322 4) name-independent members plus others. ...
2324 member_name_cmp will have ordered duplicates as
2325 <name_independent><fns><using><type> */
2327 static void
2328 member_vec_dedup (vec<tree, va_gc> *member_vec)
2330 unsigned len = member_vec->length ();
2331 unsigned store = 0;
2333 if (!len)
2334 return;
2336 tree name = OVL_NAME ((*member_vec)[0]);
2337 for (unsigned jx, ix = 0; ix < len; ix = jx)
2339 tree current = NULL_TREE;
2340 tree to_type = NULL_TREE;
2341 tree to_using = NULL_TREE;
2342 tree marker = NULL_TREE;
2343 unsigned name_independent = ix;
2345 for (jx = ix; jx < len; jx++)
2347 tree next = (*member_vec)[jx];
2348 if (jx != ix)
2350 tree next_name = OVL_NAME (next);
2351 if (next_name != name)
2353 name = next_name;
2354 break;
2358 if (IDENTIFIER_CONV_OP_P (name))
2360 marker = next;
2361 next = OVL_CHAIN (next);
2364 if (TREE_CODE (next) == USING_DECL)
2366 if (IDENTIFIER_CTOR_P (name))
2367 /* Dependent inherited ctor. */
2368 continue;
2370 next = strip_using_decl (next);
2371 if (TREE_CODE (next) == USING_DECL)
2373 to_using = next;
2374 continue;
2377 if (is_overloaded_fn (next))
2378 continue;
2381 if (DECL_DECLARES_TYPE_P (next))
2383 to_type = next;
2384 continue;
2387 if (name_independent_decl_p (next))
2388 name_independent = jx + 1;
2389 else if (!current)
2390 current = next;
2393 if (to_using)
2395 if (!current)
2396 current = to_using;
2397 else
2398 current = ovl_make (to_using, current);
2401 if (to_type)
2403 if (!current)
2404 current = to_type;
2405 else
2406 current = stat_hack (current, to_type);
2409 for (unsigned kx = name_independent; kx > ix; --kx)
2410 if (!current)
2411 current = (*member_vec)[kx - 1];
2412 else if (current == to_type)
2413 current = stat_hack ((*member_vec)[kx - 1], to_type);
2414 else
2416 current = ovl_make ((*member_vec)[kx - 1], current);
2417 OVL_NAME_INDEPENDENT_DECL_P (current) = 1;
2420 if (current)
2422 if (marker)
2424 OVL_CHAIN (marker) = current;
2425 current = marker;
2427 (*member_vec)[store++] = current;
2431 while (store++ < len)
2432 member_vec->pop ();
2435 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
2436 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
2437 know there must be at least 1 field -- the self-reference
2438 TYPE_DECL, except for anon aggregates, which will have at least
2439 one field anyway. If EXTRA < 0, always create the vector. */
2441 vec<tree, va_gc> *
2442 set_class_bindings (tree klass, int extra)
2444 unsigned n_fields = count_class_fields (klass);
2445 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2447 if (member_vec || n_fields >= 8 || extra < 0)
2449 /* Append the new fields. */
2450 vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
2451 member_vec_append_class_fields (member_vec, klass);
2454 if (member_vec)
2456 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2457 member_vec->qsort (member_name_cmp);
2458 member_vec_dedup (member_vec);
2461 return member_vec;
2464 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
2466 void
2467 insert_late_enum_def_bindings (tree klass, tree enumtype)
2469 int n_fields;
2470 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2472 /* The enum bindings will already be on the TYPE_FIELDS, so don't
2473 count them twice. */
2474 if (!member_vec)
2475 n_fields = count_class_fields (klass);
2476 else
2477 n_fields = list_length (TYPE_VALUES (enumtype));
2479 if (member_vec || n_fields >= 8)
2481 vec_safe_reserve_exact (member_vec, n_fields);
2482 if (CLASSTYPE_MEMBER_VEC (klass))
2483 member_vec_append_enum_values (member_vec, enumtype);
2484 else
2485 member_vec_append_class_fields (member_vec, klass);
2486 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2487 member_vec->qsort (member_name_cmp);
2488 member_vec_dedup (member_vec);
2492 /* The binding oracle; see cp-tree.h. */
2494 cp_binding_oracle_function *cp_binding_oracle;
2496 /* If we have a binding oracle, ask it for all namespace-scoped
2497 definitions of NAME. */
2499 static inline void
2500 query_oracle (tree name)
2502 if (!cp_binding_oracle)
2503 return;
2505 /* LOOKED_UP holds the set of identifiers that we have already
2506 looked up with the oracle. */
2507 static hash_set<tree> looked_up;
2508 if (looked_up.add (name))
2509 return;
2511 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
2514 #ifndef ENABLE_SCOPE_CHECKING
2515 # define ENABLE_SCOPE_CHECKING 0
2516 #else
2517 # define ENABLE_SCOPE_CHECKING 1
2518 #endif
2520 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
2522 static GTY((deletable)) cxx_binding *free_bindings;
2524 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
2525 field to NULL. */
2527 static inline void
2528 cxx_binding_init (cxx_binding *binding, tree value, tree type)
2530 binding->value = value;
2531 binding->type = type;
2532 binding->previous = NULL;
2535 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2537 static cxx_binding *
2538 cxx_binding_make (tree value, tree type)
2540 cxx_binding *binding = free_bindings;
2542 if (binding)
2543 free_bindings = binding->previous;
2544 else
2545 binding = ggc_alloc<cxx_binding> ();
2547 /* Clear flags by default. */
2548 LOCAL_BINDING_P (binding) = false;
2549 INHERITED_VALUE_BINDING_P (binding) = false;
2550 HIDDEN_TYPE_BINDING_P (binding) = false;
2552 cxx_binding_init (binding, value, type);
2554 return binding;
2557 /* Put BINDING back on the free list. */
2559 static inline void
2560 cxx_binding_free (cxx_binding *binding)
2562 binding->scope = NULL;
2563 binding->previous = free_bindings;
2564 free_bindings = binding;
2567 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2568 bindings) in the class scope indicated by SCOPE. */
2570 static cxx_binding *
2571 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2573 cp_class_binding cb = {cxx_binding_make (value, type), name};
2574 cxx_binding *binding = cb.base;
2575 vec_safe_push (scope->class_shadowed, cb);
2576 binding->scope = scope;
2577 return binding;
2580 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2581 level at which this declaration is being bound. */
2583 void
2584 push_binding (tree id, tree decl, cp_binding_level* level)
2586 cxx_binding *binding;
2588 if (level != class_binding_level)
2590 binding = cxx_binding_make (decl, NULL_TREE);
2591 binding->scope = level;
2593 else
2594 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2596 /* Now, fill in the binding information. */
2597 binding->previous = IDENTIFIER_BINDING (id);
2598 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2600 /* And put it on the front of the list of bindings for ID. */
2601 IDENTIFIER_BINDING (id) = binding;
2604 /* Remove the binding for DECL which should be the innermost binding
2605 for ID. */
2607 void
2608 pop_local_binding (tree id, tree decl)
2610 if (!id || IDENTIFIER_ANON_P (id))
2611 /* It's easiest to write the loops that call this function without
2612 checking whether or not the entities involved have names. We
2613 get here for such an entity. */
2614 return;
2616 /* Get the innermost binding for ID. */
2617 cxx_binding *binding = IDENTIFIER_BINDING (id);
2619 /* The name should be bound. */
2620 gcc_assert (binding != NULL);
2622 /* The DECL will be either the ordinary binding or the type binding
2623 for this identifier. Remove that binding. We don't have to
2624 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2625 away. */
2626 if (binding->value == decl)
2627 binding->value = NULL_TREE;
2628 else if (binding->type == decl)
2629 binding->type = NULL_TREE;
2630 else
2632 /* Name-independent variable was found after at least one declaration
2633 with the same name. */
2634 gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
2635 if (TREE_VALUE (binding->value) != decl)
2637 binding->value = nreverse (binding->value);
2638 /* Skip over TREE_LISTs added in pushdecl for check_local_shadow
2639 detected declarations, formerly at the tail, now at the start
2640 of the list. */
2641 while (TREE_PURPOSE (binding->value) == error_mark_node)
2642 binding->value = TREE_CHAIN (binding->value);
2644 gcc_assert (TREE_VALUE (binding->value) == decl);
2645 binding->value = TREE_CHAIN (binding->value);
2646 while (binding->value
2647 && TREE_PURPOSE (binding->value) == error_mark_node)
2648 binding->value = TREE_CHAIN (binding->value);
2651 if (!binding->value && !binding->type)
2653 /* We're completely done with the innermost binding for this
2654 identifier. Unhook it from the list of bindings. */
2655 IDENTIFIER_BINDING (id) = binding->previous;
2657 /* Add it to the free list. */
2658 cxx_binding_free (binding);
2662 /* Remove the bindings for the decls of the current level and leave
2663 the current scope. */
2665 void
2666 pop_bindings_and_leave_scope (void)
2668 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2670 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2671 tree name = OVL_NAME (decl);
2673 pop_local_binding (name, decl);
2676 leave_scope ();
2679 /* Strip non dependent using declarations. If DECL is dependent,
2680 surreptitiously create a typename_type and return it. */
2682 tree
2683 strip_using_decl (tree decl)
2685 if (decl == NULL_TREE)
2686 return NULL_TREE;
2688 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2689 decl = USING_DECL_DECLS (decl);
2691 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2692 && USING_DECL_TYPENAME_P (decl))
2694 /* We have found a type introduced by a using
2695 declaration at class scope that refers to a dependent
2696 type.
2698 using typename :: [opt] nested-name-specifier unqualified-id ;
2700 decl = make_typename_type (USING_DECL_SCOPE (decl),
2701 DECL_NAME (decl),
2702 typename_type, tf_error);
2703 if (decl != error_mark_node)
2704 decl = TYPE_NAME (decl);
2707 return decl;
2710 /* Return true if OVL is an overload for an anticipated builtin. */
2712 static bool
2713 anticipated_builtin_p (tree ovl)
2715 return (TREE_CODE (ovl) == OVERLOAD
2716 && OVL_HIDDEN_P (ovl)
2717 && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl)));
2720 /* BINDING records an existing declaration for a name in the current scope.
2721 But, DECL is another declaration for that same identifier in the
2722 same scope. This is the `struct stat' hack whereby a non-typedef
2723 class name or enum-name can be bound at the same level as some other
2724 kind of entity.
2725 3.3.7/1
2727 A class name (9.1) or enumeration name (7.2) can be hidden by the
2728 name of an object, function, or enumerator declared in the same scope.
2729 If a class or enumeration name and an object, function, or enumerator
2730 are declared in the same scope (in any order) with the same name, the
2731 class or enumeration name is hidden wherever the object, function, or
2732 enumerator name is visible.
2734 It's the responsibility of the caller to check that
2735 inserting this name is valid here. Returns nonzero if the new binding
2736 was successful. */
2738 static bool
2739 supplement_binding (cxx_binding *binding, tree decl)
2741 auto_cond_timevar tv (TV_NAME_LOOKUP);
2743 tree bval = binding->value;
2744 bool ok = true;
2745 if (bval
2746 && TREE_CODE (bval) == TREE_LIST
2747 && name_independent_decl_p (TREE_VALUE (bval)))
2748 bval = TREE_VALUE (bval);
2749 tree target_bval = strip_using_decl (bval);
2750 tree target_decl = strip_using_decl (decl);
2752 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2753 && target_decl != target_bval
2754 && (TREE_CODE (target_bval) != TYPE_DECL
2755 /* We allow pushing an enum multiple times in a class
2756 template in order to handle late matching of underlying
2757 type on an opaque-enum-declaration followed by an
2758 enum-specifier. */
2759 || (processing_template_decl
2760 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2761 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2762 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2763 (TREE_TYPE (target_decl)))
2764 || dependent_type_p (ENUM_UNDERLYING_TYPE
2765 (TREE_TYPE (target_bval)))))))
2766 /* The new name is the type name. */
2767 binding->type = decl;
2768 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2769 an inherited type-binding out of the way to make room
2770 for a new value binding. */
2771 !target_bval
2772 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2773 has been used in a non-class scope prior declaration.
2774 In that case, we should have already issued a
2775 diagnostic; for graceful error recovery purpose, pretend
2776 this was the intended declaration for that name. */
2777 || target_bval == error_mark_node
2778 /* If TARGET_BVAL is anticipated but has not yet been
2779 declared, pretend it is not there at all. */
2780 || anticipated_builtin_p (target_bval))
2781 binding->value = decl;
2782 else if (TREE_CODE (target_bval) == TYPE_DECL
2783 && DECL_ARTIFICIAL (target_bval)
2784 && target_decl != target_bval
2785 && (TREE_CODE (target_decl) != TYPE_DECL
2786 || same_type_p (TREE_TYPE (target_decl),
2787 TREE_TYPE (target_bval))))
2789 /* The old binding was a type name. It was placed in
2790 VALUE field because it was thought, at the point it was
2791 declared, to be the only entity with such a name. Move the
2792 type name into the type slot; it is now hidden by the new
2793 binding. */
2794 binding->type = bval;
2795 binding->value = decl;
2796 binding->value_is_inherited = false;
2798 else if (TREE_CODE (target_bval) == TYPE_DECL
2799 && TREE_CODE (target_decl) == TYPE_DECL
2800 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2801 && binding->scope->kind != sk_class
2802 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2803 /* If either type involves template parameters, we must
2804 wait until instantiation. */
2805 || uses_template_parms (TREE_TYPE (target_decl))
2806 || uses_template_parms (TREE_TYPE (target_bval))))
2807 /* We have two typedef-names, both naming the same type to have
2808 the same name. In general, this is OK because of:
2810 [dcl.typedef]
2812 In a given scope, a typedef specifier can be used to redefine
2813 the name of any type declared in that scope to refer to the
2814 type to which it already refers.
2816 However, in class scopes, this rule does not apply due to the
2817 stricter language in [class.mem] prohibiting redeclarations of
2818 members. */
2819 ok = false;
2820 /* There can be two block-scope declarations of the same variable,
2821 so long as they are `extern' declarations. However, there cannot
2822 be two declarations of the same static data member:
2824 [class.mem]
2826 A member shall not be declared twice in the
2827 member-specification. */
2828 else if (VAR_P (target_decl)
2829 && VAR_P (target_bval)
2830 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2831 && !DECL_CLASS_SCOPE_P (target_decl))
2833 duplicate_decls (decl, binding->value);
2834 ok = false;
2836 else if (TREE_CODE (decl) == NAMESPACE_DECL
2837 && TREE_CODE (bval) == NAMESPACE_DECL
2838 && DECL_NAMESPACE_ALIAS (decl)
2839 && DECL_NAMESPACE_ALIAS (bval)
2840 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2841 /* [namespace.alias]
2843 In a declarative region, a namespace-alias-definition can be
2844 used to redefine a namespace-alias declared in that declarative
2845 region to refer only to the namespace to which it already
2846 refers. */
2847 ok = false;
2848 else if (TREE_CODE (bval) == USING_DECL
2849 && CONST_DECL_USING_P (decl))
2850 /* Let the clone hide the using-decl that introduced it. */
2851 binding->value = decl;
2852 else if (name_independent_decl_p (decl))
2854 if (cxx_dialect < cxx26)
2855 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
2856 "name-independent declarations only available with "
2857 "%<-std=c++2c%> or %<-std=gnu++2c%>");
2858 binding->value = name_lookup::ambiguous (decl, binding->value);
2860 else
2862 if (!error_operand_p (bval))
2863 diagnose_name_conflict (decl, bval);
2864 ok = false;
2867 return ok;
2870 /* Diagnose a name conflict between DECL and BVAL.
2872 This is non-static so maybe_push_used_methods can use it and avoid changing
2873 the diagnostic for inherit/using4.C; otherwise it should not be used from
2874 outside this file. */
2876 void
2877 diagnose_name_conflict (tree decl, tree bval)
2879 if (TREE_CODE (decl) == TREE_CODE (bval)
2880 && TREE_CODE (decl) != NAMESPACE_DECL
2881 && !DECL_DECLARES_FUNCTION_P (decl)
2882 && (TREE_CODE (decl) != TYPE_DECL
2883 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2884 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2886 if (concept_definition_p (decl))
2887 error ("redeclaration of %q#D with different template parameters",
2888 decl);
2889 else
2890 error ("redeclaration of %q#D", decl);
2892 else
2893 error ("%q#D conflicts with a previous declaration", decl);
2895 inform (location_of (bval), "previous declaration %q#D", bval);
2898 /* Replace BINDING's current value on its scope's name list with
2899 NEWVAL. */
2901 static void
2902 update_local_overload (cxx_binding *binding, tree newval)
2904 tree *d;
2906 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2907 if (*d == binding->value)
2909 /* Stitch new list node in. */
2910 *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
2911 break;
2913 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2914 break;
2916 TREE_VALUE (*d) = newval;
2919 /* Compares the parameter-type-lists of ONE and TWO and
2920 returns false if they are different. If the DECLs are template
2921 functions, the return types and the template parameter lists are
2922 compared too (DR 565). */
2924 static bool
2925 matching_fn_p (tree one, tree two)
2927 if (TREE_CODE (one) != TREE_CODE (two))
2928 return false;
2930 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2931 TYPE_ARG_TYPES (TREE_TYPE (two))))
2932 return false;
2934 if (TREE_CODE (one) == TEMPLATE_DECL)
2936 /* Compare template parms. */
2937 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2938 DECL_TEMPLATE_PARMS (two)))
2939 return false;
2941 /* And return type. */
2942 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2943 TREE_TYPE (TREE_TYPE (two))))
2944 return false;
2947 if (!equivalently_constrained (one, two))
2948 return false;
2950 return true;
2953 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2954 binding value (possibly with anticipated builtins stripped).
2955 Diagnose conflicts and return updated decl. */
2957 static tree
2958 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2959 tree old, tree decl, bool hiding = false)
2961 tree old_type = NULL_TREE;
2962 bool hide_type = false;
2963 bool hide_value = false;
2964 bool name_independent_p = false;
2966 if (!slot)
2968 old_type = binding->type;
2969 hide_type = HIDDEN_TYPE_BINDING_P (binding);
2970 if (!old_type)
2971 hide_value = hide_type, hide_type = false;
2972 name_independent_p = name_independent_decl_p (decl);
2974 else if (STAT_HACK_P (*slot))
2976 old_type = STAT_TYPE (*slot);
2977 hide_type = STAT_TYPE_HIDDEN_P (*slot);
2978 hide_value = STAT_DECL_HIDDEN_P (*slot);
2981 tree to_val = decl;
2982 tree to_type = old_type;
2983 bool local_overload = false;
2985 gcc_assert (!level || level->kind == sk_namespace ? !binding
2986 : level->kind != sk_class && !slot);
2988 if (old == error_mark_node)
2989 old = NULL_TREE;
2991 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2993 /* Pushing an artificial decl. We should not find another
2994 artificial decl here already -- lookup_elaborated_type will
2995 have already found it. */
2996 gcc_checking_assert (!to_type
2997 && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
2999 if (old)
3001 /* Put DECL into the type slot. */
3002 gcc_checking_assert (!to_type);
3003 hide_type = hiding;
3004 to_type = decl;
3005 to_val = old;
3007 else
3008 hide_value = hiding;
3010 goto done;
3013 if (old && DECL_IMPLICIT_TYPEDEF_P (old))
3015 /* OLD is an implicit typedef. Move it to to_type. */
3016 gcc_checking_assert (!to_type);
3018 to_type = old;
3019 hide_type = hide_value;
3020 old = NULL_TREE;
3021 hide_value = false;
3024 if (DECL_DECLARES_FUNCTION_P (decl))
3026 if (!old)
3028 else if (OVL_P (old))
3030 for (ovl_iterator iter (old); iter; ++iter)
3032 tree fn = *iter;
3034 if (iter.using_p () && matching_fn_p (fn, decl))
3036 gcc_checking_assert (!iter.hidden_p ());
3037 /* If a function declaration in namespace scope or
3038 block scope has the same name and the same
3039 parameter-type- list (8.3.5) as a function
3040 introduced by a using-declaration, and the
3041 declarations do not declare the same function,
3042 the program is ill-formed. [namespace.udecl]/14 */
3043 if (tree match = duplicate_decls (decl, fn, hiding))
3044 return match;
3045 else
3046 /* FIXME: To preserve existing error behavior, we
3047 still push the decl. This might change. */
3048 diagnose_name_conflict (decl, fn);
3052 else
3053 goto conflict;
3055 if (to_type != old_type
3056 && warn_shadow
3057 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
3058 && !(DECL_IN_SYSTEM_HEADER (decl)
3059 && DECL_IN_SYSTEM_HEADER (to_type)))
3060 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
3061 decl, to_type);
3063 local_overload = old && level && level->kind != sk_namespace;
3064 to_val = ovl_insert (decl, old, -int (hiding));
3066 else if (old)
3068 if (name_independent_p)
3069 to_val = name_lookup::ambiguous (decl, old);
3070 else if (TREE_CODE (old) != TREE_CODE (decl))
3071 /* Different kinds of decls conflict. */
3072 goto conflict;
3073 else if (TREE_CODE (old) == TYPE_DECL)
3075 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3076 /* Two type decls to the same type. Do nothing. */
3077 return old;
3078 else
3079 goto conflict;
3081 else if (TREE_CODE (old) == NAMESPACE_DECL)
3083 /* Two maybe-aliased namespaces. If they're to the same target
3084 namespace, that's ok. */
3085 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
3086 goto conflict;
3088 /* The new one must be an alias at this point. */
3089 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
3090 return old;
3092 else if (TREE_CODE (old) == VAR_DECL)
3094 /* There can be two block-scope declarations of the same
3095 variable, so long as they are `extern' declarations. */
3096 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
3097 goto conflict;
3098 else if (tree match = duplicate_decls (decl, old))
3100 gcc_checking_assert (!hide_value && !hiding);
3101 return match;
3103 else
3104 goto conflict;
3106 else
3108 conflict:
3109 diagnose_name_conflict (decl, old);
3110 to_val = NULL_TREE;
3113 else if (hiding)
3114 hide_value = true;
3116 done:
3117 if (to_val)
3119 if (local_overload)
3121 gcc_checking_assert (binding->value && OVL_P (binding->value));
3122 update_local_overload (binding, to_val);
3124 else if (level
3125 && !(TREE_CODE (decl) == NAMESPACE_DECL
3126 && !DECL_NAMESPACE_ALIAS (decl)))
3127 /* Don't add namespaces here. They're done in
3128 push_namespace. */
3129 add_decl_to_level (level, decl);
3131 if (slot)
3133 if (STAT_HACK_P (*slot))
3135 STAT_TYPE (*slot) = to_type;
3136 STAT_DECL (*slot) = to_val;
3137 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3138 STAT_DECL_HIDDEN_P (*slot) = hide_value;
3140 else if (to_type || hide_value)
3142 *slot = stat_hack (to_val, to_type);
3143 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3144 STAT_DECL_HIDDEN_P (*slot) = hide_value;
3146 else
3148 gcc_checking_assert (!hide_type);
3149 *slot = to_val;
3152 else
3154 binding->type = to_type;
3155 binding->value = to_val;
3156 HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
3160 return decl;
3163 /* Table of identifiers to extern C declarations (or LISTS thereof). */
3165 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
3167 /* DECL has C linkage. If we have an existing instance, make sure the
3168 new one is compatible. Make sure it has the same exception
3169 specification [7.5, 7.6]. Add DECL to the map. */
3171 static void
3172 check_extern_c_conflict (tree decl)
3174 /* Ignore artificial or system header decls. */
3175 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
3176 return;
3178 /* This only applies to decls at namespace scope. */
3179 if (!DECL_NAMESPACE_SCOPE_P (decl))
3180 return;
3182 if (!extern_c_decls)
3183 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
3185 tree *slot = extern_c_decls
3186 ->find_slot_with_hash (DECL_NAME (decl),
3187 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
3188 if (tree old = *slot)
3190 if (TREE_CODE (old) == OVERLOAD)
3191 old = OVL_FUNCTION (old);
3193 int mismatch = 0;
3194 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
3195 ; /* If they're in the same context, we'll have already complained
3196 about a (possible) mismatch, when inserting the decl. */
3197 else if (!decls_match (decl, old))
3198 mismatch = 1;
3199 else if (TREE_CODE (decl) == FUNCTION_DECL
3200 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
3201 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3202 ce_normal))
3203 mismatch = -1;
3204 else if (DECL_ASSEMBLER_NAME_SET_P (old))
3205 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
3207 if (mismatch)
3209 auto_diagnostic_group d;
3210 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3211 "conflicting C language linkage declaration %q#D", decl);
3212 inform (DECL_SOURCE_LOCATION (old),
3213 "previous declaration %q#D", old);
3214 if (mismatch < 0)
3215 inform (DECL_SOURCE_LOCATION (decl),
3216 "due to different exception specifications");
3218 else
3220 if (old == *slot)
3221 /* The hash table expects OVERLOADS, so construct one with
3222 OLD as both the function and the chain. This allocate
3223 an excess OVERLOAD node, but it's rare to have multiple
3224 extern "C" decls of the same name. And we save
3225 complicating the hash table logic (which is used
3226 elsewhere). */
3227 *slot = ovl_make (old, old);
3229 slot = &OVL_CHAIN (*slot);
3231 /* Chain it on for c_linkage_binding's use. */
3232 *slot = tree_cons (NULL_TREE, decl, *slot);
3235 else
3236 *slot = decl;
3239 /* Returns a list of C-linkage decls with the name NAME. Used in
3240 c-family/c-pragma.cc to implement redefine_extname pragma. */
3242 tree
3243 c_linkage_bindings (tree name)
3245 if (extern_c_decls)
3246 if (tree *slot = extern_c_decls
3247 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
3249 tree result = *slot;
3250 if (TREE_CODE (result) == OVERLOAD)
3251 result = OVL_CHAIN (result);
3252 return result;
3255 return NULL_TREE;
3258 /* Subroutine of check_local_shadow. */
3260 static void
3261 inform_shadowed (tree shadowed)
3263 inform (DECL_SOURCE_LOCATION (shadowed),
3264 "shadowed declaration is here");
3267 /* DECL is being declared at a local scope. Emit suitable shadow
3268 warnings. */
3270 static tree
3271 check_local_shadow (tree decl)
3273 /* Don't complain about the parms we push and then pop
3274 while tentatively parsing a function declarator. */
3275 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
3276 return NULL_TREE;
3278 if (DECL_FUNCTION_SCOPE_P (decl))
3280 tree ctx = DECL_CONTEXT (decl);
3281 if (DECL_CLONED_FUNCTION_P (ctx)
3282 || DECL_TEMPLATE_INSTANTIATED (ctx)
3283 || (DECL_LANG_SPECIFIC (ctx)
3284 && DECL_DEFAULTED_FN (ctx))
3285 || (LAMBDA_FUNCTION_P (ctx)
3286 && LAMBDA_EXPR_REGEN_INFO (CLASSTYPE_LAMBDA_EXPR
3287 (DECL_CONTEXT (ctx)))))
3288 /* It suffices to check shadowing only when actually parsing.
3289 So punt for clones, instantiations, defaulted functions and
3290 regenerated lambdas. This optimization helps reduce lazy
3291 loading cascades with modules. */
3292 return NULL_TREE;
3295 tree old = NULL_TREE;
3296 cp_binding_level *old_scope = NULL;
3297 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
3299 old = binding->value;
3300 old_scope = binding->scope;
3303 if (old
3304 && (TREE_CODE (old) == PARM_DECL
3305 || VAR_P (old)
3306 || (TREE_CODE (old) == TYPE_DECL
3307 && (!DECL_ARTIFICIAL (old)
3308 || TREE_CODE (decl) == TYPE_DECL)))
3309 && DECL_FUNCTION_SCOPE_P (old)
3310 && (!DECL_ARTIFICIAL (decl)
3311 || is_capture_proxy (decl)
3312 || DECL_IMPLICIT_TYPEDEF_P (decl)
3313 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
3315 /* DECL shadows a local thing possibly of interest. */
3317 /* DR 2211: check that captures and parameters
3318 do not have the same name. */
3319 if (is_capture_proxy (decl))
3321 if (current_lambda_expr ()
3322 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
3323 && TREE_CODE (old) == PARM_DECL
3324 && DECL_NAME (decl) != this_identifier)
3325 error_at (DECL_SOURCE_LOCATION (old),
3326 "lambda parameter %qD "
3327 "previously declared as a capture", old);
3328 return NULL_TREE;
3330 /* Don't complain if it's from an enclosing function. */
3331 else if (DECL_CONTEXT (old) == current_function_decl
3332 && TREE_CODE (decl) != PARM_DECL
3333 && TREE_CODE (old) == PARM_DECL)
3335 /* Go to where the parms should be and see if we find
3336 them there. */
3337 cp_binding_level *b = current_binding_level->level_chain;
3339 if (in_function_try_handler && b->kind == sk_catch)
3340 b = b->level_chain;
3342 /* Skip artificially added scopes which aren't present
3343 in the C++ standard, e.g. for function-try-block or
3344 ctor/dtor cleanups. */
3345 while (b->artificial)
3346 b = b->level_chain;
3348 /* [basic.scope.param] A parameter name shall not be redeclared
3349 in the outermost block of the function definition. */
3350 if (b->kind == sk_function_parms)
3352 if (name_independent_decl_p (decl))
3353 return old;
3355 auto_diagnostic_group d;
3356 bool emit = true;
3357 if (DECL_EXTERNAL (decl))
3358 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3359 "declaration of %q#D shadows a parameter",
3360 decl);
3361 else
3362 error_at (DECL_SOURCE_LOCATION (decl),
3363 "declaration of %q#D shadows a parameter", decl);
3364 if (emit)
3365 inform (DECL_SOURCE_LOCATION (old),
3366 "%q#D previously declared here", old);
3367 return NULL_TREE;
3371 /* The local structure or class can't use parameters of
3372 the containing function anyway. */
3373 if (DECL_CONTEXT (old) != current_function_decl)
3375 for (cp_binding_level *scope = current_binding_level;
3376 scope != old_scope; scope = scope->level_chain)
3377 if (scope->kind == sk_class
3378 && !LAMBDA_TYPE_P (scope->this_entity))
3379 return NULL_TREE;
3381 /* Error if redeclaring a local declared in a
3382 init-statement or in the condition of an if or
3383 switch statement when the new declaration is in the
3384 outermost block of the controlled statement.
3385 Redeclaring a variable from a for or while condition is
3386 detected elsewhere. */
3387 else if (VAR_P (old)
3388 && old_scope == current_binding_level->level_chain
3389 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
3391 if (name_independent_decl_p (decl))
3392 return old;
3394 auto_diagnostic_group d;
3395 bool emit = true;
3396 if (DECL_EXTERNAL (decl))
3397 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3398 "redeclaration of %q#D", decl);
3399 else
3400 error_at (DECL_SOURCE_LOCATION (decl),
3401 "redeclaration of %q#D", decl);
3402 if (emit)
3403 inform (DECL_SOURCE_LOCATION (old),
3404 "%q#D previously declared here", old);
3405 return NULL_TREE;
3407 /* C++11:
3408 3.3.3/3: The name declared in an exception-declaration (...)
3409 shall not be redeclared in the outermost block of the handler.
3410 3.3.3/2: A parameter name shall not be redeclared (...) in
3411 the outermost block of any handler associated with a
3412 function-try-block. */
3413 else if (TREE_CODE (old) == VAR_DECL
3414 && old_scope == current_binding_level->level_chain
3415 && old_scope->kind == sk_catch)
3417 if (name_independent_decl_p (decl))
3418 return old;
3420 auto_diagnostic_group d;
3421 bool emit;
3422 if (DECL_EXTERNAL (decl))
3423 emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3424 "redeclaration of %q#D", decl);
3425 else
3426 emit = permerror (DECL_SOURCE_LOCATION (decl),
3427 "redeclaration of %q#D", decl);
3428 if (emit)
3429 inform (DECL_SOURCE_LOCATION (old),
3430 "%q#D previously declared here", old);
3431 return NULL_TREE;
3434 /* Don't emit -Wshadow* warnings for name-independent decls. */
3435 if (name_independent_decl_p (decl) || name_independent_decl_p (old))
3436 return NULL_TREE;
3438 /* If '-Wshadow=compatible-local' is specified without other
3439 -Wshadow= flags, we will warn only when the type of the
3440 shadowing variable (DECL) can be converted to that of the
3441 shadowed parameter (OLD_LOCAL). The reason why we only check
3442 if DECL's type can be converted to OLD_LOCAL's type (but not the
3443 other way around) is because when users accidentally shadow a
3444 parameter, more than often they would use the variable
3445 thinking (mistakenly) it's still the parameter. It would be
3446 rare that users would use the variable in the place that
3447 expects the parameter but thinking it's a new decl.
3448 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3449 warns regardless of whether one of the types involved
3450 is a subclass of the other, since that is never okay. */
3452 enum opt_code warning_code;
3453 if (warn_shadow)
3454 warning_code = OPT_Wshadow;
3455 else if ((TREE_CODE (decl) == TYPE_DECL)
3456 ^ (TREE_CODE (old) == TYPE_DECL))
3457 /* If exactly one is a type, they aren't compatible. */
3458 warning_code = OPT_Wshadow_local;
3459 else if ((TREE_TYPE (old)
3460 && TREE_TYPE (decl)
3461 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3462 || TREE_CODE (decl) == TYPE_DECL
3463 || TREE_CODE (old) == TYPE_DECL
3464 || (!dependent_type_p (TREE_TYPE (decl))
3465 && !dependent_type_p (TREE_TYPE (old))
3466 /* If the new decl uses auto, we don't yet know
3467 its type (the old type cannot be using auto
3468 at this point, without also being
3469 dependent). This is an indication we're
3470 (now) doing the shadow checking too
3471 early. */
3472 && !type_uses_auto (TREE_TYPE (decl))
3473 && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
3474 decl, LOOKUP_IMPLICIT, tf_none)))
3475 warning_code = OPT_Wshadow_compatible_local;
3476 else
3477 warning_code = OPT_Wshadow_local;
3479 const char *msg;
3480 if (TREE_CODE (old) == PARM_DECL)
3481 msg = "declaration of %q#D shadows a parameter";
3482 else if (is_capture_proxy (old))
3483 msg = "declaration of %qD shadows a lambda capture";
3484 else
3485 msg = "declaration of %qD shadows a previous local";
3487 auto_diagnostic_group d;
3488 if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
3489 inform_shadowed (old);
3490 return NULL_TREE;
3493 if (!warn_shadow)
3494 return NULL_TREE;
3496 /* Don't emit -Wshadow for name-independent decls. */
3497 if (name_independent_decl_p (decl))
3498 return NULL_TREE;
3500 /* Don't warn for artificial things that are not implicit typedefs. */
3501 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
3502 return NULL_TREE;
3504 if (nonlambda_method_basetype ())
3505 if (tree member = lookup_member (current_nonlambda_class_type (),
3506 DECL_NAME (decl), /*protect=*/0,
3507 /*want_type=*/false, tf_warning_or_error))
3509 member = MAYBE_BASELINK_FUNCTIONS (member);
3511 /* Warn if a variable shadows a non-function, or the variable
3512 is a function or a pointer-to-function. */
3513 if ((!OVL_P (member)
3514 || TREE_CODE (decl) == FUNCTION_DECL
3515 || (TREE_TYPE (decl)
3516 && (TYPE_PTRFN_P (TREE_TYPE (decl))
3517 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
3518 && !warning_suppressed_p (decl, OPT_Wshadow))
3520 auto_diagnostic_group d;
3521 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3522 "declaration of %qD shadows a member of %qT",
3523 decl, current_nonlambda_class_type ())
3524 && DECL_P (member))
3526 inform_shadowed (member);
3527 suppress_warning (decl, OPT_Wshadow);
3530 return NULL_TREE;
3533 /* Now look for a namespace shadow. */
3534 old = find_namespace_value (current_namespace, DECL_NAME (decl));
3535 if (old
3536 && (VAR_P (old)
3537 || (TREE_CODE (old) == TYPE_DECL
3538 && (!DECL_ARTIFICIAL (old)
3539 || TREE_CODE (decl) == TYPE_DECL)))
3540 && !DECL_EXTERNAL (decl)
3541 && !instantiating_current_function_p ()
3542 && !warning_suppressed_p (decl, OPT_Wshadow))
3543 /* XXX shadow warnings in outer-more namespaces */
3545 auto_diagnostic_group d;
3546 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3547 "declaration of %qD shadows a global declaration",
3548 decl))
3550 inform_shadowed (old);
3551 suppress_warning (decl, OPT_Wshadow);
3553 return NULL_TREE;
3556 return NULL_TREE;
3559 /* DECL is being pushed inside function CTX. Set its context, if
3560 needed. */
3562 static void
3563 set_decl_context_in_fn (tree ctx, tree decl)
3565 if (TREE_CODE (decl) == FUNCTION_DECL
3566 || (VAR_P (decl) && DECL_EXTERNAL (decl)))
3567 /* Make sure local externs are marked as such. OMP UDRs really
3568 are nested functions. */
3569 gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
3570 && (DECL_NAMESPACE_SCOPE_P (decl)
3571 || (TREE_CODE (decl) == FUNCTION_DECL
3572 && DECL_OMP_DECLARE_REDUCTION_P (decl))));
3574 if (!DECL_CONTEXT (decl)
3575 /* When parsing the parameter list of a function declarator,
3576 don't set DECL_CONTEXT to an enclosing function. */
3577 && !(TREE_CODE (decl) == PARM_DECL
3578 && parsing_function_declarator ()))
3579 DECL_CONTEXT (decl) = ctx;
3582 /* DECL is a local extern decl. Find or create the namespace-scope
3583 decl that it aliases. Also, determines the linkage of DECL. */
3585 void
3586 push_local_extern_decl_alias (tree decl)
3588 if (dependent_type_p (TREE_TYPE (decl))
3589 || (processing_template_decl
3590 && VAR_P (decl)
3591 && CP_DECL_THREAD_LOCAL_P (decl)))
3592 return;
3593 /* EH specs were not part of the function type prior to c++17, but
3594 we still can't go pushing dependent eh specs into the namespace. */
3595 if (cxx_dialect < cxx17
3596 && TREE_CODE (decl) == FUNCTION_DECL
3597 && (value_dependent_expression_p
3598 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
3599 return;
3601 gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
3602 || !DECL_TEMPLATE_INFO (decl));
3603 if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
3604 /* We're instantiating a non-dependent local decl, it already
3605 knows the alias. */
3606 return;
3608 tree alias = NULL_TREE;
3610 if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
3611 /* Do not let a VLA creep into a namespace. Diagnostic will be
3612 emitted in layout_var_decl later. */
3613 alias = error_mark_node;
3614 else
3616 /* First look for a decl that matches. */
3617 tree ns = CP_DECL_CONTEXT (decl);
3618 tree binding = find_namespace_value (ns, DECL_NAME (decl));
3620 if (binding && TREE_CODE (binding) != TREE_LIST)
3621 for (ovl_iterator iter (binding); iter; ++iter)
3622 if (decls_match (decl, *iter, /*record_versions*/false))
3624 alias = *iter;
3625 break;
3628 if (!alias)
3630 /* No existing namespace-scope decl. Make one. */
3631 alias = copy_decl (decl);
3632 if (TREE_CODE (alias) == FUNCTION_DECL)
3634 /* Recontextualize the parms. */
3635 for (tree *chain = &DECL_ARGUMENTS (alias);
3636 *chain; chain = &DECL_CHAIN (*chain))
3638 *chain = copy_decl (*chain);
3639 DECL_CONTEXT (*chain) = alias;
3642 tree type = TREE_TYPE (alias);
3643 for (tree args = TYPE_ARG_TYPES (type);
3644 args; args = TREE_CHAIN (args))
3645 if (TREE_PURPOSE (args))
3647 /* There are default args. Lose them. */
3648 tree nargs = NULL_TREE;
3649 tree *chain = &nargs;
3650 for (args = TYPE_ARG_TYPES (type);
3651 args; args = TREE_CHAIN (args))
3652 if (args == void_list_node)
3654 *chain = args;
3655 break;
3657 else
3659 *chain
3660 = build_tree_list (NULL_TREE, TREE_VALUE (args));
3661 chain = &TREE_CHAIN (*chain);
3664 tree fn_type = build_function_type (TREE_TYPE (type), nargs);
3666 fn_type = apply_memfn_quals
3667 (fn_type, type_memfn_quals (type));
3669 fn_type = build_cp_fntype_variant
3670 (fn_type, type_memfn_rqual (type),
3671 TYPE_RAISES_EXCEPTIONS (type),
3672 TYPE_HAS_LATE_RETURN_TYPE (type));
3674 TREE_TYPE (alias) = fn_type;
3675 break;
3679 /* This is the real thing. */
3680 DECL_LOCAL_DECL_P (alias) = false;
3682 /* Expected default linkage is from the namespace. */
3683 TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
3684 push_nested_namespace (ns);
3685 alias = pushdecl (alias, /* hiding= */true);
3686 pop_nested_namespace (ns);
3687 if (VAR_P (decl)
3688 && CP_DECL_THREAD_LOCAL_P (decl)
3689 && alias != error_mark_node)
3690 set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
3692 /* Adjust visibility. */
3693 determine_visibility (alias);
3697 retrofit_lang_decl (decl);
3698 DECL_LOCAL_DECL_ALIAS (decl) = alias;
3701 /* If DECL has non-internal linkage, and we have a module vector,
3702 record it in the appropriate slot. We have already checked for
3703 duplicates. */
3705 static void
3706 maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
3708 if (TREE_CODE (*slot) != BINDING_VECTOR)
3709 return;
3711 if (!TREE_PUBLIC (CP_DECL_CONTEXT (decl)))
3712 /* Member of internal namespace. */
3713 return;
3715 tree not_tmpl = STRIP_TEMPLATE (decl);
3716 if ((TREE_CODE (not_tmpl) == FUNCTION_DECL
3717 || VAR_P (not_tmpl))
3718 && DECL_THIS_STATIC (not_tmpl))
3719 /* Internal linkage. */
3720 return;
3722 bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
3723 && DECL_MODULE_ATTACH_P (not_tmpl));
3724 tree *gslot = get_fixed_binding_slot
3725 (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
3726 true);
3728 if (!is_attached)
3730 binding_slot &orig
3731 = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
3733 if (!STAT_HACK_P (tree (orig)))
3734 orig = stat_hack (tree (orig));
3736 MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
3739 add_mergeable_namespace_entity (gslot, decl);
3742 /* DECL is being pushed. Check whether it hides or ambiguates
3743 something seen as an import. This include decls seen in our own
3744 interface, which is OK. Also, check for merging a
3745 global/partition decl. */
3747 static tree
3748 check_module_override (tree decl, tree mvec, bool hiding,
3749 tree scope, tree name)
3751 tree match = NULL_TREE;
3752 bitmap imports = get_import_bitmap ();
3753 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
3754 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
3756 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
3758 cluster++;
3759 ix--;
3762 for (; ix--; cluster++)
3763 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
3765 /* Are we importing this module? */
3766 if (cluster->indices[jx].span != 1)
3767 continue;
3768 if (!cluster->indices[jx].base)
3769 continue;
3770 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
3771 continue;
3772 /* Is it loaded? */
3773 if (cluster->slots[jx].is_lazy ())
3775 gcc_assert (cluster->indices[jx].span == 1);
3776 lazy_load_binding (cluster->indices[jx].base,
3777 scope, name, &cluster->slots[jx]);
3779 tree bind = cluster->slots[jx];
3780 if (!bind)
3781 /* Errors could cause there to be nothing. */
3782 continue;
3784 if (STAT_HACK_P (bind))
3785 /* We do not have to check STAT_TYPE here, the xref_tag
3786 machinery deals with that problem. */
3787 bind = STAT_VISIBLE (bind);
3789 for (ovl_iterator iter (bind); iter; ++iter)
3790 if (!iter.using_p ())
3792 match = duplicate_decls (decl, *iter, hiding);
3793 if (match)
3794 goto matched;
3798 if (TREE_PUBLIC (scope) && TREE_PUBLIC (STRIP_TEMPLATE (decl))
3799 /* Namespaces are dealt with specially in
3800 make_namespace_finish. */
3801 && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
3803 /* Look in the appropriate mergeable decl slot. */
3804 tree mergeable = NULL_TREE;
3805 if (named_module_p ())
3806 mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
3807 / BINDING_VECTOR_SLOTS_PER_CLUSTER)
3808 .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
3809 else
3810 mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
3812 for (ovl_iterator iter (mergeable); iter; ++iter)
3814 match = duplicate_decls (decl, *iter, hiding);
3815 if (match)
3816 goto matched;
3820 return NULL_TREE;
3822 matched:
3823 if (match != error_mark_node)
3825 if (named_module_p ())
3826 BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
3827 else
3828 BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
3831 return match;
3836 /* Record DECL as belonging to the current lexical scope. Check for
3837 errors (such as an incompatible declaration for the same name
3838 already seen in the same scope).
3840 The new binding is hidden if HIDING is true (an anticipated builtin
3841 or hidden friend).
3843 Returns either DECL or an old decl for the same name. If an old
3844 decl is returned, it may have been smashed to agree with what DECL
3845 says. */
3847 tree
3848 pushdecl (tree decl, bool hiding)
3850 auto_cond_timevar tv (TV_NAME_LOOKUP);
3852 if (decl == error_mark_node)
3853 return error_mark_node;
3855 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
3856 set_decl_context_in_fn (current_function_decl, decl);
3858 /* The binding level we will be pushing into. During local class
3859 pushing, we want to push to the containing scope. */
3860 cp_binding_level *level = current_binding_level;
3861 while (level->kind == sk_class
3862 || level->kind == sk_cleanup)
3863 level = level->level_chain;
3865 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3866 insert it. Other NULL-named decls, not so much. */
3867 tree name = DECL_NAME (decl);
3868 if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
3870 cxx_binding *binding = NULL; /* Local scope binding. */
3871 tree ns = NULL_TREE; /* Searched namespace. */
3872 tree *slot = NULL; /* Binding slot in namespace. */
3873 tree *mslot = NULL; /* Current module slot in namespace. */
3874 tree old = NULL_TREE;
3875 bool name_independent_p = false;
3876 bool name_independent_diagnosed_p = false;
3878 if (level->kind == sk_namespace)
3880 /* We look in the decl's namespace for an existing
3881 declaration, even though we push into the current
3882 namespace. */
3883 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3884 ? CP_DECL_CONTEXT (decl) : current_namespace);
3885 /* Create the binding, if this is current namespace, because
3886 that's where we'll be pushing anyway. */
3887 slot = find_namespace_slot (ns, name, ns == current_namespace);
3888 if (slot)
3890 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
3891 ns == current_namespace);
3892 old = MAYBE_STAT_DECL (*mslot);
3895 else
3897 binding = find_local_binding (level, name);
3898 if (binding)
3899 old = binding->value;
3900 name_independent_p = name_independent_decl_p (decl);
3903 if (old == error_mark_node)
3904 old = NULL_TREE;
3906 tree oldi, oldn;
3907 for (oldi = old; oldi; oldi = oldn)
3909 if (TREE_CODE (oldi) == TREE_LIST)
3911 gcc_checking_assert (level->kind != sk_namespace
3912 && name_independent_decl_p
3913 (TREE_VALUE (old)));
3914 oldn = TREE_CHAIN (oldi);
3915 oldi = TREE_VALUE (oldi);
3917 else
3918 oldn = NULL_TREE;
3919 for (ovl_iterator iter (oldi); iter; ++iter)
3920 if (iter.using_p ())
3921 ; /* Ignore using decls here. */
3922 else if (iter.hidden_p ()
3923 && TREE_CODE (*iter) == FUNCTION_DECL
3924 && DECL_LANG_SPECIFIC (*iter)
3925 && DECL_MODULE_IMPORT_P (*iter))
3926 ; /* An undeclared builtin imported from elsewhere. */
3927 else if (name_independent_p)
3929 /* Ignore name-independent declarations. */
3930 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
3931 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
3932 "name-independent declarations only available with "
3933 "%<-std=c++2c%> or %<-std=gnu++2c%>");
3934 name_independent_diagnosed_p = true;
3936 else if (tree match
3937 = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
3939 if (match == error_mark_node)
3941 else if (TREE_CODE (match) == TYPE_DECL)
3942 gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
3943 == (level->kind == sk_namespace
3944 ? NULL_TREE : TREE_TYPE (match)));
3945 else if (iter.hidden_p () && !hiding)
3947 /* Unhiding a previously hidden decl. */
3948 tree head = iter.reveal_node (oldi);
3949 if (head != oldi)
3951 gcc_checking_assert (ns);
3952 if (STAT_HACK_P (*slot))
3953 STAT_DECL (*slot) = head;
3954 else
3955 *slot = head;
3957 if (DECL_EXTERN_C_P (match))
3958 /* We need to check and register the decl now. */
3959 check_extern_c_conflict (match);
3961 else if (slot
3962 && !hiding
3963 && STAT_HACK_P (*slot)
3964 && STAT_DECL_HIDDEN_P (*slot))
3966 /* Unhide the non-function. */
3967 gcc_checking_assert (oldi == match);
3968 if (!STAT_TYPE (*slot))
3969 *slot = match;
3970 else
3971 STAT_DECL (*slot) = match;
3973 return match;
3977 /* Check for redeclaring an import. */
3978 if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
3979 if (tree match
3980 = check_module_override (decl, *slot, hiding, ns, name))
3982 if (match == error_mark_node)
3983 return match;
3985 /* We found a decl in an interface, push it into this
3986 binding. */
3987 decl = update_binding (NULL, binding, mslot, old,
3988 match, hiding);
3990 return decl;
3993 /* We are pushing a new decl. */
3995 /* Skip a hidden builtin we failed to match already. There can
3996 only be one. */
3997 if (old && anticipated_builtin_p (old))
3998 old = OVL_CHAIN (old);
4000 check_template_shadow (decl);
4002 if (DECL_DECLARES_FUNCTION_P (decl))
4004 check_default_args (decl);
4006 if (hiding)
4008 if (level->kind != sk_namespace)
4010 /* In a local class, a friend function declaration must
4011 find a matching decl in the innermost non-class scope.
4012 [class.friend/11] */
4013 error_at (DECL_SOURCE_LOCATION (decl),
4014 "friend declaration %qD in local class without "
4015 "prior local declaration", decl);
4016 /* Don't attempt to push it. */
4017 return error_mark_node;
4022 if (level->kind != sk_namespace)
4024 tree local_shadow = check_local_shadow (decl);
4025 if (name_independent_p && local_shadow)
4027 if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
4028 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
4029 "name-independent declarations only available with "
4030 "%<-std=c++2c%> or %<-std=gnu++2c%>");
4031 name_independent_diagnosed_p = true;
4032 /* When a name-independent declaration is pushed into a scope
4033 which itself does not contain a _ named declaration yet (so
4034 _ name lookups wouldn't be normally ambiguous), but it
4035 shadows a _ declaration in some outer scope in cases
4036 described in [basic.scope.block]/2 where if the names of
4037 the shadowed and shadowing declarations were different it
4038 would be ill-formed program, arrange for _ name lookups
4039 in this scope to be ambiguous. */
4040 if (old == NULL_TREE)
4042 old = build_tree_list (error_mark_node, local_shadow);
4043 TREE_TYPE (old) = error_mark_node;
4047 if (TREE_CODE (decl) == NAMESPACE_DECL)
4048 /* A local namespace alias. */
4049 set_identifier_type_value_with_scope (name, NULL_TREE, level);
4051 if (!binding)
4052 binding = create_local_binding (level, name);
4054 else if (!slot)
4056 ns = current_namespace;
4057 slot = find_namespace_slot (ns, name, true);
4058 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
4059 /* Update OLD to reflect the namespace we're going to be
4060 pushing into. */
4061 old = MAYBE_STAT_DECL (*mslot);
4064 old = update_binding (level, binding, mslot, old, decl, hiding);
4066 if (old != decl)
4067 /* An existing decl matched, use it. */
4068 decl = old;
4069 else
4071 if (TREE_CODE (decl) == TYPE_DECL)
4073 tree type = TREE_TYPE (decl);
4075 if (type != error_mark_node)
4077 if (TYPE_NAME (type) != decl)
4078 set_underlying_type (decl);
4080 set_identifier_type_value_with_scope (name, decl, level);
4082 if (level->kind != sk_namespace
4083 && !instantiating_current_function_p ())
4084 /* This is a locally defined typedef in a function that
4085 is not a template instantation, record it to implement
4086 -Wunused-local-typedefs. */
4087 record_locally_defined_typedef (decl);
4090 else if (VAR_OR_FUNCTION_DECL_P (decl))
4092 if (DECL_EXTERN_C_P (decl))
4093 check_extern_c_conflict (decl);
4095 if (!DECL_LOCAL_DECL_P (decl)
4096 && VAR_P (decl))
4097 maybe_register_incomplete_var (decl);
4099 if (DECL_LOCAL_DECL_P (decl)
4100 && NAMESPACE_SCOPE_P (decl))
4101 push_local_extern_decl_alias (decl);
4104 if (level->kind == sk_namespace
4105 && TREE_PUBLIC (level->this_entity)
4106 && module_p ())
4107 maybe_record_mergeable_decl (slot, name, decl);
4110 else
4111 add_decl_to_level (level, decl);
4113 return decl;
4116 /* A mergeable entity is being loaded into namespace NS slot NAME.
4117 Create and return the appropriate vector slot for that. Either a
4118 GMF slot or a module-specific one. */
4120 tree *
4121 mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
4123 tree *mslot = find_namespace_slot (ns, name, true);
4124 tree *vslot = get_fixed_binding_slot
4125 (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
4126 true);
4128 gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
4129 *vec = *mslot;
4131 return vslot;
4134 /* Retrieve the bindings for an existing mergeable entity in namespace
4135 NS slot NAME. Returns NULL if no such bindings exists. */
4137 static tree
4138 get_mergeable_namespace_binding (tree ns, tree name, bool is_attached)
4140 tree *mslot = find_namespace_slot (ns, name, false);
4141 if (!mslot || !*mslot || TREE_CODE (*mslot) != BINDING_VECTOR)
4142 return NULL_TREE;
4144 tree *vslot = get_fixed_binding_slot
4145 (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
4146 false);
4147 return vslot ? *vslot : NULL_TREE;
4150 /* DECL is a new mergeable namespace-scope decl. Add it to the
4151 mergeable entities on GSLOT. */
4153 void
4154 add_mergeable_namespace_entity (tree *gslot, tree decl)
4156 *gslot = ovl_make (decl, *gslot);
4159 /* A mergeable entity of KLASS called NAME is being loaded. Return
4160 the set of things it could be. All such non-as_base classes have
4161 been given a member vec. */
4163 tree
4164 lookup_class_binding (tree klass, tree name)
4166 tree found = NULL_TREE;
4168 if (!COMPLETE_TYPE_P (klass))
4170 else if (TYPE_LANG_SPECIFIC (klass))
4172 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
4174 found = member_vec_binary_search (member_vec, name);
4175 if (!found)
4177 else if (STAT_HACK_P (found))
4178 /* Rearrange the stat hack so that we don't need to expose that
4179 internal detail. */
4180 found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
4181 else if (IDENTIFIER_CONV_OP_P (name))
4183 gcc_checking_assert (name == conv_op_identifier);
4184 found = OVL_CHAIN (found);
4187 else
4189 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
4190 || TYPE_PTRMEMFUNC_P (klass));
4191 found = fields_linear_search (klass, name, false);
4194 return found;
4197 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
4198 for all decls of the current module. When partitions are involved,
4199 decls might be mentioned more than once. Return the accumulation of
4200 CALLBACK results. */
4202 unsigned
4203 walk_module_binding (tree binding, bitmap partitions,
4204 bool (*callback) (tree decl, WMB_Flags, void *data),
4205 void *data)
4207 // FIXME: We don't quite deal with using decls naming stat hack
4208 // type.
4209 tree current = binding;
4210 unsigned count = 0;
4212 if (TREE_CODE (binding) == BINDING_VECTOR)
4213 current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
4215 bool decl_hidden = false;
4216 if (tree type = MAYBE_STAT_TYPE (current))
4218 WMB_Flags flags = WMB_None;
4219 if (STAT_TYPE_HIDDEN_P (current))
4220 flags = WMB_Flags (flags | WMB_Hidden);
4221 count += callback (type, flags, data);
4222 decl_hidden = STAT_DECL_HIDDEN_P (current);
4225 for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
4227 if (iter.hidden_p ())
4228 decl_hidden = true;
4229 if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
4231 WMB_Flags flags = WMB_None;
4232 if (decl_hidden)
4233 flags = WMB_Flags (flags | WMB_Hidden);
4234 if (iter.using_p ())
4236 flags = WMB_Flags (flags | WMB_Using);
4237 if (iter.exporting_p ())
4238 flags = WMB_Flags (flags | WMB_Export);
4240 count += callback (*iter, flags, data);
4242 decl_hidden = false;
4245 if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
4247 /* Process partition slots. */
4248 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
4249 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
4250 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
4252 ix--;
4253 cluster++;
4256 bool maybe_dups = BINDING_VECTOR_PARTITION_DUPS_P (binding);
4258 for (; ix--; cluster++)
4259 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
4260 if (!cluster->slots[jx].is_lazy ())
4261 if (tree bind = cluster->slots[jx])
4263 if (TREE_CODE (bind) == NAMESPACE_DECL
4264 && !DECL_NAMESPACE_ALIAS (bind))
4266 if (unsigned base = cluster->indices[jx].base)
4267 if (unsigned span = cluster->indices[jx].span)
4269 if (bitmap_bit_p (partitions, base))
4270 goto found;
4271 while (++base, --span);
4272 /* Not a partition's namespace. */
4273 continue;
4274 found:
4276 WMB_Flags flags = WMB_None;
4277 if (maybe_dups)
4278 flags = WMB_Flags (flags | WMB_Dups);
4279 count += callback (bind, flags, data);
4281 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
4283 if (tree btype = STAT_TYPE (bind))
4285 WMB_Flags flags = WMB_None;
4286 if (maybe_dups)
4287 flags = WMB_Flags (flags | WMB_Dups);
4288 if (STAT_TYPE_HIDDEN_P (bind))
4289 flags = WMB_Flags (flags | WMB_Hidden);
4291 count += callback (btype, flags, data);
4293 bool hidden = STAT_DECL_HIDDEN_P (bind);
4294 for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
4295 iter; ++iter)
4297 if (iter.hidden_p ())
4298 hidden = true;
4299 gcc_checking_assert
4300 (!(hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
4302 WMB_Flags flags = WMB_None;
4303 if (maybe_dups)
4304 flags = WMB_Flags (flags | WMB_Dups);
4305 if (decl_hidden)
4306 flags = WMB_Flags (flags | WMB_Hidden);
4307 if (iter.using_p ())
4309 flags = WMB_Flags (flags | WMB_Using);
4310 if (iter.exporting_p ())
4311 flags = WMB_Flags (flags | WMB_Export);
4313 count += callback (*iter, flags, data);
4314 hidden = false;
4320 return count;
4323 /* Imported module MOD has a binding to NS::NAME, stored in section
4324 SNUM. */
4326 bool
4327 import_module_binding (tree ns, tree name, unsigned mod, unsigned snum)
4329 tree *slot = find_namespace_slot (ns, name, true);
4330 binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
4332 if (mslot->is_lazy () || *mslot)
4333 /* Oops, something was already there. */
4334 return false;
4336 mslot->set_lazy (snum);
4337 return true;
4340 /* An import of MODULE is binding NS::NAME. There should be no
4341 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4342 is a header_unit (-1) or part of the current module (+1). VALUE
4343 and TYPE are the value and type bindings. VISIBLE are the value
4344 bindings being exported. */
4346 bool
4347 set_module_binding (tree ns, tree name, unsigned mod, int mod_glob,
4348 tree value, tree type, tree visible)
4350 if (!value)
4351 /* Bogus BMIs could give rise to nothing to bind. */
4352 return false;
4354 gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
4355 || DECL_NAMESPACE_ALIAS (value));
4356 gcc_checking_assert (mod);
4358 tree *slot = find_namespace_slot (ns, name, true);
4359 binding_slot *mslot = search_imported_binding_slot (slot, mod);
4361 if (!mslot || !mslot->is_lazy ())
4362 /* Again, bogus BMI could give find to missing or already loaded slot. */
4363 return false;
4365 tree bind = value;
4366 if (type || visible != bind || mod_glob)
4368 bind = stat_hack (bind, type);
4369 STAT_VISIBLE (bind) = visible;
4370 if ((mod_glob > 0 && TREE_PUBLIC (ns))
4371 || (type && DECL_MODULE_EXPORT_P (type)))
4372 STAT_TYPE_VISIBLE_P (bind) = true;
4375 /* Note if this is this-module or global binding. */
4376 if (mod_glob > 0)
4377 MODULE_BINDING_PARTITION_P (bind) = true;
4378 else if (mod_glob < 0)
4379 MODULE_BINDING_GLOBAL_P (bind) = true;
4381 *mslot = bind;
4383 return true;
4386 void
4387 add_module_namespace_decl (tree ns, tree decl)
4389 gcc_assert (!DECL_CHAIN (decl));
4390 gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
4391 && DECL_LOCAL_DECL_P (decl)));
4392 if (CHECKING_P)
4393 /* Expensive already-there? check. */
4394 for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
4395 probe = DECL_CHAIN (probe))
4396 gcc_assert (decl != probe);
4398 add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
4400 if (VAR_P (decl))
4401 maybe_register_incomplete_var (decl);
4403 if (VAR_OR_FUNCTION_DECL_P (decl)
4404 && DECL_EXTERN_C_P (decl))
4405 check_extern_c_conflict (decl);
4408 /* Enter DECL into the symbol table, if that's appropriate. Returns
4409 DECL, or a modified version thereof. */
4411 tree
4412 maybe_push_decl (tree decl)
4414 tree type = TREE_TYPE (decl);
4416 /* Add this decl to the current binding level, but not if it comes
4417 from another scope, e.g. a static member variable. TEM may equal
4418 DECL or it may be a previous decl of the same name. */
4419 if (decl == error_mark_node
4420 || (TREE_CODE (decl) != PARM_DECL
4421 && DECL_CONTEXT (decl) != NULL_TREE
4422 /* Definitions of namespace members outside their namespace are
4423 possible. */
4424 && !DECL_NAMESPACE_SCOPE_P (decl))
4425 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4426 || type == unknown_type_node
4427 /* The declaration of a template specialization does not affect
4428 the functions available for overload resolution, so we do not
4429 call pushdecl. */
4430 || (TREE_CODE (decl) == FUNCTION_DECL
4431 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4432 return decl;
4433 else
4434 return pushdecl (decl);
4437 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4438 binding level. If IS_USING is true, DECL got here through a
4439 using-declaration. */
4441 static void
4442 push_local_binding (tree id, tree decl, bool is_using)
4444 /* Skip over any local classes. This makes sense if we call
4445 push_local_binding with a friend decl of a local class. */
4446 cp_binding_level *b = innermost_nonclass_level ();
4448 gcc_assert (b->kind != sk_namespace);
4449 if (find_local_binding (b, id))
4451 /* Supplement the existing binding. */
4452 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
4453 /* It didn't work. Something else must be bound at this
4454 level. Do not add DECL to the list of things to pop
4455 later. */
4456 return;
4458 else
4459 /* Create a new binding. */
4460 push_binding (id, decl, b);
4462 if (TREE_CODE (decl) == OVERLOAD || is_using)
4463 /* We must put the OVERLOAD or using into a TREE_LIST since we
4464 cannot use the decl's chain itself. */
4465 decl = build_tree_list (id, decl);
4467 /* And put DECL on the list of things declared by the current
4468 binding level. */
4469 add_decl_to_level (b, decl);
4472 /* Lookup the FRIEND_TMPL within all merged module imports. Used to dedup
4473 instantiations of temploid hidden friends from imported modules. */
4475 tree
4476 lookup_imported_hidden_friend (tree friend_tmpl)
4478 /* For a class-scope friend class it should have been found by regular
4479 name lookup. Otherwise we're looking in the current namespace. */
4480 gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
4482 tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
4483 if (!DECL_LANG_SPECIFIC (inner)
4484 || !DECL_MODULE_IMPORT_P (inner))
4485 return NULL_TREE;
4487 /* Imported temploid friends are not considered as attached to this
4488 module for merging purposes. */
4489 tree bind = get_mergeable_namespace_binding (current_namespace,
4490 DECL_NAME (inner), false);
4491 if (!bind)
4492 return NULL_TREE;
4494 /* We're only interested in declarations coming from the same module
4495 of the friend class we're attempting to instantiate. */
4496 int m = get_originating_module (friend_tmpl);
4497 gcc_assert (m != 0);
4499 /* There should be at most one class template from the module we're
4500 looking for, return it. */
4501 for (ovl_iterator iter (bind); iter; ++iter)
4502 if (DECL_CLASS_TEMPLATE_P (*iter)
4503 && get_originating_module (*iter) == m)
4504 return *iter;
4506 return NULL_TREE;
4510 /* true means unconditionally make a BLOCK for the next level pushed. */
4512 static bool keep_next_level_flag;
4514 static int binding_depth = 0;
4516 static void
4517 indent (int depth)
4519 int i;
4521 for (i = 0; i < depth * 2; i++)
4522 putc (' ', stderr);
4525 /* Return a string describing the kind of SCOPE we have. */
4526 static const char *
4527 cp_binding_level_descriptor (cp_binding_level *scope)
4529 /* The order of this table must match the "scope_kind"
4530 enumerators. */
4531 static const char* scope_kind_names[] = {
4532 "block-scope",
4533 "cleanup-scope",
4534 "try-scope",
4535 "catch-scope",
4536 "for-scope",
4537 "cond-init-scope",
4538 "stmt-expr-scope",
4539 "function-parameter-scope",
4540 "class-scope",
4541 "enum-scope",
4542 "namespace-scope",
4543 "template-parameter-scope",
4544 "template-explicit-spec-scope",
4545 "transaction-scope",
4546 "openmp-scope"
4548 static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
4549 "must keep names aligned with scope_kind enum");
4551 scope_kind kind = scope->kind;
4552 if (kind == sk_template_parms && scope->explicit_spec_p)
4553 kind = sk_template_spec;
4555 return scope_kind_names[kind];
4558 /* Output a debugging information about SCOPE when performing
4559 ACTION at LINE. */
4560 static void
4561 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
4563 const char *desc = cp_binding_level_descriptor (scope);
4564 if (scope->this_entity)
4565 verbatim ("%s %<%s(%E)%> %p %d", action, desc,
4566 scope->this_entity, (void *) scope, line);
4567 else
4568 verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
4571 /* A chain of binding_level structures awaiting reuse. */
4573 static GTY((deletable)) cp_binding_level *free_binding_level;
4575 /* Insert SCOPE as the innermost binding level. */
4577 void
4578 push_binding_level (cp_binding_level *scope)
4580 /* Add it to the front of currently active scopes stack. */
4581 scope->level_chain = current_binding_level;
4582 current_binding_level = scope;
4583 keep_next_level_flag = false;
4585 if (ENABLE_SCOPE_CHECKING)
4587 scope->binding_depth = binding_depth;
4588 indent (binding_depth);
4589 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4590 "push");
4591 binding_depth++;
4595 /* Create a new KIND scope and make it the top of the active scopes stack.
4596 ENTITY is the scope of the associated C++ entity (namespace, class,
4597 function, C++0x enumeration); it is NULL otherwise. */
4599 cp_binding_level *
4600 begin_scope (scope_kind kind, tree entity)
4602 cp_binding_level *scope;
4604 /* Reuse or create a struct for this binding level. */
4605 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
4607 scope = free_binding_level;
4608 free_binding_level = scope->level_chain;
4609 memset (scope, 0, sizeof (cp_binding_level));
4611 else
4612 scope = ggc_cleared_alloc<cp_binding_level> ();
4614 scope->this_entity = entity;
4615 scope->more_cleanups_ok = true;
4616 switch (kind)
4618 case sk_cleanup:
4619 scope->keep = true;
4620 break;
4622 case sk_template_spec:
4623 scope->explicit_spec_p = true;
4624 kind = sk_template_parms;
4625 /* Fall through. */
4626 case sk_template_parms:
4627 case sk_block:
4628 case sk_try:
4629 case sk_catch:
4630 case sk_for:
4631 case sk_cond:
4632 case sk_class:
4633 case sk_scoped_enum:
4634 case sk_transaction:
4635 case sk_omp:
4636 case sk_stmt_expr:
4637 scope->keep = keep_next_level_flag;
4638 break;
4640 case sk_function_parms:
4641 scope->keep = keep_next_level_flag;
4642 break;
4644 case sk_namespace:
4645 NAMESPACE_LEVEL (entity) = scope;
4646 break;
4648 default:
4649 /* Should not happen. */
4650 gcc_unreachable ();
4651 break;
4653 scope->kind = kind;
4655 push_binding_level (scope);
4657 return scope;
4660 /* We're about to leave current scope. Pop the top of the stack of
4661 currently active scopes. Return the enclosing scope, now active. */
4663 cp_binding_level *
4664 leave_scope (void)
4666 cp_binding_level *scope = current_binding_level;
4668 if (scope->kind == sk_namespace && class_binding_level)
4669 current_binding_level = class_binding_level;
4671 /* We cannot leave a scope, if there are none left. */
4672 if (NAMESPACE_LEVEL (global_namespace))
4673 gcc_assert (!global_scope_p (scope));
4675 if (ENABLE_SCOPE_CHECKING)
4677 indent (--binding_depth);
4678 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4679 "leave");
4682 /* Move one nesting level up. */
4683 current_binding_level = scope->level_chain;
4685 /* Namespace-scopes are left most probably temporarily, not
4686 completely; they can be reopened later, e.g. in namespace-extension
4687 or any name binding activity that requires us to resume a
4688 namespace. For classes, we cache some binding levels. For other
4689 scopes, we just make the structure available for reuse. */
4690 if (scope->kind != sk_namespace
4691 && scope != previous_class_level)
4693 scope->level_chain = free_binding_level;
4694 gcc_assert (!ENABLE_SCOPE_CHECKING
4695 || scope->binding_depth == binding_depth);
4696 free_binding_level = scope;
4699 if (scope->kind == sk_class)
4701 /* Reset DEFINING_CLASS_P to allow for reuse of a
4702 class-defining scope in a non-defining context. */
4703 scope->defining_class_p = 0;
4705 /* Find the innermost enclosing class scope, and reset
4706 CLASS_BINDING_LEVEL appropriately. */
4707 class_binding_level = NULL;
4708 for (scope = current_binding_level; scope; scope = scope->level_chain)
4709 if (scope->kind == sk_class)
4711 class_binding_level = scope;
4712 break;
4716 return current_binding_level;
4719 /* When we exit a toplevel class scope, we save its binding level so
4720 that we can restore it quickly. Here, we've entered some other
4721 class, so we must invalidate our cache. */
4723 void
4724 invalidate_class_lookup_cache (void)
4726 previous_class_level->level_chain = free_binding_level;
4727 free_binding_level = previous_class_level;
4728 previous_class_level = NULL;
4731 static void
4732 resume_scope (cp_binding_level* b)
4734 /* Resuming binding levels is meant only for namespaces,
4735 and those cannot nest into classes. */
4736 gcc_assert (!class_binding_level);
4737 /* Also, resuming a non-directly nested namespace is a no-no. */
4738 gcc_assert (b->level_chain == current_binding_level);
4739 current_binding_level = b;
4740 if (ENABLE_SCOPE_CHECKING)
4742 b->binding_depth = binding_depth;
4743 indent (binding_depth);
4744 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
4745 binding_depth++;
4749 /* Return the innermost binding level that is not for a class scope. */
4751 static cp_binding_level *
4752 innermost_nonclass_level (void)
4754 cp_binding_level *b;
4756 b = current_binding_level;
4757 while (b->kind == sk_class)
4758 b = b->level_chain;
4760 return b;
4763 /* We're defining an object of type TYPE. If it needs a cleanup, but
4764 we're not allowed to add any more objects with cleanups to the current
4765 scope, create a new binding level. */
4767 void
4768 maybe_push_cleanup_level (tree type)
4770 if (type != error_mark_node
4771 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4772 && current_binding_level->more_cleanups_ok == 0)
4774 begin_scope (sk_cleanup, NULL);
4775 current_binding_level->statement_list = push_stmt_list ();
4779 /* Return true if we are in the global binding level. */
4781 bool
4782 global_bindings_p (void)
4784 return global_scope_p (current_binding_level);
4787 /* True if we are currently in a toplevel binding level. This
4788 means either the global binding level or a namespace in a toplevel
4789 binding level. Since there are no non-toplevel namespace levels,
4790 this really means any namespace or template parameter level. We
4791 also include a class whose context is toplevel. */
4793 bool
4794 toplevel_bindings_p (void)
4796 cp_binding_level *b = innermost_nonclass_level ();
4798 return b->kind == sk_namespace || b->kind == sk_template_parms;
4801 /* True if this is a namespace scope, or if we are defining a class
4802 which is itself at namespace scope, or whose enclosing class is
4803 such a class, etc. */
4805 bool
4806 namespace_bindings_p (void)
4808 cp_binding_level *b = innermost_nonclass_level ();
4810 return b->kind == sk_namespace;
4813 /* True if the innermost non-class scope is a block scope. */
4815 bool
4816 local_bindings_p (void)
4818 cp_binding_level *b = innermost_nonclass_level ();
4819 return b->kind < sk_function_parms || b->kind == sk_omp;
4822 /* True if the current level needs to have a BLOCK made. */
4824 bool
4825 kept_level_p (void)
4827 return (current_binding_level->blocks != NULL_TREE
4828 || current_binding_level->keep
4829 || current_binding_level->kind == sk_cleanup
4830 || current_binding_level->names != NULL_TREE
4831 || current_binding_level->using_directives);
4834 /* Returns the kind of the innermost scope. */
4836 scope_kind
4837 innermost_scope_kind (void)
4839 return current_binding_level->kind;
4842 /* Returns true if this scope was created to store template parameters. */
4844 bool
4845 template_parm_scope_p (void)
4847 return innermost_scope_kind () == sk_template_parms;
4850 /* If KEEP is true, make a BLOCK node for the next binding level,
4851 unconditionally. Otherwise, use the normal logic to decide whether
4852 or not to create a BLOCK. */
4854 void
4855 keep_next_level (bool keep)
4857 keep_next_level_flag = keep;
4860 /* Return the list of declarations of the current local scope. */
4862 tree
4863 get_local_decls (void)
4865 gcc_assert (current_binding_level->kind != sk_namespace
4866 && current_binding_level->kind != sk_class);
4867 return current_binding_level->names;
4870 /* Return how many function prototypes we are currently nested inside. */
4873 function_parm_depth (void)
4875 int level = 0;
4876 cp_binding_level *b;
4878 for (b = current_binding_level;
4879 b->kind == sk_function_parms;
4880 b = b->level_chain)
4881 ++level;
4883 return level;
4886 /* For debugging. */
4887 static int no_print_functions = 0;
4888 static int no_print_builtins = 0;
4890 static void
4891 print_binding_level (cp_binding_level* lvl)
4893 tree t;
4894 int i = 0, len;
4895 if (lvl->this_entity)
4896 print_node_brief (stderr, "entity=", lvl->this_entity, 1);
4897 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
4898 if (lvl->more_cleanups_ok)
4899 fprintf (stderr, " more-cleanups-ok");
4900 if (lvl->have_cleanups)
4901 fprintf (stderr, " have-cleanups");
4902 fprintf (stderr, "\n");
4903 if (lvl->names)
4905 fprintf (stderr, " names:\t");
4906 /* We can probably fit 3 names to a line? */
4907 for (t = lvl->names; t; t = TREE_CHAIN (t))
4909 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
4910 continue;
4911 if (no_print_builtins
4912 && (TREE_CODE (t) == TYPE_DECL)
4913 && DECL_IS_UNDECLARED_BUILTIN (t))
4914 continue;
4916 /* Function decls tend to have longer names. */
4917 if (TREE_CODE (t) == FUNCTION_DECL)
4918 len = 3;
4919 else
4920 len = 2;
4921 i += len;
4922 if (i > 6)
4924 fprintf (stderr, "\n\t");
4925 i = len;
4927 print_node_brief (stderr, "", t, 0);
4928 if (t == error_mark_node)
4929 break;
4931 if (i)
4932 fprintf (stderr, "\n");
4934 if (vec_safe_length (lvl->class_shadowed))
4936 size_t i;
4937 cp_class_binding *b;
4938 fprintf (stderr, " class-shadowed:");
4939 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
4940 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
4941 fprintf (stderr, "\n");
4943 if (lvl->type_shadowed)
4945 fprintf (stderr, " type-shadowed:");
4946 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
4948 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
4950 fprintf (stderr, "\n");
4954 DEBUG_FUNCTION void
4955 debug (cp_binding_level &ref)
4957 print_binding_level (&ref);
4960 DEBUG_FUNCTION void
4961 debug (cp_binding_level *ptr)
4963 if (ptr)
4964 debug (*ptr);
4965 else
4966 fprintf (stderr, "<nil>\n");
4969 static void
4970 print_other_binding_stack (cp_binding_level *stack)
4972 cp_binding_level *level;
4973 for (level = stack; !global_scope_p (level); level = level->level_chain)
4975 fprintf (stderr, "binding level %p\n", (void *) level);
4976 print_binding_level (level);
4980 DEBUG_FUNCTION void
4981 print_binding_stack (void)
4983 cp_binding_level *b;
4984 fprintf (stderr, "current_binding_level=%p\n"
4985 "class_binding_level=%p\n"
4986 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4987 (void *) current_binding_level, (void *) class_binding_level,
4988 (void *) NAMESPACE_LEVEL (global_namespace));
4989 if (class_binding_level)
4991 for (b = class_binding_level; b; b = b->level_chain)
4992 if (b == current_binding_level)
4993 break;
4994 if (b)
4995 b = class_binding_level;
4996 else
4997 b = current_binding_level;
4999 else
5000 b = current_binding_level;
5001 print_other_binding_stack (b);
5002 fprintf (stderr, "global:\n");
5003 print_binding_level (NAMESPACE_LEVEL (global_namespace));
5006 /* Push a definition of struct, union or enum tag named ID. into
5007 binding_level B. DECL is a TYPE_DECL for the type. DECL has
5008 already been pushed into its binding level. This is bookkeeping to
5009 find it easily. */
5011 static void
5012 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
5014 if (b->kind == sk_namespace)
5015 /* At namespace scope we should not see an identifier type value. */
5016 gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
5017 /* We could be pushing a friend underneath a template
5018 parm (ill-formed). */
5019 || (TEMPLATE_PARM_P
5020 (TYPE_NAME (REAL_IDENTIFIER_TYPE_VALUE (id)))));
5021 else
5023 /* Push the current type value, so we can restore it later */
5024 tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
5025 b->type_shadowed = tree_cons (id, old, b->type_shadowed);
5026 tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
5027 TREE_TYPE (b->type_shadowed) = type;
5028 SET_IDENTIFIER_TYPE_VALUE (id, type);
5032 /* As set_identifier_type_value_with_scope, but using
5033 current_binding_level. */
5035 void
5036 set_identifier_type_value (tree id, tree decl)
5038 set_identifier_type_value_with_scope (id, decl, current_binding_level);
5041 /* Return the name for the constructor (or destructor) for the
5042 specified class. */
5044 tree
5045 constructor_name (tree type)
5047 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
5049 return decl ? DECL_NAME (decl) : NULL_TREE;
5052 /* Returns TRUE if NAME is the name for the constructor for TYPE,
5053 which must be a class type. */
5055 bool
5056 constructor_name_p (tree name, tree type)
5058 gcc_assert (MAYBE_CLASS_TYPE_P (type));
5060 /* These don't have names. */
5061 if (TREE_CODE (type) == DECLTYPE_TYPE
5062 || TREE_CODE (type) == TYPEOF_TYPE)
5063 return false;
5065 if (name && name == constructor_name (type))
5066 return true;
5068 return false;
5071 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
5072 caller to set DECL_CONTEXT properly.
5074 Warning: For class and block-scope this must only be used when X
5075 will be the new innermost binding for its name, as we tack it onto
5076 the front of IDENTIFIER_BINDING without checking to see if the
5077 current IDENTIFIER_BINDING comes from a closer binding level than
5078 LEVEL.
5080 Warning: For namespace scope, this will look in LEVEL for an
5081 existing binding to match, but if not found will push the decl into
5082 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
5083 pop_nested_namespace if you really need to push it into a foreign
5084 namespace. */
5086 static tree
5087 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
5089 cp_binding_level *b;
5091 if (level->kind == sk_class)
5093 gcc_checking_assert (!hiding);
5094 b = class_binding_level;
5095 class_binding_level = level;
5096 pushdecl_class_level (x);
5097 class_binding_level = b;
5099 else
5101 tree function_decl = current_function_decl;
5102 if (level->kind == sk_namespace)
5103 current_function_decl = NULL_TREE;
5104 b = current_binding_level;
5105 current_binding_level = level;
5106 x = pushdecl (x, hiding);
5107 current_binding_level = b;
5108 current_function_decl = function_decl;
5110 return x;
5113 /* Inject X into the local scope just before the function parms. */
5115 tree
5116 pushdecl_outermost_localscope (tree x)
5118 cp_binding_level *b = NULL;
5119 auto_cond_timevar tv (TV_NAME_LOOKUP);
5121 /* Find the scope just inside the function parms. */
5122 for (cp_binding_level *n = current_binding_level;
5123 n->kind != sk_function_parms; n = b->level_chain)
5124 b = n;
5126 return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
5129 /* Checks if BINDING is a binding that we can export. */
5131 static bool
5132 check_can_export_using_decl (tree binding)
5134 tree decl = STRIP_TEMPLATE (binding);
5136 /* Linkage is determined by the owner of an enumerator. */
5137 if (TREE_CODE (decl) == CONST_DECL)
5138 decl = TYPE_NAME (DECL_CONTEXT (decl));
5140 /* If the using decl is exported, the things it refers
5141 to must also be exported (or not have module attachment). */
5142 if (!DECL_MODULE_EXPORT_P (decl)
5143 && (DECL_LANG_SPECIFIC (decl)
5144 && DECL_MODULE_ATTACH_P (decl)))
5146 bool internal_p = !TREE_PUBLIC (decl);
5148 /* A template in an anonymous namespace doesn't constrain TREE_PUBLIC
5149 until it's instantiated, so double-check its context. */
5150 if (!internal_p && TREE_CODE (binding) == TEMPLATE_DECL)
5151 internal_p = decl_internal_context_p (decl);
5153 auto_diagnostic_group d;
5154 error ("exporting %q#D that does not have external linkage",
5155 binding);
5156 if (TREE_CODE (decl) == TYPE_DECL && !DECL_IMPLICIT_TYPEDEF_P (decl))
5157 /* An un-exported explicit type alias has no linkage. */
5158 inform (DECL_SOURCE_LOCATION (binding),
5159 "%q#D declared here with no linkage", binding);
5160 else if (internal_p)
5161 inform (DECL_SOURCE_LOCATION (binding),
5162 "%q#D declared here with internal linkage", binding);
5163 else
5164 inform (DECL_SOURCE_LOCATION (binding),
5165 "%q#D declared here with module linkage", binding);
5166 return false;
5169 return true;
5172 /* Process a local-scope or namespace-scope using declaration. LOOKUP
5173 is the result of qualified lookup (both value & type are
5174 significant). FN_SCOPE_P indicates if we're at function-scope (as
5175 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
5176 bindings, which are altered to reflect the newly brought in
5177 declarations. */
5179 static bool
5180 do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
5181 bool insert_p, tree *value_p, tree *type_p)
5183 tree value = *value_p;
5184 tree type = *type_p;
5185 bool failed = false;
5187 /* Shift the old and new bindings around so we're comparing class and
5188 enumeration names to each other. */
5189 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
5191 type = value;
5192 value = NULL_TREE;
5195 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
5197 lookup.type = lookup.value;
5198 lookup.value = NULL_TREE;
5201 /* Only process exporting if we're going to be inserting. */
5202 bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
5204 /* First do the value binding. */
5205 if (!lookup.value)
5206 /* Nothing (only implicit typedef found). */
5207 gcc_checking_assert (lookup.type);
5208 else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
5210 for (lkp_iterator usings (lookup.value); usings; ++usings)
5212 tree new_fn = *usings;
5213 bool exporting = revealing_p && module_exporting_p ();
5214 if (exporting)
5215 exporting = check_can_export_using_decl (new_fn);
5217 /* [namespace.udecl]
5219 If a function declaration in namespace scope or block
5220 scope has the same name and the same parameter types as a
5221 function introduced by a using declaration the program is
5222 ill-formed. */
5223 /* This seems overreaching, asking core -- why do we care
5224 about decls in the namespace that we cannot name (because
5225 they are not transitively imported. We just check the
5226 decls that are in this TU. */
5227 bool found = false;
5228 for (ovl_iterator old (value); !found && old; ++old)
5230 tree old_fn = *old;
5232 if (new_fn == old_fn)
5234 /* The function already exists in the current
5235 namespace. We will still want to insert it if
5236 it is revealing a not-revealed thing. */
5237 found = true;
5238 if (!revealing_p)
5240 else if (old.using_p ())
5242 if (exporting)
5243 /* Update in place. 'tis ok. */
5244 OVL_EXPORT_P (old.get_using ()) = true;
5247 else if (DECL_MODULE_EXPORT_P (new_fn))
5249 else
5251 value = old.remove_node (value);
5252 found = false;
5254 break;
5256 else if (old.using_p ())
5257 continue; /* This is a using decl. */
5258 else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
5259 continue; /* This is an anticipated builtin. */
5260 else if (!matching_fn_p (new_fn, old_fn))
5261 continue; /* Parameters do not match. */
5262 else if (decls_match (new_fn, old_fn))
5264 /* Extern "C" in different namespaces. */
5265 found = true;
5266 break;
5268 else
5270 diagnose_name_conflict (new_fn, old_fn);
5271 failed = true;
5272 found = true;
5273 break;
5277 if (!found && insert_p)
5278 /* Unlike the decl-pushing case we don't drop anticipated
5279 builtins here. They don't cause a problem, and we'd
5280 like to match them with a future declaration. */
5281 value = ovl_insert (new_fn, value, 1 + exporting);
5284 else if (value
5285 /* Ignore anticipated builtins. */
5286 && !anticipated_builtin_p (value)
5287 && (fn_scope_p || !decls_match (lookup.value, value)))
5289 diagnose_name_conflict (lookup.value, value);
5290 failed = true;
5292 else if (insert_p)
5294 if (revealing_p
5295 && module_exporting_p ()
5296 && check_can_export_using_decl (lookup.value)
5297 && lookup.value == value
5298 && !DECL_MODULE_EXPORT_P (value))
5300 /* We're redeclaring the same value, but this time as
5301 newly exported: make sure to mark it as such. */
5302 if (TREE_CODE (value) == TEMPLATE_DECL)
5304 DECL_MODULE_EXPORT_P (value) = true;
5306 tree result = DECL_TEMPLATE_RESULT (value);
5307 retrofit_lang_decl (result);
5308 DECL_MODULE_PURVIEW_P (result) = true;
5309 DECL_MODULE_EXPORT_P (result) = true;
5311 else
5313 retrofit_lang_decl (value);
5314 DECL_MODULE_PURVIEW_P (value) = true;
5315 DECL_MODULE_EXPORT_P (value) = true;
5318 else
5319 value = lookup.value;
5322 /* Now the type binding. */
5323 if (lookup.type)
5325 if (type && !decls_match (lookup.type, type))
5327 diagnose_name_conflict (lookup.type, type);
5328 failed = true;
5330 else if (insert_p)
5332 if (revealing_p
5333 && module_exporting_p ()
5334 && check_can_export_using_decl (lookup.type)
5335 && lookup.type == type
5336 && !DECL_MODULE_EXPORT_P (type))
5338 /* We're redeclaring the same type, but this time as
5339 newly exported: make sure to mark it as such. */
5340 retrofit_lang_decl (type);
5341 DECL_MODULE_PURVIEW_P (type) = true;
5342 DECL_MODULE_EXPORT_P (type) = true;
5344 else
5345 type = lookup.type;
5349 if (insert_p)
5351 /* If value is empty, shift any class or enumeration name back. */
5352 if (!value)
5354 value = type;
5355 type = NULL_TREE;
5357 *value_p = value;
5358 *type_p = type;
5361 return failed;
5364 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5365 Both are namespaces. */
5367 bool
5368 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
5370 int depth = SCOPE_DEPTH (ancestor);
5372 if (!depth && !inline_only)
5373 /* The global namespace encloses everything. */
5374 return true;
5376 while (SCOPE_DEPTH (descendant) > depth
5377 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
5378 descendant = CP_DECL_CONTEXT (descendant);
5380 return ancestor == descendant;
5383 /* Returns true if ROOT (a non-alias namespace, class, or function)
5384 encloses CHILD. CHILD may be either a class type or a namespace
5385 (maybe alias). */
5387 bool
5388 is_ancestor (tree root, tree child)
5390 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
5391 && !DECL_NAMESPACE_ALIAS (root))
5392 || TREE_CODE (root) == FUNCTION_DECL
5393 || CLASS_TYPE_P (root));
5394 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
5395 || CLASS_TYPE_P (child));
5397 /* The global namespace encloses everything. Early-out for the
5398 common case. */
5399 if (root == global_namespace)
5400 return true;
5402 /* Search CHILD until we reach namespace scope. */
5403 while (TREE_CODE (child) != NAMESPACE_DECL)
5405 /* If we've reached the ROOT, it encloses CHILD. */
5406 if (root == child)
5407 return true;
5409 /* Go out one level. */
5410 if (TYPE_P (child))
5411 child = TYPE_NAME (child);
5412 child = CP_DECL_CONTEXT (child);
5415 if (TREE_CODE (root) != NAMESPACE_DECL)
5416 /* Failed to meet the non-namespace we were looking for. */
5417 return false;
5419 if (tree alias = DECL_NAMESPACE_ALIAS (child))
5420 child = alias;
5422 return is_nested_namespace (root, child);
5425 /* Enter the class or namespace scope indicated by T suitable for name
5426 lookup. T can be arbitrary scope, not necessary nested inside the
5427 current scope. Returns a non-null scope to pop iff pop_scope
5428 should be called later to exit this scope. */
5430 tree
5431 push_scope (tree t)
5433 if (TREE_CODE (t) == NAMESPACE_DECL)
5434 push_decl_namespace (t);
5435 else if (CLASS_TYPE_P (t))
5437 if (!at_class_scope_p ()
5438 || !same_type_p (current_class_type, t))
5439 push_nested_class (t);
5440 else
5441 /* T is the same as the current scope. There is therefore no
5442 need to re-enter the scope. Since we are not actually
5443 pushing a new scope, our caller should not call
5444 pop_scope. */
5445 t = NULL_TREE;
5448 return t;
5451 /* Leave scope pushed by push_scope. */
5453 void
5454 pop_scope (tree t)
5456 if (t == NULL_TREE)
5457 return;
5458 if (TREE_CODE (t) == NAMESPACE_DECL)
5459 pop_decl_namespace ();
5460 else if CLASS_TYPE_P (t)
5461 pop_nested_class ();
5464 /* Subroutine of push_inner_scope. */
5466 static void
5467 push_inner_scope_r (tree outer, tree inner)
5469 tree prev;
5471 if (outer == inner
5472 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5473 return;
5475 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5476 if (outer != prev)
5477 push_inner_scope_r (outer, prev);
5478 if (TREE_CODE (inner) == NAMESPACE_DECL)
5480 cp_binding_level *save_template_parm = 0;
5481 /* Temporary take out template parameter scopes. They are saved
5482 in reversed order in save_template_parm. */
5483 while (current_binding_level->kind == sk_template_parms)
5485 cp_binding_level *b = current_binding_level;
5486 current_binding_level = b->level_chain;
5487 b->level_chain = save_template_parm;
5488 save_template_parm = b;
5491 resume_scope (NAMESPACE_LEVEL (inner));
5492 current_namespace = inner;
5494 /* Restore template parameter scopes. */
5495 while (save_template_parm)
5497 cp_binding_level *b = save_template_parm;
5498 save_template_parm = b->level_chain;
5499 b->level_chain = current_binding_level;
5500 current_binding_level = b;
5503 else
5504 pushclass (inner);
5507 /* Enter the scope INNER from current scope. INNER must be a scope
5508 nested inside current scope. This works with both name lookup and
5509 pushing name into scope. In case a template parameter scope is present,
5510 namespace is pushed under the template parameter scope according to
5511 name lookup rule in 14.6.1/6.
5513 Return the former current scope suitable for pop_inner_scope. */
5515 tree
5516 push_inner_scope (tree inner)
5518 tree outer = current_scope ();
5519 if (!outer)
5520 outer = current_namespace;
5522 push_inner_scope_r (outer, inner);
5523 return outer;
5526 /* Exit the current scope INNER back to scope OUTER. */
5528 void
5529 pop_inner_scope (tree outer, tree inner)
5531 if (outer == inner
5532 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5533 return;
5535 while (outer != inner)
5537 if (TREE_CODE (inner) == NAMESPACE_DECL)
5539 cp_binding_level *save_template_parm = 0;
5540 /* Temporary take out template parameter scopes. They are saved
5541 in reversed order in save_template_parm. */
5542 while (current_binding_level->kind == sk_template_parms)
5544 cp_binding_level *b = current_binding_level;
5545 current_binding_level = b->level_chain;
5546 b->level_chain = save_template_parm;
5547 save_template_parm = b;
5550 pop_namespace ();
5552 /* Restore template parameter scopes. */
5553 while (save_template_parm)
5555 cp_binding_level *b = save_template_parm;
5556 save_template_parm = b->level_chain;
5557 b->level_chain = current_binding_level;
5558 current_binding_level = b;
5561 else
5562 popclass ();
5564 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5568 /* Do a pushlevel for class declarations. */
5570 void
5571 pushlevel_class (void)
5573 class_binding_level = begin_scope (sk_class, current_class_type);
5576 /* ...and a poplevel for class declarations. */
5578 void
5579 poplevel_class (void)
5581 cp_binding_level *level = class_binding_level;
5582 cp_class_binding *cb;
5583 size_t i;
5584 tree shadowed;
5586 auto_cond_timevar tv (TV_NAME_LOOKUP);
5587 gcc_assert (level != 0);
5589 /* If we're leaving a toplevel class, cache its binding level. */
5590 if (current_class_depth == 1)
5591 previous_class_level = level;
5592 for (shadowed = level->type_shadowed;
5593 shadowed;
5594 shadowed = TREE_CHAIN (shadowed))
5595 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
5597 /* Remove the bindings for all of the class-level declarations. */
5598 if (level->class_shadowed)
5600 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
5602 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
5603 cxx_binding_free (cb->base);
5605 ggc_free (level->class_shadowed);
5606 level->class_shadowed = NULL;
5609 /* Now, pop out of the binding level which we created up in the
5610 `pushlevel_class' routine. */
5611 gcc_assert (current_binding_level == level);
5612 leave_scope ();
5615 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5616 appropriate. DECL is the value to which a name has just been
5617 bound. CLASS_TYPE is the class in which the lookup occurred. */
5619 static void
5620 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
5621 tree class_type)
5623 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
5625 tree context;
5627 if (is_overloaded_fn (decl))
5628 context = ovl_scope (decl);
5629 else
5631 gcc_assert (DECL_P (decl));
5632 context = context_for_name_lookup (decl);
5635 if (is_properly_derived_from (class_type, context))
5636 INHERITED_VALUE_BINDING_P (binding) = 1;
5637 else
5638 INHERITED_VALUE_BINDING_P (binding) = 0;
5640 else if (binding->value == decl)
5641 /* We only encounter a TREE_LIST when there is an ambiguity in the
5642 base classes. Such an ambiguity can be overridden by a
5643 definition in this class. */
5644 INHERITED_VALUE_BINDING_P (binding) = 1;
5645 else
5646 INHERITED_VALUE_BINDING_P (binding) = 0;
5649 /* Make the declaration of X appear in CLASS scope. */
5651 bool
5652 pushdecl_class_level (tree x)
5654 bool is_valid = true;
5656 /* Do nothing if we're adding to an outer lambda closure type,
5657 outer_binding will add it later if it's needed. */
5658 if (current_class_type != class_binding_level->this_entity)
5659 return true;
5661 auto_cond_timevar tv (TV_NAME_LOOKUP);
5662 /* Get the name of X. */
5663 tree name = OVL_NAME (x);
5665 if (name)
5667 is_valid = push_class_level_binding (name, x);
5668 if (TREE_CODE (x) == TYPE_DECL)
5669 set_identifier_type_value (name, x);
5671 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
5673 /* If X is an anonymous aggregate, all of its members are
5674 treated as if they were members of the class containing the
5675 aggregate, for naming purposes. */
5676 location_t save_location = input_location;
5677 tree anon = TREE_TYPE (x);
5678 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
5679 for (unsigned ix = member_vec->length (); ix--;)
5681 tree binding = (*member_vec)[ix];
5682 if (STAT_HACK_P (binding))
5684 if (!pushdecl_class_level (STAT_TYPE (binding)))
5685 is_valid = false;
5686 binding = STAT_DECL (binding);
5688 if (!pushdecl_class_level (binding))
5689 is_valid = false;
5691 else
5692 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
5693 if (TREE_CODE (f) == FIELD_DECL)
5695 input_location = DECL_SOURCE_LOCATION (f);
5696 if (!pushdecl_class_level (f))
5697 is_valid = false;
5699 input_location = save_location;
5701 return is_valid;
5704 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5705 scope. If the value returned is non-NULL, and the PREVIOUS field
5706 is not set, callers must set the PREVIOUS field explicitly. */
5708 static cxx_binding *
5709 get_class_binding (tree name, cp_binding_level *scope)
5711 tree class_type;
5712 tree type_binding;
5713 tree value_binding;
5714 cxx_binding *binding;
5716 class_type = scope->this_entity;
5718 /* Get the type binding. */
5719 type_binding = lookup_member (class_type, name,
5720 /*protect=*/2, /*want_type=*/true,
5721 tf_warning_or_error);
5722 /* Get the value binding. */
5723 value_binding = lookup_member (class_type, name,
5724 /*protect=*/2, /*want_type=*/false,
5725 tf_warning_or_error);
5727 /* If we found either a type binding or a value binding, create a
5728 new binding object. */
5729 if (type_binding || value_binding)
5731 binding = new_class_binding (name,
5732 value_binding,
5733 type_binding,
5734 scope);
5735 set_inherited_value_binding_p (binding, value_binding, class_type);
5737 else
5738 binding = NULL;
5740 return binding;
5743 /* Make the declaration(s) of X appear in CLASS scope under the name
5744 NAME. Returns true if the binding is valid. */
5746 bool
5747 push_class_level_binding (tree name, tree x)
5749 cxx_binding *binding;
5750 tree decl = x;
5751 bool ok;
5753 auto_cond_timevar tv (TV_NAME_LOOKUP);
5755 /* The class_binding_level will be NULL if x is a template
5756 parameter name in a member template. */
5757 if (!class_binding_level)
5758 return true;
5760 if (name == error_mark_node)
5761 return false;
5763 /* Can happen for an erroneous declaration (c++/60384). */
5764 if (!identifier_p (name))
5766 gcc_assert (errorcount || sorrycount);
5767 return false;
5770 /* Check for invalid member names. But don't worry about a default
5771 argument-scope lambda being pushed after the class is complete. */
5772 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
5773 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
5774 /* Check that we're pushing into the right binding level. */
5775 gcc_assert (current_class_type == class_binding_level->this_entity);
5777 /* We could have been passed a tree list if this is an ambiguous
5778 declaration. If so, pull the declaration out because
5779 check_template_shadow will not handle a TREE_LIST. */
5780 if (TREE_CODE (decl) == TREE_LIST
5781 && TREE_TYPE (decl) == error_mark_node)
5782 decl = TREE_VALUE (decl);
5784 if (!check_template_shadow (decl))
5785 return false;
5787 /* [class.mem]
5789 If T is the name of a class, then each of the following shall
5790 have a name different from T:
5792 -- every static data member of class T;
5794 -- every member of class T that is itself a type;
5796 -- every enumerator of every member of class T that is an
5797 enumerated type;
5799 -- every member of every anonymous union that is a member of
5800 class T.
5802 (Non-static data members were also forbidden to have the same
5803 name as T until TC1.) */
5804 if ((VAR_P (x)
5805 || TREE_CODE (x) == CONST_DECL
5806 || (TREE_CODE (x) == TYPE_DECL
5807 && !DECL_SELF_REFERENCE_P (x))
5808 /* A data member of an anonymous union. */
5809 || (TREE_CODE (x) == FIELD_DECL
5810 && DECL_CONTEXT (x) != current_class_type))
5811 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
5813 tree scope = context_for_name_lookup (x);
5814 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
5816 error_at (DECL_SOURCE_LOCATION (x),
5817 "%qD has the same name as the class in which it is "
5818 "declared", x);
5819 return false;
5823 /* Get the current binding for NAME in this class, if any. */
5824 binding = IDENTIFIER_BINDING (name);
5825 if (!binding || binding->scope != class_binding_level)
5827 binding = get_class_binding (name, class_binding_level);
5828 /* If a new binding was created, put it at the front of the
5829 IDENTIFIER_BINDING list. */
5830 if (binding)
5832 binding->previous = IDENTIFIER_BINDING (name);
5833 IDENTIFIER_BINDING (name) = binding;
5837 /* If there is already a binding, then we may need to update the
5838 current value. */
5839 if (binding && binding->value)
5841 tree bval = binding->value;
5842 tree old_decl = NULL_TREE;
5843 tree target_decl = strip_using_decl (decl);
5844 tree target_bval = strip_using_decl (bval);
5846 if (INHERITED_VALUE_BINDING_P (binding))
5848 /* If the old binding was from a base class, and was for a
5849 tag name, slide it over to make room for the new binding.
5850 The old binding is still visible if explicitly qualified
5851 with a class-key. */
5852 if (TREE_CODE (target_bval) == TYPE_DECL
5853 && DECL_ARTIFICIAL (target_bval)
5854 && !(TREE_CODE (target_decl) == TYPE_DECL
5855 && DECL_ARTIFICIAL (target_decl)))
5857 old_decl = binding->type;
5858 binding->type = bval;
5859 binding->value = NULL_TREE;
5860 INHERITED_VALUE_BINDING_P (binding) = 0;
5862 else
5864 old_decl = bval;
5865 /* Any inherited type declaration is hidden by the type
5866 declaration in the derived class. */
5867 if (TREE_CODE (target_decl) == TYPE_DECL
5868 && DECL_ARTIFICIAL (target_decl))
5869 binding->type = NULL_TREE;
5872 else if (TREE_CODE (decl) == USING_DECL
5873 && TREE_CODE (bval) == USING_DECL
5874 && same_type_p (USING_DECL_SCOPE (decl),
5875 USING_DECL_SCOPE (bval)))
5876 /* This is a using redeclaration that will be diagnosed later
5877 in supplement_binding */
5879 else if (TREE_CODE (decl) == USING_DECL
5880 && TREE_CODE (bval) == USING_DECL
5881 && DECL_DEPENDENT_P (decl)
5882 && DECL_DEPENDENT_P (bval))
5883 return true;
5884 else if (TREE_CODE (decl) == USING_DECL
5885 && DECL_DEPENDENT_P (decl)
5886 && OVL_P (target_bval))
5887 /* The new dependent using beats an old overload. */
5888 old_decl = bval;
5889 else if (TREE_CODE (bval) == USING_DECL
5890 && DECL_DEPENDENT_P (bval)
5891 && OVL_P (target_decl))
5892 /* The old dependent using beats a new overload. */
5893 return true;
5894 else if (OVL_P (target_decl)
5895 && OVL_P (target_bval))
5896 /* The new overload set contains the old one. */
5897 old_decl = bval;
5899 if (old_decl && binding->scope == class_binding_level)
5901 binding->value = x;
5902 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5903 here. This function is only used to register bindings
5904 from with the class definition itself. */
5905 INHERITED_VALUE_BINDING_P (binding) = 0;
5906 return true;
5910 /* Note that we declared this value so that we can issue an error if
5911 this is an invalid redeclaration of a name already used for some
5912 other purpose. */
5913 note_name_declared_in_class (name, decl);
5915 /* If we didn't replace an existing binding, put the binding on the
5916 stack of bindings for the identifier, and update the shadowed
5917 list. */
5918 if (binding && binding->scope == class_binding_level)
5919 /* Supplement the existing binding. */
5920 ok = supplement_binding (binding, decl);
5921 else
5923 /* Create a new binding. */
5924 push_binding (name, decl, class_binding_level);
5925 ok = true;
5928 return ok;
5931 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5932 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5933 failure. */
5935 static tree
5936 lookup_using_decl (tree scope, name_lookup &lookup)
5938 tree current = current_scope ();
5939 bool dependent_p = false;
5940 tree binfo = NULL_TREE;
5941 base_kind b_kind = bk_not_base;
5943 /* Because C++20 breaks the invariant that only member using-decls
5944 refer to members and only non-member using-decls refer to
5945 non-members, we first do the lookups, and then do validation that
5946 what we found is ok. */
5948 if (TREE_CODE (scope) == ENUMERAL_TYPE
5949 && cxx_dialect < cxx20
5950 && UNSCOPED_ENUM_P (scope)
5951 && !TYPE_FUNCTION_SCOPE_P (scope))
5953 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5954 should allow it as meaning the enclosing scope. I don't see any
5955 justification for this in C++11, but let's keep allowing it. */
5956 tree ctx = CP_TYPE_CONTEXT (scope);
5957 if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
5958 scope = ctx;
5961 /* You cannot using-decl a destructor. */
5962 if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
5964 error ("%<%T%s%D%> names destructor", scope,
5965 &"::"[scope == global_namespace ? 2 : 0], lookup.name);
5966 return NULL_TREE;
5969 if (TREE_CODE (scope) == NAMESPACE_DECL)
5971 /* Naming a namespace member. */
5972 qualified_namespace_lookup (scope, &lookup);
5974 if (TYPE_P (current)
5975 && (!lookup.value
5976 || lookup.type
5977 || cxx_dialect < cxx20
5978 || TREE_CODE (lookup.value) != CONST_DECL))
5980 error ("using-declaration for non-member at class scope");
5981 return NULL_TREE;
5984 else if (TREE_CODE (scope) == ENUMERAL_TYPE)
5986 /* Naming an enumeration member. */
5987 if (cxx_dialect < cxx20)
5988 error ("%<using%> with enumeration scope %q#T "
5989 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5990 scope);
5991 lookup.value = lookup_enumerator (scope, lookup.name);
5993 else
5995 /* Naming a class member. This is awkward in C++20, because we
5996 might be naming an enumerator of an unrelated class. */
5998 tree npscope = scope;
5999 if (PACK_EXPANSION_P (scope))
6000 npscope = PACK_EXPANSION_PATTERN (scope);
6002 if (!MAYBE_CLASS_TYPE_P (npscope))
6004 error ("%qT is not a class, namespace, or enumeration", npscope);
6005 return NULL_TREE;
6008 /* Using T::T declares inheriting ctors, even if T is a typedef. */
6009 if (lookup.name == TYPE_IDENTIFIER (npscope)
6010 || constructor_name_p (lookup.name, npscope))
6012 if (!TYPE_P (current))
6014 error ("non-member using-declaration names constructor of %qT",
6015 npscope);
6016 return NULL_TREE;
6018 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
6019 lookup.name = ctor_identifier;
6020 CLASSTYPE_NON_AGGREGATE (current) = true;
6023 if (!TYPE_P (current) && cxx_dialect < cxx20)
6025 error ("using-declaration for member at non-class scope");
6026 return NULL_TREE;
6029 bool depscope = dependent_scope_p (scope);
6031 if (depscope)
6032 /* Leave binfo null. */;
6033 else if (TYPE_P (current))
6035 binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
6036 gcc_checking_assert (b_kind >= bk_not_base);
6038 if (b_kind == bk_not_base && any_dependent_bases_p ())
6039 /* Treat as-if dependent. */
6040 depscope = true;
6041 else if (lookup.name == ctor_identifier
6042 && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
6044 if (any_dependent_bases_p ())
6045 depscope = true;
6046 else
6048 error ("%qT is not a direct base of %qT", scope, current);
6049 return NULL_TREE;
6053 if (b_kind < bk_proper_base)
6054 binfo = TYPE_BINFO (scope);
6056 else
6057 binfo = TYPE_BINFO (scope);
6059 dependent_p = (depscope
6060 || (IDENTIFIER_CONV_OP_P (lookup.name)
6061 && dependent_type_p (TREE_TYPE (lookup.name))));
6063 if (!dependent_p)
6064 lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
6065 /*want_type=*/false, tf_none);
6067 /* If the lookup in the base contains a dependent using, this
6068 using is also dependent. */
6069 if (!dependent_p && lookup.value && dependent_type_p (scope))
6071 tree val = lookup.value;
6072 if (tree fns = maybe_get_fns (val))
6073 val = fns;
6074 for (tree f: lkp_range (val))
6075 if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
6077 dependent_p = true;
6078 break;
6082 if (!depscope && b_kind < bk_proper_base)
6084 if (cxx_dialect >= cxx20 && lookup.value
6085 && TREE_CODE (lookup.value) == CONST_DECL)
6087 /* Using an unrelated enum; check access here rather
6088 than separately for class and non-class using. */
6089 perform_or_defer_access_check
6090 (binfo, lookup.value, lookup.value, tf_warning_or_error);
6091 /* And then if this is a copy from handle_using_decl, look
6092 through to the original enumerator. */
6093 if (CONST_DECL_USING_P (lookup.value))
6094 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
6096 else if (!TYPE_P (current))
6098 error ("using-declaration for member at non-class scope");
6099 return NULL_TREE;
6101 else
6103 auto_diagnostic_group g;
6104 error_not_base_type (scope, current);
6105 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
6106 && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
6107 inform (input_location,
6108 "did you mean %<using enum %T::%D%>?",
6109 scope, lookup.name);
6110 return NULL_TREE;
6115 /* Did we find anything sane? */
6116 if (dependent_p)
6118 else if (!lookup.value)
6120 error ("%qD has not been declared in %qD", lookup.name, scope);
6121 return NULL_TREE;
6123 else if (TREE_CODE (lookup.value) == TREE_LIST
6124 /* We can (independently) have ambiguous implicit typedefs. */
6125 || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
6127 error ("reference to %qD is ambiguous", lookup.name);
6128 print_candidates (TREE_CODE (lookup.value) == TREE_LIST
6129 ? lookup.value : lookup.type);
6130 return NULL_TREE;
6132 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
6134 error ("using-declaration may not name namespace %qD", lookup.value);
6135 return NULL_TREE;
6138 if (TYPE_P (current))
6140 /* In class scope. */
6142 /* Cannot introduce a constructor name. */
6143 if (constructor_name_p (lookup.name, current))
6145 error ("%<%T::%D%> names constructor in %qT",
6146 scope, lookup.name, current);
6147 return NULL_TREE;
6150 if (lookup.value && BASELINK_P (lookup.value))
6151 /* The binfo from which the functions came does not matter. */
6152 lookup.value = BASELINK_FUNCTIONS (lookup.value);
6155 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
6156 USING_DECL_SCOPE (using_decl) = scope;
6157 USING_DECL_DECLS (using_decl) = lookup.value;
6158 DECL_DEPENDENT_P (using_decl) = dependent_p;
6159 DECL_CONTEXT (using_decl) = current;
6160 if (TYPE_P (current) && b_kind == bk_not_base)
6161 USING_DECL_UNRELATED_P (using_decl) = true;
6163 return using_decl;
6166 /* Process "using SCOPE::NAME" in a class scope. Return the
6167 USING_DECL created. */
6169 tree
6170 do_class_using_decl (tree scope, tree name)
6172 if (name == error_mark_node
6173 || scope == error_mark_node)
6174 return NULL_TREE;
6176 name_lookup lookup (name);
6177 return lookup_using_decl (scope, lookup);
6181 /* Return the binding for NAME in NS in the current TU. If NS is
6182 NULL, look in global_namespace. We will not find declarations
6183 from imports. Users of this who, having found nothing, push a new
6184 decl must be prepared for that pushing to match an existing decl. */
6186 tree
6187 get_namespace_binding (tree ns, tree name)
6189 auto_cond_timevar tv (TV_NAME_LOOKUP);
6190 if (!ns)
6191 ns = global_namespace;
6192 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
6193 tree ret = NULL_TREE;
6195 if (tree *b = find_namespace_slot (ns, name))
6197 ret = *b;
6199 if (TREE_CODE (ret) == BINDING_VECTOR)
6200 ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
6201 if (ret)
6202 ret = MAYBE_STAT_DECL (ret);
6205 return ret;
6208 /* Push internal DECL into the global namespace. Does not do the
6209 full overload fn handling and does not add it to the list of things
6210 in the namespace. */
6212 void
6213 set_global_binding (tree decl)
6215 auto_cond_timevar tv (TV_NAME_LOOKUP);
6217 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
6219 if (*slot)
6220 /* The user's placed something in the implementor's namespace. */
6221 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
6223 /* Force the binding, so compiler internals continue to work. */
6224 *slot = decl;
6227 /* Set the context of a declaration to scope. Complain if we are not
6228 outside scope. */
6230 void
6231 set_decl_namespace (tree decl, tree scope, bool friendp)
6233 /* Get rid of namespace aliases. */
6234 scope = ORIGINAL_NAMESPACE (scope);
6236 /* It is ok for friends to be qualified in parallel space. */
6237 if (!friendp && !is_nested_namespace (current_namespace, scope))
6238 error ("declaration of %qD not in a namespace surrounding %qD",
6239 decl, scope);
6240 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6242 /* See whether this has been declared in the namespace or inline
6243 children. */
6244 tree old = NULL_TREE;
6246 name_lookup lookup (DECL_NAME (decl),
6247 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
6248 if (!lookup.search_qualified (scope, /*usings=*/false))
6249 /* No old declaration at all. */
6250 goto not_found;
6251 old = lookup.value;
6254 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
6255 if (TREE_CODE (old) == TREE_LIST)
6257 ambiguous:
6258 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6259 error ("reference to %qD is ambiguous", decl);
6260 print_candidates (old);
6261 return;
6264 if (!DECL_DECLARES_FUNCTION_P (decl))
6266 /* Don't compare non-function decls with decls_match here, since
6267 it can't check for the correct constness at this
6268 point. pushdecl will find those errors later. */
6270 /* We might have found it in an inline namespace child of SCOPE. */
6271 if (TREE_CODE (decl) == TREE_CODE (old))
6272 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
6274 found:
6275 /* Writing "N::i" to declare something directly in "N" is invalid. */
6276 if (CP_DECL_CONTEXT (decl) == current_namespace
6277 && at_namespace_scope_p ())
6278 error_at (DECL_SOURCE_LOCATION (decl),
6279 "explicit qualification in declaration of %qD", decl);
6280 return;
6283 /* Since decl is a function, old should contain a function decl. */
6284 if (!OVL_P (old))
6286 not_found:
6287 /* It didn't work, go back to the explicit scope. */
6288 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6289 error ("%qD should have been declared inside %qD", decl, scope);
6291 return;
6294 /* We handle these in check_explicit_instantiation_namespace. */
6295 if (processing_explicit_instantiation)
6296 return;
6297 if (processing_template_decl || processing_specialization)
6298 /* We have not yet called push_template_decl to turn a
6299 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6300 match. But, we'll check later, when we construct the
6301 template. */
6302 return;
6304 /* Instantiations or specializations of templates may be declared as
6305 friends in any namespace. */
6306 if (friendp && DECL_USE_TEMPLATE (decl))
6307 return;
6309 tree found = NULL_TREE;
6310 bool hidden_p = false;
6311 bool saw_template = false;
6313 for (lkp_iterator iter (old); iter; ++iter)
6315 if (iter.using_p ())
6316 continue;
6318 tree ofn = *iter;
6320 /* Adjust DECL_CONTEXT first so decls_match will return true
6321 if DECL will match a declaration in an inline namespace. */
6322 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
6323 if (decls_match (decl, ofn))
6325 if (found)
6327 /* We found more than one matching declaration. This
6328 can happen if we have two inline namespace children,
6329 each containing a suitable declaration. */
6330 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6331 goto ambiguous;
6333 found = ofn;
6334 hidden_p = iter.hidden_p ();
6336 else if (TREE_CODE (decl) == FUNCTION_DECL
6337 && TREE_CODE (ofn) == TEMPLATE_DECL)
6338 saw_template = true;
6341 if (!found && friendp && saw_template)
6343 /* "[if no non-template match is found,] each remaining function template
6344 is replaced with the specialization chosen by deduction from the
6345 friend declaration or discarded if deduction fails."
6347 So tell check_explicit_specialization to look for a match. */
6348 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6349 DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
6350 return;
6353 if (found)
6355 if (hidden_p)
6357 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
6358 "%qD has not been declared within %qD", decl, scope);
6359 inform (DECL_SOURCE_LOCATION (found),
6360 "only here as a %<friend%>");
6362 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
6363 goto found;
6366 goto not_found;
6369 /* Return the namespace where the current declaration is declared. */
6371 tree
6372 current_decl_namespace (void)
6374 tree result;
6375 /* If we have been pushed into a different namespace, use it. */
6376 if (!vec_safe_is_empty (decl_namespace_list))
6377 return decl_namespace_list->last ();
6379 if (current_class_type)
6380 result = decl_namespace_context (current_class_type);
6381 else if (current_function_decl)
6382 result = decl_namespace_context (current_function_decl);
6383 else
6384 result = current_namespace;
6385 return result;
6388 /* Process any ATTRIBUTES on a namespace definition. Returns true if
6389 attribute visibility is seen. */
6391 bool
6392 handle_namespace_attrs (tree ns, tree attributes)
6394 tree d;
6395 bool saw_vis = false;
6397 if (attributes == error_mark_node)
6398 return false;
6400 for (d = attributes; d; d = TREE_CHAIN (d))
6402 tree name = get_attribute_name (d);
6403 tree args = TREE_VALUE (d);
6405 if (is_attribute_p ("visibility", name))
6407 /* attribute visibility is a property of the syntactic block
6408 rather than the namespace as a whole, so we don't touch the
6409 NAMESPACE_DECL at all. */
6410 tree x = args ? TREE_VALUE (args) : NULL_TREE;
6411 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
6413 warning (OPT_Wattributes,
6414 "%qD attribute requires a single NTBS argument",
6415 name);
6416 continue;
6419 if (!TREE_PUBLIC (ns))
6420 warning (OPT_Wattributes,
6421 "%qD attribute is meaningless since members of the "
6422 "anonymous namespace get local symbols", name);
6424 push_visibility (TREE_STRING_POINTER (x), 1);
6425 saw_vis = true;
6427 else if (is_attribute_p ("abi_tag", name))
6429 if (!DECL_NAME (ns))
6431 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6432 "namespace", name);
6433 continue;
6435 if (!DECL_NAMESPACE_INLINE_P (ns))
6437 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
6438 "namespace", name);
6439 continue;
6441 if (!args)
6443 tree dn = DECL_NAME (ns);
6444 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
6445 IDENTIFIER_POINTER (dn));
6446 TREE_TYPE (args) = char_array_type_node;
6447 args = fix_string_type (args);
6448 args = build_tree_list (NULL_TREE, args);
6450 if (check_abi_tag_args (args, name))
6451 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6452 DECL_ATTRIBUTES (ns));
6454 else if (is_attribute_p ("deprecated", name))
6456 if (!DECL_NAME (ns))
6458 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6459 "namespace", name);
6460 continue;
6462 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6464 error ("deprecated message is not a string");
6465 continue;
6467 TREE_DEPRECATED (ns) = 1;
6468 if (args)
6469 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6470 DECL_ATTRIBUTES (ns));
6472 else if (!attribute_ignored_p (d))
6474 warning (OPT_Wattributes, "%qD attribute directive ignored",
6475 name);
6476 continue;
6480 return saw_vis;
6483 /* Temporarily set the namespace for the current declaration. */
6485 void
6486 push_decl_namespace (tree decl)
6488 if (TREE_CODE (decl) != NAMESPACE_DECL)
6489 decl = decl_namespace_context (decl);
6490 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
6493 /* [namespace.memdef]/2 */
6495 void
6496 pop_decl_namespace (void)
6498 decl_namespace_list->pop ();
6501 /* Process a namespace-alias declaration. */
6503 void
6504 do_namespace_alias (tree alias, tree name_space)
6506 if (name_space == error_mark_node)
6507 return;
6509 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
6511 name_space = ORIGINAL_NAMESPACE (name_space);
6513 /* Build the alias. */
6514 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
6515 DECL_NAMESPACE_ALIAS (alias) = name_space;
6516 DECL_EXTERNAL (alias) = 1;
6517 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
6518 set_originating_module (alias);
6520 pushdecl (alias);
6522 /* Emit debug info for namespace alias. */
6523 if (!building_stmt_list_p ())
6524 (*debug_hooks->early_global_decl) (alias);
6527 /* Like pushdecl, only it places DECL in the current namespace,
6528 if appropriate. */
6530 tree
6531 pushdecl_namespace_level (tree decl, bool hiding)
6533 auto_cond_timevar tv (TV_NAME_LOOKUP);
6534 return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
6535 hiding);
6538 /* Wrapper around push_local_binding to push the bindings for
6539 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6540 is the result of name lookup during template parsing. */
6542 static void
6543 push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
6545 tree type = NULL_TREE;
6547 cxx_binding *binding = find_local_binding (current_binding_level, name);
6548 if (binding)
6550 value = binding->value;
6551 type = binding->type;
6554 /* DR 36 questions why using-decls at function scope may not be
6555 duplicates. Disallow it, as C++11 claimed and PR 20420
6556 implemented. */
6557 if (lookup)
6558 do_nonmember_using_decl (*lookup, true, true, &value, &type);
6560 if (!value)
6562 else if (binding && value == binding->value)
6563 /* Redeclaration of this USING_DECL. */;
6564 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
6566 /* We already have this binding, so replace it. */
6567 update_local_overload (IDENTIFIER_BINDING (name), value);
6568 IDENTIFIER_BINDING (name)->value = value;
6570 else
6571 /* Install the new binding. */
6572 push_local_binding (name, value, /*using=*/true);
6574 if (!type)
6576 else if (binding && type == binding->type)
6578 else
6580 push_local_binding (name, type, /*using=*/true);
6581 set_identifier_type_value (name, type);
6585 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6587 void
6588 push_using_decl_bindings (tree name, tree value)
6590 push_using_decl_bindings (nullptr, name, value);
6593 /* Process a using declaration in non-class scope. */
6595 void
6596 finish_nonmember_using_decl (tree scope, tree name)
6598 gcc_checking_assert (current_binding_level->kind != sk_class);
6600 if (scope == error_mark_node || name == error_mark_node)
6601 return;
6603 name_lookup lookup (name);
6605 tree using_decl = lookup_using_decl (scope, lookup);
6606 if (!using_decl)
6607 return;
6609 /* Emit debug info. */
6610 if (!processing_template_decl)
6611 cp_emit_debug_info_for_using (lookup.value,
6612 current_binding_level->this_entity);
6614 if (current_binding_level->kind == sk_namespace)
6616 tree *slot = find_namespace_slot (current_namespace, name, true);
6617 tree *mslot = get_fixed_binding_slot (slot, name,
6618 BINDING_SLOT_CURRENT, true);
6619 bool failed = false;
6621 if (mslot != slot)
6623 /* A module vector. I presume the binding list is going to
6624 be sparser than the import bitmap. Hence iterate over
6625 the former checking for bits set in the bitmap. */
6626 bitmap imports = get_import_bitmap ();
6627 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
6629 /* Scan the imported bindings. */
6630 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
6631 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
6633 ix--;
6634 cluster++;
6637 /* Do this in forward order, so we load modules in an order
6638 the user expects. */
6639 for (; ix--; cluster++)
6640 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
6642 /* Are we importing this module? */
6643 if (unsigned base = cluster->indices[jx].base)
6644 if (unsigned span = cluster->indices[jx].span)
6646 if (bitmap_bit_p (imports, base))
6647 goto found;
6648 while (++base, --span);
6649 continue;
6651 found:;
6652 /* Is it loaded? */
6653 if (cluster->slots[jx].is_lazy ())
6655 gcc_assert (cluster->indices[jx].span == 1);
6656 lazy_load_binding (cluster->indices[jx].base,
6657 scope, name, &cluster->slots[jx]);
6660 tree value = cluster->slots[jx];
6661 if (!value)
6662 /* Load errors could mean there's nothing here. */
6663 continue;
6665 /* Extract what we can see from here. If there's no
6666 stat_hack, then everything was exported. */
6667 tree type = NULL_TREE;
6669 /* If no stat hack, everything is visible. */
6670 if (STAT_HACK_P (value))
6672 if (STAT_TYPE_VISIBLE_P (value))
6673 type = STAT_TYPE (value);
6674 value = STAT_VISIBLE (value);
6677 if (do_nonmember_using_decl (lookup, false, false,
6678 &value, &type))
6680 failed = true;
6681 break;
6686 if (!failed)
6688 /* Now do the current slot. */
6689 tree value = MAYBE_STAT_DECL (*mslot);
6690 tree type = MAYBE_STAT_TYPE (*mslot);
6692 do_nonmember_using_decl (lookup, false, true, &value, &type);
6694 // FIXME: Partition mergeableness?
6695 if (STAT_HACK_P (*mslot))
6697 STAT_DECL (*mslot) = value;
6698 STAT_TYPE (*mslot) = type;
6700 else if (type)
6701 *mslot = stat_hack (value, type);
6702 else
6703 *mslot = value;
6706 else
6708 add_decl_expr (using_decl);
6709 if (DECL_DEPENDENT_P (using_decl))
6710 lookup.value = using_decl;
6711 push_using_decl_bindings (&lookup, name, NULL_TREE);
6715 /* Return the declarations that are members of the namespace NS. */
6717 tree
6718 cp_namespace_decls (tree ns)
6720 return NAMESPACE_LEVEL (ns)->names;
6723 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6724 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6726 static bool
6727 qualify_lookup (tree val, LOOK_want want)
6729 if (val == NULL_TREE)
6730 return false;
6732 if (bool (want & LOOK_want::TYPE))
6734 tree target_val = strip_using_decl (val);
6736 if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
6737 return true;
6740 if (bool (want & LOOK_want::TYPE_NAMESPACE))
6741 return TREE_CODE (val) == NAMESPACE_DECL;
6743 return true;
6746 /* Is there a "using namespace std;" directive within USINGS? */
6748 static bool
6749 using_directives_contain_std_p (vec<tree, va_gc> *usings)
6751 if (!usings)
6752 return false;
6754 for (unsigned ix = usings->length (); ix--;)
6755 if ((*usings)[ix] == std_node)
6756 return true;
6758 return false;
6761 /* Is there a "using namespace std;" directive within the current
6762 namespace (or its ancestors)?
6763 Compare with name_lookup::search_unqualified. */
6765 static bool
6766 has_using_namespace_std_directive_p ()
6768 for (cp_binding_level *level = current_binding_level;
6769 level;
6770 level = level->level_chain)
6771 if (using_directives_contain_std_p (level->using_directives))
6772 return true;
6774 return false;
6777 /* Subclass of deferred_diagnostic, for issuing a note when
6778 --param cxx-max-namespaces-for-diagnostic-help is reached.
6780 The note should be issued after the error, but before any other
6781 deferred diagnostics. This is handled by decorating a wrapped
6782 deferred_diagnostic, and emitting a note before that wrapped note is
6783 deleted. */
6785 class namespace_limit_reached : public deferred_diagnostic
6787 public:
6788 namespace_limit_reached (location_t loc, unsigned limit, tree name,
6789 std::unique_ptr<deferred_diagnostic> wrapped)
6790 : deferred_diagnostic (loc),
6791 m_limit (limit), m_name (name),
6792 m_wrapped (std::move (wrapped))
6796 ~namespace_limit_reached ()
6798 /* Unconditionally warn that the search was truncated. */
6799 inform (get_location (),
6800 "maximum limit of %d namespaces searched for %qE",
6801 m_limit, m_name);
6802 /* m_wrapped will be implicitly deleted after this, emitting any followup
6803 diagnostic after the above note. */
6806 private:
6807 unsigned m_limit;
6808 tree m_name;
6809 std::unique_ptr<deferred_diagnostic> m_wrapped;
6812 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6813 Emit a note showing the location of the declaration of the suggestion. */
6815 class show_candidate_location : public deferred_diagnostic
6817 public:
6818 show_candidate_location (location_t loc, tree candidate)
6819 : deferred_diagnostic (loc),
6820 m_candidate (candidate)
6824 ~show_candidate_location ()
6826 inform (location_of (m_candidate), "%qE declared here", m_candidate);
6829 private:
6830 tree m_candidate;
6833 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6834 to be suggested by suggest_alternatives_for.
6836 Emit a series of notes showing the various suggestions. */
6838 class suggest_alternatives : public deferred_diagnostic
6840 public:
6841 suggest_alternatives (location_t loc, vec<tree> candidates)
6842 : deferred_diagnostic (loc),
6843 m_candidates (candidates)
6847 ~suggest_alternatives ()
6849 if (m_candidates.length ())
6851 inform_n (get_location (), m_candidates.length (),
6852 "suggested alternative:",
6853 "suggested alternatives:");
6854 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
6856 tree val = m_candidates[ix];
6858 inform (location_of (val), " %qE", val);
6861 m_candidates.release ();
6864 private:
6865 vec<tree> m_candidates;
6868 /* A class for encapsulating the result of a search across
6869 multiple namespaces (and scoped enums within them) for an
6870 unrecognized name seen at a given source location. */
6872 class namespace_hints
6874 public:
6875 namespace_hints (location_t loc, tree name);
6877 name_hint convert_candidates_to_name_hint ();
6878 name_hint maybe_decorate_with_limit (name_hint);
6880 private:
6881 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
6883 location_t m_loc;
6884 tree m_name;
6885 vec<tree> m_candidates;
6887 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6888 unsigned m_limit;
6890 /* Was the limit reached? */
6891 bool m_limited;
6894 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6895 looking for an exact match for unrecognized NAME seen at LOC. */
6897 namespace_hints::namespace_hints (location_t loc, tree name)
6898 : m_loc(loc), m_name (name)
6900 auto_vec<tree> worklist;
6902 m_candidates = vNULL;
6903 m_limited = false;
6904 m_limit = param_cxx_max_namespaces_for_diagnostic_help;
6906 /* Breadth-first search of namespaces. Up to limit namespaces
6907 searched (limit zero == unlimited). */
6908 worklist.safe_push (global_namespace);
6909 for (unsigned ix = 0; ix != worklist.length (); ix++)
6911 tree ns = worklist[ix];
6912 name_lookup lookup (name);
6914 if (lookup.search_qualified (ns, false))
6915 m_candidates.safe_push (lookup.value);
6917 if (!m_limited)
6919 /* Look for child namespaces. We have to do this
6920 indirectly because they are chained in reverse order,
6921 which is confusing to the user. */
6922 auto_vec<tree> children;
6924 for (tree decl = NAMESPACE_LEVEL (ns)->names;
6925 decl; decl = TREE_CHAIN (decl))
6927 if (TREE_CODE (decl) == NAMESPACE_DECL
6928 && !DECL_NAMESPACE_ALIAS (decl)
6929 && !DECL_NAMESPACE_INLINE_P (decl))
6930 children.safe_push (decl);
6932 /* Look for exact matches for NAME within scoped enums.
6933 These aren't added to the worklist, and so don't count
6934 against the search limit. */
6935 if (TREE_CODE (decl) == TYPE_DECL)
6937 tree type = TREE_TYPE (decl);
6938 if (SCOPED_ENUM_P (type))
6939 maybe_add_candidate_for_scoped_enum (type, name);
6943 while (!m_limited && !children.is_empty ())
6945 if (worklist.length () == m_limit)
6946 m_limited = true;
6947 else
6948 worklist.safe_push (children.pop ());
6954 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6955 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6957 If m_candidates is non-empty, use it to generate a suggestion and/or
6958 a deferred diagnostic that lists the possible candidate(s).
6961 name_hint
6962 namespace_hints::convert_candidates_to_name_hint ()
6964 /* How many candidates do we have? */
6966 /* If we have just one candidate, issue a name_hint with it as a suggestion
6967 (so that consumers are able to suggest it within the error message and emit
6968 it as a fix-it hint), and with a note showing the candidate's location. */
6969 if (m_candidates.length () == 1)
6971 tree candidate = m_candidates[0];
6972 /* Clean up CANDIDATES. */
6973 m_candidates.release ();
6974 return name_hint (expr_to_string (candidate),
6975 new show_candidate_location (m_loc, candidate));
6977 else if (m_candidates.length () > 1)
6978 /* If we have more than one candidate, issue a name_hint without a single
6979 "suggestion", but with a deferred diagnostic that lists the
6980 various candidates. This takes ownership of m_candidates. */
6981 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
6983 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6984 gcc_assert (m_candidates.length () == 0);
6985 gcc_assert (m_candidates == vNULL);
6987 return name_hint ();
6990 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6991 then we want to emit a note about after the error, but before
6992 any other deferred diagnostics.
6994 Handle this by figuring out what hint is needed, then optionally
6995 decorating HINT with a namespace_limit_reached wrapper. */
6997 name_hint
6998 namespace_hints::maybe_decorate_with_limit (name_hint hint)
7000 if (m_limited)
7001 return name_hint (hint.suggestion (),
7002 new namespace_limit_reached (m_loc, m_limit,
7003 m_name,
7004 hint.take_deferred ()));
7005 else
7006 return hint;
7009 /* Look inside SCOPED_ENUM for exact matches for NAME.
7010 If one is found, add its CONST_DECL to m_candidates. */
7012 void
7013 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
7014 tree name)
7016 gcc_assert (SCOPED_ENUM_P (scoped_enum));
7018 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7020 tree id = TREE_PURPOSE (iter);
7021 if (id == name)
7023 m_candidates.safe_push (TREE_VALUE (iter));
7024 return;
7029 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7030 name lookup failed.
7032 Search through all available namespaces and any scoped enums within them
7033 and generate a suggestion and/or a deferred diagnostic that lists possible
7034 candidate(s).
7036 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
7037 look for near-matches and suggest the best near-match, if there is one.
7039 If nothing is found, then an empty name_hint is returned. */
7041 name_hint
7042 suggest_alternatives_for (location_t location, tree name,
7043 bool suggest_misspellings)
7045 /* First, search for exact matches in other namespaces. */
7046 namespace_hints ns_hints (location, name);
7047 name_hint result = ns_hints.convert_candidates_to_name_hint ();
7049 /* Otherwise, try other approaches. */
7050 if (!result)
7051 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
7053 return ns_hints.maybe_decorate_with_limit (std::move (result));
7056 /* The second half of suggest_alternatives_for, for when no exact matches
7057 were found in other namespaces. */
7059 static name_hint
7060 suggest_alternatives_for_1 (location_t location, tree name,
7061 bool suggest_misspellings)
7063 /* No candidates were found in the available namespaces. */
7065 /* If there's a "using namespace std;" active, and this
7066 is one of the most common "std::" names, then it's probably a
7067 missing #include. */
7068 if (has_using_namespace_std_directive_p ())
7070 name_hint hint = maybe_suggest_missing_std_header (location, name);
7071 if (hint)
7072 return hint;
7075 /* Otherwise, consider misspellings. */
7076 if (!suggest_misspellings)
7077 return name_hint ();
7079 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
7082 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7083 name lookup failed.
7085 Search through all available namespaces and generate a suggestion and/or
7086 a deferred diagnostic that lists possible candidate(s).
7088 This is similiar to suggest_alternatives_for, but doesn't fallback to
7089 the other approaches used by that function. */
7091 name_hint
7092 suggest_alternatives_in_other_namespaces (location_t location, tree name)
7094 namespace_hints ns_hints (location, name);
7096 name_hint result = ns_hints.convert_candidates_to_name_hint ();
7098 return ns_hints.maybe_decorate_with_limit (std::move (result));
7101 /* A well-known name within the C++ standard library, returned by
7102 get_std_name_hint.
7104 The gperf-generated file contains the definition of the class
7105 "std_name_hint_lookup" with a static member function which
7106 returns the pointer to a structure "std_name_hint" which
7107 is also defined in that file. */
7109 #include "std-name-hint.h"
7111 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
7112 for some of the most common names within "std::".
7113 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
7115 static const std_name_hint *
7116 get_std_name_hint (const char *name)
7118 return std_name_hint_lookup::find(name, strlen(name));
7121 /* Describe DIALECT. */
7123 const char *
7124 get_cxx_dialect_name (enum cxx_dialect dialect)
7126 switch (dialect)
7128 default:
7129 gcc_unreachable ();
7130 case cxx98:
7131 return "C++98";
7132 case cxx11:
7133 return "C++11";
7134 case cxx14:
7135 return "C++14";
7136 case cxx17:
7137 return "C++17";
7138 case cxx20:
7139 return "C++20";
7140 case cxx23:
7141 return "C++23";
7142 case cxx26:
7143 return "C++26";
7147 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7148 that weren't recognized, but for which we know which header it ought to be
7151 Emit a note either suggesting the header to be included, or noting that
7152 the current dialect is too early for the given name. */
7154 class missing_std_header : public deferred_diagnostic
7156 public:
7157 missing_std_header (location_t loc,
7158 const char *name_str,
7159 const std_name_hint *header_hint)
7160 : deferred_diagnostic (loc),
7161 m_name_str (name_str),
7162 m_header_hint (header_hint)
7164 ~missing_std_header ()
7166 gcc_rich_location richloc (get_location ());
7167 if (cxx_dialect >= m_header_hint->min_dialect)
7169 const char *header = m_header_hint->header;
7170 maybe_add_include_fixit (&richloc, header, true);
7171 inform (&richloc,
7172 "%<std::%s%> is defined in header %qs;"
7173 " this is probably fixable by adding %<#include %s%>",
7174 m_name_str, header, header);
7176 else
7177 inform (&richloc,
7178 "%<std::%s%> is only available from %s onwards",
7179 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
7182 private:
7183 const char *m_name_str;
7184 const std_name_hint *m_header_hint;
7187 /* Attempt to generate a name_hint that suggests pertinent header files
7188 for NAME at LOCATION, for common names within the "std" namespace,
7189 or an empty name_hint if this isn't applicable. */
7191 static name_hint
7192 maybe_suggest_missing_std_header (location_t location, tree name)
7194 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7196 const char *name_str = IDENTIFIER_POINTER (name);
7197 const std_name_hint *header_hint = get_std_name_hint (name_str);
7198 if (!header_hint)
7199 return name_hint ();
7201 return name_hint (NULL, new missing_std_header (location, name_str,
7202 header_hint));
7205 /* Attempt to generate a name_hint that suggests a missing header file
7206 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7207 applicable. */
7209 name_hint
7210 maybe_suggest_missing_header (location_t location, tree name, tree scope)
7212 if (scope == NULL_TREE)
7213 return name_hint ();
7214 if (TREE_CODE (scope) != NAMESPACE_DECL)
7215 return name_hint ();
7216 /* We only offer suggestions for the "std" namespace. */
7217 if (scope != std_node)
7218 return name_hint ();
7219 return maybe_suggest_missing_std_header (location, name);
7222 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7223 lookup failed within the explicitly provided SCOPE.
7225 Suggest the best meaningful candidates (if any), otherwise
7226 an empty name_hint is returned. */
7228 name_hint
7229 suggest_alternative_in_explicit_scope (location_t location, tree name,
7230 tree scope)
7232 /* Something went very wrong; don't suggest anything. */
7233 if (name == error_mark_node)
7234 return name_hint ();
7236 /* Resolve any namespace aliases. */
7237 scope = ORIGINAL_NAMESPACE (scope);
7239 name_hint hint = maybe_suggest_missing_header (location, name, scope);
7240 if (hint)
7241 return hint;
7243 cp_binding_level *level = NAMESPACE_LEVEL (scope);
7245 best_match <tree, const char *> bm (name);
7246 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
7248 /* See if we have a good suggesion for the user. */
7249 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
7250 if (fuzzy_name)
7251 return name_hint (fuzzy_name, NULL);
7253 return name_hint ();
7256 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7257 candidates. */
7259 name_hint
7260 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
7262 gcc_assert (SCOPED_ENUM_P (scoped_enum));
7264 best_match <tree, const char *> bm (name);
7265 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7267 tree id = TREE_PURPOSE (iter);
7268 bm.consider (IDENTIFIER_POINTER (id));
7270 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7273 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7274 or a class TYPE).
7276 WANT as for lookup_name_1.
7278 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7279 declaration found. If no suitable declaration can be found,
7280 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7281 neither a class-type nor a namespace a diagnostic is issued. */
7283 tree
7284 lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
7286 tree t = NULL_TREE;
7288 if (TREE_CODE (scope) == NAMESPACE_DECL)
7290 name_lookup lookup (name, want);
7292 if (qualified_namespace_lookup (scope, &lookup))
7294 t = lookup.value;
7296 /* If we have a known type overload, pull it out. This can happen
7297 for using decls. */
7298 if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
7299 t = OVL_FUNCTION (t);
7302 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
7303 t = lookup_enumerator (scope, name);
7304 else if (is_class_type (scope, complain))
7305 t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
7306 tf_warning_or_error);
7308 if (!t)
7309 return error_mark_node;
7310 return t;
7313 /* Wrapper for the above that takes a string argument. The function name is
7314 not at the beginning of the line to keep this wrapper out of etags. */
7316 tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
7318 return lookup_qualified_name (t, get_identifier (p), w, c);
7321 /* [namespace.qual]
7322 Accepts the NAME to lookup and its qualifying SCOPE.
7323 Returns the name/type pair found into the cxx_binding *RESULT,
7324 or false on error. */
7326 static bool
7327 qualified_namespace_lookup (tree scope, name_lookup *lookup)
7329 timevar_start (TV_NAME_LOOKUP);
7330 query_oracle (lookup->name);
7331 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
7332 timevar_stop (TV_NAME_LOOKUP);
7333 return found;
7336 /* If DECL is suitably visible to the user, consider its name for
7337 spelling correction. */
7339 static void
7340 consider_decl (tree decl, best_match <tree, const char *> &bm,
7341 bool consider_impl_names)
7343 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7344 within range for). */
7345 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7346 return;
7348 tree suggestion = DECL_NAME (decl);
7349 if (!suggestion)
7350 return;
7352 /* Don't suggest names that are for anonymous aggregate types, as
7353 they are an implementation detail generated by the compiler. */
7354 if (IDENTIFIER_ANON_P (suggestion))
7355 return;
7357 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
7359 /* Ignore internal names with spaces in them. */
7360 if (strchr (suggestion_str, ' '))
7361 return;
7363 /* Don't suggest names that are reserved for use by the
7364 implementation, unless NAME began with an underscore. */
7365 if (!consider_impl_names
7366 && name_reserved_for_implementation_p (suggestion_str))
7367 return;
7369 bm.consider (suggestion_str);
7372 /* If DECL is suitably visible to the user, add its name to VEC and
7373 return true. Otherwise return false. */
7375 static bool
7376 maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
7378 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7379 within range for). */
7380 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7381 return false;
7383 tree suggestion = DECL_NAME (decl);
7384 if (!suggestion)
7385 return false;
7387 /* Don't suggest names that are for anonymous aggregate types, as
7388 they are an implementation detail generated by the compiler. */
7389 if (IDENTIFIER_ANON_P (suggestion))
7390 return false;
7392 vec.safe_push (suggestion);
7394 return true;
7397 /* Examing the namespace binding BINDING, and add at most one instance
7398 of the name, if it contains a visible entity of interest. Return
7399 true if we added something. */
7401 bool
7402 maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
7403 lookup_name_fuzzy_kind kind)
7405 tree value = NULL_TREE;
7407 if (STAT_HACK_P (binding))
7409 if (!STAT_TYPE_HIDDEN_P (binding)
7410 && STAT_TYPE (binding))
7412 if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
7413 return true;
7415 else if (!STAT_DECL_HIDDEN_P (binding))
7416 value = STAT_DECL (binding);
7418 else
7419 value = binding;
7421 value = ovl_skip_hidden (value);
7422 if (value)
7424 value = OVL_FIRST (value);
7425 if (kind != FUZZY_LOOKUP_TYPENAME
7426 || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
7427 if (maybe_add_fuzzy_decl (vec, value))
7428 return true;
7431 /* Nothing found. */
7432 return false;
7435 /* Helper function for lookup_name_fuzzy.
7436 Traverse binding level LVL, looking for good name matches for NAME
7437 (and BM). */
7438 static void
7439 consider_binding_level (tree name, best_match <tree, const char *> &bm,
7440 cp_binding_level *lvl, bool look_within_fields,
7441 enum lookup_name_fuzzy_kind kind)
7443 if (look_within_fields)
7444 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
7446 tree type = lvl->this_entity;
7447 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
7448 tree best_matching_field
7449 = lookup_member_fuzzy (type, name, want_type_p);
7450 if (best_matching_field)
7451 bm.consider (IDENTIFIER_POINTER (best_matching_field));
7454 /* Only suggest names reserved for the implementation if NAME begins
7455 with an underscore. */
7456 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
7458 if (lvl->kind != sk_namespace)
7459 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
7461 tree d = t;
7463 /* OVERLOADs or decls from using declaration are wrapped into
7464 TREE_LIST. */
7465 if (TREE_CODE (d) == TREE_LIST)
7466 d = OVL_FIRST (TREE_VALUE (d));
7468 /* Don't use bindings from implicitly declared functions,
7469 as they were likely misspellings themselves. */
7470 if (TREE_TYPE (d) == error_mark_node)
7471 continue;
7473 /* If we want a typename, ignore non-types. */
7474 if (kind == FUZZY_LOOKUP_TYPENAME
7475 && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
7476 continue;
7478 consider_decl (d, bm, consider_implementation_names);
7480 else
7482 /* We need to iterate over the namespace hash table, in order to
7483 not mention hidden entities. But hash table iteration is
7484 (essentially) unpredictable, our correction-distance measure
7485 is very granular, and we pick the first of equal distances.
7486 Hence, we need to call the distance-measurer in a predictable
7487 order. So, iterate over the namespace hash, inserting
7488 visible names into a vector. Then sort the vector. Then
7489 determine spelling distance. */
7491 tree ns = lvl->this_entity;
7492 auto_vec<tree> vec;
7494 hash_table<named_decl_hash>::iterator end
7495 (DECL_NAMESPACE_BINDINGS (ns)->end ());
7496 for (hash_table<named_decl_hash>::iterator iter
7497 (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
7499 tree binding = *iter;
7501 if (TREE_CODE (binding) == BINDING_VECTOR)
7503 bitmap imports = get_import_bitmap ();
7504 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
7506 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
7507 if (maybe_add_fuzzy_binding (vec, bind, kind))
7508 continue;
7510 /* Scan the imported bindings. */
7511 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
7512 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7514 ix--;
7515 cluster++;
7518 for (; ix--; cluster++)
7519 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
7520 jx++)
7522 /* Are we importing this module? */
7523 if (unsigned base = cluster->indices[jx].base)
7524 if (unsigned span = cluster->indices[jx].span)
7526 if (bitmap_bit_p (imports, base))
7527 goto found;
7528 while (++base, --span);
7529 continue;
7531 found:;
7532 /* Is it loaded? */
7533 if (cluster->slots[jx].is_lazy ())
7534 /* Let's not read in everything on the first
7535 spello! **/
7536 continue;
7537 if (tree bind = cluster->slots[jx])
7538 if (maybe_add_fuzzy_binding (vec, bind, kind))
7539 break;
7542 else
7543 maybe_add_fuzzy_binding (vec, binding, kind);
7546 vec.qsort ([] (const void *a_, const void *b_)
7548 return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
7549 IDENTIFIER_POINTER (*(const tree *)b_));
7552 /* Examine longest to shortest. */
7553 for (unsigned ix = vec.length (); ix--;)
7555 const char *str = IDENTIFIER_POINTER (vec[ix]);
7557 /* Ignore internal names with spaces in them. */
7558 if (strchr (str, ' '))
7559 continue;
7561 /* Don't suggest names that are reserved for use by the
7562 implementation, unless NAME began with an underscore. */
7563 if (!consider_implementation_names
7564 && name_reserved_for_implementation_p (str))
7565 continue;
7567 bm.consider (str);
7572 /* Subclass of deferred_diagnostic. Notify the user that the
7573 given macro was used before it was defined.
7574 This can be done in the C++ frontend since tokenization happens
7575 upfront. */
7577 class macro_use_before_def : public deferred_diagnostic
7579 public:
7580 /* Factory function. Return a new macro_use_before_def instance if
7581 appropriate, or return NULL. */
7582 static macro_use_before_def *
7583 maybe_make (location_t use_loc, cpp_hashnode *macro)
7585 location_t def_loc = cpp_macro_definition_location (macro);
7586 if (def_loc == UNKNOWN_LOCATION)
7587 return NULL;
7589 /* We only want to issue a note if the macro was used *before* it was
7590 defined.
7591 We don't want to issue a note for cases where a macro was incorrectly
7592 used, leaving it unexpanded (e.g. by using the wrong argument
7593 count). */
7594 if (!linemap_location_before_p (line_table, use_loc, def_loc))
7595 return NULL;
7597 return new macro_use_before_def (use_loc, macro);
7600 private:
7601 /* Ctor. LOC is the location of the usage. MACRO is the
7602 macro that was used. */
7603 macro_use_before_def (location_t loc, cpp_hashnode *macro)
7604 : deferred_diagnostic (loc), m_macro (macro)
7606 gcc_assert (macro);
7609 ~macro_use_before_def ()
7611 if (is_suppressed_p ())
7612 return;
7614 inform (get_location (), "the macro %qs had not yet been defined",
7615 (const char *)m_macro->ident.str);
7616 inform (cpp_macro_definition_location (m_macro),
7617 "it was later defined here");
7620 private:
7621 cpp_hashnode *m_macro;
7624 /* Determine if it can ever make sense to offer RID as a suggestion for
7625 a misspelling.
7627 Subroutine of lookup_name_fuzzy. */
7629 static bool
7630 suggest_rid_p (enum rid rid)
7632 switch (rid)
7634 /* Support suggesting function-like keywords. */
7635 case RID_STATIC_ASSERT:
7636 return true;
7638 default:
7639 /* Support suggesting the various decl-specifier words, to handle
7640 e.g. "singed" vs "signed" typos. */
7641 if (cp_keyword_starts_decl_specifier_p (rid))
7642 return true;
7644 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7645 and "do" for short misspellings, which are likely to lead to
7646 nonsensical results. */
7647 return false;
7651 /* Search for near-matches for NAME within the current bindings, and within
7652 macro names, returning the best match as a const char *, or NULL if
7653 no reasonable match is found.
7655 Use LOC for any deferred diagnostics. */
7657 name_hint
7658 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
7660 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7662 /* First, try some well-known names in the C++ standard library, in case
7663 the user forgot a #include. */
7664 const char *header_hint
7665 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
7666 if (header_hint)
7667 return name_hint (NULL,
7668 new suggest_missing_header (loc,
7669 IDENTIFIER_POINTER (name),
7670 header_hint));
7672 best_match <tree, const char *> bm (name);
7674 cp_binding_level *lvl;
7675 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
7676 consider_binding_level (name, bm, lvl, true, kind);
7678 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
7679 consider_binding_level (name, bm, lvl, false, kind);
7681 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7683 x = SOME_OTHER_MACRO (y);
7684 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7685 as a misspelled identifier.
7687 Use the best distance so far so that a candidate is only set if
7688 a macro is better than anything so far. This allows early rejection
7689 (without calculating the edit distance) of macro names that must have
7690 distance >= bm.get_best_distance (), and means that we only get a
7691 non-NULL result for best_macro_match if it's better than any of
7692 the identifiers already checked. */
7693 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
7694 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
7695 /* If a macro is the closest so far to NAME, consider it. */
7696 if (best_macro)
7697 bm.consider ((const char *)best_macro->ident.str);
7698 else if (bmm.get_best_distance () == 0)
7700 /* If we have an exact match for a macro name, then either the
7701 macro was used with the wrong argument count, or the macro
7702 has been used before it was defined. */
7703 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
7704 if (cpp_user_macro_p (macro))
7705 return name_hint (NULL,
7706 macro_use_before_def::maybe_make (loc, macro));
7709 /* Try the "starts_decl_specifier_p" keywords to detect
7710 "singed" vs "signed" typos. */
7711 for (unsigned i = 0; i < num_c_common_reswords; i++)
7713 const c_common_resword *resword = &c_common_reswords[i];
7715 if (!suggest_rid_p (resword->rid))
7716 continue;
7718 tree resword_identifier = ridpointers [resword->rid];
7719 if (!resword_identifier)
7720 continue;
7721 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
7723 /* Only consider reserved words that survived the
7724 filtering in init_reswords (e.g. for -std). */
7725 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
7726 continue;
7728 bm.consider (IDENTIFIER_POINTER (resword_identifier));
7731 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7734 /* Subroutine of outer_binding.
7736 Returns TRUE if BINDING is a binding to a template parameter of
7737 SCOPE. In that case SCOPE is the scope of a primary template
7738 parameter -- in the sense of G++, i.e, a template that has its own
7739 template header.
7741 Returns FALSE otherwise. */
7743 static bool
7744 binding_to_template_parms_of_scope_p (cxx_binding *binding,
7745 cp_binding_level *scope)
7747 tree binding_value, tmpl, tinfo;
7748 int level;
7750 if (!binding || !scope || !scope->this_entity)
7751 return false;
7753 binding_value = binding->value ? binding->value : binding->type;
7754 tinfo = get_template_info (scope->this_entity);
7756 /* BINDING_VALUE must be a template parm. */
7757 if (binding_value == NULL_TREE
7758 || (!DECL_P (binding_value)
7759 || !DECL_TEMPLATE_PARM_P (binding_value)))
7760 return false;
7762 /* The level of BINDING_VALUE. */
7763 level =
7764 template_type_parameter_p (binding_value)
7765 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7766 (TREE_TYPE (binding_value)))
7767 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
7769 /* The template of the current scope, iff said scope is a primary
7770 template. */
7771 tmpl = (tinfo
7772 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
7773 ? TI_TEMPLATE (tinfo)
7774 : NULL_TREE);
7776 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7777 then BINDING_VALUE is a parameter of TMPL. */
7778 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
7781 /* Return the innermost non-namespace binding for NAME from a scope
7782 containing BINDING, or, if BINDING is NULL, the current scope.
7783 Please note that for a given template, the template parameters are
7784 considered to be in the scope containing the current scope.
7785 If CLASS_P is false, then class bindings are ignored. */
7787 cxx_binding *
7788 outer_binding (tree name,
7789 cxx_binding *binding,
7790 bool class_p)
7792 cxx_binding *outer;
7793 cp_binding_level *scope;
7794 cp_binding_level *outer_scope;
7796 if (binding)
7798 scope = binding->scope->level_chain;
7799 outer = binding->previous;
7801 else
7803 scope = current_binding_level;
7804 outer = IDENTIFIER_BINDING (name);
7806 outer_scope = outer ? outer->scope : NULL;
7808 /* Because we create class bindings lazily, we might be missing a
7809 class binding for NAME. If there are any class binding levels
7810 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7811 declared, we must lookup NAME in those class scopes. */
7812 if (class_p)
7813 while (scope && scope != outer_scope && scope->kind != sk_namespace)
7815 if (scope->kind == sk_class)
7817 cxx_binding *class_binding;
7819 class_binding = get_class_binding (name, scope);
7820 if (class_binding)
7822 /* Thread this new class-scope binding onto the
7823 IDENTIFIER_BINDING list so that future lookups
7824 find it quickly. */
7825 if (BASELINK_P (class_binding->value))
7826 /* Don't put a BASELINK in IDENTIFIER_BINDING. */
7827 class_binding->value
7828 = BASELINK_FUNCTIONS (class_binding->value);
7829 class_binding->previous = outer;
7830 if (binding)
7831 binding->previous = class_binding;
7832 else
7833 IDENTIFIER_BINDING (name) = class_binding;
7834 return class_binding;
7837 /* If we are in a member template, the template parms of the member
7838 template are considered to be inside the scope of the containing
7839 class, but within G++ the class bindings are all pushed between the
7840 template parms and the function body. So if the outer binding is
7841 a template parm for the current scope, return it now rather than
7842 look for a class binding. */
7843 if (outer_scope && outer_scope->kind == sk_template_parms
7844 && binding_to_template_parms_of_scope_p (outer, scope))
7845 return outer;
7847 scope = scope->level_chain;
7850 return outer;
7853 /* Return the innermost block-scope or class-scope value binding for
7854 NAME, or NULL_TREE if there is no such binding. */
7856 tree
7857 innermost_non_namespace_value (tree name)
7859 cxx_binding *binding;
7860 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
7861 return binding ? binding->value : NULL_TREE;
7864 /* True iff current_binding_level is within the potential scope of local
7865 variable DECL. */
7867 bool
7868 decl_in_scope_p (tree decl)
7870 gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
7872 tree name = DECL_NAME (decl);
7874 for (cxx_binding *iter = NULL;
7875 (iter = outer_binding (name, iter, /*class_p=*/false)); )
7877 if (!LOCAL_BINDING_P (iter))
7878 return false;
7879 if (iter->value == decl)
7880 return true;
7883 return false;
7886 /* Look up NAME in the current binding level and its superiors in the
7887 namespace of variables, functions and typedefs. Return a ..._DECL
7888 node of some kind representing its definition if there is only one
7889 such declaration, or return a TREE_LIST with all the overloaded
7890 definitions if there are many, or return NULL_TREE if it is undefined.
7891 Hidden name, either friend declaration or built-in function, are
7892 not ignored.
7894 WHERE controls which scopes are considered. It is a bit mask of
7895 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7896 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7897 scopes). It is an error for no bits to be set. These scopes are
7898 searched from innermost to outermost.
7900 WANT controls what kind of entity we'd happy with.
7901 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7902 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7903 for only NAMESPACE_DECLS. These two can be bit-ored to find
7904 namespace or type.
7906 WANT can also have LOOK_want::HIDDEN_FRIEND or
7907 LOOK_want::HIDDEN_LAMBDa added to it. */
7909 tree
7910 lookup_name (tree name, LOOK_where where, LOOK_want want)
7912 tree val = NULL_TREE;
7914 auto_cond_timevar tv (TV_NAME_LOOKUP);
7916 gcc_checking_assert (unsigned (where) != 0);
7917 /* If we're looking for hidden lambda things, we shouldn't be
7918 looking in namespace scope. */
7919 gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
7920 || !bool (where & LOOK_where::NAMESPACE));
7921 query_oracle (name);
7923 /* Conversion operators are handled specially because ordinary
7924 unqualified name lookup will not find template conversion
7925 operators. */
7926 if (IDENTIFIER_CONV_OP_P (name))
7928 cp_binding_level *level;
7930 for (level = current_binding_level;
7931 level && level->kind != sk_namespace;
7932 level = level->level_chain)
7934 tree class_type;
7935 tree operators;
7937 /* A conversion operator can only be declared in a class
7938 scope. */
7939 if (level->kind != sk_class)
7940 continue;
7942 /* Lookup the conversion operator in the class. */
7943 class_type = level->this_entity;
7944 operators = lookup_fnfields (class_type, name, /*protect=*/0,
7945 tf_warning_or_error);
7946 if (operators)
7947 return operators;
7950 return NULL_TREE;
7953 /* First, look in non-namespace scopes. */
7955 if (current_class_type == NULL_TREE)
7956 /* Maybe avoid searching the binding stack at all. */
7957 where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
7959 if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
7960 for (cxx_binding *iter = nullptr;
7961 (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
7963 /* Skip entities we don't want. */
7964 if (!bool (where & (LOCAL_BINDING_P (iter)
7965 ? LOOK_where::BLOCK : LOOK_where::CLASS)))
7966 continue;
7968 /* If this is the kind of thing we're looking for, we're done. */
7969 if (iter->value)
7971 tree binding = NULL_TREE;
7973 if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
7974 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
7975 || !is_lambda_ignored_entity (iter->value))
7976 && qualify_lookup (iter->value, want))
7977 binding = iter->value;
7978 else if (bool (want & LOOK_want::TYPE)
7979 && !HIDDEN_TYPE_BINDING_P (iter)
7980 && iter->type)
7981 binding = iter->type;
7983 if (binding)
7985 val = binding;
7986 break;
7991 /* Now lookup in namespace scopes. */
7992 if (!val && bool (where & LOOK_where::NAMESPACE))
7994 name_lookup lookup (name, want);
7995 if (lookup.search_unqualified
7996 (current_decl_namespace (), current_binding_level))
7997 val = lookup.value;
8000 /* If we have a known type overload, pull it out. This can happen
8001 for both using decls and unhidden functions. */
8002 if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
8003 val = OVL_FUNCTION (val);
8005 return val;
8008 tree
8009 lookup_name (tree name)
8011 return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
8014 /* Look up NAME for type used in elaborated name specifier in
8015 the scopes given by HOW.
8017 Unlike lookup_name_1, we make sure that NAME is actually
8018 declared in the desired scope, not from inheritance, nor using
8019 directive. For using declaration, there is DR138 still waiting
8020 to be resolved. Hidden name coming from an earlier friend
8021 declaration is also returned, and will be made visible unless HOW
8022 is TAG_how::HIDDEN_FRIEND.
8024 A TYPE_DECL best matching the NAME is returned. Catching error
8025 and issuing diagnostics are caller's responsibility. */
8027 tree
8028 lookup_elaborated_type (tree name, TAG_how how)
8030 auto_cond_timevar tv (TV_NAME_LOOKUP);
8032 cp_binding_level *b = current_binding_level;
8034 if (b->kind != sk_namespace)
8035 /* Look in non-namespace scopes. */
8036 for (cxx_binding *iter = NULL;
8037 (iter = outer_binding (name, iter, /*class_p=*/ true)); )
8039 /* First check we're supposed to be looking in this scope --
8040 if we're not, we're done. */
8041 for (; b != iter->scope; b = b->level_chain)
8042 if (!(b->kind == sk_cleanup
8043 || b->kind == sk_template_parms
8044 || b->kind == sk_function_parms
8045 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8046 return NULL_TREE;
8048 /* Check if this is the kind of thing we're looking for. If
8049 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
8050 come from base class. For ITER->VALUE, we can simply use
8051 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
8052 our own check.
8054 We check ITER->TYPE before ITER->VALUE in order to handle
8055 typedef struct C {} C;
8056 correctly. */
8058 if (tree type = iter->type)
8060 if (qualify_lookup (type, LOOK_want::TYPE)
8061 && (how != TAG_how::CURRENT_ONLY
8062 || LOCAL_BINDING_P (iter)
8063 || DECL_CONTEXT (type) == iter->scope->this_entity))
8065 if (how != TAG_how::HIDDEN_FRIEND)
8066 /* It is no longer a hidden binding. */
8067 HIDDEN_TYPE_BINDING_P (iter) = false;
8069 return type;
8072 else
8074 if (qualify_lookup (iter->value, LOOK_want::TYPE)
8075 && (how != TAG_how::CURRENT_ONLY
8076 || !INHERITED_VALUE_BINDING_P (iter)))
8078 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
8079 /* It is no longer a hidden binding. */
8080 HIDDEN_TYPE_BINDING_P (iter) = false;
8082 return iter->value;
8087 /* Now check if we can look in namespace scope. */
8088 for (; b->kind != sk_namespace; b = b->level_chain)
8089 if (!(b->kind == sk_cleanup
8090 || b->kind == sk_template_parms
8091 || b->kind == sk_function_parms
8092 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8093 return NULL_TREE;
8095 /* Look in the innermost namespace. */
8096 tree ns = b->this_entity;
8097 if (tree *slot = find_namespace_slot (ns, name))
8099 tree bind = *slot;
8100 if (TREE_CODE (bind) == BINDING_VECTOR)
8101 bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
8103 if (bind)
8105 /* If this is the kind of thing we're looking for, we're done. */
8106 if (tree type = MAYBE_STAT_TYPE (bind))
8108 if (how != TAG_how::HIDDEN_FRIEND)
8109 /* No longer hidden. */
8110 STAT_TYPE_HIDDEN_P (*slot) = false;
8112 return type;
8114 else if (tree decl = MAYBE_STAT_DECL (bind))
8116 if (qualify_lookup (decl, LOOK_want::TYPE))
8118 if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
8119 && STAT_DECL_HIDDEN_P (bind))
8121 if (STAT_TYPE (bind))
8122 STAT_DECL_HIDDEN_P (bind) = false;
8123 else
8125 /* There is no type, just remove the stat
8126 hack. */
8127 if (*slot == bind)
8128 *slot = decl;
8129 else
8130 BINDING_VECTOR_CLUSTER (*slot, 0)
8131 .slots[BINDING_SLOT_CURRENT] = decl;
8134 return decl;
8139 if (TREE_CODE (*slot) == BINDING_VECTOR)
8141 /* We could be redeclaring a global module entity, (from GMF
8142 or header unit), or from another partition, or
8143 specializing an imported template. */
8144 bitmap imports = get_import_bitmap ();
8145 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
8147 /* Scan the imported bindings. */
8148 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
8149 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
8151 ix--;
8152 cluster++;
8155 /* Do this in forward order, so we load modules in an order
8156 the user expects. */
8157 for (; ix--; cluster++)
8158 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
8160 /* Are we importing this module? */
8161 if (unsigned base = cluster->indices[jx].base)
8162 if (unsigned span = cluster->indices[jx].span)
8164 if (bitmap_bit_p (imports, base))
8165 goto found;
8166 while (++base, --span);
8167 continue;
8169 found:;
8170 /* Is it loaded? */
8171 if (cluster->slots[jx].is_lazy ())
8173 gcc_assert (cluster->indices[jx].span == 1);
8174 lazy_load_binding (cluster->indices[jx].base,
8175 ns, name, &cluster->slots[jx]);
8177 tree bind = cluster->slots[jx];
8178 if (!bind)
8179 /* Load errors could mean there's nothing here. */
8180 continue;
8182 /* Extract what we can see from here. If there's no
8183 stat_hack, then everything was exported. */
8184 tree type = NULL_TREE;
8186 /* If no stat hack, everything is visible. */
8187 if (STAT_HACK_P (bind))
8189 if (STAT_TYPE_VISIBLE_P (bind))
8190 type = STAT_TYPE (bind);
8191 bind = STAT_VISIBLE (bind);
8194 if (type && qualify_lookup (type, LOOK_want::TYPE))
8195 return type;
8197 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
8198 return bind;
8201 if (!module_purview_p ())
8203 /* We're in the global module, perhaps there's a tag
8204 there? */
8206 /* FIXME: In general we should probably merge global module
8207 classes in check_module_override rather than here, but for
8208 GCC14 let's just fix lazy declarations of __class_type_info in
8209 build_dynamic_cast_1. */
8210 if (current_namespace == abi_node)
8212 tree g = (BINDING_VECTOR_CLUSTER (*slot, 0)
8213 .slots[BINDING_SLOT_GLOBAL]);
8214 for (ovl_iterator iter (g); iter; ++iter)
8215 if (qualify_lookup (*iter, LOOK_want::TYPE))
8216 return *iter;
8222 return NULL_TREE;
8225 /* The type TYPE is being declared. If it is a class template, or a
8226 specialization of a class template, do any processing required and
8227 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8228 being declared a friend. B is the binding level at which this TYPE
8229 should be bound.
8231 Returns the TYPE_DECL for TYPE, which may have been altered by this
8232 processing. */
8234 static tree
8235 maybe_process_template_type_declaration (tree type, int is_friend,
8236 cp_binding_level *b)
8238 tree decl = TYPE_NAME (type);
8240 if (processing_template_parmlist)
8241 /* You can't declare a new template type in a template parameter
8242 list. But, you can declare a non-template type:
8244 template <class A*> struct S;
8246 is a forward-declaration of `A'. */
8248 else if (b->kind == sk_namespace
8249 && current_binding_level->kind != sk_namespace)
8250 /* If this new type is being injected into a containing scope,
8251 then it's not a template type. */
8253 else
8255 gcc_assert (MAYBE_CLASS_TYPE_P (type)
8256 || TREE_CODE (type) == ENUMERAL_TYPE);
8258 if (processing_template_decl)
8260 decl = push_template_decl (decl, is_friend);
8261 if (decl == error_mark_node)
8262 return error_mark_node;
8264 /* If the current binding level is the binding level for the
8265 template parameters (see the comment in
8266 begin_template_parm_list) and the enclosing level is a class
8267 scope, and we're not looking at a friend, push the
8268 declaration of the member class into the class scope. In the
8269 friend case, push_template_decl will already have put the
8270 friend into global scope, if appropriate. */
8271 if (TREE_CODE (type) != ENUMERAL_TYPE
8272 && !is_friend && b->kind == sk_template_parms
8273 && b->level_chain->kind == sk_class)
8275 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
8277 if (!COMPLETE_TYPE_P (current_class_type))
8278 maybe_add_class_template_decl_list (current_class_type,
8279 type, /*friend_p=*/0);
8284 return decl;
8287 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8288 that the NAME is a class template, the tag is processed but not pushed.
8290 The pushed scope depend on the SCOPE parameter:
8291 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8292 scope.
8293 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8294 non-template-parameter scope. This case is needed for forward
8295 declarations.
8296 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8297 TS_GLOBAL case except that names within template-parameter scopes
8298 are not pushed at all.
8300 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8302 tree
8303 pushtag (tree name, tree type, TAG_how how)
8305 tree decl;
8307 gcc_assert (identifier_p (name));
8309 auto_cond_timevar tv (TV_NAME_LOOKUP);
8311 cp_binding_level *b = current_binding_level;
8312 while (true)
8314 if (/* Cleanup scopes are not scopes from the point of view of
8315 the language. */
8316 b->kind == sk_cleanup
8317 /* Neither are function parameter scopes. */
8318 || b->kind == sk_function_parms
8319 /* Neither are the scopes used to hold template parameters
8320 for an explicit specialization. For an ordinary template
8321 declaration, these scopes are not scopes from the point of
8322 view of the language. */
8323 || (b->kind == sk_template_parms
8324 && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
8325 b = b->level_chain;
8326 else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
8328 b = b->level_chain;
8329 if (b->kind == sk_template_parms)
8330 b = b->level_chain;
8332 else
8333 break;
8336 /* Do C++ gratuitous typedefing. */
8337 if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
8339 tree tdef;
8340 tree context = TYPE_CONTEXT (type);
8342 if (! context)
8344 cp_binding_level *cb = b;
8345 while (cb->kind != sk_namespace
8346 && cb->kind != sk_class
8347 && (cb->kind != sk_function_parms
8348 || !cb->this_entity))
8349 cb = cb->level_chain;
8350 tree cs = cb->this_entity;
8352 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
8353 ? cs == current_function_decl
8354 : TYPE_P (cs) ? cs == current_class_type
8355 : cs == current_namespace);
8357 if (how == TAG_how::CURRENT_ONLY
8358 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
8359 context = cs;
8360 else if (cs && TYPE_P (cs))
8361 /* When declaring a friend class of a local class, we want
8362 to inject the newly named class into the scope
8363 containing the local class, not the namespace
8364 scope. */
8365 context = decl_function_context (get_type_decl (cs));
8367 if (!context)
8368 context = current_namespace;
8370 tdef = create_implicit_typedef (name, type);
8371 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
8372 set_originating_module (tdef);
8374 decl = maybe_process_template_type_declaration
8375 (type, how == TAG_how::HIDDEN_FRIEND, b);
8376 if (decl == error_mark_node)
8377 return decl;
8379 if (b->kind == sk_class)
8381 if (!TYPE_BEING_DEFINED (current_class_type))
8382 /* Don't push anywhere if the class is complete; a lambda in an
8383 NSDMI is not a member of the class. */
8385 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8386 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8387 class. But if it's a member template class, we want
8388 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8389 later. */
8390 finish_member_declaration (decl);
8391 else
8392 pushdecl_class_level (decl);
8394 else if (b->kind == sk_template_parms)
8396 /* Do not push the tag here -- we'll want to push the
8397 TEMPLATE_DECL. */
8398 if (b->level_chain->kind != sk_class)
8399 set_identifier_type_value_with_scope (name, tdef, b->level_chain);
8401 else
8403 decl = do_pushdecl_with_scope
8404 (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
8405 if (decl == error_mark_node)
8406 return decl;
8408 if (DECL_CONTEXT (decl) == std_node
8409 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
8410 && !CLASSTYPE_TEMPLATE_INFO (type))
8412 error ("declaration of %<std::initializer_list%> does not match "
8413 "%<#include <initializer_list>%>, isn%'t a template");
8414 return error_mark_node;
8418 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
8420 /* If this is a local class, keep track of it. We need this
8421 information for name-mangling, and so that it is possible to
8422 find all function definitions in a translation unit in a
8423 convenient way. (It's otherwise tricky to find a member
8424 function definition it's only pointed to from within a local
8425 class.) */
8426 if (TYPE_FUNCTION_SCOPE_P (type))
8428 if (processing_template_decl)
8430 /* Push a DECL_EXPR so we call pushtag at the right time in
8431 template instantiation rather than in some nested context. */
8432 add_decl_expr (decl);
8434 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8435 else if (!LAMBDA_TYPE_P (type))
8436 determine_local_discriminator (TYPE_NAME (type));
8440 if (b->kind == sk_class
8441 && !COMPLETE_TYPE_P (current_class_type))
8442 maybe_add_class_template_decl_list (current_class_type,
8443 type, /*friend_p=*/0);
8445 decl = TYPE_NAME (type);
8446 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
8448 /* Set type visibility now if this is a forward declaration. */
8449 TREE_PUBLIC (decl) = 1;
8450 determine_visibility (decl);
8452 return type;
8455 /* Subroutines for reverting temporarily to top-level for instantiation
8456 of templates and such. We actually need to clear out the class- and
8457 local-value slots of all identifiers, so that only the global values
8458 are at all visible. Simply setting current_binding_level to the global
8459 scope isn't enough, because more binding levels may be pushed. */
8460 struct saved_scope *scope_chain;
8462 /* Return true if ID has not already been marked. */
8464 static inline bool
8465 store_binding_p (tree id)
8467 if (!id || !IDENTIFIER_BINDING (id))
8468 return false;
8470 if (IDENTIFIER_MARKED (id))
8471 return false;
8473 return true;
8476 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8477 have enough space reserved. */
8479 static void
8480 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
8482 cxx_saved_binding saved;
8484 gcc_checking_assert (store_binding_p (id));
8486 IDENTIFIER_MARKED (id) = 1;
8488 saved.identifier = id;
8489 saved.binding = IDENTIFIER_BINDING (id);
8490 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8491 (*old_bindings)->quick_push (saved);
8492 IDENTIFIER_BINDING (id) = NULL;
8495 static void
8496 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
8498 static vec<tree> bindings_need_stored;
8499 tree t, id;
8500 size_t i;
8502 auto_cond_timevar tv (TV_NAME_LOOKUP);
8503 for (t = names; t; t = TREE_CHAIN (t))
8505 if (TREE_CODE (t) == TREE_LIST)
8506 id = TREE_PURPOSE (t);
8507 else
8508 id = DECL_NAME (t);
8510 if (store_binding_p (id))
8511 bindings_need_stored.safe_push (id);
8513 if (!bindings_need_stored.is_empty ())
8515 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8516 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8518 /* We can apparently have duplicates in NAMES. */
8519 if (store_binding_p (id))
8520 store_binding (id, old_bindings);
8522 bindings_need_stored.truncate (0);
8526 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8527 objects, rather than a TREE_LIST. */
8529 static void
8530 store_class_bindings (vec<cp_class_binding, va_gc> *names,
8531 vec<cxx_saved_binding, va_gc> **old_bindings)
8533 static vec<tree> bindings_need_stored;
8534 size_t i;
8535 cp_class_binding *cb;
8537 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
8538 if (store_binding_p (cb->identifier))
8539 bindings_need_stored.safe_push (cb->identifier);
8540 if (!bindings_need_stored.is_empty ())
8542 tree id;
8543 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8544 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8545 store_binding (id, old_bindings);
8546 bindings_need_stored.truncate (0);
8550 /* A chain of saved_scope structures awaiting reuse. */
8552 static GTY((deletable)) struct saved_scope *free_saved_scope;
8554 void
8555 push_to_top_level (void)
8557 struct saved_scope *s;
8558 cp_binding_level *b;
8559 cxx_saved_binding *sb;
8560 size_t i;
8561 bool need_pop;
8563 auto_cond_timevar tv (TV_NAME_LOOKUP);
8565 /* Reuse or create a new structure for this saved scope. */
8566 if (free_saved_scope != NULL)
8568 s = free_saved_scope;
8569 free_saved_scope = s->prev;
8571 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
8572 memset (s, 0, sizeof (*s));
8573 /* Also reuse the structure's old_bindings vector. */
8574 vec_safe_truncate (old_bindings, 0);
8575 s->old_bindings = old_bindings;
8577 else
8578 s = ggc_cleared_alloc<saved_scope> ();
8580 b = scope_chain ? current_binding_level : 0;
8582 /* If we're in the middle of some function, save our state. */
8583 if (cfun)
8585 need_pop = true;
8586 push_function_context ();
8588 else
8589 need_pop = false;
8591 if (scope_chain && previous_class_level)
8592 store_class_bindings (previous_class_level->class_shadowed,
8593 &s->old_bindings);
8595 /* Have to include the global scope, because class-scope decls
8596 aren't listed anywhere useful. */
8597 for (; b; b = b->level_chain)
8599 tree t;
8601 /* Template IDs are inserted into the global level. If they were
8602 inserted into namespace level, finish_file wouldn't find them
8603 when doing pending instantiations. Therefore, don't stop at
8604 namespace level, but continue until :: . */
8605 if (global_scope_p (b))
8606 break;
8608 store_bindings (b->names, &s->old_bindings);
8609 /* We also need to check class_shadowed to save class-level type
8610 bindings, since pushclass doesn't fill in b->names. */
8611 if (b->kind == sk_class)
8612 store_class_bindings (b->class_shadowed, &s->old_bindings);
8614 /* Unwind type-value slots back to top level. */
8615 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
8616 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
8619 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
8620 IDENTIFIER_MARKED (sb->identifier) = 0;
8622 s->prev = scope_chain;
8623 s->bindings = b;
8624 s->need_pop_function_context = need_pop;
8625 s->function_decl = current_function_decl;
8626 s->unevaluated_operand = cp_unevaluated_operand;
8627 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8628 s->suppress_location_wrappers = suppress_location_wrappers;
8629 s->x_stmt_tree.stmts_are_full_exprs_p = true;
8631 scope_chain = s;
8632 current_function_decl = NULL_TREE;
8633 current_lang_base = NULL;
8634 current_lang_name = lang_name_cplusplus;
8635 current_namespace = global_namespace;
8636 push_class_stack ();
8637 cp_unevaluated_operand = 0;
8638 c_inhibit_evaluation_warnings = 0;
8639 suppress_location_wrappers = 0;
8642 void
8643 pop_from_top_level (void)
8645 struct saved_scope *s = scope_chain;
8646 cxx_saved_binding *saved;
8647 size_t i;
8649 auto_cond_timevar tv (TV_NAME_LOOKUP);
8651 pop_class_stack ();
8653 release_tree_vector (current_lang_base);
8655 scope_chain = s->prev;
8656 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
8658 tree id = saved->identifier;
8660 IDENTIFIER_BINDING (id) = saved->binding;
8661 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8664 /* If we were in the middle of compiling a function, restore our
8665 state. */
8666 if (s->need_pop_function_context)
8667 pop_function_context ();
8668 current_function_decl = s->function_decl;
8669 cp_unevaluated_operand = s->unevaluated_operand;
8670 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
8671 suppress_location_wrappers = s->suppress_location_wrappers;
8673 /* Make this saved_scope structure available for reuse by
8674 push_to_top_level. */
8675 s->prev = free_saved_scope;
8676 free_saved_scope = s;
8679 namespace {
8681 /* Helper class for saving/restoring relevant global flags for the
8682 function-local case of maybe_push_to_top_level. */
8684 struct local_state_t
8686 int cp_unevaluated_operand;
8687 int c_inhibit_evaluation_warnings;
8689 static local_state_t
8690 save_and_clear ()
8692 local_state_t s;
8693 s.cp_unevaluated_operand = ::cp_unevaluated_operand;
8694 ::cp_unevaluated_operand = 0;
8695 s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
8696 ::c_inhibit_evaluation_warnings = 0;
8697 return s;
8700 void
8701 restore () const
8703 ::cp_unevaluated_operand = this->cp_unevaluated_operand;
8704 ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
8708 vec<local_state_t> local_state_stack;
8710 } // anon namespace
8712 /* Like push_to_top_level, but not if D is function-local. Returns whether we
8713 did push to top. */
8715 bool
8716 maybe_push_to_top_level (tree d)
8718 /* Push if D isn't function-local, or is a lambda function, for which name
8719 resolution is already done. */
8720 const bool push_to_top
8721 = (LAMBDA_FUNCTION_P (d)
8722 || (TREE_CODE (d) == TYPE_DECL
8723 && TREE_TYPE (d)
8724 && LAMBDA_TYPE_P (TREE_TYPE (d)))
8725 || !current_function_decl
8726 || !decl_function_context (d));
8728 if (push_to_top)
8729 push_to_top_level ();
8730 else
8732 gcc_assert (!processing_template_decl);
8733 push_function_context ();
8734 local_state_stack.safe_push (local_state_t::save_and_clear ());
8737 return push_to_top;
8740 /* Return from whatever maybe_push_to_top_level did. */
8742 void
8743 maybe_pop_from_top_level (bool push_to_top)
8745 if (push_to_top)
8746 pop_from_top_level ();
8747 else
8749 local_state_stack.pop ().restore ();
8750 pop_function_context ();
8754 /* Push into the scope of the namespace NS, even if it is deeply
8755 nested within another namespace. */
8757 void
8758 push_nested_namespace (tree ns)
8760 auto_cond_timevar tv (TV_NAME_LOOKUP);
8761 if (ns == global_namespace)
8762 push_to_top_level ();
8763 else
8765 push_nested_namespace (CP_DECL_CONTEXT (ns));
8766 resume_scope (NAMESPACE_LEVEL (ns));
8767 current_namespace = ns;
8771 /* Pop back from the scope of the namespace NS, which was previously
8772 entered with push_nested_namespace. */
8774 void
8775 pop_nested_namespace (tree ns)
8777 auto_cond_timevar tv (TV_NAME_LOOKUP);
8778 while (ns != global_namespace)
8780 ns = CP_DECL_CONTEXT (ns);
8781 current_namespace = ns;
8782 leave_scope ();
8785 pop_from_top_level ();
8788 /* Add TARGET to USINGS, if it does not already exist there. We used
8789 to build the complete graph of usings at this point, from the POV
8790 of the source namespaces. Now we build that as we perform the
8791 unqualified search. */
8793 static void
8794 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
8796 if (usings)
8797 for (unsigned ix = usings->length (); ix--;)
8798 if ((*usings)[ix] == target)
8799 return;
8801 vec_safe_push (usings, target);
8804 /* Tell the debug system of a using directive. */
8806 static void
8807 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
8809 /* Emit debugging info. */
8810 tree context = from != global_namespace ? from : NULL_TREE;
8811 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
8812 implicit);
8815 /* Process a using directive. */
8817 void
8818 finish_using_directive (tree target, tree attribs)
8820 if (target == error_mark_node)
8821 return;
8823 if (current_binding_level->kind != sk_namespace)
8824 add_stmt (build_stmt (input_location, USING_STMT, target));
8825 else
8826 emit_debug_info_using_namespace (current_binding_level->this_entity,
8827 ORIGINAL_NAMESPACE (target), false);
8829 add_using_namespace (current_binding_level->using_directives,
8830 ORIGINAL_NAMESPACE (target));
8832 bool diagnosed = false;
8833 if (attribs != error_mark_node)
8834 for (tree a = attribs; a; a = TREE_CHAIN (a))
8836 tree name = get_attribute_name (a);
8837 if (current_binding_level->kind == sk_namespace
8838 && is_attribute_p ("strong", name))
8840 if (warning (0, "%<strong%> using directive no longer supported")
8841 && CP_DECL_CONTEXT (target) == current_namespace)
8842 inform (DECL_SOURCE_LOCATION (target),
8843 "you can use an inline namespace instead");
8845 else if ((flag_openmp || flag_openmp_simd)
8846 && get_attribute_namespace (a) == omp_identifier
8847 && (is_attribute_p ("directive", name)
8848 || is_attribute_p ("sequence", name)
8849 || is_attribute_p ("decl", name)))
8851 if (!diagnosed)
8853 if (tree ar = TREE_VALUE (a))
8855 tree d = TREE_VALUE (ar);
8856 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
8857 error ("%<omp::%s%> not allowed to be specified in "
8858 "this context",
8859 TREE_PUBLIC (d) ? "decl" : "directive");
8861 else
8862 error ("%<omp::%E%> not allowed to be specified in this "
8863 "context", name);
8864 diagnosed = true;
8867 else if (!attribute_ignored_p (a))
8868 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
8872 /* Pushes X into the global namespace. */
8874 tree
8875 pushdecl_top_level (tree x)
8877 auto_cond_timevar tv (TV_NAME_LOOKUP);
8878 push_to_top_level ();
8879 gcc_checking_assert (!DECL_CONTEXT (x));
8880 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8881 x = pushdecl_namespace_level (x);
8882 pop_from_top_level ();
8883 return x;
8886 /* Pushes X into the global namespace and calls cp_finish_decl to
8887 register the variable, initializing it with INIT. */
8889 tree
8890 pushdecl_top_level_and_finish (tree x, tree init)
8892 auto_cond_timevar tv (TV_NAME_LOOKUP);
8893 push_to_top_level ();
8894 gcc_checking_assert (!DECL_CONTEXT (x));
8895 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8896 x = pushdecl_namespace_level (x);
8897 cp_finish_decl (x, init, false, NULL_TREE, 0);
8898 pop_from_top_level ();
8899 return x;
8902 /* Enter the namespaces from current_namerspace to NS. */
8904 static int
8905 push_inline_namespaces (tree ns)
8907 int count = 0;
8908 if (ns != current_namespace)
8910 gcc_assert (ns != global_namespace);
8911 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
8912 resume_scope (NAMESPACE_LEVEL (ns));
8913 current_namespace = ns;
8914 count++;
8916 return count;
8919 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8920 Reuse or create a namespace NAME. NAME is null for the anonymous
8921 namespace. */
8923 static tree
8924 reuse_namespace (tree *slot, tree ctx, tree name)
8926 if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
8928 /* Public namespace. Shared. */
8929 tree *global_slot = slot;
8930 if (TREE_CODE (*slot) == BINDING_VECTOR)
8931 global_slot = get_fixed_binding_slot (slot, name,
8932 BINDING_SLOT_GLOBAL, false);
8934 for (ovl_iterator iter (*global_slot); iter; ++iter)
8936 tree decl = *iter;
8938 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
8939 return decl;
8942 return NULL_TREE;
8945 static tree
8946 make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
8948 /* Create the namespace. */
8949 tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
8950 DECL_SOURCE_LOCATION (ns) = loc;
8951 SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
8952 if (!SCOPE_DEPTH (ns))
8953 /* We only allow depth 255. */
8954 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
8955 DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
8957 if (!name)
8958 /* Anon-namespaces in different header-unit imports are distinct.
8959 But that's ok as their contents all have internal linkage.
8960 (This is different to how they'd behave as textual includes,
8961 but doing this at all is really odd source.) */
8962 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
8963 else if (TREE_PUBLIC (ctx))
8964 TREE_PUBLIC (ns) = true;
8966 if (inline_p)
8967 DECL_NAMESPACE_INLINE_P (ns) = true;
8969 return ns;
8972 /* NS was newly created, finish off making it. */
8974 static void
8975 make_namespace_finish (tree ns, tree *slot, bool from_import = false)
8977 if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
8979 /* Merge into global slot. */
8980 tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
8981 BINDING_SLOT_GLOBAL, true);
8982 *gslot = ns;
8985 tree ctx = CP_DECL_CONTEXT (ns);
8986 cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
8987 scope->this_entity = ns;
8988 scope->more_cleanups_ok = true;
8989 scope->kind = sk_namespace;
8990 scope->level_chain = NAMESPACE_LEVEL (ctx);
8991 NAMESPACE_LEVEL (ns) = scope;
8993 if (DECL_NAMESPACE_INLINE_P (ns))
8994 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
8996 if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
8997 emit_debug_info_using_namespace (ctx, ns, true);
9000 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
9001 then we enter an anonymous namespace. If MAKE_INLINE is true, then
9002 we create an inline namespace (it is up to the caller to check upon
9003 redefinition). Return the number of namespaces entered. */
9006 push_namespace (tree name, bool make_inline)
9008 auto_cond_timevar tv (TV_NAME_LOOKUP);
9009 int count = 0;
9011 /* We should not get here if the global_namespace is not yet constructed
9012 nor if NAME designates the global namespace: The global scope is
9013 constructed elsewhere. */
9014 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
9016 tree ns = NULL_TREE;
9018 name_lookup lookup (name);
9019 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
9021 else if (TREE_CODE (lookup.value) == TREE_LIST)
9023 /* An ambiguous lookup. If exactly one is a namespace, we
9024 want that. If more than one is a namespace, error, but
9025 pick one of them. */
9026 /* DR2061 can cause us to find multiple namespaces of the same
9027 name. We must treat that carefully and avoid thinking we
9028 need to push a new (possibly) duplicate namespace. Hey,
9029 if you want to use the same identifier within an inline
9030 nest, knock yourself out. */
9031 for (tree *chain = &lookup.value, next; (next = *chain);)
9033 tree decl = TREE_VALUE (next);
9034 if (TREE_CODE (decl) == NAMESPACE_DECL)
9036 if (!ns)
9037 ns = decl;
9038 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
9039 ns = decl;
9041 /* Advance. */
9042 chain = &TREE_CHAIN (next);
9044 else
9045 /* Stitch out. */
9046 *chain = TREE_CHAIN (next);
9049 if (TREE_CHAIN (lookup.value))
9051 error ("%<namespace %E%> is ambiguous", name);
9052 print_candidates (lookup.value);
9055 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
9056 ns = lookup.value;
9058 if (ns)
9059 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
9061 /* A namespace alias is not allowed here, but if the alias
9062 is for a namespace also inside the current scope,
9063 accept it with a diagnostic. That's better than dying
9064 horribly. */
9065 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
9067 error ("namespace alias %qD not allowed here, "
9068 "assuming %qD", ns, dna);
9069 ns = dna;
9071 else
9072 ns = NULL_TREE;
9076 if (ns)
9078 /* DR2061. NS might be a member of an inline namespace. We
9079 need to push into those namespaces. */
9080 if (modules_p ())
9082 for (tree parent, ctx = ns; ctx != current_namespace;
9083 ctx = parent)
9085 parent = CP_DECL_CONTEXT (ctx);
9087 tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
9088 if (bind != ctx)
9090 auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
9091 binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
9092 gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
9093 slot = ctx;
9098 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
9099 if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
9100 /* It's not builtin now. */
9101 DECL_SOURCE_LOCATION (ns) = input_location;
9103 else
9105 /* Before making a new namespace, see if we already have one in
9106 the existing partitions of the current namespace. */
9107 tree *slot = find_namespace_slot (current_namespace, name, false);
9108 if (slot)
9109 ns = reuse_namespace (slot, current_namespace, name);
9110 if (!ns)
9111 ns = make_namespace (current_namespace, name,
9112 input_location, make_inline);
9114 if (pushdecl (ns) == error_mark_node)
9115 ns = NULL_TREE;
9116 else
9118 /* Finish up making the namespace. */
9119 add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
9120 if (!slot)
9122 slot = find_namespace_slot (current_namespace, name);
9123 /* This should find the slot created by pushdecl. */
9124 gcc_checking_assert (slot && *slot == ns);
9126 else
9128 /* pushdecl could have expanded the hash table, so
9129 slot might be invalid. */
9130 slot = find_namespace_slot (current_namespace, name);
9131 gcc_checking_assert (slot);
9133 make_namespace_finish (ns, slot);
9135 /* Add the anon using-directive here, we don't do it in
9136 make_namespace_finish. */
9137 if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
9138 add_using_namespace (current_binding_level->using_directives, ns);
9142 if (ns)
9144 /* A public namespace is exported only if explicitly marked, or
9145 it contains exported entities. */
9146 if (module_exporting_p ())
9148 if (TREE_PUBLIC (ns))
9149 DECL_MODULE_EXPORT_P (ns) = true;
9150 else if (!header_module_p ())
9151 error_at (input_location,
9152 "exporting namespace with internal linkage");
9154 if (module_purview_p ())
9155 DECL_MODULE_PURVIEW_P (ns) = true;
9157 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
9159 error_at (input_location,
9160 "inline namespace must be specified at initial definition");
9161 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
9163 resume_scope (NAMESPACE_LEVEL (ns));
9164 current_namespace = ns;
9165 count++;
9168 return count;
9171 /* Pop from the scope of the current namespace. */
9173 void
9174 pop_namespace (void)
9176 auto_cond_timevar tv (TV_NAME_LOOKUP);
9178 gcc_assert (current_namespace != global_namespace);
9179 current_namespace = CP_DECL_CONTEXT (current_namespace);
9180 /* The binding level is not popped, as it might be re-opened later. */
9181 leave_scope ();
9184 /* An IMPORT is an import that is defining namespace NAME inside CTX. Find or
9185 create that namespace and add it to the container's binding-vector. */
9187 tree
9188 add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
9189 bool inline_p, bool visible_p)
9191 // FIXME: Something is not correct about the VISIBLE_P handling. We
9192 // need to insert this namespace into
9193 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9194 // (b) The importing module's slot (always)
9195 // (c) Do we need to put it in the CURRENT slot? This is the
9196 // confused piece.
9198 tree *slot = find_namespace_slot (ctx, name, true);
9199 tree decl = reuse_namespace (slot, ctx, name);
9201 /* Creating and binding. */
9202 if (!decl)
9204 decl = make_namespace (ctx, name, loc, inline_p);
9205 make_namespace_finish (decl, slot, true);
9207 else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
9209 error_at (loc, "%s namespace %qD conflicts with reachable definition",
9210 inline_p ? "inline" : "non-inline", decl);
9211 inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
9212 inline_p ? "non-inline" : "inline");
9215 if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
9217 /* See if we can extend the final slot. */
9218 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
9219 gcc_checking_assert (last->indices[0].span);
9220 unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
9222 while (--jx)
9223 if (last->indices[jx].span)
9224 break;
9225 tree final = last->slots[jx];
9226 if (visible_p == !STAT_HACK_P (final)
9227 && MAYBE_STAT_DECL (final) == decl
9228 && last->indices[jx].base + last->indices[jx].span == import
9229 && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
9230 || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
9231 && jx >= BINDING_SLOTS_FIXED)))
9233 last->indices[jx].span++;
9234 return decl;
9238 /* Append a new slot. */
9239 tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
9241 gcc_assert (!*mslot);
9242 *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
9244 return decl;
9247 /* Pop off extraneous binding levels left over due to syntax errors.
9248 We don't pop past namespaces, as they might be valid. */
9250 void
9251 pop_everything (void)
9253 if (ENABLE_SCOPE_CHECKING)
9254 verbatim ("XXX entering %<pop_everything ()%>");
9255 while (!namespace_bindings_p ())
9257 if (current_binding_level->kind == sk_class)
9258 pop_nested_class ();
9259 else
9260 poplevel (0, 0, 0);
9262 if (ENABLE_SCOPE_CHECKING)
9263 verbatim ("XXX leaving %<pop_everything ()%>");
9266 /* Emit debugging information for using declarations and directives.
9267 If input tree is overloaded fn then emit debug info for all
9268 candidates. */
9270 void
9271 cp_emit_debug_info_for_using (tree t, tree context)
9273 /* Don't try to emit any debug information if we have errors. */
9274 if (seen_error ())
9275 return;
9277 /* Do not supply context to imported_module_or_decl, if
9278 it is a global namespace. */
9279 if (context == global_namespace)
9280 context = NULL_TREE;
9282 t = MAYBE_BASELINK_FUNCTIONS (t);
9284 for (lkp_iterator iter (t); iter; ++iter)
9286 tree fn = *iter;
9288 if (TREE_CODE (fn) == TEMPLATE_DECL)
9289 /* FIXME: Handle TEMPLATE_DECLs. */
9290 continue;
9292 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9293 of a builtin function. */
9294 if (TREE_CODE (fn) == FUNCTION_DECL
9295 && DECL_EXTERNAL (fn)
9296 && fndecl_built_in_p (fn))
9297 continue;
9299 if (building_stmt_list_p ())
9300 add_stmt (build_stmt (input_location, USING_STMT, fn));
9301 else
9302 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
9303 false, false);
9307 /* True if D is a local declaration in dependent scope. Assumes that it is
9308 (part of) the current lookup result for its name. */
9310 bool
9311 dependent_local_decl_p (tree d)
9313 if (!DECL_LOCAL_DECL_P (d))
9314 return false;
9316 cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
9317 cp_binding_level *l = b->scope;
9318 while (!l->this_entity)
9319 l = l->level_chain;
9320 return uses_template_parms (l->this_entity);
9325 #include "gt-cp-name-lookup.h"