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 : 299329 : stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
89 : : {
90 : 299329 : tree result = make_node (OVERLOAD);
91 : :
92 : : /* Mark this as a lookup, so we can tell this is a stat hack. */
93 : 299329 : OVL_LOOKUP_P (result) = true;
94 : 299329 : STAT_DECL (result) = decl;
95 : 299329 : STAT_TYPE (result) = type;
96 : 299329 : return result;
97 : : }
98 : :
99 : : /* Create a local binding level for NAME. */
100 : :
101 : : static cxx_binding *
102 : 799640665 : create_local_binding (cp_binding_level *level, tree name)
103 : : {
104 : 799640665 : cxx_binding *binding = cxx_binding_make (NULL, NULL);
105 : :
106 : 799640665 : LOCAL_BINDING_P (binding) = true;
107 : 799640665 : binding->scope = level;
108 : 799640665 : binding->previous = IDENTIFIER_BINDING (name);
109 : :
110 : 799640665 : IDENTIFIER_BINDING (name) = binding;
111 : :
112 : 799640665 : 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 : 8542584203 : find_namespace_slot (tree ns, tree name, bool create_p = false)
120 : : {
121 : 8542584203 : tree *slot = DECL_NAMESPACE_BINDINGS (ns)
122 : 16711798632 : ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
123 : : create_p ? INSERT : NO_INSERT);
124 : 8542584203 : return slot;
125 : : }
126 : :
127 : : static tree
128 : 37294 : find_namespace_value (tree ns, tree name)
129 : : {
130 : 37294 : tree *b = find_namespace_slot (ns, name);
131 : :
132 : 37294 : 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 : 86303 : search_imported_binding_slot (tree *slot, unsigned ix)
142 : : {
143 : 86303 : gcc_assert (ix);
144 : :
145 : 86303 : if (!*slot)
146 : : return NULL;
147 : :
148 : 86303 : if (TREE_CODE (*slot) != BINDING_VECTOR)
149 : : return NULL;
150 : :
151 : 86303 : unsigned clusters = BINDING_VECTOR_NUM_CLUSTERS (*slot);
152 : 86303 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
153 : :
154 : 86303 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
155 : : {
156 : 86303 : clusters--;
157 : 86303 : cluster++;
158 : : }
159 : :
160 : 173086 : 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 : 86303 : if (clusters)
174 : : /* Is it in this cluster? */
175 : 172324 : for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
176 : : {
177 : 172324 : if (!cluster->indices[off].span)
178 : : break;
179 : 172324 : if (cluster->indices[off].base > ix)
180 : : break;
181 : :
182 : 172324 : if (cluster->indices[off].base + cluster->indices[off].span > ix)
183 : 86303 : return &cluster->slots[off];
184 : : }
185 : :
186 : : return NULL;
187 : : }
188 : :
189 : : static void
190 : 88405 : init_global_partition (binding_cluster *cluster, tree decl)
191 : : {
192 : 88405 : bool named = true;
193 : :
194 : 88405 : if (header_module_p ())
195 : : named = false;
196 : 88327 : else if (TREE_PUBLIC (decl)
197 : 47426 : && TREE_CODE (decl) == NAMESPACE_DECL
198 : 89115 : && !DECL_NAMESPACE_ALIAS (decl))
199 : : named = false;
200 : 87546 : 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 : 88405 : mslot = &cluster[0].slots[BINDING_SLOT_GLOBAL];
211 : :
212 : 88405 : if (*mslot)
213 : 19673 : decl = ovl_make (decl, *mslot);
214 : 88405 : *mslot = decl;
215 : :
216 : 88405 : if (TREE_CODE (decl) == CONST_DECL)
217 : : {
218 : 1787 : tree type = TREE_TYPE (decl);
219 : 1787 : if (TREE_CODE (type) == ENUMERAL_TYPE
220 : 1787 : && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type)))
221 : 2458 : && decl == TREE_VALUE (TYPE_VALUES (type)))
222 : : /* Anonymous enums are keyed by their first enumerator, put
223 : : the TYPE_DECL here too. */
224 : 141 : *mslot = ovl_make (TYPE_NAME (type), *mslot);
225 : : }
226 : 88405 : }
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 : 369662528 : get_fixed_binding_slot (tree *slot, tree name, unsigned ix, int create)
235 : : {
236 : 369662528 : gcc_checking_assert (ix <= BINDING_SLOT_PARTITION);
237 : :
238 : : /* An assumption is that the fixed slots all reside in one cluster. */
239 : 369662528 : gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER >= BINDING_SLOTS_FIXED);
240 : :
241 : 369662528 : if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
242 : : {
243 : 369559817 : if (ix == BINDING_SLOT_CURRENT)
244 : : /* The current TU can just use slot directly. */
245 : : return slot;
246 : :
247 : 147527 : 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 : 147527 : bool partition_slot = true;
254 : 147527 : unsigned want = ((BINDING_SLOTS_FIXED + partition_slot + (create < 0)
255 : : + BINDING_VECTOR_SLOTS_PER_CLUSTER - 1)
256 : : / BINDING_VECTOR_SLOTS_PER_CLUSTER);
257 : 147527 : tree new_vec = make_binding_vec (name, want);
258 : 147527 : BINDING_VECTOR_NUM_CLUSTERS (new_vec) = want;
259 : 147527 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (new_vec);
260 : :
261 : : /* Initialize the fixed slots. */
262 : 442581 : for (unsigned jx = BINDING_SLOTS_FIXED; jx--;)
263 : : {
264 : 295054 : cluster[0].indices[jx].base = 0;
265 : 295054 : cluster[0].indices[jx].span = 1;
266 : 295054 : cluster[0].slots[jx] = NULL_TREE;
267 : : }
268 : :
269 : 147527 : if (partition_slot)
270 : : {
271 : 147527 : unsigned off = BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER;
272 : 147527 : unsigned ind = BINDING_SLOT_PARTITION / BINDING_VECTOR_SLOTS_PER_CLUSTER;
273 : 147527 : cluster[ind].indices[off].base = 0;
274 : 147527 : cluster[ind].indices[off].span = 1;
275 : 147527 : cluster[ind].slots[off] = NULL_TREE;
276 : : }
277 : :
278 : 147527 : 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 : 68732 : if (tree type = strip_using_decl (MAYBE_STAT_TYPE (orig)))
285 : 17 : init_global_partition (cluster, type);
286 : :
287 : 157120 : for (ovl_iterator iter (strip_using_decl (MAYBE_STAT_DECL (orig)));
288 : 157120 : iter; ++iter)
289 : : {
290 : 88388 : tree decl = *iter;
291 : :
292 : : /* Internal linkage entities are in deduplicateable. */
293 : 88388 : init_global_partition (cluster, decl);
294 : : }
295 : :
296 : 68732 : if (cluster[0].slots[BINDING_SLOT_GLOBAL]
297 : 68732 : && !(TREE_CODE (orig) == NAMESPACE_DECL
298 : 69531 : && !DECL_NAMESPACE_ALIAS (orig)))
299 : : {
300 : : /* Note that we had some GMF entries. */
301 : 67940 : if (!STAT_HACK_P (orig))
302 : 67913 : orig = stat_hack (orig);
303 : :
304 : 67940 : MODULE_BINDING_GLOBAL_P (orig) = true;
305 : : }
306 : :
307 : 68732 : cluster[0].slots[BINDING_SLOT_CURRENT] = orig;
308 : : }
309 : :
310 : 147527 : *slot = new_vec;
311 : 147527 : }
312 : : else
313 : 102711 : gcc_checking_assert (create >= 0);
314 : :
315 : 250238 : unsigned off = ix % BINDING_VECTOR_SLOTS_PER_CLUSTER;
316 : 250238 : binding_cluster &cluster
317 : 250238 : = BINDING_VECTOR_CLUSTER (*slot, ix / BINDING_VECTOR_SLOTS_PER_CLUSTER);
318 : :
319 : : /* There must always be slots for these indices */
320 : 250238 : gcc_checking_assert (cluster.indices[off].span == 1
321 : : && !cluster.indices[off].base
322 : : && !cluster.slots[off].is_lazy ());
323 : :
324 : 250238 : 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 : 142277 : append_imported_binding_slot (tree *slot, tree name, unsigned ix)
332 : : {
333 : 142277 : gcc_checking_assert (ix);
334 : :
335 : 142277 : if (!*slot || TREE_CODE (*slot) != BINDING_VECTOR)
336 : : /* Make an initial module vector. */
337 : 134596 : get_fixed_binding_slot (slot, name, BINDING_SLOT_GLOBAL, -1);
338 : 7681 : else if (!BINDING_VECTOR_CLUSTER_LAST (*slot)
339 : 7681 : ->indices[BINDING_VECTOR_SLOTS_PER_CLUSTER - 1].span)
340 : : /* There is space in the last cluster. */;
341 : 6423 : else if (BINDING_VECTOR_NUM_CLUSTERS (*slot)
342 : 6423 : != 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 : 6423 : unsigned have = BINDING_VECTOR_NUM_CLUSTERS (*slot);
349 : 6423 : unsigned want = (have * 3 + 1) / 2;
350 : :
351 : 6423 : if (want > (unsigned short)~0)
352 : : want = (unsigned short)~0;
353 : :
354 : 6423 : tree new_vec = make_binding_vec (name, want);
355 : 6423 : BINDING_VECTOR_NUM_CLUSTERS (new_vec) = have + 1;
356 : 12846 : BINDING_VECTOR_GLOBAL_DUPS_P (new_vec)
357 : 6423 : = BINDING_VECTOR_GLOBAL_DUPS_P (*slot);
358 : 12846 : BINDING_VECTOR_PARTITION_DUPS_P (new_vec)
359 : 6423 : = BINDING_VECTOR_PARTITION_DUPS_P (*slot);
360 : 12846 : memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec),
361 : 12846 : BINDING_VECTOR_CLUSTER_BASE (*slot),
362 : 6423 : have * sizeof (binding_cluster));
363 : 6423 : *slot = new_vec;
364 : : }
365 : :
366 : 142277 : binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
367 : 278131 : for (unsigned off = 0; off != BINDING_VECTOR_SLOTS_PER_CLUSTER; off++)
368 : 278131 : if (!last->indices[off].span)
369 : : {
370 : : /* Fill the free slot of the cluster. */
371 : 142277 : last->indices[off].base = ix;
372 : 142277 : last->indices[off].span = 1;
373 : 142277 : last->slots[off] = NULL_TREE;
374 : : /* Check monotonicity. */
375 : 142277 : gcc_checking_assert (last[off ? 0 : -1]
376 : : .indices[off ? off - 1
377 : : : BINDING_VECTOR_SLOTS_PER_CLUSTER - 1]
378 : : .base < ix);
379 : 142277 : 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 : 1183549032 : add_decl_to_level (cp_binding_level *b, tree decl)
389 : : {
390 : 1183549032 : 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 : 1183549032 : gcc_assert (b->names != decl);
397 : :
398 : : /* We build up the list in reverse order, and reverse it later if
399 : : necessary. */
400 : 1183549032 : TREE_CHAIN (decl) = b->names;
401 : 1183549032 : 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 : 1183549032 : if (b->kind == sk_namespace
407 : 1183549032 : && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
408 : 351619946 : || (TREE_CODE (decl) == FUNCTION_DECL
409 : 293005869 : && (!TREE_PUBLIC (decl)
410 : 292891599 : || decl_internal_context_p (decl)
411 : 292890467 : || DECL_DECLARED_INLINE_P (decl)))))
412 : 14203199 : vec_safe_push (static_decls, decl);
413 : 1183549032 : }
414 : :
415 : : /* Find the binding for NAME in the local binding level B. */
416 : :
417 : : static cxx_binding *
418 : 807098336 : find_local_binding (cp_binding_level *b, tree name)
419 : : {
420 : 807098336 : if (cxx_binding *binding = IDENTIFIER_BINDING (name))
421 : 0 : for (;; b = b->level_chain)
422 : : {
423 : 2448301 : if (binding->scope == b)
424 : : return binding;
425 : :
426 : : /* Cleanup contours are transparent to the language. */
427 : 2445412 : 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 : 1469073074 : name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
463 : 1469073074 : : name (n), value (NULL_TREE), type (NULL_TREE),
464 : 1469073074 : want (w),
465 : 1469073074 : deduping (false), scopes (NULL), previous (NULL)
466 : : {
467 : 1469073074 : preserve_state ();
468 : : }
469 : 1469073074 : ~name_lookup ()
470 : : {
471 : 1469073074 : gcc_checking_assert (!deduping);
472 : 1469073074 : restore_state ();
473 : 1469073074 : }
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 : 1503428409 : void dedup (bool state)
482 : : {
483 : 1503428409 : if (deduping != state)
484 : : {
485 : 94097302 : deduping = state;
486 : 94097302 : lookup_mark (value, state);
487 : : }
488 : 1503428409 : }
489 : :
490 : : protected:
491 : 17186583180 : static bool seen_p (tree scope)
492 : : {
493 : 17186583180 : return LOOKUP_SEEN_P (scope);
494 : : }
495 : 27666739 : static bool found_p (tree scope)
496 : : {
497 : 27666739 : return LOOKUP_FOUND_P (scope);
498 : : }
499 : :
500 : : void mark_seen (tree scope); /* Mark and add to scope vector. */
501 : 339715324 : static void mark_found (tree scope)
502 : : {
503 : 339715324 : gcc_checking_assert (seen_p (scope));
504 : 339715324 : LOOKUP_FOUND_P (scope) = true;
505 : 339715324 : }
506 : 8350991352 : bool see_and_mark (tree scope)
507 : : {
508 : 8350991352 : bool ret = seen_p (scope);
509 : 8350991352 : if (!ret)
510 : 1708089869 : mark_seen (scope);
511 : 8182281928 : 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 : 1469073074 : name_lookup::preserve_state ()
581 : : {
582 : 1469073074 : previous = active;
583 : 1469073074 : if (previous)
584 : : {
585 : 9080 : unsigned length = vec_safe_length (previous->scopes);
586 : 9080 : vec_safe_reserve (previous->scopes, length);
587 : 83172 : for (unsigned ix = length; ix--;)
588 : : {
589 : 74092 : tree decl = (*previous->scopes)[ix];
590 : :
591 : 74092 : gcc_checking_assert (LOOKUP_SEEN_P (decl));
592 : 74092 : LOOKUP_SEEN_P (decl) = false;
593 : :
594 : : /* Preserve the FOUND_P state on the interrupted lookup's
595 : : stack. */
596 : 74092 : if (LOOKUP_FOUND_P (decl))
597 : : {
598 : 8992 : LOOKUP_FOUND_P (decl) = false;
599 : 8992 : previous->scopes->quick_push (decl);
600 : : }
601 : : }
602 : :
603 : : /* Unmark the outer partial lookup. */
604 : 9080 : if (previous->deduping)
605 : 0 : lookup_mark (previous->value, false);
606 : : }
607 : : else
608 : 1469063994 : scopes = shared_scopes;
609 : 1469073074 : active = this;
610 : 1469073074 : }
611 : :
612 : : /* Restore the marking state of a lookup we interrupted. */
613 : :
614 : : void
615 : 1469073074 : name_lookup::restore_state ()
616 : : {
617 : 1469073074 : gcc_checking_assert (!deduping);
618 : :
619 : : /* Unmark and empty this lookup's scope stack. */
620 : 11120428076 : for (unsigned ix = vec_safe_length (scopes); ix--;)
621 : : {
622 : 8182281928 : tree decl = scopes->pop ();
623 : 8182281928 : gcc_checking_assert (LOOKUP_SEEN_P (decl));
624 : 8182281928 : LOOKUP_SEEN_P (decl) = false;
625 : 8182281928 : LOOKUP_FOUND_P (decl) = false;
626 : : }
627 : :
628 : 1469073074 : active = previous;
629 : 1469073074 : if (previous)
630 : : {
631 : 9080 : free (scopes);
632 : :
633 : 9080 : unsigned length = vec_safe_length (previous->scopes);
634 : 83172 : for (unsigned ix = 0; ix != length; ix++)
635 : : {
636 : 82399 : tree decl = (*previous->scopes)[ix];
637 : 82399 : 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 : 8992 : do
642 : : {
643 : 8992 : tree decl = previous->scopes->pop ();
644 : 8992 : gcc_checking_assert (LOOKUP_SEEN_P (decl)
645 : : && !LOOKUP_FOUND_P (decl));
646 : 8992 : LOOKUP_FOUND_P (decl) = true;
647 : : }
648 : 8992 : while (++ix != length);
649 : : break;
650 : : }
651 : :
652 : 74092 : gcc_checking_assert (!LOOKUP_FOUND_P (decl));
653 : 74092 : LOOKUP_SEEN_P (decl) = true;
654 : : }
655 : :
656 : : /* Remark the outer partial lookup. */
657 : 9080 : if (previous->deduping)
658 : 0 : lookup_mark (previous->value, true);
659 : : }
660 : : else
661 : 1469063994 : shared_scopes = scopes;
662 : 1469073074 : }
663 : :
664 : : void
665 : 8182281928 : name_lookup::mark_seen (tree scope)
666 : : {
667 : 8182281928 : gcc_checking_assert (!seen_p (scope));
668 : 8182281928 : LOOKUP_SEEN_P (scope) = true;
669 : 8182281928 : vec_safe_push (scopes, scope);
670 : 8182281928 : }
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 : 538 : name_lookup::ambiguous (tree thing, tree current)
690 : : {
691 : 538 : if (TREE_CODE (current) != TREE_LIST)
692 : : {
693 : 379 : current = build_tree_list (NULL_TREE, current);
694 : 379 : TREE_TYPE (current) = error_mark_node;
695 : : }
696 : 538 : current = tree_cons (NULL_TREE, thing, current);
697 : 538 : TREE_TYPE (current) = error_mark_node;
698 : :
699 : 538 : return current;
700 : : }
701 : :
702 : : /* FNS is a new overload set to add to the exising set. */
703 : :
704 : : void
705 : 501689523 : name_lookup::add_overload (tree fns)
706 : : {
707 : 501689523 : if (!deduping && TREE_CODE (fns) == OVERLOAD)
708 : : {
709 : 365801670 : tree probe = fns;
710 : 365801670 : if (!bool (want & LOOK_want::HIDDEN_FRIEND))
711 : 365677946 : probe = ovl_skip_hidden (probe);
712 : 365801670 : if (probe && TREE_CODE (probe) == OVERLOAD
713 : 731603340 : && OVL_DEDUP_P (probe))
714 : : /* We're about to add something found by multiple paths, so need to
715 : : engage deduping mode. */
716 : 30957751 : dedup (true);
717 : : }
718 : :
719 : 501689523 : value = lookup_maybe_add (fns, value, deduping);
720 : 501689523 : }
721 : :
722 : : /* Add a NEW_VAL, a found value binding into the current value binding. */
723 : :
724 : : void
725 : 1176434744 : name_lookup::add_value (tree new_val)
726 : : {
727 : 1176434744 : if (OVL_P (new_val) && (!value || OVL_P (value)))
728 : 476643889 : add_overload (new_val);
729 : 699790855 : else if (!value)
730 : 699564256 : value = new_val;
731 : 226599 : else if (value == new_val)
732 : : ;
733 : 12168 : else if ((TREE_CODE (value) == TYPE_DECL
734 : 12022 : && TREE_CODE (new_val) == TYPE_DECL
735 : 24190 : && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
736 : : /* Typedefs to the same type. */;
737 : 232 : else if (TREE_CODE (value) == NAMESPACE_DECL
738 : 59 : && TREE_CODE (new_val) == NAMESPACE_DECL
739 : 291 : && 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 : 202 : dedup (false);
747 : 202 : value = ambiguous (new_val, value);
748 : : }
749 : 1176434744 : }
750 : :
751 : : /* Add a NEW_TYPE, a found type binding into the current type binding. */
752 : :
753 : : void
754 : 419454 : name_lookup::add_type (tree new_type)
755 : : {
756 : 419454 : if (!type)
757 : 419451 : 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 : 419454 : }
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 : 1188034747 : name_lookup::process_binding (tree new_val, tree new_type)
769 : : {
770 : : /* Did we really see a type? */
771 : 1188034747 : if (new_type
772 : 1188034747 : && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
773 : : new_type = NULL_TREE;
774 : :
775 : 1188034747 : new_val = strip_using_decl (new_val);
776 : 1188034747 : new_type = strip_using_decl (new_type);
777 : :
778 : : /* Do we really see a value? */
779 : 1188034747 : if (new_val)
780 : 1176528372 : switch (TREE_CODE (new_val))
781 : : {
782 : 208071735 : case TEMPLATE_DECL:
783 : : /* If we expect types or namespaces, and not templates,
784 : : or this is not a template class. */
785 : 208071735 : if (bool (want & LOOK_want::TYPE_NAMESPACE)
786 : 208071735 : && !DECL_TYPE_TEMPLATE_P (new_val))
787 : : new_val = NULL_TREE;
788 : : break;
789 : 228071393 : case TYPE_DECL:
790 : 228071393 : if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
791 : 228071393 : || (new_type && bool (want & LOOK_want::TYPE)))
792 : : new_val = NULL_TREE;
793 : : break;
794 : 204417265 : case NAMESPACE_DECL:
795 : 204417265 : if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
796 : : new_val = NULL_TREE;
797 : : break;
798 : 535967979 : default:
799 : 535967979 : if (bool (want & LOOK_want::TYPE_NAMESPACE))
800 : : new_val = NULL_TREE;
801 : : }
802 : :
803 : : if (!new_val)
804 : : {
805 : 11642984 : new_val = new_type;
806 : 11642984 : new_type = NULL_TREE;
807 : : }
808 : :
809 : : /* Merge into the lookup */
810 : 11642984 : if (new_val)
811 : 1176434744 : add_value (new_val);
812 : 1188034747 : if (new_type)
813 : 419454 : add_type (new_type);
814 : :
815 : 1188034747 : 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 : 38272 : 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 : 38272 : if (new_val && !new_type
829 : 35049 : && TREE_CODE (new_val) == NAMESPACE_DECL
830 : 3746 : && TREE_PUBLIC (new_val)
831 : 42012 : && !DECL_NAMESPACE_ALIAS (new_val))
832 : : {
833 : 3710 : if (marker & 2)
834 : : return marker;
835 : 2207 : marker |= 2;
836 : : }
837 : :
838 : 36769 : if (new_type || new_val)
839 : 33621 : marker |= process_binding (new_val, new_type);
840 : :
841 : : return marker;
842 : : }
843 : :
844 : : /* Look in exactly namespace SCOPE. */
845 : :
846 : : bool
847 : 8032373740 : name_lookup::search_namespace_only (tree scope)
848 : : {
849 : 8032373740 : bool found = false;
850 : 8032373740 : if (tree *binding = find_namespace_slot (scope, name))
851 : : {
852 : 1188027746 : tree val = *binding;
853 : 1188027746 : 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 : 26620 : bitmap imports = get_import_bitmap ();
859 : 26620 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
860 : 26620 : int marker = 0;
861 : 26620 : int dup_detect = 0;
862 : :
863 : 26620 : if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
864 : : {
865 : 12279 : if (!deduping)
866 : : {
867 : 11198 : if (named_module_purview_p ())
868 : : {
869 : 304 : dup_detect |= 2;
870 : :
871 : 304 : if (STAT_HACK_P (bind) && MODULE_BINDING_GLOBAL_P (bind))
872 : : dup_detect |= 1;
873 : : }
874 : : else
875 : : dup_detect |= 1;
876 : : }
877 : 12279 : tree type = NULL_TREE;
878 : 12279 : tree value = bind;
879 : :
880 : 12279 : if (STAT_HACK_P (bind))
881 : : {
882 : 9812 : type = STAT_TYPE (bind);
883 : 9812 : value = STAT_DECL (bind);
884 : :
885 : 9812 : if (!bool (want & LOOK_want::HIDDEN_FRIEND))
886 : : {
887 : 9760 : if (STAT_TYPE_HIDDEN_P (bind))
888 : 0 : type = NULL_TREE;
889 : 9760 : if (STAT_DECL_HIDDEN_P (bind))
890 : : value = NULL_TREE;
891 : : else
892 : 9760 : value = ovl_skip_hidden (value);
893 : : }
894 : : }
895 : 2467 : else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
896 : 2467 : value = ovl_skip_hidden (value);
897 : :
898 : 12279 : marker = process_module_binding (value, type, marker);
899 : : }
900 : :
901 : : /* Scan the imported bindings. */
902 : 26620 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
903 : 26620 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
904 : : {
905 : 26620 : ix--;
906 : 26620 : cluster++;
907 : : }
908 : :
909 : : /* Do this in forward order, so we load modules in an order
910 : : the user expects. */
911 : 53969 : for (; ix--; cluster++)
912 : 82047 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
913 : : {
914 : : /* Are we importing this module? */
915 : 54698 : if (unsigned base = cluster->indices[jx].base)
916 : 26280 : if (unsigned span = cluster->indices[jx].span)
917 : 26289 : do
918 : 26289 : if (bool (want & LOOK_want::ANY_REACHABLE)
919 : 26289 : || bitmap_bit_p (imports, base))
920 : 25993 : goto found;
921 : 296 : while (++base, --span);
922 : 28705 : continue;
923 : :
924 : 25993 : found:;
925 : : /* Is it loaded? */
926 : 25993 : if (cluster->slots[jx].is_lazy ())
927 : : {
928 : 2487 : gcc_assert (cluster->indices[jx].span == 1);
929 : 2487 : lazy_load_binding (cluster->indices[jx].base,
930 : : scope, name, &cluster->slots[jx]);
931 : : }
932 : 25993 : tree bind = cluster->slots[jx];
933 : 25993 : 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 : 25993 : tree type = NULL_TREE;
940 : :
941 : : /* If STAT_HACK_P is false, everything is visible, and
942 : : there's no duplication possibilities. */
943 : 25993 : if (STAT_HACK_P (bind))
944 : : {
945 : 17767 : if (!deduping)
946 : : {
947 : : /* Do we need to engage deduplication? */
948 : 16554 : int dup = 0;
949 : 16554 : if (MODULE_BINDING_GLOBAL_P (bind))
950 : 14596 : dup |= 1;
951 : 16554 : if (MODULE_BINDING_PARTITION_P (bind))
952 : 1404 : dup |= 2;
953 : 16554 : if (unsigned hit = dup_detect & dup)
954 : : {
955 : 8432 : if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
956 : 9156 : || (hit & 2
957 : 234 : && BINDING_VECTOR_PARTITION_DUPS_P (val)))
958 : 8062 : dedup (true);
959 : : }
960 : 16554 : dup_detect |= dup;
961 : : }
962 : :
963 : 17767 : 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 : 17682 : if (STAT_TYPE_VISIBLE_P (bind))
971 : 1437 : type = STAT_TYPE (bind);
972 : 17682 : bind = STAT_VISIBLE (bind);
973 : : }
974 : : }
975 : :
976 : : /* And process it. */
977 : 25993 : marker = process_module_binding (bind, type, marker);
978 : 28705 : }
979 : 26620 : found |= marker & 1;
980 : : }
981 : : else
982 : : {
983 : : /* Only a current module binding, visible from the current module. */
984 : 1188001126 : tree bind = *binding;
985 : 1188001126 : tree value = bind, type = NULL_TREE;
986 : :
987 : 1188001126 : if (STAT_HACK_P (bind))
988 : : {
989 : 462987 : type = STAT_TYPE (bind);
990 : 462987 : value = STAT_DECL (bind);
991 : :
992 : 462987 : if (!bool (want & LOOK_want::HIDDEN_FRIEND))
993 : : {
994 : 462783 : if (STAT_TYPE_HIDDEN_P (bind))
995 : 3 : type = NULL_TREE;
996 : 462783 : if (STAT_DECL_HIDDEN_P (bind))
997 : : value = NULL_TREE;
998 : : else
999 : 462363 : value = ovl_skip_hidden (value);
1000 : : }
1001 : : }
1002 : 1187538139 : else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
1003 : 1186985139 : value = ovl_skip_hidden (value);
1004 : :
1005 : 1188001126 : found |= process_binding (value, type);
1006 : : }
1007 : : }
1008 : :
1009 : 8032373740 : return found;
1010 : : }
1011 : :
1012 : : /* Conditionally look in namespace SCOPE and inline children. */
1013 : :
1014 : : bool
1015 : 1564456952 : name_lookup::search_namespace (tree scope)
1016 : : {
1017 : 1564456952 : 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 : 1564456952 : 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 : 1564456952 : if (name)
1027 : : /* Recursively look in its inline children. */
1028 : 1564455232 : if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1029 : 1691373418 : for (unsigned ix = inlinees->length (); ix--;)
1030 : 1250862376 : found |= search_namespace ((*inlinees)[ix]);
1031 : :
1032 : 1564456952 : if (found)
1033 : 321130050 : 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 : 5050519 : 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 : 5050519 : if (found_p (scope))
1047 : : return true;
1048 : :
1049 : 5050519 : bool found = false;
1050 : 5050519 : if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
1051 : 894168 : for (unsigned ix = usings->length (); ix--;)
1052 : 447473 : found |= search_qualified (strip_using_decl ((*usings)[ix]), true);
1053 : :
1054 : : /* Look in its inline children. */
1055 : 5050519 : if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1056 : 2059572 : for (unsigned ix = inlinees->length (); ix--;)
1057 : 1080562 : found |= search_usings ((*inlinees)[ix]);
1058 : :
1059 : 5050519 : if (found)
1060 : 20496 : 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 : 313594576 : name_lookup::search_qualified (tree scope, bool usings)
1076 : : {
1077 : 313594576 : bool found = false;
1078 : :
1079 : 313594576 : if (seen_p (scope))
1080 : 0 : found = found_p (scope);
1081 : : else
1082 : : {
1083 : 313594576 : found = search_namespace (scope);
1084 : 313594576 : if (!found && usings)
1085 : 3969957 : found = search_usings (scope);
1086 : : }
1087 : :
1088 : 313594576 : dedup (false);
1089 : :
1090 : 313594576 : 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 : 6539900388 : name_lookup::queue_namespace (using_queue& queue, int depth, tree scope)
1098 : : {
1099 : 6539900388 : if (see_and_mark (scope))
1100 : 6539900388 : return;
1101 : :
1102 : : /* Record it. */
1103 : : tree common = scope;
1104 : 13106865028 : while (SCOPE_DEPTH (common) > depth)
1105 : 6632672969 : common = CP_DECL_CONTEXT (common);
1106 : 6474192059 : queue.safe_push (using_pair (common, scope));
1107 : :
1108 : : /* Queue its inline children. */
1109 : 6474192059 : if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1110 : 6461062801 : for (unsigned ix = inlinees->length (); ix--;)
1111 : 4778966512 : queue_namespace (queue, depth, (*inlinees)[ix]);
1112 : :
1113 : : /* Queue its using targets. */
1114 : 6474192059 : 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 : 9980759204 : name_lookup::queue_usings (using_queue& queue, int depth, vec<tree, va_gc> *usings)
1121 : : {
1122 : 9980759204 : if (usings)
1123 : 36590083 : for (unsigned ix = usings->length (); ix--;)
1124 : 18521795 : queue_namespace (queue, depth, strip_using_decl ((*usings)[ix]));
1125 : 9980759204 : }
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 : 1117239319 : name_lookup::search_unqualified (tree scope, cp_binding_level *level)
1135 : : {
1136 : 1117239319 : using_queue queue;
1137 : 1117239319 : bool found = false;
1138 : :
1139 : : /* Queue local using-directives. */
1140 : 4623806464 : for (; level->kind != sk_namespace; level = level->level_chain)
1141 : 3506567145 : queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
1142 : :
1143 : 3511653465 : for (; !found; scope = CP_DECL_CONTEXT (scope))
1144 : : {
1145 : 1742412081 : gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
1146 : 1742412081 : int depth = SCOPE_DEPTH (scope);
1147 : :
1148 : : /* Queue namespaces reachable from SCOPE. */
1149 : 1742412081 : queue_namespace (queue, depth, scope);
1150 : :
1151 : : /* Search every queued namespace where SCOPE is the common
1152 : : ancestor. Adjust the others. */
1153 : 1742412081 : unsigned ix = 0;
1154 : 1755117925 : do
1155 : : {
1156 : 1755117925 : using_pair &pair = queue[ix];
1157 : 8237091371 : while (pair.first == scope)
1158 : : {
1159 : 6467916788 : found |= search_namespace_only (pair.second);
1160 : 6467916788 : pair = queue.pop ();
1161 : 6467916788 : if (ix == queue.length ())
1162 : 1741061267 : goto done;
1163 : : }
1164 : : /* The depth is the same as SCOPE, find the parent scope. */
1165 : 14056658 : if (SCOPE_DEPTH (pair.first) == depth)
1166 : 14052653 : pair.first = CP_DECL_CONTEXT (pair.first);
1167 : 14056658 : ix++;
1168 : : }
1169 : 28113316 : while (ix < queue.length ());
1170 : 1350814 : done:;
1171 : 1742412081 : 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 : 1197608589 : if (bool (want & LOOK_want::HIDDEN_FRIEND))
1181 : : break;
1182 : : }
1183 : :
1184 : 1117239319 : dedup (false);
1185 : :
1186 : 1117239319 : return found;
1187 : 1117239319 : }
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 : 27247083 : name_lookup::add_fns (tree fns)
1194 : : {
1195 : 27247083 : if (!fns)
1196 : : return;
1197 : 25053388 : else if (TREE_CODE (fns) == OVERLOAD)
1198 : : {
1199 : 14512774 : if (TREE_TYPE (fns) != unknown_type_node)
1200 : 86759 : fns = OVL_FUNCTION (fns);
1201 : : }
1202 : 10540614 : else if (!DECL_DECLARES_FUNCTION_P (fns))
1203 : : return;
1204 : :
1205 : 25045634 : add_overload (fns);
1206 : : }
1207 : :
1208 : : /* Add the overloaded fns of SCOPE. */
1209 : :
1210 : : void
1211 : 107910664 : name_lookup::adl_namespace_fns (tree scope, bitmap imports)
1212 : : {
1213 : 107910664 : if (tree *binding = find_namespace_slot (scope, name))
1214 : : {
1215 : 21124055 : tree val = *binding;
1216 : 21124055 : if (TREE_CODE (val) != BINDING_VECTOR)
1217 : 21111000 : 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 : 13055 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val);
1224 : 13055 : int dup_detect = 0;
1225 : :
1226 : 13055 : 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 : 9362 : if (!deduping)
1232 : : {
1233 : 3047 : 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 : 9362 : add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind)));
1245 : : }
1246 : :
1247 : : /* Scan the imported bindings. */
1248 : 13055 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (val);
1249 : 13055 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
1250 : : {
1251 : 13055 : ix--;
1252 : 13055 : cluster++;
1253 : : }
1254 : :
1255 : : /* Do this in forward order, so we load modules in an order
1256 : : the user expects. */
1257 : 26119 : for (; ix--; cluster++)
1258 : 39192 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
1259 : : {
1260 : : /* Functions are never on merged slots. */
1261 : 26128 : if (!cluster->indices[jx].base
1262 : 12344 : || cluster->indices[jx].span != 1)
1263 : 13784 : continue;
1264 : :
1265 : : /* Is this slot visible? */
1266 : 12344 : if (!bitmap_bit_p (imports, cluster->indices[jx].base))
1267 : 0 : continue;
1268 : :
1269 : : /* Is it loaded. */
1270 : 12344 : if (cluster->slots[jx].is_lazy ())
1271 : 12 : lazy_load_binding (cluster->indices[jx].base,
1272 : : scope, name, &cluster->slots[jx]);
1273 : :
1274 : 12344 : tree bind = cluster->slots[jx];
1275 : 12344 : if (!bind)
1276 : : /* Load errors could mean there's nothing here. */
1277 : 0 : continue;
1278 : :
1279 : 12344 : if (STAT_HACK_P (bind))
1280 : : {
1281 : 12281 : if (!deduping)
1282 : : {
1283 : : /* Do we need to engage deduplication? */
1284 : 4881 : int dup = 0;
1285 : 4881 : if (MODULE_BINDING_GLOBAL_P (bind))
1286 : 4848 : dup |= 1;
1287 : 4881 : if (MODULE_BINDING_PARTITION_P (bind))
1288 : 15 : dup |= 2;
1289 : 4881 : if (unsigned hit = dup_detect & dup)
1290 : 2594 : if ((hit & 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val))
1291 : 2731 : || (hit & 2
1292 : 9 : && BINDING_VECTOR_PARTITION_DUPS_P (val)))
1293 : 2466 : dedup (true);
1294 : 4881 : dup_detect |= dup;
1295 : : }
1296 : :
1297 : 12281 : bind = STAT_VISIBLE (bind);
1298 : : }
1299 : :
1300 : 12344 : add_fns (bind);
1301 : : }
1302 : : }
1303 : : }
1304 : 107910664 : }
1305 : :
1306 : : /* Add the hidden friends of SCOPE. */
1307 : :
1308 : : void
1309 : 25964743 : name_lookup::adl_class_fns (tree type)
1310 : : {
1311 : : /* Add friends. */
1312 : 25964743 : for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
1313 : 59323720 : list; list = TREE_CHAIN (list))
1314 : 33358977 : if (name == FRIEND_NAME (list))
1315 : : {
1316 : 3387969 : tree context = NULL_TREE; /* Lazily computed. */
1317 : 9526950 : for (tree friends = FRIEND_DECLS (list); friends;
1318 : 6138981 : friends = TREE_CHAIN (friends))
1319 : : {
1320 : 6138981 : tree fn = TREE_VALUE (friends);
1321 : :
1322 : : /* Only interested in global functions with potentially hidden
1323 : : (i.e. unqualified) declarations. */
1324 : 6138981 : if (!context)
1325 : 3387969 : context = decl_namespace_context (type);
1326 : 6138981 : if (CP_DECL_CONTEXT (fn) != context)
1327 : 21639 : continue;
1328 : :
1329 : 6117342 : dedup (true);
1330 : :
1331 : : /* Template specializations are never found by name lookup.
1332 : : (Templates themselves can be found, but not template
1333 : : specializations.) */
1334 : 6117342 : if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
1335 : 2965 : continue;
1336 : :
1337 : 6114377 : add_fns (fn);
1338 : : }
1339 : : }
1340 : 25964743 : }
1341 : :
1342 : : /* Find the containing non-inlined namespace, add it and all its
1343 : : inlinees. */
1344 : :
1345 : : void
1346 : 119031461 : name_lookup::adl_namespace (tree scope)
1347 : : {
1348 : 203460544 : if (see_and_mark (scope))
1349 : : return;
1350 : :
1351 : : /* Look down into inline namespaces. */
1352 : 107910664 : if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
1353 : 113170843 : for (unsigned ix = inlinees->length (); ix--;)
1354 : 84429083 : adl_namespace ((*inlinees)[ix]);
1355 : :
1356 : 107910664 : if (DECL_NAMESPACE_INLINE_P (scope))
1357 : : /* Mark parent. */
1358 : 84429083 : adl_namespace (CP_DECL_CONTEXT (scope));
1359 : : }
1360 : :
1361 : : /* Adds the class and its friends to the lookup structure. */
1362 : :
1363 : : void
1364 : 26099434 : 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 : 26099434 : if (!CLASS_TYPE_P (type))
1369 : : return;
1370 : :
1371 : 26099434 : type = TYPE_MAIN_VARIANT (type);
1372 : :
1373 : 26099434 : if (see_and_mark (type))
1374 : : return;
1375 : :
1376 : 25964743 : tree context = decl_namespace_context (type);
1377 : 25964743 : 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 : 23146961 : name_lookup::adl_bases (tree type)
1385 : : {
1386 : 23146961 : adl_class_only (type);
1387 : :
1388 : : /* Process baseclasses. */
1389 : 23146961 : if (tree binfo = TYPE_BINFO (type))
1390 : : {
1391 : : tree base_binfo;
1392 : : int i;
1393 : :
1394 : 26773941 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1395 : 4582183 : adl_bases (BINFO_TYPE (base_binfo));
1396 : : }
1397 : 23146961 : }
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 : 22697725 : name_lookup::adl_class (tree type)
1417 : : {
1418 : : /* Backend build structures, such as __builtin_va_list, aren't
1419 : : affected by all this. */
1420 : 22697725 : if (!CLASS_TYPE_P (type))
1421 : : return;
1422 : :
1423 : 22616220 : 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 : 22616220 : if (found_p (type))
1428 : : return;
1429 : :
1430 : 18564778 : complete_type (type);
1431 : 18564778 : adl_bases (type);
1432 : 18564778 : mark_found (type);
1433 : :
1434 : 18564778 : if (TYPE_CLASS_SCOPE_P (type))
1435 : 1829044 : adl_class_only (TYPE_CONTEXT (type));
1436 : :
1437 : : /* Process template arguments. */
1438 : 18564778 : if (CLASSTYPE_TEMPLATE_INFO (type)
1439 : 18564778 : && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
1440 : : {
1441 : 9822098 : tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1442 : 28459069 : for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
1443 : 18636971 : adl_template_arg (TREE_VEC_ELT (list, i));
1444 : : }
1445 : : }
1446 : :
1447 : : void
1448 : 17074034 : name_lookup::adl_enum (tree type)
1449 : : {
1450 : 17074034 : type = TYPE_MAIN_VARIANT (type);
1451 : 17074034 : if (see_and_mark (type))
1452 : : return;
1453 : :
1454 : 9757510 : if (TYPE_CLASS_SCOPE_P (type))
1455 : 1123423 : adl_class_only (TYPE_CONTEXT (type));
1456 : : else
1457 : 8634087 : adl_namespace (decl_namespace_context (type));
1458 : : }
1459 : :
1460 : : void
1461 : 59795413 : name_lookup::adl_expr (tree expr)
1462 : : {
1463 : 59795413 : if (!expr)
1464 : : return;
1465 : :
1466 : 59795413 : gcc_assert (!TYPE_P (expr));
1467 : :
1468 : 59795413 : if (TREE_TYPE (expr) != unknown_type_node)
1469 : : {
1470 : 59789690 : adl_type (unlowered_expr_type (expr));
1471 : 59789690 : return;
1472 : : }
1473 : :
1474 : 5723 : if (TREE_CODE (expr) == ADDR_EXPR)
1475 : 440 : expr = TREE_OPERAND (expr, 0);
1476 : 5723 : if (TREE_CODE (expr) == COMPONENT_REF
1477 : 5723 : || TREE_CODE (expr) == OFFSET_REF)
1478 : 402 : expr = TREE_OPERAND (expr, 1);
1479 : 5723 : expr = MAYBE_BASELINK_FUNCTIONS (expr);
1480 : :
1481 : 5723 : if (OVL_P (expr))
1482 : 11054 : for (lkp_iterator iter (expr); iter; ++iter)
1483 : 5681 : adl_type (TREE_TYPE (*iter));
1484 : 350 : 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 : 350 : adl_expr (TREE_OPERAND (expr, 0));
1494 : :
1495 : : /* Now the arguments. */
1496 : 350 : if (tree args = TREE_OPERAND (expr, 1))
1497 : 709 : for (int ix = TREE_VEC_LENGTH (args); ix--;)
1498 : 359 : adl_template_arg (TREE_VEC_ELT (args, ix));
1499 : : }
1500 : : }
1501 : :
1502 : : void
1503 : 76614339 : name_lookup::adl_type (tree type)
1504 : : {
1505 : 86253671 : if (!type)
1506 : : return;
1507 : :
1508 : 86253671 : if (TYPE_PTRDATAMEM_P (type))
1509 : : {
1510 : : /* Pointer to member: associate class type and value type. */
1511 : 1097 : adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
1512 : 1097 : adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1513 : 1097 : return;
1514 : : }
1515 : :
1516 : 86252574 : switch (TREE_CODE (type))
1517 : : {
1518 : 21475840 : case RECORD_TYPE:
1519 : 21475840 : if (TYPE_PTRMEMFUNC_P (type))
1520 : : {
1521 : 48720 : adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
1522 : 48720 : return;
1523 : : }
1524 : : /* FALLTHRU */
1525 : 22697725 : case UNION_TYPE:
1526 : 22697725 : adl_class (type);
1527 : 22697725 : return;
1528 : :
1529 : 123210 : case METHOD_TYPE:
1530 : : /* The basetype is referenced in the first arg type, so just
1531 : : fall through. */
1532 : 123210 : case FUNCTION_TYPE:
1533 : : /* Associate the parameter types. */
1534 : 571185 : for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
1535 : 447975 : adl_type (TREE_VALUE (args));
1536 : : /* FALLTHROUGH */
1537 : :
1538 : 9589503 : case POINTER_TYPE:
1539 : 9589503 : case REFERENCE_TYPE:
1540 : 9589503 : case ARRAY_TYPE:
1541 : 9589503 : adl_type (TREE_TYPE (type));
1542 : 9589503 : return;
1543 : :
1544 : 17074034 : case ENUMERAL_TYPE:
1545 : 17074034 : adl_enum (type);
1546 : 17074034 : return;
1547 : :
1548 : 707 : case LANG_TYPE:
1549 : 707 : 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 : 18723989 : 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 : 18723989 : if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1581 : 18723989 : || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1582 : : ;
1583 : 18723989 : else if (TREE_CODE (arg) == TEMPLATE_DECL)
1584 : : {
1585 : 3554 : tree ctx = CP_DECL_CONTEXT (arg);
1586 : :
1587 : : /* It's not a member template. */
1588 : 3554 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
1589 : 3548 : 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 : 18720435 : else if (ARGUMENT_PACK_P (arg))
1596 : : {
1597 : 31965 : tree args = ARGUMENT_PACK_ARGS (arg);
1598 : 31965 : int i, len = TREE_VEC_LENGTH (args);
1599 : 118624 : for (i = 0; i < len; ++i)
1600 : 86659 : 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 : 18688470 : else if (TYPE_P (arg))
1605 : 16369816 : adl_type (arg);
1606 : 18723989 : }
1607 : :
1608 : : /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1609 : : the call arguments. */
1610 : :
1611 : : tree
1612 : 32692599 : name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1613 : : {
1614 : 32692599 : gcc_checking_assert (!vec_safe_length (scopes));
1615 : :
1616 : : /* Gather each associated entity onto the lookup's scope list. */
1617 : 32692599 : unsigned ix;
1618 : 32692599 : tree arg;
1619 : :
1620 : 92487742 : FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1621 : : /* OMP reduction operators put an ADL-significant type as the
1622 : : first arg. */
1623 : 59795143 : if (TYPE_P (arg))
1624 : 80 : adl_type (arg);
1625 : : else
1626 : 59795063 : adl_expr (arg);
1627 : :
1628 : 32692599 : if (vec_safe_length (scopes))
1629 : : {
1630 : : /* Now do the lookups. */
1631 : 20326450 : value = fns;
1632 : 20326450 : if (fns)
1633 : 15182241 : dedup (true);
1634 : :
1635 : : /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1636 : 20326450 : bitmap inst_path = NULL;
1637 : : /* VISIBLE is the regular import bitmap. */
1638 : 20326450 : bitmap visible = visible_instantiation_path (&inst_path);
1639 : :
1640 : 163959367 : for (unsigned ix = scopes->length (); ix--;)
1641 : : {
1642 : 143632917 : tree scope = (*scopes)[ix];
1643 : 143632917 : if (TREE_CODE (scope) == NAMESPACE_DECL)
1644 : 107910664 : adl_namespace_fns (scope, visible);
1645 : : else
1646 : : {
1647 : 35722253 : if (RECORD_OR_UNION_TYPE_P (scope))
1648 : 25964743 : 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 : 35722253 : if (!inst_path)
1658 : : /* Not 2nd phase. */
1659 : 35637849 : continue;
1660 : :
1661 : 84404 : tree ctx = CP_DECL_CONTEXT (TYPE_NAME (scope));
1662 : 84404 : if (TREE_CODE (ctx) != NAMESPACE_DECL)
1663 : : /* Not namespace-scope class. */
1664 : 10859 : continue;
1665 : :
1666 : 73545 : tree origin = get_originating_module_decl (TYPE_NAME (scope));
1667 : 73545 : tree not_tmpl = STRIP_TEMPLATE (origin);
1668 : 73545 : if (!DECL_LANG_SPECIFIC (not_tmpl)
1669 : 123378 : || !DECL_MODULE_IMPORT_P (not_tmpl))
1670 : : /* Not imported. */
1671 : 62580 : continue;
1672 : :
1673 : 10965 : unsigned module = get_importing_module (origin);
1674 : :
1675 : 10965 : if (!bitmap_bit_p (inst_path, module))
1676 : : /* Not on path of instantiation. */
1677 : 9 : continue;
1678 : :
1679 : 10956 : 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 : 10956 : 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 : 20326450 : fns = value;
1705 : 20326450 : dedup (false);
1706 : : }
1707 : :
1708 : 32692599 : 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 : 32692599 : lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1724 : : {
1725 : 32692599 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1726 : 32692599 : name_lookup lookup (name);
1727 : 32692599 : return lookup.search_adl (fns, args);
1728 : 32692599 : }
1729 : :
1730 : : /* FNS is an overload set of conversion functions. Return the
1731 : : overloads converting to TYPE. */
1732 : :
1733 : : static tree
1734 : 404540 : extract_conversion_operator (tree fns, tree type)
1735 : : {
1736 : 404540 : tree convs = NULL_TREE;
1737 : 404540 : tree tpls = NULL_TREE;
1738 : :
1739 : 1264380 : for (ovl_iterator iter (fns); iter; ++iter)
1740 : : {
1741 : 596970 : if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1742 : 424536 : convs = lookup_add (*iter, convs);
1743 : :
1744 : 596970 : if (TREE_CODE (*iter) == TEMPLATE_DECL)
1745 : 70524 : tpls = lookup_add (*iter, tpls);
1746 : : }
1747 : :
1748 : 404540 : if (!convs)
1749 : 118660 : convs = tpls;
1750 : :
1751 : 404540 : return convs;
1752 : : }
1753 : :
1754 : : /* Binary search of (ordered) MEMBER_VEC for NAME. */
1755 : :
1756 : : static tree
1757 : 2616814909 : member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1758 : : {
1759 : 15878336523 : for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1760 : : {
1761 : 11335338267 : unsigned mid = (lo + hi) / 2;
1762 : 11335338267 : tree binding = (*member_vec)[mid];
1763 : 22670676534 : tree binding_name = OVL_NAME (binding);
1764 : :
1765 : 11335338267 : if (binding_name > name)
1766 : : hi = mid;
1767 : 5594177305 : else if (binding_name < name)
1768 : 4903545743 : 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 : 682940567 : member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1780 : : {
1781 : 6499810989 : for (int ix = member_vec->length (); ix--;)
1782 : 5869388698 : if (tree binding = (*member_vec)[ix])
1783 : 5869388698 : 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 : 2448131222 : fields_linear_search (tree klass, tree name, bool want_type)
1793 : : {
1794 : 36055594292 : for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1795 : : {
1796 : 33711377606 : tree decl = fields;
1797 : :
1798 : 33711377606 : if (TREE_CODE (decl) == FIELD_DECL
1799 : 33711377606 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1800 : : {
1801 : 30330724 : if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1802 : : return temp;
1803 : : }
1804 : :
1805 : 33711365500 : if (DECL_NAME (decl) != name)
1806 : 33564535346 : continue;
1807 : :
1808 : 146830154 : if (TREE_CODE (decl) == USING_DECL)
1809 : : {
1810 : 923648 : decl = strip_using_decl (decl);
1811 : 923648 : if (is_overloaded_fn (decl))
1812 : 223649 : continue;
1813 : : }
1814 : :
1815 : 146606505 : if (DECL_DECLARES_FUNCTION_P (decl))
1816 : : /* Functions are found separately. */
1817 : 42260595 : continue;
1818 : :
1819 : 104345910 : 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 : 3058 : for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1834 : : {
1835 : 2443 : tree decl = fields;
1836 : :
1837 : 2443 : if (TREE_CODE (decl) == FIELD_DECL
1838 : 2443 : && 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 : 2443 : if (DECL_NAME (decl) != name)
1848 : 2431 : 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 : 30330724 : search_anon_aggr (tree anon, tree name, bool want_type)
1898 : : {
1899 : 30330724 : gcc_assert (COMPLETE_TYPE_P (anon));
1900 : 30330724 : tree ret = get_class_binding_direct (anon, name, want_type);
1901 : 30330724 : 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 : 5117107508 : get_class_binding_direct (tree klass, tree name, bool want_type)
1913 : : {
1914 : 5117107508 : 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 : 5117107508 : bool conv_op = IDENTIFIER_CONV_OP_P (name);
1919 : 5117107508 : tree lookup = conv_op ? conv_op_identifier : name;
1920 : 5117107508 : tree val = NULL_TREE;
1921 : 5117107508 : vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1922 : :
1923 : 5117107508 : if (COMPLETE_TYPE_P (klass) && member_vec)
1924 : : {
1925 : 2616666236 : val = member_vec_binary_search (member_vec, lookup);
1926 : 2616666236 : if (!val)
1927 : : ;
1928 : 690483245 : else if (TREE_CODE (val) == OVERLOAD
1929 : 690483245 : && 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 : 690483085 : else if (STAT_HACK_P (val))
1973 : 201 : val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
1974 : 690482884 : else if (want_type && !DECL_DECLARES_TYPE_P (val))
1975 : : val = NULL_TREE;
1976 : : }
1977 : : else
1978 : : {
1979 : 2500441272 : if (member_vec && !want_type)
1980 : 682940567 : val = member_vec_linear_search (member_vec, lookup);
1981 : :
1982 : 2500441272 : if (id_equal (lookup, "_") && !want_type)
1983 : 615 : val = name_independent_linear_search (val, klass, lookup);
1984 : 2500440657 : 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 : 2448129876 : if (tree field_val = fields_linear_search (klass, lookup, want_type))
1988 : : {
1989 : 103913190 : 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 : 5117107508 : if (val && conv_op)
1999 : : {
2000 : 15475138 : gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
2001 : 15475138 : val = OVL_CHAIN (val);
2002 : 15475138 : if (tree type = TREE_TYPE (name))
2003 : 404540 : val = extract_conversion_operator (val, type);
2004 : : }
2005 : :
2006 : 5117107508 : 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 : 2823186249 : maybe_lazily_declare (tree klass, tree name)
2014 : : {
2015 : : /* See big comment about module_state::write_pendings regarding adding
2016 : : a check bit. */
2017 : 2823186249 : if (modules_p ())
2018 : 7849792 : lazy_load_pendings (TYPE_NAME (klass));
2019 : :
2020 : : /* Lazily declare functions, if we're going to search these. */
2021 : 2823186249 : if (IDENTIFIER_CTOR_P (name))
2022 : : {
2023 : 59739492 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
2024 : 2290820 : lazily_declare_fn (sfk_constructor, klass);
2025 : 59739492 : if (CLASSTYPE_LAZY_COPY_CTOR (klass))
2026 : 4047111 : lazily_declare_fn (sfk_copy_constructor, klass);
2027 : 59739492 : if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
2028 : 3135732 : lazily_declare_fn (sfk_move_constructor, klass);
2029 : : }
2030 : 2763446757 : else if (IDENTIFIER_DTOR_P (name))
2031 : : {
2032 : 59849789 : if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
2033 : 4768288 : lazily_declare_fn (sfk_destructor, klass);
2034 : : }
2035 : 2703596968 : else if (name == assign_op_identifier)
2036 : : {
2037 : 11558764 : if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
2038 : 1242431 : lazily_declare_fn (sfk_copy_assignment, klass);
2039 : 11558764 : if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
2040 : 821924 : lazily_declare_fn (sfk_move_assignment, klass);
2041 : : }
2042 : 2823186249 : }
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 : 4710876809 : get_class_binding (tree klass, tree name, bool want_type /*=false*/)
2050 : : {
2051 : 4710876809 : klass = complete_type (klass);
2052 : :
2053 : 4710876809 : if (COMPLETE_TYPE_P (klass))
2054 : 2823186249 : maybe_lazily_declare (klass, name);
2055 : :
2056 : 4710876809 : 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 : 286696840 : find_member_slot (tree klass, tree name)
2066 : : {
2067 : 286696840 : bool complete_p = COMPLETE_TYPE_P (klass);
2068 : :
2069 : 286696840 : vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2070 : 286696840 : if (!member_vec)
2071 : : {
2072 : 20183924 : vec_alloc (member_vec, 8);
2073 : 20183924 : CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2074 : 20183924 : 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 : 1951934 : member_vec = set_class_bindings (klass, 6);
2080 : : }
2081 : :
2082 : 286696840 : if (IDENTIFIER_CONV_OP_P (name))
2083 : 1926778 : name = conv_op_identifier;
2084 : :
2085 : 286696840 : unsigned ix, length = member_vec->length ();
2086 : 3502394624 : for (ix = 0; ix < length; ix++)
2087 : : {
2088 : 3357058075 : tree *slot = &(*member_vec)[ix];
2089 : 6714116150 : tree fn_name = OVL_NAME (*slot);
2090 : :
2091 : 3357058075 : 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 : 134246606 : gcc_checking_assert (OVL_P (*slot));
2099 : 134246606 : if (name == conv_op_identifier)
2100 : : {
2101 : 488027 : gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
2102 : : /* Skip the conv-op marker. */
2103 : 488027 : slot = &OVL_CHAIN (*slot);
2104 : : }
2105 : 134246606 : return slot;
2106 : : }
2107 : :
2108 : 3222811469 : if (complete_p && fn_name > name)
2109 : : break;
2110 : : }
2111 : :
2112 : : /* No slot found, add one if the class is complete. */
2113 : 152450234 : if (complete_p)
2114 : : {
2115 : : /* Do exact allocation, as we don't expect to add many. */
2116 : 22383264 : gcc_assert (name != conv_op_identifier);
2117 : 22383264 : vec_safe_reserve_exact (member_vec, 1);
2118 : 22383264 : CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2119 : 22383264 : member_vec->quick_insert (ix, NULL_TREE);
2120 : 22383264 : 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 : 130066952 : add_member_slot (tree klass, tree name)
2131 : : {
2132 : 130066952 : gcc_assert (!COMPLETE_TYPE_P (klass));
2133 : :
2134 : 130066952 : vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2135 : 130066952 : vec_safe_push (member_vec, NULL_TREE);
2136 : 130066952 : CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2137 : :
2138 : 130066952 : tree *slot = &member_vec->last ();
2139 : 130066952 : if (IDENTIFIER_CONV_OP_P (name))
2140 : : {
2141 : : /* Install the marker prefix. */
2142 : 1438751 : *slot = ovl_make (conv_op_marker, NULL_TREE);
2143 : 1438751 : slot = &OVL_CHAIN (*slot);
2144 : : }
2145 : :
2146 : 130066952 : 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 : 5253151495 : member_name_cmp (const void *a_p, const void *b_p)
2156 : : {
2157 : 5253151495 : tree a = *(const tree *)a_p;
2158 : 5253151495 : tree b = *(const tree *)b_p;
2159 : 5253151495 : tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
2160 : 5253151495 : tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
2161 : :
2162 : 5253151495 : gcc_checking_assert (name_a && name_b);
2163 : 5253151495 : if (name_a != name_b)
2164 : 7715455006 : return name_a < name_b ? -1 : +1;
2165 : :
2166 : 16454346 : if (name_a == conv_op_identifier)
2167 : : {
2168 : : /* Strip the conv-op markers. */
2169 : 1109602 : gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
2170 : : && OVL_FUNCTION (b) == conv_op_marker);
2171 : 554801 : a = OVL_CHAIN (a);
2172 : 554801 : b = OVL_CHAIN (b);
2173 : : }
2174 : :
2175 : 16454346 : if (TREE_CODE (a) == OVERLOAD)
2176 : 5811709 : a = OVL_FUNCTION (a);
2177 : 16454346 : if (TREE_CODE (b) == OVERLOAD)
2178 : 5343993 : b = OVL_FUNCTION (b);
2179 : :
2180 : 16454346 : 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 : 16454082 : if (TREE_CODE (a) != TREE_CODE (b))
2201 : : {
2202 : : /* If one of them is a TYPE_DECL, it loses. */
2203 : 15626047 : if (TREE_CODE (a) == TYPE_DECL)
2204 : : return +1;
2205 : 15625714 : else if (TREE_CODE (b) == TYPE_DECL)
2206 : : return -1;
2207 : :
2208 : : /* If one of them is a USING_DECL, it loses. */
2209 : 15625372 : if (TREE_CODE (a) == USING_DECL)
2210 : : return +1;
2211 : 8097205 : 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 : 828035 : if (DECL_UID (a) != DECL_UID (b))
2232 : 828017 : 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 : 7185017 : resort_member_name_cmp (const void *a_p, const void *b_p)
2248 : : {
2249 : 7185017 : tree a = *(const tree *)a_p;
2250 : 7185017 : tree b = *(const tree *)b_p;
2251 : 14370034 : tree name_a = OVL_NAME (a);
2252 : 14370034 : tree name_b = OVL_NAME (b);
2253 : :
2254 : 7185017 : resort_data.new_value (&name_a, &name_a, resort_data.cookie);
2255 : 7185017 : resort_data.new_value (&name_b, &name_b, resort_data.cookie);
2256 : :
2257 : 7185017 : gcc_checking_assert (name_a != name_b);
2258 : :
2259 : 7185017 : return name_a < name_b ? -1 : +1;
2260 : : }
2261 : :
2262 : : /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2263 : :
2264 : : void
2265 : 37461 : resort_type_member_vec (void *obj, void */*orig_obj*/,
2266 : : gt_pointer_operator new_value, void* cookie)
2267 : : {
2268 : 37461 : if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
2269 : : {
2270 : 37461 : resort_data.new_value = new_value;
2271 : 37461 : resort_data.cookie = cookie;
2272 : 37461 : member_vec->qsort (resort_member_name_cmp);
2273 : : }
2274 : 37461 : }
2275 : :
2276 : : /* Recursively count the number of fields in KLASS, including anonymous
2277 : : union members. */
2278 : :
2279 : : static unsigned
2280 : 56314381 : count_class_fields (tree klass)
2281 : : {
2282 : 56314381 : unsigned n_fields = 0;
2283 : :
2284 : 496156057 : for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2285 : 439841676 : if (DECL_DECLARES_FUNCTION_P (fields))
2286 : : /* Functions are dealt with separately. */;
2287 : 201378150 : else if (TREE_CODE (fields) == FIELD_DECL
2288 : 201378150 : && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2289 : 175605 : n_fields += count_class_fields (TREE_TYPE (fields));
2290 : 201202545 : else if (DECL_NAME (fields))
2291 : 181065532 : n_fields += 1;
2292 : :
2293 : 56314381 : 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 : 20799787 : member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
2301 : : {
2302 : 376581029 : for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
2303 : 355781242 : if (DECL_DECLARES_FUNCTION_P (fields))
2304 : : /* Functions are handled separately. */;
2305 : 117317728 : else if (TREE_CODE (fields) == FIELD_DECL
2306 : 117317728 : && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
2307 : 164986 : member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
2308 : 117152742 : else if (DECL_NAME (fields))
2309 : : {
2310 : 112678083 : tree field = fields;
2311 : : /* Mark a conv-op USING_DECL with the conv-op-marker. */
2312 : 112678083 : if (TREE_CODE (field) == USING_DECL
2313 : 112678083 : && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
2314 : 138677 : field = ovl_make (conv_op_marker, field);
2315 : 112678083 : member_vec->quick_push (field);
2316 : : }
2317 : 20799787 : }
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 : 20634822 : member_vec_dedup (vec<tree, va_gc> *member_vec)
2345 : : {
2346 : 20634822 : unsigned len = member_vec->length ();
2347 : 20634822 : unsigned store = 0;
2348 : :
2349 : 20634822 : if (!len)
2350 : : return;
2351 : :
2352 : 41269640 : tree name = OVL_NAME ((*member_vec)[0]);
2353 : 259297411 : 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 : 481407770 : for (jx = ix; jx < len; jx++)
2362 : : {
2363 : 460772950 : tree next = (*member_vec)[jx];
2364 : 460772950 : if (jx != ix)
2365 : : {
2366 : 222110359 : tree next_name = OVL_NAME (next);
2367 : 222110359 : if (next_name != name)
2368 : : {
2369 : : name = next_name;
2370 : : break;
2371 : : }
2372 : : }
2373 : :
2374 : 242745179 : if (IDENTIFIER_CONV_OP_P (name))
2375 : : {
2376 : 1577428 : marker = next;
2377 : 1577428 : next = OVL_CHAIN (next);
2378 : : }
2379 : :
2380 : 242745179 : if (TREE_CODE (next) == USING_DECL)
2381 : : {
2382 : 10434023 : if (IDENTIFIER_CTOR_P (name))
2383 : : /* Dependent inherited ctor. */
2384 : 216317 : continue;
2385 : :
2386 : 10217706 : next = strip_using_decl (next);
2387 : 10217706 : if (TREE_CODE (next) == USING_DECL)
2388 : : {
2389 : 8233742 : to_using = next;
2390 : 8233742 : continue;
2391 : : }
2392 : :
2393 : 1983964 : if (is_overloaded_fn (next))
2394 : 1454902 : continue;
2395 : : }
2396 : :
2397 : 232840218 : if (DECL_DECLARES_TYPE_P (next))
2398 : : {
2399 : 67674475 : to_type = next;
2400 : 67674475 : continue;
2401 : : }
2402 : :
2403 : 165165743 : if (name_independent_decl_p (next))
2404 : 138 : name_independent = jx + 1;
2405 : 165165605 : else if (!current)
2406 : 164805656 : current = next;
2407 : : }
2408 : :
2409 : 238662591 : if (to_using)
2410 : : {
2411 : 8193918 : if (!current)
2412 : : current = to_using;
2413 : : else
2414 : 1876999 : current = ovl_make (to_using, current);
2415 : : }
2416 : :
2417 : 238662591 : if (to_type)
2418 : : {
2419 : 67509357 : if (!current)
2420 : : current = to_type;
2421 : : else
2422 : 159 : current = stat_hack (current, to_type);
2423 : : }
2424 : :
2425 : 238662729 : 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 : 238662591 : if (current)
2437 : : {
2438 : 238631857 : if (marker)
2439 : : {
2440 : 1438751 : OVL_CHAIN (marker) = current;
2441 : 1438751 : current = marker;
2442 : : }
2443 : 238631857 : (*member_vec)[store++] = current;
2444 : : }
2445 : : }
2446 : :
2447 : 24748142 : while (store++ < len)
2448 : 4113322 : 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 : 56138755 : set_class_bindings (tree klass, int extra)
2459 : : {
2460 : 56138755 : unsigned n_fields = count_class_fields (klass);
2461 : 56138755 : vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
2462 : :
2463 : 56138755 : if (member_vec || n_fields >= 8 || extra < 0)
2464 : : {
2465 : : /* Append the new fields. */
2466 : 20634795 : vec_safe_reserve_exact (member_vec, n_fields + (extra >= 0 ? extra : 0));
2467 : 20634795 : member_vec_append_class_fields (member_vec, klass);
2468 : : }
2469 : :
2470 : 56138755 : if (member_vec)
2471 : : {
2472 : 20634795 : CLASSTYPE_MEMBER_VEC (klass) = member_vec;
2473 : 20634795 : member_vec->qsort (member_name_cmp);
2474 : 20634795 : member_vec_dedup (member_vec);
2475 : : }
2476 : :
2477 : 56138755 : 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 : 4468763766 : query_oracle (tree name)
2517 : : {
2518 : 4468763766 : 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 : 1214350367 : cxx_binding_init (cxx_binding *binding, tree value, tree type)
2545 : : {
2546 : 1214350367 : binding->value = value;
2547 : 1214350367 : binding->type = type;
2548 : 1214350367 : binding->previous = NULL;
2549 : : }
2550 : :
2551 : : /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2552 : :
2553 : : static cxx_binding *
2554 : 1214350367 : cxx_binding_make (tree value, tree type)
2555 : : {
2556 : 1214350367 : cxx_binding *binding = free_bindings;
2557 : :
2558 : 1214350367 : if (binding)
2559 : 1205106571 : free_bindings = binding->previous;
2560 : : else
2561 : 9243796 : binding = ggc_alloc<cxx_binding> ();
2562 : :
2563 : : /* Clear flags by default. */
2564 : 1214350367 : LOCAL_BINDING_P (binding) = false;
2565 : 1214350367 : INHERITED_VALUE_BINDING_P (binding) = false;
2566 : 1214350367 : HIDDEN_TYPE_BINDING_P (binding) = false;
2567 : :
2568 : 1214350367 : cxx_binding_init (binding, value, type);
2569 : :
2570 : 1214350367 : return binding;
2571 : : }
2572 : :
2573 : : /* Put BINDING back on the free list. */
2574 : :
2575 : : static inline void
2576 : 1214347502 : cxx_binding_free (cxx_binding *binding)
2577 : : {
2578 : 1214347502 : binding->scope = NULL;
2579 : 1214347502 : binding->previous = free_bindings;
2580 : 1214347502 : free_bindings = binding;
2581 : 803368029 : }
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 : 410982314 : new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2588 : : {
2589 : 410982314 : cp_class_binding cb = {cxx_binding_make (value, type), name};
2590 : 410982314 : cxx_binding *binding = cb.base;
2591 : 410982314 : vec_safe_push (scope->class_shadowed, cb);
2592 : 410982314 : binding->scope = scope;
2593 : 410982314 : 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 : 275090947 : push_binding (tree id, tree decl, cp_binding_level* level)
2601 : : {
2602 : 275090947 : cxx_binding *binding;
2603 : :
2604 : 275090947 : if (level != class_binding_level)
2605 : : {
2606 : 3727388 : binding = cxx_binding_make (decl, NULL_TREE);
2607 : 3727388 : binding->scope = level;
2608 : : }
2609 : : else
2610 : 271363559 : binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2611 : :
2612 : : /* Now, fill in the binding information. */
2613 : 275090947 : binding->previous = IDENTIFIER_BINDING (id);
2614 : 275090947 : LOCAL_BINDING_P (binding) = (level != class_binding_level);
2615 : :
2616 : : /* And put it on the front of the list of bindings for ID. */
2617 : 275090947 : IDENTIFIER_BINDING (id) = binding;
2618 : 275090947 : }
2619 : :
2620 : : /* Remove the binding for DECL which should be the innermost binding
2621 : : for ID. */
2622 : :
2623 : : void
2624 : 827700648 : pop_local_binding (tree id, tree decl)
2625 : : {
2626 : 1631908845 : 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 : 803368350 : cxx_binding *binding = IDENTIFIER_BINDING (id);
2634 : :
2635 : : /* The name should be bound. */
2636 : 803368350 : 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 : 803368350 : if (binding->value == decl)
2643 : 803367786 : binding->value = NULL_TREE;
2644 : 564 : 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 : 417 : gcc_assert (TREE_CODE (binding->value) == TREE_LIST);
2651 : 417 : if (TREE_VALUE (binding->value) != decl)
2652 : : {
2653 : 156 : 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 : 165 : while (TREE_PURPOSE (binding->value) == error_mark_node)
2658 : 9 : binding->value = TREE_CHAIN (binding->value);
2659 : : }
2660 : 417 : gcc_assert (TREE_VALUE (binding->value) == decl);
2661 : 417 : binding->value = TREE_CHAIN (binding->value);
2662 : 417 : while (binding->value
2663 : 504 : && TREE_PURPOSE (binding->value) == error_mark_node)
2664 : 87 : binding->value = TREE_CHAIN (binding->value);
2665 : : }
2666 : :
2667 : 803368350 : 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 : 803368029 : IDENTIFIER_BINDING (id) = binding->previous;
2672 : :
2673 : : /* Add it to the free list. */
2674 : 803368029 : 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 : 187069379 : pop_bindings_and_leave_scope (void)
2683 : : {
2684 : 427370168 : for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2685 : : {
2686 : 240300789 : tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2687 : 480601578 : tree name = OVL_NAME (decl);
2688 : :
2689 : 240300789 : pop_local_binding (name, decl);
2690 : : }
2691 : :
2692 : 187069379 : leave_scope ();
2693 : 187069379 : }
2694 : :
2695 : : /* Strip non dependent using declarations. If DECL is dependent,
2696 : : surreptitiously create a typename_type and return it. */
2697 : :
2698 : : tree
2699 : 7681264609 : strip_using_decl (tree decl)
2700 : : {
2701 : 7681264609 : if (decl == NULL_TREE)
2702 : : return NULL_TREE;
2703 : :
2704 : 5375994546 : while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2705 : 27301437 : decl = USING_DECL_DECLS (decl);
2706 : :
2707 : 24264026 : if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2708 : 5372957135 : && 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 : 398506 : decl = make_typename_type (USING_DECL_SCOPE (decl),
2717 : 398506 : DECL_NAME (decl),
2718 : : typename_type, tf_error);
2719 : 398506 : if (decl != error_mark_node)
2720 : 398506 : 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 : 24037783 : anticipated_builtin_p (tree ovl)
2730 : : {
2731 : 24037783 : return (TREE_CODE (ovl) == OVERLOAD
2732 : 21851599 : && OVL_HIDDEN_P (ovl)
2733 : 30212354 : && 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 : 377668 : supplement_binding (cxx_binding *binding, tree decl)
2756 : : {
2757 : 377668 : auto_cond_timevar tv (TV_NAME_LOOKUP);
2758 : :
2759 : 377668 : tree bval = binding->value;
2760 : 377668 : bool ok = true;
2761 : 377668 : if (bval
2762 : 377668 : && TREE_CODE (bval) == TREE_LIST
2763 : 377674 : && name_independent_decl_p (TREE_VALUE (bval)))
2764 : : bval = TREE_VALUE (bval);
2765 : 377668 : tree target_bval = strip_using_decl (bval);
2766 : 377668 : tree target_decl = strip_using_decl (decl);
2767 : :
2768 : 17362 : if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2769 : 17179 : && target_decl != target_bval
2770 : 394838 : && (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 : 17068 : || (processing_template_decl
2776 : 2468 : && 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 : 377566 : 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 : 377566 : || 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 : 755132 : || anticipated_builtin_p (target_bval))
2797 : 0 : binding->value = decl;
2798 : 377566 : else if (TREE_CODE (target_bval) == TYPE_DECL
2799 : 17440 : && DECL_ARTIFICIAL (target_bval)
2800 : 17407 : && target_decl != target_bval
2801 : 394964 : && (TREE_CODE (target_decl) != TYPE_DECL
2802 : 17218 : || 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 : 17386 : binding->type = bval;
2811 : 17386 : binding->value = decl;
2812 : 17386 : binding->value_is_inherited = false;
2813 : : }
2814 : 360180 : 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 : 360180 : && (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 : 360180 : else if (VAR_P (target_decl)
2845 : 9 : && VAR_P (target_bval)
2846 : 9 : && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2847 : 360186 : && !DECL_CLASS_SCOPE_P (target_decl))
2848 : : {
2849 : 0 : duplicate_decls (decl, binding->value);
2850 : 0 : ok = false;
2851 : : }
2852 : 360180 : 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 : 360180 : && 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 : 360180 : else if (TREE_CODE (bval) == USING_DECL
2865 : 360180 : && CONST_DECL_USING_P (decl))
2866 : : /* Let the clone hide the using-decl that introduced it. */
2867 : 359987 : 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 : 755336 : return ok;
2890 : 377668 : }
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 : 3880529 : matching_fn_p (tree one, tree two)
2949 : : {
2950 : 3880529 : if (TREE_CODE (one) != TREE_CODE (two))
2951 : : return false;
2952 : :
2953 : 2021870 : if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2954 : 2021870 : TYPE_ARG_TYPES (TREE_TYPE (two))))
2955 : : return false;
2956 : :
2957 : 99 : 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 : 72 : 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 : 1153939626 : update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2982 : : tree old, tree decl, bool hiding = false)
2983 : : {
2984 : 1153939626 : tree old_type = NULL_TREE;
2985 : 1153939626 : bool hide_type = false;
2986 : 1153939626 : bool hide_value = false;
2987 : 1153939626 : bool name_independent_p = false;
2988 : :
2989 : 1153939626 : if (!slot)
2990 : : {
2991 : 799641062 : old_type = binding->type;
2992 : 799641062 : hide_type = HIDDEN_TYPE_BINDING_P (binding);
2993 : 799641062 : if (!old_type)
2994 : 799641059 : hide_value = hide_type, hide_type = false;
2995 : 799641062 : name_independent_p = name_independent_decl_p (decl);
2996 : : }
2997 : 354298564 : else if (STAT_HACK_P (*slot))
2998 : : {
2999 : 89 : old_type = STAT_TYPE (*slot);
3000 : 89 : hide_type = STAT_TYPE_HIDDEN_P (*slot);
3001 : 89 : hide_value = STAT_DECL_HIDDEN_P (*slot);
3002 : : }
3003 : :
3004 : 1153939626 : tree to_val = decl;
3005 : 1153939626 : tree to_type = old_type;
3006 : 1153939626 : bool local_overload = false;
3007 : :
3008 : 1153939626 : gcc_assert (!level || level->kind == sk_namespace ? !binding
3009 : : : level->kind != sk_class && !slot);
3010 : :
3011 : 1153939626 : if (old == error_mark_node)
3012 : 95396 : old = NULL_TREE;
3013 : :
3014 : 1153939626 : tree old_bval = old;
3015 : 1153939626 : old = strip_using_decl (old);
3016 : :
3017 : 1153939626 : 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 : 5165935 : 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 : 5165935 : goto done;
3037 : : }
3038 : :
3039 : 1148773691 : if (old && DECL_IMPLICIT_TYPEDEF_P (old))
3040 : : {
3041 : : /* OLD is an implicit typedef. Move it to to_type. */
3042 : 70445 : 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 : 1148773691 : if (DECL_DECLARES_FUNCTION_P (decl))
3051 : : {
3052 : 322993382 : if (!old)
3053 : : ;
3054 : 23420938 : else if (OVL_P (old))
3055 : : {
3056 : 622834061 : for (ovl_iterator iter (old); iter; ++iter)
3057 : : {
3058 : 301857294 : tree fn = *iter;
3059 : :
3060 : 301857294 : 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 : 23420932 : }
3078 : : else
3079 : 0 : goto conflict;
3080 : :
3081 : 322993376 : if (to_type != old_type
3082 : 39501 : && warn_shadow
3083 : 6 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
3084 : 322993382 : && !(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 : 322993376 : local_overload = old && level && level->kind != sk_namespace;
3090 : 322993376 : to_val = ovl_insert (decl, old, -int (hiding));
3091 : : }
3092 : 825780309 : else if (old)
3093 : : {
3094 : 321 : if (name_independent_p)
3095 : 270 : 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 : 825779988 : else if (hiding)
3136 : 25931 : hide_value = true;
3137 : :
3138 : 1153939602 : done:
3139 : 1153939602 : if (to_val)
3140 : : {
3141 : 1153939569 : 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 : 1153939496 : else if (level
3147 : 1153939496 : && !(TREE_CODE (decl) == NAMESPACE_DECL
3148 : 841036 : && !DECL_NAMESPACE_ALIAS (decl)))
3149 : : /* Don't add namespaces here. They're done in
3150 : : push_namespace. */
3151 : 1153204020 : add_decl_to_level (level, decl);
3152 : :
3153 : 1153939569 : if (slot)
3154 : : {
3155 : 354298528 : if (STAT_HACK_P (*slot))
3156 : : {
3157 : 89 : STAT_TYPE (*slot) = to_type;
3158 : 89 : STAT_DECL (*slot) = to_val;
3159 : 89 : STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3160 : 89 : STAT_DECL_HIDDEN_P (*slot) = hide_value;
3161 : : }
3162 : 354298439 : else if (to_type || hide_value)
3163 : : {
3164 : 145895 : *slot = stat_hack (to_val, to_type);
3165 : 145895 : STAT_TYPE_HIDDEN_P (*slot) = hide_type;
3166 : 145895 : STAT_DECL_HIDDEN_P (*slot) = hide_value;
3167 : : }
3168 : : else
3169 : : {
3170 : 354152544 : gcc_checking_assert (!hide_type);
3171 : 354152544 : *slot = to_val;
3172 : : }
3173 : : }
3174 : : else
3175 : : {
3176 : 799641041 : binding->type = to_type;
3177 : 799641041 : binding->value = to_val;
3178 : 799641041 : 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 : 283271220 : check_extern_c_conflict (tree decl)
3195 : : {
3196 : : /* Ignore artificial or system header decls. */
3197 : 283271220 : if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
3198 : 283088264 : return;
3199 : :
3200 : : /* This only applies to decls at namespace scope. */
3201 : 182956 : if (!DECL_NAMESPACE_SCOPE_P (decl))
3202 : : return;
3203 : :
3204 : 182944 : if (!extern_c_decls)
3205 : 39141 : extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
3206 : :
3207 : 182944 : tree *slot = extern_c_decls
3208 : 182944 : ->find_slot_with_hash (DECL_NAME (decl),
3209 : 182944 : IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
3210 : 182944 : if (tree old = *slot)
3211 : : {
3212 : 390 : if (TREE_CODE (old) == OVERLOAD)
3213 : 18 : old = OVL_FUNCTION (old);
3214 : :
3215 : 390 : int mismatch = 0;
3216 : 390 : 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 : 372 : else if (!decls_match (decl, old))
3220 : : mismatch = 1;
3221 : 351 : else if (TREE_CODE (decl) == FUNCTION_DECL
3222 : 690 : && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
3223 : 339 : TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3224 : : ce_normal))
3225 : : mismatch = -1;
3226 : 348 : 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 : 366 : 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 : 360 : *slot = ovl_make (old, old);
3250 : :
3251 : 366 : slot = &OVL_CHAIN (*slot);
3252 : :
3253 : : /* Chain it on for c_linkage_binding's use. */
3254 : 366 : *slot = tree_cons (NULL_TREE, decl, *slot);
3255 : : }
3256 : : }
3257 : : else
3258 : 182554 : *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 : 167 : inform_shadowed (tree shadowed)
3284 : : {
3285 : 167 : inform (DECL_SOURCE_LOCATION (shadowed),
3286 : : "shadowed declaration is here");
3287 : 167 : }
3288 : :
3289 : : /* DECL is being declared at a local scope. Emit suitable shadow
3290 : : warnings. */
3291 : :
3292 : : static tree
3293 : 799641062 : 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 : 799641062 : if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
3298 : : return NULL_TREE;
3299 : :
3300 : 581882536 : if (DECL_FUNCTION_SCOPE_P (decl))
3301 : : {
3302 : 372371400 : tree ctx = DECL_CONTEXT (decl);
3303 : 744742800 : if (DECL_CLONED_FUNCTION_P (ctx)
3304 : 349799039 : || DECL_TEMPLATE_INSTANTIATED (ctx)
3305 : 297814709 : || (DECL_LANG_SPECIFIC (ctx)
3306 : 297814709 : && DECL_DEFAULTED_FN (ctx))
3307 : 688652261 : || (LAMBDA_FUNCTION_P (ctx)
3308 : 3365883 : && 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 : 505380851 : tree old = NULL_TREE;
3318 : 505380851 : cp_binding_level *old_scope = NULL;
3319 : 505380851 : if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
3320 : : {
3321 : 1510101 : old = binding->value;
3322 : 1510101 : old_scope = binding->scope;
3323 : : }
3324 : :
3325 : 1510101 : if (old
3326 : 1510101 : && (TREE_CODE (old) == PARM_DECL
3327 : 653769 : || VAR_P (old)
3328 : 154338 : || (TREE_CODE (old) == TYPE_DECL
3329 : 100292 : && (!DECL_ARTIFICIAL (old)
3330 : 58706 : || TREE_CODE (decl) == TYPE_DECL)))
3331 : 1455966 : && DECL_FUNCTION_SCOPE_P (old)
3332 : 2876916 : && (!DECL_ARTIFICIAL (decl)
3333 : 1151133 : || is_capture_proxy (decl)
3334 : 304997 : || DECL_IMPLICIT_TYPEDEF_P (decl)
3335 : 304967 : || (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 : 1061854 : if (is_capture_proxy (decl))
3342 : : {
3343 : 846136 : if (current_lambda_expr ()
3344 : 846136 : && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
3345 : 39 : && TREE_CODE (old) == PARM_DECL
3346 : 846157 : && DECL_NAME (decl) != this_identifier)
3347 : 21 : error_at (DECL_SOURCE_LOCATION (old),
3348 : : "lambda parameter %qD "
3349 : : "previously declared as a capture", old);
3350 : 846136 : return NULL_TREE;
3351 : : }
3352 : : /* Don't complain if it's from an enclosing function. */
3353 : 215718 : else if (DECL_CONTEXT (old) == current_function_decl
3354 : 175105 : && TREE_CODE (decl) != PARM_DECL
3355 : 390823 : && TREE_CODE (old) == PARM_DECL)
3356 : : {
3357 : : /* Go to where the parms should be and see if we find
3358 : : them there. */
3359 : 10381 : cp_binding_level *b = current_binding_level->level_chain;
3360 : :
3361 : 10381 : if (in_function_try_handler && b->kind == sk_catch)
3362 : 39 : b = b->level_chain;
3363 : :
3364 : : /* Skip artificially added scopes which aren't present
3365 : : in the C++ standard, e.g. for function-try-block or
3366 : : ctor/dtor cleanups. */
3367 : 10462 : while (b->artificial)
3368 : 81 : b = b->level_chain;
3369 : :
3370 : : /* [basic.scope.param] A parameter name shall not be redeclared
3371 : : in the outermost block of the function definition. */
3372 : 10381 : if (b->kind == sk_function_parms)
3373 : : {
3374 : 114 : if (name_independent_decl_p (decl))
3375 : : return old;
3376 : :
3377 : 96 : auto_diagnostic_group d;
3378 : 96 : bool emit = true;
3379 : 96 : if (DECL_EXTERNAL (decl))
3380 : 40 : emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3381 : : "declaration of %q#D shadows a parameter",
3382 : : decl);
3383 : : else
3384 : 56 : error_at (DECL_SOURCE_LOCATION (decl),
3385 : : "declaration of %q#D shadows a parameter", decl);
3386 : 96 : if (emit)
3387 : 96 : inform (DECL_SOURCE_LOCATION (old),
3388 : : "%q#D previously declared here", old);
3389 : 96 : return NULL_TREE;
3390 : 96 : }
3391 : : }
3392 : :
3393 : : /* The local structure or class can't use parameters of
3394 : : the containing function anyway. */
3395 : 215604 : if (DECL_CONTEXT (old) != current_function_decl)
3396 : : {
3397 : 40613 : for (cp_binding_level *scope = current_binding_level;
3398 : 99697 : scope != old_scope; scope = scope->level_chain)
3399 : 97539 : if (scope->kind == sk_class
3400 : 140388 : && !LAMBDA_TYPE_P (scope->this_entity))
3401 : : return NULL_TREE;
3402 : : }
3403 : : /* Error if redeclaring a local declared in a
3404 : : init-statement or in the condition of an if or
3405 : : switch statement when the new declaration is in the
3406 : : outermost block of the controlled statement.
3407 : : Redeclaring a variable from a for or while condition is
3408 : : detected elsewhere. */
3409 : 174991 : else if (VAR_P (old)
3410 : 153647 : && old_scope == current_binding_level->level_chain
3411 : 1897 : && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
3412 : : {
3413 : 193 : if (name_independent_decl_p (decl))
3414 : : return old;
3415 : :
3416 : 118 : auto_diagnostic_group d;
3417 : 118 : bool emit = true;
3418 : 118 : if (DECL_EXTERNAL (decl))
3419 : 48 : emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3420 : : "redeclaration of %q#D", decl);
3421 : : else
3422 : 70 : error_at (DECL_SOURCE_LOCATION (decl),
3423 : : "redeclaration of %q#D", decl);
3424 : 118 : if (emit)
3425 : 118 : inform (DECL_SOURCE_LOCATION (old),
3426 : : "%q#D previously declared here", old);
3427 : 118 : return NULL_TREE;
3428 : 118 : }
3429 : : /* C++11:
3430 : : 3.3.3/3: The name declared in an exception-declaration (...)
3431 : : shall not be redeclared in the outermost block of the handler.
3432 : : 3.3.3/2: A parameter name shall not be redeclared (...) in
3433 : : the outermost block of any handler associated with a
3434 : : function-try-block. */
3435 : 174798 : else if (TREE_CODE (old) == VAR_DECL
3436 : 153454 : && old_scope == current_binding_level->level_chain
3437 : 1704 : && old_scope->kind == sk_catch)
3438 : : {
3439 : 18 : if (name_independent_decl_p (decl))
3440 : : return old;
3441 : :
3442 : 15 : auto_diagnostic_group d;
3443 : 15 : bool emit;
3444 : 15 : if (DECL_EXTERNAL (decl))
3445 : 6 : emit = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
3446 : : "redeclaration of %q#D", decl);
3447 : : else
3448 : 9 : emit = permerror (DECL_SOURCE_LOCATION (decl),
3449 : : "redeclaration of %q#D", decl);
3450 : 15 : if (emit)
3451 : 15 : inform (DECL_SOURCE_LOCATION (old),
3452 : : "%q#D previously declared here", old);
3453 : 15 : return NULL_TREE;
3454 : 15 : }
3455 : :
3456 : : /* Don't emit -Wshadow* warnings for name-independent decls. */
3457 : 176938 : if (name_independent_decl_p (decl) || name_independent_decl_p (old))
3458 : : return NULL_TREE;
3459 : :
3460 : : /* If '-Wshadow=compatible-local' is specified without other
3461 : : -Wshadow= flags, we will warn only when the type of the
3462 : : shadowing variable (DECL) can be converted to that of the
3463 : : shadowed parameter (OLD_LOCAL). The reason why we only check
3464 : : if DECL's type can be converted to OLD_LOCAL's type (but not the
3465 : : other way around) is because when users accidentally shadow a
3466 : : parameter, more than often they would use the variable
3467 : : thinking (mistakenly) it's still the parameter. It would be
3468 : : rare that users would use the variable in the place that
3469 : : expects the parameter but thinking it's a new decl.
3470 : : If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3471 : : warns regardless of whether one of the types involved
3472 : : is a subclass of the other, since that is never okay. */
3473 : :
3474 : 176824 : enum opt_code warning_code;
3475 : 176824 : if (warn_shadow)
3476 : : warning_code = OPT_Wshadow;
3477 : 176762 : else if ((TREE_CODE (decl) == TYPE_DECL)
3478 : 176762 : ^ (TREE_CODE (old) == TYPE_DECL))
3479 : : /* If exactly one is a type, they aren't compatible. */
3480 : : warning_code = OPT_Wshadow_local;
3481 : 176741 : else if ((TREE_TYPE (old)
3482 : 176741 : && TREE_TYPE (decl)
3483 : 176738 : && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3484 : 42279 : || TREE_CODE (decl) == TYPE_DECL
3485 : 31703 : || TREE_CODE (old) == TYPE_DECL
3486 : 208444 : || (!dependent_type_p (TREE_TYPE (decl))
3487 : 11977 : && !dependent_type_p (TREE_TYPE (old))
3488 : : /* If the new decl uses auto, we don't yet know
3489 : : its type (the old type cannot be using auto
3490 : : at this point, without also being
3491 : : dependent). This is an indication we're
3492 : : (now) doing the shadow checking too
3493 : : early. */
3494 : 11962 : && !type_uses_auto (TREE_TYPE (decl))
3495 : 11702 : && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
3496 : : decl, LOOKUP_IMPLICIT, tf_none)))
3497 : : warning_code = OPT_Wshadow_compatible_local;
3498 : : else
3499 : : warning_code = OPT_Wshadow_local;
3500 : :
3501 : 176824 : const char *msg;
3502 : 176824 : if (TREE_CODE (old) == PARM_DECL)
3503 : : msg = "declaration of %q#D shadows a parameter";
3504 : 164713 : else if (is_capture_proxy (old))
3505 : : msg = "declaration of %qD shadows a lambda capture";
3506 : : else
3507 : 164565 : msg = "declaration of %qD shadows a previous local";
3508 : :
3509 : 176824 : auto_diagnostic_group d;
3510 : 176824 : if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
3511 : 110 : inform_shadowed (old);
3512 : 176824 : return NULL_TREE;
3513 : 176824 : }
3514 : :
3515 : 504318997 : if (!warn_shadow)
3516 : : return NULL_TREE;
3517 : :
3518 : : /* Don't emit -Wshadow for name-independent decls. */
3519 : 841 : if (name_independent_decl_p (decl))
3520 : : return NULL_TREE;
3521 : :
3522 : : /* Don't warn for artificial things that are not implicit typedefs. */
3523 : 697 : if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
3524 : : return NULL_TREE;
3525 : :
3526 : 383 : if (nonlambda_method_basetype ())
3527 : 93 : if (tree member = lookup_member (current_nonlambda_class_type (),
3528 : 93 : DECL_NAME (decl), /*protect=*/0,
3529 : : /*want_type=*/false, tf_warning_or_error))
3530 : : {
3531 : 33 : member = MAYBE_BASELINK_FUNCTIONS (member);
3532 : :
3533 : : /* Warn if a variable shadows a non-function, or the variable
3534 : : is a function or a pointer-to-function. */
3535 : 24 : if ((!OVL_P (member)
3536 : 9 : || TREE_CODE (decl) == FUNCTION_DECL
3537 : 9 : || (TREE_TYPE (decl)
3538 : 6 : && (TYPE_PTRFN_P (TREE_TYPE (decl))
3539 : 6 : || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
3540 : 60 : && !warning_suppressed_p (decl, OPT_Wshadow))
3541 : : {
3542 : 27 : auto_diagnostic_group d;
3543 : 27 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3544 : : "declaration of %qD shadows a member of %qT",
3545 : : decl, current_nonlambda_class_type ())
3546 : 27 : && DECL_P (member))
3547 : : {
3548 : 27 : inform_shadowed (member);
3549 : 27 : suppress_warning (decl, OPT_Wshadow);
3550 : : }
3551 : 27 : }
3552 : 33 : return NULL_TREE;
3553 : : }
3554 : :
3555 : : /* Now look for a namespace shadow. */
3556 : 350 : old = find_namespace_value (current_namespace, DECL_NAME (decl));
3557 : 350 : if (old
3558 : 75 : && (VAR_P (old)
3559 : 51 : || (TREE_CODE (old) == TYPE_DECL
3560 : 15 : && (!DECL_ARTIFICIAL (old)
3561 : 9 : || TREE_CODE (decl) == TYPE_DECL)))
3562 : 33 : && !DECL_EXTERNAL (decl)
3563 : 30 : && !instantiating_current_function_p ()
3564 : 380 : && !warning_suppressed_p (decl, OPT_Wshadow))
3565 : : /* XXX shadow warnings in outer-more namespaces */
3566 : : {
3567 : 30 : auto_diagnostic_group d;
3568 : 30 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
3569 : : "declaration of %qD shadows a global declaration",
3570 : : decl))
3571 : : {
3572 : 30 : inform_shadowed (old);
3573 : 30 : suppress_warning (decl, OPT_Wshadow);
3574 : : }
3575 : 30 : return NULL_TREE;
3576 : 30 : }
3577 : :
3578 : : return NULL_TREE;
3579 : : }
3580 : :
3581 : : /* DECL is being pushed inside function CTX. Set its context, if
3582 : : needed. */
3583 : :
3584 : : static void
3585 : 363247279 : set_decl_context_in_fn (tree ctx, tree decl)
3586 : : {
3587 : 363247279 : if (TREE_CODE (decl) == FUNCTION_DECL
3588 : 363247279 : || (VAR_P (decl) && DECL_EXTERNAL (decl)))
3589 : : /* Make sure local externs are marked as such. OMP UDRs really
3590 : : are nested functions. */
3591 : 37653 : gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
3592 : : && (DECL_NAMESPACE_SCOPE_P (decl)
3593 : : || (TREE_CODE (decl) == FUNCTION_DECL
3594 : : && DECL_OMP_DECLARE_REDUCTION_P (decl))));
3595 : :
3596 : 363247279 : if (!DECL_CONTEXT (decl)
3597 : : /* When parsing the parameter list of a function declarator,
3598 : : don't set DECL_CONTEXT to an enclosing function. */
3599 : 364171422 : && !(TREE_CODE (decl) == PARM_DECL
3600 : 924143 : && parsing_function_declarator ()))
3601 : 83377302 : DECL_CONTEXT (decl) = ctx;
3602 : 363247279 : }
3603 : :
3604 : : /* DECL is a local extern decl. Find or create the namespace-scope
3605 : : decl that it aliases. Also, determines the linkage of DECL. */
3606 : :
3607 : : void
3608 : 37345 : push_local_extern_decl_alias (tree decl)
3609 : : {
3610 : 37345 : if (dependent_type_p (TREE_TYPE (decl))
3611 : 37345 : || (processing_template_decl
3612 : 16415 : && VAR_P (decl)
3613 : 50 : && CP_DECL_THREAD_LOCAL_P (decl)))
3614 : : return;
3615 : : /* EH specs were not part of the function type prior to c++17, but
3616 : : we still can't go pushing dependent eh specs into the namespace. */
3617 : 37262 : if (cxx_dialect < cxx17
3618 : 2328 : && TREE_CODE (decl) == FUNCTION_DECL
3619 : 39212 : && (value_dependent_expression_p
3620 : 1950 : (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))))
3621 : : return;
3622 : :
3623 : 37261 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
3624 : : || !DECL_TEMPLATE_INFO (decl));
3625 : 37261 : if (DECL_LANG_SPECIFIC (decl) && DECL_LOCAL_DECL_ALIAS (decl))
3626 : : /* We're instantiating a non-dependent local decl, it already
3627 : : knows the alias. */
3628 : : return;
3629 : :
3630 : 36947 : tree alias = NULL_TREE;
3631 : :
3632 : 36947 : if (DECL_SIZE (decl) && !TREE_CONSTANT (DECL_SIZE (decl)))
3633 : : /* Do not let a VLA creep into a namespace. Diagnostic will be
3634 : : emitted in layout_var_decl later. */
3635 : 3 : alias = error_mark_node;
3636 : : else
3637 : : {
3638 : : /* First look for a decl that matches. */
3639 : 36944 : tree ns = CP_DECL_CONTEXT (decl);
3640 : 36944 : tree binding = find_namespace_value (ns, DECL_NAME (decl));
3641 : :
3642 : 36944 : if (binding && TREE_CODE (binding) != TREE_LIST)
3643 : 888 : for (ovl_iterator iter (binding); iter; ++iter)
3644 : 620 : if (decls_match (decl, *iter, /*record_versions*/false))
3645 : : {
3646 : 427 : alias = *iter;
3647 : 427 : if (!validate_constexpr_redeclaration (alias, decl))
3648 : 12 : return;
3649 : : break;
3650 : : }
3651 : :
3652 : 520 : if (!alias)
3653 : : {
3654 : : /* No existing namespace-scope decl. Make one. */
3655 : 36517 : alias = copy_decl (decl);
3656 : 36517 : if (TREE_CODE (alias) == FUNCTION_DECL)
3657 : : {
3658 : : /* Recontextualize the parms. */
3659 : 35686 : for (tree *chain = &DECL_ARGUMENTS (alias);
3660 : 40735 : *chain; chain = &DECL_CHAIN (*chain))
3661 : : {
3662 : 5049 : *chain = copy_decl (*chain);
3663 : 5049 : DECL_CONTEXT (*chain) = alias;
3664 : : }
3665 : :
3666 : 35686 : tree type = TREE_TYPE (alias);
3667 : 35686 : for (tree args = TYPE_ARG_TYPES (type);
3668 : 81157 : args; args = TREE_CHAIN (args))
3669 : 45510 : if (TREE_PURPOSE (args))
3670 : : {
3671 : : /* There are default args. Lose them. */
3672 : 39 : tree nargs = NULL_TREE;
3673 : 39 : tree *chain = &nargs;
3674 : 39 : for (args = TYPE_ARG_TYPES (type);
3675 : 99 : args; args = TREE_CHAIN (args))
3676 : 99 : if (args == void_list_node)
3677 : : {
3678 : 39 : *chain = args;
3679 : 39 : break;
3680 : : }
3681 : : else
3682 : : {
3683 : 60 : *chain
3684 : 60 : = build_tree_list (NULL_TREE, TREE_VALUE (args));
3685 : 60 : chain = &TREE_CHAIN (*chain);
3686 : : }
3687 : :
3688 : 39 : tree fn_type = build_function_type (TREE_TYPE (type), nargs);
3689 : :
3690 : 39 : fn_type = apply_memfn_quals
3691 : 39 : (fn_type, type_memfn_quals (type));
3692 : :
3693 : 39 : fn_type = build_cp_fntype_variant
3694 : 39 : (fn_type, type_memfn_rqual (type),
3695 : 39 : TYPE_RAISES_EXCEPTIONS (type),
3696 : 39 : TYPE_HAS_LATE_RETURN_TYPE (type));
3697 : :
3698 : 39 : TREE_TYPE (alias) = fn_type;
3699 : 39 : break;
3700 : : }
3701 : : }
3702 : :
3703 : : /* This is the real thing. */
3704 : 36517 : DECL_LOCAL_DECL_P (alias) = false;
3705 : :
3706 : : /* Expected default linkage is from the namespace. */
3707 : 36517 : TREE_PUBLIC (alias) = TREE_PUBLIC (ns);
3708 : 36517 : push_nested_namespace (ns);
3709 : 36517 : alias = pushdecl (alias, /* hiding= */true);
3710 : 36517 : pop_nested_namespace (ns);
3711 : 36517 : if (VAR_P (decl)
3712 : 831 : && CP_DECL_THREAD_LOCAL_P (decl)
3713 : 36538 : && alias != error_mark_node)
3714 : 18 : set_decl_tls_model (alias, DECL_TLS_MODEL (decl));
3715 : :
3716 : : /* Adjust visibility. */
3717 : 36517 : determine_visibility (alias);
3718 : : }
3719 : : }
3720 : :
3721 : 36935 : retrofit_lang_decl (decl);
3722 : 36935 : DECL_LOCAL_DECL_ALIAS (decl) = alias;
3723 : : }
3724 : :
3725 : : /* If DECL has non-internal linkage, and we have a module vector,
3726 : : record it in the appropriate slot. We have already checked for
3727 : : duplicates. */
3728 : :
3729 : : static void
3730 : 196860 : maybe_record_mergeable_decl (tree *slot, tree name, tree decl)
3731 : : {
3732 : 196860 : if (TREE_CODE (*slot) != BINDING_VECTOR)
3733 : : return;
3734 : :
3735 : 16 : if (decl_linkage (decl) == lk_internal)
3736 : : return;
3737 : :
3738 : 16 : tree not_tmpl = STRIP_TEMPLATE (decl);
3739 : 16 : bool is_attached = (DECL_LANG_SPECIFIC (not_tmpl)
3740 : 32 : && DECL_MODULE_ATTACH_P (not_tmpl));
3741 : : tree *gslot = get_fixed_binding_slot
3742 : 16 : (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
3743 : : true);
3744 : :
3745 : 16 : if (!is_attached)
3746 : : {
3747 : 13 : binding_slot &orig
3748 : 13 : = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
3749 : :
3750 : 13 : if (!STAT_HACK_P (tree (orig)))
3751 : 5 : orig = stat_hack (tree (orig));
3752 : :
3753 : 13 : MODULE_BINDING_GLOBAL_P (tree (orig)) = true;
3754 : : }
3755 : :
3756 : 16 : add_mergeable_namespace_entity (gslot, decl);
3757 : : }
3758 : :
3759 : : /* DECL is being pushed. Check whether it hides or ambiguates
3760 : : something seen as an import. This include decls seen in our own
3761 : : interface, which is OK. Also, check for merging a
3762 : : global/partition decl. */
3763 : :
3764 : : static tree
3765 : 507 : check_module_override (tree decl, tree mvec, bool hiding,
3766 : : tree scope, tree name)
3767 : : {
3768 : 507 : tree match = NULL_TREE;
3769 : 507 : bitmap imports = get_import_bitmap ();
3770 : 507 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (mvec);
3771 : 507 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (mvec);
3772 : :
3773 : 507 : tree nontmpl = STRIP_TEMPLATE (decl);
3774 : 951 : bool attached = DECL_LANG_SPECIFIC (nontmpl) && DECL_MODULE_ATTACH_P (nontmpl);
3775 : :
3776 : : /* For deduction guides we don't do normal name lookup, but rather consider
3777 : : any reachable declaration, so we should check for overriding here too. */
3778 : 507 : bool any_reachable = deduction_guide_p (decl);
3779 : :
3780 : : /* DECL might have an originating module if it's an instantiation of a
3781 : : friend; we want to look at all reachable decls in that module. */
3782 : 507 : unsigned decl_mod = get_originating_module (decl);
3783 : :
3784 : 507 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
3785 : : {
3786 : 507 : cluster++;
3787 : 507 : ix--;
3788 : : }
3789 : :
3790 : 762 : for (; ix--; cluster++)
3791 : 1293 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
3792 : : {
3793 : : /* Are we importing this module? */
3794 : 1038 : if (cluster->indices[jx].span != 1)
3795 : 40 : continue;
3796 : 998 : unsigned cluster_mod = cluster->indices[jx].base;
3797 : 998 : if (!cluster_mod)
3798 : 507 : continue;
3799 : 491 : bool c_any_reachable = (any_reachable || cluster_mod == decl_mod);
3800 : 491 : if (!c_any_reachable && !bitmap_bit_p (imports, cluster_mod))
3801 : 9 : continue;
3802 : : /* Is it loaded? */
3803 : 482 : if (cluster->slots[jx].is_lazy ())
3804 : 0 : lazy_load_binding (cluster_mod, scope, name, &cluster->slots[jx]);
3805 : 482 : tree bind = cluster->slots[jx];
3806 : 482 : if (!bind)
3807 : : /* Errors could cause there to be nothing. */
3808 : 0 : continue;
3809 : :
3810 : 482 : tree type = NULL_TREE;
3811 : 482 : if (STAT_HACK_P (bind))
3812 : : {
3813 : : /* If there was a matching STAT_TYPE here then xref_tag
3814 : : should have found it, but we need to check anyway because
3815 : : a conflicting using-declaration may exist. */
3816 : 467 : if (c_any_reachable)
3817 : : {
3818 : 110 : type = STAT_TYPE (bind);
3819 : 110 : bind = STAT_DECL (bind);
3820 : : }
3821 : : else
3822 : : {
3823 : 357 : if (STAT_TYPE_VISIBLE_P (bind))
3824 : 105 : type = STAT_TYPE (bind);
3825 : 357 : bind = STAT_VISIBLE (bind);
3826 : : }
3827 : : }
3828 : :
3829 : 467 : if (type)
3830 : : {
3831 : 3 : match = duplicate_decls (decl, strip_using_decl (type), hiding);
3832 : 3 : if (match)
3833 : 0 : goto matched;
3834 : : }
3835 : :
3836 : 879 : for (ovl_iterator iter (strip_using_decl (bind)); iter; ++iter)
3837 : : {
3838 : 479 : match = duplicate_decls (decl, *iter, hiding);
3839 : 479 : if (match)
3840 : 267 : goto matched;
3841 : : }
3842 : : }
3843 : :
3844 : 240 : if (TREE_PUBLIC (scope) && TREE_PUBLIC (STRIP_TEMPLATE (decl))
3845 : : /* Namespaces are dealt with specially in
3846 : : make_namespace_finish. */
3847 : 462 : && !(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
3848 : : {
3849 : : /* Look in the appropriate mergeable decl slot. */
3850 : 210 : tree mergeable = NULL_TREE;
3851 : 210 : if (attached)
3852 : 36 : mergeable = BINDING_VECTOR_CLUSTER (mvec, BINDING_SLOT_PARTITION
3853 : : / BINDING_VECTOR_SLOTS_PER_CLUSTER)
3854 : 18 : .slots[BINDING_SLOT_PARTITION % BINDING_VECTOR_SLOTS_PER_CLUSTER];
3855 : : else
3856 : 192 : mergeable = BINDING_VECTOR_CLUSTER (mvec, 0).slots[BINDING_SLOT_GLOBAL];
3857 : :
3858 : 979 : for (ovl_iterator iter (mergeable); iter; ++iter)
3859 : : {
3860 : 471 : match = duplicate_decls (decl, *iter, hiding);
3861 : 471 : if (match)
3862 : 52 : goto matched;
3863 : : }
3864 : : }
3865 : :
3866 : : return NULL_TREE;
3867 : :
3868 : 319 : matched:
3869 : 319 : if (match != error_mark_node)
3870 : : {
3871 : 277 : if (attached)
3872 : 120 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
3873 : : else
3874 : 157 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
3875 : : }
3876 : :
3877 : : return match;
3878 : : }
3879 : :
3880 : : /* Record DECL as belonging to the current lexical scope. Check for
3881 : : errors (such as an incompatible declaration for the same name
3882 : : already seen in the same scope).
3883 : :
3884 : : The new binding is hidden if HIDING is true (an anticipated builtin
3885 : : or hidden friend).
3886 : :
3887 : : Returns either DECL or an old decl for the same name. If an old
3888 : : decl is returned, it may have been smashed to agree with what DECL
3889 : : says. */
3890 : :
3891 : : tree
3892 : 1188735042 : pushdecl (tree decl, bool hiding)
3893 : : {
3894 : 1188735042 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3895 : :
3896 : 1188735042 : if (decl == error_mark_node)
3897 : : return error_mark_node;
3898 : :
3899 : 1188735036 : if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
3900 : 363247279 : set_decl_context_in_fn (current_function_decl, decl);
3901 : :
3902 : : /* The binding level we will be pushing into. During local class
3903 : : pushing, we want to push to the containing scope. */
3904 : 1188735036 : cp_binding_level *level = current_binding_level;
3905 : 1188735169 : while (level->kind == sk_class
3906 : 1188735169 : || level->kind == sk_cleanup)
3907 : 133 : level = level->level_chain;
3908 : :
3909 : : /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3910 : : insert it. Other NULL-named decls, not so much. */
3911 : 1188735036 : tree name = DECL_NAME (decl);
3912 : 2353212810 : if (name ? !IDENTIFIER_ANON_P (name) : TREE_CODE (decl) == NAMESPACE_DECL)
3913 : : {
3914 : 1162897132 : cxx_binding *binding = NULL; /* Local scope binding. */
3915 : 1162897132 : tree ns = NULL_TREE; /* Searched namespace. */
3916 : 1162897132 : tree *slot = NULL; /* Binding slot in namespace. */
3917 : 1162897132 : tree *mslot = NULL; /* Current module slot in namespace. */
3918 : 1162897132 : tree old = NULL_TREE;
3919 : 1162897132 : bool name_independent_p = false;
3920 : 1162897132 : bool name_independent_diagnosed_p = false;
3921 : :
3922 : 1162897132 : if (level->kind == sk_namespace)
3923 : : {
3924 : : /* We look in the decl's namespace for an existing
3925 : : declaration, even though we push into the current
3926 : : namespace. */
3927 : 987490398 : ns = (DECL_NAMESPACE_SCOPE_P (decl)
3928 : 726507366 : ? CP_DECL_CONTEXT (decl) : current_namespace);
3929 : : /* Create the binding, if this is current namespace, because
3930 : : that's where we'll be pushing anyway. */
3931 : 363253683 : slot = find_namespace_slot (ns, name, ns == current_namespace);
3932 : 363253683 : if (slot)
3933 : : {
3934 : 726507300 : mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT,
3935 : 363253650 : ns == current_namespace);
3936 : 363253650 : old = MAYBE_STAT_DECL (*mslot);
3937 : : }
3938 : : }
3939 : : else
3940 : : {
3941 : 799643449 : binding = find_local_binding (level, name);
3942 : 799643449 : if (binding)
3943 : 2778 : old = binding->value;
3944 : 799643449 : name_independent_p = name_independent_decl_p (decl);
3945 : : }
3946 : :
3947 : 1162897132 : if (old == error_mark_node)
3948 : 95396 : old = NULL_TREE;
3949 : :
3950 : 1162897132 : tree oldi, oldn;
3951 : 1186515400 : for (oldi = old; oldi; oldi = oldn)
3952 : : {
3953 : 32575723 : if (TREE_CODE (oldi) == TREE_LIST)
3954 : : {
3955 : 120 : gcc_checking_assert (level->kind != sk_namespace
3956 : : && name_independent_decl_p
3957 : : (TREE_VALUE (old)));
3958 : 60 : oldn = TREE_CHAIN (oldi);
3959 : 60 : oldi = TREE_VALUE (oldi);
3960 : : }
3961 : : else
3962 : : oldn = NULL_TREE;
3963 : 657987479 : for (ovl_iterator iter (oldi); iter; ++iter)
3964 : 323850619 : if (iter.using_p ())
3965 : : ; /* Ignore using decls here. */
3966 : 320328984 : else if (iter.hidden_p ()
3967 : 63792613 : && TREE_CODE (*iter) == FUNCTION_DECL
3968 : 43872317 : && DECL_LANG_SPECIFIC (*iter)
3969 : 364201301 : && DECL_MODULE_IMPORT_P (*iter))
3970 : : ; /* An undeclared builtin imported from elsewhere. */
3971 : 320328981 : else if (name_independent_p)
3972 : : {
3973 : : /* Ignore name-independent declarations. */
3974 : 237 : if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
3975 : 160 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
3976 : : "name-independent declarations only available with "
3977 : : "%<-std=c++2c%> or %<-std=gnu++2c%>");
3978 : : name_independent_diagnosed_p = true;
3979 : : }
3980 : 640657488 : else if (tree match
3981 : 320328744 : = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
3982 : : {
3983 : 8957455 : if (match == error_mark_node)
3984 : : ;
3985 : 8956545 : else if (TREE_CODE (match) == TYPE_DECL)
3986 : 24677 : gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name)
3987 : : == (level->kind == sk_namespace
3988 : : ? NULL_TREE : TREE_TYPE (match)));
3989 : 8931868 : else if (iter.hidden_p () && !hiding)
3990 : : {
3991 : : /* Unhiding a previously hidden decl. */
3992 : 4011872 : tree head = iter.reveal_node (oldi);
3993 : 4011872 : if (head != oldi)
3994 : : {
3995 : 19435 : gcc_checking_assert (ns);
3996 : 19435 : if (STAT_HACK_P (*slot))
3997 : 0 : STAT_DECL (*slot) = head;
3998 : : else
3999 : 19435 : *slot = head;
4000 : : }
4001 : 4011872 : if (DECL_EXTERN_C_P (match))
4002 : : /* We need to check and register the decl now. */
4003 : 3770076 : check_extern_c_conflict (match);
4004 : : }
4005 : 4919996 : else if (slot
4006 : 4919996 : && !hiding
4007 : 4102530 : && STAT_HACK_P (*slot)
4008 : 4920021 : && STAT_DECL_HIDDEN_P (*slot))
4009 : : {
4010 : : /* Unhide the non-function. */
4011 : 25 : gcc_checking_assert (oldi == match);
4012 : 25 : if (!STAT_TYPE (*slot))
4013 : 25 : *slot = match;
4014 : : else
4015 : 0 : STAT_DECL (*slot) = match;
4016 : : }
4017 : 8957455 : return match;
4018 : : }
4019 : : }
4020 : :
4021 : : /* Check for redeclaring an import. */
4022 : 1153939677 : if (slot && *slot && TREE_CODE (*slot) == BINDING_VECTOR)
4023 : 1014 : if (tree match
4024 : 507 : = check_module_override (decl, *slot, hiding, ns, name))
4025 : : {
4026 : 319 : if (match == error_mark_node)
4027 : : return match;
4028 : :
4029 : : /* We found a decl in an interface, push it into this
4030 : : binding. */
4031 : 277 : decl = update_binding (NULL, binding, mslot, old,
4032 : : match, hiding);
4033 : :
4034 : 277 : return decl;
4035 : : }
4036 : :
4037 : : /* We are pushing a new decl. */
4038 : :
4039 : : /* Skip a hidden builtin we failed to match already. There can
4040 : : only be one. */
4041 : 1153939358 : if (old && anticipated_builtin_p (old))
4042 : 859189 : old = OVL_CHAIN (old);
4043 : :
4044 : 1153939358 : if (hiding)
4045 : : ; /* Hidden bindings don't shadow anything. */
4046 : : else
4047 : 1079730651 : check_template_shadow (decl);
4048 : :
4049 : 1153939358 : if (DECL_DECLARES_FUNCTION_P (decl))
4050 : : {
4051 : 322993204 : check_default_args (decl);
4052 : :
4053 : 322993204 : if (hiding)
4054 : : {
4055 : 74135529 : if (level->kind != sk_namespace)
4056 : : {
4057 : : /* In a local class, a friend function declaration must
4058 : : find a matching decl in the innermost non-class scope.
4059 : : [class.friend/11] */
4060 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
4061 : : "friend declaration %qD in local class without "
4062 : : "prior local declaration", decl);
4063 : : /* Don't attempt to push it. */
4064 : 9 : return error_mark_node;
4065 : : }
4066 : : }
4067 : : }
4068 : :
4069 : 1153939349 : if (level->kind != sk_namespace)
4070 : : {
4071 : 799641062 : tree local_shadow = check_local_shadow (decl);
4072 : 799641062 : if (name_independent_p && local_shadow)
4073 : : {
4074 : 96 : if (cxx_dialect < cxx26 && !name_independent_diagnosed_p)
4075 : 92 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
4076 : : "name-independent declarations only available with "
4077 : : "%<-std=c++2c%> or %<-std=gnu++2c%>");
4078 : 96 : name_independent_diagnosed_p = true;
4079 : : /* When a name-independent declaration is pushed into a scope
4080 : : which itself does not contain a _ named declaration yet (so
4081 : : _ name lookups wouldn't be normally ambiguous), but it
4082 : : shadows a _ declaration in some outer scope in cases
4083 : : described in [basic.scope.block]/2 where if the names of
4084 : : the shadowed and shadowing declarations were different it
4085 : : would be ill-formed program, arrange for _ name lookups
4086 : : in this scope to be ambiguous. */
4087 : 96 : if (old == NULL_TREE)
4088 : : {
4089 : 96 : old = build_tree_list (error_mark_node, local_shadow);
4090 : 96 : TREE_TYPE (old) = error_mark_node;
4091 : : }
4092 : : }
4093 : :
4094 : 799641062 : if (TREE_CODE (decl) == NAMESPACE_DECL)
4095 : : /* A local namespace alias. */
4096 : 100565 : set_identifier_type_value_with_scope (name, NULL_TREE, level);
4097 : :
4098 : 799641062 : if (!binding)
4099 : 799640665 : binding = create_local_binding (level, name);
4100 : : }
4101 : 354298287 : else if (!slot)
4102 : : {
4103 : 33 : ns = current_namespace;
4104 : 33 : slot = find_namespace_slot (ns, name, true);
4105 : 33 : mslot = get_fixed_binding_slot (slot, name, BINDING_SLOT_CURRENT, true);
4106 : : /* Update OLD to reflect the namespace we're going to be
4107 : : pushing into. */
4108 : 33 : old = MAYBE_STAT_DECL (*mslot);
4109 : : }
4110 : :
4111 : 1153939349 : old = update_binding (level, binding, mslot, old, decl, hiding);
4112 : :
4113 : 1153939349 : if (old != decl)
4114 : : /* An existing decl matched, use it. */
4115 : : decl = old;
4116 : : else
4117 : : {
4118 : 1153939325 : if (TREE_CODE (decl) == TYPE_DECL)
4119 : : {
4120 : 213723937 : tree type = TREE_TYPE (decl);
4121 : :
4122 : 213723937 : if (type != error_mark_node)
4123 : : {
4124 : 213723870 : if (TYPE_NAME (type) != decl)
4125 : 20444349 : set_underlying_type (decl);
4126 : :
4127 : 213723870 : set_identifier_type_value_with_scope (name, decl, level);
4128 : :
4129 : 213723870 : if (level->kind != sk_namespace
4130 : 213723870 : && !instantiating_current_function_p ())
4131 : : /* This is a locally defined typedef in a function that
4132 : : is not a template instantation, record it to implement
4133 : : -Wunused-local-typedefs. */
4134 : 199803666 : record_locally_defined_typedef (decl);
4135 : : }
4136 : : }
4137 : 940215388 : else if (VAR_OR_FUNCTION_DECL_P (decl))
4138 : : {
4139 : 374260189 : if (DECL_EXTERN_C_P (decl))
4140 : 279493446 : check_extern_c_conflict (decl);
4141 : :
4142 : 374260189 : if (!DECL_LOCAL_DECL_P (decl)
4143 : 374260189 : && VAR_P (decl))
4144 : 75555057 : maybe_register_incomplete_var (decl);
4145 : :
4146 : 374260189 : if (DECL_LOCAL_DECL_P (decl)
4147 : 374260189 : && NAMESPACE_SCOPE_P (decl))
4148 : 37339 : push_local_extern_decl_alias (decl);
4149 : : }
4150 : :
4151 : 1153939325 : if (level->kind == sk_namespace
4152 : 354298269 : && TREE_PUBLIC (level->this_entity)
4153 : 1508232947 : && module_maybe_has_cmi_p ())
4154 : 196860 : maybe_record_mergeable_decl (slot, name, decl);
4155 : : }
4156 : : }
4157 : : else
4158 : 25837904 : add_decl_to_level (level, decl);
4159 : :
4160 : : return decl;
4161 : 1188735042 : }
4162 : :
4163 : : /* A mergeable entity is being loaded into namespace NS slot NAME.
4164 : : Create and return the appropriate vector slot for that. Either a
4165 : : GMF slot or a module-specific one. */
4166 : :
4167 : : tree *
4168 : 113629 : mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
4169 : : {
4170 : 113629 : tree *mslot = find_namespace_slot (ns, name, true);
4171 : 113629 : tree *vslot = get_fixed_binding_slot
4172 : 226626 : (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
4173 : : true);
4174 : :
4175 : 113629 : gcc_checking_assert (TREE_CODE (*mslot) == BINDING_VECTOR);
4176 : 113629 : *vec = *mslot;
4177 : :
4178 : 113629 : return vslot;
4179 : : }
4180 : :
4181 : : /* DECL is a new mergeable namespace-scope decl. Add it to the
4182 : : mergeable entities on GSLOT. */
4183 : :
4184 : : void
4185 : 42673 : add_mergeable_namespace_entity (tree *gslot, tree decl)
4186 : : {
4187 : 42673 : *gslot = ovl_make (decl, *gslot);
4188 : 42673 : }
4189 : :
4190 : : /* A mergeable entity of KLASS called NAME is being loaded. Return
4191 : : the set of things it could be. All such non-as_base classes have
4192 : : been given a member vec. */
4193 : :
4194 : : tree
4195 : 150019 : lookup_class_binding (tree klass, tree name)
4196 : : {
4197 : 150019 : tree found = NULL_TREE;
4198 : :
4199 : 150019 : if (!COMPLETE_TYPE_P (klass))
4200 : : ;
4201 : 150019 : else if (TYPE_LANG_SPECIFIC (klass))
4202 : : {
4203 : 148673 : vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
4204 : :
4205 : 148673 : found = member_vec_binary_search (member_vec, name);
4206 : 148673 : if (!found)
4207 : : ;
4208 : 148317 : else if (STAT_HACK_P (found))
4209 : : /* Rearrange the stat hack so that we don't need to expose that
4210 : : internal detail. */
4211 : 6 : found = ovl_make (STAT_TYPE (found), STAT_DECL (found));
4212 : 148311 : else if (IDENTIFIER_CONV_OP_P (name))
4213 : : {
4214 : 788 : gcc_checking_assert (name == conv_op_identifier);
4215 : 788 : found = OVL_CHAIN (found);
4216 : : }
4217 : : }
4218 : : else
4219 : : {
4220 : 1346 : gcc_checking_assert (IS_FAKE_BASE_TYPE (klass)
4221 : : || TYPE_PTRMEMFUNC_P (klass));
4222 : 1346 : found = fields_linear_search (klass, name, false);
4223 : : }
4224 : :
4225 : 150019 : return found;
4226 : : }
4227 : :
4228 : : /* Whether this using is declared in the module purview. */
4229 : :
4230 : : bool
4231 : 15540 : ovl_iterator::purview_p () const
4232 : : {
4233 : 15540 : gcc_checking_assert (using_p ());
4234 : 15540 : if (TREE_CODE (ovl) == USING_DECL)
4235 : 2792 : return DECL_MODULE_PURVIEW_P (ovl);
4236 : 12748 : return OVL_PURVIEW_P (ovl);
4237 : : }
4238 : :
4239 : : /* Whether this using is exported from this module. */
4240 : :
4241 : : bool
4242 : 15540 : ovl_iterator::exporting_p () const
4243 : : {
4244 : 15540 : gcc_checking_assert (using_p ());
4245 : 15540 : if (TREE_CODE (ovl) == USING_DECL)
4246 : 2792 : return DECL_MODULE_EXPORT_P (ovl);
4247 : 12748 : return OVL_EXPORT_P (ovl);
4248 : : }
4249 : :
4250 : : /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
4251 : : for all decls of the current module. When partitions are involved,
4252 : : decls might be mentioned more than once. Return the accumulation of
4253 : : CALLBACK results. */
4254 : :
4255 : : unsigned
4256 : 6532124 : walk_module_binding (tree binding, bitmap partitions,
4257 : : bool (*callback) (tree decl, WMB_Flags, void *data),
4258 : : void *data)
4259 : : {
4260 : 6532124 : tree current = binding;
4261 : 6532124 : unsigned count = 0;
4262 : :
4263 : 6532124 : if (TREE_CODE (binding) == BINDING_VECTOR)
4264 : 15795 : current = BINDING_VECTOR_CLUSTER (binding, 0).slots[BINDING_SLOT_CURRENT];
4265 : :
4266 : 6533181 : bool decl_hidden = false;
4267 : 6532124 : if (tree type = MAYBE_STAT_TYPE (current))
4268 : : {
4269 : 68 : WMB_Flags flags = WMB_None;
4270 : 68 : if (STAT_TYPE_HIDDEN_P (current))
4271 : 0 : flags = WMB_Flags (flags | WMB_Hidden);
4272 : 68 : if (TREE_CODE (type) == USING_DECL)
4273 : : {
4274 : 3 : flags = WMB_Flags (flags | WMB_Using);
4275 : 3 : if (DECL_MODULE_PURVIEW_P (type))
4276 : 3 : flags = WMB_Flags (flags | WMB_Purview);
4277 : 3 : if (DECL_MODULE_EXPORT_P (type))
4278 : 3 : flags = WMB_Flags (flags | WMB_Export);
4279 : : }
4280 : 68 : count += callback (type, flags, data);
4281 : 68 : decl_hidden = STAT_DECL_HIDDEN_P (current);
4282 : : }
4283 : :
4284 : 13111443 : for (ovl_iterator iter (MAYBE_STAT_DECL (current)); iter; ++iter)
4285 : : {
4286 : 6579319 : if (iter.hidden_p ())
4287 : : decl_hidden = true;
4288 : 6579319 : if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)))
4289 : : {
4290 : 4991580 : WMB_Flags flags = WMB_None;
4291 : 4991580 : if (decl_hidden)
4292 : 5625 : flags = WMB_Flags (flags | WMB_Hidden);
4293 : 4991580 : if (iter.using_p ())
4294 : : {
4295 : 15528 : flags = WMB_Flags (flags | WMB_Using);
4296 : 15528 : if (iter.purview_p ())
4297 : 10379 : flags = WMB_Flags (flags | WMB_Purview);
4298 : 15528 : if (iter.exporting_p ())
4299 : 9746 : flags = WMB_Flags (flags | WMB_Export);
4300 : : }
4301 : 4991580 : count += callback (*iter, flags, data);
4302 : : }
4303 : 6579319 : decl_hidden = false;
4304 : : }
4305 : :
4306 : 6532124 : if (partitions && TREE_CODE (binding) == BINDING_VECTOR)
4307 : : {
4308 : : /* Process partition slots. */
4309 : 252 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
4310 : 252 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
4311 : 252 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
4312 : : {
4313 : 252 : ix--;
4314 : 252 : cluster++;
4315 : : }
4316 : :
4317 : : /* There could be duplicate module or GMF entries. */
4318 : 252 : bool maybe_dups = (BINDING_VECTOR_PARTITION_DUPS_P (binding)
4319 : 252 : || BINDING_VECTOR_GLOBAL_DUPS_P (binding));
4320 : :
4321 : 534 : for (; ix--; cluster++)
4322 : 846 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
4323 : 564 : if (!cluster->slots[jx].is_lazy ())
4324 : 561 : if (tree bind = cluster->slots[jx])
4325 : : {
4326 : 311 : if (TREE_CODE (bind) == NAMESPACE_DECL
4327 : 311 : && !DECL_NAMESPACE_ALIAS (bind))
4328 : : {
4329 : 18 : if (unsigned base = cluster->indices[jx].base)
4330 : 18 : if (unsigned span = cluster->indices[jx].span)
4331 : 18 : do
4332 : 18 : if (bitmap_bit_p (partitions, base))
4333 : 18 : goto found;
4334 : 0 : while (++base, --span);
4335 : : /* Not a partition's namespace. */
4336 : 0 : continue;
4337 : 18 : found:
4338 : :
4339 : 18 : WMB_Flags flags = WMB_None;
4340 : 18 : if (maybe_dups)
4341 : 0 : flags = WMB_Flags (flags | WMB_Dups);
4342 : 18 : count += callback (bind, flags, data);
4343 : 0 : }
4344 : 293 : else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind))
4345 : : {
4346 : 166 : if (tree btype = STAT_TYPE (bind))
4347 : : {
4348 : 0 : WMB_Flags flags = WMB_None;
4349 : 0 : if (maybe_dups)
4350 : 0 : flags = WMB_Flags (flags | WMB_Dups);
4351 : 0 : if (STAT_TYPE_HIDDEN_P (bind))
4352 : 0 : flags = WMB_Flags (flags | WMB_Hidden);
4353 : 0 : if (TREE_CODE (btype) == USING_DECL)
4354 : : {
4355 : 0 : flags = WMB_Flags (flags | WMB_Using);
4356 : 0 : if (DECL_MODULE_PURVIEW_P (btype))
4357 : 0 : flags = WMB_Flags (flags | WMB_Purview);
4358 : 0 : if (DECL_MODULE_EXPORT_P (btype))
4359 : 0 : flags = WMB_Flags (flags | WMB_Export);
4360 : : }
4361 : 0 : count += callback (btype, flags, data);
4362 : : }
4363 : 166 : bool part_hidden = STAT_DECL_HIDDEN_P (bind);
4364 : 166 : for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind)));
4365 : 338 : iter; ++iter)
4366 : : {
4367 : 172 : if (iter.hidden_p ())
4368 : : part_hidden = true;
4369 : 172 : gcc_checking_assert
4370 : : (!(part_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter)));
4371 : :
4372 : 172 : WMB_Flags flags = WMB_None;
4373 : 172 : if (maybe_dups)
4374 : 66 : flags = WMB_Flags (flags | WMB_Dups);
4375 : 172 : if (part_hidden)
4376 : 9 : flags = WMB_Flags (flags | WMB_Hidden);
4377 : 172 : if (iter.using_p ())
4378 : : {
4379 : 12 : flags = WMB_Flags (flags | WMB_Using);
4380 : 12 : if (iter.purview_p ())
4381 : 12 : flags = WMB_Flags (flags | WMB_Purview);
4382 : 12 : if (iter.exporting_p ())
4383 : 12 : flags = WMB_Flags (flags | WMB_Export);
4384 : : }
4385 : 172 : count += callback (*iter, flags, data);
4386 : 172 : part_hidden = false;
4387 : : }
4388 : : }
4389 : : }
4390 : : }
4391 : :
4392 : 6532124 : return count;
4393 : : }
4394 : :
4395 : : /* Imported module MOD has a binding to NS::NAME, stored in section
4396 : : SNUM. */
4397 : :
4398 : : bool
4399 : 140179 : import_module_binding (tree ns, tree name, unsigned mod, unsigned snum)
4400 : : {
4401 : 140179 : tree *slot = find_namespace_slot (ns, name, true);
4402 : 140179 : binding_slot *mslot = append_imported_binding_slot (slot, name, mod);
4403 : :
4404 : 140179 : if (mslot->is_lazy () || *mslot)
4405 : : /* Oops, something was already there. */
4406 : : return false;
4407 : :
4408 : 140179 : mslot->set_lazy (snum);
4409 : 140179 : return true;
4410 : : }
4411 : :
4412 : : /* An import of MODULE is binding NS::NAME. There should be no
4413 : : existing binding for >= MODULE. GLOBAL_P indicates whether the
4414 : : bindings include global module entities. PARTITION_P is true if
4415 : : it is part of the current module. VALUE and TYPE are the value
4416 : : and type bindings. VISIBLE are the value bindings being exported. */
4417 : :
4418 : : bool
4419 : 86300 : set_module_binding (tree ns, tree name, unsigned mod, bool global_p,
4420 : : bool partition_p, tree value, tree type, tree visible)
4421 : : {
4422 : 86300 : if (!value)
4423 : : /* Bogus BMIs could give rise to nothing to bind. */
4424 : : return false;
4425 : :
4426 : 86300 : gcc_assert (TREE_CODE (value) != NAMESPACE_DECL
4427 : : || DECL_NAMESPACE_ALIAS (value));
4428 : 86300 : gcc_checking_assert (mod);
4429 : :
4430 : 86300 : tree *slot = find_namespace_slot (ns, name, true);
4431 : 86300 : binding_slot *mslot = search_imported_binding_slot (slot, mod);
4432 : :
4433 : 86300 : if (!mslot || !mslot->is_lazy ())
4434 : : /* Again, bogus BMI could give find to missing or already loaded slot. */
4435 : : return false;
4436 : :
4437 : 86300 : tree bind = value;
4438 : 86300 : if (type || visible != bind || partition_p || global_p)
4439 : : {
4440 : 84864 : bind = stat_hack (bind, type);
4441 : 84864 : STAT_VISIBLE (bind) = visible;
4442 : 653 : if ((partition_p && TREE_PUBLIC (ns))
4443 : 84870 : || (type && DECL_MODULE_EXPORT_P (type)))
4444 : 668 : STAT_TYPE_VISIBLE_P (bind) = true;
4445 : : }
4446 : :
4447 : : /* Note if this is this-module and/or global binding. */
4448 : 84864 : if (partition_p)
4449 : 653 : MODULE_BINDING_PARTITION_P (bind) = true;
4450 : 86300 : if (global_p)
4451 : 84038 : MODULE_BINDING_GLOBAL_P (bind) = true;
4452 : :
4453 : 86300 : *mslot = bind;
4454 : :
4455 : 86300 : return true;
4456 : : }
4457 : :
4458 : : void
4459 : 44500 : add_module_namespace_decl (tree ns, tree decl)
4460 : : {
4461 : 44500 : gcc_assert (!DECL_CHAIN (decl));
4462 : 44500 : gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl)
4463 : : && DECL_LOCAL_DECL_P (decl)));
4464 : 44500 : if (CHECKING_P)
4465 : : /* Expensive already-there? check. */
4466 : 71009484 : for (auto probe = NAMESPACE_LEVEL (ns)->names; probe;
4467 : 70964984 : probe = DECL_CHAIN (probe))
4468 : 70964984 : gcc_assert (decl != probe);
4469 : :
4470 : 44500 : add_decl_to_level (NAMESPACE_LEVEL (ns), decl);
4471 : :
4472 : 44500 : if (VAR_P (decl))
4473 : 1434 : maybe_register_incomplete_var (decl);
4474 : :
4475 : 43066 : if (VAR_OR_FUNCTION_DECL_P (decl)
4476 : 59541 : && DECL_EXTERN_C_P (decl))
4477 : 7698 : check_extern_c_conflict (decl);
4478 : 44500 : }
4479 : :
4480 : : /* Enter DECL into the symbol table, if that's appropriate. Returns
4481 : : DECL, or a modified version thereof. */
4482 : :
4483 : : tree
4484 : 130189223 : maybe_push_decl (tree decl)
4485 : : {
4486 : 130189223 : tree type = TREE_TYPE (decl);
4487 : :
4488 : : /* Add this decl to the current binding level, but not if it comes
4489 : : from another scope, e.g. a static member variable. TEM may equal
4490 : : DECL or it may be a previous decl of the same name. */
4491 : 130189223 : if (decl == error_mark_node
4492 : 130189218 : || (TREE_CODE (decl) != PARM_DECL
4493 : 130189218 : && DECL_CONTEXT (decl) != NULL_TREE
4494 : : /* Definitions of namespace members outside their namespace are
4495 : : possible. */
4496 : 47497247 : && !DECL_NAMESPACE_SCOPE_P (decl))
4497 : 128895353 : || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4498 : 128895353 : || type == unknown_type_node
4499 : : /* The declaration of a template specialization does not affect
4500 : : the functions available for overload resolution, so we do not
4501 : : call pushdecl. */
4502 : 259084576 : || (TREE_CODE (decl) == FUNCTION_DECL
4503 : 35865820 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
4504 : 1385044 : return decl;
4505 : : else
4506 : 128804179 : return pushdecl (decl);
4507 : : }
4508 : :
4509 : : /* Bind DECL to ID in the current_binding_level, assumed to be a local
4510 : : binding level. If IS_USING is true, DECL got here through a
4511 : : using-declaration. */
4512 : :
4513 : : static void
4514 : 3727415 : push_local_binding (tree id, tree decl, bool is_using)
4515 : : {
4516 : : /* Skip over any local classes. This makes sense if we call
4517 : : push_local_binding with a friend decl of a local class. */
4518 : 3727415 : cp_binding_level *b = innermost_nonclass_level ();
4519 : :
4520 : 3727415 : gcc_assert (b->kind != sk_namespace);
4521 : 3727415 : if (find_local_binding (b, id))
4522 : : {
4523 : : /* Supplement the existing binding. */
4524 : 27 : if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
4525 : : /* It didn't work. Something else must be bound at this
4526 : : level. Do not add DECL to the list of things to pop
4527 : : later. */
4528 : : return;
4529 : : }
4530 : : else
4531 : : /* Create a new binding. */
4532 : 3727388 : push_binding (id, decl, b);
4533 : :
4534 : 3727406 : if (TREE_CODE (decl) == OVERLOAD || is_using)
4535 : : /* We must put the OVERLOAD or using into a TREE_LIST since we
4536 : : cannot use the decl's chain itself. */
4537 : 3727406 : decl = build_tree_list (id, decl);
4538 : :
4539 : : /* And put DECL on the list of things declared by the current
4540 : : binding level. */
4541 : 3727406 : add_decl_to_level (b, decl);
4542 : : }
4543 : :
4544 : : /* Lookup the FRIEND_TMPL within all merged module imports. Used to dedup
4545 : : instantiations of temploid hidden friends from imported modules. */
4546 : :
4547 : : tree
4548 : 260 : lookup_imported_hidden_friend (tree friend_tmpl)
4549 : : {
4550 : : /* For a class-scope friend class it should have been found by regular
4551 : : name lookup. Otherwise we're looking in the current namespace. */
4552 : 260 : gcc_checking_assert (CP_DECL_CONTEXT (friend_tmpl) == current_namespace);
4553 : :
4554 : 260 : tree inner = DECL_TEMPLATE_RESULT (friend_tmpl);
4555 : 260 : if (!DECL_LANG_SPECIFIC (inner)
4556 : 314 : || !DECL_MODULE_ENTITY_P (inner))
4557 : : return NULL_TREE;
4558 : :
4559 : : /* Load any templates matching FRIEND_TMPL from importers. */
4560 : 36 : lazy_load_pendings (friend_tmpl);
4561 : :
4562 : 36 : tree name = DECL_NAME (inner);
4563 : 36 : tree *slot = find_namespace_slot (current_namespace, name, false);
4564 : 36 : if (!slot || !*slot || TREE_CODE (*slot) != BINDING_VECTOR)
4565 : : return NULL_TREE;
4566 : :
4567 : : /* We're only interested in declarations attached to the same module
4568 : : as the friend class we're attempting to instantiate. */
4569 : 21 : int m = get_originating_module (friend_tmpl, /*global=-1*/true);
4570 : 21 : gcc_assert (m != 0);
4571 : :
4572 : : /* First check whether there's a reachable declaration attached to the module
4573 : : we're looking for. */
4574 : 21 : if (m > 0)
4575 : 3 : if (binding_slot *mslot = search_imported_binding_slot (slot, m))
4576 : : {
4577 : 3 : if (mslot->is_lazy ())
4578 : 0 : lazy_load_binding (m, current_namespace, name, mslot);
4579 : 3 : for (ovl_iterator iter (*mslot); iter; ++iter)
4580 : 3 : if (DECL_CLASS_TEMPLATE_P (*iter))
4581 : 3 : return *iter;
4582 : : }
4583 : :
4584 : : /* Otherwise, look in the mergeable slots for this name, in case an importer
4585 : : has already instantiated this declaration. */
4586 : : tree *vslot = get_fixed_binding_slot
4587 : 18 : (slot, name, m > 0 ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL, false);
4588 : 18 : if (!vslot || !*vslot)
4589 : : return NULL_TREE;
4590 : :
4591 : : /* There should be at most one class template from the module we're
4592 : : looking for, return it. */
4593 : 18 : for (ovl_iterator iter (*vslot); iter; ++iter)
4594 : 36 : if (DECL_CLASS_TEMPLATE_P (*iter)
4595 : 36 : && get_originating_module (*iter, true) == m)
4596 : 18 : return *iter;
4597 : :
4598 : 0 : return NULL_TREE;
4599 : : }
4600 : :
4601 : :
4602 : : /* true means unconditionally make a BLOCK for the next level pushed. */
4603 : :
4604 : : static bool keep_next_level_flag;
4605 : :
4606 : : static int binding_depth = 0;
4607 : :
4608 : : static void
4609 : 0 : indent (int depth)
4610 : : {
4611 : 0 : int i;
4612 : :
4613 : 0 : for (i = 0; i < depth * 2; i++)
4614 : 0 : putc (' ', stderr);
4615 : 0 : }
4616 : :
4617 : : /* Return a string describing the kind of SCOPE we have. */
4618 : : static const char *
4619 : 0 : cp_binding_level_descriptor (cp_binding_level *scope)
4620 : : {
4621 : : /* The order of this table must match the "scope_kind"
4622 : : enumerators. */
4623 : 0 : static const char* scope_kind_names[] = {
4624 : : "block-scope",
4625 : : "cleanup-scope",
4626 : : "try-scope",
4627 : : "catch-scope",
4628 : : "for-scope",
4629 : : "cond-init-scope",
4630 : : "stmt-expr-scope",
4631 : : "function-parameter-scope",
4632 : : "class-scope",
4633 : : "enum-scope",
4634 : : "namespace-scope",
4635 : : "template-parameter-scope",
4636 : : "template-explicit-spec-scope",
4637 : : "transaction-scope",
4638 : : "openmp-scope"
4639 : : };
4640 : 0 : static_assert (ARRAY_SIZE (scope_kind_names) == sk_count,
4641 : : "must keep names aligned with scope_kind enum");
4642 : :
4643 : 0 : scope_kind kind = scope->kind;
4644 : 0 : if (kind == sk_template_parms && scope->explicit_spec_p)
4645 : 0 : kind = sk_template_spec;
4646 : :
4647 : 0 : return scope_kind_names[kind];
4648 : : }
4649 : :
4650 : : /* Output a debugging information about SCOPE when performing
4651 : : ACTION at LINE. */
4652 : : static void
4653 : 0 : cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
4654 : : {
4655 : 0 : const char *desc = cp_binding_level_descriptor (scope);
4656 : 0 : if (scope->this_entity)
4657 : 0 : verbatim ("%s %<%s(%E)%> %p %d", action, desc,
4658 : : scope->this_entity, (void *) scope, line);
4659 : : else
4660 : 0 : verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
4661 : 0 : }
4662 : :
4663 : : /* A chain of binding_level structures awaiting reuse. */
4664 : :
4665 : : static GTY((deletable)) cp_binding_level *free_binding_level;
4666 : :
4667 : : /* Insert SCOPE as the innermost binding level. */
4668 : :
4669 : : void
4670 : 1112227447 : push_binding_level (cp_binding_level *scope)
4671 : : {
4672 : : /* Add it to the front of currently active scopes stack. */
4673 : 1112227447 : scope->level_chain = current_binding_level;
4674 : 1112227447 : current_binding_level = scope;
4675 : 1112227447 : keep_next_level_flag = false;
4676 : :
4677 : 1112227447 : if (ENABLE_SCOPE_CHECKING)
4678 : : {
4679 : : scope->binding_depth = binding_depth;
4680 : : indent (binding_depth);
4681 : : cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4682 : : "push");
4683 : : binding_depth++;
4684 : : }
4685 : 1112227447 : }
4686 : :
4687 : : /* Create a new KIND scope and make it the top of the active scopes stack.
4688 : : ENTITY is the scope of the associated C++ entity (namespace, class,
4689 : : function, C++0x enumeration); it is NULL otherwise. */
4690 : :
4691 : : cp_binding_level *
4692 : 935526390 : begin_scope (scope_kind kind, tree entity)
4693 : : {
4694 : 935526390 : cp_binding_level *scope;
4695 : :
4696 : : /* Reuse or create a struct for this binding level. */
4697 : 935526390 : if (!ENABLE_SCOPE_CHECKING && free_binding_level)
4698 : : {
4699 : 904490135 : scope = free_binding_level;
4700 : 904490135 : free_binding_level = scope->level_chain;
4701 : 904490135 : memset (scope, 0, sizeof (cp_binding_level));
4702 : : }
4703 : : else
4704 : 31036255 : scope = ggc_cleared_alloc<cp_binding_level> ();
4705 : :
4706 : 935526390 : scope->this_entity = entity;
4707 : 935526390 : scope->more_cleanups_ok = true;
4708 : 935526390 : switch (kind)
4709 : : {
4710 : 276 : case sk_cleanup:
4711 : 276 : scope->keep = true;
4712 : 276 : break;
4713 : :
4714 : 4998483 : case sk_template_spec:
4715 : 4998483 : scope->explicit_spec_p = true;
4716 : 4998483 : kind = sk_template_parms;
4717 : : /* Fall through. */
4718 : 608044939 : case sk_template_parms:
4719 : 608044939 : case sk_block:
4720 : 608044939 : case sk_try:
4721 : 608044939 : case sk_catch:
4722 : 608044939 : case sk_for:
4723 : 608044939 : case sk_cond:
4724 : 608044939 : case sk_class:
4725 : 608044939 : case sk_scoped_enum:
4726 : 608044939 : case sk_transaction:
4727 : 608044939 : case sk_omp:
4728 : 608044939 : case sk_stmt_expr:
4729 : 608044939 : scope->keep = keep_next_level_flag;
4730 : 608044939 : break;
4731 : :
4732 : 327385779 : case sk_function_parms:
4733 : 327385779 : scope->keep = keep_next_level_flag;
4734 : 327385779 : break;
4735 : :
4736 : 95396 : case sk_namespace:
4737 : 95396 : NAMESPACE_LEVEL (entity) = scope;
4738 : 95396 : break;
4739 : :
4740 : 0 : default:
4741 : : /* Should not happen. */
4742 : 0 : gcc_unreachable ();
4743 : 935526390 : break;
4744 : : }
4745 : 935526390 : scope->kind = kind;
4746 : :
4747 : 935526390 : push_binding_level (scope);
4748 : :
4749 : 935526390 : return scope;
4750 : : }
4751 : :
4752 : : /* We're about to leave current scope. Pop the top of the stack of
4753 : : currently active scopes. Return the enclosing scope, now active. */
4754 : :
4755 : : cp_binding_level *
4756 : 1158150072 : leave_scope (void)
4757 : : {
4758 : 1158150072 : cp_binding_level *scope = current_binding_level;
4759 : :
4760 : 1158150072 : if (scope->kind == sk_namespace && class_binding_level)
4761 : 0 : current_binding_level = class_binding_level;
4762 : :
4763 : : /* We cannot leave a scope, if there are none left. */
4764 : 1158150072 : if (NAMESPACE_LEVEL (global_namespace))
4765 : 1158150072 : gcc_assert (!global_scope_p (scope));
4766 : :
4767 : 1158150072 : if (ENABLE_SCOPE_CHECKING)
4768 : : {
4769 : : indent (--binding_depth);
4770 : : cp_binding_level_debug (scope, LOCATION_LINE (input_location),
4771 : : "leave");
4772 : : }
4773 : :
4774 : : /* Move one nesting level up. */
4775 : 1158150072 : current_binding_level = scope->level_chain;
4776 : :
4777 : : /* Namespace-scopes are left most probably temporarily, not
4778 : : completely; they can be reopened later, e.g. in namespace-extension
4779 : : or any name binding activity that requires us to resume a
4780 : : namespace. For classes, we cache some binding levels. For other
4781 : : scopes, we just make the structure available for reuse. */
4782 : 1158150072 : if (scope->kind != sk_namespace
4783 : 1112115704 : && scope != previous_class_level)
4784 : : {
4785 : 880833509 : scope->level_chain = free_binding_level;
4786 : 880833509 : gcc_assert (!ENABLE_SCOPE_CHECKING
4787 : : || scope->binding_depth == binding_depth);
4788 : 880833509 : free_binding_level = scope;
4789 : : }
4790 : :
4791 : 1158150072 : if (scope->kind == sk_class)
4792 : : {
4793 : : /* Reset DEFINING_CLASS_P to allow for reuse of a
4794 : : class-defining scope in a non-defining context. */
4795 : 361080577 : scope->defining_class_p = 0;
4796 : :
4797 : : /* Find the innermost enclosing class scope, and reset
4798 : : CLASS_BINDING_LEVEL appropriately. */
4799 : 361080577 : class_binding_level = NULL;
4800 : 1494096870 : for (scope = current_binding_level; scope; scope = scope->level_chain)
4801 : 820361012 : if (scope->kind == sk_class)
4802 : : {
4803 : 48425296 : class_binding_level = scope;
4804 : 48425296 : break;
4805 : : }
4806 : : }
4807 : :
4808 : 1158150072 : return current_binding_level;
4809 : : }
4810 : :
4811 : : /* When we exit a toplevel class scope, we save its binding level so
4812 : : that we can restore it quickly. Here, we've entered some other
4813 : : class, so we must invalidate our cache. */
4814 : :
4815 : : void
4816 : 24808238 : invalidate_class_lookup_cache (void)
4817 : : {
4818 : 24808238 : previous_class_level->level_chain = free_binding_level;
4819 : 24808238 : free_binding_level = previous_class_level;
4820 : 24808238 : previous_class_level = NULL;
4821 : 24808238 : }
4822 : :
4823 : : static void
4824 : 46034377 : resume_scope (cp_binding_level* b)
4825 : : {
4826 : : /* Resuming binding levels is meant only for namespaces,
4827 : : and those cannot nest into classes. */
4828 : 46034377 : gcc_assert (!class_binding_level);
4829 : : /* Also, resuming a non-directly nested namespace is a no-no. */
4830 : 46034377 : gcc_assert (b->level_chain == current_binding_level);
4831 : 46034377 : current_binding_level = b;
4832 : 46034377 : if (ENABLE_SCOPE_CHECKING)
4833 : : {
4834 : : b->binding_depth = binding_depth;
4835 : : indent (binding_depth);
4836 : : cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
4837 : : binding_depth++;
4838 : : }
4839 : 46034377 : }
4840 : :
4841 : : /* Return the innermost binding level that is not for a class scope. */
4842 : :
4843 : : static cp_binding_level *
4844 : 1456940354 : innermost_nonclass_level (void)
4845 : : {
4846 : 1456940354 : cp_binding_level *b;
4847 : :
4848 : 1456940354 : b = current_binding_level;
4849 : 1676238916 : while (b->kind == sk_class)
4850 : 219298562 : b = b->level_chain;
4851 : :
4852 : 1456940354 : return b;
4853 : : }
4854 : :
4855 : : /* We're defining an object of type TYPE. If it needs a cleanup, but
4856 : : we're not allowed to add any more objects with cleanups to the current
4857 : : scope, create a new binding level. */
4858 : :
4859 : : void
4860 : 5333142 : maybe_push_cleanup_level (tree type)
4861 : : {
4862 : 5333142 : if (type != error_mark_node
4863 : 5332958 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4864 : 5661857 : && current_binding_level->more_cleanups_ok == 0)
4865 : : {
4866 : 276 : begin_scope (sk_cleanup, NULL);
4867 : 276 : current_binding_level->statement_list = push_stmt_list ();
4868 : : }
4869 : 5333142 : }
4870 : :
4871 : : /* Return true if we are in the global binding level. */
4872 : :
4873 : : bool
4874 : 340003 : global_bindings_p (void)
4875 : : {
4876 : 340003 : return global_scope_p (current_binding_level);
4877 : : }
4878 : :
4879 : : /* True if we are currently in a toplevel binding level. This
4880 : : means either the global binding level or a namespace in a toplevel
4881 : : binding level. Since there are no non-toplevel namespace levels,
4882 : : this really means any namespace or template parameter level. We
4883 : : also include a class whose context is toplevel. */
4884 : :
4885 : : bool
4886 : 1428845801 : toplevel_bindings_p (void)
4887 : : {
4888 : 1428845801 : cp_binding_level *b = innermost_nonclass_level ();
4889 : :
4890 : 1428845801 : return b->kind == sk_namespace || b->kind == sk_template_parms;
4891 : : }
4892 : :
4893 : : /* True if this is a namespace scope, or if we are defining a class
4894 : : which is itself at namespace scope, or whose enclosing class is
4895 : : such a class, etc. */
4896 : :
4897 : : bool
4898 : 24365390 : namespace_bindings_p (void)
4899 : : {
4900 : 24365390 : cp_binding_level *b = innermost_nonclass_level ();
4901 : :
4902 : 24365390 : return b->kind == sk_namespace;
4903 : : }
4904 : :
4905 : : /* True if the innermost non-class scope is a block scope. */
4906 : :
4907 : : bool
4908 : 1748 : local_bindings_p (void)
4909 : : {
4910 : 1748 : cp_binding_level *b = innermost_nonclass_level ();
4911 : 1748 : return b->kind < sk_function_parms || b->kind == sk_omp;
4912 : : }
4913 : :
4914 : : /* True if the current level needs to have a BLOCK made. */
4915 : :
4916 : : bool
4917 : 302652412 : kept_level_p (void)
4918 : : {
4919 : 302652412 : return (current_binding_level->blocks != NULL_TREE
4920 : 271362203 : || current_binding_level->keep
4921 : 264561020 : || current_binding_level->kind == sk_cleanup
4922 : 264561020 : || current_binding_level->names != NULL_TREE
4923 : 536545845 : || current_binding_level->using_directives);
4924 : : }
4925 : :
4926 : : /* Returns the kind of the innermost scope. */
4927 : :
4928 : : scope_kind
4929 : 2494815094 : innermost_scope_kind (void)
4930 : : {
4931 : 2494815094 : return current_binding_level->kind;
4932 : : }
4933 : :
4934 : : /* Returns true if this scope was created to store template parameters. */
4935 : :
4936 : : bool
4937 : 573238787 : template_parm_scope_p (void)
4938 : : {
4939 : 573238787 : return innermost_scope_kind () == sk_template_parms;
4940 : : }
4941 : :
4942 : : /* If KEEP is true, make a BLOCK node for the next binding level,
4943 : : unconditionally. Otherwise, use the normal logic to decide whether
4944 : : or not to create a BLOCK. */
4945 : :
4946 : : void
4947 : 11208274 : keep_next_level (bool keep)
4948 : : {
4949 : 11208274 : keep_next_level_flag = keep;
4950 : 11208274 : }
4951 : :
4952 : : /* Return the list of declarations of the current local scope. */
4953 : :
4954 : : tree
4955 : 326962328 : get_local_decls (void)
4956 : : {
4957 : 326962328 : gcc_assert (current_binding_level->kind != sk_namespace
4958 : : && current_binding_level->kind != sk_class);
4959 : 326962328 : return current_binding_level->names;
4960 : : }
4961 : :
4962 : : /* Return how many function prototypes we are currently nested inside. */
4963 : :
4964 : : int
4965 : 258903673 : function_parm_depth (void)
4966 : : {
4967 : 258903673 : int level = 0;
4968 : 258903673 : cp_binding_level *b;
4969 : :
4970 : 258903673 : for (b = current_binding_level;
4971 : 518516449 : b->kind == sk_function_parms;
4972 : 259612776 : b = b->level_chain)
4973 : 259612776 : ++level;
4974 : :
4975 : 258903673 : return level;
4976 : : }
4977 : :
4978 : : /* For debugging. */
4979 : : static int no_print_functions = 0;
4980 : : static int no_print_builtins = 0;
4981 : :
4982 : : static void
4983 : 0 : print_binding_level (cp_binding_level* lvl)
4984 : : {
4985 : 0 : tree t;
4986 : 0 : int i = 0, len;
4987 : 0 : if (lvl->this_entity)
4988 : 0 : print_node_brief (stderr, "entity=", lvl->this_entity, 1);
4989 : 0 : fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
4990 : 0 : if (lvl->more_cleanups_ok)
4991 : 0 : fprintf (stderr, " more-cleanups-ok");
4992 : 0 : if (lvl->have_cleanups)
4993 : 0 : fprintf (stderr, " have-cleanups");
4994 : 0 : fprintf (stderr, "\n");
4995 : 0 : if (lvl->names)
4996 : : {
4997 : 0 : fprintf (stderr, " names:\t");
4998 : : /* We can probably fit 3 names to a line? */
4999 : 0 : for (t = lvl->names; t; t = TREE_CHAIN (t))
5000 : : {
5001 : 0 : if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
5002 : 0 : continue;
5003 : 0 : if (no_print_builtins
5004 : 0 : && (TREE_CODE (t) == TYPE_DECL)
5005 : 0 : && DECL_IS_UNDECLARED_BUILTIN (t))
5006 : 0 : continue;
5007 : :
5008 : : /* Function decls tend to have longer names. */
5009 : 0 : if (TREE_CODE (t) == FUNCTION_DECL)
5010 : : len = 3;
5011 : : else
5012 : 0 : len = 2;
5013 : 0 : i += len;
5014 : 0 : if (i > 6)
5015 : : {
5016 : 0 : fprintf (stderr, "\n\t");
5017 : 0 : i = len;
5018 : : }
5019 : 0 : print_node_brief (stderr, "", t, 0);
5020 : 0 : if (t == error_mark_node)
5021 : : break;
5022 : : }
5023 : 0 : if (i)
5024 : 0 : fprintf (stderr, "\n");
5025 : : }
5026 : 0 : if (vec_safe_length (lvl->class_shadowed))
5027 : : {
5028 : 0 : size_t i;
5029 : 0 : cp_class_binding *b;
5030 : 0 : fprintf (stderr, " class-shadowed:");
5031 : 0 : FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
5032 : 0 : fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
5033 : 0 : fprintf (stderr, "\n");
5034 : : }
5035 : 0 : if (lvl->type_shadowed)
5036 : : {
5037 : 0 : fprintf (stderr, " type-shadowed:");
5038 : 0 : for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
5039 : : {
5040 : 0 : fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
5041 : : }
5042 : 0 : fprintf (stderr, "\n");
5043 : : }
5044 : 0 : }
5045 : :
5046 : : DEBUG_FUNCTION void
5047 : 0 : debug (cp_binding_level &ref)
5048 : : {
5049 : 0 : print_binding_level (&ref);
5050 : 0 : }
5051 : :
5052 : : DEBUG_FUNCTION void
5053 : 0 : debug (cp_binding_level *ptr)
5054 : : {
5055 : 0 : if (ptr)
5056 : 0 : debug (*ptr);
5057 : : else
5058 : 0 : fprintf (stderr, "<nil>\n");
5059 : 0 : }
5060 : :
5061 : : static void
5062 : 0 : print_other_binding_stack (cp_binding_level *stack)
5063 : : {
5064 : 0 : cp_binding_level *level;
5065 : 0 : for (level = stack; !global_scope_p (level); level = level->level_chain)
5066 : : {
5067 : 0 : fprintf (stderr, "binding level %p\n", (void *) level);
5068 : 0 : print_binding_level (level);
5069 : : }
5070 : 0 : }
5071 : :
5072 : : DEBUG_FUNCTION void
5073 : 0 : print_binding_stack (void)
5074 : : {
5075 : 0 : cp_binding_level *b;
5076 : 0 : fprintf (stderr, "current_binding_level=%p\n"
5077 : : "class_binding_level=%p\n"
5078 : : "NAMESPACE_LEVEL (global_namespace)=%p\n",
5079 : 0 : (void *) current_binding_level, (void *) class_binding_level,
5080 : 0 : (void *) NAMESPACE_LEVEL (global_namespace));
5081 : 0 : if (class_binding_level)
5082 : : {
5083 : 0 : for (b = class_binding_level; b; b = b->level_chain)
5084 : 0 : if (b == current_binding_level)
5085 : : break;
5086 : 0 : if (b)
5087 : : b = class_binding_level;
5088 : : else
5089 : 0 : b = current_binding_level;
5090 : : }
5091 : : else
5092 : 0 : b = current_binding_level;
5093 : 0 : print_other_binding_stack (b);
5094 : 0 : fprintf (stderr, "global:\n");
5095 : 0 : print_binding_level (NAMESPACE_LEVEL (global_namespace));
5096 : 0 : }
5097 : :
5098 : : /* Push a definition of struct, union or enum tag named ID. into
5099 : : binding_level B. DECL is a TYPE_DECL for the type. DECL has
5100 : : already been pushed into its binding level. This is bookkeeping to
5101 : : find it easily. */
5102 : :
5103 : : static void
5104 : 339234691 : set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
5105 : : {
5106 : 339234691 : if (b->kind == sk_namespace)
5107 : : /* At namespace scope we should not see an identifier type value. */
5108 : 22952288 : gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id)
5109 : : /* But we might end up here with ill-formed input. */
5110 : : || seen_error ());
5111 : : else
5112 : : {
5113 : : /* Push the current type value, so we can restore it later */
5114 : 316282403 : tree old = REAL_IDENTIFIER_TYPE_VALUE (id);
5115 : 316282403 : b->type_shadowed = tree_cons (id, old, b->type_shadowed);
5116 : 316282403 : tree type = decl ? TREE_TYPE (decl) : NULL_TREE;
5117 : 316282403 : TREE_TYPE (b->type_shadowed) = type;
5118 : 316282403 : SET_IDENTIFIER_TYPE_VALUE (id, type);
5119 : : }
5120 : 339234691 : }
5121 : :
5122 : : /* As set_identifier_type_value_with_scope, but using
5123 : : current_binding_level. */
5124 : :
5125 : : void
5126 : 116501916 : set_identifier_type_value (tree id, tree decl)
5127 : : {
5128 : 116501916 : set_identifier_type_value_with_scope (id, decl, current_binding_level);
5129 : 116501916 : }
5130 : :
5131 : : /* Return the name for the constructor (or destructor) for the
5132 : : specified class. */
5133 : :
5134 : : tree
5135 : 335956096 : constructor_name (tree type)
5136 : : {
5137 : 335956096 : tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
5138 : :
5139 : 335956096 : return decl ? DECL_NAME (decl) : NULL_TREE;
5140 : : }
5141 : :
5142 : : /* Returns TRUE if NAME is the name for the constructor for TYPE,
5143 : : which must be a class type. */
5144 : :
5145 : : bool
5146 : 314375352 : constructor_name_p (tree name, tree type)
5147 : : {
5148 : 314375352 : gcc_assert (MAYBE_CLASS_TYPE_P (type));
5149 : :
5150 : : /* These don't have names. */
5151 : 314375352 : if (TREE_CODE (type) == DECLTYPE_TYPE
5152 : 314375349 : || TREE_CODE (type) == TYPEOF_TYPE)
5153 : : return false;
5154 : :
5155 : 314375349 : if (name && name == constructor_name (type))
5156 : : return true;
5157 : :
5158 : : return false;
5159 : : }
5160 : :
5161 : : /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
5162 : : caller to set DECL_CONTEXT properly.
5163 : :
5164 : : Warning: For class and block-scope this must only be used when X
5165 : : will be the new innermost binding for its name, as we tack it onto
5166 : : the front of IDENTIFIER_BINDING without checking to see if the
5167 : : current IDENTIFIER_BINDING comes from a closer binding level than
5168 : : LEVEL.
5169 : :
5170 : : Warning: For namespace scope, this will look in LEVEL for an
5171 : : existing binding to match, but if not found will push the decl into
5172 : : CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
5173 : : pop_nested_namespace if you really need to push it into a foreign
5174 : : namespace. */
5175 : :
5176 : : static tree
5177 : 54299150 : do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
5178 : : {
5179 : 54299150 : cp_binding_level *b;
5180 : :
5181 : 54299150 : if (level->kind == sk_class)
5182 : : {
5183 : 0 : gcc_checking_assert (!hiding);
5184 : 0 : b = class_binding_level;
5185 : 0 : class_binding_level = level;
5186 : 0 : pushdecl_class_level (x);
5187 : 0 : class_binding_level = b;
5188 : : }
5189 : : else
5190 : : {
5191 : 54299150 : tree function_decl = current_function_decl;
5192 : 54299150 : if (level->kind == sk_namespace)
5193 : 50989664 : current_function_decl = NULL_TREE;
5194 : 54299150 : b = current_binding_level;
5195 : 54299150 : current_binding_level = level;
5196 : 54299150 : x = pushdecl (x, hiding);
5197 : 54299150 : current_binding_level = b;
5198 : 54299150 : current_function_decl = function_decl;
5199 : : }
5200 : 54299150 : return x;
5201 : : }
5202 : :
5203 : : /* Inject X into the local scope just before the function parms. */
5204 : :
5205 : : tree
5206 : 1715458 : pushdecl_outermost_localscope (tree x)
5207 : : {
5208 : 1715458 : cp_binding_level *b = NULL;
5209 : 1715458 : auto_cond_timevar tv (TV_NAME_LOOKUP);
5210 : :
5211 : : /* Find the block scope just inside the function parms. */
5212 : 1715458 : cp_binding_level *n = current_binding_level;
5213 : 1793517 : while (n && n->kind != sk_block)
5214 : 78059 : n = n->level_chain;
5215 : 4460041 : for (; n && n->kind != sk_function_parms; n = b->level_chain)
5216 : 2744583 : b = n;
5217 : :
5218 : 1715458 : return b ? do_pushdecl_with_scope (x, b) : error_mark_node;
5219 : 1715458 : }
5220 : :
5221 : : /* Checks if BINDING is a binding that we can export. */
5222 : :
5223 : : static bool
5224 : 10322 : check_can_export_using_decl (tree binding)
5225 : : {
5226 : : /* Declarations in header units are always OK. */
5227 : 10322 : if (header_module_p ())
5228 : : return true;
5229 : :
5230 : : /* We want the linkage of the underlying entity, so strip typedefs.
5231 : : If the underlying entity is a builtin type then we're OK. */
5232 : 304 : tree entity = binding;
5233 : 304 : if (TREE_CODE (entity) == TYPE_DECL)
5234 : : {
5235 : 106 : entity = TYPE_MAIN_DECL (TREE_TYPE (entity));
5236 : 106 : if (!entity)
5237 : : return true;
5238 : : }
5239 : :
5240 : 271 : linkage_kind linkage = decl_linkage (entity);
5241 : 271 : tree not_tmpl = STRIP_TEMPLATE (entity);
5242 : :
5243 : : /* Attachment is determined by the owner of an enumerator. */
5244 : 271 : if (TREE_CODE (not_tmpl) == CONST_DECL)
5245 : 25 : not_tmpl = TYPE_NAME (DECL_CONTEXT (not_tmpl));
5246 : :
5247 : : /* If the using decl is exported, the things it refers to must
5248 : : have external linkage. decl_linkage returns lk_external for
5249 : : module linkage so also check for attachment. */
5250 : 271 : if (linkage != lk_external
5251 : 271 : || (DECL_LANG_SPECIFIC (not_tmpl)
5252 : 155 : && DECL_MODULE_ATTACH_P (not_tmpl)
5253 : 86 : && !DECL_MODULE_EXPORT_P (not_tmpl)))
5254 : : {
5255 : 136 : auto_diagnostic_group d;
5256 : 136 : error ("exporting %q#D that does not have external linkage",
5257 : : binding);
5258 : 136 : if (linkage == lk_none)
5259 : 27 : inform (DECL_SOURCE_LOCATION (entity),
5260 : : "%q#D declared here with no linkage", entity);
5261 : 109 : else if (linkage == lk_internal)
5262 : 47 : inform (DECL_SOURCE_LOCATION (entity),
5263 : : "%q#D declared here with internal linkage", entity);
5264 : : else
5265 : 62 : inform (DECL_SOURCE_LOCATION (entity),
5266 : : "%q#D declared here with module linkage", entity);
5267 : 136 : return false;
5268 : 136 : }
5269 : :
5270 : : return true;
5271 : : }
5272 : :
5273 : : /* Process a local-scope or namespace-scope using declaration. LOOKUP
5274 : : is the result of qualified lookup (both value & type are
5275 : : significant). FN_SCOPE_P indicates if we're at function-scope (as
5276 : : opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
5277 : : bindings, which are altered to reflect the newly brought in
5278 : : declarations. */
5279 : :
5280 : : static bool
5281 : 9600941 : do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
5282 : : bool insert_p, tree *value_p, tree *type_p)
5283 : : {
5284 : 9600941 : tree value = *value_p;
5285 : 9600941 : tree type = *type_p;
5286 : 9600941 : bool failed = false;
5287 : :
5288 : : /* Shift the old and new bindings around so we're comparing class and
5289 : : enumeration names to each other. */
5290 : 9600941 : if (value && DECL_IMPLICIT_TYPEDEF_P (strip_using_decl (value)))
5291 : : {
5292 : : type = value;
5293 : : value = NULL_TREE;
5294 : : }
5295 : :
5296 : 9600941 : if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
5297 : : {
5298 : 110577 : lookup.type = lookup.value;
5299 : 110577 : lookup.value = NULL_TREE;
5300 : : }
5301 : :
5302 : : /* Only process exporting if we're going to be inserting. */
5303 : 9600941 : bool revealing_p = insert_p && !fn_scope_p && module_has_cmi_p ();
5304 : :
5305 : : /* First do the value binding. */
5306 : 9600941 : if (!lookup.value)
5307 : : /* Nothing (only implicit typedef found). */
5308 : 110577 : gcc_checking_assert (lookup.type);
5309 : 9490364 : else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
5310 : : {
5311 : 15175556 : for (lkp_iterator usings (lookup.value); usings; ++usings)
5312 : : {
5313 : 9748579 : tree new_fn = *usings;
5314 : 9748579 : tree inner = STRIP_TEMPLATE (new_fn);
5315 : 9748579 : bool exporting_p = revealing_p && module_exporting_p ();
5316 : 8434 : if (exporting_p)
5317 : 8434 : exporting_p = check_can_export_using_decl (new_fn);
5318 : :
5319 : : /* [namespace.udecl]
5320 : :
5321 : : If a function declaration in namespace scope or block
5322 : : scope has the same name and the same parameter types as a
5323 : : function introduced by a using declaration the program is
5324 : : ill-formed. */
5325 : : /* This seems overreaching, asking core -- why do we care
5326 : : about decls in the namespace that we cannot name (because
5327 : : they are not transitively imported. We just check the
5328 : : decls that are in this TU. */
5329 : 9748579 : bool found = false;
5330 : 150614367 : for (ovl_iterator old (value); !found && old; ++old)
5331 : : {
5332 : 70709474 : tree old_fn = *old;
5333 : :
5334 : 70709474 : if (new_fn == old_fn)
5335 : : {
5336 : : /* The function already exists in the current
5337 : : namespace. We will still want to insert it if
5338 : : it is revealing a not-revealed thing. */
5339 : 263943 : found = true;
5340 : 263943 : if (!revealing_p)
5341 : : ;
5342 : 399 : else if (old.using_p ())
5343 : : {
5344 : : /* Update in place. 'tis ok. */
5345 : 357 : OVL_PURVIEW_P (old.get_using ()) = true;
5346 : 357 : if (exporting_p)
5347 : 333 : OVL_EXPORT_P (old.get_using ()) = true;
5348 : : }
5349 : 42 : else if (!DECL_LANG_SPECIFIC (inner)
5350 : 84 : || !DECL_MODULE_PURVIEW_P (inner))
5351 : : /* We need to re-insert this function as a revealed
5352 : : (possibly exported) declaration. We can't remove
5353 : : the existing decl because that will change any
5354 : : overloads cached in template functions. */
5355 : : found = false;
5356 : : break;
5357 : : }
5358 : 70445531 : else if (old.using_p ())
5359 : 67180337 : continue; /* This is a using decl. */
5360 : 3265194 : else if (old.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn))
5361 : 2853597 : continue; /* This is an anticipated builtin. */
5362 : 411597 : else if (!matching_fn_p (new_fn, old_fn))
5363 : 411561 : continue; /* Parameters do not match. */
5364 : 36 : else if (decls_match (new_fn, old_fn))
5365 : : {
5366 : : /* Extern "C" in different namespaces. But similarly
5367 : : to above, if revealing a not-revealed thing we may
5368 : : need to reinsert. */
5369 : 15 : found = true;
5370 : 15 : if (revealing_p
5371 : 15 : && (!DECL_LANG_SPECIFIC (inner)
5372 : 3 : || !DECL_MODULE_PURVIEW_P (inner)))
5373 : : found = false;
5374 : : break;
5375 : : }
5376 : : else
5377 : : {
5378 : 21 : diagnose_name_conflict (new_fn, old_fn);
5379 : 21 : failed = true;
5380 : 21 : found = true;
5381 : 21 : break;
5382 : : }
5383 : : }
5384 : :
5385 : 9748579 : if (!found && insert_p)
5386 : : /* Unlike the decl-pushing case we don't drop anticipated
5387 : : builtins here. They don't cause a problem, and we'd
5388 : : like to match them with a future declaration. */
5389 : 9484630 : value = ovl_insert (new_fn, value, 1 + revealing_p + exporting_p);
5390 : : }
5391 : 5426977 : }
5392 : 4063387 : else if (value
5393 : : /* Ignore anticipated builtins. */
5394 : 41991 : && !anticipated_builtin_p (value)
5395 : 4105375 : && !decls_match (lookup.value, strip_using_decl (value)))
5396 : : {
5397 : 15 : diagnose_name_conflict (lookup.value, value);
5398 : 15 : failed = true;
5399 : : }
5400 : 4063372 : else if (insert_p)
5401 : : {
5402 : : /* A using-decl does not necessarily have the same purview-ness or
5403 : : exporting as the declaration it reveals, so build a USING_DECL
5404 : : that we can attach this information to. This also gives us a
5405 : : location for the using-decl that we can use in diagnostics.
5406 : :
5407 : : But this is unnecessary if we're just redeclaring the same decl;
5408 : : in that case we can just mark it purview or exported directly. */
5409 : 4063354 : if (value != lookup.value)
5410 : : {
5411 : 4051665 : value = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5412 : 4051665 : USING_DECL_DECLS (value) = lookup.value;
5413 : 4051665 : USING_DECL_SCOPE (value) = CP_DECL_CONTEXT (lookup.value);
5414 : 4051665 : DECL_CONTEXT (value) = current_scope ();
5415 : 4051665 : DECL_MODULE_PURVIEW_P (value) = module_purview_p ();
5416 : : }
5417 : : else
5418 : 11689 : set_instantiating_module (value);
5419 : :
5420 : 4063354 : if (revealing_p
5421 : 1883 : && module_exporting_p ()
5422 : 4065105 : && check_can_export_using_decl (lookup.value))
5423 : : {
5424 : 1663 : if (TREE_CODE (value) == TEMPLATE_DECL)
5425 : 14 : DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (value)) = true;
5426 : 1663 : DECL_MODULE_EXPORT_P (value) = true;
5427 : : }
5428 : : }
5429 : :
5430 : : /* Now the type binding. */
5431 : 9600941 : if (lookup.type)
5432 : : {
5433 : 110596 : if (type && !decls_match (lookup.type, strip_using_decl (type)))
5434 : : {
5435 : 3 : diagnose_name_conflict (lookup.type, type);
5436 : 3 : failed = true;
5437 : : }
5438 : 110593 : else if (insert_p)
5439 : : {
5440 : : /* As with revealing value bindings. */
5441 : 110593 : if (type != lookup.type)
5442 : : {
5443 : 110574 : type = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5444 : 110574 : USING_DECL_DECLS (type) = lookup.type;
5445 : 110574 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (lookup.type);
5446 : 110574 : DECL_CONTEXT (type) = current_scope ();
5447 : 110574 : DECL_MODULE_PURVIEW_P (type) = module_purview_p ();
5448 : : }
5449 : : else
5450 : 19 : set_instantiating_module (type);
5451 : :
5452 : 110593 : if (revealing_p
5453 : 156 : && module_exporting_p ()
5454 : 110730 : && check_can_export_using_decl (lookup.type))
5455 : 125 : DECL_MODULE_EXPORT_P (type) = true;
5456 : : }
5457 : : }
5458 : :
5459 : 9600941 : if (insert_p)
5460 : : {
5461 : : /* If value is empty, shift any class or enumeration name back. */
5462 : 9600914 : if (!value)
5463 : : {
5464 : 110568 : value = type;
5465 : 110568 : type = NULL_TREE;
5466 : : }
5467 : 9600914 : *value_p = value;
5468 : 9600914 : *type_p = type;
5469 : : }
5470 : :
5471 : 9600941 : return failed;
5472 : : }
5473 : :
5474 : : /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5475 : : Both are namespaces. */
5476 : :
5477 : : bool
5478 : 110709817 : is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
5479 : : {
5480 : 110709817 : int depth = SCOPE_DEPTH (ancestor);
5481 : :
5482 : 110709817 : if (!depth && !inline_only)
5483 : : /* The global namespace encloses everything. */
5484 : : return true;
5485 : :
5486 : 112693904 : while (SCOPE_DEPTH (descendant) > depth
5487 : 112693904 : && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
5488 : 2078042 : descendant = CP_DECL_CONTEXT (descendant);
5489 : :
5490 : 110615862 : return ancestor == descendant;
5491 : : }
5492 : :
5493 : : /* Returns true if ROOT (a non-alias namespace, class, or function)
5494 : : encloses CHILD. CHILD may be either a class type or a namespace
5495 : : (maybe alias). */
5496 : :
5497 : : bool
5498 : 82842648 : is_ancestor (tree root, tree child)
5499 : : {
5500 : 82842648 : gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
5501 : : && !DECL_NAMESPACE_ALIAS (root))
5502 : : || TREE_CODE (root) == FUNCTION_DECL
5503 : : || CLASS_TYPE_P (root));
5504 : 82842648 : gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
5505 : : || CLASS_TYPE_P (child));
5506 : :
5507 : : /* The global namespace encloses everything. Early-out for the
5508 : : common case. */
5509 : 82842648 : if (root == global_namespace)
5510 : : return true;
5511 : :
5512 : : /* Search CHILD until we reach namespace scope. */
5513 : 172241012 : while (TREE_CODE (child) != NAMESPACE_DECL)
5514 : : {
5515 : : /* If we've reached the ROOT, it encloses CHILD. */
5516 : 89660637 : if (root == child)
5517 : : return true;
5518 : :
5519 : : /* Go out one level. */
5520 : 89660586 : if (TYPE_P (child))
5521 : 88615048 : child = TYPE_NAME (child);
5522 : 89660586 : child = CP_DECL_CONTEXT (child);
5523 : : }
5524 : :
5525 : 82580375 : if (TREE_CODE (root) != NAMESPACE_DECL)
5526 : : /* Failed to meet the non-namespace we were looking for. */
5527 : : return false;
5528 : :
5529 : 82580360 : if (tree alias = DECL_NAMESPACE_ALIAS (child))
5530 : 3 : child = alias;
5531 : :
5532 : 82580360 : return is_nested_namespace (root, child);
5533 : : }
5534 : :
5535 : : /* Enter the class or namespace scope indicated by T suitable for name
5536 : : lookup. T can be arbitrary scope, not necessary nested inside the
5537 : : current scope. Returns a non-null scope to pop iff pop_scope
5538 : : should be called later to exit this scope. */
5539 : :
5540 : : tree
5541 : 302859291 : push_scope (tree t)
5542 : : {
5543 : 302859291 : if (TREE_CODE (t) == NAMESPACE_DECL)
5544 : 71883815 : push_decl_namespace (t);
5545 : 230975476 : else if (CLASS_TYPE_P (t))
5546 : : {
5547 : 168184724 : if (!at_class_scope_p ()
5548 : 168184724 : || !same_type_p (current_class_type, t))
5549 : 78226113 : push_nested_class (t);
5550 : : else
5551 : : /* T is the same as the current scope. There is therefore no
5552 : : need to re-enter the scope. Since we are not actually
5553 : : pushing a new scope, our caller should not call
5554 : : pop_scope. */
5555 : : t = NULL_TREE;
5556 : : }
5557 : :
5558 : 302859291 : return t;
5559 : : }
5560 : :
5561 : : /* Leave scope pushed by push_scope. */
5562 : :
5563 : : void
5564 : 212901177 : pop_scope (tree t)
5565 : : {
5566 : 212901177 : if (t == NULL_TREE)
5567 : : return;
5568 : 212900677 : if (TREE_CODE (t) == NAMESPACE_DECL)
5569 : 71883812 : pop_decl_namespace ();
5570 : 141016865 : else if CLASS_TYPE_P (t)
5571 : 78226113 : pop_nested_class ();
5572 : : }
5573 : :
5574 : : /* Subroutine of push_inner_scope. */
5575 : :
5576 : : static void
5577 : 272646 : push_inner_scope_r (tree outer, tree inner)
5578 : : {
5579 : 272646 : tree prev;
5580 : :
5581 : 272646 : if (outer == inner
5582 : 272646 : || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5583 : : return;
5584 : :
5585 : 272637 : prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5586 : 272637 : if (outer != prev)
5587 : 3055 : push_inner_scope_r (outer, prev);
5588 : 272637 : if (TREE_CODE (inner) == NAMESPACE_DECL)
5589 : : {
5590 : : cp_binding_level *save_template_parm = 0;
5591 : : /* Temporary take out template parameter scopes. They are saved
5592 : : in reversed order in save_template_parm. */
5593 : 62401 : while (current_binding_level->kind == sk_template_parms)
5594 : : {
5595 : 31133 : cp_binding_level *b = current_binding_level;
5596 : 31133 : current_binding_level = b->level_chain;
5597 : 31133 : b->level_chain = save_template_parm;
5598 : 31133 : save_template_parm = b;
5599 : : }
5600 : :
5601 : 31268 : resume_scope (NAMESPACE_LEVEL (inner));
5602 : 31268 : current_namespace = inner;
5603 : :
5604 : : /* Restore template parameter scopes. */
5605 : 62401 : while (save_template_parm)
5606 : : {
5607 : 31133 : cp_binding_level *b = save_template_parm;
5608 : 31133 : save_template_parm = b->level_chain;
5609 : 31133 : b->level_chain = current_binding_level;
5610 : 31133 : current_binding_level = b;
5611 : : }
5612 : : }
5613 : : else
5614 : 241369 : pushclass (inner);
5615 : : }
5616 : :
5617 : : /* Enter the scope INNER from current scope. INNER must be a scope
5618 : : nested inside current scope. This works with both name lookup and
5619 : : pushing name into scope. In case a template parameter scope is present,
5620 : : namespace is pushed under the template parameter scope according to
5621 : : name lookup rule in 14.6.1/6.
5622 : :
5623 : : Return the former current scope suitable for pop_inner_scope. */
5624 : :
5625 : : tree
5626 : 269591 : push_inner_scope (tree inner)
5627 : : {
5628 : 269591 : tree outer = current_scope ();
5629 : 269591 : if (!outer)
5630 : 0 : outer = current_namespace;
5631 : :
5632 : 269591 : push_inner_scope_r (outer, inner);
5633 : 269591 : return outer;
5634 : : }
5635 : :
5636 : : /* Exit the current scope INNER back to scope OUTER. */
5637 : :
5638 : : void
5639 : 269591 : pop_inner_scope (tree outer, tree inner)
5640 : : {
5641 : 269591 : if (outer == inner
5642 : 269591 : || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
5643 : : return;
5644 : :
5645 : 542222 : while (outer != inner)
5646 : : {
5647 : 272640 : if (TREE_CODE (inner) == NAMESPACE_DECL)
5648 : : {
5649 : : cp_binding_level *save_template_parm = 0;
5650 : : /* Temporary take out template parameter scopes. They are saved
5651 : : in reversed order in save_template_parm. */
5652 : 62401 : while (current_binding_level->kind == sk_template_parms)
5653 : : {
5654 : 31133 : cp_binding_level *b = current_binding_level;
5655 : 31133 : current_binding_level = b->level_chain;
5656 : 31133 : b->level_chain = save_template_parm;
5657 : 31133 : save_template_parm = b;
5658 : : }
5659 : :
5660 : 31268 : pop_namespace ();
5661 : :
5662 : : /* Restore template parameter scopes. */
5663 : 93669 : while (save_template_parm)
5664 : : {
5665 : 31133 : cp_binding_level *b = save_template_parm;
5666 : 31133 : save_template_parm = b->level_chain;
5667 : 31133 : b->level_chain = current_binding_level;
5668 : 31133 : current_binding_level = b;
5669 : : }
5670 : : }
5671 : : else
5672 : 241372 : popclass ();
5673 : :
5674 : 272637 : inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
5675 : : }
5676 : : }
5677 : :
5678 : : /* Do a pushlevel for class declarations. */
5679 : :
5680 : : void
5681 : 184395786 : pushlevel_class (void)
5682 : : {
5683 : 184395786 : class_binding_level = begin_scope (sk_class, current_class_type);
5684 : 184395786 : }
5685 : :
5686 : : /* ...and a poplevel for class declarations. */
5687 : :
5688 : : void
5689 : 361080580 : poplevel_class (void)
5690 : : {
5691 : 361080580 : cp_binding_level *level = class_binding_level;
5692 : 361080580 : cp_class_binding *cb;
5693 : 361080580 : size_t i;
5694 : 361080580 : tree shadowed;
5695 : :
5696 : 361080580 : auto_cond_timevar tv (TV_NAME_LOOKUP);
5697 : 361080580 : gcc_assert (level != 0);
5698 : :
5699 : : /* If we're leaving a toplevel class, cache its binding level. */
5700 : 361080577 : if (current_class_depth == 1)
5701 : 231282195 : previous_class_level = level;
5702 : 361080577 : for (shadowed = level->type_shadowed;
5703 : 1266253697 : shadowed;
5704 : 905173120 : shadowed = TREE_CHAIN (shadowed))
5705 : 905173120 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
5706 : :
5707 : : /* Remove the bindings for all of the class-level declarations. */
5708 : 361080577 : if (level->class_shadowed)
5709 : : {
5710 : 536039480 : FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
5711 : : {
5712 : 410979473 : IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
5713 : 410979473 : cxx_binding_free (cb->base);
5714 : : }
5715 : 125060007 : ggc_free (level->class_shadowed);
5716 : 125060007 : level->class_shadowed = NULL;
5717 : : }
5718 : :
5719 : : /* Now, pop out of the binding level which we created up in the
5720 : : `pushlevel_class' routine. */
5721 : 361080577 : gcc_assert (current_binding_level == level);
5722 : 361080577 : leave_scope ();
5723 : 361080577 : }
5724 : :
5725 : : /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5726 : : appropriate. DECL is the value to which a name has just been
5727 : : bound. CLASS_TYPE is the class in which the lookup occurred. */
5728 : :
5729 : : static void
5730 : 139618755 : set_inherited_value_binding_p (cxx_binding *binding, tree decl,
5731 : : tree class_type)
5732 : : {
5733 : 139618755 : if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
5734 : : {
5735 : 139254860 : tree context;
5736 : :
5737 : 139254860 : if (is_overloaded_fn (decl))
5738 : 40364271 : context = ovl_scope (decl);
5739 : : else
5740 : : {
5741 : 98890589 : gcc_assert (DECL_P (decl));
5742 : 98890589 : context = context_for_name_lookup (decl);
5743 : : }
5744 : :
5745 : 139254860 : if (is_properly_derived_from (class_type, context))
5746 : 16773870 : INHERITED_VALUE_BINDING_P (binding) = 1;
5747 : : else
5748 : 122480990 : INHERITED_VALUE_BINDING_P (binding) = 0;
5749 : : }
5750 : 363895 : else if (binding->value == decl)
5751 : : /* We only encounter a TREE_LIST when there is an ambiguity in the
5752 : : base classes. Such an ambiguity can be overridden by a
5753 : : definition in this class. */
5754 : 363895 : INHERITED_VALUE_BINDING_P (binding) = 1;
5755 : : else
5756 : 0 : INHERITED_VALUE_BINDING_P (binding) = 0;
5757 : 139618755 : }
5758 : :
5759 : : /* Make the declaration of X appear in CLASS scope. */
5760 : :
5761 : : bool
5762 : 156548058 : pushdecl_class_level (tree x)
5763 : : {
5764 : 156548058 : bool is_valid = true;
5765 : :
5766 : : /* Do nothing if we're adding to an outer lambda closure type,
5767 : : outer_binding will add it later if it's needed. */
5768 : 156548058 : if (current_class_type != class_binding_level->this_entity)
5769 : : return true;
5770 : :
5771 : 156548058 : auto_cond_timevar tv (TV_NAME_LOOKUP);
5772 : : /* Get the name of X. */
5773 : 313096116 : tree name = OVL_NAME (x);
5774 : :
5775 : 156548058 : if (name)
5776 : : {
5777 : 156247819 : is_valid = push_class_level_binding (name, x);
5778 : 156247819 : if (TREE_CODE (x) == TYPE_DECL)
5779 : 111395246 : set_identifier_type_value (name, x);
5780 : : }
5781 : 300239 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
5782 : : {
5783 : : /* If X is an anonymous aggregate, all of its members are
5784 : : treated as if they were members of the class containing the
5785 : : aggregate, for naming purposes. */
5786 : 165769 : location_t save_location = input_location;
5787 : 165769 : tree anon = TREE_TYPE (x);
5788 : 165769 : if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
5789 : 250503 : for (unsigned ix = member_vec->length (); ix--;)
5790 : : {
5791 : 233441 : tree binding = (*member_vec)[ix];
5792 : 233441 : if (STAT_HACK_P (binding))
5793 : : {
5794 : 0 : if (!pushdecl_class_level (STAT_TYPE (binding)))
5795 : 0 : is_valid = false;
5796 : 0 : binding = STAT_DECL (binding);
5797 : : }
5798 : 233441 : if (!pushdecl_class_level (binding))
5799 : 0 : is_valid = false;
5800 : : }
5801 : : else
5802 : 651977 : for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
5803 : 503270 : if (TREE_CODE (f) == FIELD_DECL)
5804 : : {
5805 : 319854 : input_location = DECL_SOURCE_LOCATION (f);
5806 : 319854 : if (!pushdecl_class_level (f))
5807 : 503270 : is_valid = false;
5808 : : }
5809 : 165769 : input_location = save_location;
5810 : : }
5811 : 156548058 : return is_valid;
5812 : 156548058 : }
5813 : :
5814 : : /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5815 : : scope. If the value returned is non-NULL, and the PREVIOUS field
5816 : : is not set, callers must set the PREVIOUS field explicitly. */
5817 : :
5818 : : static cxx_binding *
5819 : 1745650085 : get_class_binding (tree name, cp_binding_level *scope)
5820 : : {
5821 : 1745650085 : tree class_type;
5822 : 1745650085 : tree type_binding;
5823 : 1745650085 : tree value_binding;
5824 : 1745650085 : cxx_binding *binding;
5825 : :
5826 : 1745650085 : class_type = scope->this_entity;
5827 : :
5828 : : /* Get the type binding. */
5829 : 1745650085 : type_binding = lookup_member (class_type, name,
5830 : : /*protect=*/2, /*want_type=*/true,
5831 : : tf_warning_or_error);
5832 : : /* Get the value binding. */
5833 : 1745650085 : value_binding = lookup_member (class_type, name,
5834 : : /*protect=*/2, /*want_type=*/false,
5835 : : tf_warning_or_error);
5836 : :
5837 : : /* If we found either a type binding or a value binding, create a
5838 : : new binding object. */
5839 : 1745650085 : if (type_binding || value_binding)
5840 : : {
5841 : 139618755 : binding = new_class_binding (name,
5842 : : value_binding,
5843 : : type_binding,
5844 : : scope);
5845 : 139618755 : set_inherited_value_binding_p (binding, value_binding, class_type);
5846 : : }
5847 : : else
5848 : : binding = NULL;
5849 : :
5850 : 1745650085 : return binding;
5851 : : }
5852 : :
5853 : : /* Make the declaration(s) of X appear in CLASS scope under the name
5854 : : NAME. Returns true if the binding is valid. */
5855 : :
5856 : : bool
5857 : 395979898 : push_class_level_binding (tree name, tree x)
5858 : : {
5859 : 395979898 : cxx_binding *binding;
5860 : 395979898 : tree decl = x;
5861 : 395979898 : bool ok;
5862 : :
5863 : 395979898 : auto_cond_timevar tv (TV_NAME_LOOKUP);
5864 : :
5865 : : /* The class_binding_level will be NULL if x is a template
5866 : : parameter name in a member template. */
5867 : 395979898 : if (!class_binding_level)
5868 : : return true;
5869 : :
5870 : 395979898 : if (name == error_mark_node)
5871 : : return false;
5872 : :
5873 : : /* Can happen for an erroneous declaration (c++/60384). */
5874 : 395979898 : if (!identifier_p (name))
5875 : : {
5876 : 3 : gcc_assert (errorcount || sorrycount);
5877 : : return false;
5878 : : }
5879 : :
5880 : : /* Check for invalid member names. But don't worry about a default
5881 : : argument-scope lambda being pushed after the class is complete. */
5882 : 395979916 : gcc_assert (TYPE_BEING_DEFINED (current_class_type)
5883 : : || LAMBDA_TYPE_P (TREE_TYPE (decl)));
5884 : : /* Check that we're pushing into the right binding level. */
5885 : 395979895 : gcc_assert (current_class_type == class_binding_level->this_entity);
5886 : :
5887 : : /* We could have been passed a tree list if this is an ambiguous
5888 : : declaration. If so, pull the declaration out because
5889 : : check_template_shadow will not handle a TREE_LIST. */
5890 : 395979895 : if (TREE_CODE (decl) == TREE_LIST
5891 : 395979895 : && TREE_TYPE (decl) == error_mark_node)
5892 : 0 : decl = TREE_VALUE (decl);
5893 : :
5894 : 395979895 : if (!check_template_shadow (decl))
5895 : : return false;
5896 : :
5897 : : /* [class.mem]
5898 : :
5899 : : If T is the name of a class, then each of the following shall
5900 : : have a name different from T:
5901 : :
5902 : : -- every static data member of class T;
5903 : :
5904 : : -- every member of class T that is itself a type;
5905 : :
5906 : : -- every enumerator of every member of class T that is an
5907 : : enumerated type;
5908 : :
5909 : : -- every member of every anonymous union that is a member of
5910 : : class T.
5911 : :
5912 : : (Non-static data members were also forbidden to have the same
5913 : : name as T until TC1.) */
5914 : 395979847 : if ((VAR_P (x)
5915 : 395979847 : || TREE_CODE (x) == CONST_DECL
5916 : 381523074 : || (TREE_CODE (x) == TYPE_DECL
5917 : 111395225 : && !DECL_SELF_REFERENCE_P (x))
5918 : : /* A data member of an anonymous union. */
5919 : 323402778 : || (TREE_CODE (x) == FIELD_DECL
5920 : 23682210 : && DECL_CONTEXT (x) != current_class_type))
5921 : 454614523 : && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
5922 : : {
5923 : 24 : tree scope = context_for_name_lookup (x);
5924 : 24 : if (TYPE_P (scope) && same_type_p (scope, current_class_type))
5925 : : {
5926 : 24 : error_at (DECL_SOURCE_LOCATION (x),
5927 : : "%qD has the same name as the class in which it is "
5928 : : "declared", x);
5929 : 24 : return false;
5930 : : }
5931 : : }
5932 : :
5933 : : /* Get the current binding for NAME in this class, if any. */
5934 : 395979823 : binding = IDENTIFIER_BINDING (name);
5935 : 395979823 : if (!binding || binding->scope != class_binding_level)
5936 : : {
5937 : 282082261 : binding = get_class_binding (name, class_binding_level);
5938 : : /* If a new binding was created, put it at the front of the
5939 : : IDENTIFIER_BINDING list. */
5940 : 282082261 : if (binding)
5941 : : {
5942 : 10718702 : binding->previous = IDENTIFIER_BINDING (name);
5943 : 10718702 : IDENTIFIER_BINDING (name) = binding;
5944 : : }
5945 : : }
5946 : :
5947 : : /* If there is already a binding, then we may need to update the
5948 : : current value. */
5949 : 124616264 : if (binding && binding->value)
5950 : : {
5951 : 124616264 : tree bval = binding->value;
5952 : 124616264 : tree old_decl = NULL_TREE;
5953 : 124616264 : tree target_decl = strip_using_decl (decl);
5954 : 124616264 : tree target_bval = strip_using_decl (bval);
5955 : :
5956 : 124616264 : if (INHERITED_VALUE_BINDING_P (binding))
5957 : : {
5958 : : /* If the old binding was from a base class, and was for a
5959 : : tag name, slide it over to make room for the new binding.
5960 : : The old binding is still visible if explicitly qualified
5961 : : with a class-key. */
5962 : 12905350 : if (TREE_CODE (target_bval) == TYPE_DECL
5963 : 6498646 : && DECL_ARTIFICIAL (target_bval)
5964 : 13450673 : && !(TREE_CODE (target_decl) == TYPE_DECL
5965 : 545313 : && DECL_ARTIFICIAL (target_decl)))
5966 : : {
5967 : 68188 : old_decl = binding->type;
5968 : 68188 : binding->type = bval;
5969 : 68188 : binding->value = NULL_TREE;
5970 : 68188 : INHERITED_VALUE_BINDING_P (binding) = 0;
5971 : : }
5972 : : else
5973 : : {
5974 : 12837162 : old_decl = bval;
5975 : : /* Any inherited type declaration is hidden by the type
5976 : : declaration in the derived class. */
5977 : 12837162 : if (TREE_CODE (target_decl) == TYPE_DECL
5978 : 12837162 : && DECL_ARTIFICIAL (target_decl))
5979 : 477644 : binding->type = NULL_TREE;
5980 : : }
5981 : : }
5982 : 111710914 : else if (TREE_CODE (decl) == USING_DECL
5983 : 4008 : && TREE_CODE (bval) == USING_DECL
5984 : 111711020 : && same_type_p (USING_DECL_SCOPE (decl),
5985 : : USING_DECL_SCOPE (bval)))
5986 : : /* This is a using redeclaration that will be diagnosed later
5987 : : in supplement_binding */
5988 : : ;
5989 : 111710881 : else if (TREE_CODE (decl) == USING_DECL
5990 : 3975 : && TREE_CODE (bval) == USING_DECL
5991 : 73 : && DECL_DEPENDENT_P (decl)
5992 : 111710900 : && DECL_DEPENDENT_P (bval))
5993 : : return true;
5994 : 111710862 : else if (TREE_CODE (decl) == USING_DECL
5995 : 3956 : && DECL_DEPENDENT_P (decl)
5996 : 111714576 : && OVL_P (target_bval))
5997 : : /* The new dependent using beats an old overload. */
5998 : : old_decl = bval;
5999 : 111707148 : else if (TREE_CODE (bval) == USING_DECL
6000 : 771629 : && DECL_DEPENDENT_P (bval)
6001 : 112064433 : && OVL_P (target_decl))
6002 : : /* The old dependent using beats a new overload. */
6003 : : return true;
6004 : 111349875 : else if (OVL_P (target_decl)
6005 : 110972291 : && OVL_P (target_bval))
6006 : : /* The new overload set contains the old one. */
6007 : : old_decl = bval;
6008 : :
6009 : 123881331 : if (old_decl && binding->scope == class_binding_level)
6010 : : {
6011 : 123881331 : binding->value = x;
6012 : : /* It is always safe to clear INHERITED_VALUE_BINDING_P
6013 : : here. This function is only used to register bindings
6014 : : from with the class definition itself. */
6015 : 123881331 : INHERITED_VALUE_BINDING_P (binding) = 0;
6016 : 123881331 : return true;
6017 : : }
6018 : : }
6019 : :
6020 : : /* Note that we declared this value so that we can issue an error if
6021 : : this is an invalid redeclaration of a name already used for some
6022 : : other purpose. */
6023 : 271741200 : note_name_declared_in_class (name, decl);
6024 : :
6025 : : /* If we didn't replace an existing binding, put the binding on the
6026 : : stack of bindings for the identifier, and update the shadowed
6027 : : list. */
6028 : 271741200 : if (binding && binding->scope == class_binding_level)
6029 : : /* Supplement the existing binding. */
6030 : 377641 : ok = supplement_binding (binding, decl);
6031 : : else
6032 : : {
6033 : : /* Create a new binding. */
6034 : 271363559 : push_binding (name, decl, class_binding_level);
6035 : 271363559 : ok = true;
6036 : : }
6037 : :
6038 : : return ok;
6039 : 395979898 : }
6040 : :
6041 : : /* Process and lookup a using decl SCOPE::lookup.name, filling in
6042 : : lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
6043 : : failure. */
6044 : :
6045 : : static tree
6046 : 12482256 : lookup_using_decl (tree scope, name_lookup &lookup)
6047 : : {
6048 : 12482256 : tree current = current_scope ();
6049 : 12482256 : bool dependent_p = false;
6050 : 12482256 : tree binfo = NULL_TREE;
6051 : 12482256 : base_kind b_kind = bk_not_base;
6052 : :
6053 : : /* Because C++20 breaks the invariant that only member using-decls
6054 : : refer to members and only non-member using-decls refer to
6055 : : non-members, we first do the lookups, and then do validation that
6056 : : what we found is ok. */
6057 : :
6058 : 12482256 : if (TREE_CODE (scope) == ENUMERAL_TYPE
6059 : 3472742 : && cxx_dialect < cxx20
6060 : 3472742 : && UNSCOPED_ENUM_P (scope)
6061 : 12482274 : && !TYPE_FUNCTION_SCOPE_P (scope))
6062 : : {
6063 : : /* PR c++/60265 argued that since C++11 added explicit enum scope, we
6064 : : should allow it as meaning the enclosing scope. I don't see any
6065 : : justification for this in C++11, but let's keep allowing it. */
6066 : 16 : tree ctx = CP_TYPE_CONTEXT (scope);
6067 : 42 : if (CLASS_TYPE_P (ctx) == CLASS_TYPE_P (current))
6068 : 12482256 : scope = ctx;
6069 : : }
6070 : :
6071 : : /* You cannot using-decl a destructor. */
6072 : 12482256 : if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
6073 : : {
6074 : 6 : error ("%<%T%s%D%> names destructor", scope,
6075 : 3 : &"::"[scope == global_namespace ? 2 : 0], lookup.name);
6076 : 3 : return NULL_TREE;
6077 : : }
6078 : :
6079 : 12482253 : if (TREE_CODE (scope) == NAMESPACE_DECL)
6080 : : {
6081 : : /* Naming a namespace member. */
6082 : 6488203 : qualified_namespace_lookup (scope, &lookup);
6083 : :
6084 : 6488203 : if (TYPE_P (current)
6085 : 3 : && (!lookup.value
6086 : 0 : || lookup.type
6087 : 0 : || cxx_dialect < cxx20
6088 : 0 : || TREE_CODE (lookup.value) != CONST_DECL))
6089 : : {
6090 : 3 : error ("using-declaration for non-member at class scope");
6091 : 3 : return NULL_TREE;
6092 : : }
6093 : : }
6094 : 5994050 : else if (TREE_CODE (scope) == ENUMERAL_TYPE)
6095 : : {
6096 : : /* Naming an enumeration member. */
6097 : 3472727 : if (cxx_dialect < cxx20)
6098 : 12 : error ("%<using%> with enumeration scope %q#T "
6099 : : "only available with %<-std=c++20%> or %<-std=gnu++20%>",
6100 : : scope);
6101 : 3472727 : lookup.value = lookup_enumerator (scope, lookup.name);
6102 : : }
6103 : : else
6104 : : {
6105 : : /* Naming a class member. This is awkward in C++20, because we
6106 : : might be naming an enumerator of an unrelated class. */
6107 : :
6108 : 2521323 : tree npscope = scope;
6109 : 2521323 : if (PACK_EXPANSION_P (scope))
6110 : 9639 : npscope = PACK_EXPANSION_PATTERN (scope);
6111 : :
6112 : 2521323 : if (!MAYBE_CLASS_TYPE_P (npscope))
6113 : : {
6114 : 9 : error ("%qT is not a class, namespace, or enumeration", npscope);
6115 : 9 : return NULL_TREE;
6116 : : }
6117 : :
6118 : : /* Using T::T declares inheriting ctors, even if T is a typedef. */
6119 : 2521314 : if (lookup.name == TYPE_IDENTIFIER (npscope)
6120 : 2521314 : || constructor_name_p (lookup.name, npscope))
6121 : : {
6122 : 225286 : if (!TYPE_P (current))
6123 : : {
6124 : 0 : error ("non-member using-declaration names constructor of %qT",
6125 : : npscope);
6126 : 0 : return NULL_TREE;
6127 : : }
6128 : 225286 : maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
6129 : 225286 : lookup.name = ctor_identifier;
6130 : 225286 : CLASSTYPE_NON_AGGREGATE (current) = true;
6131 : : }
6132 : :
6133 : 2521314 : if (!TYPE_P (current) && cxx_dialect < cxx20)
6134 : : {
6135 : 4 : error ("using-declaration for member at non-class scope");
6136 : 4 : return NULL_TREE;
6137 : : }
6138 : :
6139 : 2521310 : bool depscope = dependent_scope_p (scope);
6140 : :
6141 : 2521310 : if (depscope)
6142 : : /* Leave binfo null. */;
6143 : 1679011 : else if (TYPE_P (current))
6144 : : {
6145 : 1678998 : binfo = lookup_base (current, scope, ba_any, &b_kind, tf_none);
6146 : 1678998 : gcc_checking_assert (b_kind >= bk_not_base);
6147 : :
6148 : 1678998 : if (b_kind == bk_not_base && any_dependent_bases_p ())
6149 : : /* Treat as-if dependent. */
6150 : : depscope = true;
6151 : 1678977 : else if (lookup.name == ctor_identifier
6152 : 1678977 : && (b_kind < bk_proper_base || !binfo_direct_p (binfo)))
6153 : : {
6154 : 15 : if (any_dependent_bases_p ())
6155 : : depscope = true;
6156 : : else
6157 : : {
6158 : 15 : error ("%qT is not a direct base of %qT", scope, current);
6159 : 15 : return NULL_TREE;
6160 : : }
6161 : : }
6162 : :
6163 : 1678983 : if (b_kind < bk_proper_base)
6164 : 50 : binfo = TYPE_BINFO (scope);
6165 : : }
6166 : : else
6167 : 13 : binfo = TYPE_BINFO (scope);
6168 : :
6169 : 3357965 : dependent_p = (depscope
6170 : 1678996 : || (IDENTIFIER_CONV_OP_P (lookup.name)
6171 : 138686 : && dependent_type_p (TREE_TYPE (lookup.name))));
6172 : :
6173 : 1678969 : if (!dependent_p)
6174 : 1678969 : lookup.value = lookup_member (binfo, lookup.name, /*protect=*/2,
6175 : : /*want_type=*/false, tf_none);
6176 : :
6177 : : /* If the lookup in the base contains a dependent using, this
6178 : : using is also dependent. */
6179 : 1678969 : if (!dependent_p && lookup.value && dependent_type_p (scope))
6180 : : {
6181 : 27 : tree val = lookup.value;
6182 : 27 : if (tree fns = maybe_get_fns (val))
6183 : 9 : val = fns;
6184 : 75 : for (tree f: lkp_range (val))
6185 : 27 : if (TREE_CODE (f) == USING_DECL && DECL_DEPENDENT_P (f))
6186 : : {
6187 : : dependent_p = true;
6188 : : break;
6189 : : }
6190 : : }
6191 : :
6192 : 2521295 : if (!depscope && b_kind < bk_proper_base)
6193 : : {
6194 : 42 : if (cxx_dialect >= cxx20 && lookup.value
6195 : 23 : && TREE_CODE (lookup.value) == CONST_DECL)
6196 : : {
6197 : : /* Using an unrelated enum; check access here rather
6198 : : than separately for class and non-class using. */
6199 : 15 : perform_or_defer_access_check
6200 : 15 : (binfo, lookup.value, lookup.value, tf_warning_or_error);
6201 : : /* And then if this is a copy from handle_using_decl, look
6202 : : through to the original enumerator. */
6203 : 15 : if (CONST_DECL_USING_P (lookup.value))
6204 : 6 : lookup.value = DECL_ABSTRACT_ORIGIN (lookup.value);
6205 : : }
6206 : 27 : else if (!TYPE_P (current))
6207 : : {
6208 : 1 : error ("using-declaration for member at non-class scope");
6209 : 1 : return NULL_TREE;
6210 : : }
6211 : : else
6212 : : {
6213 : 26 : auto_diagnostic_group g;
6214 : 26 : error_not_base_type (scope, current);
6215 : 17 : if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)
6216 : 29 : && TREE_CODE (TREE_TYPE (lookup.value)) == ENUMERAL_TYPE)
6217 : 3 : inform (input_location,
6218 : : "did you mean %<using enum %T::%D%>?",
6219 : : scope, lookup.name);
6220 : 26 : return NULL_TREE;
6221 : 26 : }
6222 : : }
6223 : : }
6224 : :
6225 : : /* Did we find anything sane? */
6226 : 5993995 : if (dependent_p)
6227 : : ;
6228 : 11639863 : else if (!lookup.value)
6229 : : {
6230 : 59 : error ("%qD has not been declared in %qD", lookup.name, scope);
6231 : 59 : return NULL_TREE;
6232 : : }
6233 : 11639804 : else if (TREE_CODE (lookup.value) == TREE_LIST
6234 : : /* We can (independently) have ambiguous implicit typedefs. */
6235 : 11639804 : || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
6236 : : {
6237 : 3 : auto_diagnostic_group d;
6238 : 3 : error ("reference to %qD is ambiguous", lookup.name);
6239 : 3 : print_candidates (TREE_CODE (lookup.value) == TREE_LIST
6240 : : ? lookup.value : lookup.type);
6241 : 3 : return NULL_TREE;
6242 : 3 : }
6243 : 11639801 : else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
6244 : : {
6245 : 6 : error ("using-declaration may not name namespace %qD", lookup.value);
6246 : 6 : return NULL_TREE;
6247 : : }
6248 : :
6249 : 12482127 : if (TYPE_P (current))
6250 : : {
6251 : : /* In class scope. */
6252 : :
6253 : : /* Cannot introduce a constructor name. */
6254 : 2881213 : if (constructor_name_p (lookup.name, current))
6255 : : {
6256 : 3 : error ("%<%T::%D%> names constructor in %qT",
6257 : : scope, lookup.name, current);
6258 : 3 : return NULL_TREE;
6259 : : }
6260 : :
6261 : 2881210 : if (lookup.value && BASELINK_P (lookup.value))
6262 : : /* The binfo from which the functions came does not matter. */
6263 : 1510261 : lookup.value = BASELINK_FUNCTIONS (lookup.value);
6264 : : }
6265 : :
6266 : 12482124 : tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
6267 : 12482124 : USING_DECL_SCOPE (using_decl) = scope;
6268 : 12482124 : USING_DECL_DECLS (using_decl) = lookup.value;
6269 : 12482124 : DECL_DEPENDENT_P (using_decl) = dependent_p;
6270 : 12482124 : DECL_CONTEXT (using_decl) = current;
6271 : 12482124 : if (TYPE_P (current) && b_kind == bk_not_base)
6272 : 1202310 : USING_DECL_UNRELATED_P (using_decl) = true;
6273 : :
6274 : : return using_decl;
6275 : : }
6276 : :
6277 : : /* Process "using SCOPE::NAME" in a class scope. Return the
6278 : : USING_DECL created. */
6279 : :
6280 : : tree
6281 : 2881302 : do_class_using_decl (tree scope, tree name)
6282 : : {
6283 : 2881302 : if (name == error_mark_node
6284 : 2881299 : || scope == error_mark_node)
6285 : : return NULL_TREE;
6286 : :
6287 : 2881296 : name_lookup lookup (name);
6288 : 2881296 : return lookup_using_decl (scope, lookup);
6289 : 2881296 : }
6290 : :
6291 : :
6292 : : /* Return the binding for NAME in NS in the current TU. If NS is
6293 : : NULL, look in global_namespace. We will not find declarations
6294 : : from imports. Users of this who, having found nothing, push a new
6295 : : decl must be prepared for that pushing to match an existing decl. */
6296 : :
6297 : : tree
6298 : 10884651 : get_namespace_binding (tree ns, tree name)
6299 : : {
6300 : 10884651 : auto_cond_timevar tv (TV_NAME_LOOKUP);
6301 : 10884651 : if (!ns)
6302 : 10884651 : ns = global_namespace;
6303 : 10884651 : gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
6304 : 10884651 : tree ret = NULL_TREE;
6305 : :
6306 : 10884651 : if (tree *b = find_namespace_slot (ns, name))
6307 : : {
6308 : 5121659 : ret = *b;
6309 : :
6310 : 5121659 : if (TREE_CODE (ret) == BINDING_VECTOR)
6311 : 0 : ret = BINDING_VECTOR_CLUSTER (ret, 0).slots[0];
6312 : 0 : if (ret)
6313 : 5121659 : ret = strip_using_decl (MAYBE_STAT_DECL (ret));
6314 : : }
6315 : :
6316 : 21769302 : return ret;
6317 : 10884651 : }
6318 : :
6319 : : /* Push internal DECL into the global namespace. Does not do the
6320 : : full overload fn handling and does not add it to the list of things
6321 : : in the namespace. */
6322 : :
6323 : : void
6324 : 3628702 : set_global_binding (tree decl)
6325 : : {
6326 : 3628702 : auto_cond_timevar tv (TV_NAME_LOOKUP);
6327 : :
6328 : 3628702 : tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
6329 : :
6330 : 3628702 : if (*slot)
6331 : : /* The user's placed something in the implementor's namespace. */
6332 : 0 : diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
6333 : :
6334 : : /* Force the binding, so compiler internals continue to work. */
6335 : 3628702 : *slot = decl;
6336 : 3628702 : }
6337 : :
6338 : : /* Set the context of a declaration to scope. Complain if we are not
6339 : : outside scope. */
6340 : :
6341 : : void
6342 : 151304 : set_decl_namespace (tree decl, tree scope, bool friendp)
6343 : : {
6344 : : /* Get rid of namespace aliases. */
6345 : 151304 : scope = ORIGINAL_NAMESPACE (scope);
6346 : :
6347 : : /* It is ok for friends to be qualified in parallel space. */
6348 : 151304 : if (!friendp && !is_nested_namespace (current_namespace, scope))
6349 : 6 : error ("declaration of %qD not in a namespace surrounding %qD",
6350 : : decl, scope);
6351 : 151304 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6352 : :
6353 : : /* See whether this has been declared in the namespace or inline
6354 : : children. */
6355 : 151304 : tree old = NULL_TREE;
6356 : 151304 : {
6357 : 151304 : name_lookup lookup (DECL_NAME (decl),
6358 : 151304 : LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
6359 : 151304 : if (!lookup.search_qualified (scope, /*usings=*/false))
6360 : : /* No old declaration at all. */
6361 : 30 : goto not_found;
6362 : 151274 : old = lookup.value;
6363 : 151304 : }
6364 : :
6365 : : /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
6366 : 151274 : if (TREE_CODE (old) == TREE_LIST)
6367 : : {
6368 : 9 : ambiguous:
6369 : 15 : auto_diagnostic_group d;
6370 : 15 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6371 : 15 : error ("reference to %qD is ambiguous", decl);
6372 : 15 : print_candidates (old);
6373 : 15 : return;
6374 : : }
6375 : :
6376 : 151265 : if (!DECL_DECLARES_FUNCTION_P (decl))
6377 : : {
6378 : : /* Don't compare non-function decls with decls_match here, since
6379 : : it can't check for the correct constness at this
6380 : : point. pushdecl will find those errors later. */
6381 : :
6382 : : /* We might have found it in an inline namespace child of SCOPE. */
6383 : 4375 : if (TREE_CODE (decl) == TREE_CODE (old))
6384 : 35 : DECL_CONTEXT (decl) = DECL_CONTEXT (old);
6385 : :
6386 : 4340 : found:
6387 : : /* Writing "N::i" to declare something directly in "N" is invalid. */
6388 : 47701 : if (CP_DECL_CONTEXT (decl) == current_namespace
6389 : 47701 : && at_namespace_scope_p ())
6390 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6391 : : "explicit qualification in declaration of %qD", decl);
6392 : 47701 : return;
6393 : : }
6394 : :
6395 : : /* Since decl is a function, old should contain a function decl. */
6396 : 146890 : if (!OVL_P (old))
6397 : : {
6398 : 9 : not_found:
6399 : : /* It didn't work, go back to the explicit scope. */
6400 : 45 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6401 : 45 : error ("%qD should have been declared inside %qD", decl, scope);
6402 : :
6403 : 45 : return;
6404 : : }
6405 : :
6406 : : /* We handle these in check_explicit_instantiation_namespace. */
6407 : 146881 : if (processing_explicit_instantiation)
6408 : : return;
6409 : 146703 : if (processing_template_decl || processing_specialization)
6410 : : /* We have not yet called push_template_decl to turn a
6411 : : FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6412 : : match. But, we'll check later, when we construct the
6413 : : template. */
6414 : : return;
6415 : :
6416 : : /* Instantiations or specializations of templates may be declared as
6417 : : friends in any namespace. */
6418 : 43359 : if (friendp && DECL_USE_TEMPLATE (decl))
6419 : : return;
6420 : :
6421 : 43350 : tree found = NULL_TREE;
6422 : 43350 : bool hidden_p = false;
6423 : 43350 : bool saw_template = false;
6424 : :
6425 : 106652 : for (lkp_iterator iter (old); iter; ++iter)
6426 : : {
6427 : 63308 : if (iter.using_p ())
6428 : 0 : continue;
6429 : :
6430 : 63308 : tree ofn = *iter;
6431 : :
6432 : : /* Adjust DECL_CONTEXT first so decls_match will return true
6433 : : if DECL will match a declaration in an inline namespace. */
6434 : 63308 : DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
6435 : 63308 : if (decls_match (decl, ofn))
6436 : : {
6437 : 43338 : if (found)
6438 : : {
6439 : : /* We found more than one matching declaration. This
6440 : : can happen if we have two inline namespace children,
6441 : : each containing a suitable declaration. */
6442 : 6 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6443 : 6 : goto ambiguous;
6444 : : }
6445 : 43332 : found = ofn;
6446 : 43332 : hidden_p = iter.hidden_p ();
6447 : : }
6448 : 19970 : else if (TREE_CODE (decl) == FUNCTION_DECL
6449 : 19970 : && TREE_CODE (ofn) == TEMPLATE_DECL)
6450 : 63302 : saw_template = true;
6451 : : }
6452 : :
6453 : 43344 : if (!found && friendp && saw_template)
6454 : : {
6455 : : /* "[if no non-template match is found,] each remaining function template
6456 : : is replaced with the specialization chosen by deduction from the
6457 : : friend declaration or discarded if deduction fails."
6458 : :
6459 : : So tell check_explicit_specialization to look for a match. */
6460 : 12 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
6461 : 12 : DECL_TEMPLATE_INFO (decl) = build_template_info (old, NULL_TREE);
6462 : 12 : return;
6463 : : }
6464 : :
6465 : 43332 : if (found)
6466 : : {
6467 : 43326 : if (hidden_p)
6468 : : {
6469 : 6 : auto_diagnostic_group d;
6470 : 6 : pedwarn (DECL_SOURCE_LOCATION (decl), 0,
6471 : : "%qD has not been declared within %qD", decl, scope);
6472 : 6 : inform (DECL_SOURCE_LOCATION (found),
6473 : : "only here as a %<friend%>");
6474 : 6 : }
6475 : 43326 : DECL_CONTEXT (decl) = DECL_CONTEXT (found);
6476 : 43326 : goto found;
6477 : : }
6478 : :
6479 : 6 : goto not_found;
6480 : : }
6481 : :
6482 : : /* Return the namespace where the current declaration is declared. */
6483 : :
6484 : : tree
6485 : 1260703535 : current_decl_namespace (void)
6486 : : {
6487 : 1260703535 : tree result;
6488 : : /* If we have been pushed into a different namespace, use it. */
6489 : 1260703535 : if (!vec_safe_is_empty (decl_namespace_list))
6490 : 60573220 : return decl_namespace_list->last ();
6491 : :
6492 : 1200130315 : if (current_class_type)
6493 : 504654156 : result = decl_namespace_context (current_class_type);
6494 : 695476159 : else if (current_function_decl)
6495 : 253269740 : result = decl_namespace_context (current_function_decl);
6496 : : else
6497 : 442206419 : result = current_namespace;
6498 : : return result;
6499 : : }
6500 : :
6501 : : /* Process any ATTRIBUTES on a namespace definition. Returns true if
6502 : : attribute visibility is seen. */
6503 : :
6504 : : bool
6505 : 5386214 : handle_namespace_attrs (tree ns, tree attributes)
6506 : : {
6507 : 5386214 : tree d;
6508 : 5386214 : bool saw_vis = false;
6509 : :
6510 : 5386214 : if (attributes == error_mark_node)
6511 : : return false;
6512 : :
6513 : 8079202 : for (d = attributes; d; d = TREE_CHAIN (d))
6514 : : {
6515 : 2692988 : tree name = get_attribute_name (d);
6516 : 2692988 : tree args = TREE_VALUE (d);
6517 : :
6518 : 2692988 : if (is_attribute_p ("visibility", name))
6519 : : {
6520 : : /* attribute visibility is a property of the syntactic block
6521 : : rather than the namespace as a whole, so we don't touch the
6522 : : NAMESPACE_DECL at all. */
6523 : 2643733 : tree x = args ? TREE_VALUE (args) : NULL_TREE;
6524 : 2643733 : if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
6525 : : {
6526 : 0 : warning (OPT_Wattributes,
6527 : : "%qD attribute requires a single NTBS argument",
6528 : : name);
6529 : 0 : continue;
6530 : : }
6531 : :
6532 : 2643733 : if (!TREE_PUBLIC (ns))
6533 : 0 : warning (OPT_Wattributes,
6534 : : "%qD attribute is meaningless since members of the "
6535 : : "anonymous namespace get local symbols", name);
6536 : :
6537 : 2643733 : push_visibility (TREE_STRING_POINTER (x), 1);
6538 : 2643733 : saw_vis = true;
6539 : : }
6540 : 49255 : else if (is_attribute_p ("abi_tag", name))
6541 : : {
6542 : 46566 : if (!DECL_NAME (ns))
6543 : : {
6544 : 3 : warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6545 : : "namespace", name);
6546 : 3 : continue;
6547 : : }
6548 : 46563 : if (!DECL_NAMESPACE_INLINE_P (ns))
6549 : : {
6550 : 0 : warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
6551 : : "namespace", name);
6552 : 0 : continue;
6553 : : }
6554 : 46563 : if (!args)
6555 : : {
6556 : 18 : tree dn = DECL_NAME (ns);
6557 : 18 : args = build_string (IDENTIFIER_LENGTH (dn) + 1,
6558 : 18 : IDENTIFIER_POINTER (dn));
6559 : 18 : TREE_TYPE (args) = char_array_type_node;
6560 : 18 : args = fix_string_type (args);
6561 : 18 : args = build_tree_list (NULL_TREE, args);
6562 : : }
6563 : 46563 : if (check_abi_tag_args (args, name))
6564 : 46563 : DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6565 : 46563 : DECL_ATTRIBUTES (ns));
6566 : : }
6567 : 2689 : else if (is_attribute_p ("deprecated", name))
6568 : : {
6569 : 2614 : if (!DECL_NAME (ns))
6570 : : {
6571 : 7 : warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
6572 : : "namespace", name);
6573 : 7 : continue;
6574 : : }
6575 : 5150 : if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
6576 : : {
6577 : 0 : error ("deprecated message is not a string");
6578 : 0 : continue;
6579 : : }
6580 : 2607 : TREE_DEPRECATED (ns) = 1;
6581 : 2607 : if (args)
6582 : 2543 : DECL_ATTRIBUTES (ns) = tree_cons (name, args,
6583 : 2543 : DECL_ATTRIBUTES (ns));
6584 : : }
6585 : 75 : else if (!attribute_ignored_p (d))
6586 : : {
6587 : 60 : warning (OPT_Wattributes, "%qD attribute directive ignored",
6588 : : name);
6589 : 60 : continue;
6590 : : }
6591 : : }
6592 : :
6593 : : return saw_vis;
6594 : : }
6595 : :
6596 : : /* Temporarily set the namespace for the current declaration. */
6597 : :
6598 : : void
6599 : 71989104 : push_decl_namespace (tree decl)
6600 : : {
6601 : 71989104 : if (TREE_CODE (decl) != NAMESPACE_DECL)
6602 : 0 : decl = decl_namespace_context (decl);
6603 : 71989104 : vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
6604 : 71989104 : }
6605 : :
6606 : : /* [namespace.memdef]/2 */
6607 : :
6608 : : void
6609 : 71989101 : pop_decl_namespace (void)
6610 : : {
6611 : 71989101 : decl_namespace_list->pop ();
6612 : 71989101 : }
6613 : :
6614 : : /* Process a namespace-alias declaration. */
6615 : :
6616 : : void
6617 : 105864 : do_namespace_alias (location_t loc, tree alias, tree name_space)
6618 : : {
6619 : 105864 : if (name_space == error_mark_node)
6620 : : return;
6621 : :
6622 : 105855 : gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
6623 : :
6624 : 105855 : name_space = ORIGINAL_NAMESPACE (name_space);
6625 : :
6626 : : /* Build the alias. */
6627 : 105855 : alias = build_lang_decl_loc (loc, NAMESPACE_DECL, alias, void_type_node);
6628 : 105855 : DECL_NAMESPACE_ALIAS (alias) = name_space;
6629 : 105855 : DECL_EXTERNAL (alias) = 1;
6630 : 105855 : DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
6631 : 105855 : TREE_PUBLIC (alias) = TREE_PUBLIC (CP_DECL_CONTEXT (alias));
6632 : :
6633 : 105855 : alias = pushdecl (alias);
6634 : :
6635 : 211710 : if (!DECL_P (alias) || !DECL_NAMESPACE_ALIAS (alias))
6636 : : return;
6637 : :
6638 : 105852 : set_originating_module (alias);
6639 : 105852 : check_module_decl_linkage (alias);
6640 : :
6641 : : /* Emit debug info for namespace alias. */
6642 : 105852 : if (!building_stmt_list_p ())
6643 : 5287 : (*debug_hooks->early_global_decl) (alias);
6644 : : }
6645 : :
6646 : : /* Like pushdecl, only it places DECL in the current namespace,
6647 : : if appropriate. */
6648 : :
6649 : : tree
6650 : 45840399 : pushdecl_namespace_level (tree decl, bool hiding)
6651 : : {
6652 : 45840399 : auto_cond_timevar tv (TV_NAME_LOOKUP);
6653 : 45840399 : return do_pushdecl_with_scope (decl, NAMESPACE_LEVEL (current_namespace),
6654 : 45840399 : hiding);
6655 : 45840399 : }
6656 : :
6657 : : /* Wrapper around push_local_binding to push the bindings for
6658 : : a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6659 : : is the result of name lookup during template parsing. */
6660 : :
6661 : : static void
6662 : 3727472 : push_using_decl_bindings (name_lookup *lookup, tree name, tree value)
6663 : : {
6664 : 3727472 : tree type = NULL_TREE;
6665 : :
6666 : 3727472 : cxx_binding *binding = find_local_binding (current_binding_level, name);
6667 : 3727472 : if (binding)
6668 : : {
6669 : 84 : value = binding->value;
6670 : 84 : type = binding->type;
6671 : : }
6672 : :
6673 : 3727472 : if (lookup)
6674 : 3441709 : do_nonmember_using_decl (*lookup, true, true, &value, &type);
6675 : :
6676 : 3727472 : if (!value)
6677 : : ;
6678 : 3727472 : else if (binding && value == binding->value)
6679 : : /* Redeclaration of this USING_DECL. */;
6680 : 48 : else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
6681 : : {
6682 : : /* We already have this binding, so replace it. */
6683 : 36 : update_local_overload (IDENTIFIER_BINDING (name), value);
6684 : 36 : IDENTIFIER_BINDING (name)->value = value;
6685 : : }
6686 : : else
6687 : : /* Install the new binding. */
6688 : 3727400 : push_local_binding (name, value, /*using=*/true);
6689 : :
6690 : 3727472 : if (!type)
6691 : : ;
6692 : 21 : else if (binding && type == binding->type)
6693 : : ;
6694 : : else
6695 : : {
6696 : 15 : push_local_binding (name, type, /*using=*/true);
6697 : 15 : set_identifier_type_value (name, type);
6698 : : }
6699 : 3727472 : }
6700 : :
6701 : : /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6702 : :
6703 : : void
6704 : 285763 : push_using_decl_bindings (tree name, tree value)
6705 : : {
6706 : 285763 : push_using_decl_bindings (nullptr, name, value);
6707 : 285763 : }
6708 : :
6709 : : /* Process a using declaration in non-class scope. */
6710 : :
6711 : : void
6712 : 9600960 : finish_nonmember_using_decl (tree scope, tree name)
6713 : : {
6714 : 9600960 : gcc_checking_assert (current_binding_level->kind != sk_class);
6715 : :
6716 : 9600960 : if (scope == error_mark_node || name == error_mark_node)
6717 : 46 : return;
6718 : :
6719 : 9600960 : name_lookup lookup (name);
6720 : :
6721 : 9600960 : tree using_decl = lookup_using_decl (scope, lookup);
6722 : 9600960 : if (!using_decl)
6723 : 46 : return;
6724 : :
6725 : : /* Emit debug info. */
6726 : 9600914 : if (!processing_template_decl)
6727 : 6173307 : cp_emit_debug_info_for_using (lookup.value,
6728 : 6173307 : current_binding_level->this_entity);
6729 : :
6730 : 9600914 : if (current_binding_level->kind == sk_namespace)
6731 : : {
6732 : 6159205 : tree *slot = find_namespace_slot (current_namespace, name, true);
6733 : 6159205 : tree *mslot = get_fixed_binding_slot (slot, name,
6734 : : BINDING_SLOT_CURRENT, true);
6735 : 6159205 : bool failed = false;
6736 : :
6737 : 6159205 : if (mslot != slot)
6738 : : {
6739 : : /* A module vector. I presume the binding list is going to
6740 : : be sparser than the import bitmap. Hence iterate over
6741 : : the former checking for bits set in the bitmap. */
6742 : 28 : bitmap imports = get_import_bitmap ();
6743 : 28 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
6744 : :
6745 : : /* Scan the imported bindings. */
6746 : 28 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
6747 : 28 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
6748 : : {
6749 : 28 : ix--;
6750 : 28 : cluster++;
6751 : : }
6752 : :
6753 : : /* Do this in forward order, so we load modules in an order
6754 : : the user expects. */
6755 : 56 : for (; ix--; cluster++)
6756 : 84 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
6757 : : {
6758 : : /* Are we importing this module? */
6759 : 56 : if (unsigned base = cluster->indices[jx].base)
6760 : 27 : if (unsigned span = cluster->indices[jx].span)
6761 : 27 : do
6762 : 27 : if (bitmap_bit_p (imports, base))
6763 : 27 : goto found;
6764 : 0 : while (++base, --span);
6765 : 29 : continue;
6766 : :
6767 : 27 : found:;
6768 : : /* Is it loaded? */
6769 : 27 : if (cluster->slots[jx].is_lazy ())
6770 : : {
6771 : 0 : gcc_assert (cluster->indices[jx].span == 1);
6772 : 0 : lazy_load_binding (cluster->indices[jx].base,
6773 : : scope, name, &cluster->slots[jx]);
6774 : : }
6775 : :
6776 : 27 : tree value = cluster->slots[jx];
6777 : 27 : if (!value)
6778 : : /* Load errors could mean there's nothing here. */
6779 : 0 : continue;
6780 : :
6781 : : /* Extract what we can see from here. If there's no
6782 : : stat_hack, then everything was exported. */
6783 : 27 : tree type = NULL_TREE;
6784 : :
6785 : : /* If no stat hack, everything is visible. */
6786 : 27 : if (STAT_HACK_P (value))
6787 : : {
6788 : 9 : if (STAT_TYPE_VISIBLE_P (value))
6789 : 3 : type = STAT_TYPE (value);
6790 : 9 : value = STAT_VISIBLE (value);
6791 : : }
6792 : :
6793 : 27 : if (do_nonmember_using_decl (lookup, false, false,
6794 : : &value, &type))
6795 : : {
6796 : 0 : failed = true;
6797 : 0 : break;
6798 : : }
6799 : 29 : }
6800 : : }
6801 : :
6802 : 28 : if (!failed)
6803 : : {
6804 : : /* Now do the current slot. */
6805 : 6159205 : tree value = MAYBE_STAT_DECL (*mslot);
6806 : 6159205 : tree type = MAYBE_STAT_TYPE (*mslot);
6807 : :
6808 : 6159205 : do_nonmember_using_decl (lookup, false, true, &value, &type);
6809 : :
6810 : : // FIXME: Partition mergeableness?
6811 : 6159205 : if (STAT_HACK_P (*mslot))
6812 : : {
6813 : 1 : STAT_DECL (*mslot) = value;
6814 : 1 : STAT_TYPE (*mslot) = type;
6815 : : }
6816 : 6159204 : else if (type)
6817 : 22 : *mslot = stat_hack (value, type);
6818 : : else
6819 : 6159182 : *mslot = value;
6820 : : }
6821 : : }
6822 : : else
6823 : : {
6824 : 3441709 : add_decl_expr (using_decl);
6825 : 3441709 : if (DECL_DEPENDENT_P (using_decl))
6826 : 7 : lookup.value = using_decl;
6827 : 3441709 : push_using_decl_bindings (&lookup, name, NULL_TREE);
6828 : : }
6829 : 9600960 : }
6830 : :
6831 : : /* Return the declarations that are members of the namespace NS. */
6832 : :
6833 : : tree
6834 : 18 : cp_namespace_decls (tree ns)
6835 : : {
6836 : 18 : return NAMESPACE_LEVEL (ns)->names;
6837 : : }
6838 : :
6839 : : /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6840 : : ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6841 : :
6842 : : static bool
6843 : 3046687659 : qualify_lookup (tree val, LOOK_want want)
6844 : : {
6845 : 3046687659 : if (val == NULL_TREE)
6846 : : return false;
6847 : :
6848 : 3046687659 : if (bool (want & LOOK_want::TYPE))
6849 : : {
6850 : 40641956 : tree target_val = strip_using_decl (val);
6851 : :
6852 : 40641956 : if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
6853 : : return true;
6854 : : }
6855 : :
6856 : 3006323285 : if (bool (want & LOOK_want::TYPE_NAMESPACE))
6857 : 476520 : return TREE_CODE (val) == NAMESPACE_DECL;
6858 : :
6859 : : return true;
6860 : : }
6861 : :
6862 : : /* Is there a "using namespace std;" directive within USINGS? */
6863 : :
6864 : : static bool
6865 : 5157 : using_directives_contain_std_p (vec<tree, va_gc> *usings)
6866 : : {
6867 : 5157 : if (!usings)
6868 : : return false;
6869 : :
6870 : 46 : for (unsigned ix = usings->length (); ix--;)
6871 : 31 : if (strip_using_decl ((*usings)[ix]) == std_node)
6872 : : return true;
6873 : :
6874 : : return false;
6875 : : }
6876 : :
6877 : : /* Is there a "using namespace std;" directive within the current
6878 : : namespace (or its ancestors)?
6879 : : Compare with name_lookup::search_unqualified. */
6880 : :
6881 : : static bool
6882 : 1664 : has_using_namespace_std_directive_p ()
6883 : : {
6884 : 1664 : for (cp_binding_level *level = current_binding_level;
6885 : 6805 : level;
6886 : 5141 : level = level->level_chain)
6887 : 5157 : if (using_directives_contain_std_p (level->using_directives))
6888 : : return true;
6889 : :
6890 : : return false;
6891 : : }
6892 : :
6893 : : /* Subclass of deferred_diagnostic, for issuing a note when
6894 : : --param cxx-max-namespaces-for-diagnostic-help is reached.
6895 : :
6896 : : The note should be issued after the error, but before any other
6897 : : deferred diagnostics. This is handled by decorating a wrapped
6898 : : deferred_diagnostic, and emitting a note before that wrapped note is
6899 : : deleted. */
6900 : :
6901 : : class namespace_limit_reached : public deferred_diagnostic
6902 : : {
6903 : : public:
6904 : 3 : namespace_limit_reached (location_t loc, unsigned limit, tree name,
6905 : : std::unique_ptr<deferred_diagnostic> wrapped)
6906 : 3 : : deferred_diagnostic (loc),
6907 : 3 : m_limit (limit), m_name (name),
6908 : 3 : m_wrapped (std::move (wrapped))
6909 : : {
6910 : : }
6911 : :
6912 : 6 : ~namespace_limit_reached ()
6913 : 3 : {
6914 : : /* Unconditionally warn that the search was truncated. */
6915 : 3 : inform (get_location (),
6916 : : "maximum limit of %d namespaces searched for %qE",
6917 : : m_limit, m_name);
6918 : : /* m_wrapped will be implicitly deleted after this, emitting any followup
6919 : : diagnostic after the above note. */
6920 : 6 : }
6921 : :
6922 : : private:
6923 : : unsigned m_limit;
6924 : : tree m_name;
6925 : : std::unique_ptr<deferred_diagnostic> m_wrapped;
6926 : : };
6927 : :
6928 : : /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6929 : : Emit a note showing the location of the declaration of the suggestion. */
6930 : :
6931 : : class show_candidate_location : public deferred_diagnostic
6932 : : {
6933 : : public:
6934 : 108 : show_candidate_location (location_t loc, tree candidate)
6935 : 108 : : deferred_diagnostic (loc),
6936 : 108 : m_candidate (candidate)
6937 : : {
6938 : : }
6939 : :
6940 : 216 : ~show_candidate_location ()
6941 : 108 : {
6942 : 108 : inform (location_of (m_candidate), "%qE declared here", m_candidate);
6943 : 216 : }
6944 : :
6945 : : private:
6946 : : tree m_candidate;
6947 : : };
6948 : :
6949 : : /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6950 : : to be suggested by suggest_alternatives_for.
6951 : :
6952 : : Emit a series of notes showing the various suggestions. */
6953 : :
6954 : : class suggest_alternatives : public deferred_diagnostic
6955 : : {
6956 : : public:
6957 : 17 : suggest_alternatives (location_t loc, vec<tree> candidates)
6958 : 17 : : deferred_diagnostic (loc),
6959 : 17 : m_candidates (candidates)
6960 : : {
6961 : : }
6962 : :
6963 : 34 : ~suggest_alternatives ()
6964 : 17 : {
6965 : 17 : if (m_candidates.length ())
6966 : : {
6967 : 17 : inform_n (get_location (), m_candidates.length (),
6968 : : "suggested alternative:",
6969 : : "suggested alternatives:");
6970 : 108 : for (unsigned ix = 0; ix != m_candidates.length (); ix++)
6971 : : {
6972 : 37 : tree val = m_candidates[ix];
6973 : :
6974 : 37 : inform (location_of (val), " %qE", val);
6975 : : }
6976 : : }
6977 : 17 : m_candidates.release ();
6978 : 34 : }
6979 : :
6980 : : private:
6981 : : vec<tree> m_candidates;
6982 : : };
6983 : :
6984 : : /* A class for encapsulating the result of a search across
6985 : : multiple namespaces (and scoped enums within them) for an
6986 : : unrecognized name seen at a given source location. */
6987 : :
6988 : : class namespace_hints
6989 : : {
6990 : : public:
6991 : : namespace_hints (location_t loc, tree name);
6992 : :
6993 : : name_hint convert_candidates_to_name_hint ();
6994 : : name_hint maybe_decorate_with_limit (name_hint);
6995 : :
6996 : : private:
6997 : : void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
6998 : :
6999 : : location_t m_loc;
7000 : : tree m_name;
7001 : : vec<tree> m_candidates;
7002 : :
7003 : : /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
7004 : : unsigned m_limit;
7005 : :
7006 : : /* Was the limit reached? */
7007 : : bool m_limited;
7008 : : };
7009 : :
7010 : : /* Constructor for namespace_hints. Search namespaces and scoped enums,
7011 : : looking for an exact match for unrecognized NAME seen at LOC. */
7012 : :
7013 : 1822 : namespace_hints::namespace_hints (location_t loc, tree name)
7014 : 1822 : : m_loc(loc), m_name (name)
7015 : : {
7016 : 1822 : auto_vec<tree> worklist;
7017 : :
7018 : 1822 : m_candidates = vNULL;
7019 : 1822 : m_limited = false;
7020 : 1822 : m_limit = param_cxx_max_namespaces_for_diagnostic_help;
7021 : :
7022 : : /* Breadth-first search of namespaces. Up to limit namespaces
7023 : : searched (limit zero == unlimited). */
7024 : 1822 : worklist.safe_push (global_namespace);
7025 : 15768 : for (unsigned ix = 0; ix != worklist.length (); ix++)
7026 : : {
7027 : 6062 : tree ns = worklist[ix];
7028 : 6062 : name_lookup lookup (name);
7029 : :
7030 : 6062 : if (lookup.search_qualified (ns, false))
7031 : 130 : m_candidates.safe_push (lookup.value);
7032 : :
7033 : 6062 : if (!m_limited)
7034 : : {
7035 : : /* Look for child namespaces. We have to do this
7036 : : indirectly because they are chained in reverse order,
7037 : : which is confusing to the user. */
7038 : 6050 : auto_vec<tree> children;
7039 : :
7040 : 6050 : for (tree decl = NAMESPACE_LEVEL (ns)->names;
7041 : 4882713 : decl; decl = TREE_CHAIN (decl))
7042 : : {
7043 : 4876663 : if (TREE_CODE (decl) == NAMESPACE_DECL
7044 : 4360 : && !DECL_NAMESPACE_ALIAS (decl)
7045 : 4881017 : && !DECL_NAMESPACE_INLINE_P (decl))
7046 : 4249 : children.safe_push (decl);
7047 : :
7048 : : /* Look for exact matches for NAME within scoped enums.
7049 : : These aren't added to the worklist, and so don't count
7050 : : against the search limit. */
7051 : 4876663 : if (TREE_CODE (decl) == TYPE_DECL)
7052 : : {
7053 : 51913 : tree type = TREE_TYPE (decl);
7054 : 51913 : if (SCOPED_ENUM_P (type))
7055 : 1347 : maybe_add_candidate_for_scoped_enum (type, name);
7056 : : }
7057 : : }
7058 : :
7059 : 16340 : while (!m_limited && !children.is_empty ())
7060 : : {
7061 : 8486 : if (worklist.length () == m_limit)
7062 : 3 : m_limited = true;
7063 : : else
7064 : 4240 : worklist.safe_push (children.pop ());
7065 : : }
7066 : 6050 : }
7067 : 6062 : }
7068 : 1822 : }
7069 : :
7070 : : /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
7071 : : for m_name, an IDENTIFIER_NODE for which name lookup failed.
7072 : :
7073 : : If m_candidates is non-empty, use it to generate a suggestion and/or
7074 : : a deferred diagnostic that lists the possible candidate(s).
7075 : : */
7076 : :
7077 : : name_hint
7078 : 1822 : namespace_hints::convert_candidates_to_name_hint ()
7079 : : {
7080 : : /* How many candidates do we have? */
7081 : :
7082 : : /* If we have just one candidate, issue a name_hint with it as a suggestion
7083 : : (so that consumers are able to suggest it within the error message and emit
7084 : : it as a fix-it hint), and with a note showing the candidate's location. */
7085 : 1822 : if (m_candidates.length () == 1)
7086 : : {
7087 : 108 : tree candidate = m_candidates[0];
7088 : : /* Clean up CANDIDATES. */
7089 : 108 : m_candidates.release ();
7090 : 108 : return name_hint (expr_to_string (candidate),
7091 : 108 : std::make_unique<show_candidate_location> (m_loc,
7092 : 108 : candidate));
7093 : : }
7094 : 1714 : else if (m_candidates.length () > 1)
7095 : : /* If we have more than one candidate, issue a name_hint without a single
7096 : : "suggestion", but with a deferred diagnostic that lists the
7097 : : various candidates. This takes ownership of m_candidates. */
7098 : 17 : return name_hint (NULL,
7099 : 17 : std::make_unique<suggest_alternatives> (m_loc,
7100 : 17 : m_candidates));
7101 : :
7102 : : /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
7103 : 1697 : gcc_assert (m_candidates.length () == 0);
7104 : 1697 : gcc_assert (m_candidates == vNULL);
7105 : :
7106 : 1697 : return name_hint ();
7107 : : }
7108 : :
7109 : : /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
7110 : : then we want to emit a note about after the error, but before
7111 : : any other deferred diagnostics.
7112 : :
7113 : : Handle this by figuring out what hint is needed, then optionally
7114 : : decorating HINT with a namespace_limit_reached wrapper. */
7115 : :
7116 : : name_hint
7117 : 1822 : namespace_hints::maybe_decorate_with_limit (name_hint hint)
7118 : : {
7119 : 1822 : if (m_limited)
7120 : 3 : return name_hint
7121 : : (hint.suggestion (),
7122 : 3 : std::make_unique<namespace_limit_reached> (m_loc, m_limit,
7123 : 3 : m_name,
7124 : 6 : hint.take_deferred ()));
7125 : : else
7126 : 1819 : return hint;
7127 : : }
7128 : :
7129 : : /* Look inside SCOPED_ENUM for exact matches for NAME.
7130 : : If one is found, add its CONST_DECL to m_candidates. */
7131 : :
7132 : : void
7133 : 1347 : namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
7134 : : tree name)
7135 : : {
7136 : 1347 : gcc_assert (SCOPED_ENUM_P (scoped_enum));
7137 : :
7138 : 2003 : for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7139 : : {
7140 : 671 : tree id = TREE_PURPOSE (iter);
7141 : 671 : if (id == name)
7142 : : {
7143 : 15 : m_candidates.safe_push (TREE_VALUE (iter));
7144 : 15 : return;
7145 : : }
7146 : : }
7147 : : }
7148 : :
7149 : : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7150 : : name lookup failed.
7151 : :
7152 : : Search through all available namespaces and any scoped enums within them
7153 : : and generate a suggestion and/or a deferred diagnostic that lists possible
7154 : : candidate(s).
7155 : :
7156 : : If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
7157 : : look for near-matches and suggest the best near-match, if there is one.
7158 : :
7159 : : If nothing is found, then an empty name_hint is returned. */
7160 : :
7161 : : name_hint
7162 : 1768 : suggest_alternatives_for (location_t location, tree name,
7163 : : bool suggest_misspellings)
7164 : : {
7165 : : /* First, search for exact matches in other namespaces. */
7166 : 1768 : namespace_hints ns_hints (location, name);
7167 : 1768 : name_hint result = ns_hints.convert_candidates_to_name_hint ();
7168 : :
7169 : : /* Otherwise, try other approaches. */
7170 : 1768 : if (!result)
7171 : 1664 : result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
7172 : :
7173 : 1768 : return ns_hints.maybe_decorate_with_limit (std::move (result));
7174 : 1768 : }
7175 : :
7176 : : /* The second half of suggest_alternatives_for, for when no exact matches
7177 : : were found in other namespaces. */
7178 : :
7179 : : static name_hint
7180 : 1664 : suggest_alternatives_for_1 (location_t location, tree name,
7181 : : bool suggest_misspellings)
7182 : : {
7183 : : /* No candidates were found in the available namespaces. */
7184 : :
7185 : : /* If there's a "using namespace std;" active, and this
7186 : : is one of the most common "std::" names, then it's probably a
7187 : : missing #include. */
7188 : 1664 : if (has_using_namespace_std_directive_p ())
7189 : : {
7190 : 16 : name_hint hint = maybe_suggest_missing_std_header (location, name);
7191 : 16 : if (hint)
7192 : : return hint;
7193 : 0 : }
7194 : :
7195 : : /* Look for exact matches for builtin defines that would have been
7196 : : defined if the user had passed a command-line option (e.g. -fopenmp
7197 : : for "_OPENMP"). */
7198 : 1648 : diagnostic_option_id option_id
7199 : 1648 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
7200 : 1648 : if (option_id.m_idx > 0)
7201 : 6 : return name_hint
7202 : : (nullptr,
7203 : 6 : std::make_unique<suggest_missing_option> (location,
7204 : 12 : IDENTIFIER_POINTER (name),
7205 : 6 : option_id));
7206 : :
7207 : : /* Otherwise, consider misspellings. */
7208 : 1642 : if (!suggest_misspellings)
7209 : 0 : return name_hint ();
7210 : :
7211 : 1642 : return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
7212 : : }
7213 : :
7214 : : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
7215 : : name lookup failed.
7216 : :
7217 : : Search through all available namespaces and generate a suggestion and/or
7218 : : a deferred diagnostic that lists possible candidate(s).
7219 : :
7220 : : This is similiar to suggest_alternatives_for, but doesn't fallback to
7221 : : the other approaches used by that function. */
7222 : :
7223 : : name_hint
7224 : 54 : suggest_alternatives_in_other_namespaces (location_t location, tree name)
7225 : : {
7226 : 54 : namespace_hints ns_hints (location, name);
7227 : :
7228 : 54 : name_hint result = ns_hints.convert_candidates_to_name_hint ();
7229 : :
7230 : 54 : return ns_hints.maybe_decorate_with_limit (std::move (result));
7231 : 54 : }
7232 : :
7233 : : /* A well-known name within the C++ standard library, returned by
7234 : : get_std_name_hint.
7235 : :
7236 : : The gperf-generated file contains the definition of the class
7237 : : "std_name_hint_lookup" with a static member function which
7238 : : returns the pointer to a structure "std_name_hint" which
7239 : : is also defined in that file. */
7240 : :
7241 : : #include "std-name-hint.h"
7242 : :
7243 : : /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
7244 : : for some of the most common names within "std::".
7245 : : Given non-NULL NAME, return the std_name_hint for it, or NULL. */
7246 : :
7247 : : static const std_name_hint *
7248 : 179 : get_std_name_hint (const char *name)
7249 : : {
7250 : 179 : return std_name_hint_lookup::find(name, strlen(name));
7251 : : }
7252 : :
7253 : : /* Describe DIALECT. */
7254 : :
7255 : : const char *
7256 : 4153 : get_cxx_dialect_name (enum cxx_dialect dialect)
7257 : : {
7258 : 4153 : switch (dialect)
7259 : : {
7260 : 0 : default:
7261 : 0 : gcc_unreachable ();
7262 : : case cxx98:
7263 : : return "C++98";
7264 : 4 : case cxx11:
7265 : 4 : return "C++11";
7266 : 2 : case cxx14:
7267 : 2 : return "C++14";
7268 : 1329 : case cxx17:
7269 : 1329 : return "C++17";
7270 : 1421 : case cxx20:
7271 : 1421 : return "C++20";
7272 : 23 : case cxx23:
7273 : 23 : return "C++23";
7274 : 1374 : case cxx26:
7275 : 1374 : return "C++26";
7276 : : }
7277 : : }
7278 : :
7279 : : /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7280 : : that weren't recognized, but for which we know which header it ought to be
7281 : : in.
7282 : :
7283 : : Emit a note either suggesting the header to be included, or noting that
7284 : : the current dialect is too early for the given name. */
7285 : :
7286 : : class missing_std_header : public deferred_diagnostic
7287 : : {
7288 : : public:
7289 : 152 : missing_std_header (location_t loc,
7290 : : const char *name_str,
7291 : : const std_name_hint *header_hint)
7292 : 152 : : deferred_diagnostic (loc),
7293 : 152 : m_name_str (name_str),
7294 : 152 : m_header_hint (header_hint)
7295 : : {}
7296 : 304 : ~missing_std_header ()
7297 : 152 : {
7298 : 152 : gcc_rich_location richloc (get_location ());
7299 : 152 : if (cxx_dialect >= m_header_hint->min_dialect)
7300 : : {
7301 : 143 : const char *header = m_header_hint->header;
7302 : 143 : maybe_add_include_fixit (&richloc, header, true);
7303 : 143 : inform (&richloc,
7304 : : "%<std::%s%> is defined in header %qs;"
7305 : : " this is probably fixable by adding %<#include %s%>",
7306 : : m_name_str, header, header);
7307 : : }
7308 : : else
7309 : 9 : inform (&richloc,
7310 : : "%<std::%s%> is only available from %s onwards",
7311 : : m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
7312 : 304 : }
7313 : :
7314 : : private:
7315 : : const char *m_name_str;
7316 : : const std_name_hint *m_header_hint;
7317 : : };
7318 : :
7319 : : /* Attempt to generate a name_hint that suggests pertinent header files
7320 : : for NAME at LOCATION, for common names within the "std" namespace,
7321 : : or an empty name_hint if this isn't applicable. */
7322 : :
7323 : : static name_hint
7324 : 179 : maybe_suggest_missing_std_header (location_t location, tree name)
7325 : : {
7326 : 179 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7327 : :
7328 : 179 : const char *name_str = IDENTIFIER_POINTER (name);
7329 : 179 : const std_name_hint *header_hint = get_std_name_hint (name_str);
7330 : 179 : if (!header_hint)
7331 : 27 : return name_hint ();
7332 : :
7333 : 152 : return name_hint (nullptr,
7334 : 152 : std::make_unique<missing_std_header> (location, name_str,
7335 : 152 : header_hint));
7336 : : }
7337 : :
7338 : : /* Attempt to generate a name_hint that suggests a missing header file
7339 : : for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7340 : : applicable. */
7341 : :
7342 : : name_hint
7343 : 473 : maybe_suggest_missing_header (location_t location, tree name, tree scope)
7344 : : {
7345 : 473 : if (scope == NULL_TREE)
7346 : 0 : return name_hint ();
7347 : 473 : if (TREE_CODE (scope) != NAMESPACE_DECL)
7348 : 33 : return name_hint ();
7349 : : /* We only offer suggestions for the "std" namespace. */
7350 : 440 : if (scope != std_node)
7351 : 277 : return name_hint ();
7352 : 163 : return maybe_suggest_missing_std_header (location, name);
7353 : : }
7354 : :
7355 : : /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7356 : : lookup failed within the explicitly provided SCOPE.
7357 : :
7358 : : Suggest the best meaningful candidates (if any), otherwise
7359 : : an empty name_hint is returned. */
7360 : :
7361 : : name_hint
7362 : 315 : suggest_alternative_in_explicit_scope (location_t location, tree name,
7363 : : tree scope)
7364 : : {
7365 : : /* Something went very wrong; don't suggest anything. */
7366 : 315 : if (name == error_mark_node)
7367 : 2 : return name_hint ();
7368 : :
7369 : 313 : if (TREE_CODE (scope) != NAMESPACE_DECL)
7370 : 24 : return name_hint ();
7371 : :
7372 : : /* Resolve any namespace aliases. */
7373 : 289 : scope = ORIGINAL_NAMESPACE (scope);
7374 : :
7375 : 289 : name_hint hint = maybe_suggest_missing_header (location, name, scope);
7376 : 289 : if (hint)
7377 : 121 : return hint;
7378 : :
7379 : 168 : cp_binding_level *level = NAMESPACE_LEVEL (scope);
7380 : :
7381 : 168 : best_match <tree, const char *> bm (name);
7382 : 168 : consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
7383 : :
7384 : : /* See if we have a good suggesion for the user. */
7385 : 168 : const char *fuzzy_name = bm.get_best_meaningful_candidate ();
7386 : 168 : if (fuzzy_name)
7387 : 43 : return name_hint (fuzzy_name, NULL);
7388 : :
7389 : 125 : return name_hint ();
7390 : 289 : }
7391 : :
7392 : : /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7393 : : candidates. */
7394 : :
7395 : : name_hint
7396 : 15 : suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
7397 : : {
7398 : 15 : gcc_assert (SCOPED_ENUM_P (scoped_enum));
7399 : :
7400 : 15 : best_match <tree, const char *> bm (name);
7401 : 48 : for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
7402 : : {
7403 : 33 : tree id = TREE_PURPOSE (iter);
7404 : 33 : bm.consider (IDENTIFIER_POINTER (id));
7405 : : }
7406 : 15 : return name_hint (bm.get_best_meaningful_candidate (), NULL);
7407 : : }
7408 : :
7409 : : /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7410 : : or a class TYPE).
7411 : :
7412 : : WANT as for lookup_name_1.
7413 : :
7414 : : Returns a DECL (or OVERLOAD, or BASELINK) representing the
7415 : : declaration found. If no suitable declaration can be found,
7416 : : ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7417 : : neither a class-type nor a namespace a diagnostic is issued. */
7418 : :
7419 : : tree
7420 : 401239115 : lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
7421 : : {
7422 : 401239115 : tree t = NULL_TREE;
7423 : :
7424 : 401239115 : if (TREE_CODE (scope) == NAMESPACE_DECL)
7425 : : {
7426 : 300839990 : name_lookup lookup (name, want);
7427 : :
7428 : 300839990 : if (qualified_namespace_lookup (scope, &lookup))
7429 : : {
7430 : 297317472 : t = lookup.value;
7431 : :
7432 : : /* If we have a known type overload, pull it out. This can happen
7433 : : for using decls. */
7434 : 297317472 : if (TREE_CODE (t) == OVERLOAD && TREE_TYPE (t) != unknown_type_node)
7435 : 1043003 : t = OVL_FUNCTION (t);
7436 : : }
7437 : 300839990 : }
7438 : 100399125 : else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
7439 : 7294350 : t = lookup_enumerator (scope, name);
7440 : 93104775 : else if (is_class_type (scope, complain))
7441 : 93098674 : t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
7442 : : tf_warning_or_error);
7443 : :
7444 : 401232960 : if (!t)
7445 : 3591379 : return error_mark_node;
7446 : : return t;
7447 : : }
7448 : :
7449 : : /* Wrapper for the above that takes a string argument. The function name is
7450 : : not at the beginning of the line to keep this wrapper out of etags. */
7451 : :
7452 : 125719 : tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
7453 : : {
7454 : 125719 : return lookup_qualified_name (t, get_identifier (p), w, c);
7455 : : }
7456 : :
7457 : : /* [namespace.qual]
7458 : : Accepts the NAME to lookup and its qualifying SCOPE.
7459 : : Returns the name/type pair found into the cxx_binding *RESULT,
7460 : : or false on error. */
7461 : :
7462 : : static bool
7463 : 307328193 : qualified_namespace_lookup (tree scope, name_lookup *lookup)
7464 : : {
7465 : 307328193 : auto_cond_timevar tv (TV_NAME_LOOKUP);
7466 : 307328193 : query_oracle (lookup->name);
7467 : 307328193 : bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
7468 : 614656386 : return found;
7469 : 307328193 : }
7470 : :
7471 : : /* If DECL is suitably visible to the user, consider its name for
7472 : : spelling correction. */
7473 : :
7474 : : static void
7475 : 1989 : consider_decl (tree decl, best_match <tree, const char *> &bm,
7476 : : bool consider_impl_names)
7477 : : {
7478 : : /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7479 : : within range for). */
7480 : 1989 : if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7481 : : return;
7482 : :
7483 : 1906 : tree suggestion = DECL_NAME (decl);
7484 : 1906 : if (!suggestion)
7485 : : return;
7486 : :
7487 : : /* Don't suggest names that are for anonymous aggregate types, as
7488 : : they are an implementation detail generated by the compiler. */
7489 : 1805 : if (IDENTIFIER_ANON_P (suggestion))
7490 : : return;
7491 : :
7492 : 1722 : const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
7493 : :
7494 : : /* Ignore internal names with spaces in them. */
7495 : 1722 : if (strchr (suggestion_str, ' '))
7496 : : return;
7497 : :
7498 : : /* Don't suggest names that are reserved for use by the
7499 : : implementation, unless NAME began with an underscore. */
7500 : 1722 : if (!consider_impl_names
7501 : 1722 : && name_reserved_for_implementation_p (suggestion_str))
7502 : : return;
7503 : :
7504 : 1692 : bm.consider (suggestion_str);
7505 : : }
7506 : :
7507 : : /* If DECL is suitably visible to the user, add its name to VEC and
7508 : : return true. Otherwise return false. */
7509 : :
7510 : : static bool
7511 : 3296222 : maybe_add_fuzzy_decl (auto_vec<tree> &vec, tree decl)
7512 : : {
7513 : : /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7514 : : within range for). */
7515 : 3296222 : if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
7516 : : return false;
7517 : :
7518 : 3295488 : tree suggestion = DECL_NAME (decl);
7519 : 3295488 : if (!suggestion)
7520 : : return false;
7521 : :
7522 : : /* Don't suggest names that are for anonymous aggregate types, as
7523 : : they are an implementation detail generated by the compiler. */
7524 : 3295488 : if (IDENTIFIER_ANON_P (suggestion))
7525 : : return false;
7526 : :
7527 : 3295488 : vec.safe_push (suggestion);
7528 : :
7529 : 3295488 : return true;
7530 : : }
7531 : :
7532 : : /* Examing the namespace binding BINDING, and add at most one instance
7533 : : of the name, if it contains a visible entity of interest. Return
7534 : : true if we added something. */
7535 : :
7536 : : bool
7537 : 5690546 : maybe_add_fuzzy_binding (auto_vec<tree> &vec, tree binding,
7538 : : lookup_name_fuzzy_kind kind)
7539 : : {
7540 : 5690546 : tree value = NULL_TREE;
7541 : :
7542 : 5690546 : if (STAT_HACK_P (binding))
7543 : : {
7544 : 267 : if (!STAT_TYPE_HIDDEN_P (binding)
7545 : 267 : && STAT_TYPE (binding))
7546 : : {
7547 : 141 : if (maybe_add_fuzzy_decl (vec, STAT_TYPE (binding)))
7548 : : return true;
7549 : : }
7550 : 126 : else if (!STAT_DECL_HIDDEN_P (binding))
7551 : 99 : value = STAT_DECL (binding);
7552 : : }
7553 : : else
7554 : : value = binding;
7555 : :
7556 : 5690405 : value = ovl_skip_hidden (value);
7557 : 5690405 : if (value)
7558 : : {
7559 : 4846963 : value = OVL_FIRST (value);
7560 : 4846963 : if (kind != FUZZY_LOOKUP_TYPENAME
7561 : 4846963 : || TREE_CODE (STRIP_TEMPLATE (value)) == TYPE_DECL)
7562 : 3296081 : if (maybe_add_fuzzy_decl (vec, value))
7563 : : return true;
7564 : : }
7565 : :
7566 : : /* Nothing found. */
7567 : : return false;
7568 : : }
7569 : :
7570 : : /* Helper function for lookup_name_fuzzy.
7571 : : Traverse binding level LVL, looking for good name matches for NAME
7572 : : (and BM). */
7573 : : static void
7574 : 7067 : consider_binding_level (tree name, best_match <tree, const char *> &bm,
7575 : : cp_binding_level *lvl, bool look_within_fields,
7576 : : enum lookup_name_fuzzy_kind kind)
7577 : : {
7578 : 7067 : if (look_within_fields)
7579 : 1025 : if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
7580 : : {
7581 : 414 : tree type = lvl->this_entity;
7582 : 414 : bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
7583 : 414 : tree best_matching_field
7584 : 414 : = lookup_member_fuzzy (type, name, want_type_p);
7585 : 414 : if (best_matching_field)
7586 : 10 : bm.consider (IDENTIFIER_POINTER (best_matching_field));
7587 : : }
7588 : :
7589 : : /* Only suggest names reserved for the implementation if NAME begins
7590 : : with an underscore. */
7591 : 7067 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
7592 : :
7593 : 7067 : if (lvl->kind != sk_namespace)
7594 : 6876 : for (tree t = lvl->names; t; t = TREE_CHAIN (t))
7595 : : {
7596 : 2454 : tree d = t;
7597 : :
7598 : : /* OVERLOADs or decls from using declaration are wrapped into
7599 : : TREE_LIST. */
7600 : 2454 : if (TREE_CODE (d) == TREE_LIST)
7601 : 27 : d = OVL_FIRST (TREE_VALUE (d));
7602 : :
7603 : : /* Don't use bindings from implicitly declared functions,
7604 : : as they were likely misspellings themselves. */
7605 : 2454 : if (TREE_TYPE (d) == error_mark_node)
7606 : 297 : continue;
7607 : :
7608 : : /* If we want a typename, ignore non-types. */
7609 : 2325 : if (kind == FUZZY_LOOKUP_TYPENAME
7610 : 2157 : && TREE_CODE (STRIP_TEMPLATE (d)) != TYPE_DECL)
7611 : 168 : continue;
7612 : :
7613 : 1989 : consider_decl (d, bm, consider_implementation_names);
7614 : : }
7615 : : else
7616 : : {
7617 : : /* We need to iterate over the namespace hash table, in order to
7618 : : not mention hidden entities. But hash table iteration is
7619 : : (essentially) unpredictable, our correction-distance measure
7620 : : is very granular, and we pick the first of equal distances.
7621 : : Hence, we need to call the distance-measurer in a predictable
7622 : : order. So, iterate over the namespace hash, inserting
7623 : : visible names into a vector. Then sort the vector. Then
7624 : : determine spelling distance. */
7625 : :
7626 : 2645 : tree ns = lvl->this_entity;
7627 : 2645 : auto_vec<tree> vec;
7628 : :
7629 : 2645 : hash_table<named_decl_hash>::iterator end
7630 : 2645 : (DECL_NAMESPACE_BINDINGS (ns)->end ());
7631 : 5693233 : for (hash_table<named_decl_hash>::iterator iter
7632 : 11386466 : (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
7633 : : {
7634 : 5690588 : tree binding = *iter;
7635 : :
7636 : 5690588 : if (TREE_CODE (binding) == BINDING_VECTOR)
7637 : : {
7638 : 222 : bitmap imports = get_import_bitmap ();
7639 : 222 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (binding);
7640 : :
7641 : 222 : if (tree bind = cluster->slots[BINDING_SLOT_CURRENT])
7642 : 18 : if (maybe_add_fuzzy_binding (vec, bind, kind))
7643 : 9 : continue;
7644 : :
7645 : : /* Scan the imported bindings. */
7646 : 213 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (binding);
7647 : 213 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
7648 : : {
7649 : 213 : ix--;
7650 : 213 : cluster++;
7651 : : }
7652 : :
7653 : 426 : for (; ix--; cluster++)
7654 : 513 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER;
7655 : : jx++)
7656 : : {
7657 : : /* Are we importing this module? */
7658 : 426 : if (unsigned base = cluster->indices[jx].base)
7659 : 189 : if (unsigned span = cluster->indices[jx].span)
7660 : 192 : do
7661 : 192 : if (bitmap_bit_p (imports, base))
7662 : 183 : goto found;
7663 : 9 : while (++base, --span);
7664 : 243 : continue;
7665 : :
7666 : 183 : found:;
7667 : : /* Is it loaded? */
7668 : 183 : if (cluster->slots[jx].is_lazy ())
7669 : : /* Let's not read in everything on the first
7670 : : spello! **/
7671 : 21 : continue;
7672 : 162 : if (tree bind = cluster->slots[jx])
7673 : 162 : if (maybe_add_fuzzy_binding (vec, bind, kind))
7674 : : break;
7675 : 243 : }
7676 : : }
7677 : : else
7678 : 5690366 : maybe_add_fuzzy_binding (vec, binding, kind);
7679 : : }
7680 : :
7681 : 173380586 : vec.qsort ([] (const void *a_, const void *b_)
7682 : : {
7683 : : return strcmp (IDENTIFIER_POINTER (*(const tree *)a_),
7684 : : IDENTIFIER_POINTER (*(const tree *)b_));
7685 : : });
7686 : :
7687 : : /* Examine longest to shortest. */
7688 : 3300778 : for (unsigned ix = vec.length (); ix--;)
7689 : : {
7690 : 3295488 : const char *str = IDENTIFIER_POINTER (vec[ix]);
7691 : :
7692 : : /* Ignore internal names with spaces in them. */
7693 : 3295488 : if (strchr (str, ' '))
7694 : 68045 : continue;
7695 : :
7696 : : /* Don't suggest names that are reserved for use by the
7697 : : implementation, unless NAME began with an underscore. */
7698 : 6144587 : if (!consider_implementation_names
7699 : 3227443 : && name_reserved_for_implementation_p (str))
7700 : 2917144 : continue;
7701 : :
7702 : 310299 : bm.consider (str);
7703 : : }
7704 : 2645 : }
7705 : 7067 : }
7706 : :
7707 : : /* Subclass of deferred_diagnostic. Notify the user that the
7708 : : given macro was used before it was defined.
7709 : : This can be done in the C++ frontend since tokenization happens
7710 : : upfront. */
7711 : :
7712 : : class macro_use_before_def : public deferred_diagnostic
7713 : : {
7714 : : public:
7715 : : /* Factory function. Return a new macro_use_before_def instance if
7716 : : appropriate, or return NULL. */
7717 : : static std::unique_ptr<macro_use_before_def>
7718 : 59 : maybe_make (location_t use_loc, cpp_hashnode *macro)
7719 : : {
7720 : 59 : location_t def_loc = cpp_macro_definition_location (macro);
7721 : 59 : if (def_loc == UNKNOWN_LOCATION)
7722 : 0 : return nullptr;
7723 : :
7724 : : /* We only want to issue a note if the macro was used *before* it was
7725 : : defined.
7726 : : We don't want to issue a note for cases where a macro was incorrectly
7727 : : used, leaving it unexpanded (e.g. by using the wrong argument
7728 : : count). */
7729 : 59 : if (!linemap_location_before_p (line_table, use_loc, def_loc))
7730 : 21 : return nullptr;
7731 : :
7732 : 38 : return std::make_unique<macro_use_before_def> (use_loc, macro);
7733 : : }
7734 : :
7735 : : /* Ctor. LOC is the location of the usage. MACRO is the
7736 : : macro that was used. */
7737 : 38 : macro_use_before_def (location_t loc, cpp_hashnode *macro)
7738 : 38 : : deferred_diagnostic (loc), m_macro (macro)
7739 : : {
7740 : 38 : gcc_assert (macro);
7741 : 38 : }
7742 : :
7743 : 76 : ~macro_use_before_def ()
7744 : 38 : {
7745 : 38 : if (is_suppressed_p ())
7746 : 0 : return;
7747 : :
7748 : 38 : inform (get_location (), "the macro %qs had not yet been defined",
7749 : 38 : (const char *)m_macro->ident.str);
7750 : 76 : inform (cpp_macro_definition_location (m_macro),
7751 : : "it was later defined here");
7752 : 76 : }
7753 : :
7754 : : private:
7755 : : cpp_hashnode *m_macro;
7756 : : };
7757 : :
7758 : : /* Determine if it can ever make sense to offer RID as a suggestion for
7759 : : a misspelling.
7760 : :
7761 : : Subroutine of lookup_name_fuzzy. */
7762 : :
7763 : : static bool
7764 : 449844 : suggest_rid_p (enum rid rid)
7765 : : {
7766 : 449844 : switch (rid)
7767 : : {
7768 : : /* Support suggesting function-like keywords. */
7769 : : case RID_STATIC_ASSERT:
7770 : : return true;
7771 : :
7772 : 445898 : default:
7773 : : /* Support suggesting the various decl-specifier words, to handle
7774 : : e.g. "singed" vs "signed" typos. */
7775 : 445898 : if (cp_keyword_starts_decl_specifier_p (rid))
7776 : : return true;
7777 : :
7778 : : /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7779 : : and "do" for short misspellings, which are likely to lead to
7780 : : nonsensical results. */
7781 : : return false;
7782 : : }
7783 : : }
7784 : :
7785 : : /* Search for near-matches for NAME within the current bindings, and within
7786 : : macro names, returning the best match as a const char *, or NULL if
7787 : : no reasonable match is found.
7788 : :
7789 : : Use LOC for any deferred diagnostics. */
7790 : :
7791 : : name_hint
7792 : 2281 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
7793 : : {
7794 : 2281 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
7795 : :
7796 : : /* First, try some well-known names in the C++ standard library, in case
7797 : : the user forgot a #include. */
7798 : 2281 : const char *header_hint
7799 : 2281 : = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
7800 : 2281 : if (header_hint)
7801 : 249 : return name_hint
7802 : : (nullptr,
7803 : 249 : std::make_unique<suggest_missing_header> (loc,
7804 : 498 : IDENTIFIER_POINTER (name),
7805 : 249 : header_hint));
7806 : :
7807 : 2032 : best_match <tree, const char *> bm (name);
7808 : :
7809 : 2032 : cp_binding_level *lvl;
7810 : 3057 : for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
7811 : 1025 : consider_binding_level (name, bm, lvl, true, kind);
7812 : :
7813 : 9938 : for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
7814 : 5874 : consider_binding_level (name, bm, lvl, false, kind);
7815 : :
7816 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7817 : : as:
7818 : : x = SOME_OTHER_MACRO (y);
7819 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
7820 : : as a misspelled identifier.
7821 : :
7822 : : Use the best distance so far so that a candidate is only set if
7823 : : a macro is better than anything so far. This allows early rejection
7824 : : (without calculating the edit distance) of macro names that must have
7825 : : distance >= bm.get_best_distance (), and means that we only get a
7826 : : non-NULL result for best_macro_match if it's better than any of
7827 : : the identifiers already checked. */
7828 : 2032 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
7829 : 2032 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
7830 : : /* If a macro is the closest so far to NAME, consider it. */
7831 : 2032 : if (best_macro)
7832 : 27 : bm.consider ((const char *)best_macro->ident.str);
7833 : 2005 : else if (bmm.get_best_distance () == 0)
7834 : : {
7835 : : /* If we have an exact match for a macro name, then either the
7836 : : macro was used with the wrong argument count, or the macro
7837 : : has been used before it was defined. */
7838 : 98 : if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
7839 : 62 : if (cpp_user_macro_p (macro))
7840 : 59 : return name_hint (NULL,
7841 : 59 : macro_use_before_def::maybe_make (loc, macro));
7842 : : }
7843 : :
7844 : : /* Try the "starts_decl_specifier_p" keywords to detect
7845 : : "singed" vs "signed" typos. */
7846 : 451817 : for (unsigned i = 0; i < num_c_common_reswords; i++)
7847 : : {
7848 : 449844 : const c_common_resword *resword = &c_common_reswords[i];
7849 : :
7850 : 449844 : if (!suggest_rid_p (resword->rid))
7851 : 329491 : continue;
7852 : :
7853 : 120353 : tree resword_identifier = ridpointers [resword->rid];
7854 : 120353 : if (!resword_identifier)
7855 : 0 : continue;
7856 : 120353 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
7857 : :
7858 : : /* Only consider reserved words that survived the
7859 : : filtering in init_reswords (e.g. for -std). */
7860 : 120353 : if (!IDENTIFIER_KEYWORD_P (resword_identifier))
7861 : 13605 : continue;
7862 : :
7863 : 106748 : bm.consider (IDENTIFIER_POINTER (resword_identifier));
7864 : : }
7865 : :
7866 : 1973 : return name_hint (bm.get_best_meaningful_candidate (), NULL);
7867 : : }
7868 : :
7869 : : /* Subroutine of outer_binding.
7870 : :
7871 : : Returns TRUE if BINDING is a binding to a template parameter of
7872 : : SCOPE. In that case SCOPE is the scope of a primary template
7873 : : parameter -- in the sense of G++, i.e, a template that has its own
7874 : : template header.
7875 : :
7876 : : Returns FALSE otherwise. */
7877 : :
7878 : : static bool
7879 : 350744972 : binding_to_template_parms_of_scope_p (cxx_binding *binding,
7880 : : cp_binding_level *scope)
7881 : : {
7882 : 350744972 : tree binding_value, tmpl, tinfo;
7883 : 350744972 : int level;
7884 : :
7885 : 350744972 : if (!binding || !scope || !scope->this_entity)
7886 : : return false;
7887 : :
7888 : 206494374 : binding_value = binding->value ? binding->value : binding->type;
7889 : 206494374 : tinfo = get_template_info (scope->this_entity);
7890 : :
7891 : : /* BINDING_VALUE must be a template parm. */
7892 : 206494374 : if (binding_value == NULL_TREE
7893 : 206494374 : || (!DECL_P (binding_value)
7894 : 206494374 : || !DECL_TEMPLATE_PARM_P (binding_value)))
7895 : : return false;
7896 : :
7897 : : /* The level of BINDING_VALUE. */
7898 : 412988748 : level =
7899 : 206494374 : template_type_parameter_p (binding_value)
7900 : 206494374 : ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7901 : : (TREE_TYPE (binding_value)))
7902 : 80342358 : : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
7903 : :
7904 : : /* The template of the current scope, iff said scope is a primary
7905 : : template. */
7906 : 206494374 : tmpl = (tinfo
7907 : 186486486 : && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
7908 : 206494374 : ? TI_TEMPLATE (tinfo)
7909 : : : NULL_TREE);
7910 : :
7911 : : /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7912 : : then BINDING_VALUE is a parameter of TMPL. */
7913 : 160663633 : return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
7914 : : }
7915 : :
7916 : : /* Return the innermost non-namespace binding for NAME from a scope
7917 : : containing BINDING, or, if BINDING is NULL, the current scope.
7918 : : Please note that for a given template, the template parameters are
7919 : : considered to be in the scope containing the current scope.
7920 : : If CLASS_P is false, then class bindings are ignored. */
7921 : :
7922 : : cxx_binding *
7923 : 5496024401 : outer_binding (tree name,
7924 : : cxx_binding *binding,
7925 : : bool class_p)
7926 : : {
7927 : 5496024401 : cxx_binding *outer;
7928 : 5496024401 : cp_binding_level *scope;
7929 : 5496024401 : cp_binding_level *outer_scope;
7930 : :
7931 : 5496024401 : if (binding)
7932 : : {
7933 : 2670920 : scope = binding->scope->level_chain;
7934 : 2670920 : outer = binding->previous;
7935 : : }
7936 : : else
7937 : : {
7938 : 5493353481 : scope = current_binding_level;
7939 : 5493353481 : outer = IDENTIFIER_BINDING (name);
7940 : : }
7941 : 5496024401 : outer_scope = outer ? outer->scope : NULL;
7942 : :
7943 : : /* Because we create class bindings lazily, we might be missing a
7944 : : class binding for NAME. If there are any class binding levels
7945 : : between the LAST_BINDING_LEVEL and the scope in which OUTER was
7946 : : declared, we must lookup NAME in those class scopes. */
7947 : 5496024401 : if (class_p)
7948 : 12955671638 : while (scope && scope != outer_scope && scope->kind != sk_namespace)
7949 : : {
7950 : 9472115143 : if (scope->kind == sk_class)
7951 : : {
7952 : 1463567824 : cxx_binding *class_binding;
7953 : :
7954 : 1463567824 : class_binding = get_class_binding (name, scope);
7955 : 1463567824 : if (class_binding)
7956 : : {
7957 : : /* Thread this new class-scope binding onto the
7958 : : IDENTIFIER_BINDING list so that future lookups
7959 : : find it quickly. */
7960 : 128900053 : if (BASELINK_P (class_binding->value))
7961 : : /* Don't put a BASELINK in IDENTIFIER_BINDING. */
7962 : 36941363 : class_binding->value
7963 : 36941363 : = BASELINK_FUNCTIONS (class_binding->value);
7964 : 128900053 : class_binding->previous = outer;
7965 : 128900053 : if (binding)
7966 : 3 : binding->previous = class_binding;
7967 : : else
7968 : 128900050 : IDENTIFIER_BINDING (name) = class_binding;
7969 : 128900053 : return class_binding;
7970 : : }
7971 : : }
7972 : : /* If we are in a member template, the template parms of the member
7973 : : template are considered to be inside the scope of the containing
7974 : : class, but within G++ the class bindings are all pushed between the
7975 : : template parms and the function body. So if the outer binding is
7976 : : a template parm for the current scope, return it now rather than
7977 : : look for a class binding. */
7978 : 4163765069 : if (outer_scope && outer_scope->kind == sk_template_parms
7979 : 9693960062 : && binding_to_template_parms_of_scope_p (outer, scope))
7980 : : return outer;
7981 : :
7982 : 9195320696 : scope = scope->level_chain;
7983 : : }
7984 : :
7985 : : return outer;
7986 : : }
7987 : :
7988 : : /* Return the innermost block-scope or class-scope value binding for
7989 : : NAME, or NULL_TREE if there is no such binding. */
7990 : :
7991 : : tree
7992 : 812856708 : innermost_non_namespace_value (tree name)
7993 : : {
7994 : 812856708 : cxx_binding *binding;
7995 : 812856708 : binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
7996 : 812856708 : return binding ? binding->value : NULL_TREE;
7997 : : }
7998 : :
7999 : : /* True iff current_binding_level is within the potential scope of local
8000 : : variable DECL. */
8001 : :
8002 : : bool
8003 : 145 : decl_in_scope_p (tree decl)
8004 : : {
8005 : 145 : gcc_checking_assert (DECL_FUNCTION_SCOPE_P (decl));
8006 : :
8007 : 145 : tree name = DECL_NAME (decl);
8008 : :
8009 : 145 : for (cxx_binding *iter = NULL;
8010 : 152 : (iter = outer_binding (name, iter, /*class_p=*/false)); )
8011 : : {
8012 : 74 : if (!LOCAL_BINDING_P (iter))
8013 : : return false;
8014 : 74 : if (iter->value == decl)
8015 : : return true;
8016 : : }
8017 : :
8018 : : return false;
8019 : : }
8020 : :
8021 : : /* Look up NAME in the current binding level and its superiors in the
8022 : : namespace of variables, functions and typedefs. Return a ..._DECL
8023 : : node of some kind representing its definition if there is only one
8024 : : such declaration, or return a TREE_LIST with all the overloaded
8025 : : definitions if there are many, or return NULL_TREE if it is undefined.
8026 : : Hidden name, either friend declaration or built-in function, are
8027 : : not ignored.
8028 : :
8029 : : WHERE controls which scopes are considered. It is a bit mask of
8030 : : LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
8031 : : (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
8032 : : scopes). It is an error for no bits to be set. These scopes are
8033 : : searched from innermost to outermost.
8034 : :
8035 : : WANT controls what kind of entity we'd happy with.
8036 : : LOOK_want::NORMAL for normal lookup (implicit typedefs can be
8037 : : hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
8038 : : for only NAMESPACE_DECLS. These two can be bit-ored to find
8039 : : namespace or type.
8040 : :
8041 : : WANT can also have LOOK_want::HIDDEN_FRIEND or
8042 : : LOOK_want::HIDDEN_LAMBDa added to it. */
8043 : :
8044 : : tree
8045 : 4161435573 : lookup_name (tree name, LOOK_where where, LOOK_want want)
8046 : : {
8047 : 4161435573 : tree val = NULL_TREE;
8048 : :
8049 : 4161435573 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8050 : :
8051 : 4161435573 : gcc_checking_assert (unsigned (where) != 0);
8052 : : /* If we're looking for hidden lambda things, we shouldn't be
8053 : : looking in namespace scope. */
8054 : 4161435573 : gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
8055 : : || !bool (where & LOOK_where::NAMESPACE));
8056 : 4161435573 : query_oracle (name);
8057 : :
8058 : : /* Conversion operators are handled specially because ordinary
8059 : : unqualified name lookup will not find template conversion
8060 : : operators. */
8061 : 4161435573 : if (IDENTIFIER_CONV_OP_P (name))
8062 : : {
8063 : 624079 : cp_binding_level *level;
8064 : :
8065 : 624079 : for (level = current_binding_level;
8066 : 1750269 : level && level->kind != sk_namespace;
8067 : 1126190 : level = level->level_chain)
8068 : : {
8069 : 1218986 : tree class_type;
8070 : 1218986 : tree operators;
8071 : :
8072 : : /* A conversion operator can only be declared in a class
8073 : : scope. */
8074 : 1218986 : if (level->kind != sk_class)
8075 : 498789 : continue;
8076 : :
8077 : : /* Lookup the conversion operator in the class. */
8078 : 720197 : class_type = level->this_entity;
8079 : 720197 : operators = lookup_fnfields (class_type, name, /*protect=*/0,
8080 : : tf_warning_or_error);
8081 : 720197 : if (operators)
8082 : : return operators;
8083 : : }
8084 : :
8085 : : return NULL_TREE;
8086 : : }
8087 : :
8088 : : /* First, look in non-namespace scopes. */
8089 : :
8090 : 4160811494 : if (current_class_type == NULL_TREE)
8091 : : /* Maybe avoid searching the binding stack at all. */
8092 : 1660779513 : where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
8093 : :
8094 : 4160811494 : if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
8095 : : for (cxx_binding *iter = nullptr;
8096 : 4162975247 : (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
8097 : : {
8098 : : /* Skip entities we don't want. */
8099 : 3798325404 : if (!bool (where & (LOCAL_BINDING_P (iter)
8100 : : ? LOOK_where::BLOCK : LOOK_where::CLASS)))
8101 : 50903 : continue;
8102 : :
8103 : : /* If this is the kind of thing we're looking for, we're done. */
8104 : 3046191792 : if (iter->value)
8105 : : {
8106 : 3046191792 : tree binding = NULL_TREE;
8107 : :
8108 : 2985622655 : if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
8109 : 3046191727 : && (bool (want & LOOK_want::HIDDEN_LAMBDA)
8110 : 3045846017 : || !is_lambda_ignored_entity (iter->value))
8111 : 6089839816 : && qualify_lookup (iter->value, want))
8112 : 3043571889 : binding = iter->value;
8113 : 2619903 : else if (bool (want & LOOK_want::TYPE)
8114 : 76111 : && !HIDDEN_TYPE_BINDING_P (iter)
8115 : 2695999 : && iter->type)
8116 : 3046191792 : binding = iter->type;
8117 : :
8118 : 3046191792 : if (binding)
8119 : : {
8120 : 3043571959 : val = strip_using_decl (binding);
8121 : 3043571959 : break;
8122 : : }
8123 : : }
8124 : : }
8125 : :
8126 : : /* Now lookup in namespace scopes. */
8127 : 4160811494 : if (!val && bool (where & LOOK_where::NAMESPACE))
8128 : : {
8129 : 1117239319 : name_lookup lookup (name, want);
8130 : 1117239319 : if (lookup.search_unqualified
8131 : 1117239319 : (current_decl_namespace (), current_binding_level))
8132 : 855676231 : val = lookup.value;
8133 : 1117239319 : }
8134 : :
8135 : : /* If we have a known type overload, pull it out. This can happen
8136 : : for both using decls and unhidden functions. */
8137 : 4160811494 : if (val && TREE_CODE (val) == OVERLOAD && TREE_TYPE (val) != unknown_type_node)
8138 : 2597275 : val = OVL_FUNCTION (val);
8139 : :
8140 : : return val;
8141 : 4161435573 : }
8142 : :
8143 : : tree
8144 : 277924518 : lookup_name (tree name)
8145 : : {
8146 : 277924518 : return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
8147 : : }
8148 : :
8149 : : /* Look up NAME for type used in elaborated name specifier in
8150 : : the scopes given by HOW.
8151 : :
8152 : : Unlike lookup_name_1, we make sure that NAME is actually
8153 : : declared in the desired scope, not from inheritance, nor using
8154 : : directive. For using declaration, there is DR138 still waiting
8155 : : to be resolved. Hidden name coming from an earlier friend
8156 : : declaration is also returned, and will be made visible unless HOW
8157 : : is TAG_how::HIDDEN_FRIEND.
8158 : :
8159 : : A TYPE_DECL best matching the NAME is returned. Catching error
8160 : : and issuing diagnostics are caller's responsibility. */
8161 : :
8162 : : tree
8163 : 20088729 : lookup_elaborated_type (tree name, TAG_how how)
8164 : : {
8165 : 20088729 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8166 : :
8167 : 20088729 : cp_binding_level *b = current_binding_level;
8168 : :
8169 : 20088729 : if (b->kind != sk_namespace)
8170 : : /* Look in non-namespace scopes. */
8171 : : for (cxx_binding *iter = NULL;
8172 : 14811443 : (iter = outer_binding (name, iter, /*class_p=*/ true)); )
8173 : : {
8174 : : /* First check we're supposed to be looking in this scope --
8175 : : if we're not, we're done. */
8176 : 613073 : for (; b != iter->scope; b = b->level_chain)
8177 : 390669 : if (!(b->kind == sk_cleanup
8178 : 276293 : || b->kind == sk_template_parms
8179 : 114385 : || b->kind == sk_function_parms
8180 : 114355 : || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8181 : : return NULL_TREE;
8182 : :
8183 : : /* Check if this is the kind of thing we're looking for. If
8184 : : HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
8185 : : come from base class. For ITER->VALUE, we can simply use
8186 : : INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
8187 : : our own check.
8188 : :
8189 : : We check ITER->TYPE before ITER->VALUE in order to handle
8190 : : typedef struct C {} C;
8191 : : correctly. */
8192 : :
8193 : 336777 : if (tree type = strip_using_decl (iter->type))
8194 : : {
8195 : 61092 : if (qualify_lookup (type, LOOK_want::TYPE)
8196 : 61092 : && (how != TAG_how::CURRENT_ONLY
8197 : 117 : || LOCAL_BINDING_P (iter)
8198 : 117 : || DECL_CONTEXT (type) == iter->scope->this_entity))
8199 : : {
8200 : 61044 : if (how != TAG_how::HIDDEN_FRIEND)
8201 : : /* It is no longer a hidden binding. */
8202 : 72 : HIDDEN_TYPE_BINDING_P (iter) = false;
8203 : :
8204 : 61044 : return type;
8205 : : }
8206 : : }
8207 : : else
8208 : : {
8209 : 275685 : tree value = strip_using_decl (iter->value);
8210 : 275685 : if (qualify_lookup (value, LOOK_want::TYPE)
8211 : 275685 : && (how != TAG_how::CURRENT_ONLY
8212 : 50136 : || !INHERITED_VALUE_BINDING_P (iter)))
8213 : : {
8214 : 275556 : if (how != TAG_how::HIDDEN_FRIEND && !iter->type)
8215 : : /* It is no longer a hidden binding. */
8216 : 50142 : HIDDEN_TYPE_BINDING_P (iter) = false;
8217 : :
8218 : 275556 : return value;
8219 : : }
8220 : : }
8221 : : }
8222 : :
8223 : : /* Now check if we can look in namespace scope. */
8224 : 32262362 : for (; b->kind != sk_namespace; b = b->level_chain)
8225 : : if (!(b->kind == sk_cleanup
8226 : : || b->kind == sk_template_parms
8227 : : || b->kind == sk_function_parms
8228 : 3564431 : || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
8229 : : return NULL_TREE;
8230 : :
8231 : : /* Look in the innermost namespace. */
8232 : 16504840 : tree ns = b->this_entity;
8233 : 16504840 : if (tree *slot = find_namespace_slot (ns, name))
8234 : : {
8235 : 2703023 : tree bind = *slot;
8236 : 2703023 : if (TREE_CODE (bind) == BINDING_VECTOR)
8237 : 81 : bind = BINDING_VECTOR_CLUSTER (bind, 0).slots[BINDING_SLOT_CURRENT];
8238 : :
8239 : 81 : if (bind)
8240 : : {
8241 : : /* If this is the kind of thing we're looking for, we're done. */
8242 : 2702963 : if (tree type = strip_using_decl (MAYBE_STAT_TYPE (bind)))
8243 : : {
8244 : 165 : if (how != TAG_how::HIDDEN_FRIEND)
8245 : : /* No longer hidden. */
8246 : 165 : STAT_TYPE_HIDDEN_P (*slot) = false;
8247 : :
8248 : 165 : return type;
8249 : : }
8250 : 2702798 : else if (tree decl = strip_using_decl (MAYBE_STAT_DECL (bind)))
8251 : : {
8252 : 2702798 : if (qualify_lookup (decl, LOOK_want::TYPE))
8253 : : {
8254 : 2390009 : if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (bind)
8255 : 2771483 : && STAT_DECL_HIDDEN_P (bind))
8256 : : {
8257 : 71111 : if (STAT_TYPE (bind))
8258 : 0 : STAT_DECL_HIDDEN_P (bind) = false;
8259 : : else
8260 : : {
8261 : : /* There is no type, just remove the stat
8262 : : hack. */
8263 : 71111 : if (*slot == bind)
8264 : 71108 : *slot = decl;
8265 : : else
8266 : 3 : BINDING_VECTOR_CLUSTER (*slot, 0)
8267 : 3 : .slots[BINDING_SLOT_CURRENT] = decl;
8268 : : }
8269 : : }
8270 : 2700358 : return decl;
8271 : : }
8272 : : }
8273 : : }
8274 : :
8275 : 2500 : if (TREE_CODE (*slot) == BINDING_VECTOR)
8276 : : {
8277 : : /* We could be redeclaring a global module entity, (from GMF
8278 : : or header unit), or from another partition, or
8279 : : specializing an imported template. */
8280 : 60 : bitmap imports = get_import_bitmap ();
8281 : 60 : binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (*slot);
8282 : :
8283 : : /* Scan the imported bindings. */
8284 : 60 : unsigned ix = BINDING_VECTOR_NUM_CLUSTERS (*slot);
8285 : 60 : if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED)
8286 : : {
8287 : 60 : ix--;
8288 : 60 : cluster++;
8289 : : }
8290 : :
8291 : : /* Do this in forward order, so we load modules in an order
8292 : : the user expects. */
8293 : 64 : for (; ix--; cluster++)
8294 : 124 : for (unsigned jx = 0; jx != BINDING_VECTOR_SLOTS_PER_CLUSTER; jx++)
8295 : : {
8296 : : /* Are we importing this module? */
8297 : 120 : if (unsigned base = cluster->indices[jx].base)
8298 : 56 : if (unsigned span = cluster->indices[jx].span)
8299 : 56 : do
8300 : 56 : if (bitmap_bit_p (imports, base))
8301 : 56 : goto found;
8302 : 0 : while (++base, --span);
8303 : 64 : continue;
8304 : :
8305 : 56 : found:;
8306 : : /* Is it loaded? */
8307 : 56 : if (cluster->slots[jx].is_lazy ())
8308 : : {
8309 : 36 : gcc_assert (cluster->indices[jx].span == 1);
8310 : 36 : lazy_load_binding (cluster->indices[jx].base,
8311 : : ns, name, &cluster->slots[jx]);
8312 : : }
8313 : 56 : tree bind = cluster->slots[jx];
8314 : 56 : if (!bind)
8315 : : /* Load errors could mean there's nothing here. */
8316 : 0 : continue;
8317 : :
8318 : : /* Extract what we can see from here. If there's no
8319 : : stat_hack, then everything was exported. */
8320 : 56 : tree type = NULL_TREE;
8321 : :
8322 : : /* If no stat hack, everything is visible. */
8323 : 56 : if (STAT_HACK_P (bind))
8324 : : {
8325 : 47 : if (STAT_TYPE_VISIBLE_P (bind))
8326 : 18 : type = STAT_TYPE (bind);
8327 : 47 : bind = STAT_VISIBLE (bind);
8328 : : }
8329 : :
8330 : 47 : if (type && qualify_lookup (type, LOOK_want::TYPE))
8331 : 3 : return strip_using_decl (type);
8332 : :
8333 : 53 : if (bind && qualify_lookup (bind, LOOK_want::TYPE))
8334 : 53 : return strip_using_decl (bind);
8335 : 64 : }
8336 : :
8337 : 4 : if (!module_purview_p ())
8338 : : {
8339 : : /* We're in the global module, perhaps there's a tag
8340 : : there? */
8341 : :
8342 : : /* FIXME: In general we should probably merge global module
8343 : : classes in check_module_override rather than here, but for
8344 : : GCC14 let's just fix lazy declarations of __class_type_info in
8345 : : build_dynamic_cast_1. */
8346 : 4 : if (current_namespace == abi_node)
8347 : : {
8348 : 4 : tree g = (BINDING_VECTOR_CLUSTER (*slot, 0)
8349 : 4 : .slots[BINDING_SLOT_GLOBAL]);
8350 : 4 : for (ovl_iterator iter (g); iter; ++iter)
8351 : 4 : if (qualify_lookup (*iter, LOOK_want::TYPE))
8352 : 4 : return *iter;
8353 : : }
8354 : : }
8355 : : }
8356 : : }
8357 : :
8358 : : return NULL_TREE;
8359 : 20088729 : }
8360 : :
8361 : : /* The type TYPE is being declared. If it is a class template, or a
8362 : : specialization of a class template, do any processing required and
8363 : : perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8364 : : being declared a friend. B is the binding level at which this TYPE
8365 : : should be bound.
8366 : :
8367 : : Returns the TYPE_DECL for TYPE, which may have been altered by this
8368 : : processing. */
8369 : :
8370 : : static tree
8371 : 19408386 : maybe_process_template_type_declaration (tree type, int is_friend,
8372 : : cp_binding_level *b)
8373 : : {
8374 : 19408386 : tree decl = TYPE_NAME (type);
8375 : :
8376 : 19408386 : if (processing_template_parmlist)
8377 : : /* You can't declare a new template type in a template parameter
8378 : : list. But, you can declare a non-template type:
8379 : :
8380 : : template <class A*> struct S;
8381 : :
8382 : : is a forward-declaration of `A'. */
8383 : : ;
8384 : 19408256 : else if (b->kind == sk_namespace
8385 : 5149250 : && current_binding_level->kind != sk_namespace)
8386 : : /* If this new type is being injected into a containing scope,
8387 : : then it's not a template type. */
8388 : : ;
8389 : : else
8390 : : {
8391 : 19343028 : gcc_assert (MAYBE_CLASS_TYPE_P (type)
8392 : : || TREE_CODE (type) == ENUMERAL_TYPE);
8393 : :
8394 : 19343028 : if (processing_template_decl)
8395 : : {
8396 : 11866141 : decl = push_template_decl (decl, is_friend);
8397 : 11866141 : if (decl == error_mark_node)
8398 : : return error_mark_node;
8399 : :
8400 : : /* If the current binding level is the binding level for the
8401 : : template parameters (see the comment in
8402 : : begin_template_parm_list) and the enclosing level is a class
8403 : : scope, and we're not looking at a friend, push the
8404 : : declaration of the member class into the class scope. In the
8405 : : friend case, push_template_decl will already have put the
8406 : : friend into global scope, if appropriate. */
8407 : 11866120 : if (TREE_CODE (type) != ENUMERAL_TYPE
8408 : 11560130 : && !is_friend && b->kind == sk_template_parms
8409 : 9573414 : && b->level_chain->kind == sk_class)
8410 : : {
8411 : 665171 : finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
8412 : :
8413 : 665171 : if (!COMPLETE_TYPE_P (current_class_type))
8414 : 665150 : maybe_add_class_template_decl_list (current_class_type,
8415 : : type, /*friend_p=*/0);
8416 : : }
8417 : : }
8418 : : }
8419 : :
8420 : : return decl;
8421 : : }
8422 : :
8423 : : /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8424 : : that the NAME is a class template, the tag is processed but not pushed.
8425 : :
8426 : : The pushed scope depend on the SCOPE parameter:
8427 : : - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8428 : : scope.
8429 : : - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8430 : : non-template-parameter scope. This case is needed for forward
8431 : : declarations.
8432 : : - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8433 : : TS_GLOBAL case except that names within template-parameter scopes
8434 : : are not pushed at all.
8435 : :
8436 : : Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8437 : :
8438 : : tree
8439 : 19408386 : pushtag (tree name, tree type, TAG_how how)
8440 : : {
8441 : 19408386 : tree decl;
8442 : :
8443 : 19408386 : gcc_assert (identifier_p (name));
8444 : :
8445 : 19408386 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8446 : :
8447 : 19408386 : cp_binding_level *b = current_binding_level;
8448 : 19476815 : while (true)
8449 : : {
8450 : 19476815 : if (/* Cleanup scopes are not scopes from the point of view of
8451 : : the language. */
8452 : 19476815 : b->kind == sk_cleanup
8453 : : /* Neither are function parameter scopes. */
8454 : 19476809 : || b->kind == sk_function_parms
8455 : : /* Neither are the scopes used to hold template parameters
8456 : : for an explicit specialization. For an ordinary template
8457 : : declaration, these scopes are not scopes from the point of
8458 : : view of the language. */
8459 : 19471880 : || (b->kind == sk_template_parms
8460 : 9982645 : && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
8461 : 4995 : b = b->level_chain;
8462 : 19471820 : else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
8463 : : {
8464 : 63434 : b = b->level_chain;
8465 : 63434 : if (b->kind == sk_template_parms)
8466 : 483 : b = b->level_chain;
8467 : : }
8468 : : else
8469 : : break;
8470 : : }
8471 : :
8472 : : /* Do C++ gratuitous typedefing. */
8473 : 19408386 : if (REAL_IDENTIFIER_TYPE_VALUE (name) != type)
8474 : : {
8475 : 19408386 : tree tdef;
8476 : 19408386 : tree context = TYPE_CONTEXT (type);
8477 : :
8478 : 19408386 : if (! context)
8479 : : {
8480 : : cp_binding_level *cb = b;
8481 : 31250142 : while (cb->kind != sk_namespace
8482 : 17192510 : && cb->kind != sk_class
8483 : 44958670 : && (cb->kind != sk_function_parms
8484 : 1594016 : || !cb->this_entity))
8485 : 12114512 : cb = cb->level_chain;
8486 : 19135630 : tree cs = cb->this_entity;
8487 : :
8488 : 19135630 : gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
8489 : : ? cs == current_function_decl
8490 : : : TYPE_P (cs) ? cs == current_class_type
8491 : : : cs == current_namespace);
8492 : :
8493 : 19135630 : if (how == TAG_how::CURRENT_ONLY
8494 : 201489 : || (cs && TREE_CODE (cs) == FUNCTION_DECL))
8495 : : context = cs;
8496 : 201393 : else if (cs && TYPE_P (cs))
8497 : : /* When declaring a friend class of a local class, we want
8498 : : to inject the newly named class into the scope
8499 : : containing the local class, not the namespace
8500 : : scope. */
8501 : 136276 : context = decl_function_context (get_type_decl (cs));
8502 : : }
8503 : 19135534 : if (!context)
8504 : 201393 : context = current_namespace;
8505 : :
8506 : 19408386 : tdef = create_implicit_typedef (name, type);
8507 : 19408386 : DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
8508 : 19408386 : set_originating_module (tdef);
8509 : :
8510 : 19408386 : decl = maybe_process_template_type_declaration
8511 : 19408386 : (type, how == TAG_how::HIDDEN_FRIEND, b);
8512 : 19408386 : if (decl == error_mark_node)
8513 : : return decl;
8514 : :
8515 : 19408365 : if (b->kind == sk_class)
8516 : : {
8517 : 2682502 : if (!TYPE_BEING_DEFINED (current_class_type))
8518 : : /* Don't push anywhere if the class is complete; a lambda in an
8519 : : NSDMI is not a member of the class. */
8520 : : ;
8521 : 2681503 : else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8522 : : /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8523 : : class. But if it's a member template class, we want
8524 : : the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8525 : : later. */
8526 : 2681489 : finish_member_declaration (decl);
8527 : : else
8528 : 14 : pushdecl_class_level (decl);
8529 : : }
8530 : 16725863 : else if (b->kind == sk_template_parms)
8531 : : {
8532 : : /* Do not push the tag here -- we'll want to push the
8533 : : TEMPLATE_DECL. */
8534 : 9982567 : if (b->level_chain->kind != sk_class)
8535 : 8908340 : set_identifier_type_value_with_scope (name, tdef, b->level_chain);
8536 : : }
8537 : : else
8538 : : {
8539 : 6743296 : decl = do_pushdecl_with_scope
8540 : 6743296 : (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
8541 : 6743296 : if (decl == error_mark_node)
8542 : : return decl;
8543 : :
8544 : 6743251 : if (DECL_CONTEXT (decl) == std_node
8545 : 2189540 : && init_list_identifier == DECL_NAME (TYPE_NAME (type))
8546 : 6743257 : && !CLASSTYPE_TEMPLATE_INFO (type))
8547 : : {
8548 : 6 : error ("declaration of %<std::initializer_list%> does not match "
8549 : : "%<#include <initializer_list>%>, isn%'t a template");
8550 : 6 : return error_mark_node;
8551 : : }
8552 : : }
8553 : :
8554 : 19408314 : TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
8555 : :
8556 : : /* If this is a local class, keep track of it. We need this
8557 : : information for name-mangling, and so that it is possible to
8558 : : find all function definitions in a translation unit in a
8559 : : convenient way. (It's otherwise tricky to find a member
8560 : : function definition it's only pointed to from within a local
8561 : : class.) */
8562 : 19408314 : if (TYPE_FUNCTION_SCOPE_P (type))
8563 : : {
8564 : 1593995 : if (processing_template_decl)
8565 : : {
8566 : : /* Push a DECL_EXPR so we call pushtag at the right time in
8567 : : template instantiation rather than in some nested context. */
8568 : 752628 : add_decl_expr (decl);
8569 : : }
8570 : : /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8571 : 1679372 : else if (!LAMBDA_TYPE_P (type))
8572 : 553663 : determine_local_discriminator (TYPE_NAME (type));
8573 : : }
8574 : : }
8575 : :
8576 : 19408314 : if (b->kind == sk_class
8577 : 19408314 : && !COMPLETE_TYPE_P (current_class_type))
8578 : 2681533 : maybe_add_class_template_decl_list (current_class_type,
8579 : : type, /*friend_p=*/0);
8580 : :
8581 : 19408314 : decl = TYPE_NAME (type);
8582 : 19408314 : gcc_assert (TREE_CODE (decl) == TYPE_DECL);
8583 : :
8584 : : /* Set type visibility now if this is a forward declaration. */
8585 : 19408314 : TREE_PUBLIC (decl) = 1;
8586 : 19408314 : determine_visibility (decl);
8587 : 19408314 : check_module_decl_linkage (decl);
8588 : :
8589 : 19408314 : return type;
8590 : 19408386 : }
8591 : :
8592 : : /* Subroutines for reverting temporarily to top-level for instantiation
8593 : : of templates and such. We actually need to clear out the class- and
8594 : : local-value slots of all identifiers, so that only the global values
8595 : : are at all visible. Simply setting current_binding_level to the global
8596 : : scope isn't enough, because more binding levels may be pushed. */
8597 : : struct saved_scope *scope_chain;
8598 : :
8599 : : /* Return true if ID has not already been marked. */
8600 : :
8601 : : static inline bool
8602 : 2371819586 : store_binding_p (tree id)
8603 : : {
8604 : 4739390117 : if (!id || !IDENTIFIER_BINDING (id))
8605 : : return false;
8606 : :
8607 : 2176206002 : if (IDENTIFIER_MARKED (id))
8608 : 0 : return false;
8609 : :
8610 : : return true;
8611 : : }
8612 : :
8613 : : /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8614 : : have enough space reserved. */
8615 : :
8616 : : static void
8617 : 903841128 : store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
8618 : : {
8619 : 903841128 : cxx_saved_binding saved;
8620 : :
8621 : 903841128 : gcc_checking_assert (store_binding_p (id));
8622 : :
8623 : 903841128 : IDENTIFIER_MARKED (id) = 1;
8624 : :
8625 : 903841128 : saved.identifier = id;
8626 : 903841128 : saved.binding = IDENTIFIER_BINDING (id);
8627 : 903841128 : saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
8628 : 903841128 : (*old_bindings)->quick_push (saved);
8629 : 903841128 : IDENTIFIER_BINDING (id) = NULL;
8630 : 903841128 : }
8631 : :
8632 : : static void
8633 : 451823034 : store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
8634 : : {
8635 : 451823034 : static vec<tree> bindings_need_stored;
8636 : 451823034 : tree t, id;
8637 : 451823034 : size_t i;
8638 : :
8639 : 451823034 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8640 : 1283829889 : for (t = names; t; t = TREE_CHAIN (t))
8641 : : {
8642 : 380183821 : if (TREE_CODE (t) == TREE_LIST)
8643 : 10740570 : id = TREE_PURPOSE (t);
8644 : : else
8645 : 369443251 : id = DECL_NAME (t);
8646 : :
8647 : 380183821 : if (store_binding_p (id))
8648 : 368523746 : bindings_need_stored.safe_push (id);
8649 : : }
8650 : 451823034 : if (!bindings_need_stored.is_empty ())
8651 : : {
8652 : 142572548 : vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8653 : 653668842 : for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8654 : : {
8655 : : /* We can apparently have duplicates in NAMES. */
8656 : 368523746 : if (store_binding_p (id))
8657 : 368523686 : store_binding (id, old_bindings);
8658 : : }
8659 : 142572548 : bindings_need_stored.truncate (0);
8660 : : }
8661 : 451823034 : }
8662 : :
8663 : : /* Like store_bindings, but NAMES is a vector of cp_class_binding
8664 : : objects, rather than a TREE_LIST. */
8665 : :
8666 : : static void
8667 : 272956588 : store_class_bindings (vec<cp_class_binding, va_gc> *names,
8668 : : vec<cxx_saved_binding, va_gc> **old_bindings)
8669 : : {
8670 : 272956588 : static vec<tree> bindings_need_stored;
8671 : 272956588 : size_t i;
8672 : 272956588 : cp_class_binding *cb;
8673 : :
8674 : 992227479 : for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
8675 : 719270891 : if (store_binding_p (cb->identifier))
8676 : 535317442 : bindings_need_stored.safe_push (cb->identifier);
8677 : 272956588 : if (!bindings_need_stored.is_empty ())
8678 : : {
8679 : 59977875 : tree id;
8680 : 59977875 : vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
8681 : 655273192 : for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
8682 : 535317442 : store_binding (id, old_bindings);
8683 : 59977875 : bindings_need_stored.truncate (0);
8684 : : }
8685 : 272956588 : }
8686 : :
8687 : : /* A chain of saved_scope structures awaiting reuse. */
8688 : :
8689 : : static GTY((deletable)) struct saved_scope *free_saved_scope;
8690 : :
8691 : : /* Temporarily make the current scope the global namespace, saving away
8692 : : the current scope for pop_from_top_level. */
8693 : :
8694 : : void
8695 : 355673569 : push_to_top_level (void)
8696 : : {
8697 : 355673569 : struct saved_scope *s;
8698 : 355673569 : cp_binding_level *b;
8699 : 355673569 : cxx_saved_binding *sb;
8700 : 355673569 : size_t i;
8701 : 355673569 : bool need_pop;
8702 : :
8703 : 355673569 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8704 : :
8705 : : /* Reuse or create a new structure for this saved scope. */
8706 : 355673569 : if (free_saved_scope != NULL)
8707 : : {
8708 : 354494828 : s = free_saved_scope;
8709 : 354494828 : free_saved_scope = s->prev;
8710 : :
8711 : 354494828 : vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
8712 : 354494828 : memset (s, 0, sizeof (*s));
8713 : : /* Also reuse the structure's old_bindings vector. */
8714 : 354494828 : vec_safe_truncate (old_bindings, 0);
8715 : 354494828 : s->old_bindings = old_bindings;
8716 : : }
8717 : : else
8718 : 1178741 : s = ggc_cleared_alloc<saved_scope> ();
8719 : :
8720 : 355673569 : b = scope_chain ? current_binding_level : 0;
8721 : :
8722 : : /* If we're in the middle of some function, save our state. */
8723 : 355673569 : if (cfun)
8724 : : {
8725 : 67404816 : need_pop = true;
8726 : 67404816 : push_function_context ();
8727 : : }
8728 : : else
8729 : : need_pop = false;
8730 : :
8731 : 355673569 : if (scope_chain && previous_class_level)
8732 : 98525434 : store_class_bindings (previous_class_level->class_shadowed,
8733 : : &s->old_bindings);
8734 : :
8735 : : /* Save and clear any IDENTIFIER_BINDING from local scopes. */
8736 : 807496603 : for (; b; b = b->level_chain)
8737 : : {
8738 : 807401207 : tree t;
8739 : :
8740 : : /* We don't need to consider namespace scopes, they don't affect
8741 : : IDENTIFIER_BINDING. */
8742 : 807401207 : if (b->kind == sk_namespace)
8743 : : {
8744 : : /* Jump straight to '::'. */
8745 : 355578173 : b = NAMESPACE_LEVEL (global_namespace);
8746 : 355578173 : break;
8747 : : }
8748 : :
8749 : 451823034 : store_bindings (b->names, &s->old_bindings);
8750 : : /* We also need to check class_shadowed to save class-level type
8751 : : bindings, since pushclass doesn't fill in b->names. */
8752 : 451823034 : if (b->kind == sk_class)
8753 : 174431154 : store_class_bindings (b->class_shadowed, &s->old_bindings);
8754 : :
8755 : : /* Unwind type-value slots back to top level. */
8756 : 792871583 : for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
8757 : 341048549 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
8758 : : }
8759 : :
8760 : 1259514697 : FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
8761 : 903841128 : IDENTIFIER_MARKED (sb->identifier) = 0;
8762 : :
8763 : 355673569 : s->prev = scope_chain;
8764 : 355673569 : s->bindings = b;
8765 : 355673569 : s->need_pop_function_context = need_pop;
8766 : 355673569 : s->function_decl = current_function_decl;
8767 : 355673569 : s->unevaluated_operand = cp_unevaluated_operand;
8768 : 355673569 : s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8769 : 355673569 : s->suppress_location_wrappers = suppress_location_wrappers;
8770 : 355673569 : s->x_stmt_tree.stmts_are_full_exprs_p = true;
8771 : :
8772 : 355673569 : scope_chain = s;
8773 : 355673569 : current_function_decl = NULL_TREE;
8774 : 355673569 : current_lang_base = NULL;
8775 : 355673569 : current_lang_name = lang_name_cplusplus;
8776 : 355673569 : current_namespace = global_namespace;
8777 : 355673569 : push_class_stack ();
8778 : 355673569 : cp_unevaluated_operand = 0;
8779 : 355673569 : c_inhibit_evaluation_warnings = 0;
8780 : 355673569 : suppress_location_wrappers = 0;
8781 : 355673569 : }
8782 : :
8783 : : void
8784 : 355551083 : pop_from_top_level (void)
8785 : : {
8786 : 355551083 : struct saved_scope *s = scope_chain;
8787 : 355551083 : cxx_saved_binding *saved;
8788 : 355551083 : size_t i;
8789 : :
8790 : 355551083 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8791 : :
8792 : 355551083 : pop_class_stack ();
8793 : :
8794 : 355551083 : release_tree_vector (current_lang_base);
8795 : :
8796 : 355551083 : scope_chain = s->prev;
8797 : 1259389397 : FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
8798 : : {
8799 : 903838314 : tree id = saved->identifier;
8800 : :
8801 : 903838314 : IDENTIFIER_BINDING (id) = saved->binding;
8802 : 903838314 : SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
8803 : : }
8804 : :
8805 : : /* If we were in the middle of compiling a function, restore our
8806 : : state. */
8807 : 355551083 : if (s->need_pop_function_context)
8808 : 67404795 : pop_function_context ();
8809 : 355551083 : current_function_decl = s->function_decl;
8810 : 355551083 : cp_unevaluated_operand = s->unevaluated_operand;
8811 : 355551083 : c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
8812 : 355551083 : suppress_location_wrappers = s->suppress_location_wrappers;
8813 : :
8814 : : /* Make this saved_scope structure available for reuse by
8815 : : push_to_top_level. */
8816 : 355551083 : s->prev = free_saved_scope;
8817 : 355551083 : free_saved_scope = s;
8818 : 355551083 : }
8819 : :
8820 : : namespace {
8821 : :
8822 : : /* Helper class for saving/restoring relevant global flags for the
8823 : : function-local case of maybe_push_to_top_level. */
8824 : :
8825 : : struct local_state_t
8826 : : {
8827 : : int cp_unevaluated_operand;
8828 : : int c_inhibit_evaluation_warnings;
8829 : : int cp_noexcept_operand_;
8830 : :
8831 : : static local_state_t
8832 : 1836543 : save_and_clear ()
8833 : : {
8834 : 1836543 : local_state_t s;
8835 : 1836543 : s.cp_unevaluated_operand = ::cp_unevaluated_operand;
8836 : 1836543 : ::cp_unevaluated_operand = 0;
8837 : 1836543 : s.c_inhibit_evaluation_warnings = ::c_inhibit_evaluation_warnings;
8838 : 1836543 : ::c_inhibit_evaluation_warnings = 0;
8839 : 1836543 : s.cp_noexcept_operand_ = ::cp_noexcept_operand;
8840 : 1836543 : ::cp_noexcept_operand = 0;
8841 : 1836543 : return s;
8842 : : }
8843 : :
8844 : : void
8845 : 1836543 : restore () const
8846 : : {
8847 : 1836543 : ::cp_unevaluated_operand = this->cp_unevaluated_operand;
8848 : 1836543 : ::c_inhibit_evaluation_warnings = this->c_inhibit_evaluation_warnings;
8849 : 1836543 : ::cp_noexcept_operand = this->cp_noexcept_operand_;
8850 : : }
8851 : : };
8852 : :
8853 : : vec<local_state_t> local_state_stack;
8854 : :
8855 : : } // anon namespace
8856 : :
8857 : : /* Like push_to_top_level, but not if D is function-local. Returns whether we
8858 : : did push to top. */
8859 : :
8860 : : bool
8861 : 51725826 : maybe_push_to_top_level (tree d)
8862 : : {
8863 : : /* Push if D isn't function-local, or is a lambda function, for which name
8864 : : resolution is already done. */
8865 : 51725826 : const bool push_to_top
8866 : 52423338 : = (LAMBDA_FUNCTION_P (d)
8867 : 51548471 : || (TREE_CODE (d) == TYPE_DECL
8868 : 25483503 : && TREE_TYPE (d)
8869 : 50549968 : && LAMBDA_TYPE_P (TREE_TYPE (d)))
8870 : 50930200 : || !current_function_decl
8871 : 65734439 : || !decl_function_context (d));
8872 : :
8873 : 51725826 : if (push_to_top)
8874 : 49889283 : push_to_top_level ();
8875 : : else
8876 : : {
8877 : 1836543 : gcc_assert (!processing_template_decl);
8878 : 1836543 : push_function_context ();
8879 : 1836543 : local_state_stack.safe_push (local_state_t::save_and_clear ());
8880 : : }
8881 : :
8882 : 51725826 : return push_to_top;
8883 : : }
8884 : :
8885 : : /* Return from whatever maybe_push_to_top_level did. */
8886 : :
8887 : : void
8888 : 51725817 : maybe_pop_from_top_level (bool push_to_top)
8889 : : {
8890 : 51725817 : if (push_to_top)
8891 : 49889274 : pop_from_top_level ();
8892 : : else
8893 : : {
8894 : 1836543 : local_state_stack.pop ().restore ();
8895 : 1836543 : pop_function_context ();
8896 : : }
8897 : 51725817 : }
8898 : :
8899 : : /* Push into the scope of the namespace NS, even if it is deeply
8900 : : nested within another namespace. */
8901 : :
8902 : : void
8903 : 80211678 : push_nested_namespace (tree ns)
8904 : : {
8905 : 80211678 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8906 : 80211678 : if (ns == global_namespace)
8907 : 39870116 : push_to_top_level ();
8908 : : else
8909 : : {
8910 : 79560817 : push_nested_namespace (CP_DECL_CONTEXT (ns));
8911 : 40341562 : resume_scope (NAMESPACE_LEVEL (ns));
8912 : 40341562 : current_namespace = ns;
8913 : : }
8914 : 80211678 : }
8915 : :
8916 : : /* Pop back from the scope of the namespace NS, which was previously
8917 : : entered with push_nested_namespace. */
8918 : :
8919 : : void
8920 : 39859322 : pop_nested_namespace (tree ns)
8921 : : {
8922 : 39859322 : auto_cond_timevar tv (TV_NAME_LOOKUP);
8923 : 120060206 : while (ns != global_namespace)
8924 : : {
8925 : 40341562 : ns = CP_DECL_CONTEXT (ns);
8926 : 40341562 : current_namespace = ns;
8927 : 40341562 : leave_scope ();
8928 : : }
8929 : :
8930 : 39859322 : pop_from_top_level ();
8931 : 39859322 : }
8932 : :
8933 : : /* Add TARGET to USINGS, if it does not already exist there. We used
8934 : : to build the complete graph of usings at this point, from the POV
8935 : : of the source namespaces. Now we build that as we perform the
8936 : : unqualified search. */
8937 : :
8938 : : static void
8939 : 135113 : add_using_namespace (vec<tree, va_gc> *&usings, tree target)
8940 : : {
8941 : 135113 : if (usings)
8942 : 1540 : for (unsigned ix = usings->length (); ix--;)
8943 : 1005 : if ((*usings)[ix] == target)
8944 : : return;
8945 : :
8946 : 134658 : if (modules_p ())
8947 : : {
8948 : 948 : tree u = build_lang_decl (USING_DECL, NULL_TREE, NULL_TREE);
8949 : 948 : USING_DECL_DECLS (u) = target;
8950 : 948 : DECL_MODULE_EXPORT_P (u) = module_exporting_p ();
8951 : 948 : DECL_MODULE_PURVIEW_P (u) = module_purview_p ();
8952 : 948 : target = u;
8953 : : }
8954 : 134658 : vec_safe_push (usings, target);
8955 : : }
8956 : :
8957 : : /* Convenience overload for the above, taking the user as its first
8958 : : parameter. */
8959 : :
8960 : : void
8961 : 79 : add_using_namespace (tree ns, tree target)
8962 : : {
8963 : 158 : add_using_namespace (NAMESPACE_LEVEL (ns)->using_directives,
8964 : 79 : ORIGINAL_NAMESPACE (target));
8965 : 79 : }
8966 : :
8967 : : /* Tell the debug system of a using directive. */
8968 : :
8969 : : static void
8970 : 178287 : emit_debug_info_using_namespace (tree from, tree target, bool implicit)
8971 : : {
8972 : : /* Emit debugging info. */
8973 : 178287 : tree context = from != global_namespace ? from : NULL_TREE;
8974 : 178287 : debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
8975 : : implicit);
8976 : 178287 : }
8977 : :
8978 : : /* Process a using directive. */
8979 : :
8980 : : void
8981 : 133757 : finish_using_directive (tree target, tree attribs)
8982 : : {
8983 : 133757 : if (target == error_mark_node)
8984 : : return;
8985 : :
8986 : 133737 : if (current_binding_level->kind != sk_namespace)
8987 : 102735 : add_stmt (build_stmt (input_location, USING_STMT, target));
8988 : : else
8989 : 62004 : emit_debug_info_using_namespace (current_binding_level->this_entity,
8990 : 31002 : ORIGINAL_NAMESPACE (target), false);
8991 : :
8992 : 267474 : add_using_namespace (current_binding_level->using_directives,
8993 : 133737 : ORIGINAL_NAMESPACE (target));
8994 : :
8995 : 133737 : bool diagnosed = false;
8996 : 133737 : if (attribs != error_mark_node)
8997 : 133810 : for (tree a = attribs; a; a = TREE_CHAIN (a))
8998 : : {
8999 : 73 : tree name = get_attribute_name (a);
9000 : 73 : if (current_binding_level->kind == sk_namespace
9001 : 73 : && is_attribute_p ("strong", name))
9002 : : {
9003 : 12 : auto_diagnostic_group d;
9004 : 18 : if (warning (0, "%<strong%> using directive no longer supported")
9005 : 12 : && CP_DECL_CONTEXT (target) == current_namespace)
9006 : 6 : inform (DECL_SOURCE_LOCATION (target),
9007 : : "you can use an inline namespace instead");
9008 : 12 : }
9009 : 58 : else if ((flag_openmp || flag_openmp_simd)
9010 : 3 : && get_attribute_namespace (a) == omp_identifier
9011 : 64 : && (is_attribute_p ("directive", name)
9012 : 0 : || is_attribute_p ("sequence", name)
9013 : 0 : || is_attribute_p ("decl", name)))
9014 : : {
9015 : 3 : if (!diagnosed)
9016 : : {
9017 : 3 : if (tree ar = TREE_VALUE (a))
9018 : : {
9019 : 3 : tree d = TREE_VALUE (ar);
9020 : 3 : gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
9021 : 3 : error ("%<omp::%s%> not allowed to be specified in "
9022 : : "this context",
9023 : 3 : TREE_PUBLIC (d) ? "decl" : "directive");
9024 : : }
9025 : : else
9026 : 0 : error ("%<omp::%E%> not allowed to be specified in this "
9027 : : "context", name);
9028 : : diagnosed = true;
9029 : : }
9030 : : }
9031 : 58 : else if (!attribute_ignored_p (a))
9032 : 43 : warning (OPT_Wattributes, "%qD attribute directive ignored", name);
9033 : : }
9034 : : }
9035 : :
9036 : : /* Pushes X into the global namespace. */
9037 : :
9038 : : tree
9039 : 359716 : pushdecl_top_level (tree x)
9040 : : {
9041 : 359716 : auto_cond_timevar tv (TV_NAME_LOOKUP);
9042 : 359716 : push_to_top_level ();
9043 : 359716 : gcc_checking_assert (!DECL_CONTEXT (x));
9044 : 359716 : DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
9045 : 359716 : x = pushdecl_namespace_level (x);
9046 : 359716 : pop_from_top_level ();
9047 : 719432 : return x;
9048 : 359716 : }
9049 : :
9050 : : /* Pushes X into the global namespace and calls cp_finish_decl to
9051 : : register the variable, initializing it with INIT. */
9052 : :
9053 : : tree
9054 : 2146349 : pushdecl_top_level_and_finish (tree x, tree init)
9055 : : {
9056 : 2146349 : auto_cond_timevar tv (TV_NAME_LOOKUP);
9057 : 2146349 : push_to_top_level ();
9058 : 2146349 : gcc_checking_assert (!DECL_CONTEXT (x));
9059 : 2146349 : DECL_CONTEXT (x) = FROB_CONTEXT (global_namespace);
9060 : 2146349 : x = pushdecl_namespace_level (x);
9061 : 2146349 : cp_finish_decl (x, init, false, NULL_TREE, 0);
9062 : 2146349 : pop_from_top_level ();
9063 : 4292698 : return x;
9064 : 2146349 : }
9065 : :
9066 : : /* Enter the namespaces from current_namerspace to NS. */
9067 : :
9068 : : static int
9069 : 4926345 : push_inline_namespaces (tree ns)
9070 : : {
9071 : 4926345 : int count = 0;
9072 : 4926345 : if (ns != current_namespace)
9073 : : {
9074 : 15 : gcc_assert (ns != global_namespace);
9075 : 24 : count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
9076 : 15 : resume_scope (NAMESPACE_LEVEL (ns));
9077 : 15 : current_namespace = ns;
9078 : 15 : count++;
9079 : : }
9080 : 4926345 : return count;
9081 : : }
9082 : :
9083 : : /* SLOT is the (possibly empty) binding slot for NAME in CTX.
9084 : : Reuse or create a namespace NAME. NAME is null for the anonymous
9085 : : namespace. */
9086 : :
9087 : : static tree
9088 : 2230 : reuse_namespace (tree *slot, tree ctx, tree name)
9089 : : {
9090 : 2230 : if (modules_p () && *slot && TREE_PUBLIC (ctx) && name)
9091 : : {
9092 : : /* Public namespace. Shared. */
9093 : 917 : tree *global_slot = slot;
9094 : 917 : if (TREE_CODE (*slot) == BINDING_VECTOR)
9095 : 126 : global_slot = get_fixed_binding_slot (slot, name,
9096 : : BINDING_SLOT_GLOBAL, false);
9097 : :
9098 : 920 : for (ovl_iterator iter (*global_slot); iter; ++iter)
9099 : : {
9100 : 917 : tree decl = *iter;
9101 : :
9102 : 917 : if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl))
9103 : 914 : return decl;
9104 : : }
9105 : : }
9106 : : return NULL_TREE;
9107 : : }
9108 : :
9109 : : static tree
9110 : 736497 : make_namespace (tree ctx, tree name, location_t loc, bool inline_p)
9111 : : {
9112 : : /* Create the namespace. */
9113 : 736497 : tree ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
9114 : 736497 : DECL_SOURCE_LOCATION (ns) = loc;
9115 : 736497 : SCOPE_DEPTH (ns) = SCOPE_DEPTH (ctx) + 1;
9116 : 736497 : if (!SCOPE_DEPTH (ns))
9117 : : /* We only allow depth 255. */
9118 : 0 : sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx));
9119 : 736497 : DECL_CONTEXT (ns) = FROB_CONTEXT (ctx);
9120 : :
9121 : 736497 : if (!name)
9122 : : /* Anon-namespaces in different header-unit imports are distinct.
9123 : : But that's ok as their contents all have internal linkage.
9124 : : (This is different to how they'd behave as textual includes,
9125 : : but doing this at all is really odd source.) */
9126 : 1312 : SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
9127 : 735185 : else if (TREE_PUBLIC (ctx))
9128 : 735119 : TREE_PUBLIC (ns) = true;
9129 : :
9130 : 736497 : if (inline_p)
9131 : 145988 : DECL_NAMESPACE_INLINE_P (ns) = true;
9132 : :
9133 : 736497 : return ns;
9134 : : }
9135 : :
9136 : : /* NS was newly created, finish off making it. */
9137 : :
9138 : : static void
9139 : 736497 : make_namespace_finish (tree ns, tree *slot, bool from_import = false)
9140 : : {
9141 : 736497 : if (modules_p () && TREE_PUBLIC (ns) && (from_import || *slot != ns))
9142 : : {
9143 : : /* Merge into global slot. */
9144 : 1255 : tree *gslot = get_fixed_binding_slot (slot, DECL_NAME (ns),
9145 : : BINDING_SLOT_GLOBAL, true);
9146 : 1255 : *gslot = ns;
9147 : : }
9148 : :
9149 : 736497 : tree ctx = CP_DECL_CONTEXT (ns);
9150 : 736497 : cp_binding_level *scope = ggc_cleared_alloc<cp_binding_level> ();
9151 : 736497 : scope->this_entity = ns;
9152 : 736497 : scope->more_cleanups_ok = true;
9153 : 736497 : scope->kind = sk_namespace;
9154 : 736497 : scope->level_chain = NAMESPACE_LEVEL (ctx);
9155 : 736497 : NAMESPACE_LEVEL (ns) = scope;
9156 : :
9157 : 736497 : if (DECL_NAMESPACE_INLINE_P (ns))
9158 : 145988 : vec_safe_push (DECL_NAMESPACE_INLINEES (ctx), ns);
9159 : :
9160 : 736497 : if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns))
9161 : 147285 : emit_debug_info_using_namespace (ctx, ns, true);
9162 : :
9163 : : /* An unnamed namespace implicitly has a using-directive inserted so
9164 : : that its contents are usable in the surrounding context. */
9165 : 736497 : if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
9166 : 1297 : add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns);
9167 : 736497 : }
9168 : :
9169 : : /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
9170 : : then we enter an anonymous namespace. If MAKE_INLINE is true, then
9171 : : we create an inline namespace (it is up to the caller to check upon
9172 : : redefinition). Return the number of namespaces entered. */
9173 : :
9174 : : int
9175 : 5661544 : push_namespace (tree name, bool make_inline)
9176 : : {
9177 : 5661544 : auto_cond_timevar tv (TV_NAME_LOOKUP);
9178 : 5661544 : int count = 0;
9179 : :
9180 : : /* We should not get here if the global_namespace is not yet constructed
9181 : : nor if NAME designates the global namespace: The global scope is
9182 : : constructed elsewhere. */
9183 : 5661544 : gcc_checking_assert (global_namespace != NULL && name != global_identifier);
9184 : :
9185 : 5661544 : tree ns = NULL_TREE;
9186 : 5661544 : {
9187 : 5661544 : name_lookup lookup (name);
9188 : 5661544 : if (!lookup.search_qualified (current_namespace, /*usings=*/false))
9189 : : ;
9190 : 4926345 : else if (TREE_CODE (lookup.value) == TREE_LIST)
9191 : : {
9192 : : /* An ambiguous lookup. If exactly one is a namespace, we
9193 : : want that. If more than one is a namespace, error, but
9194 : : pick one of them. */
9195 : : /* DR2061 can cause us to find multiple namespaces of the same
9196 : : name. We must treat that carefully and avoid thinking we
9197 : : need to push a new (possibly) duplicate namespace. Hey,
9198 : : if you want to use the same identifier within an inline
9199 : : nest, knock yourself out. */
9200 : 9 : for (tree *chain = &lookup.value, next; (next = *chain);)
9201 : : {
9202 : 6 : tree decl = TREE_VALUE (next);
9203 : 6 : if (TREE_CODE (decl) == NAMESPACE_DECL)
9204 : : {
9205 : 6 : if (!ns)
9206 : : ns = decl;
9207 : 3 : else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
9208 : 6 : ns = decl;
9209 : :
9210 : : /* Advance. */
9211 : 6 : chain = &TREE_CHAIN (next);
9212 : : }
9213 : : else
9214 : : /* Stitch out. */
9215 : 0 : *chain = TREE_CHAIN (next);
9216 : : }
9217 : :
9218 : 3 : if (TREE_CHAIN (lookup.value))
9219 : : {
9220 : 3 : error ("%<namespace %E%> is ambiguous", name);
9221 : 3 : print_candidates (lookup.value);
9222 : : }
9223 : : }
9224 : 4926342 : else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
9225 : : ns = lookup.value;
9226 : :
9227 : 4926333 : if (ns)
9228 : 4926333 : if (tree dna = DECL_NAMESPACE_ALIAS (ns))
9229 : : {
9230 : : /* A namespace alias is not allowed here, but if the alias
9231 : : is for a namespace also inside the current scope,
9232 : : accept it with a diagnostic. That's better than dying
9233 : : horribly. */
9234 : 9 : if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
9235 : : {
9236 : 6 : error ("namespace alias %qD not allowed here, "
9237 : : "assuming %qD", ns, dna);
9238 : 6 : ns = dna;
9239 : : }
9240 : : else
9241 : : ns = NULL_TREE;
9242 : : }
9243 : 5661544 : }
9244 : :
9245 : 5661544 : if (ns)
9246 : : {
9247 : : /* DR2061. NS might be a member of an inline namespace. We
9248 : : need to push into those namespaces. */
9249 : 4926330 : if (modules_p ())
9250 : : {
9251 : 37268 : for (tree parent, ctx = ns; ctx != current_namespace;
9252 : : ctx = parent)
9253 : : {
9254 : 18634 : parent = CP_DECL_CONTEXT (ctx);
9255 : :
9256 : 18634 : tree bind = *find_namespace_slot (parent, DECL_NAME (ctx), false);
9257 : 18634 : if (bind != ctx)
9258 : : {
9259 : 129 : auto &cluster = BINDING_VECTOR_CLUSTER (bind, 0);
9260 : 129 : binding_slot &slot = cluster.slots[BINDING_SLOT_CURRENT];
9261 : 129 : gcc_checking_assert (!(tree)slot || (tree)slot == ctx);
9262 : 129 : slot = ctx;
9263 : : }
9264 : : }
9265 : : }
9266 : :
9267 : 4926330 : count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
9268 : 4926330 : if (DECL_SOURCE_LOCATION (ns) == BUILTINS_LOCATION)
9269 : : /* It's not builtin now. */
9270 : 32917 : DECL_SOURCE_LOCATION (ns) = input_location;
9271 : : }
9272 : : else
9273 : : {
9274 : : /* Before making a new namespace, see if we already have one in
9275 : : the existing partitions of the current namespace. */
9276 : 735214 : tree *slot = find_namespace_slot (current_namespace, name, false);
9277 : 735214 : if (slot)
9278 : 33 : ns = reuse_namespace (slot, current_namespace, name);
9279 : 735214 : if (!ns)
9280 : 735202 : ns = make_namespace (current_namespace, name,
9281 : : input_location, make_inline);
9282 : :
9283 : 735214 : if (pushdecl (ns) == error_mark_node)
9284 : : ns = NULL_TREE;
9285 : : else
9286 : : {
9287 : : /* Finish up making the namespace. */
9288 : 735202 : add_decl_to_level (NAMESPACE_LEVEL (current_namespace), ns);
9289 : 735202 : if (!slot)
9290 : : {
9291 : 735181 : slot = find_namespace_slot (current_namespace, name);
9292 : : /* This should find the slot created by pushdecl. */
9293 : 735181 : gcc_checking_assert (slot && *slot == ns);
9294 : : }
9295 : : else
9296 : : {
9297 : : /* pushdecl could have expanded the hash table, so
9298 : : slot might be invalid. */
9299 : 21 : slot = find_namespace_slot (current_namespace, name);
9300 : 21 : gcc_checking_assert (slot);
9301 : : }
9302 : 735202 : make_namespace_finish (ns, slot);
9303 : : }
9304 : : }
9305 : :
9306 : 5661532 : if (ns)
9307 : : {
9308 : : /* A public namespace is exported only if explicitly marked, or
9309 : : it contains exported entities. */
9310 : 5661532 : if (module_exporting_p ())
9311 : : {
9312 : 8934 : if (TREE_PUBLIC (ns))
9313 : 8891 : DECL_MODULE_EXPORT_P (ns) = true;
9314 : 43 : else if (!header_module_p ())
9315 : : {
9316 : 12 : if (name)
9317 : : {
9318 : 6 : auto_diagnostic_group d;
9319 : 6 : error_at (input_location, "exporting namespace %qD with "
9320 : : "internal linkage", ns);
9321 : 6 : inform (input_location, "%qD has internal linkage because "
9322 : : "it was declared in an unnamed namespace", ns);
9323 : 6 : }
9324 : : else
9325 : 6 : error_at (input_location, "exporting unnamed namespace");
9326 : : }
9327 : : }
9328 : 5661532 : if (module_purview_p ())
9329 : 10026 : DECL_MODULE_PURVIEW_P (ns) = true;
9330 : :
9331 : 5885128 : if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
9332 : : {
9333 : 3 : auto_diagnostic_group d;
9334 : 3 : error_at (input_location,
9335 : : "inline namespace must be specified at initial definition");
9336 : 3 : inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
9337 : 3 : }
9338 : 5661532 : resume_scope (NAMESPACE_LEVEL (ns));
9339 : 5661532 : current_namespace = ns;
9340 : 5661532 : count++;
9341 : : }
9342 : :
9343 : 11323088 : return count;
9344 : 5661544 : }
9345 : :
9346 : : /* Pop from the scope of the current namespace. */
9347 : :
9348 : : void
9349 : 5692806 : pop_namespace (void)
9350 : : {
9351 : 5692806 : auto_cond_timevar tv (TV_NAME_LOOKUP);
9352 : :
9353 : 5692806 : gcc_assert (current_namespace != global_namespace);
9354 : 5692806 : current_namespace = CP_DECL_CONTEXT (current_namespace);
9355 : : /* The binding level is not popped, as it might be re-opened later. */
9356 : 5692806 : leave_scope ();
9357 : 5692806 : }
9358 : :
9359 : : /* An IMPORT is an import that is defining namespace NAME inside CTX. Find or
9360 : : create that namespace and add it to the container's binding-vector. */
9361 : :
9362 : : tree
9363 : 2197 : add_imported_namespace (tree ctx, tree name, location_t loc, unsigned import,
9364 : : bool inline_p, bool visible_p)
9365 : : {
9366 : : // FIXME: Something is not correct about the VISIBLE_P handling. We
9367 : : // need to insert this namespace into
9368 : : // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9369 : : // (b) The importing module's slot (always)
9370 : : // (c) Do we need to put it in the CURRENT slot? This is the
9371 : : // confused piece.
9372 : :
9373 : 2197 : tree *slot = find_namespace_slot (ctx, name, true);
9374 : 2197 : tree decl = reuse_namespace (slot, ctx, name);
9375 : :
9376 : : /* Creating and binding. */
9377 : 2197 : if (!decl)
9378 : : {
9379 : 1295 : decl = make_namespace (ctx, name, loc, inline_p);
9380 : 1295 : make_namespace_finish (decl, slot, true);
9381 : : }
9382 : 902 : else if (DECL_NAMESPACE_INLINE_P (decl) != inline_p)
9383 : : {
9384 : 0 : auto_diagnostic_group d;
9385 : 0 : error_at (loc, "%s namespace %qD conflicts with reachable definition",
9386 : : inline_p ? "inline" : "non-inline", decl);
9387 : 0 : inform (DECL_SOURCE_LOCATION (decl), "reachable %s definition here",
9388 : : inline_p ? "non-inline" : "inline");
9389 : 0 : }
9390 : :
9391 : 2197 : if (TREE_PUBLIC (decl) && TREE_CODE (*slot) == BINDING_VECTOR)
9392 : : {
9393 : : /* See if we can extend the final slot. */
9394 : 1357 : binding_cluster *last = BINDING_VECTOR_CLUSTER_LAST (*slot);
9395 : 1357 : gcc_checking_assert (last->indices[0].span);
9396 : : unsigned jx = BINDING_VECTOR_SLOTS_PER_CLUSTER;
9397 : :
9398 : 2600 : while (--jx)
9399 : 1357 : if (last->indices[jx].span)
9400 : : break;
9401 : 1357 : tree final = last->slots[jx];
9402 : 1357 : if (visible_p == !STAT_HACK_P (final)
9403 : 1075 : && MAYBE_STAT_DECL (final) == decl
9404 : 102 : && last->indices[jx].base + last->indices[jx].span == import
9405 : 1456 : && (BINDING_VECTOR_NUM_CLUSTERS (*slot) > 1
9406 : : || (BINDING_VECTOR_SLOTS_PER_CLUSTER > BINDING_SLOTS_FIXED
9407 : : && jx >= BINDING_SLOTS_FIXED)))
9408 : : {
9409 : 99 : last->indices[jx].span++;
9410 : 99 : return decl;
9411 : : }
9412 : : }
9413 : :
9414 : : /* Append a new slot. */
9415 : 2098 : tree *mslot = &(tree &)*append_imported_binding_slot (slot, name, import);
9416 : :
9417 : 2098 : gcc_assert (!*mslot);
9418 : 2098 : *mslot = visible_p ? decl : stat_hack (decl, NULL_TREE);
9419 : :
9420 : 2098 : return decl;
9421 : : }
9422 : :
9423 : : /* Pop off extraneous binding levels left over due to syntax errors.
9424 : : We don't pop past namespaces, as they might be valid. */
9425 : :
9426 : : void
9427 : 93968 : pop_everything (void)
9428 : : {
9429 : 93968 : if (ENABLE_SCOPE_CHECKING)
9430 : : verbatim ("XXX entering %<pop_everything ()%>");
9431 : 93986 : while (!namespace_bindings_p ())
9432 : : {
9433 : 18 : if (current_binding_level->kind == sk_class)
9434 : 0 : pop_nested_class ();
9435 : : else
9436 : 18 : poplevel (0, 0, 0);
9437 : : }
9438 : 93968 : if (ENABLE_SCOPE_CHECKING)
9439 : : verbatim ("XXX leaving %<pop_everything ()%>");
9440 : 93968 : }
9441 : :
9442 : : /* Emit debugging information for using declarations and directives.
9443 : : If input tree is overloaded fn then emit debug info for all
9444 : : candidates. */
9445 : :
9446 : : void
9447 : 8156609 : cp_emit_debug_info_for_using (tree t, tree context)
9448 : : {
9449 : : /* Don't try to emit any debug information if we have errors. */
9450 : 8156609 : if (seen_error ())
9451 : : return;
9452 : :
9453 : : /* Do not supply context to imported_module_or_decl, if
9454 : : it is a global namespace. */
9455 : 8153872 : if (context == global_namespace)
9456 : 290591 : context = NULL_TREE;
9457 : :
9458 : 8153872 : t = MAYBE_BASELINK_FUNCTIONS (t);
9459 : :
9460 : 17518744 : for (lkp_iterator iter (t); iter; ++iter)
9461 : : {
9462 : 9364872 : tree fn = *iter;
9463 : :
9464 : 9364872 : if (TREE_CODE (fn) == TEMPLATE_DECL)
9465 : : /* FIXME: Handle TEMPLATE_DECLs. */
9466 : 668525 : continue;
9467 : :
9468 : : /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9469 : : of a builtin function. */
9470 : 11609349 : if (TREE_CODE (fn) == FUNCTION_DECL
9471 : 7286912 : && DECL_EXTERNAL (fn)
9472 : 15980590 : && fndecl_built_in_p (fn))
9473 : 2913002 : continue;
9474 : :
9475 : 5783345 : if (building_stmt_list_p ())
9476 : 1297 : add_stmt (build_stmt (input_location, USING_STMT, fn));
9477 : : else
9478 : 5782048 : debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
9479 : : false, false);
9480 : : }
9481 : : }
9482 : :
9483 : : /* True if D is a local declaration in dependent scope. Assumes that it is
9484 : : (part of) the current lookup result for its name. */
9485 : :
9486 : : bool
9487 : 53089364 : dependent_local_decl_p (tree d)
9488 : : {
9489 : 53089364 : if (!DECL_LOCAL_DECL_P (d))
9490 : : return false;
9491 : :
9492 : 16332 : cxx_binding *b = IDENTIFIER_BINDING (DECL_NAME (d));
9493 : 16332 : cp_binding_level *l = b->scope;
9494 : 44429 : while (!l->this_entity)
9495 : 28097 : l = l->level_chain;
9496 : 16332 : return uses_template_parms (l->this_entity);
9497 : : }
9498 : :
9499 : :
9500 : :
9501 : : #include "gt-cp-name-lookup.h"
|