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