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