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