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