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