Debian Codegraph @

gcc/cp/name-lookup.cc

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