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