Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / name-lookup.cc
blob2d747561e1f5a4ffb01655e66e41843964a51798
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
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 private:
515 static tree ambiguous (tree thing, tree current);
516 void add_overload (tree fns);
517 void add_value (tree new_val);
518 void add_type (tree new_type);
519 bool process_binding (tree val_bind, tree type_bind);
520 unsigned process_module_binding (tree val_bind, tree type_bind, unsigned);
521 /* Look in only namespace. */
522 bool search_namespace_only (tree scope);
523 /* Look in namespace and its (recursive) inlines. Ignore using
524 directives. Return true if something found (inc dups). */
525 bool search_namespace (tree scope);
526 /* Look in the using directives of namespace + inlines using
527 qualified lookup rules. */
528 bool search_usings (tree scope);
530 private:
531 void queue_namespace (using_queue& queue, int depth, tree scope);
532 void queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings);
534 private:
535 void add_fns (tree);
537 private:
538 void adl_expr (tree);
539 void adl_type (tree);
540 void adl_template_arg (tree);
541 void adl_class (tree);
542 void adl_enum (tree);
543 void adl_bases (tree);
544 void adl_class_only (tree);
545 void adl_namespace (tree);
546 void adl_class_fns (tree);
547 void adl_namespace_fns (tree, bitmap);
549 public:
550 /* Search namespace + inlines + maybe usings as qualified lookup. */
551 bool search_qualified (tree scope, bool usings = true);
553 /* Search namespace + inlines + usings as unqualified lookup. */
554 bool search_unqualified (tree scope, cp_binding_level *);
556 /* ADL lookup of ARGS. */
557 tree search_adl (tree fns, vec<tree, va_gc> *args);
560 /* Scope stack shared by all outermost lookups. This avoids us
561 allocating and freeing on every single lookup. */
562 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
564 /* Currently active lookup. */
565 name_lookup *name_lookup::active;
567 /* Name lookup is recursive, becase ADL can cause template
568 instatiation. This is of course a rare event, so we optimize for
569 it not happening. When we discover an active name-lookup, which
570 must be an ADL lookup, we need to unmark the marked scopes and also
571 unmark the lookup we might have been accumulating. */
573 void
574 name_lookup::preserve_state ()
576 previous = active;
577 if (previous)
579 unsigned length = vec_safe_length (previous->scopes);
580 vec_safe_reserve (previous->scopes, length * 2);
581 for (unsigned ix = length; ix--;)
583 tree decl = (*previous->scopes)[ix];
585 gcc_checking_assert (LOOKUP_SEEN_P (decl));
586 LOOKUP_SEEN_P (decl) = false;
588 /* Preserve the FOUND_P state on the interrupted lookup's
589 stack. */
590 if (LOOKUP_FOUND_P (decl))
592 LOOKUP_FOUND_P (decl) = false;
593 previous->scopes->quick_push (decl);
597 /* Unmark the outer partial lookup. */
598 if (previous->deduping)
599 lookup_mark (previous->value, false);
601 else
602 scopes = shared_scopes;
603 active = this;
606 /* Restore the marking state of a lookup we interrupted. */
608 void
609 name_lookup::restore_state ()
611 gcc_checking_assert (!deduping);
613 /* Unmark and empty this lookup's scope stack. */
614 for (unsigned ix = vec_safe_length (scopes); ix--;)
616 tree decl = scopes->pop ();
617 gcc_checking_assert (LOOKUP_SEEN_P (decl));
618 LOOKUP_SEEN_P (decl) = false;
619 LOOKUP_FOUND_P (decl) = false;
622 active = previous;
623 if (previous)
625 free (scopes);
627 unsigned length = vec_safe_length (previous->scopes);
628 for (unsigned ix = 0; ix != length; ix++)
630 tree decl = (*previous->scopes)[ix];
631 if (LOOKUP_SEEN_P (decl))
633 /* The remainder of the scope stack must be recording
634 FOUND_P decls, which we want to pop off. */
637 tree decl = previous->scopes->pop ();
638 gcc_checking_assert (LOOKUP_SEEN_P (decl)
639 && !LOOKUP_FOUND_P (decl));
640 LOOKUP_FOUND_P (decl) = true;
642 while (++ix != length);
643 break;
646 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
647 LOOKUP_SEEN_P (decl) = true;
650 /* Remark the outer partial lookup. */
651 if (previous->deduping)
652 lookup_mark (previous->value, true);
654 else
655 shared_scopes = scopes;
658 void
659 name_lookup::mark_seen (tree scope)
661 gcc_checking_assert (!seen_p (scope));
662 LOOKUP_SEEN_P (scope) = true;
663 vec_safe_push (scopes, scope);
666 bool
667 name_lookup::find_and_mark (tree scope)
669 bool result = LOOKUP_FOUND_P (scope);
670 if (!result)
672 LOOKUP_FOUND_P (scope) = true;
673 if (!LOOKUP_SEEN_P (scope))
674 vec_safe_push (scopes, scope);
677 return result;
680 /* THING and CURRENT are ambiguous, concatenate them. */
682 tree
683 name_lookup::ambiguous (tree thing, tree current)
685 if (TREE_CODE (current) != TREE_LIST)
687 current = build_tree_list (NULL_TREE, current);
688 TREE_TYPE (current) = error_mark_node;
690 current = tree_cons (NULL_TREE, thing, current);
691 TREE_TYPE (current) = error_mark_node;
693 return current;
696 /* FNS is a new overload set to add to the exising set. */
698 void
699 name_lookup::add_overload (tree fns)
701 if (!deduping && TREE_CODE (fns) == OVERLOAD)
703 tree probe = fns;
704 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
705 probe = ovl_skip_hidden (probe);
706 if (probe && TREE_CODE (probe) == OVERLOAD
707 && OVL_DEDUP_P (probe))
708 /* We're about to add something found by multiple paths, so need to
709 engage deduping mode. */
710 dedup (true);
713 value = lookup_maybe_add (fns, value, deduping);
716 /* Add a NEW_VAL, a found value binding into the current value binding. */
718 void
719 name_lookup::add_value (tree new_val)
721 if (OVL_P (new_val) && (!value || OVL_P (value)))
722 add_overload (new_val);
723 else if (!value)
724 value = new_val;
725 else if (value == new_val)
727 else if ((TREE_CODE (value) == TYPE_DECL
728 && TREE_CODE (new_val) == TYPE_DECL
729 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
730 /* Typedefs to the same type. */;
731 else if (TREE_CODE (value) == NAMESPACE_DECL
732 && TREE_CODE (new_val) == NAMESPACE_DECL
733 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
734 /* Namespace (possibly aliased) to the same namespace. Locate
735 the namespace*/
736 value = ORIGINAL_NAMESPACE (value);
737 else
739 /* Disengage deduping mode. */
740 dedup (false);
741 value = ambiguous (new_val, value);
745 /* Add a NEW_TYPE, a found type binding into the current type binding. */
747 void
748 name_lookup::add_type (tree new_type)
750 if (!type)
751 type = new_type;
752 else if (TREE_CODE (type) == TREE_LIST
753 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
754 type = ambiguous (new_type, type);
757 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
758 true if we actually found something noteworthy. Hiddenness has
759 already been handled in the caller. */
761 bool
762 name_lookup::process_binding (tree new_val, tree new_type)
764 /* Did we really see a type? */
765 if (new_type
766 && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
767 new_type = NULL_TREE;
769 /* Do we really see a value? */
770 if (new_val)
771 switch (TREE_CODE (new_val))
773 case TEMPLATE_DECL:
774 /* If we expect types or namespaces, and not templates,
775 or this is not a template class. */
776 if (bool (want & LOOK_want::TYPE_NAMESPACE)
777 && !DECL_TYPE_TEMPLATE_P (new_val))
778 new_val = NULL_TREE;
779 break;
780 case TYPE_DECL:
781 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
782 || (new_type && bool (want & LOOK_want::TYPE)))
783 new_val = NULL_TREE;
784 break;
785 case NAMESPACE_DECL:
786 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
787 new_val = NULL_TREE;
788 break;
789 default:
790 if (bool (want & LOOK_want::TYPE_NAMESPACE))
791 new_val = NULL_TREE;
794 if (!new_val)
796 new_val = new_type;
797 new_type = NULL_TREE;
800 /* Merge into the lookup */
801 if (new_val)
802 add_value (new_val);
803 if (new_type)
804 add_type (new_type);
806 return new_val != NULL_TREE;
809 /* If we're importing a module containing this binding, add it to the
810 lookup set. The trickiness is with namespaces, we only want to
811 find it once. */
813 unsigned
814 name_lookup::process_module_binding (tree new_val, tree new_type,
815 unsigned marker)
817 /* Optimize for (re-)finding a public namespace. We only need to
818 look once. */
819 if (new_val && !new_type
820 && TREE_CODE (new_val) == NAMESPACE_DECL
821 && TREE_PUBLIC (new_val)
822 && !DECL_NAMESPACE_ALIAS (new_val))
824 if (marker & 2)
825 return marker;
826 marker |= 2;
829 if (new_type || new_val)
830 marker |= process_binding (new_val, new_type);
832 return marker;
835 /* Look in exactly namespace SCOPE. */
837 bool
838 name_lookup::search_namespace_only (tree scope)
840 bool found = false;
841 if (tree *binding = find_namespace_slot (scope, name))
843 tree val = *binding;
844 if (TREE_CODE (val) == BINDING_VECTOR)
846 /* I presume the binding list is going to be sparser than
847 the import bitmap. Hence iterate over the former
848 checking for bits set in the bitmap. */
849 bitmap imports = get_import_bitmap ();
850 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
851 int marker = 0;
852 int dup_detect = 0;
854 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
856 if (!deduping)
858 if (named_module_purview_p ())
860 dup_detect |= 2;
862 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
863 dup_detect |= 1;
865 else
866 dup_detect |= 1;
868 tree type = NULL_TREE;
869 tree value = bind;
871 if (STAT_HACK_P (bind))
873 type = STAT_TYPE (bind);
874 value = STAT_DECL (bind);
876 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
878 if (STAT_TYPE_HIDDEN_P (bind))
879 type = NULL_TREE;
880 if (STAT_DECL_HIDDEN_P (bind))
881 value = NULL_TREE;
882 else
883 value = ovl_skip_hidden (value);
886 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
887 value = ovl_skip_hidden (value);
889 marker = process_module_binding (value, type, marker);
892 /* Scan the imported bindings. */
893 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
894 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
896 ix--;
897 cluster++;
900 /* Do this in forward order, so we load modules in an order
901 the user expects. */
902 for (; ix--; cluster++)
903 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
905 /* Are we importing this module? */
906 if (unsigned base = cluster->indices[jx].base)
907 if (unsigned span = cluster->indices[jx].span)
909 if (bitmap_bit_p (imports, base))
910 goto found;
911 while (++base, --span);
912 continue;
914 found:;
915 /* Is it loaded? */
916 if (cluster->slots[jx].is_lazy ())
918 gcc_assert (cluster->indices[jx].span == 1);
919 lazy_load_binding (cluster->indices[jx].base,
920 scope, name, &cluster->slots[jx]);
922 tree bind = cluster->slots[jx];
923 if (!bind)
924 /* Load errors could mean there's nothing here. */
925 continue;
927 /* Extract what we can see from here. If there's no
928 stat_hack, then everything was exported. */
929 tree type = NULL_TREE;
932 /* If STAT_HACK_P is false, everything is visible, and
933 there's no duplication possibilities. */
934 if (STAT_HACK_P (bind))
936 if (!deduping)
938 /* Do we need to engage deduplication? */
939 int dup = 0;
940 if (MODULE_BINDING_GLOBAL_P (bind))
941 dup = 1;
942 else if (MODULE_BINDING_PARTITION_P (bind))
943 dup = 2;
944 if (unsigned hit = dup_detect & dup)
946 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
947 || (hit & 2
948 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
949 dedup (true);
951 dup_detect |= dup;
954 if (STAT_TYPE_VISIBLE_P (bind))
955 type = STAT_TYPE (bind);
956 bind = STAT_VISIBLE (bind);
959 /* And process it. */
960 marker = process_module_binding (bind, type, marker);
962 found |= marker & 1;
964 else
966 /* Only a current module binding, visible from the current module. */
967 tree bind = *binding;
968 tree value = bind, type = NULL_TREE;
970 if (STAT_HACK_P (bind))
972 type = STAT_TYPE (bind);
973 value = STAT_DECL (bind);
975 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
977 if (STAT_TYPE_HIDDEN_P (bind))
978 type = NULL_TREE;
979 if (STAT_DECL_HIDDEN_P (bind))
980 value = NULL_TREE;
981 else
982 value = ovl_skip_hidden (value);
985 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
986 value = ovl_skip_hidden (value);
988 found |= process_binding (value, type);
992 return found;
995 /* Conditionally look in namespace SCOPE and inline children. */
997 bool
998 name_lookup::search_namespace (tree scope)
1000 if (see_and_mark (scope))
1001 /* We've visited this scope before. Return what we found then. */
1002 return found_p (scope);
1004 /* Look in exactly namespace. */
1005 bool found = search_namespace_only (scope);
1007 /* Don't look into inline children, if we're looking for an
1008 anonymous name -- it must be in the current scope, if anywhere. */
1009 if (name)
1010 /* Recursively look in its inline children. */
1011 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1012 for (unsigned ix = inlinees->length (); ix--;)
1013 found |= search_namespace ((*inlinees)[ix]);
1015 if (found)
1016 mark_found (scope);
1018 return found;
1021 /* Recursively follow using directives of SCOPE & its inline children.
1022 Such following is essentially a flood-fill algorithm. */
1024 bool
1025 name_lookup::search_usings (tree scope)
1027 /* We do not check seen_p here, as that was already set during the
1028 namespace_only walk. */
1029 if (found_p (scope))
1030 return true;
1032 bool found = false;
1033 if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
1034 for (unsigned ix = usings->length (); ix--;)
1035 found |= search_qualified ((*usings)[ix], true);
1037 /* Look in its inline children. */
1038 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1039 for (unsigned ix = inlinees->length (); ix--;)
1040 found |= search_usings ((*inlinees)[ix]);
1042 if (found)
1043 mark_found (scope);
1045 return found;
1048 /* Qualified namespace lookup in SCOPE.
1049 1) Look in SCOPE (+inlines). If found, we're done.
1050 2) Otherwise, if USINGS is true,
1051 recurse for every using directive of SCOPE (+inlines).
1053 Trickiness is (a) loops and (b) multiple paths to same namespace.
1054 In both cases we want to not repeat any lookups, and know whether
1055 to stop the caller's step #2. Do this via the FOUND_P marker. */
1057 bool
1058 name_lookup::search_qualified (tree scope, bool usings)
1060 bool found = false;
1062 if (seen_p (scope))
1063 found = found_p (scope);
1064 else
1066 found = search_namespace (scope);
1067 if (!found && usings)
1068 found = search_usings (scope);
1071 dedup (false);
1073 return found;
1076 /* Add SCOPE to the unqualified search queue, recursively add its
1077 inlines and those via using directives. */
1079 void
1080 name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
1082 if (see_and_mark (scope))
1083 return;
1085 /* Record it. */
1086 tree common = scope;
1087 while (SCOPE_DEPTH (common) > depth)
1088 common = CP_DECL_CONTEXT (common);
1089 queue.safe_push (using_pair (common, scope));
1091 /* Queue its inline children. */
1092 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1093 for (unsigned ix = inlinees->length (); ix--;)
1094 queue_namespace (queue, depth, (*inlinees)[ix]);
1096 /* Queue its using targets. */
1097 queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
1100 /* Add the namespaces in USINGS to the unqualified search queue. */
1102 void
1103 name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
1105 if (usings)
1106 for (unsigned ix = usings->length (); ix--;)
1107 queue_namespace (queue, depth, (*usings)[ix]);
1110 /* Unqualified namespace lookup in SCOPE.
1111 1) add scope+inlins to worklist.
1112 2) recursively add target of every using directive
1113 3) for each worklist item where SCOPE is common ancestor, search it
1114 4) if nothing find, scope=parent, goto 1. */
1116 bool
1117 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
1119 using_queue queue;
1120 bool found = false;
1122 /* Queue local using-directives. */
1123 for (; level->kind != sk_namespace; level = level->level_chain)
1124 queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
1126 for (; !found; scope = CP_DECL_CONTEXT (scope))
1128 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
1129 int depth = SCOPE_DEPTH (scope);
1131 /* Queue namespaces reachable from SCOPE. */
1132 queue_namespace (queue, depth, scope);
1134 /* Search every queued namespace where SCOPE is the common
1135 ancestor. Adjust the others. */
1136 unsigned ix = 0;
1139 using_pair &pair = queue[ix];
1140 while (pair.first == scope)
1142 found |= search_namespace_only (pair.second);
1143 pair = queue.pop ();
1144 if (ix == queue.length ())
1145 goto done;
1147 /* The depth is the same as SCOPE, find the parent scope. */
1148 if (SCOPE_DEPTH (pair.first) == depth)
1149 pair.first = CP_DECL_CONTEXT (pair.first);
1150 ix++;
1152 while (ix < queue.length ());
1153 done:;
1154 if (scope == global_namespace)
1155 break;
1157 /* If looking for hidden friends, we only look in the innermost
1158 namespace scope. [namespace.memdef]/3 If a friend
1159 declaration in a non-local class first declares a class,
1160 function, class template or function template the friend is a
1161 member of the innermost enclosing namespace. See also
1162 [basic.lookup.unqual]/7 */
1163 if (bool (want & LOOK_want::HIDDEN_FRIEND))
1164 break;
1167 dedup (false);
1169 return found;
1172 /* FNS is a value binding. If it is a (set of overloaded) functions,
1173 add them into the current value. */
1175 void
1176 name_lookup::add_fns (tree fns)
1178 if (!fns)
1179 return;
1180 else if (TREE_CODE (fns) == OVERLOAD)
1182 if (TREE_TYPE (fns) != unknown_type_node)
1183 fns = OVL_FUNCTION (fns);
1185 else if (!DECL_DECLARES_FUNCTION_P (fns))
1186 return;
1188 add_overload (fns);
1191 /* Add the overloaded fns of SCOPE. */
1193 void
1194 name_lookup::adl_namespace_fns (tree scope, bitmap imports)
1196 if (tree *binding = find_namespace_slot (scope, name))
1198 tree val = *binding;
1199 if (TREE_CODE (val) != BINDING_VECTOR)
1200 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val)));
1201 else
1203 /* I presume the binding list is going to be sparser than
1204 the import bitmap. Hence iterate over the former
1205 checking for bits set in the bitmap. */
1206 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
1207 int dup_detect = 0;
1209 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
1211 /* The current TU's bindings must be visible, we don't
1212 need to check the bitmaps. */
1214 if (!deduping)
1216 if (named_module_purview_p ())
1218 dup_detect |= 2;
1220 if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
1221 dup_detect |= 1;
1223 else
1224 dup_detect |= 1;
1227 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
1230 /* Scan the imported bindings. */
1231 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
1232 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
1234 ix--;
1235 cluster++;
1238 /* Do this in forward order, so we load modules in an order
1239 the user expects. */
1240 for (; ix--; cluster++)
1241 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
1243 /* Functions are never on merged slots. */
1244 if (!cluster->indices[jx].base
1245 || cluster->indices[jx].span != 1)
1246 continue;
1248 /* Is this slot visible? */
1249 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
1250 continue;
1252 /* Is it loaded. */
1253 if (cluster->slots[jx].is_lazy ())
1254 lazy_load_binding (cluster->indices[jx].base,
1255 scope, name, &cluster->slots[jx]);
1257 tree bind = cluster->slots[jx];
1258 if (!bind)
1259 /* Load errors could mean there's nothing here. */
1260 continue;
1262 if (STAT_HACK_P (bind))
1264 if (!deduping)
1266 /* Do we need to engage deduplication? */
1267 int dup = 0;
1268 if (MODULE_BINDING_GLOBAL_P (bind))
1269 dup = 1;
1270 else if (MODULE_BINDING_PARTITION_P (bind))
1271 dup = 2;
1272 if (unsigned hit = dup_detect & dup)
1273 if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
1274 || (hit & 2
1275 && BINDING_VECTOR_PARTITION_DUPS_P (val)))
1276 dedup (true);
1277 dup_detect |= dup;
1280 bind = STAT_VISIBLE (bind);
1283 add_fns (bind);
1289 /* Add the hidden friends of SCOPE. */
1291 void
1292 name_lookup::adl_class_fns (tree type)
1294 /* Add friends. */
1295 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
1296 list; list = TREE_CHAIN (list))
1297 if (name == FRIEND_NAME (list))
1299 tree context = NULL_TREE; /* Lazily computed. */
1300 for (tree friends = FRIEND_DECLS (list); friends;
1301 friends = TREE_CHAIN (friends))
1303 tree fn = TREE_VALUE (friends);
1305 /* Only interested in global functions with potentially hidden
1306 (i.e. unqualified) declarations. */
1307 if (!context)
1308 context = decl_namespace_context (type);
1309 if (CP_DECL_CONTEXT (fn) != context)
1310 continue;
1312 dedup (true);
1314 /* Template specializations are never found by name lookup.
1315 (Templates themselves can be found, but not template
1316 specializations.) */
1317 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
1318 continue;
1320 add_fns (fn);
1325 /* Find the containing non-inlined namespace, add it and all its
1326 inlinees. */
1328 void
1329 name_lookup::adl_namespace (tree scope)
1331 if (see_and_mark (scope))
1332 return;
1334 /* Look down into inline namespaces. */
1335 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1336 for (unsigned ix = inlinees->length (); ix--;)
1337 adl_namespace ((*inlinees)[ix]);
1339 if (DECL_NAMESPACE_INLINE_P (scope))
1340 /* Mark parent. */
1341 adl_namespace (CP_DECL_CONTEXT (scope));
1344 /* Adds the class and its friends to the lookup structure. */
1346 void
1347 name_lookup::adl_class_only (tree type)
1349 /* Backend-built structures, such as __builtin_va_list, aren't
1350 affected by all this. */
1351 if (!CLASS_TYPE_P (type))
1352 return;
1354 type = TYPE_MAIN_VARIANT (type);
1356 if (see_and_mark (type))
1357 return;
1359 tree context = decl_namespace_context (type);
1360 adl_namespace (context);
1363 /* Adds the class and its bases to the lookup structure.
1364 Returns true on error. */
1366 void
1367 name_lookup::adl_bases (tree type)
1369 adl_class_only (type);
1371 /* Process baseclasses. */
1372 if (tree binfo = TYPE_BINFO (type))
1374 tree base_binfo;
1375 int i;
1377 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1378 adl_bases (BINFO_TYPE (base_binfo));
1382 /* Adds everything associated with a class argument type to the lookup
1383 structure.
1385 If T is a class type (including unions), its associated classes are: the
1386 class itself; the class of which it is a member, if any; and its direct
1387 and indirect base classes. Its associated namespaces are the namespaces
1388 of which its associated classes are members. Furthermore, if T is a
1389 class template specialization, its associated namespaces and classes
1390 also include: the namespaces and classes associated with the types of
1391 the template arguments provided for template type parameters (excluding
1392 template template parameters); the namespaces of which any template
1393 template arguments are members; and the classes of which any member
1394 templates used as template template arguments are members. [ Note:
1395 non-type template arguments do not contribute to the set of associated
1396 namespaces. --end note] */
1398 void
1399 name_lookup::adl_class (tree type)
1401 /* Backend build structures, such as __builtin_va_list, aren't
1402 affected by all this. */
1403 if (!CLASS_TYPE_P (type))
1404 return;
1406 type = TYPE_MAIN_VARIANT (type);
1408 /* We don't set found here because we have to have set seen first,
1409 which is done in the adl_bases walk. */
1410 if (found_p (type))
1411 return;
1413 complete_type (type);
1414 adl_bases (type);
1415 mark_found (type);
1417 if (TYPE_CLASS_SCOPE_P (type))
1418 adl_class_only (TYPE_CONTEXT (type));
1420 /* Process template arguments. */
1421 if (CLASSTYPE_TEMPLATE_INFO (type)
1422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
1424 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1425 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
1426 adl_template_arg (TREE_VEC_ELT (list, i));
1430 void
1431 name_lookup::adl_enum (tree type)
1433 type = TYPE_MAIN_VARIANT (type);
1434 if (see_and_mark (type))
1435 return;
1437 if (TYPE_CLASS_SCOPE_P (type))
1438 adl_class_only (TYPE_CONTEXT (type));
1439 else
1440 adl_namespace (decl_namespace_context (type));
1443 void
1444 name_lookup::adl_expr (tree expr)
1446 if (!expr)
1447 return;
1449 gcc_assert (!TYPE_P (expr));
1451 if (TREE_TYPE (expr) != unknown_type_node)
1453 adl_type (unlowered_expr_type (expr));
1454 return;
1457 if (TREE_CODE (expr) == ADDR_EXPR)
1458 expr = TREE_OPERAND (expr, 0);
1459 if (TREE_CODE (expr) == COMPONENT_REF
1460 || TREE_CODE (expr) == OFFSET_REF)
1461 expr = TREE_OPERAND (expr, 1);
1462 expr = MAYBE_BASELINK_FUNCTIONS (expr);
1464 if (OVL_P (expr))
1465 for (lkp_iterator iter (expr); iter; ++iter)
1466 adl_type (TREE_TYPE (*iter));
1467 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
1469 /* The working paper doesn't currently say how to handle
1470 template-id arguments. The sensible thing would seem to be
1471 to handle the list of template candidates like a normal
1472 overload set, and handle the template arguments like we do
1473 for class template specializations. */
1475 /* First the templates. */
1476 adl_expr (TREE_OPERAND (expr, 0));
1478 /* Now the arguments. */
1479 if (tree args = TREE_OPERAND (expr, 1))
1480 for (int ix = TREE_VEC_LENGTH (args); ix--;)
1481 adl_template_arg (TREE_VEC_ELT (args, ix));
1485 void
1486 name_lookup::adl_type (tree type)
1488 if (!type)
1489 return;
1491 if (TYPE_PTRDATAMEM_P (type))
1493 /* Pointer to member: associate class type and value type. */
1494 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
1495 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1496 return;
1499 switch (TREE_CODE (type))
1501 case RECORD_TYPE:
1502 if (TYPE_PTRMEMFUNC_P (type))
1504 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
1505 return;
1507 /* FALLTHRU */
1508 case UNION_TYPE:
1509 adl_class (type);
1510 return;
1512 case METHOD_TYPE:
1513 /* The basetype is referenced in the first arg type, so just
1514 fall through. */
1515 case FUNCTION_TYPE:
1516 /* Associate the parameter types. */
1517 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
1518 adl_type (TREE_VALUE (args));
1519 /* FALLTHROUGH */
1521 case POINTER_TYPE:
1522 case REFERENCE_TYPE:
1523 case ARRAY_TYPE:
1524 adl_type (TREE_TYPE (type));
1525 return;
1527 case ENUMERAL_TYPE:
1528 adl_enum (type);
1529 return;
1531 case LANG_TYPE:
1532 gcc_assert (type == unknown_type_node
1533 || type == init_list_type_node);
1534 return;
1536 case TYPE_PACK_EXPANSION:
1537 adl_type (PACK_EXPANSION_PATTERN (type));
1538 return;
1540 default:
1541 break;
1545 /* Adds everything associated with a template argument to the lookup
1546 structure. */
1548 void
1549 name_lookup::adl_template_arg (tree arg)
1551 /* [basic.lookup.koenig]
1553 If T is a template-id, its associated namespaces and classes are
1554 ... the namespaces and classes associated with the types of the
1555 template arguments provided for template type parameters
1556 (excluding template template parameters); the namespaces in which
1557 any template template arguments are defined; and the classes in
1558 which any member templates used as template template arguments
1559 are defined. [Note: non-type template arguments do not
1560 contribute to the set of associated namespaces. ] */
1562 /* Consider first template template arguments. */
1563 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1564 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1566 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1568 tree ctx = CP_DECL_CONTEXT (arg);
1570 /* It's not a member template. */
1571 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1572 adl_namespace (ctx);
1573 /* Otherwise, it must be member template. */
1574 else
1575 adl_class_only (ctx);
1577 /* It's an argument pack; handle it recursively. */
1578 else if (ARGUMENT_PACK_P (arg))
1580 tree args = ARGUMENT_PACK_ARGS (arg);
1581 int i, len = TREE_VEC_LENGTH (args);
1582 for (i = 0; i < len; ++i)
1583 adl_template_arg (TREE_VEC_ELT (args, i));
1585 /* It's not a template template argument, but it is a type template
1586 argument. */
1587 else if (TYPE_P (arg))
1588 adl_type (arg);
1591 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1592 the call arguments. */
1594 tree
1595 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1597 gcc_checking_assert (!vec_safe_length (scopes));
1599 /* Gather each associated entity onto the lookup's scope list. */
1600 unsigned ix;
1601 tree arg;
1603 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1604 /* OMP reduction operators put an ADL-significant type as the
1605 first arg. */
1606 if (TYPE_P (arg))
1607 adl_type (arg);
1608 else
1609 adl_expr (arg);
1611 if (vec_safe_length (scopes))
1613 /* Now do the lookups. */
1614 value = fns;
1615 if (fns)
1616 dedup (true);
1618 /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1619 bitmap inst_path = NULL;
1620 /* VISIBLE is the regular import bitmap. */
1621 bitmap visible = visible_instantiation_path (&inst_path);
1623 for (unsigned ix = scopes->length (); ix--;)
1625 tree scope = (*scopes)[ix];
1626 if (TREE_CODE (scope) == NAMESPACE_DECL)
1627 adl_namespace_fns (scope, visible);
1628 else
1630 if (RECORD_OR_UNION_TYPE_P (scope))
1631 adl_class_fns (scope);
1633 /* During 2nd phase ADL: Any exported declaration D in N
1634 declared within the purview of a named module M
1635 (10.2) is visible if there is an associated entity
1636 attached to M with the same innermost enclosing
1637 non-inline namespace as D.
1638 [basic.lookup.argdep]/4.4 */
1640 if (!inst_path)
1641 /* Not 2nd phase. */
1642 continue;
1644 tree ctx = CP_DECL_CONTEXT (TYPE_NAME (scope));
1645 if (TREE_CODE (ctx) != NAMESPACE_DECL)
1646 /* Not namespace-scope class. */
1647 continue;
1649 tree origin = get_originating_module_decl (TYPE_NAME (scope));
1650 tree not_tmpl = STRIP_TEMPLATE (origin);
1651 if (!DECL_LANG_SPECIFIC (not_tmpl)
1652 || !DECL_MODULE_IMPORT_P (not_tmpl))
1653 /* Not imported. */
1654 continue;
1656 unsigned module = get_importing_module (origin);
1658 if (!bitmap_bit_p (inst_path, module))
1659 /* Not on path of instantiation. */
1660 continue;
1662 if (bitmap_bit_p (visible, module))
1663 /* If the module was in the visible set, we'll look at
1664 its namespace partition anyway. */
1665 continue;
1667 if (tree *slot = find_namespace_slot (ctx, name, false))
1668 if (binding_slot *mslot = search_imported_binding_slot (slot, module))
1670 if (mslot->is_lazy ())
1671 lazy_load_binding (module, ctx, name, mslot);
1673 if (tree bind = *mslot)
1675 /* We must turn on deduping, because some other class
1676 from this module might also be in this namespace. */
1677 dedup (true);
1679 /* Add the exported fns */
1680 if (STAT_HACK_P (bind))
1681 add_fns (STAT_VISIBLE (bind));
1687 fns = value;
1688 dedup (false);
1691 return fns;
1694 static bool qualified_namespace_lookup (tree, name_lookup *);
1695 static void consider_binding_level (tree name,
1696 best_match <tree, const char *> &bm,
1697 cp_binding_level *lvl,
1698 bool look_within_fields,
1699 enum lookup_name_fuzzy_kind kind);
1701 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1702 don't add duplicates to it. ARGS is the vector of call
1703 arguments (which will not be empty). */
1705 tree
1706 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1708 auto_cond_timevar tv (TV_NAME_LOOKUP);
1709 name_lookup lookup (name);
1710 return lookup.search_adl (fns, args);
1713 /* FNS is an overload set of conversion functions. Return the
1714 overloads converting to TYPE. */
1716 static tree
1717 extract_conversion_operator (tree fns, tree type)
1719 tree convs = NULL_TREE;
1720 tree tpls = NULL_TREE;
1722 for (ovl_iterator iter (fns); iter; ++iter)
1724 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1725 convs = lookup_add (*iter, convs);
1727 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1728 tpls = lookup_add (*iter, tpls);
1731 if (!convs)
1732 convs = tpls;
1734 return convs;
1737 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1739 static tree
1740 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1742 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1744 unsigned mid = (lo + hi) / 2;
1745 tree binding = (*member_vec)[mid];
1746 tree binding_name = OVL_NAME (binding);
1748 if (binding_name > name)
1749 hi = mid;
1750 else if (binding_name < name)
1751 lo = mid + 1;
1752 else
1753 return binding;
1756 return NULL_TREE;
1759 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1761 static tree
1762 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1764 for (int ix = member_vec->length (); ix--;)
1765 if (tree binding = (*member_vec)[ix])
1766 if (OVL_NAME (binding) == name)
1767 return binding;
1769 return NULL_TREE;
1772 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1774 static tree
1775 fields_linear_search (tree klass, tree name, bool want_type)
1777 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1779 tree decl = fields;
1781 if (TREE_CODE (decl) == FIELD_DECL
1782 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1784 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1785 return temp;
1788 if (DECL_NAME (decl) != name)
1789 continue;
1791 if (TREE_CODE (decl) == USING_DECL)
1793 decl = strip_using_decl (decl);
1794 if (is_overloaded_fn (decl))
1795 continue;
1798 if (DECL_DECLARES_FUNCTION_P (decl))
1799 /* Functions are found separately. */
1800 continue;
1802 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1803 return decl;
1806 return NULL_TREE;
1809 /* Look for NAME member inside of anonymous aggregate ANON. Although
1810 such things should only contain FIELD_DECLs, we check that too
1811 late, and would give very confusing errors if we weren't
1812 permissive here. */
1814 tree
1815 search_anon_aggr (tree anon, tree name, bool want_type)
1817 gcc_assert (COMPLETE_TYPE_P (anon));
1818 tree ret = get_class_binding_direct (anon, name, want_type);
1819 return ret;
1822 /* Look for NAME as an immediate member of KLASS (including
1823 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1824 regular search. >0 to get a type binding (if there is one) and <0
1825 if you want (just) the member function binding.
1827 Use this if you do not want lazy member creation. */
1829 tree
1830 get_class_binding_direct (tree klass, tree name, bool want_type)
1832 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1834 /* Conversion operators can only be found by the marker conversion
1835 operator name. */
1836 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1837 tree lookup = conv_op ? conv_op_identifier : name;
1838 tree val = NULL_TREE;
1839 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1841 if (COMPLETE_TYPE_P (klass) && member_vec)
1843 val = member_vec_binary_search (member_vec, lookup);
1844 if (!val)
1846 else if (STAT_HACK_P (val))
1847 val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
1848 else if (want_type && !DECL_DECLARES_TYPE_P (val))
1849 val = NULL_TREE;
1851 else
1853 if (member_vec && !want_type)
1854 val = member_vec_linear_search (member_vec, lookup);
1856 if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
1857 /* Dependent using declarations are a 'field', make sure we
1858 return that even if we saw an overload already. */
1859 if (tree field_val = fields_linear_search (klass, lookup, want_type))
1861 if (!val)
1862 val = field_val;
1863 else if (TREE_CODE (field_val) == USING_DECL)
1864 val = ovl_make (field_val, val);
1868 /* Extract the conversion operators asked for, unless the general
1869 conversion operator was requested. */
1870 if (val && conv_op)
1872 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1873 val = OVL_CHAIN (val);
1874 if (tree type = TREE_TYPE (name))
1875 val = extract_conversion_operator (val, type);
1878 return val;
1881 /* We're about to lookup NAME in KLASS. Make sure any lazily declared
1882 members are now declared. */
1884 static void
1885 maybe_lazily_declare (tree klass, tree name)
1887 /* See big comment anout module_state::write_pendings regarding adding a check
1888 bit. */
1889 if (modules_p ())
1890 lazy_load_pendings (TYPE_NAME (klass));
1892 /* Lazily declare functions, if we're going to search these. */
1893 if (IDENTIFIER_CTOR_P (name))
1895 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1896 lazily_declare_fn (sfk_constructor, klass);
1897 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1898 lazily_declare_fn (sfk_copy_constructor, klass);
1899 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1900 lazily_declare_fn (sfk_move_constructor, klass);
1902 else if (IDENTIFIER_DTOR_P (name))
1904 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1905 lazily_declare_fn (sfk_destructor, klass);
1907 else if (name == assign_op_identifier)
1909 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1910 lazily_declare_fn (sfk_copy_assignment, klass);
1911 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1912 lazily_declare_fn (sfk_move_assignment, klass);
1916 /* Look for NAME's binding in exactly KLASS. See
1917 get_class_binding_direct for argument description. Does lazy
1918 special function creation as necessary. */
1920 tree
1921 get_class_binding (tree klass, tree name, bool want_type /*=false*/)
1923 klass = complete_type (klass);
1925 if (COMPLETE_TYPE_P (klass))
1926 maybe_lazily_declare (klass, name);
1928 return get_class_binding_direct (klass, name, want_type);
1931 /* Find the slot containing overloads called 'NAME'. If there is no
1932 such slot and the class is complete, create an empty one, at the
1933 correct point in the sorted member vector. Otherwise return NULL.
1934 Deals with conv_op marker handling. */
1936 tree *
1937 find_member_slot (tree klass, tree name)
1939 bool complete_p = COMPLETE_TYPE_P (klass);
1941 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1942 if (!member_vec)
1944 vec_alloc (member_vec, 8);
1945 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1946 if (complete_p)
1947 /* If the class is complete but had no member_vec, we need to
1948 add the TYPE_FIELDS into it. We're also most likely to be
1949 adding ctors & dtors, so ask for 6 spare slots (the
1950 abstract cdtors and their clones). */
1951 member_vec = set_class_bindings (klass, 6);
1954 if (IDENTIFIER_CONV_OP_P (name))
1955 name = conv_op_identifier;
1957 unsigned ix, length = member_vec->length ();
1958 for (ix = 0; ix < length; ix++)
1960 tree *slot = &(*member_vec)[ix];
1961 tree fn_name = OVL_NAME (*slot);
1963 if (fn_name == name)
1965 /* If we found an existing slot, it must be a function set.
1966 Even with insertion after completion, because those only
1967 happen with artificial fns that have unspellable names.
1968 This means we do not have to deal with the stat hack
1969 either. */
1970 gcc_checking_assert (OVL_P (*slot));
1971 if (name == conv_op_identifier)
1973 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1974 /* Skip the conv-op marker. */
1975 slot = &OVL_CHAIN (*slot);
1977 return slot;
1980 if (complete_p && fn_name > name)
1981 break;
1984 /* No slot found, add one if the class is complete. */
1985 if (complete_p)
1987 /* Do exact allocation, as we don't expect to add many. */
1988 gcc_assert (name != conv_op_identifier);
1989 vec_safe_reserve_exact (member_vec, 1);
1990 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1991 member_vec->quick_insert (ix, NULL_TREE);
1992 return &(*member_vec)[ix];
1995 return NULL;
1998 /* KLASS is an incomplete class to which we're adding a method NAME.
1999 Add a slot and deal with conv_op marker handling. */
2001 tree *
2002 add_member_slot (tree klass, tree name)
2004 gcc_assert (!COMPLETE_TYPE_P (klass));
2006 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2007 vec_safe_push (member_vec, NULL_TREE);
2008 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2010 tree *slot = &member_vec->last ();
2011 if (IDENTIFIER_CONV_OP_P (name))
2013 /* Install the marker prefix. */
2014 *slot = ovl_make (conv_op_marker, NULL_TREE);
2015 slot = &OVL_CHAIN (*slot);
2018 return slot;
2021 /* Comparison function to compare two MEMBER_VEC entries by name.
2022 Because we can have duplicates during insertion of TYPE_FIELDS, we
2023 do extra checking so deduping doesn't have to deal with so many
2024 cases. */
2026 static int
2027 member_name_cmp (const void *a_p, const void *b_p)
2029 tree a = *(const tree *)a_p;
2030 tree b = *(const tree *)b_p;
2031 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
2032 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
2034 gcc_checking_assert (name_a && name_b);
2035 if (name_a != name_b)
2036 return name_a < name_b ? -1 : +1;
2038 if (name_a == conv_op_identifier)
2040 /* Strip the conv-op markers. */
2041 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
2042 && OVL_FUNCTION (b) == conv_op_marker);
2043 a = OVL_CHAIN (a);
2044 b = OVL_CHAIN (b);
2047 if (TREE_CODE (a) == OVERLOAD)
2048 a = OVL_FUNCTION (a);
2049 if (TREE_CODE (b) == OVERLOAD)
2050 b = OVL_FUNCTION (b);
2052 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
2053 if (TREE_CODE (a) != TREE_CODE (b))
2055 /* If one of them is a TYPE_DECL, it loses. */
2056 if (TREE_CODE (a) == TYPE_DECL)
2057 return +1;
2058 else if (TREE_CODE (b) == TYPE_DECL)
2059 return -1;
2061 /* If one of them is a USING_DECL, it loses. */
2062 if (TREE_CODE (a) == USING_DECL)
2063 return +1;
2064 else if (TREE_CODE (b) == USING_DECL)
2065 return -1;
2067 /* There are no other cases with different kinds of decls, as
2068 duplicate detection should have kicked in earlier. However,
2069 some erroneous cases get though. */
2070 gcc_assert (errorcount);
2073 /* Using source location would be the best thing here, but we can
2074 get identically-located decls in the following circumstances:
2076 1) duplicate artificial type-decls for the same type.
2078 2) pack expansions of using-decls.
2080 We should not be doing #1, but in either case it doesn't matter
2081 how we order these. Use UID as a proxy for source ordering, so
2082 that identically-located decls still have a well-defined stable
2083 ordering. */
2084 if (DECL_UID (a) != DECL_UID (b))
2085 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
2086 gcc_assert (a == b);
2087 return 0;
2090 static struct {
2091 gt_pointer_operator new_value;
2092 void *cookie;
2093 } resort_data;
2095 /* This routine compares two fields like member_name_cmp but using the
2096 pointer operator in resort_field_decl_data. We don't have to deal
2097 with duplicates here. */
2099 static int
2100 resort_member_name_cmp (const void *a_p, const void *b_p)
2102 tree a = *(const tree *)a_p;
2103 tree b = *(const tree *)b_p;
2104 tree name_a = OVL_NAME (a);
2105 tree name_b = OVL_NAME (b);
2107 resort_data.new_value (&name_a, &name_a, resort_data.cookie);
2108 resort_data.new_value (&name_b, &name_b, resort_data.cookie);
2110 gcc_checking_assert (name_a != name_b);
2112 return name_a < name_b ? -1 : +1;
2115 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2117 void
2118 resort_type_member_vec (void *obj, void */*orig_obj*/,
2119 gt_pointer_operator new_value, void* cookie)
2121 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
2123 resort_data.new_value = new_value;
2124 resort_data.cookie = cookie;
2125 member_vec->qsort (resort_member_name_cmp);
2129 /* Recursively count the number of fields in KLASS, including anonymous
2130 union members. */
2132 static unsigned
2133 count_class_fields (tree klass)
2135 unsigned n_fields = 0;
2137 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2138 if (DECL_DECLARES_FUNCTION_P (fields))
2139 /* Functions are dealt with separately. */;
2140 else if (TREE_CODE (fields) == FIELD_DECL
2141 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2142 n_fields += count_class_fields (TREE_TYPE (fields));
2143 else if (DECL_NAME (fields))
2144 n_fields += 1;
2146 return n_fields;
2149 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
2150 Recurse for anonymous members. MEMBER_VEC must have space. */
2152 static void
2153 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
2155 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2156 if (DECL_DECLARES_FUNCTION_P (fields))
2157 /* Functions are handled separately. */;
2158 else if (TREE_CODE (fields) == FIELD_DECL
2159 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2160 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
2161 else if (DECL_NAME (fields))
2163 tree field = fields;
2164 /* Mark a conv-op USING_DECL with the conv-op-marker. */
2165 if (TREE_CODE (field) == USING_DECL
2166 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
2167 field = ovl_make (conv_op_marker, field);
2168 member_vec->quick_push (field);
2172 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
2173 MEMBER_VEC must have space. */
2175 static void
2176 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
2178 for (tree values = TYPE_VALUES (enumtype);
2179 values; values = TREE_CHAIN (values))
2180 member_vec->quick_push (TREE_VALUE (values));
2183 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
2184 DeDup adjacent DECLS of the same name. We already dealt with
2185 conflict resolution when adding the fields or methods themselves.
2186 There are three cases (which could all be combined):
2187 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
2188 2) a USING_DECL and an overload. If the USING_DECL is dependent,
2189 it wins. Otherwise the OVERLOAD does.
2190 3) two USING_DECLS. ...
2192 member_name_cmp will have ordered duplicates as
2193 <fns><using><type> */
2195 static void
2196 member_vec_dedup (vec<tree, va_gc> *member_vec)
2198 unsigned len = member_vec->length ();
2199 unsigned store = 0;
2201 if (!len)
2202 return;
2204 tree name = OVL_NAME ((*member_vec)[0]);
2205 for (unsigned jx, ix = 0; ix < len; ix = jx)
2207 tree current = NULL_TREE;
2208 tree to_type = NULL_TREE;
2209 tree to_using = NULL_TREE;
2210 tree marker = NULL_TREE;
2212 for (jx = ix; jx < len; jx++)
2214 tree next = (*member_vec)[jx];
2215 if (jx != ix)
2217 tree next_name = OVL_NAME (next);
2218 if (next_name != name)
2220 name = next_name;
2221 break;
2225 if (IDENTIFIER_CONV_OP_P (name))
2227 marker = next;
2228 next = OVL_CHAIN (next);
2231 if (TREE_CODE (next) == USING_DECL)
2233 if (IDENTIFIER_CTOR_P (name))
2234 /* Dependent inherited ctor. */
2235 continue;
2237 next = strip_using_decl (next);
2238 if (TREE_CODE (next) == USING_DECL)
2240 to_using = next;
2241 continue;
2244 if (is_overloaded_fn (next))
2245 continue;
2248 if (DECL_DECLARES_TYPE_P (next))
2250 to_type = next;
2251 continue;
2254 if (!current)
2255 current = next;
2258 if (to_using)
2260 if (!current)
2261 current = to_using;
2262 else
2263 current = ovl_make (to_using, current);
2266 if (to_type)
2268 if (!current)
2269 current = to_type;
2270 else
2271 current = stat_hack (current, to_type);
2274 if (current)
2276 if (marker)
2278 OVL_CHAIN (marker) = current;
2279 current = marker;
2281 (*member_vec)[store++] = current;
2285 while (store++ < len)
2286 member_vec->pop ();
2289 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
2290 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
2291 know there must be at least 1 field -- the self-reference
2292 TYPE_DECL, except for anon aggregates, which will have at least
2293 one field anyway. If EXTRA < 0, always create the vector. */
2295 vec<tree, va_gc> *
2296 set_class_bindings (tree klass, int extra)
2298 unsigned n_fields = count_class_fields (klass);
2299 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2301 if (member_vec || n_fields >= 8 || extra < 0)
2303 /* Append the new fields. */
2304 vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
2305 member_vec_append_class_fields (member_vec, klass);
2308 if (member_vec)
2310 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2311 member_vec->qsort (member_name_cmp);
2312 member_vec_dedup (member_vec);
2315 return member_vec;
2318 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
2320 void
2321 insert_late_enum_def_bindings (tree klass, tree enumtype)
2323 int n_fields;
2324 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2326 /* The enum bindings will already be on the TYPE_FIELDS, so don't
2327 count them twice. */
2328 if (!member_vec)
2329 n_fields = count_class_fields (klass);
2330 else
2331 n_fields = list_length (TYPE_VALUES (enumtype));
2333 if (member_vec || n_fields >= 8)
2335 vec_safe_reserve_exact (member_vec, n_fields);
2336 if (CLASSTYPE_MEMBER_VEC (klass))
2337 member_vec_append_enum_values (member_vec, enumtype);
2338 else
2339 member_vec_append_class_fields (member_vec, klass);
2340 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2341 member_vec->qsort (member_name_cmp);
2342 member_vec_dedup (member_vec);
2346 /* The binding oracle; see cp-tree.h. */
2348 cp_binding_oracle_function *cp_binding_oracle;
2350 /* If we have a binding oracle, ask it for all namespace-scoped
2351 definitions of NAME. */
2353 static inline void
2354 query_oracle (tree name)
2356 if (!cp_binding_oracle)
2357 return;
2359 /* LOOKED_UP holds the set of identifiers that we have already
2360 looked up with the oracle. */
2361 static hash_set<tree> looked_up;
2362 if (looked_up.add (name))
2363 return;
2365 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
2368 #ifndef ENABLE_SCOPE_CHECKING
2369 # define ENABLE_SCOPE_CHECKING 0
2370 #else
2371 # define ENABLE_SCOPE_CHECKING 1
2372 #endif
2374 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
2376 static GTY((deletable)) cxx_binding *free_bindings;
2378 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
2379 field to NULL. */
2381 static inline void
2382 cxx_binding_init (cxx_binding *binding, tree value, tree type)
2384 binding->value = value;
2385 binding->type = type;
2386 binding->previous = NULL;
2389 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2391 static cxx_binding *
2392 cxx_binding_make (tree value, tree type)
2394 cxx_binding *binding = free_bindings;
2396 if (binding)
2397 free_bindings = binding->previous;
2398 else
2399 binding = ggc_alloc<cxx_binding> ();
2401 /* Clear flags by default. */
2402 LOCAL_BINDING_P (binding) = false;
2403 INHERITED_VALUE_BINDING_P (binding) = false;
2404 HIDDEN_TYPE_BINDING_P (binding) = false;
2406 cxx_binding_init (binding, value, type);
2408 return binding;
2411 /* Put BINDING back on the free list. */
2413 static inline void
2414 cxx_binding_free (cxx_binding *binding)
2416 binding->scope = NULL;
2417 binding->previous = free_bindings;
2418 free_bindings = binding;
2421 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2422 bindings) in the class scope indicated by SCOPE. */
2424 static cxx_binding *
2425 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2427 cp_class_binding cb = {cxx_binding_make (value, type), name};
2428 cxx_binding *binding = cb.base;
2429 vec_safe_push (scope->class_shadowed, cb);
2430 binding->scope = scope;
2431 return binding;
2434 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2435 level at which this declaration is being bound. */
2437 void
2438 push_binding (tree id, tree decl, cp_binding_level* level)
2440 cxx_binding *binding;
2442 if (level != class_binding_level)
2444 binding = cxx_binding_make (decl, NULL_TREE);
2445 binding->scope = level;
2447 else
2448 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2450 /* Now, fill in the binding information. */
2451 binding->previous = IDENTIFIER_BINDING (id);
2452 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2454 /* And put it on the front of the list of bindings for ID. */
2455 IDENTIFIER_BINDING (id) = binding;
2458 /* Remove the binding for DECL which should be the innermost binding
2459 for ID. */
2461 void
2462 pop_local_binding (tree id, tree decl)
2464 if (!id || IDENTIFIER_ANON_P (id))
2465 /* It's easiest to write the loops that call this function without
2466 checking whether or not the entities involved have names. We
2467 get here for such an entity. */
2468 return;
2470 /* Get the innermost binding for ID. */
2471 cxx_binding *binding = IDENTIFIER_BINDING (id);
2473 /* The name should be bound. */
2474 gcc_assert (binding != NULL);
2476 /* The DECL will be either the ordinary binding or the type binding
2477 for this identifier. Remove that binding. We don't have to
2478 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2479 away. */
2480 if (binding->value == decl)
2481 binding->value = NULL_TREE;
2482 else
2484 gcc_checking_assert (binding->type == decl);
2485 binding->type = NULL_TREE;
2488 if (!binding->value && !binding->type)
2490 /* We're completely done with the innermost binding for this
2491 identifier. Unhook it from the list of bindings. */
2492 IDENTIFIER_BINDING (id) = binding->previous;
2494 /* Add it to the free list. */
2495 cxx_binding_free (binding);
2499 /* Remove the bindings for the decls of the current level and leave
2500 the current scope. */
2502 void
2503 pop_bindings_and_leave_scope (void)
2505 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2507 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2508 tree name = OVL_NAME (decl);
2510 pop_local_binding (name, decl);
2513 leave_scope ();
2516 /* Strip non dependent using declarations. If DECL is dependent,
2517 surreptitiously create a typename_type and return it. */
2519 tree
2520 strip_using_decl (tree decl)
2522 if (decl == NULL_TREE)
2523 return NULL_TREE;
2525 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2526 decl = USING_DECL_DECLS (decl);
2528 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2529 && USING_DECL_TYPENAME_P (decl))
2531 /* We have found a type introduced by a using
2532 declaration at class scope that refers to a dependent
2533 type.
2535 using typename :: [opt] nested-name-specifier unqualified-id ;
2537 decl = make_typename_type (USING_DECL_SCOPE (decl),
2538 DECL_NAME (decl),
2539 typename_type, tf_error);
2540 if (decl != error_mark_node)
2541 decl = TYPE_NAME (decl);
2544 return decl;
2547 /* Return true if OVL is an overload for an anticipated builtin. */
2549 static bool
2550 anticipated_builtin_p (tree ovl)
2552 return (TREE_CODE (ovl) == OVERLOAD
2553 && OVL_HIDDEN_P (ovl)
2554 && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl)));
2557 /* BINDING records an existing declaration for a name in the current scope.
2558 But, DECL is another declaration for that same identifier in the
2559 same scope. This is the `struct stat' hack whereby a non-typedef
2560 class name or enum-name can be bound at the same level as some other
2561 kind of entity.
2562 3.3.7/1
2564 A class name (9.1) or enumeration name (7.2) can be hidden by the
2565 name of an object, function, or enumerator declared in the same scope.
2566 If a class or enumeration name and an object, function, or enumerator
2567 are declared in the same scope (in any order) with the same name, the
2568 class or enumeration name is hidden wherever the object, function, or
2569 enumerator name is visible.
2571 It's the responsibility of the caller to check that
2572 inserting this name is valid here. Returns nonzero if the new binding
2573 was successful. */
2575 static bool
2576 supplement_binding (cxx_binding *binding, tree decl)
2578 auto_cond_timevar tv (TV_NAME_LOOKUP);
2580 tree bval = binding->value;
2581 bool ok = true;
2582 tree target_bval = strip_using_decl (bval);
2583 tree target_decl = strip_using_decl (decl);
2585 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2586 && target_decl != target_bval
2587 && (TREE_CODE (target_bval) != TYPE_DECL
2588 /* We allow pushing an enum multiple times in a class
2589 template in order to handle late matching of underlying
2590 type on an opaque-enum-declaration followed by an
2591 enum-specifier. */
2592 || (processing_template_decl
2593 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2594 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2595 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2596 (TREE_TYPE (target_decl)))
2597 || dependent_type_p (ENUM_UNDERLYING_TYPE
2598 (TREE_TYPE (target_bval)))))))
2599 /* The new name is the type name. */
2600 binding->type = decl;
2601 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2602 an inherited type-binding out of the way to make room
2603 for a new value binding. */
2604 !target_bval
2605 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2606 has been used in a non-class scope prior declaration.
2607 In that case, we should have already issued a
2608 diagnostic; for graceful error recovery purpose, pretend
2609 this was the intended declaration for that name. */
2610 || target_bval == error_mark_node
2611 /* If TARGET_BVAL is anticipated but has not yet been
2612 declared, pretend it is not there at all. */
2613 || anticipated_builtin_p (target_bval))
2614 binding->value = decl;
2615 else if (TREE_CODE (target_bval) == TYPE_DECL
2616 && DECL_ARTIFICIAL (target_bval)
2617 && target_decl != target_bval
2618 && (TREE_CODE (target_decl) != TYPE_DECL
2619 || same_type_p (TREE_TYPE (target_decl),
2620 TREE_TYPE (target_bval))))
2622 /* The old binding was a type name. It was placed in
2623 VALUE field because it was thought, at the point it was
2624 declared, to be the only entity with such a name. Move the
2625 type name into the type slot; it is now hidden by the new
2626 binding. */
2627 binding->type = bval;
2628 binding->value = decl;
2629 binding->value_is_inherited = false;
2631 else if (TREE_CODE (target_bval) == TYPE_DECL
2632 && TREE_CODE (target_decl) == TYPE_DECL
2633 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2634 && binding->scope->kind != sk_class
2635 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2636 /* If either type involves template parameters, we must
2637 wait until instantiation. */
2638 || uses_template_parms (TREE_TYPE (target_decl))
2639 || uses_template_parms (TREE_TYPE (target_bval))))
2640 /* We have two typedef-names, both naming the same type to have
2641 the same name. In general, this is OK because of:
2643 [dcl.typedef]
2645 In a given scope, a typedef specifier can be used to redefine
2646 the name of any type declared in that scope to refer to the
2647 type to which it already refers.
2649 However, in class scopes, this rule does not apply due to the
2650 stricter language in [class.mem] prohibiting redeclarations of
2651 members. */
2652 ok = false;
2653 /* There can be two block-scope declarations of the same variable,
2654 so long as they are `extern' declarations. However, there cannot
2655 be two declarations of the same static data member:
2657 [class.mem]
2659 A member shall not be declared twice in the
2660 member-specification. */
2661 else if (VAR_P (target_decl)
2662 && VAR_P (target_bval)
2663 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2664 && !DECL_CLASS_SCOPE_P (target_decl))
2666 duplicate_decls (decl, binding->value);
2667 ok = false;
2669 else if (TREE_CODE (decl) == NAMESPACE_DECL
2670 && TREE_CODE (bval) == NAMESPACE_DECL
2671 && DECL_NAMESPACE_ALIAS (decl)
2672 && DECL_NAMESPACE_ALIAS (bval)
2673 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2674 /* [namespace.alias]
2676 In a declarative region, a namespace-alias-definition can be
2677 used to redefine a namespace-alias declared in that declarative
2678 region to refer only to the namespace to which it already
2679 refers. */
2680 ok = false;
2681 else if (TREE_CODE (bval) == USING_DECL
2682 && CONST_DECL_USING_P (decl))
2683 /* Let the clone hide the using-decl that introduced it. */
2684 binding->value = decl;
2685 else
2687 if (!error_operand_p (bval))
2688 diagnose_name_conflict (decl, bval);
2689 ok = false;
2692 return ok;
2695 /* Diagnose a name conflict between DECL and BVAL.
2697 This is non-static so maybe_push_used_methods can use it and avoid changing
2698 the diagnostic for inherit/using4.C; otherwise it should not be used from
2699 outside this file. */
2701 void
2702 diagnose_name_conflict (tree decl, tree bval)
2704 if (TREE_CODE (decl) == TREE_CODE (bval)
2705 && TREE_CODE (decl) != NAMESPACE_DECL
2706 && !DECL_DECLARES_FUNCTION_P (decl)
2707 && (TREE_CODE (decl) != TYPE_DECL
2708 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2709 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2711 if (concept_definition_p (decl))
2712 error ("redeclaration of %q#D with different template parameters",
2713 decl);
2714 else
2715 error ("redeclaration of %q#D", decl);
2717 else
2718 error ("%q#D conflicts with a previous declaration", decl);
2720 inform (location_of (bval), "previous declaration %q#D", bval);
2723 /* Replace BINDING's current value on its scope's name list with
2724 NEWVAL. */
2726 static void
2727 update_local_overload (cxx_binding *binding, tree newval)
2729 tree *d;
2731 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2732 if (*d == binding->value)
2734 /* Stitch new list node in. */
2735 *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
2736 break;
2738 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2739 break;
2741 TREE_VALUE (*d) = newval;
2744 /* Compares the parameter-type-lists of ONE and TWO and
2745 returns false if they are different. If the DECLs are template
2746 functions, the return types and the template parameter lists are
2747 compared too (DR 565). */
2749 static bool
2750 matching_fn_p (tree one, tree two)
2752 if (TREE_CODE (one) != TREE_CODE (two))
2753 return false;
2755 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2756 TYPE_ARG_TYPES (TREE_TYPE (two))))
2757 return false;
2759 if (TREE_CODE (one) == TEMPLATE_DECL)
2761 /* Compare template parms. */
2762 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2763 DECL_TEMPLATE_PARMS (two)))
2764 return false;
2766 /* And return type. */
2767 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2768 TREE_TYPE (TREE_TYPE (two))))
2769 return false;
2772 if (!equivalently_constrained (one, two))
2773 return false;
2775 return true;
2778 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2779 binding value (possibly with anticipated builtins stripped).
2780 Diagnose conflicts and return updated decl. */
2782 static tree
2783 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2784 tree old, tree decl, bool hiding = false)
2786 tree old_type = NULL_TREE;
2787 bool hide_type = false;
2788 bool hide_value = false;
2790 if (!slot)
2792 old_type = binding->type;
2793 hide_type = HIDDEN_TYPE_BINDING_P (binding);
2794 if (!old_type)
2795 hide_value = hide_type, hide_type = false;
2797 else if (STAT_HACK_P (*slot))
2799 old_type = STAT_TYPE (*slot);
2800 hide_type = STAT_TYPE_HIDDEN_P (*slot);
2801 hide_value = STAT_DECL_HIDDEN_P (*slot);
2804 tree to_val = decl;
2805 tree to_type = old_type;
2806 bool local_overload = false;
2808 gcc_assert (!level || level->kind == sk_namespace ? !binding
2809 : level->kind != sk_class && !slot);
2811 if (old == error_mark_node)
2812 old = NULL_TREE;
2814 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2816 /* Pushing an artificial decl. We should not find another
2817 artificial decl here already -- lookup_elaborated_type will
2818 have already found it. */
2819 gcc_checking_assert (!to_type
2820 && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
2822 if (old)
2824 /* Put DECL into the type slot. */
2825 gcc_checking_assert (!to_type);
2826 hide_type = hiding;
2827 to_type = decl;
2828 to_val = old;
2830 else
2831 hide_value = hiding;
2833 goto done;
2836 if (old && DECL_IMPLICIT_TYPEDEF_P (old))
2838 /* OLD is an implicit typedef. Move it to to_type. */
2839 gcc_checking_assert (!to_type);
2841 to_type = old;
2842 hide_type = hide_value;
2843 old = NULL_TREE;
2844 hide_value = false;
2847 if (DECL_DECLARES_FUNCTION_P (decl))
2849 if (!old)
2851 else if (OVL_P (old))
2853 for (ovl_iterator iter (old); iter; ++iter)
2855 tree fn = *iter;
2857 if (iter.using_p () && matching_fn_p (fn, decl))
2859 gcc_checking_assert (!iter.hidden_p ());
2860 /* If a function declaration in namespace scope or
2861 block scope has the same name and the same
2862 parameter-type- list (8.3.5) as a function
2863 introduced by a using-declaration, and the
2864 declarations do not declare the same function,
2865 the program is ill-formed. [namespace.udecl]/14 */
2866 if (tree match = duplicate_decls (decl, fn, hiding))
2867 return match;
2868 else
2869 /* FIXME: To preserve existing error behavior, we
2870 still push the decl. This might change. */
2871 diagnose_name_conflict (decl, fn);
2875 else
2876 goto conflict;
2878 if (to_type != old_type
2879 && warn_shadow
2880 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2881 && !(DECL_IN_SYSTEM_HEADER (decl)
2882 && DECL_IN_SYSTEM_HEADER (to_type)))
2883 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2884 decl, to_type);
2886 local_overload = old && level && level->kind != sk_namespace;
2887 to_val = ovl_insert (decl, old, -int (hiding));
2889 else if (old)
2891 if (TREE_CODE (old) != TREE_CODE (decl))
2892 /* Different kinds of decls conflict. */
2893 goto conflict;
2894 else if (TREE_CODE (old) == TYPE_DECL)
2896 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2897 /* Two type decls to the same type. Do nothing. */
2898 return old;
2899 else
2900 goto conflict;
2902 else if (TREE_CODE (old) == NAMESPACE_DECL)
2904 /* Two maybe-aliased namespaces. If they're to the same target
2905 namespace, that's ok. */
2906 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2907 goto conflict;
2909 /* The new one must be an alias at this point. */
2910 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2911 return old;
2913 else if (TREE_CODE (old) == VAR_DECL)
2915 /* There can be two block-scope declarations of the same
2916 variable, so long as they are `extern' declarations. */
2917 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2918 goto conflict;
2919 else if (tree match = duplicate_decls (decl, old))
2921 gcc_checking_assert (!hide_value && !hiding);
2922 return match;
2924 else
2925 goto conflict;
2927 else
2929 conflict:
2930 diagnose_name_conflict (decl, old);
2931 to_val = NULL_TREE;
2934 else if (hiding)
2935 hide_value = true;
2937 done:
2938 if (to_val)
2940 if (local_overload)
2942 gcc_checking_assert (binding->value && OVL_P (binding->value));
2943 update_local_overload (binding, to_val);
2945 else if (level
2946 && !(TREE_CODE (decl) == NAMESPACE_DECL
2947 && !DECL_NAMESPACE_ALIAS (decl)))
2948 /* Don't add namespaces here. They're done in
2949 push_namespace. */
2950 add_decl_to_level (level, decl);
2952 if (slot)
2954 if (STAT_HACK_P (*slot))
2956 STAT_TYPE (*slot) = to_type;
2957 STAT_DECL (*slot) = to_val;
2958 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
2959 STAT_DECL_HIDDEN_P (*slot) = hide_value;
2961 else if (to_type || hide_value)
2963 *slot = stat_hack (to_val, to_type);
2964 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
2965 STAT_DECL_HIDDEN_P (*slot) = hide_value;
2967 else
2969 gcc_checking_assert (!hide_type);
2970 *slot = to_val;
2973 else
2975 binding->type = to_type;
2976 binding->value = to_val;
2977 HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
2981 return decl;
2984 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2986 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2988 /* DECL has C linkage. If we have an existing instance, make sure the
2989 new one is compatible. Make sure it has the same exception
2990 specification [7.5, 7.6]. Add DECL to the map. */
2992 static void
2993 check_extern_c_conflict (tree decl)
2995 /* Ignore artificial or system header decls. */
2996 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2997 return;
2999 /* This only applies to decls at namespace scope. */
3000 if (!DECL_NAMESPACE_SCOPE_P (decl))
3001 return;
3003 if (!extern_c_decls)
3004 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
3006 tree *slot = extern_c_decls
3007 ->find_slot_with_hash (DECL_NAME (decl),
3008 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
3009 if (tree old = *slot)
3011 if (TREE_CODE (old) == OVERLOAD)
3012 old = OVL_FUNCTION (old);
3014 int mismatch = 0;
3015 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
3016 ; /* If they're in the same context, we'll have already complained
3017 about a (possible) mismatch, when inserting the decl. */
3018 else if (!decls_match (decl, old))
3019 mismatch = 1;
3020 else if (TREE_CODE (decl) == FUNCTION_DECL
3021 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
3022 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3023 ce_normal))
3024 mismatch = -1;
3025 else if (DECL_ASSEMBLER_NAME_SET_P (old))
3026 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
3028 if (mismatch)
3030 auto_diagnostic_group d;
3031 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3032 "conflicting C language linkage declaration %q#D", decl);
3033 inform (DECL_SOURCE_LOCATION (old),
3034 "previous declaration %q#D", old);
3035 if (mismatch < 0)
3036 inform (DECL_SOURCE_LOCATION (decl),
3037 "due to different exception specifications");
3039 else
3041 if (old == *slot)
3042 /* The hash table expects OVERLOADS, so construct one with
3043 OLD as both the function and the chain. This allocate
3044 an excess OVERLOAD node, but it's rare to have multiple
3045 extern "C" decls of the same name. And we save
3046 complicating the hash table logic (which is used
3047 elsewhere). */
3048 *slot = ovl_make (old, old);
3050 slot = &OVL_CHAIN (*slot);
3052 /* Chain it on for c_linkage_binding's use. */
3053 *slot = tree_cons (NULL_TREE, decl, *slot);
3056 else
3057 *slot = decl;
3060 /* Returns a list of C-linkage decls with the name NAME. Used in
3061 c-family/c-pragma.cc to implement redefine_extname pragma. */
3063 tree
3064 c_linkage_bindings (tree name)
3066 if (extern_c_decls)
3067 if (tree *slot = extern_c_decls
3068 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
3070 tree result = *slot;
3071 if (TREE_CODE (result) == OVERLOAD)
3072 result = OVL_CHAIN (result);
3073 return result;
3076 return NULL_TREE;
3079 /* Subroutine of check_local_shadow. */
3081 static void
3082 inform_shadowed (tree shadowed)
3084 inform (DECL_SOURCE_LOCATION (shadowed),
3085 "shadowed declaration is here");
3088 /* DECL is being declared at a local scope. Emit suitable shadow
3089 warnings. */
3091 static void
3092 check_local_shadow (tree decl)
3094 /* Don't complain about the parms we push and then pop
3095 while tentatively parsing a function declarator. */
3096 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
3097 return;
3099 /* External decls are something else. */
3100 if (DECL_EXTERNAL (decl))
3101 return;
3103 tree old = NULL_TREE;
3104 cp_binding_level *old_scope = NULL;
3105 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
3107 old = binding->value;
3108 old_scope = binding->scope;
3111 if (old
3112 && (TREE_CODE (old) == PARM_DECL
3113 || VAR_P (old)
3114 || (TREE_CODE (old) == TYPE_DECL
3115 && (!DECL_ARTIFICIAL (old)
3116 || TREE_CODE (decl) == TYPE_DECL)))
3117 && DECL_FUNCTION_SCOPE_P (old)
3118 && (!DECL_ARTIFICIAL (decl)
3119 || is_capture_proxy (decl)
3120 || DECL_IMPLICIT_TYPEDEF_P (decl)
3121 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
3123 /* DECL shadows a local thing possibly of interest. */
3125 /* DR 2211: check that captures and parameters
3126 do not have the same name. */
3127 if (is_capture_proxy (decl))
3129 if (current_lambda_expr ()
3130 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
3131 && TREE_CODE (old) == PARM_DECL
3132 && DECL_NAME (decl) != this_identifier)
3134 error_at (DECL_SOURCE_LOCATION (old),
3135 "lambda parameter %qD "
3136 "previously declared as a capture", old);
3138 return;
3140 /* Don't complain if it's from an enclosing function. */
3141 else if (DECL_CONTEXT (old) == current_function_decl
3142 && TREE_CODE (decl) != PARM_DECL
3143 && TREE_CODE (old) == PARM_DECL)
3145 /* Go to where the parms should be and see if we find
3146 them there. */
3147 cp_binding_level *b = current_binding_level->level_chain;
3149 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
3150 /* Skip the ctor/dtor cleanup level. */
3151 b = b->level_chain;
3153 /* [basic.scope.param] A parameter name shall not be redeclared
3154 in the outermost block of the function definition. */
3155 if (b->kind == sk_function_parms)
3157 error_at (DECL_SOURCE_LOCATION (decl),
3158 "declaration of %q#D shadows a parameter", decl);
3159 inform (DECL_SOURCE_LOCATION (old),
3160 "%q#D previously declared here", old);
3161 return;
3165 /* The local structure or class can't use parameters of
3166 the containing function anyway. */
3167 if (DECL_CONTEXT (old) != current_function_decl)
3169 for (cp_binding_level *scope = current_binding_level;
3170 scope != old_scope; scope = scope->level_chain)
3171 if (scope->kind == sk_class
3172 && !LAMBDA_TYPE_P (scope->this_entity))
3173 return;
3175 /* Error if redeclaring a local declared in a
3176 init-statement or in the condition of an if or
3177 switch statement when the new declaration is in the
3178 outermost block of the controlled statement.
3179 Redeclaring a variable from a for or while condition is
3180 detected elsewhere. */
3181 else if (VAR_P (old)
3182 && old_scope == current_binding_level->level_chain
3183 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
3185 auto_diagnostic_group d;
3186 error_at (DECL_SOURCE_LOCATION (decl),
3187 "redeclaration of %q#D", decl);
3188 inform (DECL_SOURCE_LOCATION (old),
3189 "%q#D previously declared here", old);
3190 return;
3192 /* C++11:
3193 3.3.3/3: The name declared in an exception-declaration (...)
3194 shall not be redeclared in the outermost block of the handler.
3195 3.3.3/2: A parameter name shall not be redeclared (...) in
3196 the outermost block of any handler associated with a
3197 function-try-block.
3198 3.4.1/15: The function parameter names shall not be redeclared
3199 in the exception-declaration nor in the outermost block of a
3200 handler for the function-try-block. */
3201 else if ((TREE_CODE (old) == VAR_DECL
3202 && old_scope == current_binding_level->level_chain
3203 && old_scope->kind == sk_catch)
3204 || (TREE_CODE (old) == PARM_DECL
3205 && (current_binding_level->kind == sk_catch
3206 || current_binding_level->level_chain->kind == sk_catch)
3207 && in_function_try_handler))
3209 auto_diagnostic_group d;
3210 if (permerror (DECL_SOURCE_LOCATION (decl),
3211 "redeclaration of %q#D", decl))
3212 inform (DECL_SOURCE_LOCATION (old),
3213 "%q#D previously declared here", old);
3214 return;
3217 /* If '-Wshadow=compatible-local' is specified without other
3218 -Wshadow= flags, we will warn only when the type of the
3219 shadowing variable (DECL) can be converted to that of the
3220 shadowed parameter (OLD_LOCAL). The reason why we only check
3221 if DECL's type can be converted to OLD_LOCAL's type (but not the
3222 other way around) is because when users accidentally shadow a
3223 parameter, more than often they would use the variable
3224 thinking (mistakenly) it's still the parameter. It would be
3225 rare that users would use the variable in the place that
3226 expects the parameter but thinking it's a new decl.
3227 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3228 warns regardless of whether one of the types involved
3229 is a subclass of the other, since that is never okay. */
3231 enum opt_code warning_code;
3232 if (warn_shadow)
3233 warning_code = OPT_Wshadow;
3234 else if ((TREE_CODE (decl) == TYPE_DECL)
3235 ^ (TREE_CODE (old) == TYPE_DECL))
3236 /* If exactly one is a type, they aren't compatible. */
3237 warning_code = OPT_Wshadow_local;
3238 else if ((TREE_TYPE (old)
3239 && TREE_TYPE (decl)
3240 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3241 || TREE_CODE (decl) == TYPE_DECL
3242 || TREE_CODE (old) == TYPE_DECL
3243 || (!dependent_type_p (TREE_TYPE (decl))
3244 && !dependent_type_p (TREE_TYPE (old))
3245 /* If the new decl uses auto, we don't yet know
3246 its type (the old type cannot be using auto
3247 at this point, without also being
3248 dependent). This is an indication we're
3249 (now) doing the shadow checking too
3250 early. */
3251 && !type_uses_auto (TREE_TYPE (decl))
3252 && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
3253 decl, LOOKUP_IMPLICIT, tf_none)))
3254 warning_code = OPT_Wshadow_compatible_local;
3255 else
3256 warning_code = OPT_Wshadow_local;
3258 const char *msg;
3259 if (TREE_CODE (old) == PARM_DECL)
3260 msg = "declaration of %q#D shadows a parameter";
3261 else if (is_capture_proxy (old))
3262 msg = "declaration of %qD shadows a lambda capture";
3263 else
3264 msg = "declaration of %qD shadows a previous local";
3266 auto_diagnostic_group d;
3267 if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
3268 inform_shadowed (old);
3269 return;
3272 if (!warn_shadow)
3273 return;
3275 /* Don't warn for artificial things that are not implicit typedefs. */
3276 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
3277 return;
3279 if (nonlambda_method_basetype ())
3280 if (tree member = lookup_member (current_nonlambda_class_type (),
3281 DECL_NAME (decl), /*protect=*/0,
3282 /*want_type=*/false, tf_warning_or_error))
3284 member = MAYBE_BASELINK_FUNCTIONS (member);
3286 /* Warn if a variable shadows a non-function, or the variable
3287 is a function or a pointer-to-function. */
3288 if ((!OVL_P (member)
3289 || TREE_CODE (decl) == FUNCTION_DECL
3290 || (TREE_TYPE (decl)
3291 && (TYPE_PTRFN_P (TREE_TYPE (decl))
3292 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
3293 && !warning_suppressed_p (decl, OPT_Wshadow))
3295 auto_diagnostic_group d;
3296 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3297 "declaration of %qD shadows a member of %qT",
3298 decl, current_nonlambda_class_type ())
3299 && DECL_P (member))
3301 inform_shadowed (member);
3302 suppress_warning (decl, OPT_Wshadow);
3305 return;
3308 /* Now look for a namespace shadow. */
3309 old = find_namespace_value (current_namespace, DECL_NAME (decl));
3310 if (old
3311 && (VAR_P (old)
3312 || (TREE_CODE (old) == TYPE_DECL
3313 && (!DECL_ARTIFICIAL (old)
3314 || TREE_CODE (decl) == TYPE_DECL)))
3315 && !instantiating_current_function_p ()
3316 && !warning_suppressed_p (decl, OPT_Wshadow))
3317 /* XXX shadow warnings in outer-more namespaces */
3319 auto_diagnostic_group d;
3320 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3321 "declaration of %qD shadows a global declaration",
3322 decl))
3324 inform_shadowed (old);
3325 suppress_warning (decl, OPT_Wshadow);
3327 return;
3330 return;
3333 /* DECL is being pushed inside function CTX. Set its context, if
3334 needed. */
3336 static void
3337 set_decl_context_in_fn (tree ctx, tree decl)
3339 if (TREE_CODE (decl) == FUNCTION_DECL
3340 || (VAR_P (decl) && DECL_EXTERNAL (decl)))
3341 /* Make sure local externs are marked as such. OMP UDRs really
3342 are nested functions. */
3343 gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
3344 && (DECL_NAMESPACE_SCOPE_P (decl)
3345 || (TREE_CODE (decl) == FUNCTION_DECL
3346 && DECL_OMP_DECLARE_REDUCTION_P (decl))));
3348 if (!DECL_CONTEXT (decl)
3349 /* When parsing the parameter list of a function declarator,
3350 don't set DECL_CONTEXT to an enclosing function. */
3351 && !(TREE_CODE (decl) == PARM_DECL
3352 && parsing_function_declarator ()))
3353 DECL_CONTEXT (decl) = ctx;
3356 /* DECL is a local extern decl. Find or create the namespace-scope
3357 decl that it aliases. Also, determines the linkage of DECL. */
3359 void
3360 push_local_extern_decl_alias (tree decl)
3362 if (dependent_type_p (TREE_TYPE (decl))
3363 || (processing_template_decl
3364 && VAR_P (decl)
3365 && CP_DECL_THREAD_LOCAL_P (decl)))
3366 return;
3367 /* EH specs were not part of the function type prior to c++17, but
3368 we still can't go pushing dependent eh specs into the namespace. */
3369 if (cxx_dialect < cxx17
3370 && TREE_CODE (decl) == FUNCTION_DECL
3371 && (value_dependent_expression_p
3372 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
3373 return;
3375 gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
3376 || !DECL_TEMPLATE_INFO (decl));
3377 if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
3378 /* We're instantiating a non-dependent local decl, it already
3379 knows the alias. */
3380 return;
3382 tree alias = NULL_TREE;
3384 if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
3385 /* Do not let a VLA creep into a namespace. Diagnostic will be
3386 emitted in layout_var_decl later. */
3387 alias = error_mark_node;
3388 else
3390 /* First look for a decl that matches. */
3391 tree ns = CP_DECL_CONTEXT (decl);
3392 tree binding = find_namespace_value (ns, DECL_NAME (decl));
3394 if (binding && TREE_CODE (binding) != TREE_LIST)
3395 for (ovl_iterator iter (binding); iter; ++iter)
3396 if (decls_match (decl, *iter, /*record_versions*/false))
3398 alias = *iter;
3399 break;
3402 if (!alias)
3404 /* No existing namespace-scope decl. Make one. */
3405 alias = copy_decl (decl);
3406 if (TREE_CODE (alias) == FUNCTION_DECL)
3408 /* Recontextualize the parms. */
3409 for (tree *chain = &DECL_ARGUMENTS (alias);
3410 *chain; chain = &DECL_CHAIN (*chain))
3412 *chain = copy_decl (*chain);
3413 DECL_CONTEXT (*chain) = alias;
3416 tree type = TREE_TYPE (alias);
3417 for (tree args = TYPE_ARG_TYPES (type);
3418 args; args = TREE_CHAIN (args))
3419 if (TREE_PURPOSE (args))
3421 /* There are default args. Lose them. */
3422 tree nargs = NULL_TREE;
3423 tree *chain = &nargs;
3424 for (args = TYPE_ARG_TYPES (type);
3425 args; args = TREE_CHAIN (args))
3426 if (args == void_list_node)
3428 *chain = args;
3429 break;
3431 else
3433 *chain
3434 = build_tree_list (NULL_TREE, TREE_VALUE (args));
3435 chain = &TREE_CHAIN (*chain);
3438 tree fn_type = build_function_type (TREE_TYPE (type), nargs);
3440 fn_type = apply_memfn_quals
3441 (fn_type, type_memfn_quals (type));
3443 fn_type = build_cp_fntype_variant
3444 (fn_type, type_memfn_rqual (type),
3445 TYPE_RAISES_EXCEPTIONS (type),
3446 TYPE_HAS_LATE_RETURN_TYPE (type));
3448 TREE_TYPE (alias) = fn_type;
3449 break;
3453 /* This is the real thing. */
3454 DECL_LOCAL_DECL_P (alias) = false;
3456 /* Expected default linkage is from the namespace. */
3457 TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
3458 push_nested_namespace (ns);
3459 alias = pushdecl (alias, /* hiding= */true);
3460 pop_nested_namespace (ns);
3461 if (VAR_P (decl)
3462 && CP_DECL_THREAD_LOCAL_P (decl)
3463 && alias != error_mark_node)
3464 set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
3466 /* Adjust visibility. */
3467 determine_visibility (alias);
3471 retrofit_lang_decl (decl);
3472 DECL_LOCAL_DECL_ALIAS (decl) = alias;
3475 /* If DECL has non-internal linkage, and we have a module vector,
3476 record it in the appropriate slot. We have already checked for
3477 duplicates. */
3479 static void
3480 maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
3482 if (TREE_CODE (*slot) != BINDING_VECTOR)
3483 return;
3485 if (!TREE_PUBLIC (CP_DECL_CONTEXT (decl)))
3486 /* Member of internal namespace. */
3487 return;
3489 tree not_tmpl = STRIP_TEMPLATE (decl);
3490 if ((TREE_CODE (not_tmpl) == FUNCTION_DECL
3491 || VAR_P (not_tmpl))
3492 && DECL_THIS_STATIC (not_tmpl))
3493 /* Internal linkage. */
3494 return;
3496 bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
3497 && DECL_MODULE_ATTACH_P (not_tmpl));
3498 tree *gslot = get_fixed_binding_slot
3499 (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
3500 true);
3502 if (!is_attached)
3504 binding_slot &orig
3505 = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
3507 if (!STAT_HACK_P (tree (orig)))
3508 orig = stat_hack (tree (orig));
3510 MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
3513 add_mergeable_namespace_entity (gslot, decl);
3516 /* DECL is being pushed. Check whether it hides or ambiguates
3517 something seen as an import. This include decls seen in our own
3518 interface, which is OK. Also, check for merging a
3519 global/partition decl. */
3521 static tree
3522 check_module_override (tree decl, tree mvec, bool hiding,
3523 tree scope, tree name)
3525 tree match = NULL_TREE;
3526 bitmap imports = get_import_bitmap ();
3527 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
3528 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
3530 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
3532 cluster++;
3533 ix--;
3536 for (; ix--; cluster++)
3537 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
3539 /* Are we importing this module? */
3540 if (cluster->indices[jx].span != 1)
3541 continue;
3542 if (!cluster->indices[jx].base)
3543 continue;
3544 if (!bitmap_bit_p (imports, cluster->indices[jx].base))
3545 continue;
3546 /* Is it loaded? */
3547 if (cluster->slots[jx].is_lazy ())
3549 gcc_assert (cluster->indices[jx].span == 1);
3550 lazy_load_binding (cluster->indices[jx].base,
3551 scope, name, &cluster->slots[jx]);
3553 tree bind = cluster->slots[jx];
3554 if (!bind)
3555 /* Errors could cause there to be nothing. */
3556 continue;
3558 if (STAT_HACK_P (bind))
3559 /* We do not have to check STAT_TYPE here, the xref_tag
3560 machinery deals with that problem. */
3561 bind = STAT_VISIBLE (bind);
3563 for (ovl_iterator iter (bind); iter; ++iter)
3564 if (!iter.using_p ())
3566 match = duplicate_decls (decl, *iter, hiding);
3567 if (match)
3568 goto matched;
3572 if (TREE_PUBLIC (scope) && TREE_PUBLIC (STRIP_TEMPLATE (decl))
3573 /* Namespaces are dealt with specially in
3574 make_namespace_finish. */
3575 && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
3577 /* Look in the appropriate mergeable decl slot. */
3578 tree mergeable = NULL_TREE;
3579 if (named_module_p ())
3580 mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
3581 / BINDING_VECTOR_SLOTS_PER_CLUSTER)
3582 .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
3583 else
3584 mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
3586 for (ovl_iterator iter (mergeable); iter; ++iter)
3588 match = duplicate_decls (decl, *iter, hiding);
3589 if (match)
3590 goto matched;
3594 return NULL_TREE;
3596 matched:
3597 if (match != error_mark_node)
3599 if (named_module_p ())
3600 BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
3601 else
3602 BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
3605 return match;
3610 /* Record DECL as belonging to the current lexical scope. Check for
3611 errors (such as an incompatible declaration for the same name
3612 already seen in the same scope).
3614 The new binding is hidden if HIDING is true (an anticipated builtin
3615 or hidden friend).
3617 Returns either DECL or an old decl for the same name. If an old
3618 decl is returned, it may have been smashed to agree with what DECL
3619 says. */
3621 tree
3622 pushdecl (tree decl, bool hiding)
3624 auto_cond_timevar tv (TV_NAME_LOOKUP);
3626 if (decl == error_mark_node)
3627 return error_mark_node;
3629 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
3630 set_decl_context_in_fn (current_function_decl, decl);
3632 /* The binding level we will be pushing into. During local class
3633 pushing, we want to push to the containing scope. */
3634 cp_binding_level *level = current_binding_level;
3635 while (level->kind == sk_class
3636 || level->kind == sk_cleanup)
3637 level = level->level_chain;
3639 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3640 insert it. Other NULL-named decls, not so much. */
3641 tree name = DECL_NAME (decl);
3642 if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
3644 cxx_binding *binding = NULL; /* Local scope binding. */
3645 tree ns = NULL_TREE; /* Searched namespace. */
3646 tree *slot = NULL; /* Binding slot in namespace. */
3647 tree *mslot = NULL; /* Current module slot in namespace. */
3648 tree old = NULL_TREE;
3650 if (level->kind == sk_namespace)
3652 /* We look in the decl's namespace for an existing
3653 declaration, even though we push into the current
3654 namespace. */
3655 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3656 ? CP_DECL_CONTEXT (decl) : current_namespace);
3657 /* Create the binding, if this is current namespace, because
3658 that's where we'll be pushing anyway. */
3659 slot = find_namespace_slot (ns, name, ns == current_namespace);
3660 if (slot)
3662 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
3663 ns == current_namespace);
3664 old = MAYBE_STAT_DECL (*mslot);
3667 else
3669 binding = find_local_binding (level, name);
3670 if (binding)
3671 old = binding->value;
3674 if (old == error_mark_node)
3675 old = NULL_TREE;
3677 for (ovl_iterator iter (old); iter; ++iter)
3678 if (iter.using_p ())
3679 ; /* Ignore using decls here. */
3680 else if (iter.hidden_p ()
3681 && TREE_CODE (*iter) == FUNCTION_DECL
3682 && DECL_LANG_SPECIFIC (*iter)
3683 && DECL_MODULE_IMPORT_P (*iter))
3684 ; /* An undeclared builtin imported from elsewhere. */
3685 else if (tree match
3686 = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
3688 if (match == error_mark_node)
3690 else if (TREE_CODE (match) == TYPE_DECL)
3691 gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
3692 == (level->kind == sk_namespace
3693 ? NULL_TREE : TREE_TYPE (match)));
3694 else if (iter.hidden_p () && !hiding)
3696 /* Unhiding a previously hidden decl. */
3697 tree head = iter.reveal_node (old);
3698 if (head != old)
3700 gcc_checking_assert (ns);
3701 if (STAT_HACK_P (*slot))
3702 STAT_DECL (*slot) = head;
3703 else
3704 *slot = head;
3706 if (DECL_EXTERN_C_P (match))
3707 /* We need to check and register the decl now. */
3708 check_extern_c_conflict (match);
3710 else if (slot && !hiding
3711 && STAT_HACK_P (*slot) && STAT_DECL_HIDDEN_P (*slot))
3713 /* Unhide the non-function. */
3714 gcc_checking_assert (old == match);
3715 if (!STAT_TYPE (*slot))
3716 *slot = match;
3717 else
3718 STAT_DECL (*slot) = match;
3720 return match;
3723 /* Check for redeclaring an import. */
3724 if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
3725 if (tree match
3726 = check_module_override (decl, *slot, hiding, ns, name))
3728 if (match == error_mark_node)
3729 return match;
3731 /* We found a decl in an interface, push it into this
3732 binding. */
3733 decl = update_binding (NULL, binding, mslot, old,
3734 match, hiding);
3736 return decl;
3739 /* We are pushing a new decl. */
3741 /* Skip a hidden builtin we failed to match already. There can
3742 only be one. */
3743 if (old && anticipated_builtin_p (old))
3744 old = OVL_CHAIN (old);
3746 check_template_shadow (decl);
3748 if (DECL_DECLARES_FUNCTION_P (decl))
3750 check_default_args (decl);
3752 if (hiding)
3754 if (level->kind != sk_namespace)
3756 /* In a local class, a friend function declaration must
3757 find a matching decl in the innermost non-class scope.
3758 [class.friend/11] */
3759 error_at (DECL_SOURCE_LOCATION (decl),
3760 "friend declaration %qD in local class without "
3761 "prior local declaration", decl);
3762 /* Don't attempt to push it. */
3763 return error_mark_node;
3768 if (level->kind != sk_namespace)
3770 check_local_shadow (decl);
3772 if (TREE_CODE (decl) == NAMESPACE_DECL)
3773 /* A local namespace alias. */
3774 set_identifier_type_value_with_scope (name, NULL_TREE, level);
3776 if (!binding)
3777 binding = create_local_binding (level, name);
3779 else if (!slot)
3781 ns = current_namespace;
3782 slot = find_namespace_slot (ns, name, true);
3783 mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
3784 /* Update OLD to reflect the namespace we're going to be
3785 pushing into. */
3786 old = MAYBE_STAT_DECL (*mslot);
3789 old = update_binding (level, binding, mslot, old, decl, hiding);
3791 if (old != decl)
3792 /* An existing decl matched, use it. */
3793 decl = old;
3794 else
3796 if (TREE_CODE (decl) == TYPE_DECL)
3798 tree type = TREE_TYPE (decl);
3800 if (type != error_mark_node)
3802 if (TYPE_NAME (type) != decl)
3803 set_underlying_type (decl);
3805 set_identifier_type_value_with_scope (name, decl, level);
3807 if (level->kind != sk_namespace
3808 && !instantiating_current_function_p ())
3809 /* This is a locally defined typedef in a function that
3810 is not a template instantation, record it to implement
3811 -Wunused-local-typedefs. */
3812 record_locally_defined_typedef (decl);
3815 else if (VAR_OR_FUNCTION_DECL_P (decl))
3817 if (DECL_EXTERN_C_P (decl))
3818 check_extern_c_conflict (decl);
3820 if (!DECL_LOCAL_DECL_P (decl)
3821 && VAR_P (decl))
3822 maybe_register_incomplete_var (decl);
3824 if (DECL_LOCAL_DECL_P (decl)
3825 && NAMESPACE_SCOPE_P (decl))
3826 push_local_extern_decl_alias (decl);
3829 if (level->kind == sk_namespace
3830 && TREE_PUBLIC (level->this_entity)
3831 && module_p ())
3832 maybe_record_mergeable_decl (slot, name, decl);
3835 else
3836 add_decl_to_level (level, decl);
3838 return decl;
3841 /* A mergeable entity is being loaded into namespace NS slot NAME.
3842 Create and return the appropriate vector slot for that. Either a
3843 GMF slot or a module-specific one. */
3845 tree *
3846 mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
3848 tree *mslot = find_namespace_slot (ns, name, true);
3849 tree *vslot = get_fixed_binding_slot
3850 (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
3851 true);
3853 gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
3854 *vec = *mslot;
3856 return vslot;
3859 /* DECL is a new mergeable namespace-scope decl. Add it to the
3860 mergeable entities on GSLOT. */
3862 void
3863 add_mergeable_namespace_entity (tree *gslot, tree decl)
3865 *gslot = ovl_make (decl, *gslot);
3868 /* A mergeable entity of KLASS called NAME is being loaded. Return
3869 the set of things it could be. All such non-as_base classes have
3870 been given a member vec. */
3872 tree
3873 lookup_class_binding (tree klass, tree name)
3875 tree found = NULL_TREE;
3877 if (!COMPLETE_TYPE_P (klass))
3879 else if (TYPE_LANG_SPECIFIC (klass))
3881 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
3883 found = member_vec_binary_search (member_vec, name);
3884 if (!found)
3886 else if (STAT_HACK_P (found))
3887 /* Rearrange the stat hack so that we don't need to expose that
3888 internal detail. */
3889 found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
3890 else if (IDENTIFIER_CONV_OP_P (name))
3892 gcc_checking_assert (name == conv_op_identifier);
3893 found = OVL_CHAIN (found);
3896 else
3898 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
3899 || TYPE_PTRMEMFUNC_P (klass));
3900 found = fields_linear_search (klass, name, false);
3903 return found;
3906 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
3907 for all decls of the current module. When partitions are involved,
3908 decls might be mentioned more than once. Return the accumulation of
3909 CALLBACK results. */
3911 unsigned
3912 walk_module_binding (tree binding, bitmap partitions,
3913 bool (*callback) (tree decl, WMB_Flags, void *data),
3914 void *data)
3916 // FIXME: We don't quite deal with using decls naming stat hack
3917 // type. Also using decls exporting something from the same scope.
3918 tree current = binding;
3919 unsigned count = 0;
3921 if (TREE_CODE (binding) == BINDING_VECTOR)
3922 current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
3924 bool decl_hidden = false;
3925 if (tree type = MAYBE_STAT_TYPE (current))
3927 WMB_Flags flags = WMB_None;
3928 if (STAT_TYPE_HIDDEN_P (current))
3929 flags = WMB_Flags (flags | WMB_Hidden);
3930 count += callback (type, flags, data);
3931 decl_hidden = STAT_DECL_HIDDEN_P (current);
3934 for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
3936 if (iter.hidden_p ())
3937 decl_hidden = true;
3938 if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
3940 WMB_Flags flags = WMB_None;
3941 if (decl_hidden)
3942 flags = WMB_Flags (flags | WMB_Hidden);
3943 if (iter.using_p ())
3945 flags = WMB_Flags (flags | WMB_Using);
3946 if (iter.exporting_p ())
3947 flags = WMB_Flags (flags | WMB_Export);
3949 count += callback (*iter, flags, data);
3951 decl_hidden = false;
3954 if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
3956 /* Process partition slots. */
3957 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
3958 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
3959 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
3961 ix--;
3962 cluster++;
3965 bool maybe_dups = BINDING_VECTOR_PARTITION_DUPS_P (binding);
3967 for (; ix--; cluster++)
3968 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
3969 if (!cluster->slots[jx].is_lazy ())
3970 if (tree bind = cluster->slots[jx])
3972 if (TREE_CODE (bind) == NAMESPACE_DECL
3973 && !DECL_NAMESPACE_ALIAS (bind))
3975 if (unsigned base = cluster->indices[jx].base)
3976 if (unsigned span = cluster->indices[jx].span)
3978 if (bitmap_bit_p (partitions, base))
3979 goto found;
3980 while (++base, --span);
3981 /* Not a partition's namespace. */
3982 continue;
3983 found:
3985 WMB_Flags flags = WMB_None;
3986 if (maybe_dups)
3987 flags = WMB_Flags (flags | WMB_Dups);
3988 count += callback (bind, flags, data);
3990 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
3992 if (tree btype = STAT_TYPE (bind))
3994 WMB_Flags flags = WMB_None;
3995 if (maybe_dups)
3996 flags = WMB_Flags (flags | WMB_Dups);
3997 if (STAT_TYPE_HIDDEN_P (bind))
3998 flags = WMB_Flags (flags | WMB_Hidden);
4000 count += callback (btype, flags, data);
4002 bool hidden = STAT_DECL_HIDDEN_P (bind);
4003 for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
4004 iter; ++iter)
4006 if (iter.hidden_p ())
4007 hidden = true;
4008 gcc_checking_assert
4009 (!(hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
4011 WMB_Flags flags = WMB_None;
4012 if (maybe_dups)
4013 flags = WMB_Flags (flags | WMB_Dups);
4014 if (decl_hidden)
4015 flags = WMB_Flags (flags | WMB_Hidden);
4016 if (iter.using_p ())
4018 flags = WMB_Flags (flags | WMB_Using);
4019 if (iter.exporting_p ())
4020 flags = WMB_Flags (flags | WMB_Export);
4022 count += callback (*iter, flags, data);
4023 hidden = false;
4029 return count;
4032 /* Imported module MOD has a binding to NS::NAME, stored in section
4033 SNUM. */
4035 bool
4036 import_module_binding (tree ns, tree name, unsigned mod, unsigned snum)
4038 tree *slot = find_namespace_slot (ns, name, true);
4039 binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
4041 if (mslot->is_lazy () || *mslot)
4042 /* Oops, something was already there. */
4043 return false;
4045 mslot->set_lazy (snum);
4046 return true;
4049 /* An import of MODULE is binding NS::NAME. There should be no
4050 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4051 is a header_unit (-1) or part of the current module (+1). VALUE
4052 and TYPE are the value and type bindings. VISIBLE are the value
4053 bindings being exported. */
4055 bool
4056 set_module_binding (tree ns, tree name, unsigned mod, int mod_glob,
4057 tree value, tree type, tree visible)
4059 if (!value)
4060 /* Bogus BMIs could give rise to nothing to bind. */
4061 return false;
4063 gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
4064 || DECL_NAMESPACE_ALIAS (value));
4065 gcc_checking_assert (mod);
4067 tree *slot = find_namespace_slot (ns, name, true);
4068 binding_slot *mslot = search_imported_binding_slot (slot, mod);
4070 if (!mslot || !mslot->is_lazy ())
4071 /* Again, bogus BMI could give find to missing or already loaded slot. */
4072 return false;
4074 tree bind = value;
4075 if (type || visible != bind || mod_glob)
4077 bind = stat_hack (bind, type);
4078 STAT_VISIBLE (bind) = visible;
4079 if ((mod_glob > 0 && TREE_PUBLIC (ns))
4080 || (type && DECL_MODULE_EXPORT_P (type)))
4081 STAT_TYPE_VISIBLE_P (bind) = true;
4084 /* Note if this is this-module or global binding. */
4085 if (mod_glob > 0)
4086 MODULE_BINDING_PARTITION_P (bind) = true;
4087 else if (mod_glob < 0)
4088 MODULE_BINDING_GLOBAL_P (bind) = true;
4090 *mslot = bind;
4092 return true;
4095 void
4096 add_module_namespace_decl (tree ns, tree decl)
4098 gcc_assert (!DECL_CHAIN (decl));
4099 gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
4100 && DECL_LOCAL_DECL_P (decl)));
4101 if (CHECKING_P)
4102 /* Expensive already-there? check. */
4103 for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
4104 probe = DECL_CHAIN (probe))
4105 gcc_assert (decl != probe);
4107 add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
4109 if (VAR_P (decl))
4110 maybe_register_incomplete_var (decl);
4112 if (VAR_OR_FUNCTION_DECL_P (decl)
4113 && DECL_EXTERN_C_P (decl))
4114 check_extern_c_conflict (decl);
4117 /* Enter DECL into the symbol table, if that's appropriate. Returns
4118 DECL, or a modified version thereof. */
4120 tree
4121 maybe_push_decl (tree decl)
4123 tree type = TREE_TYPE (decl);
4125 /* Add this decl to the current binding level, but not if it comes
4126 from another scope, e.g. a static member variable. TEM may equal
4127 DECL or it may be a previous decl of the same name. */
4128 if (decl == error_mark_node
4129 || (TREE_CODE (decl) != PARM_DECL
4130 && DECL_CONTEXT (decl) != NULL_TREE
4131 /* Definitions of namespace members outside their namespace are
4132 possible. */
4133 && !DECL_NAMESPACE_SCOPE_P (decl))
4134 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4135 || type == unknown_type_node
4136 /* The declaration of a template specialization does not affect
4137 the functions available for overload resolution, so we do not
4138 call pushdecl. */
4139 || (TREE_CODE (decl) == FUNCTION_DECL
4140 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4141 return decl;
4142 else
4143 return pushdecl (decl);
4146 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4147 binding level. If IS_USING is true, DECL got here through a
4148 using-declaration. */
4150 static void
4151 push_local_binding (tree id, tree decl, bool is_using)
4153 /* Skip over any local classes. This makes sense if we call
4154 push_local_binding with a friend decl of a local class. */
4155 cp_binding_level *b = innermost_nonclass_level ();
4157 gcc_assert (b->kind != sk_namespace);
4158 if (find_local_binding (b, id))
4160 /* Supplement the existing binding. */
4161 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
4162 /* It didn't work. Something else must be bound at this
4163 level. Do not add DECL to the list of things to pop
4164 later. */
4165 return;
4167 else
4168 /* Create a new binding. */
4169 push_binding (id, decl, b);
4171 if (TREE_CODE (decl) == OVERLOAD || is_using)
4172 /* We must put the OVERLOAD or using into a TREE_LIST since we
4173 cannot use the decl's chain itself. */
4174 decl = build_tree_list (id, decl);
4176 /* And put DECL on the list of things declared by the current
4177 binding level. */
4178 add_decl_to_level (b, decl);
4182 /* true means unconditionally make a BLOCK for the next level pushed. */
4184 static bool keep_next_level_flag;
4186 static int binding_depth = 0;
4188 static void
4189 indent (int depth)
4191 int i;
4193 for (i = 0; i < depth * 2; i++)
4194 putc (' ', stderr);
4197 /* Return a string describing the kind of SCOPE we have. */
4198 static const char *
4199 cp_binding_level_descriptor (cp_binding_level *scope)
4201 /* The order of this table must match the "scope_kind"
4202 enumerators. */
4203 static const char* scope_kind_names[] = {
4204 "block-scope",
4205 "cleanup-scope",
4206 "try-scope",
4207 "catch-scope",
4208 "for-scope",
4209 "function-parameter-scope",
4210 "class-scope",
4211 "namespace-scope",
4212 "template-parameter-scope",
4213 "template-explicit-spec-scope"
4215 const scope_kind kind = scope->explicit_spec_p
4216 ? sk_template_spec : scope->kind;
4218 return scope_kind_names[kind];
4221 /* Output a debugging information about SCOPE when performing
4222 ACTION at LINE. */
4223 static void
4224 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
4226 const char *desc = cp_binding_level_descriptor (scope);
4227 if (scope->this_entity)
4228 verbatim ("%s %<%s(%E)%> %p %d", action, desc,
4229 scope->this_entity, (void *) scope, line);
4230 else
4231 verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
4234 /* A chain of binding_level structures awaiting reuse. */
4236 static GTY((deletable)) cp_binding_level *free_binding_level;
4238 /* Insert SCOPE as the innermost binding level. */
4240 void
4241 push_binding_level (cp_binding_level *scope)
4243 /* Add it to the front of currently active scopes stack. */
4244 scope->level_chain = current_binding_level;
4245 current_binding_level = scope;
4246 keep_next_level_flag = false;
4248 if (ENABLE_SCOPE_CHECKING)
4250 scope->binding_depth = binding_depth;
4251 indent (binding_depth);
4252 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4253 "push");
4254 binding_depth++;
4258 /* Create a new KIND scope and make it the top of the active scopes stack.
4259 ENTITY is the scope of the associated C++ entity (namespace, class,
4260 function, C++0x enumeration); it is NULL otherwise. */
4262 cp_binding_level *
4263 begin_scope (scope_kind kind, tree entity)
4265 cp_binding_level *scope;
4267 /* Reuse or create a struct for this binding level. */
4268 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
4270 scope = free_binding_level;
4271 free_binding_level = scope->level_chain;
4272 memset (scope, 0, sizeof (cp_binding_level));
4274 else
4275 scope = ggc_cleared_alloc<cp_binding_level> ();
4277 scope->this_entity = entity;
4278 scope->more_cleanups_ok = true;
4279 switch (kind)
4281 case sk_cleanup:
4282 scope->keep = true;
4283 break;
4285 case sk_template_spec:
4286 scope->explicit_spec_p = true;
4287 kind = sk_template_parms;
4288 /* Fall through. */
4289 case sk_template_parms:
4290 case sk_block:
4291 case sk_try:
4292 case sk_catch:
4293 case sk_for:
4294 case sk_cond:
4295 case sk_class:
4296 case sk_scoped_enum:
4297 case sk_transaction:
4298 case sk_omp:
4299 case sk_stmt_expr:
4300 scope->keep = keep_next_level_flag;
4301 break;
4303 case sk_function_parms:
4304 scope->keep = keep_next_level_flag;
4305 break;
4307 case sk_namespace:
4308 NAMESPACE_LEVEL (entity) = scope;
4309 break;
4311 default:
4312 /* Should not happen. */
4313 gcc_unreachable ();
4314 break;
4316 scope->kind = kind;
4318 push_binding_level (scope);
4320 return scope;
4323 /* We're about to leave current scope. Pop the top of the stack of
4324 currently active scopes. Return the enclosing scope, now active. */
4326 cp_binding_level *
4327 leave_scope (void)
4329 cp_binding_level *scope = current_binding_level;
4331 if (scope->kind == sk_namespace && class_binding_level)
4332 current_binding_level = class_binding_level;
4334 /* We cannot leave a scope, if there are none left. */
4335 if (NAMESPACE_LEVEL (global_namespace))
4336 gcc_assert (!global_scope_p (scope));
4338 if (ENABLE_SCOPE_CHECKING)
4340 indent (--binding_depth);
4341 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4342 "leave");
4345 /* Move one nesting level up. */
4346 current_binding_level = scope->level_chain;
4348 /* Namespace-scopes are left most probably temporarily, not
4349 completely; they can be reopened later, e.g. in namespace-extension
4350 or any name binding activity that requires us to resume a
4351 namespace. For classes, we cache some binding levels. For other
4352 scopes, we just make the structure available for reuse. */
4353 if (scope->kind != sk_namespace
4354 && scope != previous_class_level)
4356 scope->level_chain = free_binding_level;
4357 gcc_assert (!ENABLE_SCOPE_CHECKING
4358 || scope->binding_depth == binding_depth);
4359 free_binding_level = scope;
4362 if (scope->kind == sk_class)
4364 /* Reset DEFINING_CLASS_P to allow for reuse of a
4365 class-defining scope in a non-defining context. */
4366 scope->defining_class_p = 0;
4368 /* Find the innermost enclosing class scope, and reset
4369 CLASS_BINDING_LEVEL appropriately. */
4370 class_binding_level = NULL;
4371 for (scope = current_binding_level; scope; scope = scope->level_chain)
4372 if (scope->kind == sk_class)
4374 class_binding_level = scope;
4375 break;
4379 return current_binding_level;
4382 /* When we exit a toplevel class scope, we save its binding level so
4383 that we can restore it quickly. Here, we've entered some other
4384 class, so we must invalidate our cache. */
4386 void
4387 invalidate_class_lookup_cache (void)
4389 previous_class_level->level_chain = free_binding_level;
4390 free_binding_level = previous_class_level;
4391 previous_class_level = NULL;
4394 static void
4395 resume_scope (cp_binding_level* b)
4397 /* Resuming binding levels is meant only for namespaces,
4398 and those cannot nest into classes. */
4399 gcc_assert (!class_binding_level);
4400 /* Also, resuming a non-directly nested namespace is a no-no. */
4401 gcc_assert (b->level_chain == current_binding_level);
4402 current_binding_level = b;
4403 if (ENABLE_SCOPE_CHECKING)
4405 b->binding_depth = binding_depth;
4406 indent (binding_depth);
4407 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
4408 binding_depth++;
4412 /* Return the innermost binding level that is not for a class scope. */
4414 static cp_binding_level *
4415 innermost_nonclass_level (void)
4417 cp_binding_level *b;
4419 b = current_binding_level;
4420 while (b->kind == sk_class)
4421 b = b->level_chain;
4423 return b;
4426 /* We're defining an object of type TYPE. If it needs a cleanup, but
4427 we're not allowed to add any more objects with cleanups to the current
4428 scope, create a new binding level. */
4430 void
4431 maybe_push_cleanup_level (tree type)
4433 if (type != error_mark_node
4434 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4435 && current_binding_level->more_cleanups_ok == 0)
4437 begin_scope (sk_cleanup, NULL);
4438 current_binding_level->statement_list = push_stmt_list ();
4442 /* Return true if we are in the global binding level. */
4444 bool
4445 global_bindings_p (void)
4447 return global_scope_p (current_binding_level);
4450 /* True if we are currently in a toplevel binding level. This
4451 means either the global binding level or a namespace in a toplevel
4452 binding level. Since there are no non-toplevel namespace levels,
4453 this really means any namespace or template parameter level. We
4454 also include a class whose context is toplevel. */
4456 bool
4457 toplevel_bindings_p (void)
4459 cp_binding_level *b = innermost_nonclass_level ();
4461 return b->kind == sk_namespace || b->kind == sk_template_parms;
4464 /* True if this is a namespace scope, or if we are defining a class
4465 which is itself at namespace scope, or whose enclosing class is
4466 such a class, etc. */
4468 bool
4469 namespace_bindings_p (void)
4471 cp_binding_level *b = innermost_nonclass_level ();
4473 return b->kind == sk_namespace;
4476 /* True if the innermost non-class scope is a block scope. */
4478 bool
4479 local_bindings_p (void)
4481 cp_binding_level *b = innermost_nonclass_level ();
4482 return b->kind < sk_function_parms || b->kind == sk_omp;
4485 /* True if the current level needs to have a BLOCK made. */
4487 bool
4488 kept_level_p (void)
4490 return (current_binding_level->blocks != NULL_TREE
4491 || current_binding_level->keep
4492 || current_binding_level->kind == sk_cleanup
4493 || current_binding_level->names != NULL_TREE
4494 || current_binding_level->using_directives);
4497 /* Returns the kind of the innermost scope. */
4499 scope_kind
4500 innermost_scope_kind (void)
4502 return current_binding_level->kind;
4505 /* Returns true if this scope was created to store template parameters. */
4507 bool
4508 template_parm_scope_p (void)
4510 return innermost_scope_kind () == sk_template_parms;
4513 /* If KEEP is true, make a BLOCK node for the next binding level,
4514 unconditionally. Otherwise, use the normal logic to decide whether
4515 or not to create a BLOCK. */
4517 void
4518 keep_next_level (bool keep)
4520 keep_next_level_flag = keep;
4523 /* Return the list of declarations of the current local scope. */
4525 tree
4526 get_local_decls (void)
4528 gcc_assert (current_binding_level->kind != sk_namespace
4529 && current_binding_level->kind != sk_class);
4530 return current_binding_level->names;
4533 /* Return how many function prototypes we are currently nested inside. */
4536 function_parm_depth (void)
4538 int level = 0;
4539 cp_binding_level *b;
4541 for (b = current_binding_level;
4542 b->kind == sk_function_parms;
4543 b = b->level_chain)
4544 ++level;
4546 return level;
4549 /* For debugging. */
4550 static int no_print_functions = 0;
4551 static int no_print_builtins = 0;
4553 static void
4554 print_binding_level (cp_binding_level* lvl)
4556 tree t;
4557 int i = 0, len;
4558 if (lvl->this_entity)
4559 print_node_brief (stderr, "entity=", lvl->this_entity, 1);
4560 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
4561 if (lvl->more_cleanups_ok)
4562 fprintf (stderr, " more-cleanups-ok");
4563 if (lvl->have_cleanups)
4564 fprintf (stderr, " have-cleanups");
4565 fprintf (stderr, "\n");
4566 if (lvl->names)
4568 fprintf (stderr, " names:\t");
4569 /* We can probably fit 3 names to a line? */
4570 for (t = lvl->names; t; t = TREE_CHAIN (t))
4572 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
4573 continue;
4574 if (no_print_builtins
4575 && (TREE_CODE (t) == TYPE_DECL)
4576 && DECL_IS_UNDECLARED_BUILTIN (t))
4577 continue;
4579 /* Function decls tend to have longer names. */
4580 if (TREE_CODE (t) == FUNCTION_DECL)
4581 len = 3;
4582 else
4583 len = 2;
4584 i += len;
4585 if (i > 6)
4587 fprintf (stderr, "\n\t");
4588 i = len;
4590 print_node_brief (stderr, "", t, 0);
4591 if (t == error_mark_node)
4592 break;
4594 if (i)
4595 fprintf (stderr, "\n");
4597 if (vec_safe_length (lvl->class_shadowed))
4599 size_t i;
4600 cp_class_binding *b;
4601 fprintf (stderr, " class-shadowed:");
4602 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
4603 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
4604 fprintf (stderr, "\n");
4606 if (lvl->type_shadowed)
4608 fprintf (stderr, " type-shadowed:");
4609 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
4611 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
4613 fprintf (stderr, "\n");
4617 DEBUG_FUNCTION void
4618 debug (cp_binding_level &ref)
4620 print_binding_level (&ref);
4623 DEBUG_FUNCTION void
4624 debug (cp_binding_level *ptr)
4626 if (ptr)
4627 debug (*ptr);
4628 else
4629 fprintf (stderr, "<nil>\n");
4632 static void
4633 print_other_binding_stack (cp_binding_level *stack)
4635 cp_binding_level *level;
4636 for (level = stack; !global_scope_p (level); level = level->level_chain)
4638 fprintf (stderr, "binding level %p\n", (void *) level);
4639 print_binding_level (level);
4643 DEBUG_FUNCTION void
4644 print_binding_stack (void)
4646 cp_binding_level *b;
4647 fprintf (stderr, "current_binding_level=%p\n"
4648 "class_binding_level=%p\n"
4649 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4650 (void *) current_binding_level, (void *) class_binding_level,
4651 (void *) NAMESPACE_LEVEL (global_namespace));
4652 if (class_binding_level)
4654 for (b = class_binding_level; b; b = b->level_chain)
4655 if (b == current_binding_level)
4656 break;
4657 if (b)
4658 b = class_binding_level;
4659 else
4660 b = current_binding_level;
4662 else
4663 b = current_binding_level;
4664 print_other_binding_stack (b);
4665 fprintf (stderr, "global:\n");
4666 print_binding_level (NAMESPACE_LEVEL (global_namespace));
4669 /* Push a definition of struct, union or enum tag named ID. into
4670 binding_level B. DECL is a TYPE_DECL for the type. DECL has
4671 already been pushed into its binding level. This is bookkeeping to
4672 find it easily. */
4674 static void
4675 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
4677 if (b->kind == sk_namespace)
4678 /* At namespace scope we should not see an identifier type value. */
4679 gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
4680 /* We could be pushing a friend underneath a template
4681 parm (ill-formed). */
4682 || (TEMPLATE_PARM_P
4683 (TYPE_NAME (REAL_IDENTIFIER_TYPE_VALUE (id)))));
4684 else
4686 /* Push the current type value, so we can restore it later */
4687 tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
4688 b->type_shadowed = tree_cons (id, old, b->type_shadowed);
4689 tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
4690 TREE_TYPE (b->type_shadowed) = type;
4691 SET_IDENTIFIER_TYPE_VALUE (id, type);
4695 /* As set_identifier_type_value_with_scope, but using
4696 current_binding_level. */
4698 void
4699 set_identifier_type_value (tree id, tree decl)
4701 set_identifier_type_value_with_scope (id, decl, current_binding_level);
4704 /* Return the name for the constructor (or destructor) for the
4705 specified class. */
4707 tree
4708 constructor_name (tree type)
4710 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
4712 return decl ? DECL_NAME (decl) : NULL_TREE;
4715 /* Returns TRUE if NAME is the name for the constructor for TYPE,
4716 which must be a class type. */
4718 bool
4719 constructor_name_p (tree name, tree type)
4721 gcc_assert (MAYBE_CLASS_TYPE_P (type));
4723 /* These don't have names. */
4724 if (TREE_CODE (type) == DECLTYPE_TYPE
4725 || TREE_CODE (type) == TYPEOF_TYPE)
4726 return false;
4728 if (name && name == constructor_name (type))
4729 return true;
4731 return false;
4734 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4735 caller to set DECL_CONTEXT properly.
4737 Warning: For class and block-scope this must only be used when X
4738 will be the new innermost binding for its name, as we tack it onto
4739 the front of IDENTIFIER_BINDING without checking to see if the
4740 current IDENTIFIER_BINDING comes from a closer binding level than
4741 LEVEL.
4743 Warning: For namespace scope, this will look in LEVEL for an
4744 existing binding to match, but if not found will push the decl into
4745 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
4746 pop_nested_namespace if you really need to push it into a foreign
4747 namespace. */
4749 static tree
4750 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
4752 cp_binding_level *b;
4754 if (level->kind == sk_class)
4756 gcc_checking_assert (!hiding);
4757 b = class_binding_level;
4758 class_binding_level = level;
4759 pushdecl_class_level (x);
4760 class_binding_level = b;
4762 else
4764 tree function_decl = current_function_decl;
4765 if (level->kind == sk_namespace)
4766 current_function_decl = NULL_TREE;
4767 b = current_binding_level;
4768 current_binding_level = level;
4769 x = pushdecl (x, hiding);
4770 current_binding_level = b;
4771 current_function_decl = function_decl;
4773 return x;
4776 /* Inject X into the local scope just before the function parms. */
4778 tree
4779 pushdecl_outermost_localscope (tree x)
4781 cp_binding_level *b = NULL;
4782 auto_cond_timevar tv (TV_NAME_LOOKUP);
4784 /* Find the scope just inside the function parms. */
4785 for (cp_binding_level *n = current_binding_level;
4786 n->kind != sk_function_parms; n = b->level_chain)
4787 b = n;
4789 return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
4792 /* Process a local-scope or namespace-scope using declaration. LOOKUP
4793 is the result of qualified lookup (both value & type are
4794 significant). FN_SCOPE_P indicates if we're at function-scope (as
4795 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
4796 bindings, which are altered to reflect the newly brought in
4797 declarations. */
4799 static bool
4800 do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
4801 bool insert_p, tree *value_p, tree *type_p)
4803 tree value = *value_p;
4804 tree type = *type_p;
4805 bool failed = false;
4807 /* Shift the old and new bindings around so we're comparing class and
4808 enumeration names to each other. */
4809 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4811 type = value;
4812 value = NULL_TREE;
4815 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4817 lookup.type = lookup.value;
4818 lookup.value = NULL_TREE;
4821 /* Only process exporting if we're going to be inserting. */
4822 bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
4824 /* First do the value binding. */
4825 if (!lookup.value)
4826 /* Nothing (only implicit typedef found). */
4827 gcc_checking_assert (lookup.type);
4828 else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4830 for (lkp_iterator usings (lookup.value); usings; ++usings)
4832 tree new_fn = *usings;
4833 bool exporting = revealing_p && module_exporting_p ();
4834 if (exporting)
4836 /* If the using decl is exported, the things it refers
4837 to must also be exported (or not habve module attachment). */
4838 if (!DECL_MODULE_EXPORT_P (new_fn)
4839 && (DECL_LANG_SPECIFIC (new_fn)
4840 && DECL_MODULE_ATTACH_P (new_fn)))
4842 error ("%q#D does not have external linkage", new_fn);
4843 inform (DECL_SOURCE_LOCATION (new_fn),
4844 "%q#D declared here", new_fn);
4845 exporting = false;
4849 /* [namespace.udecl]
4851 If a function declaration in namespace scope or block
4852 scope has the same name and the same parameter types as a
4853 function introduced by a using declaration the program is
4854 ill-formed. */
4855 /* This seems overreaching, asking core -- why do we care
4856 about decls in the namespace that we cannot name (because
4857 they are not transitively imported. We just check the
4858 decls that are in this TU. */
4859 bool found = false;
4860 for (ovl_iterator old (value); !found && old; ++old)
4862 tree old_fn = *old;
4864 if (new_fn == old_fn)
4866 /* The function already exists in the current
4867 namespace. We will still want to insert it if
4868 it is revealing a not-revealed thing. */
4869 found = true;
4870 if (!revealing_p)
4872 else if (old.using_p ())
4874 if (exporting)
4875 /* Update in place. 'tis ok. */
4876 OVL_EXPORT_P (old.get_using ()) = true;
4879 else if (DECL_MODULE_EXPORT_P (new_fn))
4881 else
4883 value = old.remove_node (value);
4884 found = false;
4886 break;
4888 else if (old.using_p ())
4889 continue; /* This is a using decl. */
4890 else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
4891 continue; /* This is an anticipated builtin. */
4892 else if (!matching_fn_p (new_fn, old_fn))
4893 continue; /* Parameters do not match. */
4894 else if (decls_match (new_fn, old_fn))
4896 /* Extern "C" in different namespaces. */
4897 found = true;
4898 break;
4900 else
4902 diagnose_name_conflict (new_fn, old_fn);
4903 failed = true;
4904 found = true;
4905 break;
4909 if (!found && insert_p)
4910 /* Unlike the decl-pushing case we don't drop anticipated
4911 builtins here. They don't cause a problem, and we'd
4912 like to match them with a future declaration. */
4913 value = ovl_insert (new_fn, value, 1 + exporting);
4916 else if (value
4917 /* Ignore anticipated builtins. */
4918 && !anticipated_builtin_p (value)
4919 && (fn_scope_p || !decls_match (lookup.value, value)))
4921 diagnose_name_conflict (lookup.value, value);
4922 failed = true;
4924 else if (insert_p)
4925 // FIXME:what if we're newly exporting lookup.value
4926 value = lookup.value;
4928 /* Now the type binding. */
4929 if (lookup.type && lookup.type != type)
4931 // FIXME: What if we're exporting lookup.type?
4932 if (type && !decls_match (lookup.type, type))
4934 diagnose_name_conflict (lookup.type, type);
4935 failed = true;
4937 else if (insert_p)
4938 type = lookup.type;
4941 if (insert_p)
4943 /* If value is empty, shift any class or enumeration name back. */
4944 if (!value)
4946 value = type;
4947 type = NULL_TREE;
4949 *value_p = value;
4950 *type_p = type;
4953 return failed;
4956 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4957 Both are namespaces. */
4959 bool
4960 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4962 int depth = SCOPE_DEPTH (ancestor);
4964 if (!depth && !inline_only)
4965 /* The global namespace encloses everything. */
4966 return true;
4968 while (SCOPE_DEPTH (descendant) > depth
4969 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4970 descendant = CP_DECL_CONTEXT (descendant);
4972 return ancestor == descendant;
4975 /* Returns true if ROOT (a non-alias namespace, class, or function)
4976 encloses CHILD. CHILD may be either a class type or a namespace
4977 (maybe alias). */
4979 bool
4980 is_ancestor (tree root, tree child)
4982 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
4983 && !DECL_NAMESPACE_ALIAS (root))
4984 || TREE_CODE (root) == FUNCTION_DECL
4985 || CLASS_TYPE_P (root));
4986 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
4987 || CLASS_TYPE_P (child));
4989 /* The global namespace encloses everything. Early-out for the
4990 common case. */
4991 if (root == global_namespace)
4992 return true;
4994 /* Search CHILD until we reach namespace scope. */
4995 while (TREE_CODE (child) != NAMESPACE_DECL)
4997 /* If we've reached the ROOT, it encloses CHILD. */
4998 if (root == child)
4999 return true;
5001 /* Go out one level. */
5002 if (TYPE_P (child))
5003 child = TYPE_NAME (child);
5004 child = CP_DECL_CONTEXT (child);
5007 if (TREE_CODE (root) != NAMESPACE_DECL)
5008 /* Failed to meet the non-namespace we were looking for. */
5009 return false;
5011 if (tree alias = DECL_NAMESPACE_ALIAS (child))
5012 child = alias;
5014 return is_nested_namespace (root, child);
5017 /* Enter the class or namespace scope indicated by T suitable for name
5018 lookup. T can be arbitrary scope, not necessary nested inside the
5019 current scope. Returns a non-null scope to pop iff pop_scope
5020 should be called later to exit this scope. */
5022 tree
5023 push_scope (tree t)
5025 if (TREE_CODE (t) == NAMESPACE_DECL)
5026 push_decl_namespace (t);
5027 else if (CLASS_TYPE_P (t))
5029 if (!at_class_scope_p ()
5030 || !same_type_p (current_class_type, t))
5031 push_nested_class (t);
5032 else
5033 /* T is the same as the current scope. There is therefore no
5034 need to re-enter the scope. Since we are not actually
5035 pushing a new scope, our caller should not call
5036 pop_scope. */
5037 t = NULL_TREE;
5040 return t;
5043 /* Leave scope pushed by push_scope. */
5045 void
5046 pop_scope (tree t)
5048 if (t == NULL_TREE)
5049 return;
5050 if (TREE_CODE (t) == NAMESPACE_DECL)
5051 pop_decl_namespace ();
5052 else if CLASS_TYPE_P (t)
5053 pop_nested_class ();
5056 /* Subroutine of push_inner_scope. */
5058 static void
5059 push_inner_scope_r (tree outer, tree inner)
5061 tree prev;
5063 if (outer == inner
5064 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5065 return;
5067 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5068 if (outer != prev)
5069 push_inner_scope_r (outer, prev);
5070 if (TREE_CODE (inner) == NAMESPACE_DECL)
5072 cp_binding_level *save_template_parm = 0;
5073 /* Temporary take out template parameter scopes. They are saved
5074 in reversed order in save_template_parm. */
5075 while (current_binding_level->kind == sk_template_parms)
5077 cp_binding_level *b = current_binding_level;
5078 current_binding_level = b->level_chain;
5079 b->level_chain = save_template_parm;
5080 save_template_parm = b;
5083 resume_scope (NAMESPACE_LEVEL (inner));
5084 current_namespace = inner;
5086 /* Restore template parameter scopes. */
5087 while (save_template_parm)
5089 cp_binding_level *b = save_template_parm;
5090 save_template_parm = b->level_chain;
5091 b->level_chain = current_binding_level;
5092 current_binding_level = b;
5095 else
5096 pushclass (inner);
5099 /* Enter the scope INNER from current scope. INNER must be a scope
5100 nested inside current scope. This works with both name lookup and
5101 pushing name into scope. In case a template parameter scope is present,
5102 namespace is pushed under the template parameter scope according to
5103 name lookup rule in 14.6.1/6.
5105 Return the former current scope suitable for pop_inner_scope. */
5107 tree
5108 push_inner_scope (tree inner)
5110 tree outer = current_scope ();
5111 if (!outer)
5112 outer = current_namespace;
5114 push_inner_scope_r (outer, inner);
5115 return outer;
5118 /* Exit the current scope INNER back to scope OUTER. */
5120 void
5121 pop_inner_scope (tree outer, tree inner)
5123 if (outer == inner
5124 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5125 return;
5127 while (outer != inner)
5129 if (TREE_CODE (inner) == NAMESPACE_DECL)
5131 cp_binding_level *save_template_parm = 0;
5132 /* Temporary take out template parameter scopes. They are saved
5133 in reversed order in save_template_parm. */
5134 while (current_binding_level->kind == sk_template_parms)
5136 cp_binding_level *b = current_binding_level;
5137 current_binding_level = b->level_chain;
5138 b->level_chain = save_template_parm;
5139 save_template_parm = b;
5142 pop_namespace ();
5144 /* Restore template parameter scopes. */
5145 while (save_template_parm)
5147 cp_binding_level *b = save_template_parm;
5148 save_template_parm = b->level_chain;
5149 b->level_chain = current_binding_level;
5150 current_binding_level = b;
5153 else
5154 popclass ();
5156 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5160 /* Do a pushlevel for class declarations. */
5162 void
5163 pushlevel_class (void)
5165 class_binding_level = begin_scope (sk_class, current_class_type);
5168 /* ...and a poplevel for class declarations. */
5170 void
5171 poplevel_class (void)
5173 cp_binding_level *level = class_binding_level;
5174 cp_class_binding *cb;
5175 size_t i;
5176 tree shadowed;
5178 auto_cond_timevar tv (TV_NAME_LOOKUP);
5179 gcc_assert (level != 0);
5181 /* If we're leaving a toplevel class, cache its binding level. */
5182 if (current_class_depth == 1)
5183 previous_class_level = level;
5184 for (shadowed = level->type_shadowed;
5185 shadowed;
5186 shadowed = TREE_CHAIN (shadowed))
5187 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
5189 /* Remove the bindings for all of the class-level declarations. */
5190 if (level->class_shadowed)
5192 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
5194 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
5195 cxx_binding_free (cb->base);
5197 ggc_free (level->class_shadowed);
5198 level->class_shadowed = NULL;
5201 /* Now, pop out of the binding level which we created up in the
5202 `pushlevel_class' routine. */
5203 gcc_assert (current_binding_level == level);
5204 leave_scope ();
5207 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5208 appropriate. DECL is the value to which a name has just been
5209 bound. CLASS_TYPE is the class in which the lookup occurred. */
5211 static void
5212 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
5213 tree class_type)
5215 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
5217 tree context;
5219 if (is_overloaded_fn (decl))
5220 context = ovl_scope (decl);
5221 else
5223 gcc_assert (DECL_P (decl));
5224 context = context_for_name_lookup (decl);
5227 if (is_properly_derived_from (class_type, context))
5228 INHERITED_VALUE_BINDING_P (binding) = 1;
5229 else
5230 INHERITED_VALUE_BINDING_P (binding) = 0;
5232 else if (binding->value == decl)
5233 /* We only encounter a TREE_LIST when there is an ambiguity in the
5234 base classes. Such an ambiguity can be overridden by a
5235 definition in this class. */
5236 INHERITED_VALUE_BINDING_P (binding) = 1;
5237 else
5238 INHERITED_VALUE_BINDING_P (binding) = 0;
5241 /* Make the declaration of X appear in CLASS scope. */
5243 bool
5244 pushdecl_class_level (tree x)
5246 bool is_valid = true;
5248 /* Do nothing if we're adding to an outer lambda closure type,
5249 outer_binding will add it later if it's needed. */
5250 if (current_class_type != class_binding_level->this_entity)
5251 return true;
5253 auto_cond_timevar tv (TV_NAME_LOOKUP);
5254 /* Get the name of X. */
5255 tree name = OVL_NAME (x);
5257 if (name)
5259 is_valid = push_class_level_binding (name, x);
5260 if (TREE_CODE (x) == TYPE_DECL)
5261 set_identifier_type_value (name, x);
5263 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
5265 /* If X is an anonymous aggregate, all of its members are
5266 treated as if they were members of the class containing the
5267 aggregate, for naming purposes. */
5268 location_t save_location = input_location;
5269 tree anon = TREE_TYPE (x);
5270 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
5271 for (unsigned ix = member_vec->length (); ix--;)
5273 tree binding = (*member_vec)[ix];
5274 if (STAT_HACK_P (binding))
5276 if (!pushdecl_class_level (STAT_TYPE (binding)))
5277 is_valid = false;
5278 binding = STAT_DECL (binding);
5280 if (!pushdecl_class_level (binding))
5281 is_valid = false;
5283 else
5284 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
5285 if (TREE_CODE (f) == FIELD_DECL)
5287 input_location = DECL_SOURCE_LOCATION (f);
5288 if (!pushdecl_class_level (f))
5289 is_valid = false;
5291 input_location = save_location;
5293 return is_valid;
5296 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5297 scope. If the value returned is non-NULL, and the PREVIOUS field
5298 is not set, callers must set the PREVIOUS field explicitly. */
5300 static cxx_binding *
5301 get_class_binding (tree name, cp_binding_level *scope)
5303 tree class_type;
5304 tree type_binding;
5305 tree value_binding;
5306 cxx_binding *binding;
5308 class_type = scope->this_entity;
5310 /* Get the type binding. */
5311 type_binding = lookup_member (class_type, name,
5312 /*protect=*/2, /*want_type=*/true,
5313 tf_warning_or_error);
5314 /* Get the value binding. */
5315 value_binding = lookup_member (class_type, name,
5316 /*protect=*/2, /*want_type=*/false,
5317 tf_warning_or_error);
5319 /* If we found either a type binding or a value binding, create a
5320 new binding object. */
5321 if (type_binding || value_binding)
5323 binding = new_class_binding (name,
5324 value_binding,
5325 type_binding,
5326 scope);
5327 set_inherited_value_binding_p (binding, value_binding, class_type);
5329 else
5330 binding = NULL;
5332 return binding;
5335 /* Make the declaration(s) of X appear in CLASS scope under the name
5336 NAME. Returns true if the binding is valid. */
5338 bool
5339 push_class_level_binding (tree name, tree x)
5341 cxx_binding *binding;
5342 tree decl = x;
5343 bool ok;
5345 auto_cond_timevar tv (TV_NAME_LOOKUP);
5347 /* The class_binding_level will be NULL if x is a template
5348 parameter name in a member template. */
5349 if (!class_binding_level)
5350 return true;
5352 if (name == error_mark_node)
5353 return false;
5355 /* Can happen for an erroneous declaration (c++/60384). */
5356 if (!identifier_p (name))
5358 gcc_assert (errorcount || sorrycount);
5359 return false;
5362 /* Check for invalid member names. But don't worry about a default
5363 argument-scope lambda being pushed after the class is complete. */
5364 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
5365 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
5366 /* Check that we're pushing into the right binding level. */
5367 gcc_assert (current_class_type == class_binding_level->this_entity);
5369 /* We could have been passed a tree list if this is an ambiguous
5370 declaration. If so, pull the declaration out because
5371 check_template_shadow will not handle a TREE_LIST. */
5372 if (TREE_CODE (decl) == TREE_LIST
5373 && TREE_TYPE (decl) == error_mark_node)
5374 decl = TREE_VALUE (decl);
5376 if (!check_template_shadow (decl))
5377 return false;
5379 /* [class.mem]
5381 If T is the name of a class, then each of the following shall
5382 have a name different from T:
5384 -- every static data member of class T;
5386 -- every member of class T that is itself a type;
5388 -- every enumerator of every member of class T that is an
5389 enumerated type;
5391 -- every member of every anonymous union that is a member of
5392 class T.
5394 (Non-static data members were also forbidden to have the same
5395 name as T until TC1.) */
5396 if ((VAR_P (x)
5397 || TREE_CODE (x) == CONST_DECL
5398 || (TREE_CODE (x) == TYPE_DECL
5399 && !DECL_SELF_REFERENCE_P (x))
5400 /* A data member of an anonymous union. */
5401 || (TREE_CODE (x) == FIELD_DECL
5402 && DECL_CONTEXT (x) != current_class_type))
5403 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
5405 tree scope = context_for_name_lookup (x);
5406 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
5408 error_at (DECL_SOURCE_LOCATION (x),
5409 "%qD has the same name as the class in which it is "
5410 "declared", x);
5411 return false;
5415 /* Get the current binding for NAME in this class, if any. */
5416 binding = IDENTIFIER_BINDING (name);
5417 if (!binding || binding->scope != class_binding_level)
5419 binding = get_class_binding (name, class_binding_level);
5420 /* If a new binding was created, put it at the front of the
5421 IDENTIFIER_BINDING list. */
5422 if (binding)
5424 binding->previous = IDENTIFIER_BINDING (name);
5425 IDENTIFIER_BINDING (name) = binding;
5429 /* If there is already a binding, then we may need to update the
5430 current value. */
5431 if (binding && binding->value)
5433 tree bval = binding->value;
5434 tree old_decl = NULL_TREE;
5435 tree target_decl = strip_using_decl (decl);
5436 tree target_bval = strip_using_decl (bval);
5438 if (INHERITED_VALUE_BINDING_P (binding))
5440 /* If the old binding was from a base class, and was for a
5441 tag name, slide it over to make room for the new binding.
5442 The old binding is still visible if explicitly qualified
5443 with a class-key. */
5444 if (TREE_CODE (target_bval) == TYPE_DECL
5445 && DECL_ARTIFICIAL (target_bval)
5446 && !(TREE_CODE (target_decl) == TYPE_DECL
5447 && DECL_ARTIFICIAL (target_decl)))
5449 old_decl = binding->type;
5450 binding->type = bval;
5451 binding->value = NULL_TREE;
5452 INHERITED_VALUE_BINDING_P (binding) = 0;
5454 else
5456 old_decl = bval;
5457 /* Any inherited type declaration is hidden by the type
5458 declaration in the derived class. */
5459 if (TREE_CODE (target_decl) == TYPE_DECL
5460 && DECL_ARTIFICIAL (target_decl))
5461 binding->type = NULL_TREE;
5464 else if (TREE_CODE (decl) == USING_DECL
5465 && TREE_CODE (bval) == USING_DECL
5466 && same_type_p (USING_DECL_SCOPE (decl),
5467 USING_DECL_SCOPE (bval)))
5468 /* This is a using redeclaration that will be diagnosed later
5469 in supplement_binding */
5471 else if (TREE_CODE (decl) == USING_DECL
5472 && TREE_CODE (bval) == USING_DECL
5473 && DECL_DEPENDENT_P (decl)
5474 && DECL_DEPENDENT_P (bval))
5475 return true;
5476 else if (TREE_CODE (decl) == USING_DECL
5477 && DECL_DEPENDENT_P (decl)
5478 && OVL_P (target_bval))
5479 /* The new dependent using beats an old overload. */
5480 old_decl = bval;
5481 else if (TREE_CODE (bval) == USING_DECL
5482 && DECL_DEPENDENT_P (bval)
5483 && OVL_P (target_decl))
5484 /* The old dependent using beats a new overload. */
5485 return true;
5486 else if (OVL_P (target_decl)
5487 && OVL_P (target_bval))
5488 /* The new overload set contains the old one. */
5489 old_decl = bval;
5491 if (old_decl && binding->scope == class_binding_level)
5493 binding->value = x;
5494 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5495 here. This function is only used to register bindings
5496 from with the class definition itself. */
5497 INHERITED_VALUE_BINDING_P (binding) = 0;
5498 return true;
5502 /* Note that we declared this value so that we can issue an error if
5503 this is an invalid redeclaration of a name already used for some
5504 other purpose. */
5505 note_name_declared_in_class (name, decl);
5507 /* If we didn't replace an existing binding, put the binding on the
5508 stack of bindings for the identifier, and update the shadowed
5509 list. */
5510 if (binding && binding->scope == class_binding_level)
5511 /* Supplement the existing binding. */
5512 ok = supplement_binding (binding, decl);
5513 else
5515 /* Create a new binding. */
5516 push_binding (name, decl, class_binding_level);
5517 ok = true;
5520 return ok;
5523 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5524 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5525 failure. */
5527 static tree
5528 lookup_using_decl (tree scope, name_lookup &lookup)
5530 tree current = current_scope ();
5531 bool dependent_p = false;
5532 tree binfo = NULL_TREE;
5533 base_kind b_kind = bk_not_base;
5535 /* Because C++20 breaks the invariant that only member using-decls
5536 refer to members and only non-member using-decls refer to
5537 non-members, we first do the lookups, and then do validation that
5538 what we found is ok. */
5540 if (TREE_CODE (scope) == ENUMERAL_TYPE
5541 && cxx_dialect < cxx20
5542 && UNSCOPED_ENUM_P (scope)
5543 && !TYPE_FUNCTION_SCOPE_P (scope))
5545 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5546 should allow it as meaning the enclosing scope. I don't see any
5547 justification for this in C++11, but let's keep allowing it. */
5548 tree ctx = CP_TYPE_CONTEXT (scope);
5549 if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
5550 scope = ctx;
5553 /* You cannot using-decl a destructor. */
5554 if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
5556 error ("%<%T%s%D%> names destructor", scope,
5557 &"::"[scope == global_namespace ? 2 : 0], lookup.name);
5558 return NULL_TREE;
5561 if (TREE_CODE (scope) == NAMESPACE_DECL)
5563 /* Naming a namespace member. */
5564 qualified_namespace_lookup (scope, &lookup);
5566 if (TYPE_P (current)
5567 && (!lookup.value
5568 || lookup.type
5569 || cxx_dialect < cxx20
5570 || TREE_CODE (lookup.value) != CONST_DECL))
5572 error ("using-declaration for non-member at class scope");
5573 return NULL_TREE;
5576 else if (TREE_CODE (scope) == ENUMERAL_TYPE)
5578 /* Naming an enumeration member. */
5579 if (cxx_dialect < cxx20)
5580 error ("%<using%> with enumeration scope %q#T "
5581 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5582 scope);
5583 lookup.value = lookup_enumerator (scope, lookup.name);
5585 else
5587 /* Naming a class member. This is awkward in C++20, because we
5588 might be naming an enumerator of an unrelated class. */
5590 tree npscope = scope;
5591 if (PACK_EXPANSION_P (scope))
5592 npscope = PACK_EXPANSION_PATTERN (scope);
5594 if (!MAYBE_CLASS_TYPE_P (npscope))
5596 error ("%qT is not a class, namespace, or enumeration", npscope);
5597 return NULL_TREE;
5600 /* Using T::T declares inheriting ctors, even if T is a typedef. */
5601 if (lookup.name == TYPE_IDENTIFIER (npscope)
5602 || constructor_name_p (lookup.name, npscope))
5604 if (!TYPE_P (current))
5606 error ("non-member using-declaration names constructor of %qT",
5607 npscope);
5608 return NULL_TREE;
5610 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
5611 lookup.name = ctor_identifier;
5612 CLASSTYPE_NON_AGGREGATE (current) = true;
5615 if (!TYPE_P (current) && cxx_dialect < cxx20)
5617 error ("using-declaration for member at non-class scope");
5618 return NULL_TREE;
5621 bool depscope = dependent_scope_p (scope);
5623 if (depscope)
5624 /* Leave binfo null. */;
5625 else if (TYPE_P (current))
5627 binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
5628 gcc_checking_assert (b_kind >= bk_not_base);
5630 if (b_kind == bk_not_base && any_dependent_bases_p ())
5631 /* Treat as-if dependent. */
5632 depscope = true;
5633 else if (lookup.name == ctor_identifier
5634 && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
5636 if (any_dependent_bases_p ())
5637 depscope = true;
5638 else
5640 error ("%qT is not a direct base of %qT", scope, current);
5641 return NULL_TREE;
5645 if (b_kind < bk_proper_base)
5646 binfo = TYPE_BINFO (scope);
5648 else
5649 binfo = TYPE_BINFO (scope);
5651 dependent_p = (depscope
5652 || (IDENTIFIER_CONV_OP_P (lookup.name)
5653 && dependent_type_p (TREE_TYPE (lookup.name))));
5655 if (!dependent_p)
5656 lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
5657 /*want_type=*/false, tf_none);
5659 /* If the lookup in the base contains a dependent using, this
5660 using is also dependent. */
5661 if (!dependent_p && lookup.value && dependent_type_p (scope))
5663 tree val = lookup.value;
5664 if (tree fns = maybe_get_fns (val))
5665 val = fns;
5666 for (tree f: lkp_range (val))
5667 if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
5669 dependent_p = true;
5670 break;
5674 if (!depscope && b_kind < bk_proper_base)
5676 if (cxx_dialect >= cxx20 && lookup.value
5677 && TREE_CODE (lookup.value) == CONST_DECL)
5679 /* Using an unrelated enum; check access here rather
5680 than separately for class and non-class using. */
5681 perform_or_defer_access_check
5682 (binfo, lookup.value, lookup.value, tf_warning_or_error);
5683 /* And then if this is a copy from handle_using_decl, look
5684 through to the original enumerator. */
5685 if (CONST_DECL_USING_P (lookup.value))
5686 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
5688 else if (!TYPE_P (current))
5690 error ("using-declaration for member at non-class scope");
5691 return NULL_TREE;
5693 else
5695 auto_diagnostic_group g;
5696 error_not_base_type (scope, current);
5697 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
5698 && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
5699 inform (input_location,
5700 "did you mean %<using enum %T::%D%>?",
5701 scope, lookup.name);
5702 return NULL_TREE;
5707 /* Did we find anything sane? */
5708 if (dependent_p)
5710 else if (!lookup.value)
5712 error ("%qD has not been declared in %qD", lookup.name, scope);
5713 return NULL_TREE;
5715 else if (TREE_CODE (lookup.value) == TREE_LIST
5716 /* We can (independently) have ambiguous implicit typedefs. */
5717 || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
5719 error ("reference to %qD is ambiguous", lookup.name);
5720 print_candidates (TREE_CODE (lookup.value) == TREE_LIST
5721 ? lookup.value : lookup.type);
5722 return NULL_TREE;
5724 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
5726 error ("using-declaration may not name namespace %qD", lookup.value);
5727 return NULL_TREE;
5730 if (TYPE_P (current))
5732 /* In class scope. */
5734 /* Cannot introduce a constructor name. */
5735 if (constructor_name_p (lookup.name, current))
5737 error ("%<%T::%D%> names constructor in %qT",
5738 scope, lookup.name, current);
5739 return NULL_TREE;
5742 if (lookup.value && BASELINK_P (lookup.value))
5743 /* The binfo from which the functions came does not matter. */
5744 lookup.value = BASELINK_FUNCTIONS (lookup.value);
5747 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5748 USING_DECL_SCOPE (using_decl) = scope;
5749 USING_DECL_DECLS (using_decl) = lookup.value;
5750 DECL_DEPENDENT_P (using_decl) = dependent_p;
5751 DECL_CONTEXT (using_decl) = current;
5752 if (TYPE_P (current) && b_kind == bk_not_base)
5753 USING_DECL_UNRELATED_P (using_decl) = true;
5755 return using_decl;
5758 /* Process "using SCOPE::NAME" in a class scope. Return the
5759 USING_DECL created. */
5761 tree
5762 do_class_using_decl (tree scope, tree name)
5764 if (name == error_mark_node
5765 || scope == error_mark_node)
5766 return NULL_TREE;
5768 name_lookup lookup (name);
5769 return lookup_using_decl (scope, lookup);
5773 /* Return the binding for NAME in NS in the current TU. If NS is
5774 NULL, look in global_namespace. We will not find declarations
5775 from imports. Users of this who, having found nothing, push a new
5776 decl must be prepared for that pushing to match an existing decl. */
5778 tree
5779 get_namespace_binding (tree ns, tree name)
5781 auto_cond_timevar tv (TV_NAME_LOOKUP);
5782 if (!ns)
5783 ns = global_namespace;
5784 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
5785 tree ret = NULL_TREE;
5787 if (tree *b = find_namespace_slot (ns, name))
5789 ret = *b;
5791 if (TREE_CODE (ret) == BINDING_VECTOR)
5792 ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
5793 if (ret)
5794 ret = MAYBE_STAT_DECL (ret);
5797 return ret;
5800 /* Push internal DECL into the global namespace. Does not do the
5801 full overload fn handling and does not add it to the list of things
5802 in the namespace. */
5804 void
5805 set_global_binding (tree decl)
5807 auto_cond_timevar tv (TV_NAME_LOOKUP);
5809 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
5811 if (*slot)
5812 /* The user's placed something in the implementor's namespace. */
5813 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
5815 /* Force the binding, so compiler internals continue to work. */
5816 *slot = decl;
5819 /* Set the context of a declaration to scope. Complain if we are not
5820 outside scope. */
5822 void
5823 set_decl_namespace (tree decl, tree scope, bool friendp)
5825 /* Get rid of namespace aliases. */
5826 scope = ORIGINAL_NAMESPACE (scope);
5828 /* It is ok for friends to be qualified in parallel space. */
5829 if (!friendp && !is_nested_namespace (current_namespace, scope))
5830 error ("declaration of %qD not in a namespace surrounding %qD",
5831 decl, scope);
5832 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5834 /* See whether this has been declared in the namespace or inline
5835 children. */
5836 tree old = NULL_TREE;
5838 name_lookup lookup (DECL_NAME (decl),
5839 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
5840 if (!lookup.search_qualified (scope, /*usings=*/false))
5841 /* No old declaration at all. */
5842 goto not_found;
5843 old = lookup.value;
5846 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
5847 if (TREE_CODE (old) == TREE_LIST)
5849 ambiguous:
5850 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5851 error ("reference to %qD is ambiguous", decl);
5852 print_candidates (old);
5853 return;
5856 if (!DECL_DECLARES_FUNCTION_P (decl))
5858 /* Don't compare non-function decls with decls_match here, since
5859 it can't check for the correct constness at this
5860 point. pushdecl will find those errors later. */
5862 /* We might have found it in an inline namespace child of SCOPE. */
5863 if (TREE_CODE (decl) == TREE_CODE (old))
5864 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
5866 found:
5867 /* Writing "N::i" to declare something directly in "N" is invalid. */
5868 if (CP_DECL_CONTEXT (decl) == current_namespace
5869 && at_namespace_scope_p ())
5870 error_at (DECL_SOURCE_LOCATION (decl),
5871 "explicit qualification in declaration of %qD", decl);
5872 return;
5875 /* Since decl is a function, old should contain a function decl. */
5876 if (!OVL_P (old))
5878 not_found:
5879 /* It didn't work, go back to the explicit scope. */
5880 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5881 error ("%qD should have been declared inside %qD", decl, scope);
5883 return;
5886 /* We handle these in check_explicit_instantiation_namespace. */
5887 if (processing_explicit_instantiation)
5888 return;
5889 if (processing_template_decl || processing_specialization)
5890 /* We have not yet called push_template_decl to turn a
5891 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
5892 match. But, we'll check later, when we construct the
5893 template. */
5894 return;
5896 /* Instantiations or specializations of templates may be declared as
5897 friends in any namespace. */
5898 if (friendp && DECL_USE_TEMPLATE (decl))
5899 return;
5901 tree found = NULL_TREE;
5902 bool hidden_p = false;
5903 bool saw_template = false;
5905 for (lkp_iterator iter (old); iter; ++iter)
5907 if (iter.using_p ())
5908 continue;
5910 tree ofn = *iter;
5912 /* Adjust DECL_CONTEXT first so decls_match will return true
5913 if DECL will match a declaration in an inline namespace. */
5914 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
5915 if (decls_match (decl, ofn))
5917 if (found)
5919 /* We found more than one matching declaration. This
5920 can happen if we have two inline namespace children,
5921 each containing a suitable declaration. */
5922 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5923 goto ambiguous;
5925 found = ofn;
5926 hidden_p = iter.hidden_p ();
5928 else if (TREE_CODE (decl) == FUNCTION_DECL
5929 && TREE_CODE (ofn) == TEMPLATE_DECL)
5930 saw_template = true;
5933 if (!found && friendp && saw_template)
5935 /* "[if no non-template match is found,] each remaining function template
5936 is replaced with the specialization chosen by deduction from the
5937 friend declaration or discarded if deduction fails."
5939 So tell check_explicit_specialization to look for a match. */
5940 SET_DECL_IMPLICIT_INSTANTIATION (decl);
5941 DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
5942 return;
5945 if (found)
5947 if (hidden_p)
5949 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
5950 "%qD has not been declared within %qD", decl, scope);
5951 inform (DECL_SOURCE_LOCATION (found),
5952 "only here as a %<friend%>");
5954 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
5955 goto found;
5958 goto not_found;
5961 /* Return the namespace where the current declaration is declared. */
5963 tree
5964 current_decl_namespace (void)
5966 tree result;
5967 /* If we have been pushed into a different namespace, use it. */
5968 if (!vec_safe_is_empty (decl_namespace_list))
5969 return decl_namespace_list->last ();
5971 if (current_class_type)
5972 result = decl_namespace_context (current_class_type);
5973 else if (current_function_decl)
5974 result = decl_namespace_context (current_function_decl);
5975 else
5976 result = current_namespace;
5977 return result;
5980 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5981 attribute visibility is seen. */
5983 bool
5984 handle_namespace_attrs (tree ns, tree attributes)
5986 tree d;
5987 bool saw_vis = false;
5989 if (attributes == error_mark_node)
5990 return false;
5992 for (d = attributes; d; d = TREE_CHAIN (d))
5994 tree name = get_attribute_name (d);
5995 tree args = TREE_VALUE (d);
5997 if (is_attribute_p ("visibility", name))
5999 /* attribute visibility is a property of the syntactic block
6000 rather than the namespace as a whole, so we don't touch the
6001 NAMESPACE_DECL at all. */
6002 tree x = args ? TREE_VALUE (args) : NULL_TREE;
6003 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
6005 warning (OPT_Wattributes,
6006 "%qD attribute requires a single NTBS argument",
6007 name);
6008 continue;
6011 if (!TREE_PUBLIC (ns))
6012 warning (OPT_Wattributes,
6013 "%qD attribute is meaningless since members of the "
6014 "anonymous namespace get local symbols", name);
6016 push_visibility (TREE_STRING_POINTER (x), 1);
6017 saw_vis = true;
6019 else if (is_attribute_p ("abi_tag", name))
6021 if (!DECL_NAME (ns))
6023 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6024 "namespace", name);
6025 continue;
6027 if (!DECL_NAMESPACE_INLINE_P (ns))
6029 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
6030 "namespace", name);
6031 continue;
6033 if (!args)
6035 tree dn = DECL_NAME (ns);
6036 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
6037 IDENTIFIER_POINTER (dn));
6038 TREE_TYPE (args) = char_array_type_node;
6039 args = fix_string_type (args);
6040 args = build_tree_list (NULL_TREE, args);
6042 if (check_abi_tag_args (args, name))
6043 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6044 DECL_ATTRIBUTES (ns));
6046 else if (is_attribute_p ("deprecated", name))
6048 if (!DECL_NAME (ns))
6050 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6051 "namespace", name);
6052 continue;
6054 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6056 error ("deprecated message is not a string");
6057 continue;
6059 TREE_DEPRECATED (ns) = 1;
6060 if (args)
6061 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6062 DECL_ATTRIBUTES (ns));
6064 else
6066 warning (OPT_Wattributes, "%qD attribute directive ignored",
6067 name);
6068 continue;
6072 return saw_vis;
6075 /* Temporarily set the namespace for the current declaration. */
6077 void
6078 push_decl_namespace (tree decl)
6080 if (TREE_CODE (decl) != NAMESPACE_DECL)
6081 decl = decl_namespace_context (decl);
6082 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
6085 /* [namespace.memdef]/2 */
6087 void
6088 pop_decl_namespace (void)
6090 decl_namespace_list->pop ();
6093 /* Process a namespace-alias declaration. */
6095 void
6096 do_namespace_alias (tree alias, tree name_space)
6098 if (name_space == error_mark_node)
6099 return;
6101 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
6103 name_space = ORIGINAL_NAMESPACE (name_space);
6105 /* Build the alias. */
6106 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
6107 DECL_NAMESPACE_ALIAS (alias) = name_space;
6108 DECL_EXTERNAL (alias) = 1;
6109 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
6110 set_originating_module (alias);
6112 pushdecl (alias);
6114 /* Emit debug info for namespace alias. */
6115 if (!building_stmt_list_p ())
6116 (*debug_hooks->early_global_decl) (alias);
6119 /* Like pushdecl, only it places DECL in the current namespace,
6120 if appropriate. */
6122 tree
6123 pushdecl_namespace_level (tree decl, bool hiding)
6125 auto_cond_timevar tv (TV_NAME_LOOKUP);
6126 return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
6127 hiding);
6130 /* Wrapper around push_local_binding to push the bindings for
6131 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6132 is the result of name lookup during template parsing. */
6134 static void
6135 push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
6137 tree type = NULL_TREE;
6139 cxx_binding *binding = find_local_binding (current_binding_level, name);
6140 if (binding)
6142 value = binding->value;
6143 type = binding->type;
6146 /* DR 36 questions why using-decls at function scope may not be
6147 duplicates. Disallow it, as C++11 claimed and PR 20420
6148 implemented. */
6149 if (lookup)
6150 do_nonmember_using_decl (*lookup, true, true, &value, &type);
6152 if (!value)
6154 else if (binding && value == binding->value)
6155 /* Redeclaration of this USING_DECL. */;
6156 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
6158 /* We already have this binding, so replace it. */
6159 update_local_overload (IDENTIFIER_BINDING (name), value);
6160 IDENTIFIER_BINDING (name)->value = value;
6162 else
6163 /* Install the new binding. */
6164 push_local_binding (name, value, /*using=*/true);
6166 if (!type)
6168 else if (binding && type == binding->type)
6170 else
6172 push_local_binding (name, type, /*using=*/true);
6173 set_identifier_type_value (name, type);
6177 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6179 void
6180 push_using_decl_bindings (tree name, tree value)
6182 push_using_decl_bindings (nullptr, name, value);
6185 /* Process a using declaration in non-class scope. */
6187 void
6188 finish_nonmember_using_decl (tree scope, tree name)
6190 gcc_checking_assert (current_binding_level->kind != sk_class);
6192 if (scope == error_mark_node || name == error_mark_node)
6193 return;
6195 name_lookup lookup (name);
6197 tree using_decl = lookup_using_decl (scope, lookup);
6198 if (!using_decl)
6199 return;
6201 /* Emit debug info. */
6202 if (!processing_template_decl)
6203 cp_emit_debug_info_for_using (lookup.value,
6204 current_binding_level->this_entity);
6206 if (current_binding_level->kind == sk_namespace)
6208 tree *slot = find_namespace_slot (current_namespace, name, true);
6209 tree *mslot = get_fixed_binding_slot (slot, name,
6210 BINDING_SLOT_CURRENT, true);
6211 bool failed = false;
6213 if (mslot != slot)
6215 /* A module vector. I presume the binding list is going to
6216 be sparser than the import bitmap. Hence iterate over
6217 the former checking for bits set in the bitmap. */
6218 bitmap imports = get_import_bitmap ();
6219 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
6221 /* Scan the imported bindings. */
6222 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
6223 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
6225 ix--;
6226 cluster++;
6229 /* Do this in forward order, so we load modules in an order
6230 the user expects. */
6231 for (; ix--; cluster++)
6232 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
6234 /* Are we importing this module? */
6235 if (unsigned base = cluster->indices[jx].base)
6236 if (unsigned span = cluster->indices[jx].span)
6238 if (bitmap_bit_p (imports, base))
6239 goto found;
6240 while (++base, --span);
6241 continue;
6243 found:;
6244 /* Is it loaded? */
6245 if (cluster->slots[jx].is_lazy ())
6247 gcc_assert (cluster->indices[jx].span == 1);
6248 lazy_load_binding (cluster->indices[jx].base,
6249 scope, name, &cluster->slots[jx]);
6252 tree value = cluster->slots[jx];
6253 if (!value)
6254 /* Load errors could mean there's nothing here. */
6255 continue;
6257 /* Extract what we can see from here. If there's no
6258 stat_hack, then everything was exported. */
6259 tree type = NULL_TREE;
6261 /* If no stat hack, everything is visible. */
6262 if (STAT_HACK_P (value))
6264 if (STAT_TYPE_VISIBLE_P (value))
6265 type = STAT_TYPE (value);
6266 value = STAT_VISIBLE (value);
6269 if (do_nonmember_using_decl (lookup, false, false,
6270 &value, &type))
6272 failed = true;
6273 break;
6278 if (!failed)
6280 /* Now do the current slot. */
6281 tree value = MAYBE_STAT_DECL (*mslot);
6282 tree type = MAYBE_STAT_TYPE (*mslot);
6284 do_nonmember_using_decl (lookup, false, true, &value, &type);
6286 // FIXME: Partition mergeableness?
6287 if (STAT_HACK_P (*mslot))
6289 STAT_DECL (*mslot) = value;
6290 STAT_TYPE (*mslot) = type;
6292 else if (type)
6293 *mslot = stat_hack (value, type);
6294 else
6295 *mslot = value;
6298 else
6300 add_decl_expr (using_decl);
6301 if (DECL_DEPENDENT_P (using_decl))
6302 lookup.value = using_decl;
6303 push_using_decl_bindings (&lookup, name, NULL_TREE);
6307 /* Return the declarations that are members of the namespace NS. */
6309 tree
6310 cp_namespace_decls (tree ns)
6312 return NAMESPACE_LEVEL (ns)->names;
6315 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6316 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6318 static bool
6319 qualify_lookup (tree val, LOOK_want want)
6321 if (val == NULL_TREE)
6322 return false;
6324 if (bool (want & LOOK_want::TYPE))
6326 tree target_val = strip_using_decl (val);
6328 if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
6329 return true;
6332 if (bool (want & LOOK_want::TYPE_NAMESPACE))
6333 return TREE_CODE (val) == NAMESPACE_DECL;
6335 return true;
6338 /* Is there a "using namespace std;" directive within USINGS? */
6340 static bool
6341 using_directives_contain_std_p (vec<tree, va_gc> *usings)
6343 if (!usings)
6344 return false;
6346 for (unsigned ix = usings->length (); ix--;)
6347 if ((*usings)[ix] == std_node)
6348 return true;
6350 return false;
6353 /* Is there a "using namespace std;" directive within the current
6354 namespace (or its ancestors)?
6355 Compare with name_lookup::search_unqualified. */
6357 static bool
6358 has_using_namespace_std_directive_p ()
6360 for (cp_binding_level *level = current_binding_level;
6361 level;
6362 level = level->level_chain)
6363 if (using_directives_contain_std_p (level->using_directives))
6364 return true;
6366 return false;
6369 /* Subclass of deferred_diagnostic, for issuing a note when
6370 --param cxx-max-namespaces-for-diagnostic-help is reached.
6372 The note should be issued after the error, but before any other
6373 deferred diagnostics. This is handled by decorating a wrapped
6374 deferred_diagnostic, and emitting a note before that wrapped note is
6375 deleted. */
6377 class namespace_limit_reached : public deferred_diagnostic
6379 public:
6380 namespace_limit_reached (location_t loc, unsigned limit, tree name,
6381 std::unique_ptr<deferred_diagnostic> wrapped)
6382 : deferred_diagnostic (loc),
6383 m_limit (limit), m_name (name),
6384 m_wrapped (std::move (wrapped))
6388 ~namespace_limit_reached ()
6390 /* Unconditionally warn that the search was truncated. */
6391 inform (get_location (),
6392 "maximum limit of %d namespaces searched for %qE",
6393 m_limit, m_name);
6394 /* m_wrapped will be implicitly deleted after this, emitting any followup
6395 diagnostic after the above note. */
6398 private:
6399 unsigned m_limit;
6400 tree m_name;
6401 std::unique_ptr<deferred_diagnostic> m_wrapped;
6404 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6405 Emit a note showing the location of the declaration of the suggestion. */
6407 class show_candidate_location : public deferred_diagnostic
6409 public:
6410 show_candidate_location (location_t loc, tree candidate)
6411 : deferred_diagnostic (loc),
6412 m_candidate (candidate)
6416 ~show_candidate_location ()
6418 inform (location_of (m_candidate), "%qE declared here", m_candidate);
6421 private:
6422 tree m_candidate;
6425 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6426 to be suggested by suggest_alternatives_for.
6428 Emit a series of notes showing the various suggestions. */
6430 class suggest_alternatives : public deferred_diagnostic
6432 public:
6433 suggest_alternatives (location_t loc, vec<tree> candidates)
6434 : deferred_diagnostic (loc),
6435 m_candidates (candidates)
6439 ~suggest_alternatives ()
6441 if (m_candidates.length ())
6443 inform_n (get_location (), m_candidates.length (),
6444 "suggested alternative:",
6445 "suggested alternatives:");
6446 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
6448 tree val = m_candidates[ix];
6450 inform (location_of (val), " %qE", val);
6453 m_candidates.release ();
6456 private:
6457 vec<tree> m_candidates;
6460 /* A class for encapsulating the result of a search across
6461 multiple namespaces (and scoped enums within them) for an
6462 unrecognized name seen at a given source location. */
6464 class namespace_hints
6466 public:
6467 namespace_hints (location_t loc, tree name);
6469 name_hint convert_candidates_to_name_hint ();
6470 name_hint maybe_decorate_with_limit (name_hint);
6472 private:
6473 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
6475 location_t m_loc;
6476 tree m_name;
6477 vec<tree> m_candidates;
6479 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6480 unsigned m_limit;
6482 /* Was the limit reached? */
6483 bool m_limited;
6486 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6487 looking for an exact match for unrecognized NAME seen at LOC. */
6489 namespace_hints::namespace_hints (location_t loc, tree name)
6490 : m_loc(loc), m_name (name)
6492 auto_vec<tree> worklist;
6494 m_candidates = vNULL;
6495 m_limited = false;
6496 m_limit = param_cxx_max_namespaces_for_diagnostic_help;
6498 /* Breadth-first search of namespaces. Up to limit namespaces
6499 searched (limit zero == unlimited). */
6500 worklist.safe_push (global_namespace);
6501 for (unsigned ix = 0; ix != worklist.length (); ix++)
6503 tree ns = worklist[ix];
6504 name_lookup lookup (name);
6506 if (lookup.search_qualified (ns, false))
6507 m_candidates.safe_push (lookup.value);
6509 if (!m_limited)
6511 /* Look for child namespaces. We have to do this
6512 indirectly because they are chained in reverse order,
6513 which is confusing to the user. */
6514 auto_vec<tree> children;
6516 for (tree decl = NAMESPACE_LEVEL (ns)->names;
6517 decl; decl = TREE_CHAIN (decl))
6519 if (TREE_CODE (decl) == NAMESPACE_DECL
6520 && !DECL_NAMESPACE_ALIAS (decl)
6521 && !DECL_NAMESPACE_INLINE_P (decl))
6522 children.safe_push (decl);
6524 /* Look for exact matches for NAME within scoped enums.
6525 These aren't added to the worklist, and so don't count
6526 against the search limit. */
6527 if (TREE_CODE (decl) == TYPE_DECL)
6529 tree type = TREE_TYPE (decl);
6530 if (SCOPED_ENUM_P (type))
6531 maybe_add_candidate_for_scoped_enum (type, name);
6535 while (!m_limited && !children.is_empty ())
6537 if (worklist.length () == m_limit)
6538 m_limited = true;
6539 else
6540 worklist.safe_push (children.pop ());
6546 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6547 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6549 If m_candidates is non-empty, use it to generate a suggestion and/or
6550 a deferred diagnostic that lists the possible candidate(s).
6553 name_hint
6554 namespace_hints::convert_candidates_to_name_hint ()
6556 /* How many candidates do we have? */
6558 /* If we have just one candidate, issue a name_hint with it as a suggestion
6559 (so that consumers are able to suggest it within the error message and emit
6560 it as a fix-it hint), and with a note showing the candidate's location. */
6561 if (m_candidates.length () == 1)
6563 tree candidate = m_candidates[0];
6564 /* Clean up CANDIDATES. */
6565 m_candidates.release ();
6566 return name_hint (expr_to_string (candidate),
6567 new show_candidate_location (m_loc, candidate));
6569 else if (m_candidates.length () > 1)
6570 /* If we have more than one candidate, issue a name_hint without a single
6571 "suggestion", but with a deferred diagnostic that lists the
6572 various candidates. This takes ownership of m_candidates. */
6573 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
6575 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6576 gcc_assert (m_candidates.length () == 0);
6577 gcc_assert (m_candidates == vNULL);
6579 return name_hint ();
6582 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6583 then we want to emit a note about after the error, but before
6584 any other deferred diagnostics.
6586 Handle this by figuring out what hint is needed, then optionally
6587 decorating HINT with a namespace_limit_reached wrapper. */
6589 name_hint
6590 namespace_hints::maybe_decorate_with_limit (name_hint hint)
6592 if (m_limited)
6593 return name_hint (hint.suggestion (),
6594 new namespace_limit_reached (m_loc, m_limit,
6595 m_name,
6596 hint.take_deferred ()));
6597 else
6598 return hint;
6601 /* Look inside SCOPED_ENUM for exact matches for NAME.
6602 If one is found, add its CONST_DECL to m_candidates. */
6604 void
6605 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
6606 tree name)
6608 gcc_assert (SCOPED_ENUM_P (scoped_enum));
6610 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
6612 tree id = TREE_PURPOSE (iter);
6613 if (id == name)
6615 m_candidates.safe_push (TREE_VALUE (iter));
6616 return;
6621 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6622 name lookup failed.
6624 Search through all available namespaces and any scoped enums within them
6625 and generate a suggestion and/or a deferred diagnostic that lists possible
6626 candidate(s).
6628 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
6629 look for near-matches and suggest the best near-match, if there is one.
6631 If nothing is found, then an empty name_hint is returned. */
6633 name_hint
6634 suggest_alternatives_for (location_t location, tree name,
6635 bool suggest_misspellings)
6637 /* First, search for exact matches in other namespaces. */
6638 namespace_hints ns_hints (location, name);
6639 name_hint result = ns_hints.convert_candidates_to_name_hint ();
6641 /* Otherwise, try other approaches. */
6642 if (!result)
6643 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
6645 return ns_hints.maybe_decorate_with_limit (std::move (result));
6648 /* The second half of suggest_alternatives_for, for when no exact matches
6649 were found in other namespaces. */
6651 static name_hint
6652 suggest_alternatives_for_1 (location_t location, tree name,
6653 bool suggest_misspellings)
6655 /* No candidates were found in the available namespaces. */
6657 /* If there's a "using namespace std;" active, and this
6658 is one of the most common "std::" names, then it's probably a
6659 missing #include. */
6660 if (has_using_namespace_std_directive_p ())
6662 name_hint hint = maybe_suggest_missing_std_header (location, name);
6663 if (hint)
6664 return hint;
6667 /* Otherwise, consider misspellings. */
6668 if (!suggest_misspellings)
6669 return name_hint ();
6671 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
6674 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6675 name lookup failed.
6677 Search through all available namespaces and generate a suggestion and/or
6678 a deferred diagnostic that lists possible candidate(s).
6680 This is similiar to suggest_alternatives_for, but doesn't fallback to
6681 the other approaches used by that function. */
6683 name_hint
6684 suggest_alternatives_in_other_namespaces (location_t location, tree name)
6686 namespace_hints ns_hints (location, name);
6688 name_hint result = ns_hints.convert_candidates_to_name_hint ();
6690 return ns_hints.maybe_decorate_with_limit (std::move (result));
6693 /* A well-known name within the C++ standard library, returned by
6694 get_std_name_hint.
6696 The gperf-generated file contains the definition of the class
6697 "std_name_hint_lookup" with a static member function which
6698 returns the pointer to a structure "std_name_hint" which
6699 is also defined in that file. */
6701 #include "std-name-hint.h"
6703 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
6704 for some of the most common names within "std::".
6705 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
6707 static const std_name_hint *
6708 get_std_name_hint (const char *name)
6710 return std_name_hint_lookup::find(name, strlen(name));
6713 /* Describe DIALECT. */
6715 const char *
6716 get_cxx_dialect_name (enum cxx_dialect dialect)
6718 switch (dialect)
6720 default:
6721 gcc_unreachable ();
6722 case cxx98:
6723 return "C++98";
6724 case cxx11:
6725 return "C++11";
6726 case cxx14:
6727 return "C++14";
6728 case cxx17:
6729 return "C++17";
6730 case cxx20:
6731 return "C++20";
6732 case cxx23:
6733 return "C++23";
6734 case cxx26:
6735 return "C++26";
6739 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
6740 that weren't recognized, but for which we know which header it ought to be
6743 Emit a note either suggesting the header to be included, or noting that
6744 the current dialect is too early for the given name. */
6746 class missing_std_header : public deferred_diagnostic
6748 public:
6749 missing_std_header (location_t loc,
6750 const char *name_str,
6751 const std_name_hint *header_hint)
6752 : deferred_diagnostic (loc),
6753 m_name_str (name_str),
6754 m_header_hint (header_hint)
6756 ~missing_std_header ()
6758 gcc_rich_location richloc (get_location ());
6759 if (cxx_dialect >= m_header_hint->min_dialect)
6761 const char *header = m_header_hint->header;
6762 maybe_add_include_fixit (&richloc, header, true);
6763 inform (&richloc,
6764 "%<std::%s%> is defined in header %qs;"
6765 " this is probably fixable by adding %<#include %s%>",
6766 m_name_str, header, header);
6768 else
6769 inform (&richloc,
6770 "%<std::%s%> is only available from %s onwards",
6771 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
6774 private:
6775 const char *m_name_str;
6776 const std_name_hint *m_header_hint;
6779 /* Attempt to generate a name_hint that suggests pertinent header files
6780 for NAME at LOCATION, for common names within the "std" namespace,
6781 or an empty name_hint if this isn't applicable. */
6783 static name_hint
6784 maybe_suggest_missing_std_header (location_t location, tree name)
6786 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
6788 const char *name_str = IDENTIFIER_POINTER (name);
6789 const std_name_hint *header_hint = get_std_name_hint (name_str);
6790 if (!header_hint)
6791 return name_hint ();
6793 return name_hint (NULL, new missing_std_header (location, name_str,
6794 header_hint));
6797 /* Attempt to generate a name_hint that suggests a missing header file
6798 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
6799 applicable. */
6801 name_hint
6802 maybe_suggest_missing_header (location_t location, tree name, tree scope)
6804 if (scope == NULL_TREE)
6805 return name_hint ();
6806 if (TREE_CODE (scope) != NAMESPACE_DECL)
6807 return name_hint ();
6808 /* We only offer suggestions for the "std" namespace. */
6809 if (scope != std_node)
6810 return name_hint ();
6811 return maybe_suggest_missing_std_header (location, name);
6814 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
6815 lookup failed within the explicitly provided SCOPE.
6817 Suggest the best meaningful candidates (if any), otherwise
6818 an empty name_hint is returned. */
6820 name_hint
6821 suggest_alternative_in_explicit_scope (location_t location, tree name,
6822 tree scope)
6824 /* Something went very wrong; don't suggest anything. */
6825 if (name == error_mark_node)
6826 return name_hint ();
6828 /* Resolve any namespace aliases. */
6829 scope = ORIGINAL_NAMESPACE (scope);
6831 name_hint hint = maybe_suggest_missing_header (location, name, scope);
6832 if (hint)
6833 return hint;
6835 cp_binding_level *level = NAMESPACE_LEVEL (scope);
6837 best_match <tree, const char *> bm (name);
6838 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
6840 /* See if we have a good suggesion for the user. */
6841 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
6842 if (fuzzy_name)
6843 return name_hint (fuzzy_name, NULL);
6845 return name_hint ();
6848 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
6849 candidates. */
6851 name_hint
6852 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
6854 gcc_assert (SCOPED_ENUM_P (scoped_enum));
6856 best_match <tree, const char *> bm (name);
6857 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
6859 tree id = TREE_PURPOSE (iter);
6860 bm.consider (IDENTIFIER_POINTER (id));
6862 return name_hint (bm.get_best_meaningful_candidate (), NULL);
6865 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
6866 or a class TYPE).
6868 WANT as for lookup_name_1.
6870 Returns a DECL (or OVERLOAD, or BASELINK) representing the
6871 declaration found. If no suitable declaration can be found,
6872 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
6873 neither a class-type nor a namespace a diagnostic is issued. */
6875 tree
6876 lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
6878 tree t = NULL_TREE;
6880 if (TREE_CODE (scope) == NAMESPACE_DECL)
6882 name_lookup lookup (name, want);
6884 if (qualified_namespace_lookup (scope, &lookup))
6886 t = lookup.value;
6888 /* If we have a known type overload, pull it out. This can happen
6889 for using decls. */
6890 if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
6891 t = OVL_FUNCTION (t);
6894 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
6895 t = lookup_enumerator (scope, name);
6896 else if (is_class_type (scope, complain))
6897 t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
6898 tf_warning_or_error);
6900 if (!t)
6901 return error_mark_node;
6902 return t;
6905 /* Wrapper for the above that takes a string argument. The function name is
6906 not at the beginning of the line to keep this wrapper out of etags. */
6908 tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
6910 return lookup_qualified_name (t, get_identifier (p), w, c);
6913 /* [namespace.qual]
6914 Accepts the NAME to lookup and its qualifying SCOPE.
6915 Returns the name/type pair found into the cxx_binding *RESULT,
6916 or false on error. */
6918 static bool
6919 qualified_namespace_lookup (tree scope, name_lookup *lookup)
6921 timevar_start (TV_NAME_LOOKUP);
6922 query_oracle (lookup->name);
6923 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
6924 timevar_stop (TV_NAME_LOOKUP);
6925 return found;
6928 /* If DECL is suitably visible to the user, consider its name for
6929 spelling correction. */
6931 static void
6932 consider_decl (tree decl, best_match <tree, const char *> &bm,
6933 bool consider_impl_names)
6935 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6936 within range for). */
6937 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
6938 return;
6940 tree suggestion = DECL_NAME (decl);
6941 if (!suggestion)
6942 return;
6944 /* Don't suggest names that are for anonymous aggregate types, as
6945 they are an implementation detail generated by the compiler. */
6946 if (IDENTIFIER_ANON_P (suggestion))
6947 return;
6949 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
6951 /* Ignore internal names with spaces in them. */
6952 if (strchr (suggestion_str, ' '))
6953 return;
6955 /* Don't suggest names that are reserved for use by the
6956 implementation, unless NAME began with an underscore. */
6957 if (!consider_impl_names
6958 && name_reserved_for_implementation_p (suggestion_str))
6959 return;
6961 bm.consider (suggestion_str);
6964 /* If DECL is suitably visible to the user, add its name to VEC and
6965 return true. Otherwise return false. */
6967 static bool
6968 maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
6970 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6971 within range for). */
6972 if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
6973 return false;
6975 tree suggestion = DECL_NAME (decl);
6976 if (!suggestion)
6977 return false;
6979 /* Don't suggest names that are for anonymous aggregate types, as
6980 they are an implementation detail generated by the compiler. */
6981 if (IDENTIFIER_ANON_P (suggestion))
6982 return false;
6984 vec.safe_push (suggestion);
6986 return true;
6989 /* Examing the namespace binding BINDING, and add at most one instance
6990 of the name, if it contains a visible entity of interest. Return
6991 true if we added something. */
6993 bool
6994 maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
6995 lookup_name_fuzzy_kind kind)
6997 tree value = NULL_TREE;
6999 if (STAT_HACK_P (binding))
7001 if (!STAT_TYPE_HIDDEN_P (binding)
7002 && STAT_TYPE (binding))
7004 if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
7005 return true;
7007 else if (!STAT_DECL_HIDDEN_P (binding))
7008 value = STAT_DECL (binding);
7010 else
7011 value = binding;
7013 value = ovl_skip_hidden (value);
7014 if (value)
7016 value = OVL_FIRST (value);
7017 if (kind != FUZZY_LOOKUP_TYPENAME
7018 || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
7019 if (maybe_add_fuzzy_decl (vec, value))
7020 return true;
7023 /* Nothing found. */
7024 return false;
7027 /* Helper function for lookup_name_fuzzy.
7028 Traverse binding level LVL, looking for good name matches for NAME
7029 (and BM). */
7030 static void
7031 consider_binding_level (tree name, best_match <tree, const char *> &bm,
7032 cp_binding_level *lvl, bool look_within_fields,
7033 enum lookup_name_fuzzy_kind kind)
7035 if (look_within_fields)
7036 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
7038 tree type = lvl->this_entity;
7039 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
7040 tree best_matching_field
7041 = lookup_member_fuzzy (type, name, want_type_p);
7042 if (best_matching_field)
7043 bm.consider (IDENTIFIER_POINTER (best_matching_field));
7046 /* Only suggest names reserved for the implementation if NAME begins
7047 with an underscore. */
7048 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
7050 if (lvl->kind != sk_namespace)
7051 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
7053 tree d = t;
7055 /* OVERLOADs or decls from using declaration are wrapped into
7056 TREE_LIST. */
7057 if (TREE_CODE (d) == TREE_LIST)
7058 d = OVL_FIRST (TREE_VALUE (d));
7060 /* Don't use bindings from implicitly declared functions,
7061 as they were likely misspellings themselves. */
7062 if (TREE_TYPE (d) == error_mark_node)
7063 continue;
7065 /* If we want a typename, ignore non-types. */
7066 if (kind == FUZZY_LOOKUP_TYPENAME
7067 && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
7068 continue;
7070 consider_decl (d, bm, consider_implementation_names);
7072 else
7074 /* We need to iterate over the namespace hash table, in order to
7075 not mention hidden entities. But hash table iteration is
7076 (essentially) unpredictable, our correction-distance measure
7077 is very granular, and we pick the first of equal distances.
7078 Hence, we need to call the distance-measurer in a predictable
7079 order. So, iterate over the namespace hash, inserting
7080 visible names into a vector. Then sort the vector. Then
7081 determine spelling distance. */
7083 tree ns = lvl->this_entity;
7084 auto_vec<tree> vec;
7086 hash_table<named_decl_hash>::iterator end
7087 (DECL_NAMESPACE_BINDINGS (ns)->end ());
7088 for (hash_table<named_decl_hash>::iterator iter
7089 (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
7091 tree binding = *iter;
7093 if (TREE_CODE (binding) == BINDING_VECTOR)
7095 bitmap imports = get_import_bitmap ();
7096 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
7098 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
7099 if (maybe_add_fuzzy_binding (vec, bind, kind))
7100 continue;
7102 /* Scan the imported bindings. */
7103 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
7104 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7106 ix--;
7107 cluster++;
7110 for (; ix--; cluster++)
7111 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
7112 jx++)
7114 /* Are we importing this module? */
7115 if (unsigned base = cluster->indices[jx].base)
7116 if (unsigned span = cluster->indices[jx].span)
7118 if (bitmap_bit_p (imports, base))
7119 goto found;
7120 while (++base, --span);
7121 continue;
7123 found:;
7124 /* Is it loaded? */
7125 if (cluster->slots[jx].is_lazy ())
7126 /* Let's not read in everything on the first
7127 spello! **/
7128 continue;
7129 if (tree bind = cluster->slots[jx])
7130 if (maybe_add_fuzzy_binding (vec, bind, kind))
7131 break;
7134 else
7135 maybe_add_fuzzy_binding (vec, binding, kind);
7138 vec.qsort ([] (const void *a_, const void *b_)
7140 return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
7141 IDENTIFIER_POINTER (*(const tree *)b_));
7144 /* Examine longest to shortest. */
7145 for (unsigned ix = vec.length (); ix--;)
7147 const char *str = IDENTIFIER_POINTER (vec[ix]);
7149 /* Ignore internal names with spaces in them. */
7150 if (strchr (str, ' '))
7151 continue;
7153 /* Don't suggest names that are reserved for use by the
7154 implementation, unless NAME began with an underscore. */
7155 if (!consider_implementation_names
7156 && name_reserved_for_implementation_p (str))
7157 continue;
7159 bm.consider (str);
7164 /* Subclass of deferred_diagnostic. Notify the user that the
7165 given macro was used before it was defined.
7166 This can be done in the C++ frontend since tokenization happens
7167 upfront. */
7169 class macro_use_before_def : public deferred_diagnostic
7171 public:
7172 /* Factory function. Return a new macro_use_before_def instance if
7173 appropriate, or return NULL. */
7174 static macro_use_before_def *
7175 maybe_make (location_t use_loc, cpp_hashnode *macro)
7177 location_t def_loc = cpp_macro_definition_location (macro);
7178 if (def_loc == UNKNOWN_LOCATION)
7179 return NULL;
7181 /* We only want to issue a note if the macro was used *before* it was
7182 defined.
7183 We don't want to issue a note for cases where a macro was incorrectly
7184 used, leaving it unexpanded (e.g. by using the wrong argument
7185 count). */
7186 if (!linemap_location_before_p (line_table, use_loc, def_loc))
7187 return NULL;
7189 return new macro_use_before_def (use_loc, macro);
7192 private:
7193 /* Ctor. LOC is the location of the usage. MACRO is the
7194 macro that was used. */
7195 macro_use_before_def (location_t loc, cpp_hashnode *macro)
7196 : deferred_diagnostic (loc), m_macro (macro)
7198 gcc_assert (macro);
7201 ~macro_use_before_def ()
7203 if (is_suppressed_p ())
7204 return;
7206 inform (get_location (), "the macro %qs had not yet been defined",
7207 (const char *)m_macro->ident.str);
7208 inform (cpp_macro_definition_location (m_macro),
7209 "it was later defined here");
7212 private:
7213 cpp_hashnode *m_macro;
7216 /* Determine if it can ever make sense to offer RID as a suggestion for
7217 a misspelling.
7219 Subroutine of lookup_name_fuzzy. */
7221 static bool
7222 suggest_rid_p (enum rid rid)
7224 switch (rid)
7226 /* Support suggesting function-like keywords. */
7227 case RID_STATIC_ASSERT:
7228 return true;
7230 default:
7231 /* Support suggesting the various decl-specifier words, to handle
7232 e.g. "singed" vs "signed" typos. */
7233 if (cp_keyword_starts_decl_specifier_p (rid))
7234 return true;
7236 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7237 and "do" for short misspellings, which are likely to lead to
7238 nonsensical results. */
7239 return false;
7243 /* Search for near-matches for NAME within the current bindings, and within
7244 macro names, returning the best match as a const char *, or NULL if
7245 no reasonable match is found.
7247 Use LOC for any deferred diagnostics. */
7249 name_hint
7250 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
7252 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7254 /* First, try some well-known names in the C++ standard library, in case
7255 the user forgot a #include. */
7256 const char *header_hint
7257 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
7258 if (header_hint)
7259 return name_hint (NULL,
7260 new suggest_missing_header (loc,
7261 IDENTIFIER_POINTER (name),
7262 header_hint));
7264 best_match <tree, const char *> bm (name);
7266 cp_binding_level *lvl;
7267 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
7268 consider_binding_level (name, bm, lvl, true, kind);
7270 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
7271 consider_binding_level (name, bm, lvl, false, kind);
7273 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7275 x = SOME_OTHER_MACRO (y);
7276 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7277 as a misspelled identifier.
7279 Use the best distance so far so that a candidate is only set if
7280 a macro is better than anything so far. This allows early rejection
7281 (without calculating the edit distance) of macro names that must have
7282 distance >= bm.get_best_distance (), and means that we only get a
7283 non-NULL result for best_macro_match if it's better than any of
7284 the identifiers already checked. */
7285 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
7286 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
7287 /* If a macro is the closest so far to NAME, consider it. */
7288 if (best_macro)
7289 bm.consider ((const char *)best_macro->ident.str);
7290 else if (bmm.get_best_distance () == 0)
7292 /* If we have an exact match for a macro name, then either the
7293 macro was used with the wrong argument count, or the macro
7294 has been used before it was defined. */
7295 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
7296 if (cpp_user_macro_p (macro))
7297 return name_hint (NULL,
7298 macro_use_before_def::maybe_make (loc, macro));
7301 /* Try the "starts_decl_specifier_p" keywords to detect
7302 "singed" vs "signed" typos. */
7303 for (unsigned i = 0; i < num_c_common_reswords; i++)
7305 const c_common_resword *resword = &c_common_reswords[i];
7307 if (!suggest_rid_p (resword->rid))
7308 continue;
7310 tree resword_identifier = ridpointers [resword->rid];
7311 if (!resword_identifier)
7312 continue;
7313 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
7315 /* Only consider reserved words that survived the
7316 filtering in init_reswords (e.g. for -std). */
7317 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
7318 continue;
7320 bm.consider (IDENTIFIER_POINTER (resword_identifier));
7323 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7326 /* Subroutine of outer_binding.
7328 Returns TRUE if BINDING is a binding to a template parameter of
7329 SCOPE. In that case SCOPE is the scope of a primary template
7330 parameter -- in the sense of G++, i.e, a template that has its own
7331 template header.
7333 Returns FALSE otherwise. */
7335 static bool
7336 binding_to_template_parms_of_scope_p (cxx_binding *binding,
7337 cp_binding_level *scope)
7339 tree binding_value, tmpl, tinfo;
7340 int level;
7342 if (!binding || !scope || !scope->this_entity)
7343 return false;
7345 binding_value = binding->value ? binding->value : binding->type;
7346 tinfo = get_template_info (scope->this_entity);
7348 /* BINDING_VALUE must be a template parm. */
7349 if (binding_value == NULL_TREE
7350 || (!DECL_P (binding_value)
7351 || !DECL_TEMPLATE_PARM_P (binding_value)))
7352 return false;
7354 /* The level of BINDING_VALUE. */
7355 level =
7356 template_type_parameter_p (binding_value)
7357 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7358 (TREE_TYPE (binding_value)))
7359 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
7361 /* The template of the current scope, iff said scope is a primary
7362 template. */
7363 tmpl = (tinfo
7364 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
7365 ? TI_TEMPLATE (tinfo)
7366 : NULL_TREE);
7368 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7369 then BINDING_VALUE is a parameter of TMPL. */
7370 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
7373 /* Return the innermost non-namespace binding for NAME from a scope
7374 containing BINDING, or, if BINDING is NULL, the current scope.
7375 Please note that for a given template, the template parameters are
7376 considered to be in the scope containing the current scope.
7377 If CLASS_P is false, then class bindings are ignored. */
7379 cxx_binding *
7380 outer_binding (tree name,
7381 cxx_binding *binding,
7382 bool class_p)
7384 cxx_binding *outer;
7385 cp_binding_level *scope;
7386 cp_binding_level *outer_scope;
7388 if (binding)
7390 scope = binding->scope->level_chain;
7391 outer = binding->previous;
7393 else
7395 scope = current_binding_level;
7396 outer = IDENTIFIER_BINDING (name);
7398 outer_scope = outer ? outer->scope : NULL;
7400 /* Because we create class bindings lazily, we might be missing a
7401 class binding for NAME. If there are any class binding levels
7402 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7403 declared, we must lookup NAME in those class scopes. */
7404 if (class_p)
7405 while (scope && scope != outer_scope && scope->kind != sk_namespace)
7407 if (scope->kind == sk_class)
7409 cxx_binding *class_binding;
7411 class_binding = get_class_binding (name, scope);
7412 if (class_binding)
7414 /* Thread this new class-scope binding onto the
7415 IDENTIFIER_BINDING list so that future lookups
7416 find it quickly. */
7417 if (BASELINK_P (class_binding->value))
7418 /* Don't put a BASELINK in IDENTIFIER_BINDING. */
7419 class_binding->value
7420 = BASELINK_FUNCTIONS (class_binding->value);
7421 class_binding->previous = outer;
7422 if (binding)
7423 binding->previous = class_binding;
7424 else
7425 IDENTIFIER_BINDING (name) = class_binding;
7426 return class_binding;
7429 /* If we are in a member template, the template parms of the member
7430 template are considered to be inside the scope of the containing
7431 class, but within G++ the class bindings are all pushed between the
7432 template parms and the function body. So if the outer binding is
7433 a template parm for the current scope, return it now rather than
7434 look for a class binding. */
7435 if (outer_scope && outer_scope->kind == sk_template_parms
7436 && binding_to_template_parms_of_scope_p (outer, scope))
7437 return outer;
7439 scope = scope->level_chain;
7442 return outer;
7445 /* Return the innermost block-scope or class-scope value binding for
7446 NAME, or NULL_TREE if there is no such binding. */
7448 tree
7449 innermost_non_namespace_value (tree name)
7451 cxx_binding *binding;
7452 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
7453 return binding ? binding->value : NULL_TREE;
7456 /* True iff current_binding_level is within the potential scope of local
7457 variable DECL. */
7459 bool
7460 decl_in_scope_p (tree decl)
7462 gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
7464 tree name = DECL_NAME (decl);
7466 for (cxx_binding *iter = NULL;
7467 (iter = outer_binding (name, iter, /*class_p=*/false)); )
7469 if (!LOCAL_BINDING_P (iter))
7470 return false;
7471 if (iter->value == decl)
7472 return true;
7475 return false;
7478 /* Look up NAME in the current binding level and its superiors in the
7479 namespace of variables, functions and typedefs. Return a ..._DECL
7480 node of some kind representing its definition if there is only one
7481 such declaration, or return a TREE_LIST with all the overloaded
7482 definitions if there are many, or return NULL_TREE if it is undefined.
7483 Hidden name, either friend declaration or built-in function, are
7484 not ignored.
7486 WHERE controls which scopes are considered. It is a bit mask of
7487 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7488 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7489 scopes). It is an error for no bits to be set. These scopes are
7490 searched from innermost to outermost.
7492 WANT controls what kind of entity we'd happy with.
7493 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7494 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7495 for only NAMESPACE_DECLS. These two can be bit-ored to find
7496 namespace or type.
7498 WANT can also have LOOK_want::HIDDEN_FRIEND or
7499 LOOK_want::HIDDEN_LAMBDa added to it. */
7501 tree
7502 lookup_name (tree name, LOOK_where where, LOOK_want want)
7504 tree val = NULL_TREE;
7506 auto_cond_timevar tv (TV_NAME_LOOKUP);
7508 gcc_checking_assert (unsigned (where) != 0);
7509 /* If we're looking for hidden lambda things, we shouldn't be
7510 looking in namespace scope. */
7511 gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
7512 || !bool (where & LOOK_where::NAMESPACE));
7513 query_oracle (name);
7515 /* Conversion operators are handled specially because ordinary
7516 unqualified name lookup will not find template conversion
7517 operators. */
7518 if (IDENTIFIER_CONV_OP_P (name))
7520 cp_binding_level *level;
7522 for (level = current_binding_level;
7523 level && level->kind != sk_namespace;
7524 level = level->level_chain)
7526 tree class_type;
7527 tree operators;
7529 /* A conversion operator can only be declared in a class
7530 scope. */
7531 if (level->kind != sk_class)
7532 continue;
7534 /* Lookup the conversion operator in the class. */
7535 class_type = level->this_entity;
7536 operators = lookup_fnfields (class_type, name, /*protect=*/0,
7537 tf_warning_or_error);
7538 if (operators)
7539 return operators;
7542 return NULL_TREE;
7545 /* First, look in non-namespace scopes. */
7547 if (current_class_type == NULL_TREE)
7548 /* Maybe avoid searching the binding stack at all. */
7549 where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
7551 if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
7552 for (cxx_binding *iter = nullptr;
7553 (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
7555 /* Skip entities we don't want. */
7556 if (!bool (where & (LOCAL_BINDING_P (iter)
7557 ? LOOK_where::BLOCK : LOOK_where::CLASS)))
7558 continue;
7560 /* If this is the kind of thing we're looking for, we're done. */
7561 if (iter->value)
7563 tree binding = NULL_TREE;
7565 if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
7566 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
7567 || !is_lambda_ignored_entity (iter->value))
7568 && qualify_lookup (iter->value, want))
7569 binding = iter->value;
7570 else if (bool (want & LOOK_want::TYPE)
7571 && !HIDDEN_TYPE_BINDING_P (iter)
7572 && iter->type)
7573 binding = iter->type;
7575 if (binding)
7577 val = binding;
7578 break;
7583 /* Now lookup in namespace scopes. */
7584 if (!val && bool (where & LOOK_where::NAMESPACE))
7586 name_lookup lookup (name, want);
7587 if (lookup.search_unqualified
7588 (current_decl_namespace (), current_binding_level))
7589 val = lookup.value;
7592 /* If we have a known type overload, pull it out. This can happen
7593 for both using decls and unhidden functions. */
7594 if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
7595 val = OVL_FUNCTION (val);
7597 return val;
7600 tree
7601 lookup_name (tree name)
7603 return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
7606 /* Look up NAME for type used in elaborated name specifier in
7607 the scopes given by HOW.
7609 Unlike lookup_name_1, we make sure that NAME is actually
7610 declared in the desired scope, not from inheritance, nor using
7611 directive. For using declaration, there is DR138 still waiting
7612 to be resolved. Hidden name coming from an earlier friend
7613 declaration is also returned, and will be made visible unless HOW
7614 is TAG_how::HIDDEN_FRIEND.
7616 A TYPE_DECL best matching the NAME is returned. Catching error
7617 and issuing diagnostics are caller's responsibility. */
7619 tree
7620 lookup_elaborated_type (tree name, TAG_how how)
7622 auto_cond_timevar tv (TV_NAME_LOOKUP);
7624 cp_binding_level *b = current_binding_level;
7626 if (b->kind != sk_namespace)
7627 /* Look in non-namespace scopes. */
7628 for (cxx_binding *iter = NULL;
7629 (iter = outer_binding (name, iter, /*class_p=*/ true)); )
7631 /* First check we're supposed to be looking in this scope --
7632 if we're not, we're done. */
7633 for (; b != iter->scope; b = b->level_chain)
7634 if (!(b->kind == sk_cleanup
7635 || b->kind == sk_template_parms
7636 || b->kind == sk_function_parms
7637 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
7638 return NULL_TREE;
7640 /* Check if this is the kind of thing we're looking for. If
7641 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
7642 come from base class. For ITER->VALUE, we can simply use
7643 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
7644 our own check.
7646 We check ITER->TYPE before ITER->VALUE in order to handle
7647 typedef struct C {} C;
7648 correctly. */
7650 if (tree type = iter->type)
7652 if (qualify_lookup (type, LOOK_want::TYPE)
7653 && (how != TAG_how::CURRENT_ONLY
7654 || LOCAL_BINDING_P (iter)
7655 || DECL_CONTEXT (type) == iter->scope->this_entity))
7657 if (how != TAG_how::HIDDEN_FRIEND)
7658 /* It is no longer a hidden binding. */
7659 HIDDEN_TYPE_BINDING_P (iter) = false;
7661 return type;
7664 else
7666 if (qualify_lookup (iter->value, LOOK_want::TYPE)
7667 && (how != TAG_how::CURRENT_ONLY
7668 || !INHERITED_VALUE_BINDING_P (iter)))
7670 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
7671 /* It is no longer a hidden binding. */
7672 HIDDEN_TYPE_BINDING_P (iter) = false;
7674 return iter->value;
7679 /* Now check if we can look in namespace scope. */
7680 for (; b->kind != sk_namespace; b = b->level_chain)
7681 if (!(b->kind == sk_cleanup
7682 || b->kind == sk_template_parms
7683 || b->kind == sk_function_parms
7684 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
7685 return NULL_TREE;
7687 /* Look in the innermost namespace. */
7688 tree ns = b->this_entity;
7689 if (tree *slot = find_namespace_slot (ns, name))
7691 tree bind = *slot;
7692 if (TREE_CODE (bind) == BINDING_VECTOR)
7693 bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
7695 if (bind)
7697 /* If this is the kind of thing we're looking for, we're done. */
7698 if (tree type = MAYBE_STAT_TYPE (bind))
7700 if (how != TAG_how::HIDDEN_FRIEND)
7701 /* No longer hidden. */
7702 STAT_TYPE_HIDDEN_P (*slot) = false;
7704 return type;
7706 else if (tree decl = MAYBE_STAT_DECL (bind))
7708 if (qualify_lookup (decl, LOOK_want::TYPE))
7710 if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
7711 && STAT_DECL_HIDDEN_P (bind))
7713 if (STAT_TYPE (bind))
7714 STAT_DECL_HIDDEN_P (bind) = false;
7715 else
7717 /* There is no type, just remove the stat
7718 hack. */
7719 if (*slot == bind)
7720 *slot = decl;
7721 else
7722 BINDING_VECTOR_CLUSTER (*slot, 0)
7723 .slots[BINDING_SLOT_CURRENT] = decl;
7726 return decl;
7731 if (TREE_CODE (*slot) == BINDING_VECTOR)
7733 /* We could be redeclaring a global module entity, (from GMF
7734 or header unit), or from another partition, or
7735 specializing an imported template. */
7736 bitmap imports = get_import_bitmap ();
7737 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
7739 /* Scan the imported bindings. */
7740 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
7741 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7743 ix--;
7744 cluster++;
7747 /* Do this in forward order, so we load modules in an order
7748 the user expects. */
7749 for (; ix--; cluster++)
7750 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
7752 /* Are we importing this module? */
7753 if (unsigned base = cluster->indices[jx].base)
7754 if (unsigned span = cluster->indices[jx].span)
7756 if (bitmap_bit_p (imports, base))
7757 goto found;
7758 while (++base, --span);
7759 continue;
7761 found:;
7762 /* Is it loaded? */
7763 if (cluster->slots[jx].is_lazy ())
7765 gcc_assert (cluster->indices[jx].span == 1);
7766 lazy_load_binding (cluster->indices[jx].base,
7767 ns, name, &cluster->slots[jx]);
7769 tree bind = cluster->slots[jx];
7770 if (!bind)
7771 /* Load errors could mean there's nothing here. */
7772 continue;
7774 /* Extract what we can see from here. If there's no
7775 stat_hack, then everything was exported. */
7776 tree type = NULL_TREE;
7778 /* If no stat hack, everything is visible. */
7779 if (STAT_HACK_P (bind))
7781 if (STAT_TYPE_VISIBLE_P (bind))
7782 type = STAT_TYPE (bind);
7783 bind = STAT_VISIBLE (bind);
7786 if (type && qualify_lookup (type, LOOK_want::TYPE))
7787 return type;
7789 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
7790 return bind;
7793 if (!module_purview_p ())
7795 /* We're in the global module, perhaps there's a tag
7796 there? */
7797 // FIXME: This isn't quite right, if we find something
7798 // here, from the language PoV we're not supposed to
7799 // know it?
7804 return NULL_TREE;
7807 /* The type TYPE is being declared. If it is a class template, or a
7808 specialization of a class template, do any processing required and
7809 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
7810 being declared a friend. B is the binding level at which this TYPE
7811 should be bound.
7813 Returns the TYPE_DECL for TYPE, which may have been altered by this
7814 processing. */
7816 static tree
7817 maybe_process_template_type_declaration (tree type, int is_friend,
7818 cp_binding_level *b)
7820 tree decl = TYPE_NAME (type);
7822 if (processing_template_parmlist)
7823 /* You can't declare a new template type in a template parameter
7824 list. But, you can declare a non-template type:
7826 template <class A*> struct S;
7828 is a forward-declaration of `A'. */
7830 else if (b->kind == sk_namespace
7831 && current_binding_level->kind != sk_namespace)
7832 /* If this new type is being injected into a containing scope,
7833 then it's not a template type. */
7835 else
7837 gcc_assert (MAYBE_CLASS_TYPE_P (type)
7838 || TREE_CODE (type) == ENUMERAL_TYPE);
7840 if (processing_template_decl)
7842 decl = push_template_decl (decl, is_friend);
7843 if (decl == error_mark_node)
7844 return error_mark_node;
7846 /* If the current binding level is the binding level for the
7847 template parameters (see the comment in
7848 begin_template_parm_list) and the enclosing level is a class
7849 scope, and we're not looking at a friend, push the
7850 declaration of the member class into the class scope. In the
7851 friend case, push_template_decl will already have put the
7852 friend into global scope, if appropriate. */
7853 if (TREE_CODE (type) != ENUMERAL_TYPE
7854 && !is_friend && b->kind == sk_template_parms
7855 && b->level_chain->kind == sk_class)
7857 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
7859 if (!COMPLETE_TYPE_P (current_class_type))
7860 maybe_add_class_template_decl_list (current_class_type,
7861 type, /*friend_p=*/0);
7866 return decl;
7869 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
7870 that the NAME is a class template, the tag is processed but not pushed.
7872 The pushed scope depend on the SCOPE parameter:
7873 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
7874 scope.
7875 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
7876 non-template-parameter scope. This case is needed for forward
7877 declarations.
7878 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
7879 TS_GLOBAL case except that names within template-parameter scopes
7880 are not pushed at all.
7882 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
7884 tree
7885 pushtag (tree name, tree type, TAG_how how)
7887 tree decl;
7889 gcc_assert (identifier_p (name));
7891 auto_cond_timevar tv (TV_NAME_LOOKUP);
7893 cp_binding_level *b = current_binding_level;
7894 while (true)
7896 if (/* Cleanup scopes are not scopes from the point of view of
7897 the language. */
7898 b->kind == sk_cleanup
7899 /* Neither are function parameter scopes. */
7900 || b->kind == sk_function_parms
7901 /* Neither are the scopes used to hold template parameters
7902 for an explicit specialization. For an ordinary template
7903 declaration, these scopes are not scopes from the point of
7904 view of the language. */
7905 || (b->kind == sk_template_parms
7906 && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
7907 b = b->level_chain;
7908 else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
7910 b = b->level_chain;
7911 if (b->kind == sk_template_parms)
7912 b = b->level_chain;
7914 else
7915 break;
7918 /* Do C++ gratuitous typedefing. */
7919 if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
7921 tree tdef;
7922 tree context = TYPE_CONTEXT (type);
7924 if (! context)
7926 cp_binding_level *cb = b;
7927 while (cb->kind != sk_namespace
7928 && cb->kind != sk_class
7929 && (cb->kind != sk_function_parms
7930 || !cb->this_entity))
7931 cb = cb->level_chain;
7932 tree cs = cb->this_entity;
7934 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
7935 ? cs == current_function_decl
7936 : TYPE_P (cs) ? cs == current_class_type
7937 : cs == current_namespace);
7939 if (how == TAG_how::CURRENT_ONLY
7940 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
7941 context = cs;
7942 else if (cs && TYPE_P (cs))
7943 /* When declaring a friend class of a local class, we want
7944 to inject the newly named class into the scope
7945 containing the local class, not the namespace
7946 scope. */
7947 context = decl_function_context (get_type_decl (cs));
7949 if (!context)
7950 context = current_namespace;
7952 tdef = create_implicit_typedef (name, type);
7953 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
7954 set_originating_module (tdef);
7956 decl = maybe_process_template_type_declaration
7957 (type, how == TAG_how::HIDDEN_FRIEND, b);
7958 if (decl == error_mark_node)
7959 return decl;
7961 if (b->kind == sk_class)
7963 if (!TYPE_BEING_DEFINED (current_class_type))
7964 /* Don't push anywhere if the class is complete; a lambda in an
7965 NSDMI is not a member of the class. */
7967 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
7968 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
7969 class. But if it's a member template class, we want
7970 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
7971 later. */
7972 finish_member_declaration (decl);
7973 else
7974 pushdecl_class_level (decl);
7976 else if (b->kind == sk_template_parms)
7978 /* Do not push the tag here -- we'll want to push the
7979 TEMPLATE_DECL. */
7980 if (b->level_chain->kind != sk_class)
7981 set_identifier_type_value_with_scope (name, tdef, b->level_chain);
7983 else
7985 decl = do_pushdecl_with_scope
7986 (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
7987 if (decl == error_mark_node)
7988 return decl;
7990 if (DECL_CONTEXT (decl) == std_node
7991 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
7992 && !CLASSTYPE_TEMPLATE_INFO (type))
7994 error ("declaration of %<std::initializer_list%> does not match "
7995 "%<#include <initializer_list>%>, isn%'t a template");
7996 return error_mark_node;
8000 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
8002 /* If this is a local class, keep track of it. We need this
8003 information for name-mangling, and so that it is possible to
8004 find all function definitions in a translation unit in a
8005 convenient way. (It's otherwise tricky to find a member
8006 function definition it's only pointed to from within a local
8007 class.) */
8008 if (TYPE_FUNCTION_SCOPE_P (type))
8010 if (processing_template_decl)
8012 /* Push a DECL_EXPR so we call pushtag at the right time in
8013 template instantiation rather than in some nested context. */
8014 add_decl_expr (decl);
8016 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8017 else if (!LAMBDA_TYPE_P (type))
8018 determine_local_discriminator (TYPE_NAME (type));
8022 if (b->kind == sk_class
8023 && !COMPLETE_TYPE_P (current_class_type))
8024 maybe_add_class_template_decl_list (current_class_type,
8025 type, /*friend_p=*/0);
8027 decl = TYPE_NAME (type);
8028 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
8030 /* Set type visibility now if this is a forward declaration. */
8031 TREE_PUBLIC (decl) = 1;
8032 determine_visibility (decl);
8034 return type;
8037 /* Subroutines for reverting temporarily to top-level for instantiation
8038 of templates and such. We actually need to clear out the class- and
8039 local-value slots of all identifiers, so that only the global values
8040 are at all visible. Simply setting current_binding_level to the global
8041 scope isn't enough, because more binding levels may be pushed. */
8042 struct saved_scope *scope_chain;
8044 /* Return true if ID has not already been marked. */
8046 static inline bool
8047 store_binding_p (tree id)
8049 if (!id || !IDENTIFIER_BINDING (id))
8050 return false;
8052 if (IDENTIFIER_MARKED (id))
8053 return false;
8055 return true;
8058 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8059 have enough space reserved. */
8061 static void
8062 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
8064 cxx_saved_binding saved;
8066 gcc_checking_assert (store_binding_p (id));
8068 IDENTIFIER_MARKED (id) = 1;
8070 saved.identifier = id;
8071 saved.binding = IDENTIFIER_BINDING (id);
8072 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8073 (*old_bindings)->quick_push (saved);
8074 IDENTIFIER_BINDING (id) = NULL;
8077 static void
8078 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
8080 static vec<tree> bindings_need_stored;
8081 tree t, id;
8082 size_t i;
8084 auto_cond_timevar tv (TV_NAME_LOOKUP);
8085 for (t = names; t; t = TREE_CHAIN (t))
8087 if (TREE_CODE (t) == TREE_LIST)
8088 id = TREE_PURPOSE (t);
8089 else
8090 id = DECL_NAME (t);
8092 if (store_binding_p (id))
8093 bindings_need_stored.safe_push (id);
8095 if (!bindings_need_stored.is_empty ())
8097 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8098 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8100 /* We can apparently have duplicates in NAMES. */
8101 if (store_binding_p (id))
8102 store_binding (id, old_bindings);
8104 bindings_need_stored.truncate (0);
8108 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8109 objects, rather than a TREE_LIST. */
8111 static void
8112 store_class_bindings (vec<cp_class_binding, va_gc> *names,
8113 vec<cxx_saved_binding, va_gc> **old_bindings)
8115 static vec<tree> bindings_need_stored;
8116 size_t i;
8117 cp_class_binding *cb;
8119 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
8120 if (store_binding_p (cb->identifier))
8121 bindings_need_stored.safe_push (cb->identifier);
8122 if (!bindings_need_stored.is_empty ())
8124 tree id;
8125 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8126 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8127 store_binding (id, old_bindings);
8128 bindings_need_stored.truncate (0);
8132 /* A chain of saved_scope structures awaiting reuse. */
8134 static GTY((deletable)) struct saved_scope *free_saved_scope;
8136 void
8137 push_to_top_level (void)
8139 struct saved_scope *s;
8140 cp_binding_level *b;
8141 cxx_saved_binding *sb;
8142 size_t i;
8143 bool need_pop;
8145 auto_cond_timevar tv (TV_NAME_LOOKUP);
8147 /* Reuse or create a new structure for this saved scope. */
8148 if (free_saved_scope != NULL)
8150 s = free_saved_scope;
8151 free_saved_scope = s->prev;
8153 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
8154 memset (s, 0, sizeof (*s));
8155 /* Also reuse the structure's old_bindings vector. */
8156 vec_safe_truncate (old_bindings, 0);
8157 s->old_bindings = old_bindings;
8159 else
8160 s = ggc_cleared_alloc<saved_scope> ();
8162 b = scope_chain ? current_binding_level : 0;
8164 /* If we're in the middle of some function, save our state. */
8165 if (cfun)
8167 need_pop = true;
8168 push_function_context ();
8170 else
8171 need_pop = false;
8173 if (scope_chain && previous_class_level)
8174 store_class_bindings (previous_class_level->class_shadowed,
8175 &s->old_bindings);
8177 /* Have to include the global scope, because class-scope decls
8178 aren't listed anywhere useful. */
8179 for (; b; b = b->level_chain)
8181 tree t;
8183 /* Template IDs are inserted into the global level. If they were
8184 inserted into namespace level, finish_file wouldn't find them
8185 when doing pending instantiations. Therefore, don't stop at
8186 namespace level, but continue until :: . */
8187 if (global_scope_p (b))
8188 break;
8190 store_bindings (b->names, &s->old_bindings);
8191 /* We also need to check class_shadowed to save class-level type
8192 bindings, since pushclass doesn't fill in b->names. */
8193 if (b->kind == sk_class)
8194 store_class_bindings (b->class_shadowed, &s->old_bindings);
8196 /* Unwind type-value slots back to top level. */
8197 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
8198 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
8201 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
8202 IDENTIFIER_MARKED (sb->identifier) = 0;
8204 s->prev = scope_chain;
8205 s->bindings = b;
8206 s->need_pop_function_context = need_pop;
8207 s->function_decl = current_function_decl;
8208 s->unevaluated_operand = cp_unevaluated_operand;
8209 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8210 s->suppress_location_wrappers = suppress_location_wrappers;
8211 s->x_stmt_tree.stmts_are_full_exprs_p = true;
8213 scope_chain = s;
8214 current_function_decl = NULL_TREE;
8215 current_lang_base = NULL;
8216 current_lang_name = lang_name_cplusplus;
8217 current_namespace = global_namespace;
8218 push_class_stack ();
8219 cp_unevaluated_operand = 0;
8220 c_inhibit_evaluation_warnings = 0;
8221 suppress_location_wrappers = 0;
8224 void
8225 pop_from_top_level (void)
8227 struct saved_scope *s = scope_chain;
8228 cxx_saved_binding *saved;
8229 size_t i;
8231 auto_cond_timevar tv (TV_NAME_LOOKUP);
8233 pop_class_stack ();
8235 release_tree_vector (current_lang_base);
8237 scope_chain = s->prev;
8238 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
8240 tree id = saved->identifier;
8242 IDENTIFIER_BINDING (id) = saved->binding;
8243 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8246 /* If we were in the middle of compiling a function, restore our
8247 state. */
8248 if (s->need_pop_function_context)
8249 pop_function_context ();
8250 current_function_decl = s->function_decl;
8251 cp_unevaluated_operand = s->unevaluated_operand;
8252 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
8253 suppress_location_wrappers = s->suppress_location_wrappers;
8255 /* Make this saved_scope structure available for reuse by
8256 push_to_top_level. */
8257 s->prev = free_saved_scope;
8258 free_saved_scope = s;
8261 /* Like push_to_top_level, but not if D is function-local. Returns whether we
8262 did push to top. */
8264 bool
8265 maybe_push_to_top_level (tree d)
8267 /* Push if D isn't function-local, or is a lambda function, for which name
8268 resolution is already done. */
8269 bool push_to_top
8270 = !(current_function_decl
8271 && !LAMBDA_FUNCTION_P (d)
8272 && decl_function_context (d) == current_function_decl);
8274 if (push_to_top)
8275 push_to_top_level ();
8276 else
8278 gcc_assert (!processing_template_decl);
8279 push_function_context ();
8280 cp_unevaluated_operand = 0;
8281 c_inhibit_evaluation_warnings = 0;
8284 return push_to_top;
8287 /* Return from whatever maybe_push_to_top_level did. */
8289 void
8290 maybe_pop_from_top_level (bool push_to_top)
8292 if (push_to_top)
8293 pop_from_top_level ();
8294 else
8295 pop_function_context ();
8298 /* Push into the scope of the namespace NS, even if it is deeply
8299 nested within another namespace. */
8301 void
8302 push_nested_namespace (tree ns)
8304 auto_cond_timevar tv (TV_NAME_LOOKUP);
8305 if (ns == global_namespace)
8306 push_to_top_level ();
8307 else
8309 push_nested_namespace (CP_DECL_CONTEXT (ns));
8310 resume_scope (NAMESPACE_LEVEL (ns));
8311 current_namespace = ns;
8315 /* Pop back from the scope of the namespace NS, which was previously
8316 entered with push_nested_namespace. */
8318 void
8319 pop_nested_namespace (tree ns)
8321 auto_cond_timevar tv (TV_NAME_LOOKUP);
8322 while (ns != global_namespace)
8324 ns = CP_DECL_CONTEXT (ns);
8325 current_namespace = ns;
8326 leave_scope ();
8329 pop_from_top_level ();
8332 /* Add TARGET to USINGS, if it does not already exist there. We used
8333 to build the complete graph of usings at this point, from the POV
8334 of the source namespaces. Now we build that as we perform the
8335 unqualified search. */
8337 static void
8338 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
8340 if (usings)
8341 for (unsigned ix = usings->length (); ix--;)
8342 if ((*usings)[ix] == target)
8343 return;
8345 vec_safe_push (usings, target);
8348 /* Tell the debug system of a using directive. */
8350 static void
8351 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
8353 /* Emit debugging info. */
8354 tree context = from != global_namespace ? from : NULL_TREE;
8355 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
8356 implicit);
8359 /* Process a using directive. */
8361 void
8362 finish_using_directive (tree target, tree attribs)
8364 if (target == error_mark_node)
8365 return;
8367 if (current_binding_level->kind != sk_namespace)
8368 add_stmt (build_stmt (input_location, USING_STMT, target));
8369 else
8370 emit_debug_info_using_namespace (current_binding_level->this_entity,
8371 ORIGINAL_NAMESPACE (target), false);
8373 add_using_namespace (current_binding_level->using_directives,
8374 ORIGINAL_NAMESPACE (target));
8376 bool diagnosed = false;
8377 if (attribs != error_mark_node)
8378 for (tree a = attribs; a; a = TREE_CHAIN (a))
8380 tree name = get_attribute_name (a);
8381 if (current_binding_level->kind == sk_namespace
8382 && is_attribute_p ("strong", name))
8384 if (warning (0, "%<strong%> using directive no longer supported")
8385 && CP_DECL_CONTEXT (target) == current_namespace)
8386 inform (DECL_SOURCE_LOCATION (target),
8387 "you can use an inline namespace instead");
8389 else if ((flag_openmp || flag_openmp_simd)
8390 && get_attribute_namespace (a) == omp_identifier
8391 && (is_attribute_p ("directive", name)
8392 || is_attribute_p ("sequence", name)))
8394 if (!diagnosed)
8395 error ("%<omp::%E%> not allowed to be specified in this "
8396 "context", name);
8397 diagnosed = true;
8399 else
8400 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
8404 /* Pushes X into the global namespace. */
8406 tree
8407 pushdecl_top_level (tree x)
8409 auto_cond_timevar tv (TV_NAME_LOOKUP);
8410 push_to_top_level ();
8411 gcc_checking_assert (!DECL_CONTEXT (x));
8412 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8413 x = pushdecl_namespace_level (x);
8414 pop_from_top_level ();
8415 return x;
8418 /* Pushes X into the global namespace and calls cp_finish_decl to
8419 register the variable, initializing it with INIT. */
8421 tree
8422 pushdecl_top_level_and_finish (tree x, tree init)
8424 auto_cond_timevar tv (TV_NAME_LOOKUP);
8425 push_to_top_level ();
8426 gcc_checking_assert (!DECL_CONTEXT (x));
8427 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8428 x = pushdecl_namespace_level (x);
8429 cp_finish_decl (x, init, false, NULL_TREE, 0);
8430 pop_from_top_level ();
8431 return x;
8434 /* Enter the namespaces from current_namerspace to NS. */
8436 static int
8437 push_inline_namespaces (tree ns)
8439 int count = 0;
8440 if (ns != current_namespace)
8442 gcc_assert (ns != global_namespace);
8443 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
8444 resume_scope (NAMESPACE_LEVEL (ns));
8445 current_namespace = ns;
8446 count++;
8448 return count;
8451 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8452 Reuse or create a namespace NAME. NAME is null for the anonymous
8453 namespace. */
8455 static tree
8456 reuse_namespace (tree *slot, tree ctx, tree name)
8458 if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
8460 /* Public namespace. Shared. */
8461 tree *global_slot = slot;
8462 if (TREE_CODE (*slot) == BINDING_VECTOR)
8463 global_slot = get_fixed_binding_slot (slot, name,
8464 BINDING_SLOT_GLOBAL, false);
8466 for (ovl_iterator iter (*global_slot); iter; ++iter)
8468 tree decl = *iter;
8470 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
8471 return decl;
8474 return NULL_TREE;
8477 static tree
8478 make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
8480 /* Create the namespace. */
8481 tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
8482 DECL_SOURCE_LOCATION (ns) = loc;
8483 SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
8484 if (!SCOPE_DEPTH (ns))
8485 /* We only allow depth 255. */
8486 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
8487 DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
8489 if (!name)
8490 /* Anon-namespaces in different header-unit imports are distinct.
8491 But that's ok as their contents all have internal linkage.
8492 (This is different to how they'd behave as textual includes,
8493 but doing this at all is really odd source.) */
8494 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
8495 else if (TREE_PUBLIC (ctx))
8496 TREE_PUBLIC (ns) = true;
8498 if (inline_p)
8499 DECL_NAMESPACE_INLINE_P (ns) = true;
8501 return ns;
8504 /* NS was newly created, finish off making it. */
8506 static void
8507 make_namespace_finish (tree ns, tree *slot, bool from_import = false)
8509 if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
8511 /* Merge into global slot. */
8512 tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
8513 BINDING_SLOT_GLOBAL, true);
8514 *gslot = ns;
8517 tree ctx = CP_DECL_CONTEXT (ns);
8518 cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
8519 scope->this_entity = ns;
8520 scope->more_cleanups_ok = true;
8521 scope->kind = sk_namespace;
8522 scope->level_chain = NAMESPACE_LEVEL (ctx);
8523 NAMESPACE_LEVEL (ns) = scope;
8525 if (DECL_NAMESPACE_INLINE_P (ns))
8526 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
8528 if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
8529 emit_debug_info_using_namespace (ctx, ns, true);
8532 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
8533 then we enter an anonymous namespace. If MAKE_INLINE is true, then
8534 we create an inline namespace (it is up to the caller to check upon
8535 redefinition). Return the number of namespaces entered. */
8538 push_namespace (tree name, bool make_inline)
8540 auto_cond_timevar tv (TV_NAME_LOOKUP);
8541 int count = 0;
8543 /* We should not get here if the global_namespace is not yet constructed
8544 nor if NAME designates the global namespace: The global scope is
8545 constructed elsewhere. */
8546 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
8548 tree ns = NULL_TREE;
8550 name_lookup lookup (name);
8551 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
8553 else if (TREE_CODE (lookup.value) == TREE_LIST)
8555 /* An ambiguous lookup. If exactly one is a namespace, we
8556 want that. If more than one is a namespace, error, but
8557 pick one of them. */
8558 /* DR2061 can cause us to find multiple namespaces of the same
8559 name. We must treat that carefully and avoid thinking we
8560 need to push a new (possibly) duplicate namespace. Hey,
8561 if you want to use the same identifier within an inline
8562 nest, knock yourself out. */
8563 for (tree *chain = &lookup.value, next; (next = *chain);)
8565 tree decl = TREE_VALUE (next);
8566 if (TREE_CODE (decl) == NAMESPACE_DECL)
8568 if (!ns)
8569 ns = decl;
8570 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
8571 ns = decl;
8573 /* Advance. */
8574 chain = &TREE_CHAIN (next);
8576 else
8577 /* Stitch out. */
8578 *chain = TREE_CHAIN (next);
8581 if (TREE_CHAIN (lookup.value))
8583 error ("%<namespace %E%> is ambiguous", name);
8584 print_candidates (lookup.value);
8587 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
8588 ns = lookup.value;
8590 if (ns)
8591 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
8593 /* A namespace alias is not allowed here, but if the alias
8594 is for a namespace also inside the current scope,
8595 accept it with a diagnostic. That's better than dying
8596 horribly. */
8597 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
8599 error ("namespace alias %qD not allowed here, "
8600 "assuming %qD", ns, dna);
8601 ns = dna;
8603 else
8604 ns = NULL_TREE;
8608 if (ns)
8610 /* DR2061. NS might be a member of an inline namespace. We
8611 need to push into those namespaces. */
8612 if (modules_p ())
8614 for (tree parent, ctx = ns; ctx != current_namespace;
8615 ctx = parent)
8617 parent = CP_DECL_CONTEXT (ctx);
8619 tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
8620 if (bind != ctx)
8622 auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
8623 binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
8624 gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
8625 slot = ctx;
8630 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
8631 if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
8632 /* It's not builtin now. */
8633 DECL_SOURCE_LOCATION (ns) = input_location;
8635 else
8637 /* Before making a new namespace, see if we already have one in
8638 the existing partitions of the current namespace. */
8639 tree *slot = find_namespace_slot (current_namespace, name, false);
8640 if (slot)
8641 ns = reuse_namespace (slot, current_namespace, name);
8642 if (!ns)
8643 ns = make_namespace (current_namespace, name,
8644 input_location, make_inline);
8646 if (pushdecl (ns) == error_mark_node)
8647 ns = NULL_TREE;
8648 else
8650 /* Finish up making the namespace. */
8651 add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
8652 if (!slot)
8654 slot = find_namespace_slot (current_namespace, name);
8655 /* This should find the slot created by pushdecl. */
8656 gcc_checking_assert (slot && *slot == ns);
8658 else
8660 /* pushdecl could have expanded the hash table, so
8661 slot might be invalid. */
8662 slot = find_namespace_slot (current_namespace, name);
8663 gcc_checking_assert (slot);
8665 make_namespace_finish (ns, slot);
8667 /* Add the anon using-directive here, we don't do it in
8668 make_namespace_finish. */
8669 if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
8670 add_using_namespace (current_binding_level->using_directives, ns);
8674 if (ns)
8676 /* A public namespace is exported only if explicitly marked, or
8677 it contains exported entities. */
8678 if (TREE_PUBLIC (ns) && module_exporting_p ())
8679 DECL_MODULE_EXPORT_P (ns) = true;
8680 if (module_purview_p ())
8681 DECL_MODULE_PURVIEW_P (ns) = true;
8683 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
8685 error_at (input_location,
8686 "inline namespace must be specified at initial definition");
8687 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
8689 resume_scope (NAMESPACE_LEVEL (ns));
8690 current_namespace = ns;
8691 count++;
8694 return count;
8697 /* Pop from the scope of the current namespace. */
8699 void
8700 pop_namespace (void)
8702 auto_cond_timevar tv (TV_NAME_LOOKUP);
8704 gcc_assert (current_namespace != global_namespace);
8705 current_namespace = CP_DECL_CONTEXT (current_namespace);
8706 /* The binding level is not popped, as it might be re-opened later. */
8707 leave_scope ();
8710 /* An IMPORT is an import that is defining namespace NAME inside CTX. Find or
8711 create that namespace and add it to the container's binding-vector. */
8713 tree
8714 add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
8715 bool inline_p, bool visible_p)
8717 // FIXME: Something is not correct about the VISIBLE_P handling. We
8718 // need to insert this namespace into
8719 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
8720 // (b) The importing module's slot (always)
8721 // (c) Do we need to put it in the CURRENT slot? This is the
8722 // confused piece.
8724 tree *slot = find_namespace_slot (ctx, name, true);
8725 tree decl = reuse_namespace (slot, ctx, name);
8727 /* Creating and binding. */
8728 if (!decl)
8730 decl = make_namespace (ctx, name, loc, inline_p);
8731 DECL_MODULE_IMPORT_P (decl) = true;
8732 make_namespace_finish (decl, slot, true);
8734 else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
8736 error_at (loc, "%s namespace %qD conflicts with reachable definition",
8737 inline_p ? "inline" : "non-inline", decl);
8738 inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
8739 inline_p ? "non-inline" : "inline");
8742 if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
8744 /* See if we can extend the final slot. */
8745 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
8746 gcc_checking_assert (last->indices[0].span);
8747 unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
8749 while (--jx)
8750 if (last->indices[jx].span)
8751 break;
8752 tree final = last->slots[jx];
8753 if (visible_p == !STAT_HACK_P (final)
8754 && MAYBE_STAT_DECL (final) == decl
8755 && last->indices[jx].base + last->indices[jx].span == import
8756 && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
8757 || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
8758 && jx >= BINDING_SLOTS_FIXED)))
8760 last->indices[jx].span++;
8761 return decl;
8765 /* Append a new slot. */
8766 tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
8768 gcc_assert (!*mslot);
8769 *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
8771 return decl;
8774 /* Pop off extraneous binding levels left over due to syntax errors.
8775 We don't pop past namespaces, as they might be valid. */
8777 void
8778 pop_everything (void)
8780 if (ENABLE_SCOPE_CHECKING)
8781 verbatim ("XXX entering %<pop_everything ()%>");
8782 while (!namespace_bindings_p ())
8784 if (current_binding_level->kind == sk_class)
8785 pop_nested_class ();
8786 else
8787 poplevel (0, 0, 0);
8789 if (ENABLE_SCOPE_CHECKING)
8790 verbatim ("XXX leaving %<pop_everything ()%>");
8793 /* Emit debugging information for using declarations and directives.
8794 If input tree is overloaded fn then emit debug info for all
8795 candidates. */
8797 void
8798 cp_emit_debug_info_for_using (tree t, tree context)
8800 /* Don't try to emit any debug information if we have errors. */
8801 if (seen_error ())
8802 return;
8804 /* Do not supply context to imported_module_or_decl, if
8805 it is a global namespace. */
8806 if (context == global_namespace)
8807 context = NULL_TREE;
8809 t = MAYBE_BASELINK_FUNCTIONS (t);
8811 for (lkp_iterator iter (t); iter; ++iter)
8813 tree fn = *iter;
8815 if (TREE_CODE (fn) == TEMPLATE_DECL)
8816 /* FIXME: Handle TEMPLATE_DECLs. */
8817 continue;
8819 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
8820 of a builtin function. */
8821 if (TREE_CODE (fn) == FUNCTION_DECL
8822 && DECL_EXTERNAL (fn)
8823 && fndecl_built_in_p (fn))
8824 continue;
8826 if (building_stmt_list_p ())
8827 add_stmt (build_stmt (input_location, USING_STMT, fn));
8828 else
8829 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
8830 false, false);
8834 /* True if D is a local declaration in dependent scope. Assumes that it is
8835 (part of) the current lookup result for its name. */
8837 bool
8838 dependent_local_decl_p (tree d)
8840 if (!DECL_LOCAL_DECL_P (d))
8841 return false;
8843 cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
8844 cp_binding_level *l = b->scope;
8845 while (!l->this_entity)
8846 l = l->level_chain;
8847 return uses_template_parms (l->this_entity);
8852 #include "gt-cp-name-lookup.h"