c++: Stat-hack for members [PR 98530]
[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 (!found)
3930 ;
3931 else if (STAT_HACK_P (found))
3932 /* Rearrange the stat hack so that we don't need to expose that
3933 internal detail. */
3934 found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
3935 else if (IDENTIFIER_CONV_OP_P (name))
3936 {
3937 gcc_checking_assert (name == conv_op_identifier);
3938 found = OVL_CHAIN (found);
3939 }
3940 }
3941 else
3942 {
3943 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
3944 || TYPE_PTRMEMFUNC_P (klass));
3945 found = fields_linear_search (klass, name, false);
3946 }
3947
3948 return found;
3949 }
3950
3951 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
3952 for all decls of the current module. When partitions are involved,
3953 decls might be mentioned more than once. */
3954
3955 unsigned
3956 walk_module_binding (tree binding, bitmap partitions,
3957 bool (*callback) (tree decl, WMB_Flags, void *data),
3958 void *data)
3959 {
3960 // FIXME: We don't quite deal with using decls naming stat hack
3961 // type. Also using decls exporting something from the same scope.
3962 tree current = binding;
3963 unsigned count = 0;
3964
3965 if (TREE_CODE (binding) == BINDING_VECTOR)
3966 current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
3967
3968 bool decl_hidden = false;
3969 if (tree type = MAYBE_STAT_TYPE (current))
3970 {
3971 WMB_Flags flags = WMB_None;
3972 if (STAT_TYPE_HIDDEN_P (current))
3973 flags = WMB_Flags (flags | WMB_Hidden);
3974 count += callback (type, flags, data);
3975 decl_hidden = STAT_DECL_HIDDEN_P (current);
3976 }
3977
3978 for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
3979 {
3980 if (iter.hidden_p ())
3981 decl_hidden = true;
3982 if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
3983 {
3984 WMB_Flags flags = WMB_None;
3985 if (decl_hidden)
3986 flags = WMB_Flags (flags | WMB_Hidden);
3987 if (iter.using_p ())
3988 {
3989 flags = WMB_Flags (flags | WMB_Using);
3990 if (iter.exporting_p ())
3991 flags = WMB_Flags (flags | WMB_Export);
3992 }
3993 count += callback (*iter, flags, data);
3994 }
3995 decl_hidden = false;
3996 }
3997
3998 if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
3999 {
4000 /* Process partition slots. */
4001 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
4002 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
4003 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
4004 {
4005 ix--;
4006 cluster++;
4007 }
4008
4009 bool maybe_dups = BINDING_VECTOR_PARTITION_DUPS_P (binding);
4010
4011 for (; ix--; cluster++)
4012 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
4013 if (!cluster->slots[jx].is_lazy ())
4014 if (tree bind = cluster->slots[jx])
4015 {
4016 if (TREE_CODE (bind) == NAMESPACE_DECL
4017 && !DECL_NAMESPACE_ALIAS (bind))
4018 {
4019 if (unsigned base = cluster->indices[jx].base)
4020 if (unsigned span = cluster->indices[jx].span)
4021 do
4022 if (bitmap_bit_p (partitions, base))
4023 goto found;
4024 while (++base, --span);
4025 /* Not a partition's namespace. */
4026 continue;
4027 found:
4028
4029 WMB_Flags flags = WMB_None;
4030 if (maybe_dups)
4031 flags = WMB_Flags (flags | WMB_Dups);
4032 count += callback (bind, flags, data);
4033 }
4034 else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
4035 {
4036 if (tree btype = STAT_TYPE (bind))
4037 {
4038 WMB_Flags flags = WMB_None;
4039 if (maybe_dups)
4040 flags = WMB_Flags (flags | WMB_Dups);
4041 if (STAT_TYPE_HIDDEN_P (bind))
4042 flags = WMB_Flags (flags | WMB_Hidden);
4043
4044 count += callback (btype, flags, data);
4045 }
4046 bool hidden = STAT_DECL_HIDDEN_P (bind);
4047 for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
4048 iter; ++iter)
4049 {
4050 if (iter.hidden_p ())
4051 hidden = true;
4052 gcc_checking_assert
4053 (!(hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
4054
4055 WMB_Flags flags = WMB_None;
4056 if (maybe_dups)
4057 flags = WMB_Flags (flags | WMB_Dups);
4058 if (decl_hidden)
4059 flags = WMB_Flags (flags | WMB_Hidden);
4060 if (iter.using_p ())
4061 {
4062 flags = WMB_Flags (flags | WMB_Using);
4063 if (iter.exporting_p ())
4064 flags = WMB_Flags (flags | WMB_Export);
4065 }
4066 count += callback (*iter, flags, data);
4067 hidden = false;
4068 }
4069 }
4070 }
4071 }
4072
4073 return count;
4074 }
4075
4076 /* Imported module MOD has a binding to NS::NAME, stored in section
4077 SNUM. */
4078
4079 bool
4080 import_module_binding (tree ns, tree name, unsigned mod, unsigned snum)
4081 {
4082 tree *slot = find_namespace_slot (ns, name, true);
4083 binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
4084
4085 if (mslot->is_lazy () || *mslot)
4086 /* Oops, something was already there. */
4087 return false;
4088
4089 mslot->set_lazy (snum);
4090 return true;
4091 }
4092
4093 /* An import of MODULE is binding NS::NAME. There should be no
4094 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4095 is a header_unit (-1) or part of the current module (+1). VALUE
4096 and TYPE are the value and type bindings. VISIBLE are the value
4097 bindings being exported. */
4098
4099 bool
4100 set_module_binding (tree ns, tree name, unsigned mod, int mod_glob,
4101 tree value, tree type, tree visible)
4102 {
4103 if (!value)
4104 /* Bogus BMIs could give rise to nothing to bind. */
4105 return false;
4106
4107 gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
4108 || DECL_NAMESPACE_ALIAS (value));
4109 gcc_checking_assert (mod);
4110
4111 tree *slot = find_namespace_slot (ns, name, true);
4112 binding_slot *mslot = search_imported_binding_slot (slot, mod);
4113
4114 if (!mslot || !mslot->is_lazy ())
4115 /* Again, bogus BMI could give find to missing or already loaded slot. */
4116 return false;
4117
4118 tree bind = value;
4119 if (type || visible != bind || mod_glob)
4120 {
4121 bind = stat_hack (bind, type);
4122 STAT_VISIBLE (bind) = visible;
4123 if ((mod_glob > 0 && TREE_PUBLIC (ns))
4124 || (type && DECL_MODULE_EXPORT_P (type)))
4125 STAT_TYPE_VISIBLE_P (bind) = true;
4126 }
4127
4128 /* Note if this is this-module or global binding. */
4129 if (mod_glob > 0)
4130 MODULE_BINDING_PARTITION_P (bind) = true;
4131 else if (mod_glob < 0)
4132 MODULE_BINDING_GLOBAL_P (bind) = true;
4133
4134 *mslot = bind;
4135
4136 return true;
4137 }
4138
4139 void
4140 note_pending_specializations (tree ns, tree name, bool is_header)
4141 {
4142 if (tree *slot = find_namespace_slot (ns, name, false))
4143 if (TREE_CODE (*slot) == BINDING_VECTOR)
4144 {
4145 tree vec = *slot;
4146 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec) = true;
4147 if (is_header)
4148 BINDING_VECTOR_PENDING_IS_HEADER_P (vec) = true;
4149 else
4150 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec) = true;
4151 }
4152 }
4153
4154 void
4155 load_pending_specializations (tree ns, tree name)
4156 {
4157 tree *slot = find_namespace_slot (ns, name, false);
4158
4159 if (!slot || TREE_CODE (*slot) != BINDING_VECTOR
4160 || !BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (*slot))
4161 return;
4162
4163 tree vec = *slot;
4164 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec) = false;
4165
4166 bool do_header = BINDING_VECTOR_PENDING_IS_HEADER_P (vec);
4167 bool do_partition = BINDING_VECTOR_PENDING_IS_PARTITION_P (vec);
4168 BINDING_VECTOR_PENDING_IS_HEADER_P (vec) = false;
4169 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec) = false;
4170
4171 gcc_checking_assert (do_header | do_partition);
4172 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (vec);
4173 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (vec);
4174 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
4175 {
4176 ix--;
4177 cluster++;
4178 }
4179
4180 for (; ix--; cluster++)
4181 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
4182 if (cluster->indices[jx].span
4183 && cluster->slots[jx].is_lazy ()
4184 && lazy_specializations_p (cluster->indices[jx].base,
4185 do_header, do_partition))
4186 lazy_load_binding (cluster->indices[jx].base, ns, name,
4187 &cluster->slots[jx]);
4188 }
4189
4190 void
4191 add_module_decl (tree ns, tree name, tree decl)
4192 {
4193 gcc_assert (!DECL_CHAIN (decl));
4194 add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
4195 newbinding_bookkeeping (name, decl, NAMESPACE_LEVEL (ns));
4196 }
4197
4198 /* Enter DECL into the symbol table, if that's appropriate. Returns
4199 DECL, or a modified version thereof. */
4200
4201 tree
4202 maybe_push_decl (tree decl)
4203 {
4204 tree type = TREE_TYPE (decl);
4205
4206 /* Add this decl to the current binding level, but not if it comes
4207 from another scope, e.g. a static member variable. TEM may equal
4208 DECL or it may be a previous decl of the same name. */
4209 if (decl == error_mark_node
4210 || (TREE_CODE (decl) != PARM_DECL
4211 && DECL_CONTEXT (decl) != NULL_TREE
4212 /* Definitions of namespace members outside their namespace are
4213 possible. */
4214 && !DECL_NAMESPACE_SCOPE_P (decl))
4215 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4216 || type == unknown_type_node
4217 /* The declaration of a template specialization does not affect
4218 the functions available for overload resolution, so we do not
4219 call pushdecl. */
4220 || (TREE_CODE (decl) == FUNCTION_DECL
4221 && DECL_TEMPLATE_SPECIALIZATION (decl)))
4222 return decl;
4223 else
4224 return pushdecl (decl);
4225 }
4226
4227 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4228 binding level. If IS_USING is true, DECL got here through a
4229 using-declaration. */
4230
4231 static void
4232 push_local_binding (tree id, tree decl, bool is_using)
4233 {
4234 /* Skip over any local classes. This makes sense if we call
4235 push_local_binding with a friend decl of a local class. */
4236 cp_binding_level *b = innermost_nonclass_level ();
4237
4238 gcc_assert (b->kind != sk_namespace);
4239 if (find_local_binding (b, id))
4240 {
4241 /* Supplement the existing binding. */
4242 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
4243 /* It didn't work. Something else must be bound at this
4244 level. Do not add DECL to the list of things to pop
4245 later. */
4246 return;
4247 }
4248 else
4249 /* Create a new binding. */
4250 push_binding (id, decl, b);
4251
4252 if (TREE_CODE (decl) == OVERLOAD || is_using)
4253 /* We must put the OVERLOAD or using into a TREE_LIST since we
4254 cannot use the decl's chain itself. */
4255 decl = build_tree_list (id, decl);
4256
4257 /* And put DECL on the list of things declared by the current
4258 binding level. */
4259 add_decl_to_level (b, decl);
4260 }
4261
4262 \f
4263 /* true means unconditionally make a BLOCK for the next level pushed. */
4264
4265 static bool keep_next_level_flag;
4266
4267 static int binding_depth = 0;
4268
4269 static void
4270 indent (int depth)
4271 {
4272 int i;
4273
4274 for (i = 0; i < depth * 2; i++)
4275 putc (' ', stderr);
4276 }
4277
4278 /* Return a string describing the kind of SCOPE we have. */
4279 static const char *
4280 cp_binding_level_descriptor (cp_binding_level *scope)
4281 {
4282 /* The order of this table must match the "scope_kind"
4283 enumerators. */
4284 static const char* scope_kind_names[] = {
4285 "block-scope",
4286 "cleanup-scope",
4287 "try-scope",
4288 "catch-scope",
4289 "for-scope",
4290 "function-parameter-scope",
4291 "class-scope",
4292 "namespace-scope",
4293 "template-parameter-scope",
4294 "template-explicit-spec-scope"
4295 };
4296 const scope_kind kind = scope->explicit_spec_p
4297 ? sk_template_spec : scope->kind;
4298
4299 return scope_kind_names[kind];
4300 }
4301
4302 /* Output a debugging information about SCOPE when performing
4303 ACTION at LINE. */
4304 static void
4305 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
4306 {
4307 const char *desc = cp_binding_level_descriptor (scope);
4308 if (scope->this_entity)
4309 verbatim ("%s %<%s(%E)%> %p %d", action, desc,
4310 scope->this_entity, (void *) scope, line);
4311 else
4312 verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
4313 }
4314
4315 /* A chain of binding_level structures awaiting reuse. */
4316
4317 static GTY((deletable)) cp_binding_level *free_binding_level;
4318
4319 /* Insert SCOPE as the innermost binding level. */
4320
4321 void
4322 push_binding_level (cp_binding_level *scope)
4323 {
4324 /* Add it to the front of currently active scopes stack. */
4325 scope->level_chain = current_binding_level;
4326 current_binding_level = scope;
4327 keep_next_level_flag = false;
4328
4329 if (ENABLE_SCOPE_CHECKING)
4330 {
4331 scope->binding_depth = binding_depth;
4332 indent (binding_depth);
4333 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4334 "push");
4335 binding_depth++;
4336 }
4337 }
4338
4339 /* Create a new KIND scope and make it the top of the active scopes stack.
4340 ENTITY is the scope of the associated C++ entity (namespace, class,
4341 function, C++0x enumeration); it is NULL otherwise. */
4342
4343 cp_binding_level *
4344 begin_scope (scope_kind kind, tree entity)
4345 {
4346 cp_binding_level *scope;
4347
4348 /* Reuse or create a struct for this binding level. */
4349 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
4350 {
4351 scope = free_binding_level;
4352 free_binding_level = scope->level_chain;
4353 memset (scope, 0, sizeof (cp_binding_level));
4354 }
4355 else
4356 scope = ggc_cleared_alloc<cp_binding_level> ();
4357
4358 scope->this_entity = entity;
4359 scope->more_cleanups_ok = true;
4360 switch (kind)
4361 {
4362 case sk_cleanup:
4363 scope->keep = true;
4364 break;
4365
4366 case sk_template_spec:
4367 scope->explicit_spec_p = true;
4368 kind = sk_template_parms;
4369 /* Fall through. */
4370 case sk_template_parms:
4371 case sk_block:
4372 case sk_try:
4373 case sk_catch:
4374 case sk_for:
4375 case sk_cond:
4376 case sk_class:
4377 case sk_scoped_enum:
4378 case sk_transaction:
4379 case sk_omp:
4380 scope->keep = keep_next_level_flag;
4381 break;
4382
4383 case sk_function_parms:
4384 scope->keep = keep_next_level_flag;
4385 if (entity)
4386 scope->immediate_fn_ctx_p = DECL_IMMEDIATE_FUNCTION_P (entity);
4387 break;
4388
4389 case sk_namespace:
4390 NAMESPACE_LEVEL (entity) = scope;
4391 break;
4392
4393 default:
4394 /* Should not happen. */
4395 gcc_unreachable ();
4396 break;
4397 }
4398 scope->kind = kind;
4399
4400 push_binding_level (scope);
4401
4402 return scope;
4403 }
4404
4405 /* We're about to leave current scope. Pop the top of the stack of
4406 currently active scopes. Return the enclosing scope, now active. */
4407
4408 cp_binding_level *
4409 leave_scope (void)
4410 {
4411 cp_binding_level *scope = current_binding_level;
4412
4413 if (scope->kind == sk_namespace && class_binding_level)
4414 current_binding_level = class_binding_level;
4415
4416 /* We cannot leave a scope, if there are none left. */
4417 if (NAMESPACE_LEVEL (global_namespace))
4418 gcc_assert (!global_scope_p (scope));
4419
4420 if (ENABLE_SCOPE_CHECKING)
4421 {
4422 indent (--binding_depth);
4423 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4424 "leave");
4425 }
4426
4427 /* Move one nesting level up. */
4428 current_binding_level = scope->level_chain;
4429
4430 /* Namespace-scopes are left most probably temporarily, not
4431 completely; they can be reopened later, e.g. in namespace-extension
4432 or any name binding activity that requires us to resume a
4433 namespace. For classes, we cache some binding levels. For other
4434 scopes, we just make the structure available for reuse. */
4435 if (scope->kind != sk_namespace
4436 && scope != previous_class_level)
4437 {
4438 scope->level_chain = free_binding_level;
4439 gcc_assert (!ENABLE_SCOPE_CHECKING
4440 || scope->binding_depth == binding_depth);
4441 free_binding_level = scope;
4442 }
4443
4444 if (scope->kind == sk_class)
4445 {
4446 /* Reset DEFINING_CLASS_P to allow for reuse of a
4447 class-defining scope in a non-defining context. */
4448 scope->defining_class_p = 0;
4449
4450 /* Find the innermost enclosing class scope, and reset
4451 CLASS_BINDING_LEVEL appropriately. */
4452 class_binding_level = NULL;
4453 for (scope = current_binding_level; scope; scope = scope->level_chain)
4454 if (scope->kind == sk_class)
4455 {
4456 class_binding_level = scope;
4457 break;
4458 }
4459 }
4460
4461 return current_binding_level;
4462 }
4463
4464 /* When we exit a toplevel class scope, we save its binding level so
4465 that we can restore it quickly. Here, we've entered some other
4466 class, so we must invalidate our cache. */
4467
4468 void
4469 invalidate_class_lookup_cache (void)
4470 {
4471 previous_class_level->level_chain = free_binding_level;
4472 free_binding_level = previous_class_level;
4473 previous_class_level = NULL;
4474 }
4475
4476 static void
4477 resume_scope (cp_binding_level* b)
4478 {
4479 /* Resuming binding levels is meant only for namespaces,
4480 and those cannot nest into classes. */
4481 gcc_assert (!class_binding_level);
4482 /* Also, resuming a non-directly nested namespace is a no-no. */
4483 gcc_assert (b->level_chain == current_binding_level);
4484 current_binding_level = b;
4485 if (ENABLE_SCOPE_CHECKING)
4486 {
4487 b->binding_depth = binding_depth;
4488 indent (binding_depth);
4489 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
4490 binding_depth++;
4491 }
4492 }
4493
4494 /* Return the innermost binding level that is not for a class scope. */
4495
4496 static cp_binding_level *
4497 innermost_nonclass_level (void)
4498 {
4499 cp_binding_level *b;
4500
4501 b = current_binding_level;
4502 while (b->kind == sk_class)
4503 b = b->level_chain;
4504
4505 return b;
4506 }
4507
4508 /* We're defining an object of type TYPE. If it needs a cleanup, but
4509 we're not allowed to add any more objects with cleanups to the current
4510 scope, create a new binding level. */
4511
4512 void
4513 maybe_push_cleanup_level (tree type)
4514 {
4515 if (type != error_mark_node
4516 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4517 && current_binding_level->more_cleanups_ok == 0)
4518 {
4519 begin_scope (sk_cleanup, NULL);
4520 current_binding_level->statement_list = push_stmt_list ();
4521 }
4522 }
4523
4524 /* Return true if we are in the global binding level. */
4525
4526 bool
4527 global_bindings_p (void)
4528 {
4529 return global_scope_p (current_binding_level);
4530 }
4531
4532 /* True if we are currently in a toplevel binding level. This
4533 means either the global binding level or a namespace in a toplevel
4534 binding level. Since there are no non-toplevel namespace levels,
4535 this really means any namespace or template parameter level. We
4536 also include a class whose context is toplevel. */
4537
4538 bool
4539 toplevel_bindings_p (void)
4540 {
4541 cp_binding_level *b = innermost_nonclass_level ();
4542
4543 return b->kind == sk_namespace || b->kind == sk_template_parms;
4544 }
4545
4546 /* True if this is a namespace scope, or if we are defining a class
4547 which is itself at namespace scope, or whose enclosing class is
4548 such a class, etc. */
4549
4550 bool
4551 namespace_bindings_p (void)
4552 {
4553 cp_binding_level *b = innermost_nonclass_level ();
4554
4555 return b->kind == sk_namespace;
4556 }
4557
4558 /* True if the innermost non-class scope is a block scope. */
4559
4560 bool
4561 local_bindings_p (void)
4562 {
4563 cp_binding_level *b = innermost_nonclass_level ();
4564 return b->kind < sk_function_parms || b->kind == sk_omp;
4565 }
4566
4567 /* True if the current level needs to have a BLOCK made. */
4568
4569 bool
4570 kept_level_p (void)
4571 {
4572 return (current_binding_level->blocks != NULL_TREE
4573 || current_binding_level->keep
4574 || current_binding_level->kind == sk_cleanup
4575 || current_binding_level->names != NULL_TREE
4576 || current_binding_level->using_directives);
4577 }
4578
4579 /* Returns the kind of the innermost scope. */
4580
4581 scope_kind
4582 innermost_scope_kind (void)
4583 {
4584 return current_binding_level->kind;
4585 }
4586
4587 /* Returns true if this scope was created to store template parameters. */
4588
4589 bool
4590 template_parm_scope_p (void)
4591 {
4592 return innermost_scope_kind () == sk_template_parms;
4593 }
4594
4595 /* If KEEP is true, make a BLOCK node for the next binding level,
4596 unconditionally. Otherwise, use the normal logic to decide whether
4597 or not to create a BLOCK. */
4598
4599 void
4600 keep_next_level (bool keep)
4601 {
4602 keep_next_level_flag = keep;
4603 }
4604
4605 /* Return the list of declarations of the current local scope. */
4606
4607 tree
4608 get_local_decls (void)
4609 {
4610 gcc_assert (current_binding_level->kind != sk_namespace
4611 && current_binding_level->kind != sk_class);
4612 return current_binding_level->names;
4613 }
4614
4615 /* Return how many function prototypes we are currently nested inside. */
4616
4617 int
4618 function_parm_depth (void)
4619 {
4620 int level = 0;
4621 cp_binding_level *b;
4622
4623 for (b = current_binding_level;
4624 b->kind == sk_function_parms;
4625 b = b->level_chain)
4626 ++level;
4627
4628 return level;
4629 }
4630
4631 /* For debugging. */
4632 static int no_print_functions = 0;
4633 static int no_print_builtins = 0;
4634
4635 static void
4636 print_binding_level (cp_binding_level* lvl)
4637 {
4638 tree t;
4639 int i = 0, len;
4640 if (lvl->this_entity)
4641 print_node_brief (stderr, "entity=", lvl->this_entity, 1);
4642 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
4643 if (lvl->more_cleanups_ok)
4644 fprintf (stderr, " more-cleanups-ok");
4645 if (lvl->have_cleanups)
4646 fprintf (stderr, " have-cleanups");
4647 fprintf (stderr, "\n");
4648 if (lvl->names)
4649 {
4650 fprintf (stderr, " names:\t");
4651 /* We can probably fit 3 names to a line? */
4652 for (t = lvl->names; t; t = TREE_CHAIN (t))
4653 {
4654 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
4655 continue;
4656 if (no_print_builtins
4657 && (TREE_CODE (t) == TYPE_DECL)
4658 && DECL_IS_UNDECLARED_BUILTIN (t))
4659 continue;
4660
4661 /* Function decls tend to have longer names. */
4662 if (TREE_CODE (t) == FUNCTION_DECL)
4663 len = 3;
4664 else
4665 len = 2;
4666 i += len;
4667 if (i > 6)
4668 {
4669 fprintf (stderr, "\n\t");
4670 i = len;
4671 }
4672 print_node_brief (stderr, "", t, 0);
4673 if (t == error_mark_node)
4674 break;
4675 }
4676 if (i)
4677 fprintf (stderr, "\n");
4678 }
4679 if (vec_safe_length (lvl->class_shadowed))
4680 {
4681 size_t i;
4682 cp_class_binding *b;
4683 fprintf (stderr, " class-shadowed:");
4684 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
4685 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
4686 fprintf (stderr, "\n");
4687 }
4688 if (lvl->type_shadowed)
4689 {
4690 fprintf (stderr, " type-shadowed:");
4691 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
4692 {
4693 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
4694 }
4695 fprintf (stderr, "\n");
4696 }
4697 }
4698
4699 DEBUG_FUNCTION void
4700 debug (cp_binding_level &ref)
4701 {
4702 print_binding_level (&ref);
4703 }
4704
4705 DEBUG_FUNCTION void
4706 debug (cp_binding_level *ptr)
4707 {
4708 if (ptr)
4709 debug (*ptr);
4710 else
4711 fprintf (stderr, "<nil>\n");
4712 }
4713
4714 static void
4715 print_other_binding_stack (cp_binding_level *stack)
4716 {
4717 cp_binding_level *level;
4718 for (level = stack; !global_scope_p (level); level = level->level_chain)
4719 {
4720 fprintf (stderr, "binding level %p\n", (void *) level);
4721 print_binding_level (level);
4722 }
4723 }
4724
4725 DEBUG_FUNCTION void
4726 print_binding_stack (void)
4727 {
4728 cp_binding_level *b;
4729 fprintf (stderr, "current_binding_level=%p\n"
4730 "class_binding_level=%p\n"
4731 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4732 (void *) current_binding_level, (void *) class_binding_level,
4733 (void *) NAMESPACE_LEVEL (global_namespace));
4734 if (class_binding_level)
4735 {
4736 for (b = class_binding_level; b; b = b->level_chain)
4737 if (b == current_binding_level)
4738 break;
4739 if (b)
4740 b = class_binding_level;
4741 else
4742 b = current_binding_level;
4743 }
4744 else
4745 b = current_binding_level;
4746 print_other_binding_stack (b);
4747 fprintf (stderr, "global:\n");
4748 print_binding_level (NAMESPACE_LEVEL (global_namespace));
4749 }
4750 \f
4751 /* Return the type associated with ID. */
4752
4753 static tree
4754 identifier_type_value_1 (tree id)
4755 {
4756 /* There is no type with that name, anywhere. */
4757 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
4758 return NULL_TREE;
4759 /* This is not the type marker, but the real thing. */
4760 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
4761 return REAL_IDENTIFIER_TYPE_VALUE (id);
4762 /* Have to search for it. It must be on the global level, now.
4763 Ask lookup_name not to return non-types. */
4764 id = lookup_name (id, LOOK_where::BLOCK_NAMESPACE, LOOK_want::TYPE);
4765 if (id)
4766 return TREE_TYPE (id);
4767 return NULL_TREE;
4768 }
4769
4770 /* Wrapper for identifier_type_value_1. */
4771
4772 tree
4773 identifier_type_value (tree id)
4774 {
4775 tree ret;
4776 timevar_start (TV_NAME_LOOKUP);
4777 ret = identifier_type_value_1 (id);
4778 timevar_stop (TV_NAME_LOOKUP);
4779 return ret;
4780 }
4781
4782 /* Push a definition of struct, union or enum tag named ID. into
4783 binding_level B. DECL is a TYPE_DECL for the type. DECL has
4784 already been pushed into its binding level. This is bookkeeping to
4785 find it easily. */
4786
4787 static void
4788 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
4789 {
4790 tree type;
4791
4792 if (b->kind != sk_namespace)
4793 {
4794 /* Shadow the marker, not the real thing, so that the marker
4795 gets restored later. */
4796 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4797 b->type_shadowed = tree_cons (id, old_type_value, b->type_shadowed);
4798 type = decl ? TREE_TYPE (decl) : NULL_TREE;
4799 TREE_TYPE (b->type_shadowed) = type;
4800 }
4801 else
4802 {
4803 gcc_assert (decl);
4804
4805 /* Store marker instead of real type. */
4806 type = global_type_node;
4807 }
4808
4809 SET_IDENTIFIER_TYPE_VALUE (id, type);
4810 }
4811
4812 /* As set_identifier_type_value_with_scope, but using
4813 current_binding_level. */
4814
4815 void
4816 set_identifier_type_value (tree id, tree decl)
4817 {
4818 set_identifier_type_value_with_scope (id, decl, current_binding_level);
4819 }
4820
4821 /* Return the name for the constructor (or destructor) for the
4822 specified class. */
4823
4824 tree
4825 constructor_name (tree type)
4826 {
4827 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
4828
4829 return decl ? DECL_NAME (decl) : NULL_TREE;
4830 }
4831
4832 /* Returns TRUE if NAME is the name for the constructor for TYPE,
4833 which must be a class type. */
4834
4835 bool
4836 constructor_name_p (tree name, tree type)
4837 {
4838 gcc_assert (MAYBE_CLASS_TYPE_P (type));
4839
4840 /* These don't have names. */
4841 if (TREE_CODE (type) == DECLTYPE_TYPE
4842 || TREE_CODE (type) == TYPEOF_TYPE)
4843 return false;
4844
4845 if (name && name == constructor_name (type))
4846 return true;
4847
4848 return false;
4849 }
4850
4851 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4852 caller to set DECL_CONTEXT properly.
4853
4854 Warning: For class and block-scope this must only be used when X
4855 will be the new innermost binding for its name, as we tack it onto
4856 the front of IDENTIFIER_BINDING without checking to see if the
4857 current IDENTIFIER_BINDING comes from a closer binding level than
4858 LEVEL.
4859
4860 Warning: For namespace scope, this will look in LEVEL for an
4861 existing binding to match, but if not found will push the decl into
4862 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
4863 pop_nested_namespace if you really need to push it into a foreign
4864 namespace. */
4865
4866 static tree
4867 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
4868 {
4869 cp_binding_level *b;
4870
4871 if (level->kind == sk_class)
4872 {
4873 gcc_checking_assert (!hiding);
4874 b = class_binding_level;
4875 class_binding_level = level;
4876 pushdecl_class_level (x);
4877 class_binding_level = b;
4878 }
4879 else
4880 {
4881 tree function_decl = current_function_decl;
4882 if (level->kind == sk_namespace)
4883 current_function_decl = NULL_TREE;
4884 b = current_binding_level;
4885 current_binding_level = level;
4886 x = do_pushdecl (x, hiding);
4887 current_binding_level = b;
4888 current_function_decl = function_decl;
4889 }
4890 return x;
4891 }
4892
4893 /* Inject X into the local scope just before the function parms. */
4894
4895 tree
4896 pushdecl_outermost_localscope (tree x)
4897 {
4898 cp_binding_level *b = NULL;
4899 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4900
4901 /* Find the scope just inside the function parms. */
4902 for (cp_binding_level *n = current_binding_level;
4903 n->kind != sk_function_parms; n = b->level_chain)
4904 b = n;
4905
4906 tree ret = b ? do_pushdecl_with_scope (x, b) : error_mark_node;
4907 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4908
4909 return ret;
4910 }
4911
4912 /* Process a local-scope or namespace-scope using declaration. LOOKUP
4913 is the result of qualified lookup (both value & type are
4914 significant). FN_SCOPE_P indicates if we're at function-scope (as
4915 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
4916 bindings, which are altered to reflect the newly brought in
4917 declarations. */
4918
4919 static bool
4920 do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
4921 bool insert_p, tree *value_p, tree *type_p)
4922 {
4923 tree value = *value_p;
4924 tree type = *type_p;
4925 bool failed = false;
4926
4927 /* Shift the old and new bindings around so we're comparing class and
4928 enumeration names to each other. */
4929 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4930 {
4931 type = value;
4932 value = NULL_TREE;
4933 }
4934
4935 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4936 {
4937 lookup.type = lookup.value;
4938 lookup.value = NULL_TREE;
4939 }
4940
4941 /* Only process exporting if we're going to be inserting. */
4942 bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
4943
4944 /* First do the value binding. */
4945 if (!lookup.value)
4946 /* Nothing (only implicit typedef found). */
4947 gcc_checking_assert (lookup.type);
4948 else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4949 {
4950 for (lkp_iterator usings (lookup.value); usings; ++usings)
4951 {
4952 tree new_fn = *usings;
4953 bool exporting = revealing_p && module_exporting_p ();
4954 if (exporting)
4955 {
4956 /* If the using decl is exported, the things it refers
4957 to must also be exported (or not in module purview). */
4958 if (!DECL_MODULE_EXPORT_P (new_fn)
4959 && (DECL_LANG_SPECIFIC (new_fn)
4960 && DECL_MODULE_PURVIEW_P (new_fn)))
4961 {
4962 error ("%q#D does not have external linkage", new_fn);
4963 inform (DECL_SOURCE_LOCATION (new_fn),
4964 "%q#D declared here", new_fn);
4965 exporting = false;
4966 }
4967 }
4968
4969 /* [namespace.udecl]
4970
4971 If a function declaration in namespace scope or block
4972 scope has the same name and the same parameter types as a
4973 function introduced by a using declaration the program is
4974 ill-formed. */
4975 /* This seems overreaching, asking core -- why do we care
4976 about decls in the namespace that we cannot name (because
4977 they are not transitively imported. We just check the
4978 decls that are in this TU. */
4979 bool found = false;
4980 for (ovl_iterator old (value); !found && old; ++old)
4981 {
4982 tree old_fn = *old;
4983
4984 if (new_fn == old_fn)
4985 {
4986 /* The function already exists in the current
4987 namespace. We will still want to insert it if
4988 it is revealing a not-revealed thing. */
4989 found = true;
4990 if (!revealing_p)
4991 ;
4992 else if (old.using_p ())
4993 {
4994 if (exporting)
4995 /* Update in place. 'tis ok. */
4996 OVL_EXPORT_P (old.get_using ()) = true;
4997 ;
4998 }
4999 else if (DECL_MODULE_EXPORT_P (new_fn))
5000 ;
5001 else
5002 {
5003 value = old.remove_node (value);
5004 found = false;
5005 }
5006 break;
5007 }
5008 else if (old.using_p ())
5009 continue; /* This is a using decl. */
5010 else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
5011 continue; /* This is an anticipated builtin. */
5012 else if (!matching_fn_p (new_fn, old_fn))
5013 continue; /* Parameters do not match. */
5014 else if (decls_match (new_fn, old_fn))
5015 {
5016 /* Extern "C" in different namespaces. */
5017 found = true;
5018 break;
5019 }
5020 else
5021 {
5022 diagnose_name_conflict (new_fn, old_fn);
5023 failed = true;
5024 found = true;
5025 break;
5026 }
5027 }
5028
5029 if (!found && insert_p)
5030 /* Unlike the decl-pushing case we don't drop anticipated
5031 builtins here. They don't cause a problem, and we'd
5032 like to match them with a future declaration. */
5033 value = ovl_insert (new_fn, value, 1 + exporting);
5034 }
5035 }
5036 else if (value
5037 /* Ignore anticipated builtins. */
5038 && !anticipated_builtin_p (value)
5039 && (fn_scope_p || !decls_match (lookup.value, value)))
5040 {
5041 diagnose_name_conflict (lookup.value, value);
5042 failed = true;
5043 }
5044 else if (insert_p)
5045 // FIXME:what if we're newly exporting lookup.value
5046 value = lookup.value;
5047
5048 /* Now the type binding. */
5049 if (lookup.type && lookup.type != type)
5050 {
5051 // FIXME: What if we're exporting lookup.type?
5052 if (type && !decls_match (lookup.type, type))
5053 {
5054 diagnose_name_conflict (lookup.type, type);
5055 failed = true;
5056 }
5057 else if (insert_p)
5058 type = lookup.type;
5059 }
5060
5061 if (insert_p)
5062 {
5063 /* If value is empty, shift any class or enumeration name back. */
5064 if (!value)
5065 {
5066 value = type;
5067 type = NULL_TREE;
5068 }
5069 *value_p = value;
5070 *type_p = type;
5071 }
5072
5073 return failed;
5074 }
5075
5076 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5077 Both are namespaces. */
5078
5079 bool
5080 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
5081 {
5082 int depth = SCOPE_DEPTH (ancestor);
5083
5084 if (!depth && !inline_only)
5085 /* The global namespace encloses everything. */
5086 return true;
5087
5088 while (SCOPE_DEPTH (descendant) > depth
5089 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
5090 descendant = CP_DECL_CONTEXT (descendant);
5091
5092 return ancestor == descendant;
5093 }
5094
5095 /* Returns true if ROOT (a non-alias namespace, class, or function)
5096 encloses CHILD. CHILD may be either a class type or a namespace
5097 (maybe alias). */
5098
5099 bool
5100 is_ancestor (tree root, tree child)
5101 {
5102 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
5103 && !DECL_NAMESPACE_ALIAS (root))
5104 || TREE_CODE (root) == FUNCTION_DECL
5105 || CLASS_TYPE_P (root));
5106 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
5107 || CLASS_TYPE_P (child));
5108
5109 /* The global namespace encloses everything. Early-out for the
5110 common case. */
5111 if (root == global_namespace)
5112 return true;
5113
5114 /* Search CHILD until we reach namespace scope. */
5115 while (TREE_CODE (child) != NAMESPACE_DECL)
5116 {
5117 /* If we've reached the ROOT, it encloses CHILD. */
5118 if (root == child)
5119 return true;
5120
5121 /* Go out one level. */
5122 if (TYPE_P (child))
5123 child = TYPE_NAME (child);
5124 child = CP_DECL_CONTEXT (child);
5125 }
5126
5127 if (TREE_CODE (root) != NAMESPACE_DECL)
5128 /* Failed to meet the non-namespace we were looking for. */
5129 return false;
5130
5131 if (tree alias = DECL_NAMESPACE_ALIAS (child))
5132 child = alias;
5133
5134 return is_nested_namespace (root, child);
5135 }
5136
5137 /* Enter the class or namespace scope indicated by T suitable for name
5138 lookup. T can be arbitrary scope, not necessary nested inside the
5139 current scope. Returns a non-null scope to pop iff pop_scope
5140 should be called later to exit this scope. */
5141
5142 tree
5143 push_scope (tree t)
5144 {
5145 if (TREE_CODE (t) == NAMESPACE_DECL)
5146 push_decl_namespace (t);
5147 else if (CLASS_TYPE_P (t))
5148 {
5149 if (!at_class_scope_p ()
5150 || !same_type_p (current_class_type, t))
5151 push_nested_class (t);
5152 else
5153 /* T is the same as the current scope. There is therefore no
5154 need to re-enter the scope. Since we are not actually
5155 pushing a new scope, our caller should not call
5156 pop_scope. */
5157 t = NULL_TREE;
5158 }
5159
5160 return t;
5161 }
5162
5163 /* Leave scope pushed by push_scope. */
5164
5165 void
5166 pop_scope (tree t)
5167 {
5168 if (t == NULL_TREE)
5169 return;
5170 if (TREE_CODE (t) == NAMESPACE_DECL)
5171 pop_decl_namespace ();
5172 else if CLASS_TYPE_P (t)
5173 pop_nested_class ();
5174 }
5175
5176 /* Subroutine of push_inner_scope. */
5177
5178 static void
5179 push_inner_scope_r (tree outer, tree inner)
5180 {
5181 tree prev;
5182
5183 if (outer == inner
5184 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5185 return;
5186
5187 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5188 if (outer != prev)
5189 push_inner_scope_r (outer, prev);
5190 if (TREE_CODE (inner) == NAMESPACE_DECL)
5191 {
5192 cp_binding_level *save_template_parm = 0;
5193 /* Temporary take out template parameter scopes. They are saved
5194 in reversed order in save_template_parm. */
5195 while (current_binding_level->kind == sk_template_parms)
5196 {
5197 cp_binding_level *b = current_binding_level;
5198 current_binding_level = b->level_chain;
5199 b->level_chain = save_template_parm;
5200 save_template_parm = b;
5201 }
5202
5203 resume_scope (NAMESPACE_LEVEL (inner));
5204 current_namespace = inner;
5205
5206 /* Restore template parameter scopes. */
5207 while (save_template_parm)
5208 {
5209 cp_binding_level *b = save_template_parm;
5210 save_template_parm = b->level_chain;
5211 b->level_chain = current_binding_level;
5212 current_binding_level = b;
5213 }
5214 }
5215 else
5216 pushclass (inner);
5217 }
5218
5219 /* Enter the scope INNER from current scope. INNER must be a scope
5220 nested inside current scope. This works with both name lookup and
5221 pushing name into scope. In case a template parameter scope is present,
5222 namespace is pushed under the template parameter scope according to
5223 name lookup rule in 14.6.1/6.
5224
5225 Return the former current scope suitable for pop_inner_scope. */
5226
5227 tree
5228 push_inner_scope (tree inner)
5229 {
5230 tree outer = current_scope ();
5231 if (!outer)
5232 outer = current_namespace;
5233
5234 push_inner_scope_r (outer, inner);
5235 return outer;
5236 }
5237
5238 /* Exit the current scope INNER back to scope OUTER. */
5239
5240 void
5241 pop_inner_scope (tree outer, tree inner)
5242 {
5243 if (outer == inner
5244 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5245 return;
5246
5247 while (outer != inner)
5248 {
5249 if (TREE_CODE (inner) == NAMESPACE_DECL)
5250 {
5251 cp_binding_level *save_template_parm = 0;
5252 /* Temporary take out template parameter scopes. They are saved
5253 in reversed order in save_template_parm. */
5254 while (current_binding_level->kind == sk_template_parms)
5255 {
5256 cp_binding_level *b = current_binding_level;
5257 current_binding_level = b->level_chain;
5258 b->level_chain = save_template_parm;
5259 save_template_parm = b;
5260 }
5261
5262 pop_namespace ();
5263
5264 /* Restore template parameter scopes. */
5265 while (save_template_parm)
5266 {
5267 cp_binding_level *b = save_template_parm;
5268 save_template_parm = b->level_chain;
5269 b->level_chain = current_binding_level;
5270 current_binding_level = b;
5271 }
5272 }
5273 else
5274 popclass ();
5275
5276 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5277 }
5278 }
5279 \f
5280 /* Do a pushlevel for class declarations. */
5281
5282 void
5283 pushlevel_class (void)
5284 {
5285 class_binding_level = begin_scope (sk_class, current_class_type);
5286 }
5287
5288 /* ...and a poplevel for class declarations. */
5289
5290 void
5291 poplevel_class (void)
5292 {
5293 cp_binding_level *level = class_binding_level;
5294 cp_class_binding *cb;
5295 size_t i;
5296 tree shadowed;
5297
5298 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5299 gcc_assert (level != 0);
5300
5301 /* If we're leaving a toplevel class, cache its binding level. */
5302 if (current_class_depth == 1)
5303 previous_class_level = level;
5304 for (shadowed = level->type_shadowed;
5305 shadowed;
5306 shadowed = TREE_CHAIN (shadowed))
5307 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
5308
5309 /* Remove the bindings for all of the class-level declarations. */
5310 if (level->class_shadowed)
5311 {
5312 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
5313 {
5314 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
5315 cxx_binding_free (cb->base);
5316 }
5317 ggc_free (level->class_shadowed);
5318 level->class_shadowed = NULL;
5319 }
5320
5321 /* Now, pop out of the binding level which we created up in the
5322 `pushlevel_class' routine. */
5323 gcc_assert (current_binding_level == level);
5324 leave_scope ();
5325 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5326 }
5327
5328 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5329 appropriate. DECL is the value to which a name has just been
5330 bound. CLASS_TYPE is the class in which the lookup occurred. */
5331
5332 static void
5333 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
5334 tree class_type)
5335 {
5336 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
5337 {
5338 tree context;
5339
5340 if (TREE_CODE (decl) == OVERLOAD)
5341 context = ovl_scope (decl);
5342 else
5343 {
5344 gcc_assert (DECL_P (decl));
5345 context = context_for_name_lookup (decl);
5346 }
5347
5348 if (is_properly_derived_from (class_type, context))
5349 INHERITED_VALUE_BINDING_P (binding) = 1;
5350 else
5351 INHERITED_VALUE_BINDING_P (binding) = 0;
5352 }
5353 else if (binding->value == decl)
5354 /* We only encounter a TREE_LIST when there is an ambiguity in the
5355 base classes. Such an ambiguity can be overridden by a
5356 definition in this class. */
5357 INHERITED_VALUE_BINDING_P (binding) = 1;
5358 else
5359 INHERITED_VALUE_BINDING_P (binding) = 0;
5360 }
5361
5362 /* Make the declaration of X appear in CLASS scope. */
5363
5364 bool
5365 pushdecl_class_level (tree x)
5366 {
5367 bool is_valid = true;
5368 bool subtime;
5369
5370 /* Do nothing if we're adding to an outer lambda closure type,
5371 outer_binding will add it later if it's needed. */
5372 if (current_class_type != class_binding_level->this_entity)
5373 return true;
5374
5375 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5376 /* Get the name of X. */
5377 tree name = OVL_NAME (x);
5378
5379 if (name)
5380 {
5381 is_valid = push_class_level_binding (name, x);
5382 if (TREE_CODE (x) == TYPE_DECL)
5383 set_identifier_type_value (name, x);
5384 }
5385 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
5386 {
5387 /* If X is an anonymous aggregate, all of its members are
5388 treated as if they were members of the class containing the
5389 aggregate, for naming purposes. */
5390 location_t save_location = input_location;
5391 tree anon = TREE_TYPE (x);
5392 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
5393 for (unsigned ix = member_vec->length (); ix--;)
5394 {
5395 tree binding = (*member_vec)[ix];
5396 if (STAT_HACK_P (binding))
5397 {
5398 if (!pushdecl_class_level (STAT_TYPE (binding)))
5399 is_valid = false;
5400 binding = STAT_DECL (binding);
5401 }
5402 if (!pushdecl_class_level (binding))
5403 is_valid = false;
5404 }
5405 else
5406 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
5407 if (TREE_CODE (f) == FIELD_DECL)
5408 {
5409 input_location = DECL_SOURCE_LOCATION (f);
5410 if (!pushdecl_class_level (f))
5411 is_valid = false;
5412 }
5413 input_location = save_location;
5414 }
5415 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5416 return is_valid;
5417 }
5418
5419 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5420 scope. If the value returned is non-NULL, and the PREVIOUS field
5421 is not set, callers must set the PREVIOUS field explicitly. */
5422
5423 static cxx_binding *
5424 get_class_binding (tree name, cp_binding_level *scope)
5425 {
5426 tree class_type;
5427 tree type_binding;
5428 tree value_binding;
5429 cxx_binding *binding;
5430
5431 class_type = scope->this_entity;
5432
5433 /* Get the type binding. */
5434 type_binding = lookup_member (class_type, name,
5435 /*protect=*/2, /*want_type=*/true,
5436 tf_warning_or_error);
5437 /* Get the value binding. */
5438 value_binding = lookup_member (class_type, name,
5439 /*protect=*/2, /*want_type=*/false,
5440 tf_warning_or_error);
5441
5442 if (value_binding
5443 && (TREE_CODE (value_binding) == TYPE_DECL
5444 || DECL_CLASS_TEMPLATE_P (value_binding)
5445 || (TREE_CODE (value_binding) == TREE_LIST
5446 && TREE_TYPE (value_binding) == error_mark_node
5447 && (TREE_CODE (TREE_VALUE (value_binding))
5448 == TYPE_DECL))))
5449 /* We found a type binding, even when looking for a non-type
5450 binding. This means that we already processed this binding
5451 above. */
5452 ;
5453 else if (value_binding)
5454 {
5455 if (TREE_CODE (value_binding) == TREE_LIST
5456 && TREE_TYPE (value_binding) == error_mark_node)
5457 /* NAME is ambiguous. */
5458 ;
5459 else if (BASELINK_P (value_binding))
5460 /* NAME is some overloaded functions. */
5461 value_binding = BASELINK_FUNCTIONS (value_binding);
5462 }
5463
5464 /* If we found either a type binding or a value binding, create a
5465 new binding object. */
5466 if (type_binding || value_binding)
5467 {
5468 binding = new_class_binding (name,
5469 value_binding,
5470 type_binding,
5471 scope);
5472 set_inherited_value_binding_p (binding, value_binding, class_type);
5473 }
5474 else
5475 binding = NULL;
5476
5477 return binding;
5478 }
5479
5480 /* Make the declaration(s) of X appear in CLASS scope under the name
5481 NAME. Returns true if the binding is valid. */
5482
5483 static bool
5484 push_class_level_binding_1 (tree name, tree x)
5485 {
5486 cxx_binding *binding;
5487 tree decl = x;
5488 bool ok;
5489
5490 /* The class_binding_level will be NULL if x is a template
5491 parameter name in a member template. */
5492 if (!class_binding_level)
5493 return true;
5494
5495 if (name == error_mark_node)
5496 return false;
5497
5498 /* Can happen for an erroneous declaration (c++/60384). */
5499 if (!identifier_p (name))
5500 {
5501 gcc_assert (errorcount || sorrycount);
5502 return false;
5503 }
5504
5505 /* Check for invalid member names. But don't worry about a default
5506 argument-scope lambda being pushed after the class is complete. */
5507 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
5508 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
5509 /* Check that we're pushing into the right binding level. */
5510 gcc_assert (current_class_type == class_binding_level->this_entity);
5511
5512 /* We could have been passed a tree list if this is an ambiguous
5513 declaration. If so, pull the declaration out because
5514 check_template_shadow will not handle a TREE_LIST. */
5515 if (TREE_CODE (decl) == TREE_LIST
5516 && TREE_TYPE (decl) == error_mark_node)
5517 decl = TREE_VALUE (decl);
5518
5519 if (!check_template_shadow (decl))
5520 return false;
5521
5522 /* [class.mem]
5523
5524 If T is the name of a class, then each of the following shall
5525 have a name different from T:
5526
5527 -- every static data member of class T;
5528
5529 -- every member of class T that is itself a type;
5530
5531 -- every enumerator of every member of class T that is an
5532 enumerated type;
5533
5534 -- every member of every anonymous union that is a member of
5535 class T.
5536
5537 (Non-static data members were also forbidden to have the same
5538 name as T until TC1.) */
5539 if ((VAR_P (x)
5540 || TREE_CODE (x) == CONST_DECL
5541 || (TREE_CODE (x) == TYPE_DECL
5542 && !DECL_SELF_REFERENCE_P (x))
5543 /* A data member of an anonymous union. */
5544 || (TREE_CODE (x) == FIELD_DECL
5545 && DECL_CONTEXT (x) != current_class_type))
5546 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
5547 {
5548 tree scope = context_for_name_lookup (x);
5549 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
5550 {
5551 error_at (DECL_SOURCE_LOCATION (x),
5552 "%qD has the same name as the class in which it is "
5553 "declared", x);
5554 return false;
5555 }
5556 }
5557
5558 /* Get the current binding for NAME in this class, if any. */
5559 binding = IDENTIFIER_BINDING (name);
5560 if (!binding || binding->scope != class_binding_level)
5561 {
5562 binding = get_class_binding (name, class_binding_level);
5563 /* If a new binding was created, put it at the front of the
5564 IDENTIFIER_BINDING list. */
5565 if (binding)
5566 {
5567 binding->previous = IDENTIFIER_BINDING (name);
5568 IDENTIFIER_BINDING (name) = binding;
5569 }
5570 }
5571
5572 /* If there is already a binding, then we may need to update the
5573 current value. */
5574 if (binding && binding->value)
5575 {
5576 tree bval = binding->value;
5577 tree old_decl = NULL_TREE;
5578 tree target_decl = strip_using_decl (decl);
5579 tree target_bval = strip_using_decl (bval);
5580
5581 if (INHERITED_VALUE_BINDING_P (binding))
5582 {
5583 /* If the old binding was from a base class, and was for a
5584 tag name, slide it over to make room for the new binding.
5585 The old binding is still visible if explicitly qualified
5586 with a class-key. */
5587 if (TREE_CODE (target_bval) == TYPE_DECL
5588 && DECL_ARTIFICIAL (target_bval)
5589 && !(TREE_CODE (target_decl) == TYPE_DECL
5590 && DECL_ARTIFICIAL (target_decl)))
5591 {
5592 old_decl = binding->type;
5593 binding->type = bval;
5594 binding->value = NULL_TREE;
5595 INHERITED_VALUE_BINDING_P (binding) = 0;
5596 }
5597 else
5598 {
5599 old_decl = bval;
5600 /* Any inherited type declaration is hidden by the type
5601 declaration in the derived class. */
5602 if (TREE_CODE (target_decl) == TYPE_DECL
5603 && DECL_ARTIFICIAL (target_decl))
5604 binding->type = NULL_TREE;
5605 }
5606 }
5607 else if (TREE_CODE (decl) == USING_DECL
5608 && TREE_CODE (bval) == USING_DECL
5609 && same_type_p (USING_DECL_SCOPE (decl),
5610 USING_DECL_SCOPE (bval)))
5611 /* This is a using redeclaration that will be diagnosed later
5612 in supplement_binding */
5613 ;
5614 else if (TREE_CODE (decl) == USING_DECL
5615 && TREE_CODE (bval) == USING_DECL
5616 && DECL_DEPENDENT_P (decl)
5617 && DECL_DEPENDENT_P (bval))
5618 return true;
5619 else if (TREE_CODE (decl) == USING_DECL
5620 && OVL_P (target_bval))
5621 old_decl = bval;
5622 else if (TREE_CODE (bval) == USING_DECL
5623 && OVL_P (target_decl))
5624 return true;
5625 else if (OVL_P (target_decl)
5626 && OVL_P (target_bval))
5627 old_decl = bval;
5628
5629 if (old_decl && binding->scope == class_binding_level)
5630 {
5631 binding->value = x;
5632 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5633 here. This function is only used to register bindings
5634 from with the class definition itself. */
5635 INHERITED_VALUE_BINDING_P (binding) = 0;
5636 return true;
5637 }
5638 }
5639
5640 /* Note that we declared this value so that we can issue an error if
5641 this is an invalid redeclaration of a name already used for some
5642 other purpose. */
5643 note_name_declared_in_class (name, decl);
5644
5645 /* If we didn't replace an existing binding, put the binding on the
5646 stack of bindings for the identifier, and update the shadowed
5647 list. */
5648 if (binding && binding->scope == class_binding_level)
5649 /* Supplement the existing binding. */
5650 ok = supplement_binding (binding, decl);
5651 else
5652 {
5653 /* Create a new binding. */
5654 push_binding (name, decl, class_binding_level);
5655 ok = true;
5656 }
5657
5658 return ok;
5659 }
5660
5661 /* Wrapper for push_class_level_binding_1. */
5662
5663 bool
5664 push_class_level_binding (tree name, tree x)
5665 {
5666 bool ret;
5667 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5668 ret = push_class_level_binding_1 (name, x);
5669 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5670 return ret;
5671 }
5672
5673 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5674 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5675 failure. */
5676
5677 static tree
5678 lookup_using_decl (tree scope, name_lookup &lookup)
5679 {
5680 tree current = current_scope ();
5681 bool dependent_p = false;
5682 tree binfo = NULL_TREE;
5683 base_kind b_kind = bk_not_base;
5684
5685 /* Because C++20 breaks the invariant that only member using-decls
5686 refer to members and only non-member using-decls refer to
5687 non-members, we first do the lookups, and then do validation that
5688 what we found is ok. */
5689
5690 if (TREE_CODE (scope) == ENUMERAL_TYPE
5691 && cxx_dialect < cxx20
5692 && UNSCOPED_ENUM_P (scope)
5693 && !TYPE_FUNCTION_SCOPE_P (scope))
5694 {
5695 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5696 should allow it as meaning the enclosing scope. I don't see any
5697 justification for this in C++11, but let's keep allowing it. */
5698 tree ctx = CP_TYPE_CONTEXT (scope);
5699 if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
5700 scope = ctx;
5701 }
5702
5703 if (TREE_CODE (scope) == NAMESPACE_DECL)
5704 {
5705 /* Naming a namespace member. */
5706 qualified_namespace_lookup (scope, &lookup);
5707
5708 if (TYPE_P (current)
5709 && (!lookup.value
5710 || lookup.type
5711 || cxx_dialect < cxx20
5712 || TREE_CODE (lookup.value) != CONST_DECL))
5713 {
5714 error ("using-declaration for non-member at class scope");
5715 return NULL_TREE;
5716 }
5717 }
5718 else if (TREE_CODE (scope) == ENUMERAL_TYPE)
5719 {
5720 /* Naming an enumeration member. */
5721 if (cxx_dialect < cxx20)
5722 error ("%<using%> with enumeration scope %q#T "
5723 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5724 scope);
5725 lookup.value = lookup_enumerator (scope, lookup.name);
5726 }
5727 else
5728 {
5729 /* Naming a class member. This is awkward in C++20, because we
5730 might be naming an enumerator of an unrelated class. */
5731
5732 /* You cannot using-decl a destructor. */
5733 if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
5734 {
5735 error ("%<%T::%D%> names destructor", scope, lookup.name);
5736 return NULL_TREE;
5737 }
5738
5739 /* Using T::T declares inheriting ctors, even if T is a typedef. */
5740 if (MAYBE_CLASS_TYPE_P (scope)
5741 && (lookup.name == TYPE_IDENTIFIER (scope)
5742 || constructor_name_p (lookup.name, scope)))
5743 {
5744 if (!TYPE_P (current))
5745 {
5746 error ("non-member using-declaration names constructor of %qT",
5747 scope);
5748 return NULL_TREE;
5749 }
5750 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
5751 lookup.name = ctor_identifier;
5752 CLASSTYPE_NON_AGGREGATE (current) = true;
5753 }
5754
5755 if (!MAYBE_CLASS_TYPE_P (scope))
5756 ;
5757 else if (TYPE_P (current))
5758 {
5759 dependent_p = dependent_scope_p (scope);
5760 if (!dependent_p)
5761 {
5762 binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
5763 gcc_checking_assert (b_kind >= bk_not_base);
5764
5765 if (lookup.name == ctor_identifier)
5766 {
5767 /* Even if there are dependent bases, SCOPE will not
5768 be direct base, no matter. */
5769 if (b_kind < bk_proper_base || !binfo_direct_p (binfo))
5770 {
5771 error ("%qT is not a direct base of %qT", scope, current);
5772 return NULL_TREE;
5773 }
5774 }
5775 else if (b_kind < bk_proper_base)
5776 binfo = TYPE_BINFO (scope);
5777 else if (IDENTIFIER_CONV_OP_P (lookup.name)
5778 && dependent_type_p (TREE_TYPE (lookup.name)))
5779 dependent_p = true;
5780 }
5781 }
5782 else
5783 binfo = TYPE_BINFO (scope);
5784
5785 if (!dependent_p)
5786 {
5787 if (binfo)
5788 lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
5789 /*want_type=*/false, tf_none);
5790
5791 tree saved_value = lookup.value;
5792 if (lookup.value
5793 && b_kind < bk_proper_base)
5794 {
5795 if (cxx_dialect >= cxx20
5796 && TREE_CODE (lookup.value) == CONST_DECL)
5797 {
5798 /* Using an unrelated enum; check access here rather
5799 than separately for class and non-class using. */
5800 perform_or_defer_access_check
5801 (binfo, lookup.value, lookup.value, tf_warning_or_error);
5802 /* And then if this is a copy from handle_using_decl, look
5803 through to the original enumerator. */
5804 if (CONST_DECL_USING_P (lookup.value))
5805 lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
5806 }
5807 else
5808 lookup.value = NULL_TREE;
5809 }
5810
5811 if (!lookup.value)
5812 {
5813 if (!TYPE_P (current))
5814 {
5815 error ("using-declaration for member at non-class scope");
5816 return NULL_TREE;
5817 }
5818
5819 if (b_kind < bk_proper_base)
5820 {
5821 if (b_kind == bk_not_base && any_dependent_bases_p ())
5822 /* Treat as-if dependent. */
5823 dependent_p = true;
5824 else
5825 {
5826 auto_diagnostic_group g;
5827 error_not_base_type (scope, current);
5828 if (saved_value && DECL_IMPLICIT_TYPEDEF_P (saved_value)
5829 && (TREE_CODE (TREE_TYPE (saved_value))
5830 == ENUMERAL_TYPE))
5831 inform (input_location,
5832 "did you mean %<using enum %T::%D%>?",
5833 scope, lookup.name);
5834 return NULL_TREE;
5835 }
5836 }
5837 }
5838 }
5839 }
5840
5841 /* Did we find anything sane? */
5842 if (dependent_p)
5843 ;
5844 else if (!lookup.value)
5845 {
5846 error ("%qD has not been declared in %qD", lookup.name, scope);
5847 return NULL_TREE;
5848 }
5849 else if (TREE_CODE (lookup.value) == TREE_LIST
5850 /* We can (independently) have ambiguous implicit typedefs. */
5851 || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
5852 {
5853 error ("reference to %qD is ambiguous", lookup.name);
5854 print_candidates (TREE_CODE (lookup.value) == TREE_LIST
5855 ? lookup.value : lookup.type);
5856 return NULL_TREE;
5857 }
5858 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
5859 {
5860 error ("using-declaration may not name namespace %qD", lookup.value);
5861 return NULL_TREE;
5862 }
5863
5864 if (TYPE_P (current))
5865 {
5866 /* In class scope. */
5867
5868 /* Cannot introduce a constructor name. */
5869 if (constructor_name_p (lookup.name, current))
5870 {
5871 error ("%<%T::%D%> names constructor in %qT",
5872 scope, lookup.name, current);
5873 return NULL_TREE;
5874 }
5875
5876 if (lookup.value && BASELINK_P (lookup.value))
5877 /* The binfo from which the functions came does not matter. */
5878 lookup.value = BASELINK_FUNCTIONS (lookup.value);
5879 }
5880
5881 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5882 USING_DECL_SCOPE (using_decl) = scope;
5883 USING_DECL_DECLS (using_decl) = lookup.value;
5884 DECL_DEPENDENT_P (using_decl) = dependent_p;
5885 DECL_CONTEXT (using_decl) = current;
5886 if (TYPE_P (current) && b_kind == bk_not_base)
5887 USING_DECL_UNRELATED_P (using_decl) = true;
5888
5889 return using_decl;
5890 }
5891
5892 /* Process "using SCOPE::NAME" in a class scope. Return the
5893 USING_DECL created. */
5894
5895 tree
5896 do_class_using_decl (tree scope, tree name)
5897 {
5898 if (name == error_mark_node
5899 || scope == error_mark_node)
5900 return NULL_TREE;
5901
5902 name_lookup lookup (name);
5903 return lookup_using_decl (scope, lookup);
5904 }
5905
5906 \f
5907 /* Return the binding for NAME in NS in the current TU. If NS is
5908 NULL, look in global_namespace. We will not find declarations
5909 from imports. Users of this who, having found nothing, push a new
5910 decl must be prepared for that pushing to match an existing decl. */
5911
5912 tree
5913 get_namespace_binding (tree ns, tree name)
5914 {
5915 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5916 if (!ns)
5917 ns = global_namespace;
5918 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
5919 tree ret = NULL_TREE;
5920
5921 if (tree *b = find_namespace_slot (ns, name))
5922 {
5923 ret = *b;
5924
5925 if (TREE_CODE (ret) == BINDING_VECTOR)
5926 ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
5927 if (ret)
5928 ret = MAYBE_STAT_DECL (ret);
5929 }
5930
5931 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5932 return ret;
5933 }
5934
5935 /* Push internal DECL into the global namespace. Does not do the
5936 full overload fn handling and does not add it to the list of things
5937 in the namespace. */
5938
5939 void
5940 set_global_binding (tree decl)
5941 {
5942 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5943
5944 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
5945
5946 if (*slot)
5947 /* The user's placed something in the implementor's namespace. */
5948 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
5949
5950 /* Force the binding, so compiler internals continue to work. */
5951 *slot = decl;
5952
5953 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5954 }
5955
5956 /* Set the context of a declaration to scope. Complain if we are not
5957 outside scope. */
5958
5959 void
5960 set_decl_namespace (tree decl, tree scope, bool friendp)
5961 {
5962 /* Get rid of namespace aliases. */
5963 scope = ORIGINAL_NAMESPACE (scope);
5964
5965 /* It is ok for friends to be qualified in parallel space. */
5966 if (!friendp && !is_nested_namespace (current_namespace, scope))
5967 error ("declaration of %qD not in a namespace surrounding %qD",
5968 decl, scope);
5969 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5970
5971 /* See whether this has been declared in the namespace or inline
5972 children. */
5973 tree old = NULL_TREE;
5974 {
5975 name_lookup lookup (DECL_NAME (decl),
5976 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
5977 if (!lookup.search_qualified (scope, /*usings=*/false))
5978 /* No old declaration at all. */
5979 goto not_found;
5980 old = lookup.value;
5981 }
5982
5983 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
5984 if (TREE_CODE (old) == TREE_LIST)
5985 {
5986 ambiguous:
5987 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5988 error ("reference to %qD is ambiguous", decl);
5989 print_candidates (old);
5990 return;
5991 }
5992
5993 if (!DECL_DECLARES_FUNCTION_P (decl))
5994 {
5995 /* Don't compare non-function decls with decls_match here, since
5996 it can't check for the correct constness at this
5997 point. pushdecl will find those errors later. */
5998
5999 /* We might have found it in an inline namespace child of SCOPE. */
6000 if (TREE_CODE (decl) == TREE_CODE (old))
6001 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
6002
6003 found:
6004 /* Writing "N::i" to declare something directly in "N" is invalid. */
6005 if (CP_DECL_CONTEXT (decl) == current_namespace
6006 && at_namespace_scope_p ())
6007 error_at (DECL_SOURCE_LOCATION (decl),
6008 "explicit qualification in declaration of %qD", decl);
6009 return;
6010 }
6011
6012 /* Since decl is a function, old should contain a function decl. */
6013 if (!OVL_P (old))
6014 {
6015 not_found:
6016 /* It didn't work, go back to the explicit scope. */
6017 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6018 error ("%qD should have been declared inside %qD", decl, scope);
6019
6020 return;
6021 }
6022
6023 /* We handle these in check_explicit_instantiation_namespace. */
6024 if (processing_explicit_instantiation)
6025 return;
6026 if (processing_template_decl || processing_specialization)
6027 /* We have not yet called push_template_decl to turn a
6028 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6029 match. But, we'll check later, when we construct the
6030 template. */
6031 return;
6032
6033 /* Instantiations or specializations of templates may be declared as
6034 friends in any namespace. */
6035 if (friendp && DECL_USE_TEMPLATE (decl))
6036 return;
6037
6038 tree found = NULL_TREE;
6039 bool hidden_p = false;
6040
6041 for (lkp_iterator iter (old); iter; ++iter)
6042 {
6043 if (iter.using_p ())
6044 continue;
6045
6046 tree ofn = *iter;
6047
6048 /* Adjust DECL_CONTEXT first so decls_match will return true
6049 if DECL will match a declaration in an inline namespace. */
6050 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
6051 if (decls_match (decl, ofn))
6052 {
6053 if (found)
6054 {
6055 /* We found more than one matching declaration. This
6056 can happen if we have two inline namespace children,
6057 each containing a suitable declaration. */
6058 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6059 goto ambiguous;
6060 }
6061 found = ofn;
6062 hidden_p = iter.hidden_p ();
6063 }
6064 }
6065
6066 if (found)
6067 {
6068 if (hidden_p)
6069 {
6070 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
6071 "%qD has not been declared within %qD", decl, scope);
6072 inform (DECL_SOURCE_LOCATION (found),
6073 "only here as a %<friend%>");
6074 }
6075 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
6076 goto found;
6077 }
6078
6079 goto not_found;
6080 }
6081
6082 /* Return the namespace where the current declaration is declared. */
6083
6084 tree
6085 current_decl_namespace (void)
6086 {
6087 tree result;
6088 /* If we have been pushed into a different namespace, use it. */
6089 if (!vec_safe_is_empty (decl_namespace_list))
6090 return decl_namespace_list->last ();
6091
6092 if (current_class_type)
6093 result = decl_namespace_context (current_class_type);
6094 else if (current_function_decl)
6095 result = decl_namespace_context (current_function_decl);
6096 else
6097 result = current_namespace;
6098 return result;
6099 }
6100
6101 /* Process any ATTRIBUTES on a namespace definition. Returns true if
6102 attribute visibility is seen. */
6103
6104 bool
6105 handle_namespace_attrs (tree ns, tree attributes)
6106 {
6107 tree d;
6108 bool saw_vis = false;
6109
6110 if (attributes == error_mark_node)
6111 return false;
6112
6113 for (d = attributes; d; d = TREE_CHAIN (d))
6114 {
6115 tree name = get_attribute_name (d);
6116 tree args = TREE_VALUE (d);
6117
6118 if (is_attribute_p ("visibility", name))
6119 {
6120 /* attribute visibility is a property of the syntactic block
6121 rather than the namespace as a whole, so we don't touch the
6122 NAMESPACE_DECL at all. */
6123 tree x = args ? TREE_VALUE (args) : NULL_TREE;
6124 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
6125 {
6126 warning (OPT_Wattributes,
6127 "%qD attribute requires a single NTBS argument",
6128 name);
6129 continue;
6130 }
6131
6132 if (!TREE_PUBLIC (ns))
6133 warning (OPT_Wattributes,
6134 "%qD attribute is meaningless since members of the "
6135 "anonymous namespace get local symbols", name);
6136
6137 push_visibility (TREE_STRING_POINTER (x), 1);
6138 saw_vis = true;
6139 }
6140 else if (is_attribute_p ("abi_tag", name))
6141 {
6142 if (!DECL_NAME (ns))
6143 {
6144 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6145 "namespace", name);
6146 continue;
6147 }
6148 if (!DECL_NAMESPACE_INLINE_P (ns))
6149 {
6150 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
6151 "namespace", name);
6152 continue;
6153 }
6154 if (!args)
6155 {
6156 tree dn = DECL_NAME (ns);
6157 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
6158 IDENTIFIER_POINTER (dn));
6159 TREE_TYPE (args) = char_array_type_node;
6160 args = fix_string_type (args);
6161 args = build_tree_list (NULL_TREE, args);
6162 }
6163 if (check_abi_tag_args (args, name))
6164 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6165 DECL_ATTRIBUTES (ns));
6166 }
6167 else if (is_attribute_p ("deprecated", name))
6168 {
6169 if (!DECL_NAME (ns))
6170 {
6171 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6172 "namespace", name);
6173 continue;
6174 }
6175 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6176 {
6177 error ("deprecated message is not a string");
6178 continue;
6179 }
6180 TREE_DEPRECATED (ns) = 1;
6181 if (args)
6182 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6183 DECL_ATTRIBUTES (ns));
6184 }
6185 else
6186 {
6187 warning (OPT_Wattributes, "%qD attribute directive ignored",
6188 name);
6189 continue;
6190 }
6191 }
6192
6193 return saw_vis;
6194 }
6195
6196 /* Temporarily set the namespace for the current declaration. */
6197
6198 void
6199 push_decl_namespace (tree decl)
6200 {
6201 if (TREE_CODE (decl) != NAMESPACE_DECL)
6202 decl = decl_namespace_context (decl);
6203 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
6204 }
6205
6206 /* [namespace.memdef]/2 */
6207
6208 void
6209 pop_decl_namespace (void)
6210 {
6211 decl_namespace_list->pop ();
6212 }
6213
6214 /* Process a namespace-alias declaration. */
6215
6216 void
6217 do_namespace_alias (tree alias, tree name_space)
6218 {
6219 if (name_space == error_mark_node)
6220 return;
6221
6222 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
6223
6224 name_space = ORIGINAL_NAMESPACE (name_space);
6225
6226 /* Build the alias. */
6227 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
6228 DECL_NAMESPACE_ALIAS (alias) = name_space;
6229 DECL_EXTERNAL (alias) = 1;
6230 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
6231 set_originating_module (alias);
6232
6233 pushdecl (alias);
6234
6235 /* Emit debug info for namespace alias. */
6236 if (!building_stmt_list_p ())
6237 (*debug_hooks->early_global_decl) (alias);
6238 }
6239
6240 /* Like pushdecl, only it places X in the current namespace,
6241 if appropriate. */
6242
6243 tree
6244 pushdecl_namespace_level (tree x, bool hiding)
6245 {
6246 cp_binding_level *b = current_binding_level;
6247 tree t;
6248
6249 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6250 t = do_pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), hiding);
6251
6252 /* Now, the type_shadowed stack may screw us. Munge it so it does
6253 what we want. */
6254 if (TREE_CODE (t) == TYPE_DECL)
6255 {
6256 tree name = DECL_NAME (t);
6257 tree newval;
6258 tree *ptr = (tree *)0;
6259 for (; !global_scope_p (b); b = b->level_chain)
6260 {
6261 tree shadowed = b->type_shadowed;
6262 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
6263 if (TREE_PURPOSE (shadowed) == name)
6264 {
6265 ptr = &TREE_VALUE (shadowed);
6266 /* Can't break out of the loop here because sometimes
6267 a binding level will have duplicate bindings for
6268 PT names. It's gross, but I haven't time to fix it. */
6269 }
6270 }
6271 newval = TREE_TYPE (t);
6272 if (ptr == (tree *)0)
6273 {
6274 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
6275 up here if this is changed to an assertion. --KR */
6276 SET_IDENTIFIER_TYPE_VALUE (name, t);
6277 }
6278 else
6279 {
6280 *ptr = newval;
6281 }
6282 }
6283 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6284 return t;
6285 }
6286
6287 /* Wrapper around push_local_binding to push the bindings for
6288 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6289 is the result of name lookup during template parsing. */
6290
6291 static void
6292 push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
6293 {
6294 tree type = NULL_TREE;
6295
6296 cxx_binding *binding = find_local_binding (current_binding_level, name);
6297 if (binding)
6298 {
6299 value = binding->value;
6300 type = binding->type;
6301 }
6302
6303 /* DR 36 questions why using-decls at function scope may not be
6304 duplicates. Disallow it, as C++11 claimed and PR 20420
6305 implemented. */
6306 if (lookup)
6307 do_nonmember_using_decl (*lookup, true, true, &value, &type);
6308
6309 if (!value)
6310 ;
6311 else if (binding && value == binding->value)
6312 /* Redeclaration of this USING_DECL. */;
6313 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
6314 {
6315 /* We already have this binding, so replace it. */
6316 update_local_overload (IDENTIFIER_BINDING (name), value);
6317 IDENTIFIER_BINDING (name)->value = value;
6318 }
6319 else
6320 /* Install the new binding. */
6321 push_local_binding (name, value, /*using=*/true);
6322
6323 if (!type)
6324 ;
6325 else if (binding && type == binding->type)
6326 ;
6327 else
6328 {
6329 push_local_binding (name, type, /*using=*/true);
6330 set_identifier_type_value (name, type);
6331 }
6332 }
6333
6334 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6335
6336 void
6337 push_using_decl_bindings (tree name, tree value)
6338 {
6339 push_using_decl_bindings (nullptr, name, value);
6340 }
6341
6342 /* Process a using declaration in non-class scope. */
6343
6344 void
6345 finish_nonmember_using_decl (tree scope, tree name)
6346 {
6347 gcc_checking_assert (current_binding_level->kind != sk_class);
6348
6349 if (scope == error_mark_node || name == error_mark_node)
6350 return;
6351
6352 name_lookup lookup (name);
6353
6354 tree using_decl = lookup_using_decl (scope, lookup);
6355 if (!using_decl)
6356 return;
6357
6358 /* Emit debug info. */
6359 if (!processing_template_decl)
6360 cp_emit_debug_info_for_using (lookup.value,
6361 current_binding_level->this_entity);
6362
6363 if (current_binding_level->kind == sk_namespace)
6364 {
6365 tree *slot = find_namespace_slot (current_namespace, name, true);
6366 tree *mslot = get_fixed_binding_slot (slot, name,
6367 BINDING_SLOT_CURRENT, true);
6368 bool failed = false;
6369
6370 if (mslot != slot)
6371 {
6372 /* A module vector. I presume the binding list is going to
6373 be sparser than the import bitmap. Hence iterate over
6374 the former checking for bits set in the bitmap. */
6375 bitmap imports = get_import_bitmap ();
6376 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
6377
6378 /* Scan the imported bindings. */
6379 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
6380 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
6381 {
6382 ix--;
6383 cluster++;
6384 }
6385
6386 /* Do this in forward order, so we load modules in an order
6387 the user expects. */
6388 for (; ix--; cluster++)
6389 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
6390 {
6391 /* Are we importing this module? */
6392 if (unsigned base = cluster->indices[jx].base)
6393 if (unsigned span = cluster->indices[jx].span)
6394 do
6395 if (bitmap_bit_p (imports, base))
6396 goto found;
6397 while (++base, --span);
6398 continue;
6399
6400 found:;
6401 /* Is it loaded? */
6402 if (cluster->slots[jx].is_lazy ())
6403 {
6404 gcc_assert (cluster->indices[jx].span == 1);
6405 lazy_load_binding (cluster->indices[jx].base,
6406 scope, name, &cluster->slots[jx]);
6407 }
6408
6409 tree value = cluster->slots[jx];
6410 if (!value)
6411 /* Load errors could mean there's nothing here. */
6412 continue;
6413
6414 /* Extract what we can see from here. If there's no
6415 stat_hack, then everything was exported. */
6416 tree type = NULL_TREE;
6417
6418 /* If no stat hack, everything is visible. */
6419 if (STAT_HACK_P (value))
6420 {
6421 if (STAT_TYPE_VISIBLE_P (value))
6422 type = STAT_TYPE (value);
6423 value = STAT_VISIBLE (value);
6424 }
6425
6426 if (do_nonmember_using_decl (lookup, false, false,
6427 &value, &type))
6428 {
6429 failed = true;
6430 break;
6431 }
6432 }
6433 }
6434
6435 if (!failed)
6436 {
6437 /* Now do the current slot. */
6438 tree value = MAYBE_STAT_DECL (*mslot);
6439 tree type = MAYBE_STAT_TYPE (*mslot);
6440
6441 do_nonmember_using_decl (lookup, false, true, &value, &type);
6442
6443 // FIXME: Partition mergeableness?
6444 if (STAT_HACK_P (*mslot))
6445 {
6446 STAT_DECL (*mslot) = value;
6447 STAT_TYPE (*mslot) = type;
6448 }
6449 else if (type)
6450 *mslot = stat_hack (value, type);
6451 else
6452 *mslot = value;
6453 }
6454 }
6455 else
6456 {
6457 add_decl_expr (using_decl);
6458 push_using_decl_bindings (&lookup, name, NULL_TREE);
6459 }
6460 }
6461
6462 /* Return the declarations that are members of the namespace NS. */
6463
6464 tree
6465 cp_namespace_decls (tree ns)
6466 {
6467 return NAMESPACE_LEVEL (ns)->names;
6468 }
6469
6470 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6471 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6472
6473 static bool
6474 qualify_lookup (tree val, LOOK_want want)
6475 {
6476 if (val == NULL_TREE)
6477 return false;
6478
6479 if (bool (want & LOOK_want::TYPE))
6480 {
6481 tree target_val = strip_using_decl (val);
6482
6483 if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
6484 return true;
6485 }
6486
6487 if (bool (want & LOOK_want::TYPE_NAMESPACE))
6488 return TREE_CODE (val) == NAMESPACE_DECL;
6489
6490 return true;
6491 }
6492
6493 /* Is there a "using namespace std;" directive within USINGS? */
6494
6495 static bool
6496 using_directives_contain_std_p (vec<tree, va_gc> *usings)
6497 {
6498 if (!usings)
6499 return false;
6500
6501 for (unsigned ix = usings->length (); ix--;)
6502 if ((*usings)[ix] == std_node)
6503 return true;
6504
6505 return false;
6506 }
6507
6508 /* Is there a "using namespace std;" directive within the current
6509 namespace (or its ancestors)?
6510 Compare with name_lookup::search_unqualified. */
6511
6512 static bool
6513 has_using_namespace_std_directive_p ()
6514 {
6515 for (cp_binding_level *level = current_binding_level;
6516 level;
6517 level = level->level_chain)
6518 if (using_directives_contain_std_p (level->using_directives))
6519 return true;
6520
6521 return false;
6522 }
6523
6524 /* Subclass of deferred_diagnostic, for issuing a note when
6525 --param cxx-max-namespaces-for-diagnostic-help is reached.
6526
6527 The note should be issued after the error, but before any other
6528 deferred diagnostics. This is handled by decorating a wrapped
6529 deferred_diagnostic, and emitting a note before that wrapped note is
6530 deleted. */
6531
6532 class namespace_limit_reached : public deferred_diagnostic
6533 {
6534 public:
6535 namespace_limit_reached (location_t loc, unsigned limit, tree name,
6536 gnu::unique_ptr<deferred_diagnostic> wrapped)
6537 : deferred_diagnostic (loc),
6538 m_limit (limit), m_name (name),
6539 m_wrapped (move (wrapped))
6540 {
6541 }
6542
6543 ~namespace_limit_reached ()
6544 {
6545 /* Unconditionally warn that the search was truncated. */
6546 inform (get_location (),
6547 "maximum limit of %d namespaces searched for %qE",
6548 m_limit, m_name);
6549 /* m_wrapped will be implicitly deleted after this, emitting any followup
6550 diagnostic after the above note. */
6551 }
6552
6553 private:
6554 unsigned m_limit;
6555 tree m_name;
6556 gnu::unique_ptr<deferred_diagnostic> m_wrapped;
6557 };
6558
6559 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6560 Emit a note showing the location of the declaration of the suggestion. */
6561
6562 class show_candidate_location : public deferred_diagnostic
6563 {
6564 public:
6565 show_candidate_location (location_t loc, tree candidate)
6566 : deferred_diagnostic (loc),
6567 m_candidate (candidate)
6568 {
6569 }
6570
6571 ~show_candidate_location ()
6572 {
6573 inform (location_of (m_candidate), "%qE declared here", m_candidate);
6574 }
6575
6576 private:
6577 tree m_candidate;
6578 };
6579
6580 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6581 to be suggested by suggest_alternatives_for.
6582
6583 Emit a series of notes showing the various suggestions. */
6584
6585 class suggest_alternatives : public deferred_diagnostic
6586 {
6587 public:
6588 suggest_alternatives (location_t loc, vec<tree> candidates)
6589 : deferred_diagnostic (loc),
6590 m_candidates (candidates)
6591 {
6592 }
6593
6594 ~suggest_alternatives ()
6595 {
6596 if (m_candidates.length ())
6597 {
6598 inform_n (get_location (), m_candidates.length (),
6599 "suggested alternative:",
6600 "suggested alternatives:");
6601 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
6602 {
6603 tree val = m_candidates[ix];
6604
6605 inform (location_of (val), " %qE", val);
6606 }
6607 }
6608 m_candidates.release ();
6609 }
6610
6611 private:
6612 vec<tree> m_candidates;
6613 };
6614
6615 /* A class for encapsulating the result of a search across
6616 multiple namespaces (and scoped enums within them) for an
6617 unrecognized name seen at a given source location. */
6618
6619 class namespace_hints
6620 {
6621 public:
6622 namespace_hints (location_t loc, tree name);
6623
6624 name_hint convert_candidates_to_name_hint ();
6625 name_hint maybe_decorate_with_limit (name_hint);
6626
6627 private:
6628 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
6629
6630 location_t m_loc;
6631 tree m_name;
6632 vec<tree> m_candidates;
6633
6634 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6635 unsigned m_limit;
6636
6637 /* Was the limit reached? */
6638 bool m_limited;
6639 };
6640
6641 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6642 looking for an exact match for unrecognized NAME seen at LOC. */
6643
6644 namespace_hints::namespace_hints (location_t loc, tree name)
6645 : m_loc(loc), m_name (name)
6646 {
6647 auto_vec<tree> worklist;
6648
6649 m_candidates = vNULL;
6650 m_limited = false;
6651 m_limit = param_cxx_max_namespaces_for_diagnostic_help;
6652
6653 /* Breadth-first search of namespaces. Up to limit namespaces
6654 searched (limit zero == unlimited). */
6655 worklist.safe_push (global_namespace);
6656 for (unsigned ix = 0; ix != worklist.length (); ix++)
6657 {
6658 tree ns = worklist[ix];
6659 name_lookup lookup (name);
6660
6661 if (lookup.search_qualified (ns, false))
6662 m_candidates.safe_push (lookup.value);
6663
6664 if (!m_limited)
6665 {
6666 /* Look for child namespaces. We have to do this
6667 indirectly because they are chained in reverse order,
6668 which is confusing to the user. */
6669 auto_vec<tree> children;
6670
6671 for (tree decl = NAMESPACE_LEVEL (ns)->names;
6672 decl; decl = TREE_CHAIN (decl))
6673 {
6674 if (TREE_CODE (decl) == NAMESPACE_DECL
6675 && !DECL_NAMESPACE_ALIAS (decl)
6676 && !DECL_NAMESPACE_INLINE_P (decl))
6677 children.safe_push (decl);
6678
6679 /* Look for exact matches for NAME within scoped enums.
6680 These aren't added to the worklist, and so don't count
6681 against the search limit. */
6682 if (TREE_CODE (decl) == TYPE_DECL)
6683 {
6684 tree type = TREE_TYPE (decl);
6685 if (SCOPED_ENUM_P (type))
6686 maybe_add_candidate_for_scoped_enum (type, name);
6687 }
6688 }
6689
6690 while (!m_limited && !children.is_empty ())
6691 {
6692 if (worklist.length () == m_limit)
6693 m_limited = true;
6694 else
6695 worklist.safe_push (children.pop ());
6696 }
6697 }
6698 }
6699 }
6700
6701 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6702 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6703
6704 If m_candidates is non-empty, use it to generate a suggestion and/or
6705 a deferred diagnostic that lists the possible candidate(s).
6706 */
6707
6708 name_hint
6709 namespace_hints::convert_candidates_to_name_hint ()
6710 {
6711 /* How many candidates do we have? */
6712
6713 /* If we have just one candidate, issue a name_hint with it as a suggestion
6714 (so that consumers are able to suggest it within the error message and emit
6715 it as a fix-it hint), and with a note showing the candidate's location. */
6716 if (m_candidates.length () == 1)
6717 {
6718 tree candidate = m_candidates[0];
6719 /* Clean up CANDIDATES. */
6720 m_candidates.release ();
6721 return name_hint (expr_to_string (candidate),
6722 new show_candidate_location (m_loc, candidate));
6723 }
6724 else if (m_candidates.length () > 1)
6725 /* If we have more than one candidate, issue a name_hint without a single
6726 "suggestion", but with a deferred diagnostic that lists the
6727 various candidates. This takes ownership of m_candidates. */
6728 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
6729
6730 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6731 gcc_assert (m_candidates.length () == 0);
6732 gcc_assert (m_candidates == vNULL);
6733
6734 return name_hint ();
6735 }
6736
6737 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6738 then we want to emit a note about after the error, but before
6739 any other deferred diagnostics.
6740
6741 Handle this by figuring out what hint is needed, then optionally
6742 decorating HINT with a namespace_limit_reached wrapper. */
6743
6744 name_hint
6745 namespace_hints::maybe_decorate_with_limit (name_hint hint)
6746 {
6747 if (m_limited)
6748 return name_hint (hint.suggestion (),
6749 new namespace_limit_reached (m_loc, m_limit,
6750 m_name,
6751 hint.take_deferred ()));
6752 else
6753 return hint;
6754 }
6755
6756 /* Look inside SCOPED_ENUM for exact matches for NAME.
6757 If one is found, add its CONST_DECL to m_candidates. */
6758
6759 void
6760 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
6761 tree name)
6762 {
6763 gcc_assert (SCOPED_ENUM_P (scoped_enum));
6764
6765 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
6766 {
6767 tree id = TREE_PURPOSE (iter);
6768 if (id == name)
6769 {
6770 m_candidates.safe_push (TREE_VALUE (iter));
6771 return;
6772 }
6773 }
6774 }
6775
6776 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6777 name lookup failed.
6778
6779 Search through all available namespaces and any scoped enums within them
6780 and generate a suggestion and/or a deferred diagnostic that lists possible
6781 candidate(s).
6782
6783 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
6784 look for near-matches and suggest the best near-match, if there is one.
6785
6786 If nothing is found, then an empty name_hint is returned. */
6787
6788 name_hint
6789 suggest_alternatives_for (location_t location, tree name,
6790 bool suggest_misspellings)
6791 {
6792 /* First, search for exact matches in other namespaces. */
6793 namespace_hints ns_hints (location, name);
6794 name_hint result = ns_hints.convert_candidates_to_name_hint ();
6795
6796 /* Otherwise, try other approaches. */
6797 if (!result)
6798 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
6799
6800 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
6801 }
6802
6803 /* The second half of suggest_alternatives_for, for when no exact matches
6804 were found in other namespaces. */
6805
6806 static name_hint
6807 suggest_alternatives_for_1 (location_t location, tree name,
6808 bool suggest_misspellings)
6809 {
6810 /* No candidates were found in the available namespaces. */
6811
6812 /* If there's a "using namespace std;" active, and this
6813 is one of the most common "std::" names, then it's probably a
6814 missing #include. */
6815 if (has_using_namespace_std_directive_p ())
6816 {
6817 name_hint hint = maybe_suggest_missing_std_header (location, name);
6818 if (hint)
6819 return hint;
6820 }
6821
6822 /* Otherwise, consider misspellings. */
6823 if (!suggest_misspellings)
6824 return name_hint ();
6825
6826 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
6827 }
6828
6829 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6830 name lookup failed.
6831
6832 Search through all available namespaces and generate a suggestion and/or
6833 a deferred diagnostic that lists possible candidate(s).
6834
6835 This is similiar to suggest_alternatives_for, but doesn't fallback to
6836 the other approaches used by that function. */
6837
6838 name_hint
6839 suggest_alternatives_in_other_namespaces (location_t location, tree name)
6840 {
6841 namespace_hints ns_hints (location, name);
6842
6843 name_hint result = ns_hints.convert_candidates_to_name_hint ();
6844
6845 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
6846 }
6847
6848 /* A well-known name within the C++ standard library, returned by
6849 get_std_name_hint. */
6850
6851 struct std_name_hint
6852 {
6853 /* A name within "std::". */
6854 const char *name;
6855
6856 /* The header name defining it within the C++ Standard Library
6857 (with '<' and '>'). */
6858 const char *header;
6859
6860 /* The dialect of C++ in which this was added. */
6861 enum cxx_dialect min_dialect;
6862 };
6863
6864 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
6865 for some of the most common names within "std::".
6866 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
6867
6868 static const std_name_hint *
6869 get_std_name_hint (const char *name)
6870 {
6871 static const std_name_hint hints[] = {
6872 /* <any>. */
6873 {"any", "<any>", cxx17},
6874 {"any_cast", "<any>", cxx17},
6875 {"make_any", "<any>", cxx17},
6876 /* <array>. */
6877 {"array", "<array>", cxx11},
6878 {"to_array", "<array>", cxx20},
6879 /* <atomic>. */
6880 {"atomic", "<atomic>", cxx11},
6881 {"atomic_flag", "<atomic>", cxx11},
6882 {"atomic_ref", "<atomic>", cxx20},
6883 /* <bitset>. */
6884 {"bitset", "<bitset>", cxx11},
6885 /* <compare> */
6886 {"weak_equality", "<compare>", cxx20},
6887 {"strong_equality", "<compare>", cxx20},
6888 {"partial_ordering", "<compare>", cxx20},
6889 {"weak_ordering", "<compare>", cxx20},
6890 {"strong_ordering", "<compare>", cxx20},
6891 /* <complex>. */
6892 {"complex", "<complex>", cxx98},
6893 {"complex_literals", "<complex>", cxx14},
6894 /* <condition_variable>. */
6895 {"condition_variable", "<condition_variable>", cxx11},
6896 {"condition_variable_any", "<condition_variable>", cxx11},
6897 /* <cstddef>. */
6898 {"byte", "<cstddef>", cxx17},
6899 /* <deque>. */
6900 {"deque", "<deque>", cxx98},
6901 /* <forward_list>. */
6902 {"forward_list", "<forward_list>", cxx11},
6903 /* <fstream>. */
6904 {"basic_filebuf", "<fstream>", cxx98},
6905 {"basic_ifstream", "<fstream>", cxx98},
6906 {"basic_ofstream", "<fstream>", cxx98},
6907 {"basic_fstream", "<fstream>", cxx98},
6908 {"fstream", "<fstream>", cxx98},
6909 {"ifstream", "<fstream>", cxx98},
6910 {"ofstream", "<fstream>", cxx98},
6911 /* <functional>. */
6912 {"bind", "<functional>", cxx11},
6913 {"bind_front", "<functional>", cxx20},
6914 {"function", "<functional>", cxx11},
6915 {"hash", "<functional>", cxx11},
6916 {"invoke", "<functional>", cxx17},
6917 {"mem_fn", "<functional>", cxx11},
6918 {"not_fn", "<functional>", cxx17},
6919 {"reference_wrapper", "<functional>", cxx11},
6920 {"unwrap_reference", "<functional>", cxx20},
6921 {"unwrap_reference_t", "<functional>", cxx20},
6922 {"unwrap_ref_decay", "<functional>", cxx20},
6923 {"unwrap_ref_decay_t", "<functional>", cxx20},
6924 /* <future>. */
6925 {"async", "<future>", cxx11},
6926 {"future", "<future>", cxx11},
6927 {"packaged_task", "<future>", cxx11},
6928 {"promise", "<future>", cxx11},
6929 /* <iostream>. */
6930 {"cin", "<iostream>", cxx98},
6931 {"cout", "<iostream>", cxx98},
6932 {"cerr", "<iostream>", cxx98},
6933 {"clog", "<iostream>", cxx98},
6934 {"wcin", "<iostream>", cxx98},
6935 {"wcout", "<iostream>", cxx98},
6936 {"wclog", "<iostream>", cxx98},
6937 /* <istream>. */
6938 {"istream", "<istream>", cxx98},
6939 /* <iterator>. */
6940 {"advance", "<iterator>", cxx98},
6941 {"back_inserter", "<iterator>", cxx98},
6942 {"begin", "<iterator>", cxx11},
6943 {"distance", "<iterator>", cxx98},
6944 {"end", "<iterator>", cxx11},
6945 {"front_inserter", "<iterator>", cxx98},
6946 {"inserter", "<iterator>", cxx98},
6947 {"istream_iterator", "<iterator>", cxx98},
6948 {"istreambuf_iterator", "<iterator>", cxx98},
6949 {"iterator_traits", "<iterator>", cxx98},
6950 {"move_iterator", "<iterator>", cxx11},
6951 {"next", "<iterator>", cxx11},
6952 {"ostream_iterator", "<iterator>", cxx98},
6953 {"ostreambuf_iterator", "<iterator>", cxx98},
6954 {"prev", "<iterator>", cxx11},
6955 {"reverse_iterator", "<iterator>", cxx98},
6956 /* <ostream>. */
6957 {"ostream", "<ostream>", cxx98},
6958 /* <list>. */
6959 {"list", "<list>", cxx98},
6960 /* <map>. */
6961 {"map", "<map>", cxx98},
6962 {"multimap", "<map>", cxx98},
6963 /* <memory>. */
6964 {"allocate_shared", "<memory>", cxx11},
6965 {"allocator", "<memory>", cxx98},
6966 {"allocator_traits", "<memory>", cxx11},
6967 {"make_shared", "<memory>", cxx11},
6968 {"make_unique", "<memory>", cxx14},
6969 {"shared_ptr", "<memory>", cxx11},
6970 {"unique_ptr", "<memory>", cxx11},
6971 {"weak_ptr", "<memory>", cxx11},
6972 /* <memory_resource>. */
6973 {"pmr", "<memory_resource>", cxx17},
6974 /* <mutex>. */
6975 {"mutex", "<mutex>", cxx11},
6976 {"timed_mutex", "<mutex>", cxx11},
6977 {"recursive_mutex", "<mutex>", cxx11},
6978 {"recursive_timed_mutex", "<mutex>", cxx11},
6979 {"once_flag", "<mutex>", cxx11},
6980 {"call_once,", "<mutex>", cxx11},
6981 {"lock", "<mutex>", cxx11},
6982 {"scoped_lock", "<mutex>", cxx17},
6983 {"try_lock", "<mutex>", cxx11},
6984 {"lock_guard", "<mutex>", cxx11},
6985 {"unique_lock", "<mutex>", cxx11},
6986 /* <optional>. */
6987 {"optional", "<optional>", cxx17},
6988 {"make_optional", "<optional>", cxx17},
6989 /* <ostream>. */
6990 {"ostream", "<ostream>", cxx98},
6991 {"wostream", "<ostream>", cxx98},
6992 {"ends", "<ostream>", cxx98},
6993 {"flush", "<ostream>", cxx98},
6994 {"endl", "<ostream>", cxx98},
6995 /* <queue>. */
6996 {"queue", "<queue>", cxx98},
6997 {"priority_queue", "<queue>", cxx98},
6998 /* <set>. */
6999 {"set", "<set>", cxx98},
7000 {"multiset", "<set>", cxx98},
7001 /* <shared_mutex>. */
7002 {"shared_lock", "<shared_mutex>", cxx14},
7003 {"shared_mutex", "<shared_mutex>", cxx17},
7004 {"shared_timed_mutex", "<shared_mutex>", cxx14},
7005 /* <source_location>. */
7006 {"source_location", "<source_location>", cxx20},
7007 /* <sstream>. */
7008 {"basic_stringbuf", "<sstream>", cxx98},
7009 {"basic_istringstream", "<sstream>", cxx98},
7010 {"basic_ostringstream", "<sstream>", cxx98},
7011 {"basic_stringstream", "<sstream>", cxx98},
7012 {"istringstream", "<sstream>", cxx98},
7013 {"ostringstream", "<sstream>", cxx98},
7014 {"stringstream", "<sstream>", cxx98},
7015 /* <stack>. */
7016 {"stack", "<stack>", cxx98},
7017 /* <string>. */
7018 {"basic_string", "<string>", cxx98},
7019 {"string", "<string>", cxx98},
7020 {"wstring", "<string>", cxx98},
7021 {"u8string", "<string>", cxx20},
7022 {"u16string", "<string>", cxx11},
7023 {"u32string", "<string>", cxx11},
7024 /* <string_view>. */
7025 {"basic_string_view", "<string_view>", cxx17},
7026 {"string_view", "<string_view>", cxx17},
7027 /* <thread>. */
7028 {"thread", "<thread>", cxx11},
7029 {"this_thread", "<thread>", cxx11},
7030 /* <tuple>. */
7031 {"apply", "<tuple>", cxx17},
7032 {"forward_as_tuple", "<tuple>", cxx11},
7033 {"make_from_tuple", "<tuple>", cxx17},
7034 {"make_tuple", "<tuple>", cxx11},
7035 {"tie", "<tuple>", cxx11},
7036 {"tuple", "<tuple>", cxx11},
7037 {"tuple_cat", "<tuple>", cxx11},
7038 {"tuple_element", "<tuple>", cxx11},
7039 {"tuple_element_t", "<tuple>", cxx14},
7040 {"tuple_size", "<tuple>", cxx11},
7041 {"tuple_size_v", "<tuple>", cxx17},
7042 /* <type_traits>. */
7043 {"enable_if", "<type_traits>", cxx11},
7044 {"enable_if_t", "<type_traits>", cxx14},
7045 {"invoke_result", "<type_traits>", cxx17},
7046 {"invoke_result_t", "<type_traits>", cxx17},
7047 {"remove_cvref", "<type_traits>", cxx20},
7048 {"remove_cvref_t", "<type_traits>", cxx20},
7049 {"type_identity", "<type_traits>", cxx20},
7050 {"type_identity_t", "<type_traits>", cxx20},
7051 {"void_t", "<type_traits>", cxx17},
7052 {"conjunction", "<type_traits>", cxx17},
7053 {"conjunction_v", "<type_traits>", cxx17},
7054 {"disjunction", "<type_traits>", cxx17},
7055 {"disjunction_v", "<type_traits>", cxx17},
7056 {"negation", "<type_traits>", cxx17},
7057 {"negation_v", "<type_traits>", cxx17},
7058 /* <unordered_map>. */
7059 {"unordered_map", "<unordered_map>", cxx11},
7060 {"unordered_multimap", "<unordered_map>", cxx11},
7061 /* <unordered_set>. */
7062 {"unordered_set", "<unordered_set>", cxx11},
7063 {"unordered_multiset", "<unordered_set>", cxx11},
7064 /* <utility>. */
7065 {"declval", "<utility>", cxx11},
7066 {"forward", "<utility>", cxx11},
7067 {"make_pair", "<utility>", cxx98},
7068 {"move", "<utility>", cxx11},
7069 {"pair", "<utility>", cxx98},
7070 /* <variant>. */
7071 {"variant", "<variant>", cxx17},
7072 {"visit", "<variant>", cxx17},
7073 /* <vector>. */
7074 {"vector", "<vector>", cxx98},
7075 };
7076 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
7077 for (size_t i = 0; i < num_hints; i++)
7078 {
7079 if (strcmp (name, hints[i].name) == 0)
7080 return &hints[i];
7081 }
7082 return NULL;
7083 }
7084
7085 /* Describe DIALECT. */
7086
7087 const char *
7088 get_cxx_dialect_name (enum cxx_dialect dialect)
7089 {
7090 switch (dialect)
7091 {
7092 default:
7093 gcc_unreachable ();
7094 case cxx98:
7095 return "C++98";
7096 case cxx11:
7097 return "C++11";
7098 case cxx14:
7099 return "C++14";
7100 case cxx17:
7101 return "C++17";
7102 case cxx20:
7103 return "C++20";
7104 }
7105 }
7106
7107 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7108 that weren't recognized, but for which we know which header it ought to be
7109 in.
7110
7111 Emit a note either suggesting the header to be included, or noting that
7112 the current dialect is too early for the given name. */
7113
7114 class missing_std_header : public deferred_diagnostic
7115 {
7116 public:
7117 missing_std_header (location_t loc,
7118 const char *name_str,
7119 const std_name_hint *header_hint)
7120 : deferred_diagnostic (loc),
7121 m_name_str (name_str),
7122 m_header_hint (header_hint)
7123 {}
7124 ~missing_std_header ()
7125 {
7126 gcc_rich_location richloc (get_location ());
7127 if (cxx_dialect >= m_header_hint->min_dialect)
7128 {
7129 const char *header = m_header_hint->header;
7130 maybe_add_include_fixit (&richloc, header, true);
7131 inform (&richloc,
7132 "%<std::%s%> is defined in header %qs;"
7133 " did you forget to %<#include %s%>?",
7134 m_name_str, header, header);
7135 }
7136 else
7137 inform (&richloc,
7138 "%<std::%s%> is only available from %s onwards",
7139 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
7140 }
7141
7142 private:
7143 const char *m_name_str;
7144 const std_name_hint *m_header_hint;
7145 };
7146
7147 /* Attempt to generate a name_hint that suggests pertinent header files
7148 for NAME at LOCATION, for common names within the "std" namespace,
7149 or an empty name_hint if this isn't applicable. */
7150
7151 static name_hint
7152 maybe_suggest_missing_std_header (location_t location, tree name)
7153 {
7154 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7155
7156 const char *name_str = IDENTIFIER_POINTER (name);
7157 const std_name_hint *header_hint = get_std_name_hint (name_str);
7158 if (!header_hint)
7159 return name_hint ();
7160
7161 return name_hint (NULL, new missing_std_header (location, name_str,
7162 header_hint));
7163 }
7164
7165 /* Attempt to generate a name_hint that suggests a missing header file
7166 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7167 applicable. */
7168
7169 static name_hint
7170 maybe_suggest_missing_header (location_t location, tree name, tree scope)
7171 {
7172 if (scope == NULL_TREE)
7173 return name_hint ();
7174 if (TREE_CODE (scope) != NAMESPACE_DECL)
7175 return name_hint ();
7176 /* We only offer suggestions for the "std" namespace. */
7177 if (scope != std_node)
7178 return name_hint ();
7179 return maybe_suggest_missing_std_header (location, name);
7180 }
7181
7182 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7183 lookup failed within the explicitly provided SCOPE.
7184
7185 Suggest the best meaningful candidates (if any), otherwise
7186 an empty name_hint is returned. */
7187
7188 name_hint
7189 suggest_alternative_in_explicit_scope (location_t location, tree name,
7190 tree scope)
7191 {
7192 /* Something went very wrong; don't suggest anything. */
7193 if (name == error_mark_node)
7194 return name_hint ();
7195
7196 /* Resolve any namespace aliases. */
7197 scope = ORIGINAL_NAMESPACE (scope);
7198
7199 name_hint hint = maybe_suggest_missing_header (location, name, scope);
7200 if (hint)
7201 return hint;
7202
7203 cp_binding_level *level = NAMESPACE_LEVEL (scope);
7204
7205 best_match <tree, const char *> bm (name);
7206 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
7207
7208 /* See if we have a good suggesion for the user. */
7209 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
7210 if (fuzzy_name)
7211 return name_hint (fuzzy_name, NULL);
7212
7213 return name_hint ();
7214 }
7215
7216 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7217 candidates. */
7218
7219 name_hint
7220 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
7221 {
7222 gcc_assert (SCOPED_ENUM_P (scoped_enum));
7223
7224 best_match <tree, const char *> bm (name);
7225 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7226 {
7227 tree id = TREE_PURPOSE (iter);
7228 bm.consider (IDENTIFIER_POINTER (id));
7229 }
7230 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7231 }
7232
7233 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7234 or a class TYPE).
7235
7236 WANT as for lookup_name_1.
7237
7238 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7239 declaration found. If no suitable declaration can be found,
7240 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7241 neither a class-type nor a namespace a diagnostic is issued. */
7242
7243 tree
7244 lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
7245 {
7246 tree t = NULL_TREE;
7247
7248 if (TREE_CODE (scope) == NAMESPACE_DECL)
7249 {
7250 name_lookup lookup (name, want);
7251
7252 if (qualified_namespace_lookup (scope, &lookup))
7253 {
7254 t = lookup.value;
7255
7256 /* If we have a known type overload, pull it out. This can happen
7257 for using decls. */
7258 if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
7259 t = OVL_FUNCTION (t);
7260 }
7261 }
7262 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
7263 t = lookup_enumerator (scope, name);
7264 else if (is_class_type (scope, complain))
7265 t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
7266 tf_warning_or_error);
7267
7268 if (!t)
7269 return error_mark_node;
7270 return t;
7271 }
7272
7273 /* Wrapper for the above that takes a string argument. The function name is
7274 not at the beginning of the line to keep this wrapper out of etags. */
7275
7276 tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
7277 {
7278 return lookup_qualified_name (t, get_identifier (p), w, c);
7279 }
7280
7281 /* [namespace.qual]
7282 Accepts the NAME to lookup and its qualifying SCOPE.
7283 Returns the name/type pair found into the cxx_binding *RESULT,
7284 or false on error. */
7285
7286 static bool
7287 qualified_namespace_lookup (tree scope, name_lookup *lookup)
7288 {
7289 timevar_start (TV_NAME_LOOKUP);
7290 query_oracle (lookup->name);
7291 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
7292 timevar_stop (TV_NAME_LOOKUP);
7293 return found;
7294 }
7295
7296 /* If DECL is suitably visible to the user, consider its name for
7297 spelling correction. */
7298
7299 static void
7300 consider_decl (tree decl, best_match <tree, const char *> &bm,
7301 bool consider_impl_names)
7302 {
7303 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7304 within range for). */
7305 if (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl))
7306 return;
7307
7308 tree suggestion = DECL_NAME (decl);
7309 if (!suggestion)
7310 return;
7311
7312 /* Don't suggest names that are for anonymous aggregate types, as
7313 they are an implementation detail generated by the compiler. */
7314 if (IDENTIFIER_ANON_P (suggestion))
7315 return;
7316
7317 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
7318
7319 /* Ignore internal names with spaces in them. */
7320 if (strchr (suggestion_str, ' '))
7321 return;
7322
7323 /* Don't suggest names that are reserved for use by the
7324 implementation, unless NAME began with an underscore. */
7325 if (!consider_impl_names
7326 && name_reserved_for_implementation_p (suggestion_str))
7327 return;
7328
7329 bm.consider (suggestion_str);
7330 }
7331
7332 /* If DECL is suitably visible to the user, add its name to VEC and
7333 return true. Otherwise return false. */
7334
7335 static bool
7336 maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
7337 {
7338 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7339 within range for). */
7340 if (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl))
7341 return false;
7342
7343 tree suggestion = DECL_NAME (decl);
7344 if (!suggestion)
7345 return false;
7346
7347 /* Don't suggest names that are for anonymous aggregate types, as
7348 they are an implementation detail generated by the compiler. */
7349 if (IDENTIFIER_ANON_P (suggestion))
7350 return false;
7351
7352 vec.safe_push (suggestion);
7353
7354 return true;
7355 }
7356
7357 /* Examing the namespace binding BINDING, and add at most one instance
7358 of the name, if it contains a visible entity of interest. Return
7359 true if we added something. */
7360
7361 bool
7362 maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
7363 lookup_name_fuzzy_kind kind)
7364 {
7365 tree value = NULL_TREE;
7366
7367 if (STAT_HACK_P (binding))
7368 {
7369 if (!STAT_TYPE_HIDDEN_P (binding)
7370 && STAT_TYPE (binding))
7371 {
7372 if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
7373 return true;
7374 }
7375 else if (!STAT_DECL_HIDDEN_P (binding))
7376 value = STAT_DECL (binding);
7377 }
7378 else
7379 value = binding;
7380
7381 value = ovl_skip_hidden (value);
7382 if (value)
7383 {
7384 value = OVL_FIRST (value);
7385 if (kind != FUZZY_LOOKUP_TYPENAME
7386 || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
7387 if (maybe_add_fuzzy_decl (vec, value))
7388 return true;
7389 }
7390
7391 /* Nothing found. */
7392 return false;
7393 }
7394
7395 /* Helper function for lookup_name_fuzzy.
7396 Traverse binding level LVL, looking for good name matches for NAME
7397 (and BM). */
7398 static void
7399 consider_binding_level (tree name, best_match <tree, const char *> &bm,
7400 cp_binding_level *lvl, bool look_within_fields,
7401 enum lookup_name_fuzzy_kind kind)
7402 {
7403 if (look_within_fields)
7404 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
7405 {
7406 tree type = lvl->this_entity;
7407 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
7408 tree best_matching_field
7409 = lookup_member_fuzzy (type, name, want_type_p);
7410 if (best_matching_field)
7411 bm.consider (IDENTIFIER_POINTER (best_matching_field));
7412 }
7413
7414 /* Only suggest names reserved for the implementation if NAME begins
7415 with an underscore. */
7416 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
7417
7418 if (lvl->kind != sk_namespace)
7419 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
7420 {
7421 tree d = t;
7422
7423 /* OVERLOADs or decls from using declaration are wrapped into
7424 TREE_LIST. */
7425 if (TREE_CODE (d) == TREE_LIST)
7426 d = OVL_FIRST (TREE_VALUE (d));
7427
7428 /* Don't use bindings from implicitly declared functions,
7429 as they were likely misspellings themselves. */
7430 if (TREE_TYPE (d) == error_mark_node)
7431 continue;
7432
7433 /* If we want a typename, ignore non-types. */
7434 if (kind == FUZZY_LOOKUP_TYPENAME
7435 && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
7436 continue;
7437
7438 consider_decl (d, bm, consider_implementation_names);
7439 }
7440 else
7441 {
7442 /* We need to iterate over the namespace hash table, in order to
7443 not mention hidden entities. But hash table iteration is
7444 (essentially) unpredictable, our correction-distance measure
7445 is very granular, and we pick the first of equal distances.
7446 Hence, we need to call the distance-measurer in a predictable
7447 order. So, iterate over the namespace hash, inserting
7448 visible names into a vector. Then sort the vector. Then
7449 determine spelling distance. */
7450
7451 tree ns = lvl->this_entity;
7452 auto_vec<tree> vec;
7453
7454 hash_table<named_decl_hash>::iterator end
7455 (DECL_NAMESPACE_BINDINGS (ns)->end ());
7456 for (hash_table<named_decl_hash>::iterator iter
7457 (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
7458 {
7459 tree binding = *iter;
7460
7461 if (TREE_CODE (binding) == BINDING_VECTOR)
7462 {
7463 bitmap imports = get_import_bitmap ();
7464 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
7465
7466 if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
7467 if (maybe_add_fuzzy_binding (vec, bind, kind))
7468 continue;
7469
7470 /* Scan the imported bindings. */
7471 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
7472 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7473 {
7474 ix--;
7475 cluster++;
7476 }
7477
7478 for (; ix--; cluster++)
7479 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
7480 jx++)
7481 {
7482 /* Are we importing this module? */
7483 if (unsigned base = cluster->indices[jx].base)
7484 if (unsigned span = cluster->indices[jx].span)
7485 do
7486 if (bitmap_bit_p (imports, base))
7487 goto found;
7488 while (++base, --span);
7489 continue;
7490
7491 found:;
7492 /* Is it loaded? */
7493 if (cluster->slots[jx].is_lazy ())
7494 /* Let's not read in everything on the first
7495 spello! **/
7496 continue;
7497 if (tree bind = cluster->slots[jx])
7498 if (maybe_add_fuzzy_binding (vec, bind, kind))
7499 break;
7500 }
7501 }
7502 else
7503 maybe_add_fuzzy_binding (vec, binding, kind);
7504 }
7505
7506 vec.qsort ([] (const void *a_, const void *b_)
7507 {
7508 return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
7509 IDENTIFIER_POINTER (*(const tree *)b_));
7510 });
7511
7512 /* Examine longest to shortest. */
7513 for (unsigned ix = vec.length (); ix--;)
7514 {
7515 const char *str = IDENTIFIER_POINTER (vec[ix]);
7516
7517 /* Ignore internal names with spaces in them. */
7518 if (strchr (str, ' '))
7519 continue;
7520
7521 /* Don't suggest names that are reserved for use by the
7522 implementation, unless NAME began with an underscore. */
7523 if (!consider_implementation_names
7524 && name_reserved_for_implementation_p (str))
7525 continue;
7526
7527 bm.consider (str);
7528 }
7529 }
7530 }
7531
7532 /* Subclass of deferred_diagnostic. Notify the user that the
7533 given macro was used before it was defined.
7534 This can be done in the C++ frontend since tokenization happens
7535 upfront. */
7536
7537 class macro_use_before_def : public deferred_diagnostic
7538 {
7539 public:
7540 /* Factory function. Return a new macro_use_before_def instance if
7541 appropriate, or return NULL. */
7542 static macro_use_before_def *
7543 maybe_make (location_t use_loc, cpp_hashnode *macro)
7544 {
7545 location_t def_loc = cpp_macro_definition_location (macro);
7546 if (def_loc == UNKNOWN_LOCATION)
7547 return NULL;
7548
7549 /* We only want to issue a note if the macro was used *before* it was
7550 defined.
7551 We don't want to issue a note for cases where a macro was incorrectly
7552 used, leaving it unexpanded (e.g. by using the wrong argument
7553 count). */
7554 if (!linemap_location_before_p (line_table, use_loc, def_loc))
7555 return NULL;
7556
7557 return new macro_use_before_def (use_loc, macro);
7558 }
7559
7560 private:
7561 /* Ctor. LOC is the location of the usage. MACRO is the
7562 macro that was used. */
7563 macro_use_before_def (location_t loc, cpp_hashnode *macro)
7564 : deferred_diagnostic (loc), m_macro (macro)
7565 {
7566 gcc_assert (macro);
7567 }
7568
7569 ~macro_use_before_def ()
7570 {
7571 if (is_suppressed_p ())
7572 return;
7573
7574 inform (get_location (), "the macro %qs had not yet been defined",
7575 (const char *)m_macro->ident.str);
7576 inform (cpp_macro_definition_location (m_macro),
7577 "it was later defined here");
7578 }
7579
7580 private:
7581 cpp_hashnode *m_macro;
7582 };
7583
7584 /* Determine if it can ever make sense to offer RID as a suggestion for
7585 a misspelling.
7586
7587 Subroutine of lookup_name_fuzzy. */
7588
7589 static bool
7590 suggest_rid_p (enum rid rid)
7591 {
7592 switch (rid)
7593 {
7594 /* Support suggesting function-like keywords. */
7595 case RID_STATIC_ASSERT:
7596 return true;
7597
7598 default:
7599 /* Support suggesting the various decl-specifier words, to handle
7600 e.g. "singed" vs "signed" typos. */
7601 if (cp_keyword_starts_decl_specifier_p (rid))
7602 return true;
7603
7604 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7605 and "do" for short misspellings, which are likely to lead to
7606 nonsensical results. */
7607 return false;
7608 }
7609 }
7610
7611 /* Search for near-matches for NAME within the current bindings, and within
7612 macro names, returning the best match as a const char *, or NULL if
7613 no reasonable match is found.
7614
7615 Use LOC for any deferred diagnostics. */
7616
7617 name_hint
7618 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
7619 {
7620 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7621
7622 /* First, try some well-known names in the C++ standard library, in case
7623 the user forgot a #include. */
7624 const char *header_hint
7625 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
7626 if (header_hint)
7627 return name_hint (NULL,
7628 new suggest_missing_header (loc,
7629 IDENTIFIER_POINTER (name),
7630 header_hint));
7631
7632 best_match <tree, const char *> bm (name);
7633
7634 cp_binding_level *lvl;
7635 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
7636 consider_binding_level (name, bm, lvl, true, kind);
7637
7638 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
7639 consider_binding_level (name, bm, lvl, false, kind);
7640
7641 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7642 as:
7643 x = SOME_OTHER_MACRO (y);
7644 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7645 as a misspelled identifier.
7646
7647 Use the best distance so far so that a candidate is only set if
7648 a macro is better than anything so far. This allows early rejection
7649 (without calculating the edit distance) of macro names that must have
7650 distance >= bm.get_best_distance (), and means that we only get a
7651 non-NULL result for best_macro_match if it's better than any of
7652 the identifiers already checked. */
7653 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
7654 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
7655 /* If a macro is the closest so far to NAME, consider it. */
7656 if (best_macro)
7657 bm.consider ((const char *)best_macro->ident.str);
7658 else if (bmm.get_best_distance () == 0)
7659 {
7660 /* If we have an exact match for a macro name, then either the
7661 macro was used with the wrong argument count, or the macro
7662 has been used before it was defined. */
7663 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
7664 if (cpp_user_macro_p (macro))
7665 return name_hint (NULL,
7666 macro_use_before_def::maybe_make (loc, macro));
7667 }
7668
7669 /* Try the "starts_decl_specifier_p" keywords to detect
7670 "singed" vs "signed" typos. */
7671 for (unsigned i = 0; i < num_c_common_reswords; i++)
7672 {
7673 const c_common_resword *resword = &c_common_reswords[i];
7674
7675 if (!suggest_rid_p (resword->rid))
7676 continue;
7677
7678 tree resword_identifier = ridpointers [resword->rid];
7679 if (!resword_identifier)
7680 continue;
7681 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
7682
7683 /* Only consider reserved words that survived the
7684 filtering in init_reswords (e.g. for -std). */
7685 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
7686 continue;
7687
7688 bm.consider (IDENTIFIER_POINTER (resword_identifier));
7689 }
7690
7691 return name_hint (bm.get_best_meaningful_candidate (), NULL);
7692 }
7693
7694 /* Subroutine of outer_binding.
7695
7696 Returns TRUE if BINDING is a binding to a template parameter of
7697 SCOPE. In that case SCOPE is the scope of a primary template
7698 parameter -- in the sense of G++, i.e, a template that has its own
7699 template header.
7700
7701 Returns FALSE otherwise. */
7702
7703 static bool
7704 binding_to_template_parms_of_scope_p (cxx_binding *binding,
7705 cp_binding_level *scope)
7706 {
7707 tree binding_value, tmpl, tinfo;
7708 int level;
7709
7710 if (!binding || !scope || !scope->this_entity)
7711 return false;
7712
7713 binding_value = binding->value ? binding->value : binding->type;
7714 tinfo = get_template_info (scope->this_entity);
7715
7716 /* BINDING_VALUE must be a template parm. */
7717 if (binding_value == NULL_TREE
7718 || (!DECL_P (binding_value)
7719 || !DECL_TEMPLATE_PARM_P (binding_value)))
7720 return false;
7721
7722 /* The level of BINDING_VALUE. */
7723 level =
7724 template_type_parameter_p (binding_value)
7725 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7726 (TREE_TYPE (binding_value)))
7727 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
7728
7729 /* The template of the current scope, iff said scope is a primary
7730 template. */
7731 tmpl = (tinfo
7732 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
7733 ? TI_TEMPLATE (tinfo)
7734 : NULL_TREE);
7735
7736 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7737 then BINDING_VALUE is a parameter of TMPL. */
7738 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
7739 }
7740
7741 /* Return the innermost non-namespace binding for NAME from a scope
7742 containing BINDING, or, if BINDING is NULL, the current scope.
7743 Please note that for a given template, the template parameters are
7744 considered to be in the scope containing the current scope.
7745 If CLASS_P is false, then class bindings are ignored. */
7746
7747 cxx_binding *
7748 outer_binding (tree name,
7749 cxx_binding *binding,
7750 bool class_p)
7751 {
7752 cxx_binding *outer;
7753 cp_binding_level *scope;
7754 cp_binding_level *outer_scope;
7755
7756 if (binding)
7757 {
7758 scope = binding->scope->level_chain;
7759 outer = binding->previous;
7760 }
7761 else
7762 {
7763 scope = current_binding_level;
7764 outer = IDENTIFIER_BINDING (name);
7765 }
7766 outer_scope = outer ? outer->scope : NULL;
7767
7768 /* Because we create class bindings lazily, we might be missing a
7769 class binding for NAME. If there are any class binding levels
7770 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7771 declared, we must lookup NAME in those class scopes. */
7772 if (class_p)
7773 while (scope && scope != outer_scope && scope->kind != sk_namespace)
7774 {
7775 if (scope->kind == sk_class)
7776 {
7777 cxx_binding *class_binding;
7778
7779 class_binding = get_class_binding (name, scope);
7780 if (class_binding)
7781 {
7782 /* Thread this new class-scope binding onto the
7783 IDENTIFIER_BINDING list so that future lookups
7784 find it quickly. */
7785 class_binding->previous = outer;
7786 if (binding)
7787 binding->previous = class_binding;
7788 else
7789 IDENTIFIER_BINDING (name) = class_binding;
7790 return class_binding;
7791 }
7792 }
7793 /* If we are in a member template, the template parms of the member
7794 template are considered to be inside the scope of the containing
7795 class, but within G++ the class bindings are all pushed between the
7796 template parms and the function body. So if the outer binding is
7797 a template parm for the current scope, return it now rather than
7798 look for a class binding. */
7799 if (outer_scope && outer_scope->kind == sk_template_parms
7800 && binding_to_template_parms_of_scope_p (outer, scope))
7801 return outer;
7802
7803 scope = scope->level_chain;
7804 }
7805
7806 return outer;
7807 }
7808
7809 /* Return the innermost block-scope or class-scope value binding for
7810 NAME, or NULL_TREE if there is no such binding. */
7811
7812 tree
7813 innermost_non_namespace_value (tree name)
7814 {
7815 cxx_binding *binding;
7816 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
7817 return binding ? binding->value : NULL_TREE;
7818 }
7819
7820 /* Look up NAME in the current binding level and its superiors in the
7821 namespace of variables, functions and typedefs. Return a ..._DECL
7822 node of some kind representing its definition if there is only one
7823 such declaration, or return a TREE_LIST with all the overloaded
7824 definitions if there are many, or return NULL_TREE if it is undefined.
7825 Hidden name, either friend declaration or built-in function, are
7826 not ignored.
7827
7828 WHERE controls which scopes are considered. It is a bit mask of
7829 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7830 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7831 scopes). It is an error for no bits to be set. These scopes are
7832 searched from innermost to outermost.
7833
7834 WANT controls what kind of entity we'd happy with.
7835 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7836 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7837 for only NAMESPACE_DECLS. These two can be bit-ored to find
7838 namespace or type.
7839
7840 WANT can also have LOOK_want::HIDDEN_FRIEND or
7841 LOOK_want::HIDDEN_LAMBDa added to it. */
7842
7843 static tree
7844 lookup_name_1 (tree name, LOOK_where where, LOOK_want want)
7845 {
7846 tree val = NULL_TREE;
7847
7848 gcc_checking_assert (unsigned (where) != 0);
7849 /* If we're looking for hidden lambda things, we shouldn't be
7850 looking in namespace scope. */
7851 gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
7852 || !bool (where & LOOK_where::NAMESPACE));
7853 query_oracle (name);
7854
7855 /* Conversion operators are handled specially because ordinary
7856 unqualified name lookup will not find template conversion
7857 operators. */
7858 if (IDENTIFIER_CONV_OP_P (name))
7859 {
7860 cp_binding_level *level;
7861
7862 for (level = current_binding_level;
7863 level && level->kind != sk_namespace;
7864 level = level->level_chain)
7865 {
7866 tree class_type;
7867 tree operators;
7868
7869 /* A conversion operator can only be declared in a class
7870 scope. */
7871 if (level->kind != sk_class)
7872 continue;
7873
7874 /* Lookup the conversion operator in the class. */
7875 class_type = level->this_entity;
7876 operators = lookup_fnfields (class_type, name, /*protect=*/0,
7877 tf_warning_or_error);
7878 if (operators)
7879 return operators;
7880 }
7881
7882 return NULL_TREE;
7883 }
7884
7885 /* First, look in non-namespace scopes. */
7886
7887 if (current_class_type == NULL_TREE)
7888 /* Maybe avoid searching the binding stack at all. */
7889 where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
7890
7891 if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
7892 for (cxx_binding *iter = nullptr;
7893 (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
7894 {
7895 /* Skip entities we don't want. */
7896 if (!bool (where & (LOCAL_BINDING_P (iter)
7897 ? LOOK_where::BLOCK : LOOK_where::CLASS)))
7898 continue;
7899
7900 /* If this is the kind of thing we're looking for, we're done. */
7901 if (iter->value)
7902 {
7903 tree binding = NULL_TREE;
7904
7905 if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
7906 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
7907 || !is_lambda_ignored_entity (iter->value))
7908 && qualify_lookup (iter->value, want))
7909 binding = iter->value;
7910 else if (bool (want & LOOK_want::TYPE)
7911 && !HIDDEN_TYPE_BINDING_P (iter)
7912 && iter->type)
7913 binding = iter->type;
7914
7915 if (binding)
7916 {
7917 /* The saved lookups for an operator record 'nothing
7918 found' as error_mark_node. We need to stop the search
7919 here, but not return the error mark node. */
7920 if (binding == error_mark_node)
7921 binding = NULL_TREE;
7922
7923 val = binding;
7924 goto found;
7925 }
7926 }
7927 }
7928
7929 /* Now lookup in namespace scopes. */
7930 if (bool (where & LOOK_where::NAMESPACE))
7931 {
7932 name_lookup lookup (name, want);
7933 if (lookup.search_unqualified
7934 (current_decl_namespace (), current_binding_level))
7935 val = lookup.value;
7936 }
7937
7938 found:;
7939
7940 /* If we have a known type overload, pull it out. This can happen
7941 for both using decls and unhidden functions. */
7942 if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
7943 val = OVL_FUNCTION (val);
7944
7945 return val;
7946 }
7947
7948 /* Wrapper for lookup_name_1. */
7949
7950 tree
7951 lookup_name (tree name, LOOK_where where, LOOK_want want)
7952 {
7953 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7954 tree ret = lookup_name_1 (name, where, want);
7955 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7956 return ret;
7957 }
7958
7959 tree
7960 lookup_name (tree name)
7961 {
7962 return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
7963 }
7964
7965 /* Look up NAME for type used in elaborated name specifier in
7966 the scopes given by HOW.
7967
7968 Unlike lookup_name_1, we make sure that NAME is actually
7969 declared in the desired scope, not from inheritance, nor using
7970 directive. For using declaration, there is DR138 still waiting
7971 to be resolved. Hidden name coming from an earlier friend
7972 declaration is also returned, and will be made visible unless HOW
7973 is TAG_how::HIDDEN_FRIEND.
7974
7975 A TYPE_DECL best matching the NAME is returned. Catching error
7976 and issuing diagnostics are caller's responsibility. */
7977
7978 static tree
7979 lookup_elaborated_type_1 (tree name, TAG_how how)
7980 {
7981 cp_binding_level *b = current_binding_level;
7982
7983 if (b->kind != sk_namespace)
7984 /* Look in non-namespace scopes. */
7985 for (cxx_binding *iter = NULL;
7986 (iter = outer_binding (name, iter, /*class_p=*/ true)); )
7987 {
7988 /* First check we're supposed to be looking in this scope --
7989 if we're not, we're done. */
7990 for (; b != iter->scope; b = b->level_chain)
7991 if (!(b->kind == sk_cleanup
7992 || b->kind == sk_template_parms
7993 || b->kind == sk_function_parms
7994 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
7995 return NULL_TREE;
7996
7997 /* Check if this is the kind of thing we're looking for. If
7998 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
7999 come from base class. For ITER->VALUE, we can simply use
8000 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
8001 our own check.
8002
8003 We check ITER->TYPE before ITER->VALUE in order to handle
8004 typedef struct C {} C;
8005 correctly. */
8006
8007 if (tree type = iter->type)
8008 {
8009 if (qualify_lookup (type, LOOK_want::TYPE)
8010 && (how != TAG_how::CURRENT_ONLY
8011 || LOCAL_BINDING_P (iter)
8012 || DECL_CONTEXT (type) == iter->scope->this_entity))
8013 {
8014 if (how != TAG_how::HIDDEN_FRIEND)
8015 /* It is no longer a hidden binding. */
8016 HIDDEN_TYPE_BINDING_P (iter) = false;
8017
8018 return type;
8019 }
8020 }
8021 else
8022 {
8023 if (qualify_lookup (iter->value, LOOK_want::TYPE)
8024 && (how != TAG_how::CURRENT_ONLY
8025 || !INHERITED_VALUE_BINDING_P (iter)))
8026 {
8027 if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
8028 /* It is no longer a hidden binding. */
8029 HIDDEN_TYPE_BINDING_P (iter) = false;
8030
8031 return iter->value;
8032 }
8033 }
8034 }
8035
8036 /* Now check if we can look in namespace scope. */
8037 for (; b->kind != sk_namespace; b = b->level_chain)
8038 if (!(b->kind == sk_cleanup
8039 || b->kind == sk_template_parms
8040 || b->kind == sk_function_parms
8041 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8042 return NULL_TREE;
8043
8044 /* Look in the innermost namespace. */
8045 tree ns = b->this_entity;
8046 if (tree *slot = find_namespace_slot (ns, name))
8047 {
8048 tree bind = *slot;
8049 if (TREE_CODE (bind) == BINDING_VECTOR)
8050 bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
8051
8052 if (bind)
8053 {
8054 /* If this is the kind of thing we're looking for, we're done. */
8055 if (tree type = MAYBE_STAT_TYPE (bind))
8056 {
8057 if (how != TAG_how::HIDDEN_FRIEND)
8058 /* No longer hidden. */
8059 STAT_TYPE_HIDDEN_P (*slot) = false;
8060
8061 return type;
8062 }
8063 else if (tree decl = MAYBE_STAT_DECL (bind))
8064 {
8065 if (qualify_lookup (decl, LOOK_want::TYPE))
8066 {
8067 if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
8068 && STAT_DECL_HIDDEN_P (bind))
8069 {
8070 if (STAT_TYPE (bind))
8071 STAT_DECL_HIDDEN_P (bind) = false;
8072 else
8073 {
8074 /* There is no type, just remove the stat
8075 hack. */
8076 if (*slot == bind)
8077 *slot = decl;
8078 else
8079 BINDING_VECTOR_CLUSTER (bind, 0)
8080 .slots[BINDING_SLOT_CURRENT] = decl;
8081 }
8082 }
8083 return decl;
8084 }
8085 }
8086 }
8087
8088 if (TREE_CODE (*slot) == BINDING_VECTOR)
8089 {
8090 /* We could be redeclaring a global module entity, (from GMF
8091 or header unit), or from another partition, or
8092 specializing an imported template. */
8093 bitmap imports = get_import_bitmap ();
8094 binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
8095
8096 /* Scan the imported bindings. */
8097 unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
8098 if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
8099 {
8100 ix--;
8101 cluster++;
8102 }
8103
8104 /* Do this in forward order, so we load modules in an order
8105 the user expects. */
8106 for (; ix--; cluster++)
8107 for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
8108 {
8109 /* Are we importing this module? */
8110 if (unsigned base = cluster->indices[jx].base)
8111 if (unsigned span = cluster->indices[jx].span)
8112 do
8113 if (bitmap_bit_p (imports, base))
8114 goto found;
8115 while (++base, --span);
8116 continue;
8117
8118 found:;
8119 /* Is it loaded? */
8120 if (cluster->slots[jx].is_lazy ())
8121 {
8122 gcc_assert (cluster->indices[jx].span == 1);
8123 lazy_load_binding (cluster->indices[jx].base,
8124 ns, name, &cluster->slots[jx]);
8125 }
8126 tree bind = cluster->slots[jx];
8127 if (!bind)
8128 /* Load errors could mean there's nothing here. */
8129 continue;
8130
8131 /* Extract what we can see from here. If there's no
8132 stat_hack, then everything was exported. */
8133 tree type = NULL_TREE;
8134
8135 /* If no stat hack, everything is visible. */
8136 if (STAT_HACK_P (bind))
8137 {
8138 if (STAT_TYPE_VISIBLE_P (bind))
8139 type = STAT_TYPE (bind);
8140 bind = STAT_VISIBLE (bind);
8141 }
8142
8143 if (type && qualify_lookup (type, LOOK_want::TYPE))
8144 return type;
8145
8146 if (bind && qualify_lookup (bind, LOOK_want::TYPE))
8147 return bind;
8148 }
8149
8150 if (!module_purview_p ())
8151 {
8152 /* We're in the global module, perhaps there's a tag
8153 there? */
8154 // FIXME: This isn't quite right, if we find something
8155 // here, from the language PoV we're not supposed to
8156 // know it?
8157 }
8158 }
8159 }
8160
8161 return NULL_TREE;
8162 }
8163
8164 /* Wrapper for lookup_type_scope_1. */
8165
8166 tree
8167 lookup_elaborated_type (tree name, TAG_how how)
8168 {
8169 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8170 tree ret = lookup_elaborated_type_1 (name, how);
8171 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
8172 return ret;
8173 }
8174
8175 /* The type TYPE is being declared. If it is a class template, or a
8176 specialization of a class template, do any processing required and
8177 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8178 being declared a friend. B is the binding level at which this TYPE
8179 should be bound.
8180
8181 Returns the TYPE_DECL for TYPE, which may have been altered by this
8182 processing. */
8183
8184 static tree
8185 maybe_process_template_type_declaration (tree type, int is_friend,
8186 cp_binding_level *b)
8187 {
8188 tree decl = TYPE_NAME (type);
8189
8190 if (processing_template_parmlist)
8191 /* You can't declare a new template type in a template parameter
8192 list. But, you can declare a non-template type:
8193
8194 template <class A*> struct S;
8195
8196 is a forward-declaration of `A'. */
8197 ;
8198 else if (b->kind == sk_namespace
8199 && current_binding_level->kind != sk_namespace)
8200 /* If this new type is being injected into a containing scope,
8201 then it's not a template type. */
8202 ;
8203 else
8204 {
8205 gcc_assert (MAYBE_CLASS_TYPE_P (type)
8206 || TREE_CODE (type) == ENUMERAL_TYPE);
8207
8208 if (processing_template_decl)
8209 {
8210 decl = push_template_decl (decl, is_friend);
8211 if (decl == error_mark_node)
8212 return error_mark_node;
8213
8214 /* If the current binding level is the binding level for the
8215 template parameters (see the comment in
8216 begin_template_parm_list) and the enclosing level is a class
8217 scope, and we're not looking at a friend, push the
8218 declaration of the member class into the class scope. In the
8219 friend case, push_template_decl will already have put the
8220 friend into global scope, if appropriate. */
8221 if (TREE_CODE (type) != ENUMERAL_TYPE
8222 && !is_friend && b->kind == sk_template_parms
8223 && b->level_chain->kind == sk_class)
8224 {
8225 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
8226
8227 if (!COMPLETE_TYPE_P (current_class_type))
8228 maybe_add_class_template_decl_list (current_class_type,
8229 type, /*friend_p=*/0);
8230 }
8231 }
8232 }
8233
8234 return decl;
8235 }
8236
8237 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8238 that the NAME is a class template, the tag is processed but not pushed.
8239
8240 The pushed scope depend on the SCOPE parameter:
8241 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8242 scope.
8243 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8244 non-template-parameter scope. This case is needed for forward
8245 declarations.
8246 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8247 TS_GLOBAL case except that names within template-parameter scopes
8248 are not pushed at all.
8249
8250 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8251
8252 static tree
8253 do_pushtag (tree name, tree type, TAG_how how)
8254 {
8255 tree decl;
8256
8257 cp_binding_level *b = current_binding_level;
8258 while (true)
8259 {
8260 if (/* Cleanup scopes are not scopes from the point of view of
8261 the language. */
8262 b->kind == sk_cleanup
8263 /* Neither are function parameter scopes. */
8264 || b->kind == sk_function_parms
8265 /* Neither are the scopes used to hold template parameters
8266 for an explicit specialization. For an ordinary template
8267 declaration, these scopes are not scopes from the point of
8268 view of the language. */
8269 || (b->kind == sk_template_parms
8270 && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
8271 b = b->level_chain;
8272 else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
8273 {
8274 b = b->level_chain;
8275 if (b->kind == sk_template_parms)
8276 b = b->level_chain;
8277 }
8278 else
8279 break;
8280 }
8281
8282 gcc_assert (identifier_p (name));
8283
8284 /* Do C++ gratuitous typedefing. */
8285 if (identifier_type_value_1 (name) != type)
8286 {
8287 tree tdef;
8288 tree context = TYPE_CONTEXT (type);
8289
8290 if (! context)
8291 {
8292 cp_binding_level *cb = b;
8293 while (cb->kind != sk_namespace
8294 && cb->kind != sk_class
8295 && (cb->kind != sk_function_parms
8296 || !cb->this_entity))
8297 cb = cb->level_chain;
8298 tree cs = cb->this_entity;
8299
8300 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
8301 ? cs == current_function_decl
8302 : TYPE_P (cs) ? cs == current_class_type
8303 : cs == current_namespace);
8304
8305 if (how == TAG_how::CURRENT_ONLY
8306 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
8307 context = cs;
8308 else if (cs && TYPE_P (cs))
8309 /* When declaring a friend class of a local class, we want
8310 to inject the newly named class into the scope
8311 containing the local class, not the namespace
8312 scope. */
8313 context = decl_function_context (get_type_decl (cs));
8314 }
8315 if (!context)
8316 context = current_namespace;
8317
8318 tdef = create_implicit_typedef (name, type);
8319 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
8320 set_originating_module (tdef);
8321
8322 decl = maybe_process_template_type_declaration
8323 (type, how == TAG_how::HIDDEN_FRIEND, b);
8324 if (decl == error_mark_node)
8325 return decl;
8326
8327 bool in_class = false;
8328 if (b->kind == sk_class)
8329 {
8330 in_class = true;
8331 if (!TYPE_BEING_DEFINED (current_class_type))
8332 /* Don't push anywhere if the class is complete; a lambda in an
8333 NSDMI is not a member of the class. */
8334 ;
8335 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8336 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8337 class. But if it's a member template class, we want
8338 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8339 later. */
8340 finish_member_declaration (decl);
8341 else
8342 pushdecl_class_level (decl);
8343 }
8344 else if (b->kind == sk_template_parms)
8345 in_class = b->level_chain->kind == sk_class;
8346 else
8347 {
8348 decl = do_pushdecl_with_scope
8349 (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
8350 if (decl == error_mark_node)
8351 return decl;
8352
8353 if (DECL_CONTEXT (decl) == std_node
8354 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
8355 && !CLASSTYPE_TEMPLATE_INFO (type))
8356 {
8357 error ("declaration of %<std::initializer_list%> does not match "
8358 "%<#include <initializer_list>%>, isn%'t a template");
8359 return error_mark_node;
8360 }
8361 }
8362
8363 if (!in_class)
8364 set_identifier_type_value_with_scope (name, tdef, b);
8365
8366 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
8367
8368 /* If this is a local class, keep track of it. We need this
8369 information for name-mangling, and so that it is possible to
8370 find all function definitions in a translation unit in a
8371 convenient way. (It's otherwise tricky to find a member
8372 function definition it's only pointed to from within a local
8373 class.) */
8374 if (TYPE_FUNCTION_SCOPE_P (type))
8375 {
8376 if (processing_template_decl)
8377 {
8378 /* Push a DECL_EXPR so we call pushtag at the right time in
8379 template instantiation rather than in some nested context. */
8380 add_decl_expr (decl);
8381 }
8382 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8383 else if (!LAMBDA_TYPE_P (type))
8384 determine_local_discriminator (TYPE_NAME (type));
8385 }
8386 }
8387
8388 if (b->kind == sk_class
8389 && !COMPLETE_TYPE_P (current_class_type))
8390 maybe_add_class_template_decl_list (current_class_type,
8391 type, /*friend_p=*/0);
8392
8393 decl = TYPE_NAME (type);
8394 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
8395
8396 /* Set type visibility now if this is a forward declaration. */
8397 TREE_PUBLIC (decl) = 1;
8398 determine_visibility (decl);
8399
8400 return type;
8401 }
8402
8403 /* Wrapper for do_pushtag. */
8404
8405 tree
8406 pushtag (tree name, tree type, TAG_how how)
8407 {
8408 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8409 tree ret = do_pushtag (name, type, how);
8410 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
8411 return ret;
8412 }
8413
8414 \f
8415 /* Subroutines for reverting temporarily to top-level for instantiation
8416 of templates and such. We actually need to clear out the class- and
8417 local-value slots of all identifiers, so that only the global values
8418 are at all visible. Simply setting current_binding_level to the global
8419 scope isn't enough, because more binding levels may be pushed. */
8420 struct saved_scope *scope_chain;
8421
8422 /* Return true if ID has not already been marked. */
8423
8424 static inline bool
8425 store_binding_p (tree id)
8426 {
8427 if (!id || !IDENTIFIER_BINDING (id))
8428 return false;
8429
8430 if (IDENTIFIER_MARKED (id))
8431 return false;
8432
8433 return true;
8434 }
8435
8436 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8437 have enough space reserved. */
8438
8439 static void
8440 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
8441 {
8442 cxx_saved_binding saved;
8443
8444 gcc_checking_assert (store_binding_p (id));
8445
8446 IDENTIFIER_MARKED (id) = 1;
8447
8448 saved.identifier = id;
8449 saved.binding = IDENTIFIER_BINDING (id);
8450 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8451 (*old_bindings)->quick_push (saved);
8452 IDENTIFIER_BINDING (id) = NULL;
8453 }
8454
8455 static void
8456 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
8457 {
8458 static vec<tree> bindings_need_stored;
8459 tree t, id;
8460 size_t i;
8461
8462 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8463 for (t = names; t; t = TREE_CHAIN (t))
8464 {
8465 if (TREE_CODE (t) == TREE_LIST)
8466 id = TREE_PURPOSE (t);
8467 else
8468 id = DECL_NAME (t);
8469
8470 if (store_binding_p (id))
8471 bindings_need_stored.safe_push (id);
8472 }
8473 if (!bindings_need_stored.is_empty ())
8474 {
8475 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8476 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8477 {
8478 /* We can apparently have duplicates in NAMES. */
8479 if (store_binding_p (id))
8480 store_binding (id, old_bindings);
8481 }
8482 bindings_need_stored.truncate (0);
8483 }
8484 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
8485 }
8486
8487 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8488 objects, rather than a TREE_LIST. */
8489
8490 static void
8491 store_class_bindings (vec<cp_class_binding, va_gc> *names,
8492 vec<cxx_saved_binding, va_gc> **old_bindings)
8493 {
8494 static vec<tree> bindings_need_stored;
8495 size_t i;
8496 cp_class_binding *cb;
8497
8498 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
8499 if (store_binding_p (cb->identifier))
8500 bindings_need_stored.safe_push (cb->identifier);
8501 if (!bindings_need_stored.is_empty ())
8502 {
8503 tree id;
8504 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8505 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8506 store_binding (id, old_bindings);
8507 bindings_need_stored.truncate (0);
8508 }
8509 }
8510
8511 /* A chain of saved_scope structures awaiting reuse. */
8512
8513 static GTY((deletable)) struct saved_scope *free_saved_scope;
8514
8515 static void
8516 do_push_to_top_level (void)
8517 {
8518 struct saved_scope *s;
8519 cp_binding_level *b;
8520 cxx_saved_binding *sb;
8521 size_t i;
8522 bool need_pop;
8523
8524 /* Reuse or create a new structure for this saved scope. */
8525 if (free_saved_scope != NULL)
8526 {
8527 s = free_saved_scope;
8528 free_saved_scope = s->prev;
8529
8530 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
8531 memset (s, 0, sizeof (*s));
8532 /* Also reuse the structure's old_bindings vector. */
8533 vec_safe_truncate (old_bindings, 0);
8534 s->old_bindings = old_bindings;
8535 }
8536 else
8537 s = ggc_cleared_alloc<saved_scope> ();
8538
8539 b = scope_chain ? current_binding_level : 0;
8540
8541 /* If we're in the middle of some function, save our state. */
8542 if (cfun)
8543 {
8544 need_pop = true;
8545 push_function_context ();
8546 }
8547 else
8548 need_pop = false;
8549
8550 if (scope_chain && previous_class_level)
8551 store_class_bindings (previous_class_level->class_shadowed,
8552 &s->old_bindings);
8553
8554 /* Have to include the global scope, because class-scope decls
8555 aren't listed anywhere useful. */
8556 for (; b; b = b->level_chain)
8557 {
8558 tree t;
8559
8560 /* Template IDs are inserted into the global level. If they were
8561 inserted into namespace level, finish_file wouldn't find them
8562 when doing pending instantiations. Therefore, don't stop at
8563 namespace level, but continue until :: . */
8564 if (global_scope_p (b))
8565 break;
8566
8567 store_bindings (b->names, &s->old_bindings);
8568 /* We also need to check class_shadowed to save class-level type
8569 bindings, since pushclass doesn't fill in b->names. */
8570 if (b->kind == sk_class)
8571 store_class_bindings (b->class_shadowed, &s->old_bindings);
8572
8573 /* Unwind type-value slots back to top level. */
8574 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
8575 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
8576 }
8577
8578 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
8579 IDENTIFIER_MARKED (sb->identifier) = 0;
8580
8581 s->prev = scope_chain;
8582 s->bindings = b;
8583 s->need_pop_function_context = need_pop;
8584 s->function_decl = current_function_decl;
8585 s->unevaluated_operand = cp_unevaluated_operand;
8586 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8587 s->suppress_location_wrappers = suppress_location_wrappers;
8588 s->x_stmt_tree.stmts_are_full_exprs_p = true;
8589
8590 scope_chain = s;
8591 current_function_decl = NULL_TREE;
8592 current_lang_base = NULL;
8593 current_lang_name = lang_name_cplusplus;
8594 current_namespace = global_namespace;
8595 push_class_stack ();
8596 cp_unevaluated_operand = 0;
8597 c_inhibit_evaluation_warnings = 0;
8598 suppress_location_wrappers = 0;
8599 }
8600
8601 static void
8602 do_pop_from_top_level (void)
8603 {
8604 struct saved_scope *s = scope_chain;
8605 cxx_saved_binding *saved;
8606 size_t i;
8607
8608 /* Clear out class-level bindings cache. */
8609 if (previous_class_level)
8610 invalidate_class_lookup_cache ();
8611 pop_class_stack ();
8612
8613 release_tree_vector (current_lang_base);
8614
8615 scope_chain = s->prev;
8616 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
8617 {
8618 tree id = saved->identifier;
8619
8620 IDENTIFIER_BINDING (id) = saved->binding;
8621 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8622 }
8623
8624 /* If we were in the middle of compiling a function, restore our
8625 state. */
8626 if (s->need_pop_function_context)
8627 pop_function_context ();
8628 current_function_decl = s->function_decl;
8629 cp_unevaluated_operand = s->unevaluated_operand;
8630 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
8631 suppress_location_wrappers = s->suppress_location_wrappers;
8632
8633 /* Make this saved_scope structure available for reuse by
8634 push_to_top_level. */
8635 s->prev = free_saved_scope;
8636 free_saved_scope = s;
8637 }
8638
8639 /* Push into the scope of the namespace NS, even if it is deeply
8640 nested within another namespace. */
8641
8642 static void
8643 do_push_nested_namespace (tree ns)
8644 {
8645 if (ns == global_namespace)
8646 do_push_to_top_level ();
8647 else
8648 {
8649 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
8650 resume_scope (NAMESPACE_LEVEL (ns));
8651 current_namespace = ns;
8652 }
8653 }
8654
8655 /* Pop back from the scope of the namespace NS, which was previously
8656 entered with push_nested_namespace. */
8657
8658 static void
8659 do_pop_nested_namespace (tree ns)
8660 {
8661 while (ns != global_namespace)
8662 {
8663 ns = CP_DECL_CONTEXT (ns);
8664 current_namespace = ns;
8665 leave_scope ();
8666 }
8667
8668 do_pop_from_top_level ();
8669 }
8670
8671 /* Add TARGET to USINGS, if it does not already exist there. We used
8672 to build the complete graph of usings at this point, from the POV
8673 of the source namespaces. Now we build that as we perform the
8674 unqualified search. */
8675
8676 static void
8677 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
8678 {
8679 if (usings)
8680 for (unsigned ix = usings->length (); ix--;)
8681 if ((*usings)[ix] == target)
8682 return;
8683
8684 vec_safe_push (usings, target);
8685 }
8686
8687 /* Tell the debug system of a using directive. */
8688
8689 static void
8690 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
8691 {
8692 /* Emit debugging info. */
8693 tree context = from != global_namespace ? from : NULL_TREE;
8694 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
8695 implicit);
8696 }
8697
8698 /* Process a using directive. */
8699
8700 void
8701 finish_using_directive (tree target, tree attribs)
8702 {
8703 if (target == error_mark_node)
8704 return;
8705
8706 if (current_binding_level->kind != sk_namespace)
8707 add_stmt (build_stmt (input_location, USING_STMT, target));
8708 else
8709 emit_debug_info_using_namespace (current_binding_level->this_entity,
8710 ORIGINAL_NAMESPACE (target), false);
8711
8712 add_using_namespace (current_binding_level->using_directives,
8713 ORIGINAL_NAMESPACE (target));
8714
8715 if (attribs != error_mark_node)
8716 for (tree a = attribs; a; a = TREE_CHAIN (a))
8717 {
8718 tree name = get_attribute_name (a);
8719 if (current_binding_level->kind == sk_namespace
8720 && is_attribute_p ("strong", name))
8721 {
8722 if (warning (0, "%<strong%> using directive no longer supported")
8723 && CP_DECL_CONTEXT (target) == current_namespace)
8724 inform (DECL_SOURCE_LOCATION (target),
8725 "you can use an inline namespace instead");
8726 }
8727 else
8728 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
8729 }
8730 }
8731
8732 /* Pushes X into the global namespace. */
8733
8734 tree
8735 pushdecl_top_level (tree x)
8736 {
8737 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8738 do_push_to_top_level ();
8739 gcc_checking_assert (!DECL_CONTEXT (x));
8740 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8741 x = pushdecl_namespace_level (x);
8742 do_pop_from_top_level ();
8743 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
8744 return x;
8745 }
8746
8747 /* Pushes X into the global namespace and calls cp_finish_decl to
8748 register the variable, initializing it with INIT. */
8749
8750 tree
8751 pushdecl_top_level_and_finish (tree x, tree init)
8752 {
8753 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8754 do_push_to_top_level ();
8755 gcc_checking_assert (!DECL_CONTEXT (x));
8756 DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
8757 x = pushdecl_namespace_level (x);
8758 cp_finish_decl (x, init, false, NULL_TREE, 0);
8759 do_pop_from_top_level ();
8760 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
8761 return x;
8762 }
8763
8764 /* Enter the namespaces from current_namerspace to NS. */
8765
8766 static int
8767 push_inline_namespaces (tree ns)
8768 {
8769 int count = 0;
8770 if (ns != current_namespace)
8771 {
8772 gcc_assert (ns != global_namespace);
8773 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
8774 resume_scope (NAMESPACE_LEVEL (ns));
8775 current_namespace = ns;
8776 count++;
8777 }
8778 return count;
8779 }
8780
8781 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8782 Reuse or create a namespace NAME. NAME is null for the anonymous
8783 namespace. */
8784
8785 static tree
8786 reuse_namespace (tree *slot, tree ctx, tree name)
8787 {
8788 if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
8789 {
8790 /* Public namespace. Shared. */
8791 tree *global_slot = slot;
8792 if (TREE_CODE (*slot) == BINDING_VECTOR)
8793 global_slot = get_fixed_binding_slot (slot, name,
8794 BINDING_SLOT_GLOBAL, false);
8795
8796 for (ovl_iterator iter (*global_slot); iter; ++iter)
8797 {
8798 tree decl = *iter;
8799
8800 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
8801 return decl;
8802 }
8803 }
8804 return NULL_TREE;
8805 }
8806
8807 static tree
8808 make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
8809 {
8810 /* Create the namespace. */
8811 tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
8812 DECL_SOURCE_LOCATION (ns) = loc;
8813 SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
8814 if (!SCOPE_DEPTH (ns))
8815 /* We only allow depth 255. */
8816 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
8817 DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
8818
8819 if (!name)
8820 /* Anon-namespaces in different header-unit imports are distinct.
8821 But that's ok as their contents all have internal linkage.
8822 (This is different to how they'd behave as textual includes,
8823 but doing this at all is really odd source.) */
8824 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
8825 else if (TREE_PUBLIC (ctx))
8826 TREE_PUBLIC (ns) = true;
8827
8828 if (inline_p)
8829 DECL_NAMESPACE_INLINE_P (ns) = true;
8830
8831 return ns;
8832 }
8833
8834 /* NS was newly created, finish off making it. */
8835
8836 static void
8837 make_namespace_finish (tree ns, tree *slot, bool from_import = false)
8838 {
8839 if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
8840 {
8841 /* Merge into global slot. */
8842 tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
8843 BINDING_SLOT_GLOBAL, true);
8844 *gslot = ns;
8845 }
8846
8847 tree ctx = CP_DECL_CONTEXT (ns);
8848 cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
8849 scope->this_entity = ns;
8850 scope->more_cleanups_ok = true;
8851 scope->kind = sk_namespace;
8852 scope->level_chain = NAMESPACE_LEVEL (ctx);
8853 NAMESPACE_LEVEL (ns) = scope;
8854
8855 if (DECL_NAMESPACE_INLINE_P (ns))
8856 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
8857
8858 if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
8859 emit_debug_info_using_namespace (ctx, ns, true);
8860 }
8861
8862 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
8863 then we enter an anonymous namespace. If MAKE_INLINE is true, then
8864 we create an inline namespace (it is up to the caller to check upon
8865 redefinition). Return the number of namespaces entered. */
8866
8867 int
8868 push_namespace (tree name, bool make_inline)
8869 {
8870 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
8871 int count = 0;
8872
8873 /* We should not get here if the global_namespace is not yet constructed
8874 nor if NAME designates the global namespace: The global scope is
8875 constructed elsewhere. */
8876 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
8877
8878 tree ns = NULL_TREE;
8879 {
8880 name_lookup lookup (name);
8881 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
8882 ;
8883 else if (TREE_CODE (lookup.value) == TREE_LIST)
8884 {
8885 /* An ambiguous lookup. If exactly one is a namespace, we
8886 want that. If more than one is a namespace, error, but
8887 pick one of them. */
8888 /* DR2061 can cause us to find multiple namespaces of the same
8889 name. We must treat that carefully and avoid thinking we
8890 need to push a new (possibly) duplicate namespace. Hey,
8891 if you want to use the same identifier within an inline
8892 nest, knock yourself out. */
8893 for (tree *chain = &lookup.value, next; (next = *chain);)
8894 {
8895 tree decl = TREE_VALUE (next);
8896 if (TREE_CODE (decl) == NAMESPACE_DECL)
8897 {
8898 if (!ns)
8899 ns = decl;
8900 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
8901 ns = decl;
8902
8903 /* Advance. */
8904 chain = &TREE_CHAIN (next);
8905 }
8906 else
8907 /* Stitch out. */
8908 *chain = TREE_CHAIN (next);
8909 }
8910
8911 if (TREE_CHAIN (lookup.value))
8912 {
8913 error ("%<namespace %E%> is ambiguous", name);
8914 print_candidates (lookup.value);
8915 }
8916 }
8917 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
8918 ns = lookup.value;
8919
8920 if (ns)
8921 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
8922 {
8923 /* A namespace alias is not allowed here, but if the alias
8924 is for a namespace also inside the current scope,
8925 accept it with a diagnostic. That's better than dying
8926 horribly. */
8927 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
8928 {
8929 error ("namespace alias %qD not allowed here, "
8930 "assuming %qD", ns, dna);
8931 ns = dna;
8932 }
8933 else
8934 ns = NULL_TREE;
8935 }
8936 }
8937
8938 if (ns)
8939 {
8940 /* DR2061. NS might be a member of an inline namespace. We
8941 need to push into those namespaces. */
8942 if (modules_p ())
8943 {
8944 for (tree parent, ctx = ns; ctx != current_namespace;
8945 ctx = parent)
8946 {
8947 parent = CP_DECL_CONTEXT (ctx);
8948
8949 tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
8950 if (bind != ctx)
8951 {
8952 auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
8953 binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
8954 gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
8955 slot = ctx;
8956 }
8957 }
8958 }
8959
8960 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
8961 if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
8962 /* It's not builtin now. */
8963 DECL_SOURCE_LOCATION (ns) = input_location;
8964 }
8965 else
8966 {
8967 /* Before making a new namespace, see if we already have one in
8968 the existing partitions of the current namespace. */
8969 tree *slot = find_namespace_slot (current_namespace, name, false);
8970 if (slot)
8971 ns = reuse_namespace (slot, current_namespace, name);
8972 if (!ns)
8973 ns = make_namespace (current_namespace, name,
8974 input_location, make_inline);
8975
8976 if (pushdecl (ns) == error_mark_node)
8977 ns = NULL_TREE;
8978 else
8979 {
8980 /* Finish up making the namespace. */
8981 add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
8982 if (!slot)
8983 {
8984 slot = find_namespace_slot (current_namespace, name);
8985 /* This should find the slot created by pushdecl. */
8986 gcc_checking_assert (slot && *slot == ns);
8987 }
8988 make_namespace_finish (ns, slot);
8989
8990 /* Add the anon using-directive here, we don't do it in
8991 make_namespace_finish. */
8992 if (!DECL_NAMESPACE_INLINE_P (ns) && !name)
8993 add_using_namespace (current_binding_level->using_directives, ns);
8994 }
8995 }
8996
8997 if (ns)
8998 {
8999 /* A public namespace is exported only if explicitly marked, or
9000 it contains exported entities. */
9001 if (!DECL_MODULE_EXPORT_P (ns) && TREE_PUBLIC (ns)
9002 && module_exporting_p ())
9003 implicitly_export_namespace (ns);
9004
9005 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
9006 {
9007 error_at (input_location,
9008 "inline namespace must be specified at initial definition");
9009 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
9010 }
9011 resume_scope (NAMESPACE_LEVEL (ns));
9012 current_namespace = ns;
9013 count++;
9014 }
9015
9016 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9017 return count;
9018 }
9019
9020 /* Pop from the scope of the current namespace. */
9021
9022 void
9023 pop_namespace (void)
9024 {
9025 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
9026
9027 gcc_assert (current_namespace != global_namespace);
9028 current_namespace = CP_DECL_CONTEXT (current_namespace);
9029 /* The binding level is not popped, as it might be re-opened later. */
9030 leave_scope ();
9031
9032 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9033 }
9034
9035 /* An import is defining namespace NAME inside CTX. Find or create
9036 that namespace and add it to the container's binding-vector. */
9037
9038 tree
9039 add_imported_namespace (tree ctx, tree name, unsigned origin, location_t loc,
9040 bool visible_p, bool inline_p)
9041 {
9042 // FIXME: Something is not correct about the VISIBLE_P handling. We
9043 // need to insert this namespace into
9044 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9045 // (b) The importing module's slot (always)
9046 // (c) Do we need to put it in the CURRENT slot? This is the
9047 // confused piece.
9048
9049 gcc_checking_assert (origin);
9050 tree *slot = find_namespace_slot (ctx, name, true);
9051 tree decl = reuse_namespace (slot, ctx, name);
9052 if (!decl)
9053 {
9054 decl = make_namespace (ctx, name, loc, inline_p);
9055 DECL_MODULE_IMPORT_P (decl) = true;
9056 make_namespace_finish (decl, slot, true);
9057 }
9058 else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
9059 {
9060 error_at (loc, "%s namespace %qD conflicts with reachable definition",
9061 inline_p ? "inline" : "non-inline", decl);
9062 inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
9063 inline_p ? "non-inline" : "inline");
9064 }
9065
9066 if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
9067 {
9068 /* See if we can extend the final slot. */
9069 binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
9070 gcc_checking_assert (last->indices[0].span);
9071 unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
9072
9073 while (--jx)
9074 if (last->indices[jx].span)
9075 break;
9076 tree final = last->slots[jx];
9077 if (visible_p == !STAT_HACK_P (final)
9078 && MAYBE_STAT_DECL (final) == decl
9079 && last->indices[jx].base + last->indices[jx].span == origin
9080 && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
9081 || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
9082 && jx >= BINDING_SLOTS_FIXED)))
9083 {
9084 last->indices[jx].span++;
9085 return decl;
9086 }
9087 }
9088
9089 /* Append a new slot. */
9090 tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, origin);
9091
9092 gcc_assert (!*mslot);
9093 *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
9094
9095 return decl;
9096 }
9097
9098 /* External entry points for do_{push_to/pop_from}_top_level. */
9099
9100 void
9101 push_to_top_level (void)
9102 {
9103 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
9104 do_push_to_top_level ();
9105 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9106 }
9107
9108 void
9109 pop_from_top_level (void)
9110 {
9111 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
9112 do_pop_from_top_level ();
9113 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9114 }
9115
9116 /* External entry points for do_{push,pop}_nested_namespace. */
9117
9118 void
9119 push_nested_namespace (tree ns)
9120 {
9121 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
9122 do_push_nested_namespace (ns);
9123 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9124 }
9125
9126 void
9127 pop_nested_namespace (tree ns)
9128 {
9129 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
9130 gcc_assert (current_namespace == ns);
9131 do_pop_nested_namespace (ns);
9132 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
9133 }
9134
9135 /* Pop off extraneous binding levels left over due to syntax errors.
9136 We don't pop past namespaces, as they might be valid. */
9137
9138 void
9139 pop_everything (void)
9140 {
9141 if (ENABLE_SCOPE_CHECKING)
9142 verbatim ("XXX entering %<pop_everything ()%>");
9143 while (!namespace_bindings_p ())
9144 {
9145 if (current_binding_level->kind == sk_class)
9146 pop_nested_class ();
9147 else
9148 poplevel (0, 0, 0);
9149 }
9150 if (ENABLE_SCOPE_CHECKING)
9151 verbatim ("XXX leaving %<pop_everything ()%>");
9152 }
9153
9154 /* Emit debugging information for using declarations and directives.
9155 If input tree is overloaded fn then emit debug info for all
9156 candidates. */
9157
9158 void
9159 cp_emit_debug_info_for_using (tree t, tree context)
9160 {
9161 /* Don't try to emit any debug information if we have errors. */
9162 if (seen_error ())
9163 return;
9164
9165 /* Do not supply context to imported_module_or_decl, if
9166 it is a global namespace. */
9167 if (context == global_namespace)
9168 context = NULL_TREE;
9169
9170 t = MAYBE_BASELINK_FUNCTIONS (t);
9171
9172 for (lkp_iterator iter (t); iter; ++iter)
9173 {
9174 tree fn = *iter;
9175
9176 if (TREE_CODE (fn) == TEMPLATE_DECL)
9177 /* FIXME: Handle TEMPLATE_DECLs. */
9178 continue;
9179
9180 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9181 of a builtin function. */
9182 if (TREE_CODE (fn) == FUNCTION_DECL
9183 && DECL_EXTERNAL (fn)
9184 && fndecl_built_in_p (fn))
9185 continue;
9186
9187 if (building_stmt_list_p ())
9188 add_stmt (build_stmt (input_location, USING_STMT, fn));
9189 else
9190 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
9191 false, false);
9192 }
9193 }
9194
9195 /* Return the result of unqualified lookup for the overloaded operator
9196 designated by CODE, if we are in a template and the binding we find is
9197 not. */
9198
9199 static tree
9200 op_unqualified_lookup (tree fnname)
9201 {
9202 if (cxx_binding *binding = IDENTIFIER_BINDING (fnname))
9203 {
9204 cp_binding_level *l = binding->scope;
9205 while (l && !l->this_entity)
9206 l = l->level_chain;
9207
9208 if (l && uses_template_parms (l->this_entity))
9209 /* Don't preserve decls from an uninstantiated template,
9210 wait until that template is instantiated. */
9211 return NULL_TREE;
9212 }
9213
9214 tree fns = lookup_name (fnname);
9215 if (!fns)
9216 /* Remember we found nothing! */
9217 return error_mark_node;
9218
9219 tree d = is_overloaded_fn (fns) ? get_first_fn (fns) : fns;
9220 if (DECL_CLASS_SCOPE_P (d))
9221 /* We don't need to remember class-scope functions or declarations,
9222 normal unqualified lookup will find them again. */
9223 fns = NULL_TREE;
9224
9225 return fns;
9226 }
9227
9228 /* E is an expression representing an operation with dependent type, so we
9229 don't know yet whether it will use the built-in meaning of the operator or a
9230 function. Remember declarations of that operator in scope.
9231
9232 We then inject a fake binding of that lookup into the
9233 instantiation's parameter scope. This approach fails if the user
9234 has different using declarations or directives in different local
9235 binding of the current function from whence we need to do lookups
9236 (we'll cache what we see on the first lookup). */
9237
9238 static const char *const op_bind_attrname = "operator bindings";
9239
9240 void
9241 maybe_save_operator_binding (tree e)
9242 {
9243 /* This is only useful in a generic lambda. */
9244 if (!processing_template_decl)
9245 return;
9246
9247 tree cfn = current_function_decl;
9248 if (!cfn)
9249 return;
9250
9251 /* Do this for lambdas and code that will emit a CMI. In a module's
9252 GMF we don't yet know whether there will be a CMI. */
9253 if (!module_has_cmi_p () && !global_purview_p () && !current_lambda_expr())
9254 return;
9255
9256 tree fnname = ovl_op_identifier (false, TREE_CODE (e));
9257 if (!fnname)
9258 return;
9259
9260 tree attributes = DECL_ATTRIBUTES (cfn);
9261 tree op_attr = lookup_attribute (op_bind_attrname, attributes);
9262 if (!op_attr)
9263 {
9264 op_attr = tree_cons (get_identifier (op_bind_attrname),
9265 NULL_TREE, attributes);
9266 DECL_ATTRIBUTES (cfn) = op_attr;
9267 }
9268
9269 tree op_bind = purpose_member (fnname, TREE_VALUE (op_attr));
9270 if (!op_bind)
9271 {
9272 tree fns = op_unqualified_lookup (fnname);
9273
9274 /* Always record, so we don't keep looking for this
9275 operator. */
9276 TREE_VALUE (op_attr) = tree_cons (fnname, fns, TREE_VALUE (op_attr));
9277 }
9278 }
9279
9280 /* Called from cp_free_lang_data so we don't put this into LTO. */
9281
9282 void
9283 discard_operator_bindings (tree decl)
9284 {
9285 DECL_ATTRIBUTES (decl) = remove_attribute (op_bind_attrname,
9286 DECL_ATTRIBUTES (decl));
9287 }
9288
9289 /* Subroutine of start_preparsed_function: push the bindings we saved away in
9290 maybe_save_op_lookup into the function parameter binding level. */
9291
9292 void
9293 push_operator_bindings ()
9294 {
9295 tree decl1 = current_function_decl;
9296 if (tree attr = lookup_attribute (op_bind_attrname,
9297 DECL_ATTRIBUTES (decl1)))
9298 for (tree binds = TREE_VALUE (attr); binds; binds = TREE_CHAIN (binds))
9299 if (tree val = TREE_VALUE (binds))
9300 {
9301 tree name = TREE_PURPOSE (binds);
9302 push_local_binding (name, val, /*using*/true);
9303 }
9304 }
9305
9306 #include "gt-cp-name-lookup.h"