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

source code of gcc/cp/name-lookup.cc