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