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