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