c++: Handle std::construct_at on automatic vars during constant evaluation [PR97195]
[gcc.git] / gcc / cp / name-lookup.c
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2020 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
35 #include "c-family/name-hint.h"
36 #include "c-family/known-headers.h"
37 #include "c-family/c-spellcheck.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 /* Create an overload suitable for recording an artificial TYPE_DECL
49 and another decl. We use this machanism to implement the struct
50 stat hack within a namespace. It'd be nice to use it everywhere. */
51
52 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
53 #define STAT_TYPE(N) TREE_TYPE (N)
54 #define STAT_DECL(N) OVL_FUNCTION (N)
55 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
56 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
57
58 /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
59 But we also need to indicate hiddenness on implicit type decls
60 (injected friend classes), and (coming soon) decls injected from
61 block-scope externs. It is too awkward to press the existing
62 overload marking for that. If we have a hidden non-function, we
63 always create a STAT_HACK, and use these two markers as needed. */
64 #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
65 #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
66
67 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
68 the type binding. */
69
70 static tree
71 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
72 {
73 tree result = make_node (OVERLOAD);
74
75 /* Mark this as a lookup, so we can tell this is a stat hack. */
76 OVL_LOOKUP_P (result) = true;
77 STAT_DECL (result) = decl;
78 STAT_TYPE (result) = type;
79 return result;
80 }
81
82 /* Create a local binding level for NAME. */
83
84 static cxx_binding *
85 create_local_binding (cp_binding_level *level, tree name)
86 {
87 cxx_binding *binding = cxx_binding_make (NULL, NULL);
88
89 LOCAL_BINDING_P (binding) = true;
90 binding->scope = level;
91 binding->previous = IDENTIFIER_BINDING (name);
92
93 IDENTIFIER_BINDING (name) = binding;
94
95 return binding;
96 }
97
98 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
99 make an empty binding if there wasn't one. */
100
101 static tree *
102 find_namespace_slot (tree ns, tree name, bool create_p = false)
103 {
104 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
105 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
106 create_p ? INSERT : NO_INSERT);
107 return slot;
108 }
109
110 static tree
111 find_namespace_value (tree ns, tree name)
112 {
113 tree *b = find_namespace_slot (ns, name);
114
115 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
116 }
117
118 /* Add DECL to the list of things declared in B. */
119
120 static void
121 add_decl_to_level (cp_binding_level *b, tree decl)
122 {
123 gcc_assert (b->kind != sk_class);
124
125 /* Make sure we don't create a circular list. xref_tag can end
126 up pushing the same artificial decl more than once. We
127 should have already detected that in update_binding. */
128 gcc_assert (b->names != decl);
129
130 /* We build up the list in reverse order, and reverse it later if
131 necessary. */
132 TREE_CHAIN (decl) = b->names;
133 b->names = decl;
134
135 /* If appropriate, add decl to separate list of statics. We include
136 extern variables because they might turn out to be static later.
137 It's OK for this list to contain a few false positives. */
138 if (b->kind == sk_namespace
139 && ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
140 || (TREE_CODE (decl) == FUNCTION_DECL
141 && (!TREE_PUBLIC (decl)
142 || decl_anon_ns_mem_p (decl)
143 || DECL_DECLARED_INLINE_P (decl)))))
144 vec_safe_push (static_decls, decl);
145 }
146
147 /* Find the binding for NAME in the local binding level B. */
148
149 static cxx_binding *
150 find_local_binding (cp_binding_level *b, tree name)
151 {
152 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
153 for (;; b = b->level_chain)
154 {
155 if (binding->scope == b)
156 return binding;
157
158 /* Cleanup contours are transparent to the language. */
159 if (b->kind != sk_cleanup)
160 break;
161 }
162 return NULL;
163 }
164
165 class name_lookup
166 {
167 public:
168 typedef std::pair<tree, tree> using_pair;
169 typedef vec<using_pair, va_heap, vl_embed> using_queue;
170
171 public:
172 tree name; /* The identifier being looked for. */
173 tree value; /* A (possibly ambiguous) set of things found. */
174 tree type; /* A type that has been found. */
175 LOOK_want want; /* What kind of entity we want. */
176
177 bool deduping; /* Full deduping is needed because using declarations
178 are in play. */
179 vec<tree, va_heap, vl_embed> *scopes;
180 name_lookup *previous; /* Previously active lookup. */
181
182 protected:
183 /* Marked scope stack for outermost name lookup. */
184 static vec<tree, va_heap, vl_embed> *shared_scopes;
185 /* Currently active lookup. */
186 static name_lookup *active;
187
188 public:
189 name_lookup (tree n, LOOK_want w = LOOK_want::NORMAL)
190 : name (n), value (NULL_TREE), type (NULL_TREE),
191 want (w),
192 deduping (false), scopes (NULL), previous (NULL)
193 {
194 preserve_state ();
195 }
196 ~name_lookup ()
197 {
198 restore_state ();
199 }
200
201 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
202 name_lookup (const name_lookup &);
203 name_lookup &operator= (const name_lookup &);
204
205 protected:
206 static bool seen_p (tree scope)
207 {
208 return LOOKUP_SEEN_P (scope);
209 }
210 static bool found_p (tree scope)
211 {
212 return LOOKUP_FOUND_P (scope);
213 }
214
215 void mark_seen (tree scope); /* Mark and add to scope vector. */
216 static void mark_found (tree scope)
217 {
218 gcc_checking_assert (seen_p (scope));
219 LOOKUP_FOUND_P (scope) = true;
220 }
221 bool see_and_mark (tree scope)
222 {
223 bool ret = seen_p (scope);
224 if (!ret)
225 mark_seen (scope);
226 return ret;
227 }
228 bool find_and_mark (tree scope);
229
230 private:
231 void preserve_state ();
232 void restore_state ();
233
234 private:
235 static tree ambiguous (tree thing, tree current);
236 void add_overload (tree fns);
237 void add_value (tree new_val);
238 void add_type (tree new_type);
239 bool process_binding (tree val_bind, tree type_bind);
240
241 /* Look in only namespace. */
242 bool search_namespace_only (tree scope);
243 /* Look in namespace and its (recursive) inlines. Ignore using
244 directives. Return true if something found (inc dups). */
245 bool search_namespace (tree scope);
246 /* Look in the using directives of namespace + inlines using
247 qualified lookup rules. */
248 bool search_usings (tree scope);
249
250 private:
251 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
252 using_queue *do_queue_usings (using_queue *queue, int depth,
253 vec<tree, va_gc> *usings);
254 using_queue *queue_usings (using_queue *queue, int depth,
255 vec<tree, va_gc> *usings)
256 {
257 if (usings)
258 queue = do_queue_usings (queue, depth, usings);
259 return queue;
260 }
261
262 private:
263 void add_fns (tree);
264
265 void adl_expr (tree);
266 void adl_type (tree);
267 void adl_template_arg (tree);
268 void adl_class (tree);
269 void adl_bases (tree);
270 void adl_class_only (tree);
271 void adl_namespace (tree);
272 void adl_namespace_only (tree);
273
274 public:
275 /* Search namespace + inlines + maybe usings as qualified lookup. */
276 bool search_qualified (tree scope, bool usings = true);
277
278 /* Search namespace + inlines + usings as unqualified lookup. */
279 bool search_unqualified (tree scope, cp_binding_level *);
280
281 /* ADL lookup of ARGS. */
282 tree search_adl (tree fns, vec<tree, va_gc> *args);
283 };
284
285 /* Scope stack shared by all outermost lookups. This avoids us
286 allocating and freeing on every single lookup. */
287 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
288
289 /* Currently active lookup. */
290 name_lookup *name_lookup::active;
291
292 /* Name lookup is recursive, becase ADL can cause template
293 instatiation. This is of course a rare event, so we optimize for
294 it not happening. When we discover an active name-lookup, which
295 must be an ADL lookup, we need to unmark the marked scopes and also
296 unmark the lookup we might have been accumulating. */
297
298 void
299 name_lookup::preserve_state ()
300 {
301 previous = active;
302 if (previous)
303 {
304 unsigned length = vec_safe_length (previous->scopes);
305 vec_safe_reserve (previous->scopes, length * 2);
306 for (unsigned ix = length; ix--;)
307 {
308 tree decl = (*previous->scopes)[ix];
309
310 gcc_checking_assert (LOOKUP_SEEN_P (decl));
311 LOOKUP_SEEN_P (decl) = false;
312
313 /* Preserve the FOUND_P state on the interrupted lookup's
314 stack. */
315 if (LOOKUP_FOUND_P (decl))
316 {
317 LOOKUP_FOUND_P (decl) = false;
318 previous->scopes->quick_push (decl);
319 }
320 }
321
322 /* Unmark the outer partial lookup. */
323 if (previous->deduping)
324 lookup_mark (previous->value, false);
325 }
326 else
327 scopes = shared_scopes;
328 active = this;
329 }
330
331 /* Restore the marking state of a lookup we interrupted. */
332
333 void
334 name_lookup::restore_state ()
335 {
336 if (deduping)
337 lookup_mark (value, false);
338
339 /* Unmark and empty this lookup's scope stack. */
340 for (unsigned ix = vec_safe_length (scopes); ix--;)
341 {
342 tree decl = scopes->pop ();
343 gcc_checking_assert (LOOKUP_SEEN_P (decl));
344 LOOKUP_SEEN_P (decl) = false;
345 LOOKUP_FOUND_P (decl) = false;
346 }
347
348 active = previous;
349 if (previous)
350 {
351 free (scopes);
352
353 unsigned length = vec_safe_length (previous->scopes);
354 for (unsigned ix = 0; ix != length; ix++)
355 {
356 tree decl = (*previous->scopes)[ix];
357 if (LOOKUP_SEEN_P (decl))
358 {
359 /* The remainder of the scope stack must be recording
360 FOUND_P decls, which we want to pop off. */
361 do
362 {
363 tree decl = previous->scopes->pop ();
364 gcc_checking_assert (LOOKUP_SEEN_P (decl)
365 && !LOOKUP_FOUND_P (decl));
366 LOOKUP_FOUND_P (decl) = true;
367 }
368 while (++ix != length);
369 break;
370 }
371
372 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
373 LOOKUP_SEEN_P (decl) = true;
374 }
375
376 /* Remark the outer partial lookup. */
377 if (previous->deduping)
378 lookup_mark (previous->value, true);
379 }
380 else
381 shared_scopes = scopes;
382 }
383
384 void
385 name_lookup::mark_seen (tree scope)
386 {
387 gcc_checking_assert (!seen_p (scope));
388 LOOKUP_SEEN_P (scope) = true;
389 vec_safe_push (scopes, scope);
390 }
391
392 bool
393 name_lookup::find_and_mark (tree scope)
394 {
395 bool result = LOOKUP_FOUND_P (scope);
396 if (!result)
397 {
398 LOOKUP_FOUND_P (scope) = true;
399 if (!LOOKUP_SEEN_P (scope))
400 vec_safe_push (scopes, scope);
401 }
402
403 return result;
404 }
405
406 /* THING and CURRENT are ambiguous, concatenate them. */
407
408 tree
409 name_lookup::ambiguous (tree thing, tree current)
410 {
411 if (TREE_CODE (current) != TREE_LIST)
412 {
413 current = build_tree_list (NULL_TREE, current);
414 TREE_TYPE (current) = error_mark_node;
415 }
416 current = tree_cons (NULL_TREE, thing, current);
417 TREE_TYPE (current) = error_mark_node;
418
419 return current;
420 }
421
422 /* FNS is a new overload set to add to the exising set. */
423
424 void
425 name_lookup::add_overload (tree fns)
426 {
427 if (!deduping && TREE_CODE (fns) == OVERLOAD)
428 {
429 tree probe = fns;
430 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
431 probe = ovl_skip_hidden (probe);
432 if (probe && TREE_CODE (probe) == OVERLOAD
433 && OVL_DEDUP_P (probe))
434 {
435 /* We're about to add something found by a using
436 declaration, so need to engage deduping mode. */
437 lookup_mark (value, true);
438 deduping = true;
439 }
440 }
441
442 value = lookup_maybe_add (fns, value, deduping);
443 }
444
445 /* Add a NEW_VAL, a found value binding into the current value binding. */
446
447 void
448 name_lookup::add_value (tree new_val)
449 {
450 if (OVL_P (new_val) && (!value || OVL_P (value)))
451 add_overload (new_val);
452 else if (!value)
453 value = new_val;
454 else if (value == new_val)
455 ;
456 else if ((TREE_CODE (value) == TYPE_DECL
457 && TREE_CODE (new_val) == TYPE_DECL
458 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
459 /* Typedefs to the same type. */;
460 else if (TREE_CODE (value) == NAMESPACE_DECL
461 && TREE_CODE (new_val) == NAMESPACE_DECL
462 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
463 /* Namespace (possibly aliased) to the same namespace. Locate
464 the namespace*/
465 value = ORIGINAL_NAMESPACE (value);
466 else
467 {
468 if (deduping)
469 {
470 /* Disengage deduping mode. */
471 lookup_mark (value, false);
472 deduping = false;
473 }
474 value = ambiguous (new_val, value);
475 }
476 }
477
478 /* Add a NEW_TYPE, a found type binding into the current type binding. */
479
480 void
481 name_lookup::add_type (tree new_type)
482 {
483 if (!type)
484 type = new_type;
485 else if (TREE_CODE (type) == TREE_LIST
486 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
487 type = ambiguous (new_type, type);
488 }
489
490 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
491 true if we actually found something noteworthy. Hiddenness has
492 already been handled in the caller. */
493
494 bool
495 name_lookup::process_binding (tree new_val, tree new_type)
496 {
497 /* Did we really see a type? */
498 if (new_type
499 && (want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE)
500 new_type = NULL_TREE;
501
502 /* Do we really see a value? */
503 if (new_val)
504 switch (TREE_CODE (new_val))
505 {
506 case TEMPLATE_DECL:
507 /* If we expect types or namespaces, and not templates,
508 or this is not a template class. */
509 if (bool (want & LOOK_want::TYPE_NAMESPACE)
510 && !DECL_TYPE_TEMPLATE_P (new_val))
511 new_val = NULL_TREE;
512 break;
513 case TYPE_DECL:
514 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::NAMESPACE
515 || (new_type && bool (want & LOOK_want::TYPE)))
516 new_val = NULL_TREE;
517 break;
518 case NAMESPACE_DECL:
519 if ((want & LOOK_want::TYPE_NAMESPACE) == LOOK_want::TYPE)
520 new_val = NULL_TREE;
521 break;
522 default:
523 if (bool (want & LOOK_want::TYPE_NAMESPACE))
524 new_val = NULL_TREE;
525 }
526
527 if (!new_val)
528 {
529 new_val = new_type;
530 new_type = NULL_TREE;
531 }
532
533 /* Merge into the lookup */
534 if (new_val)
535 add_value (new_val);
536 if (new_type)
537 add_type (new_type);
538
539 return new_val != NULL_TREE;
540 }
541
542 /* Look in exactly namespace SCOPE. */
543
544 bool
545 name_lookup::search_namespace_only (tree scope)
546 {
547 bool found = false;
548
549 if (tree *binding = find_namespace_slot (scope, name))
550 {
551 tree value = *binding, type = NULL_TREE;
552
553 if (STAT_HACK_P (value))
554 {
555 type = STAT_TYPE (value);
556 value = STAT_DECL (value);
557
558 if (!bool (want & LOOK_want::HIDDEN_FRIEND))
559 {
560 if (STAT_TYPE_HIDDEN_P (*binding))
561 type = NULL_TREE;
562 if (STAT_DECL_HIDDEN_P (*binding))
563 value = NULL_TREE;
564 else
565 value = ovl_skip_hidden (value);
566 }
567 }
568 else if (!bool (want & LOOK_want::HIDDEN_FRIEND))
569 value = ovl_skip_hidden (value);
570
571 found |= process_binding (value, type);
572 }
573
574 return found;
575 }
576
577 /* Conditionally look in namespace SCOPE and inline children. */
578
579 bool
580 name_lookup::search_namespace (tree scope)
581 {
582 if (see_and_mark (scope))
583 /* We've visited this scope before. Return what we found then. */
584 return found_p (scope);
585
586 /* Look in exactly namespace. */
587 bool found = search_namespace_only (scope);
588
589 /* Don't look into inline children, if we're looking for an
590 anonymous name -- it must be in the current scope, if anywhere. */
591 if (name)
592 /* Recursively look in its inline children. */
593 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
594 for (unsigned ix = inlinees->length (); ix--;)
595 found |= search_namespace ((*inlinees)[ix]);
596
597 if (found)
598 mark_found (scope);
599
600 return found;
601 }
602
603 /* Recursively follow using directives of SCOPE & its inline children.
604 Such following is essentially a flood-fill algorithm. */
605
606 bool
607 name_lookup::search_usings (tree scope)
608 {
609 /* We do not check seen_p here, as that was already set during the
610 namespace_only walk. */
611 if (found_p (scope))
612 return true;
613
614 bool found = false;
615 if (vec<tree, va_gc> *usings = NAMESPACE_LEVEL (scope)->using_directives)
616 for (unsigned ix = usings->length (); ix--;)
617 found |= search_qualified ((*usings)[ix], true);
618
619 /* Look in its inline children. */
620 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
621 for (unsigned ix = inlinees->length (); ix--;)
622 found |= search_usings ((*inlinees)[ix]);
623
624 if (found)
625 mark_found (scope);
626
627 return found;
628 }
629
630 /* Qualified namespace lookup in SCOPE.
631 1) Look in SCOPE (+inlines). If found, we're done.
632 2) Otherwise, if USINGS is true,
633 recurse for every using directive of SCOPE (+inlines).
634
635 Trickiness is (a) loops and (b) multiple paths to same namespace.
636 In both cases we want to not repeat any lookups, and know whether
637 to stop the caller's step #2. Do this via the FOUND_P marker. */
638
639 bool
640 name_lookup::search_qualified (tree scope, bool usings)
641 {
642 bool found = false;
643
644 if (seen_p (scope))
645 found = found_p (scope);
646 else
647 {
648 found = search_namespace (scope);
649 if (!found && usings)
650 found = search_usings (scope);
651 }
652
653 return found;
654 }
655
656 /* Add SCOPE to the unqualified search queue, recursively add its
657 inlines and those via using directives. */
658
659 name_lookup::using_queue *
660 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
661 {
662 if (see_and_mark (scope))
663 return queue;
664
665 /* Record it. */
666 tree common = scope;
667 while (SCOPE_DEPTH (common) > depth)
668 common = CP_DECL_CONTEXT (common);
669 vec_safe_push (queue, using_pair (common, scope));
670
671 /* Queue its inline children. */
672 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
673 for (unsigned ix = inlinees->length (); ix--;)
674 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
675
676 /* Queue its using targets. */
677 queue = queue_usings (queue, depth, NAMESPACE_LEVEL (scope)->using_directives);
678
679 return queue;
680 }
681
682 /* Add the namespaces in USINGS to the unqualified search queue. */
683
684 name_lookup::using_queue *
685 name_lookup::do_queue_usings (using_queue *queue, int depth,
686 vec<tree, va_gc> *usings)
687 {
688 for (unsigned ix = usings->length (); ix--;)
689 queue = queue_namespace (queue, depth, (*usings)[ix]);
690
691 return queue;
692 }
693
694 /* Unqualified namespace lookup in SCOPE.
695 1) add scope+inlins to worklist.
696 2) recursively add target of every using directive
697 3) for each worklist item where SCOPE is common ancestor, search it
698 4) if nothing find, scope=parent, goto 1. */
699
700 bool
701 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
702 {
703 /* Make static to avoid continual reallocation. We're not
704 recursive. */
705 static using_queue *queue = NULL;
706 bool found = false;
707 int length = vec_safe_length (queue);
708
709 /* Queue local using-directives. */
710 for (; level->kind != sk_namespace; level = level->level_chain)
711 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
712
713 for (; !found; scope = CP_DECL_CONTEXT (scope))
714 {
715 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
716 int depth = SCOPE_DEPTH (scope);
717
718 /* Queue namespaces reachable from SCOPE. */
719 queue = queue_namespace (queue, depth, scope);
720
721 /* Search every queued namespace where SCOPE is the common
722 ancestor. Adjust the others. */
723 unsigned ix = length;
724 do
725 {
726 using_pair &pair = (*queue)[ix];
727 while (pair.first == scope)
728 {
729 found |= search_namespace_only (pair.second);
730 pair = queue->pop ();
731 if (ix == queue->length ())
732 goto done;
733 }
734 /* The depth is the same as SCOPE, find the parent scope. */
735 if (SCOPE_DEPTH (pair.first) == depth)
736 pair.first = CP_DECL_CONTEXT (pair.first);
737 ix++;
738 }
739 while (ix < queue->length ());
740 done:;
741 if (scope == global_namespace)
742 break;
743
744 /* If looking for hidden friends, we only look in the innermost
745 namespace scope. [namespace.memdef]/3 If a friend
746 declaration in a non-local class first declares a class,
747 function, class template or function template the friend is a
748 member of the innermost enclosing namespace. See also
749 [basic.lookup.unqual]/7 */
750 if (bool (want & LOOK_want::HIDDEN_FRIEND))
751 break;
752 }
753
754 /* Restore to incoming length. */
755 vec_safe_truncate (queue, length);
756
757 return found;
758 }
759
760 /* FNS is a value binding. If it is a (set of overloaded) functions,
761 add them into the current value. */
762
763 void
764 name_lookup::add_fns (tree fns)
765 {
766 if (!fns)
767 return;
768 else if (TREE_CODE (fns) == OVERLOAD)
769 {
770 if (TREE_TYPE (fns) != unknown_type_node)
771 fns = OVL_FUNCTION (fns);
772 }
773 else if (!DECL_DECLARES_FUNCTION_P (fns))
774 return;
775
776 add_overload (fns);
777 }
778
779 /* Add functions of a namespace to the lookup structure. */
780
781 void
782 name_lookup::adl_namespace_only (tree scope)
783 {
784 mark_seen (scope);
785
786 /* Look down into inline namespaces. */
787 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
788 for (unsigned ix = inlinees->length (); ix--;)
789 adl_namespace_only ((*inlinees)[ix]);
790
791 if (tree fns = find_namespace_value (scope, name))
792 add_fns (ovl_skip_hidden (fns));
793 }
794
795 /* Find the containing non-inlined namespace, add it and all its
796 inlinees. */
797
798 void
799 name_lookup::adl_namespace (tree scope)
800 {
801 if (seen_p (scope))
802 return;
803
804 /* Find the containing non-inline namespace. */
805 while (DECL_NAMESPACE_INLINE_P (scope))
806 scope = CP_DECL_CONTEXT (scope);
807
808 adl_namespace_only (scope);
809 }
810
811 /* Adds the class and its friends to the lookup structure. */
812
813 void
814 name_lookup::adl_class_only (tree type)
815 {
816 /* Backend-built structures, such as __builtin_va_list, aren't
817 affected by all this. */
818 if (!CLASS_TYPE_P (type))
819 return;
820
821 type = TYPE_MAIN_VARIANT (type);
822
823 if (see_and_mark (type))
824 return;
825
826 tree context = decl_namespace_context (type);
827 adl_namespace (context);
828
829 complete_type (type);
830
831 /* Add friends. */
832 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
833 list = TREE_CHAIN (list))
834 if (name == FRIEND_NAME (list))
835 for (tree friends = FRIEND_DECLS (list); friends;
836 friends = TREE_CHAIN (friends))
837 {
838 tree fn = TREE_VALUE (friends);
839
840 /* Only interested in global functions with potentially hidden
841 (i.e. unqualified) declarations. */
842 if (CP_DECL_CONTEXT (fn) != context)
843 continue;
844
845 /* Only interested in anticipated friends. (Non-anticipated
846 ones will have been inserted during the namespace
847 adl.) */
848 if (!DECL_ANTICIPATED (fn))
849 continue;
850
851 /* Template specializations are never found by name lookup.
852 (Templates themselves can be found, but not template
853 specializations.) */
854 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
855 continue;
856
857 add_fns (fn);
858 }
859 }
860
861 /* Adds the class and its bases to the lookup structure.
862 Returns true on error. */
863
864 void
865 name_lookup::adl_bases (tree type)
866 {
867 adl_class_only (type);
868
869 /* Process baseclasses. */
870 if (tree binfo = TYPE_BINFO (type))
871 {
872 tree base_binfo;
873 int i;
874
875 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
876 adl_bases (BINFO_TYPE (base_binfo));
877 }
878 }
879
880 /* Adds everything associated with a class argument type to the lookup
881 structure. Returns true on error.
882
883 If T is a class type (including unions), its associated classes are: the
884 class itself; the class of which it is a member, if any; and its direct
885 and indirect base classes. Its associated namespaces are the namespaces
886 of which its associated classes are members. Furthermore, if T is a
887 class template specialization, its associated namespaces and classes
888 also include: the namespaces and classes associated with the types of
889 the template arguments provided for template type parameters (excluding
890 template template parameters); the namespaces of which any template
891 template arguments are members; and the classes of which any member
892 templates used as template template arguments are members. [ Note:
893 non-type template arguments do not contribute to the set of associated
894 namespaces. --end note] */
895
896 void
897 name_lookup::adl_class (tree type)
898 {
899 /* Backend build structures, such as __builtin_va_list, aren't
900 affected by all this. */
901 if (!CLASS_TYPE_P (type))
902 return;
903
904 type = TYPE_MAIN_VARIANT (type);
905 /* We don't set found here because we have to have set seen first,
906 which is done in the adl_bases walk. */
907 if (found_p (type))
908 return;
909
910 adl_bases (type);
911 mark_found (type);
912
913 if (TYPE_CLASS_SCOPE_P (type))
914 adl_class_only (TYPE_CONTEXT (type));
915
916 /* Process template arguments. */
917 if (CLASSTYPE_TEMPLATE_INFO (type)
918 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
919 {
920 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
921 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
922 adl_template_arg (TREE_VEC_ELT (list, i));
923 }
924 }
925
926 void
927 name_lookup::adl_expr (tree expr)
928 {
929 if (!expr)
930 return;
931
932 gcc_assert (!TYPE_P (expr));
933
934 if (TREE_TYPE (expr) != unknown_type_node)
935 {
936 adl_type (unlowered_expr_type (expr));
937 return;
938 }
939
940 if (TREE_CODE (expr) == ADDR_EXPR)
941 expr = TREE_OPERAND (expr, 0);
942 if (TREE_CODE (expr) == COMPONENT_REF
943 || TREE_CODE (expr) == OFFSET_REF)
944 expr = TREE_OPERAND (expr, 1);
945 expr = MAYBE_BASELINK_FUNCTIONS (expr);
946
947 if (OVL_P (expr))
948 for (lkp_iterator iter (expr); iter; ++iter)
949 adl_type (TREE_TYPE (*iter));
950 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
951 {
952 /* The working paper doesn't currently say how to handle
953 template-id arguments. The sensible thing would seem to be
954 to handle the list of template candidates like a normal
955 overload set, and handle the template arguments like we do
956 for class template specializations. */
957
958 /* First the templates. */
959 adl_expr (TREE_OPERAND (expr, 0));
960
961 /* Now the arguments. */
962 if (tree args = TREE_OPERAND (expr, 1))
963 for (int ix = TREE_VEC_LENGTH (args); ix--;)
964 adl_template_arg (TREE_VEC_ELT (args, ix));
965 }
966 }
967
968 void
969 name_lookup::adl_type (tree type)
970 {
971 if (!type)
972 return;
973
974 if (TYPE_PTRDATAMEM_P (type))
975 {
976 /* Pointer to member: associate class type and value type. */
977 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
978 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
979 return;
980 }
981
982 switch (TREE_CODE (type))
983 {
984 case RECORD_TYPE:
985 if (TYPE_PTRMEMFUNC_P (type))
986 {
987 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
988 return;
989 }
990 /* FALLTHRU */
991 case UNION_TYPE:
992 adl_class (type);
993 return;
994
995 case METHOD_TYPE:
996 /* The basetype is referenced in the first arg type, so just
997 fall through. */
998 case FUNCTION_TYPE:
999 /* Associate the parameter types. */
1000 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
1001 adl_type (TREE_VALUE (args));
1002 /* FALLTHROUGH */
1003
1004 case POINTER_TYPE:
1005 case REFERENCE_TYPE:
1006 case ARRAY_TYPE:
1007 adl_type (TREE_TYPE (type));
1008 return;
1009
1010 case ENUMERAL_TYPE:
1011 if (TYPE_CLASS_SCOPE_P (type))
1012 adl_class_only (TYPE_CONTEXT (type));
1013 adl_namespace (decl_namespace_context (type));
1014 return;
1015
1016 case LANG_TYPE:
1017 gcc_assert (type == unknown_type_node
1018 || type == init_list_type_node);
1019 return;
1020
1021 case TYPE_PACK_EXPANSION:
1022 adl_type (PACK_EXPANSION_PATTERN (type));
1023 return;
1024
1025 default:
1026 break;
1027 }
1028 }
1029
1030 /* Adds everything associated with a template argument to the lookup
1031 structure. */
1032
1033 void
1034 name_lookup::adl_template_arg (tree arg)
1035 {
1036 /* [basic.lookup.koenig]
1037
1038 If T is a template-id, its associated namespaces and classes are
1039 ... the namespaces and classes associated with the types of the
1040 template arguments provided for template type parameters
1041 (excluding template template parameters); the namespaces in which
1042 any template template arguments are defined; and the classes in
1043 which any member templates used as template template arguments
1044 are defined. [Note: non-type template arguments do not
1045 contribute to the set of associated namespaces. ] */
1046
1047 /* Consider first template template arguments. */
1048 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1049 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1050 ;
1051 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1052 {
1053 tree ctx = CP_DECL_CONTEXT (arg);
1054
1055 /* It's not a member template. */
1056 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1057 adl_namespace (ctx);
1058 /* Otherwise, it must be member template. */
1059 else
1060 adl_class_only (ctx);
1061 }
1062 /* It's an argument pack; handle it recursively. */
1063 else if (ARGUMENT_PACK_P (arg))
1064 {
1065 tree args = ARGUMENT_PACK_ARGS (arg);
1066 int i, len = TREE_VEC_LENGTH (args);
1067 for (i = 0; i < len; ++i)
1068 adl_template_arg (TREE_VEC_ELT (args, i));
1069 }
1070 /* It's not a template template argument, but it is a type template
1071 argument. */
1072 else if (TYPE_P (arg))
1073 adl_type (arg);
1074 }
1075
1076 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1077 the call arguments. */
1078
1079 tree
1080 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1081 {
1082 if (fns)
1083 {
1084 deduping = true;
1085 lookup_mark (fns, true);
1086 }
1087 value = fns;
1088
1089 unsigned ix;
1090 tree arg;
1091
1092 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1093 /* OMP reduction operators put an ADL-significant type as the
1094 first arg. */
1095 if (TYPE_P (arg))
1096 adl_type (arg);
1097 else
1098 adl_expr (arg);
1099
1100 fns = value;
1101
1102 return fns;
1103 }
1104
1105 static bool qualified_namespace_lookup (tree, name_lookup *);
1106 static void consider_binding_level (tree name,
1107 best_match <tree, const char *> &bm,
1108 cp_binding_level *lvl,
1109 bool look_within_fields,
1110 enum lookup_name_fuzzy_kind kind);
1111 static void diagnose_name_conflict (tree, tree);
1112
1113 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1114 don't add duplicates to it. ARGS is the vector of call
1115 arguments (which will not be empty). */
1116
1117 tree
1118 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1119 {
1120 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1121 name_lookup lookup (name);
1122 fns = lookup.search_adl (fns, args);
1123 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1124 return fns;
1125 }
1126
1127 /* FNS is an overload set of conversion functions. Return the
1128 overloads converting to TYPE. */
1129
1130 static tree
1131 extract_conversion_operator (tree fns, tree type)
1132 {
1133 tree convs = NULL_TREE;
1134 tree tpls = NULL_TREE;
1135
1136 for (ovl_iterator iter (fns); iter; ++iter)
1137 {
1138 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1139 convs = lookup_add (*iter, convs);
1140
1141 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1142 tpls = lookup_add (*iter, tpls);
1143 }
1144
1145 if (!convs)
1146 convs = tpls;
1147
1148 return convs;
1149 }
1150
1151 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1152
1153 static tree
1154 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1155 {
1156 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1157 {
1158 unsigned mid = (lo + hi) / 2;
1159 tree binding = (*member_vec)[mid];
1160 tree binding_name = OVL_NAME (binding);
1161
1162 if (binding_name > name)
1163 hi = mid;
1164 else if (binding_name < name)
1165 lo = mid + 1;
1166 else
1167 return binding;
1168 }
1169
1170 return NULL_TREE;
1171 }
1172
1173 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1174
1175 static tree
1176 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1177 {
1178 for (int ix = member_vec->length (); ix--;)
1179 if (tree binding = (*member_vec)[ix])
1180 if (OVL_NAME (binding) == name)
1181 return binding;
1182
1183 return NULL_TREE;
1184 }
1185
1186 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1187
1188 static tree
1189 fields_linear_search (tree klass, tree name, bool want_type)
1190 {
1191 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1192 {
1193 tree decl = fields;
1194
1195 if (TREE_CODE (decl) == FIELD_DECL
1196 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1197 {
1198 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1199 return temp;
1200 }
1201
1202 if (DECL_NAME (decl) != name)
1203 continue;
1204
1205 if (TREE_CODE (decl) == USING_DECL)
1206 {
1207 decl = strip_using_decl (decl);
1208 if (is_overloaded_fn (decl))
1209 continue;
1210 }
1211
1212 if (DECL_DECLARES_FUNCTION_P (decl))
1213 /* Functions are found separately. */
1214 continue;
1215
1216 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1217 return decl;
1218 }
1219
1220 return NULL_TREE;
1221 }
1222
1223 /* Look for NAME member inside of anonymous aggregate ANON. Although
1224 such things should only contain FIELD_DECLs, we check that too
1225 late, and would give very confusing errors if we weren't
1226 permissive here. */
1227
1228 tree
1229 search_anon_aggr (tree anon, tree name, bool want_type)
1230 {
1231 gcc_assert (COMPLETE_TYPE_P (anon));
1232 tree ret = get_class_binding_direct (anon, name, want_type);
1233 return ret;
1234 }
1235
1236 /* Look for NAME as an immediate member of KLASS (including
1237 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1238 regular search. >0 to get a type binding (if there is one) and <0
1239 if you want (just) the member function binding.
1240
1241 Use this if you do not want lazy member creation. */
1242
1243 tree
1244 get_class_binding_direct (tree klass, tree name, bool want_type)
1245 {
1246 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1247
1248 /* Conversion operators can only be found by the marker conversion
1249 operator name. */
1250 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1251 tree lookup = conv_op ? conv_op_identifier : name;
1252 tree val = NULL_TREE;
1253 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1254
1255 if (COMPLETE_TYPE_P (klass) && member_vec)
1256 {
1257 val = member_vec_binary_search (member_vec, lookup);
1258 if (!val)
1259 ;
1260 else if (STAT_HACK_P (val))
1261 val = want_type ? STAT_TYPE (val) : STAT_DECL (val);
1262 else if (want_type && !DECL_DECLARES_TYPE_P (val))
1263 val = NULL_TREE;
1264 }
1265 else
1266 {
1267 if (member_vec && !want_type)
1268 val = member_vec_linear_search (member_vec, lookup);
1269
1270 if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val)))
1271 /* Dependent using declarations are a 'field', make sure we
1272 return that even if we saw an overload already. */
1273 if (tree field_val = fields_linear_search (klass, lookup, want_type))
1274 {
1275 if (!val)
1276 val = field_val;
1277 else if (TREE_CODE (field_val) == USING_DECL)
1278 val = ovl_make (field_val, val);
1279 }
1280 }
1281
1282 /* Extract the conversion operators asked for, unless the general
1283 conversion operator was requested. */
1284 if (val && conv_op)
1285 {
1286 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1287 val = OVL_CHAIN (val);
1288 if (tree type = TREE_TYPE (name))
1289 val = extract_conversion_operator (val, type);
1290 }
1291
1292 return val;
1293 }
1294
1295 /* Look for NAME's binding in exactly KLASS. See
1296 get_class_binding_direct for argument description. Does lazy
1297 special function creation as necessary. */
1298
1299 tree
1300 get_class_binding (tree klass, tree name, bool want_type /*=false*/)
1301 {
1302 klass = complete_type (klass);
1303
1304 if (COMPLETE_TYPE_P (klass))
1305 {
1306 /* Lazily declare functions, if we're going to search these. */
1307 if (IDENTIFIER_CTOR_P (name))
1308 {
1309 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1310 lazily_declare_fn (sfk_constructor, klass);
1311 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1312 lazily_declare_fn (sfk_copy_constructor, klass);
1313 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1314 lazily_declare_fn (sfk_move_constructor, klass);
1315 }
1316 else if (IDENTIFIER_DTOR_P (name))
1317 {
1318 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1319 lazily_declare_fn (sfk_destructor, klass);
1320 }
1321 else if (name == assign_op_identifier)
1322 {
1323 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1324 lazily_declare_fn (sfk_copy_assignment, klass);
1325 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1326 lazily_declare_fn (sfk_move_assignment, klass);
1327 }
1328 }
1329
1330 return get_class_binding_direct (klass, name, want_type);
1331 }
1332
1333 /* Find the slot containing overloads called 'NAME'. If there is no
1334 such slot and the class is complete, create an empty one, at the
1335 correct point in the sorted member vector. Otherwise return NULL.
1336 Deals with conv_op marker handling. */
1337
1338 tree *
1339 find_member_slot (tree klass, tree name)
1340 {
1341 bool complete_p = COMPLETE_TYPE_P (klass);
1342
1343 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1344 if (!member_vec)
1345 {
1346 vec_alloc (member_vec, 8);
1347 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1348 if (complete_p)
1349 {
1350 /* If the class is complete but had no member_vec, we need
1351 to add the TYPE_FIELDS into it. We're also most likely
1352 to be adding ctors & dtors, so ask for 6 spare slots (the
1353 abstract cdtors and their clones). */
1354 set_class_bindings (klass, 6);
1355 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1356 }
1357 }
1358
1359 if (IDENTIFIER_CONV_OP_P (name))
1360 name = conv_op_identifier;
1361
1362 unsigned ix, length = member_vec->length ();
1363 for (ix = 0; ix < length; ix++)
1364 {
1365 tree *slot = &(*member_vec)[ix];
1366 tree fn_name = OVL_NAME (*slot);
1367
1368 if (fn_name == name)
1369 {
1370 /* If we found an existing slot, it must be a function set.
1371 Even with insertion after completion, because those only
1372 happen with artificial fns that have unspellable names.
1373 This means we do not have to deal with the stat hack
1374 either. */
1375 gcc_checking_assert (OVL_P (*slot));
1376 if (name == conv_op_identifier)
1377 {
1378 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1379 /* Skip the conv-op marker. */
1380 slot = &OVL_CHAIN (*slot);
1381 }
1382 return slot;
1383 }
1384
1385 if (complete_p && fn_name > name)
1386 break;
1387 }
1388
1389 /* No slot found, add one if the class is complete. */
1390 if (complete_p)
1391 {
1392 /* Do exact allocation, as we don't expect to add many. */
1393 gcc_assert (name != conv_op_identifier);
1394 vec_safe_reserve_exact (member_vec, 1);
1395 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1396 member_vec->quick_insert (ix, NULL_TREE);
1397 return &(*member_vec)[ix];
1398 }
1399
1400 return NULL;
1401 }
1402
1403 /* KLASS is an incomplete class to which we're adding a method NAME.
1404 Add a slot and deal with conv_op marker handling. */
1405
1406 tree *
1407 add_member_slot (tree klass, tree name)
1408 {
1409 gcc_assert (!COMPLETE_TYPE_P (klass));
1410
1411 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1412 vec_safe_push (member_vec, NULL_TREE);
1413 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1414
1415 tree *slot = &member_vec->last ();
1416 if (IDENTIFIER_CONV_OP_P (name))
1417 {
1418 /* Install the marker prefix. */
1419 *slot = ovl_make (conv_op_marker, NULL_TREE);
1420 slot = &OVL_CHAIN (*slot);
1421 }
1422
1423 return slot;
1424 }
1425
1426 /* Comparison function to compare two MEMBER_VEC entries by name.
1427 Because we can have duplicates during insertion of TYPE_FIELDS, we
1428 do extra checking so deduping doesn't have to deal with so many
1429 cases. */
1430
1431 static int
1432 member_name_cmp (const void *a_p, const void *b_p)
1433 {
1434 tree a = *(const tree *)a_p;
1435 tree b = *(const tree *)b_p;
1436 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1437 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1438
1439 gcc_checking_assert (name_a && name_b);
1440 if (name_a != name_b)
1441 return name_a < name_b ? -1 : +1;
1442
1443 if (name_a == conv_op_identifier)
1444 {
1445 /* Strip the conv-op markers. */
1446 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1447 && OVL_FUNCTION (b) == conv_op_marker);
1448 a = OVL_CHAIN (a);
1449 b = OVL_CHAIN (b);
1450 }
1451
1452 if (TREE_CODE (a) == OVERLOAD)
1453 a = OVL_FUNCTION (a);
1454 if (TREE_CODE (b) == OVERLOAD)
1455 b = OVL_FUNCTION (b);
1456
1457 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1458 if (TREE_CODE (a) != TREE_CODE (b))
1459 {
1460 /* If one of them is a TYPE_DECL, it loses. */
1461 if (TREE_CODE (a) == TYPE_DECL)
1462 return +1;
1463 else if (TREE_CODE (b) == TYPE_DECL)
1464 return -1;
1465
1466 /* If one of them is a USING_DECL, it loses. */
1467 if (TREE_CODE (a) == USING_DECL)
1468 return +1;
1469 else if (TREE_CODE (b) == USING_DECL)
1470 return -1;
1471
1472 /* There are no other cases with different kinds of decls, as
1473 duplicate detection should have kicked in earlier. However,
1474 some erroneous cases get though. */
1475 gcc_assert (errorcount);
1476 }
1477
1478 /* Using source location would be the best thing here, but we can
1479 get identically-located decls in the following circumstances:
1480
1481 1) duplicate artificial type-decls for the same type.
1482
1483 2) pack expansions of using-decls.
1484
1485 We should not be doing #1, but in either case it doesn't matter
1486 how we order these. Use UID as a proxy for source ordering, so
1487 that identically-located decls still have a well-defined stable
1488 ordering. */
1489 if (DECL_UID (a) != DECL_UID (b))
1490 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1491 gcc_assert (a == b);
1492 return 0;
1493 }
1494
1495 static struct {
1496 gt_pointer_operator new_value;
1497 void *cookie;
1498 } resort_data;
1499
1500 /* This routine compares two fields like member_name_cmp but using the
1501 pointer operator in resort_field_decl_data. We don't have to deal
1502 with duplicates here. */
1503
1504 static int
1505 resort_member_name_cmp (const void *a_p, const void *b_p)
1506 {
1507 tree a = *(const tree *)a_p;
1508 tree b = *(const tree *)b_p;
1509 tree name_a = OVL_NAME (a);
1510 tree name_b = OVL_NAME (b);
1511
1512 resort_data.new_value (&name_a, resort_data.cookie);
1513 resort_data.new_value (&name_b, resort_data.cookie);
1514
1515 gcc_checking_assert (name_a != name_b);
1516
1517 return name_a < name_b ? -1 : +1;
1518 }
1519
1520 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1521
1522 void
1523 resort_type_member_vec (void *obj, void */*orig_obj*/,
1524 gt_pointer_operator new_value, void* cookie)
1525 {
1526 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1527 {
1528 resort_data.new_value = new_value;
1529 resort_data.cookie = cookie;
1530 member_vec->qsort (resort_member_name_cmp);
1531 }
1532 }
1533
1534 /* Recursively count the number of fields in KLASS, including anonymous
1535 union members. */
1536
1537 static unsigned
1538 count_class_fields (tree klass)
1539 {
1540 unsigned n_fields = 0;
1541
1542 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1543 if (DECL_DECLARES_FUNCTION_P (fields))
1544 /* Functions are dealt with separately. */;
1545 else if (TREE_CODE (fields) == FIELD_DECL
1546 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1547 n_fields += count_class_fields (TREE_TYPE (fields));
1548 else if (DECL_NAME (fields))
1549 n_fields += 1;
1550
1551 return n_fields;
1552 }
1553
1554 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1555 Recurse for anonymous members. MEMBER_VEC must have space. */
1556
1557 static void
1558 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1559 {
1560 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1561 if (DECL_DECLARES_FUNCTION_P (fields))
1562 /* Functions are handled separately. */;
1563 else if (TREE_CODE (fields) == FIELD_DECL
1564 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1565 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1566 else if (DECL_NAME (fields))
1567 {
1568 tree field = fields;
1569 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1570 if (TREE_CODE (field) == USING_DECL
1571 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1572 field = ovl_make (conv_op_marker, field);
1573 member_vec->quick_push (field);
1574 }
1575 }
1576
1577 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1578 MEMBER_VEC must have space. */
1579
1580 static void
1581 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1582 {
1583 for (tree values = TYPE_VALUES (enumtype);
1584 values; values = TREE_CHAIN (values))
1585 member_vec->quick_push (TREE_VALUE (values));
1586 }
1587
1588 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1589 DeDup adjacent DECLS of the same name. We already dealt with
1590 conflict resolution when adding the fields or methods themselves.
1591 There are three cases (which could all be combined):
1592 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1593 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1594 it wins. Otherwise the OVERLOAD does.
1595 3) two USING_DECLS. ...
1596
1597 member_name_cmp will have ordered duplicates as
1598 <fns><using><type> */
1599
1600 static void
1601 member_vec_dedup (vec<tree, va_gc> *member_vec)
1602 {
1603 unsigned len = member_vec->length ();
1604 unsigned store = 0;
1605
1606 if (!len)
1607 return;
1608
1609 tree name = OVL_NAME ((*member_vec)[0]);
1610 for (unsigned jx, ix = 0; ix < len; ix = jx)
1611 {
1612 tree current = NULL_TREE;
1613 tree to_type = NULL_TREE;
1614 tree to_using = NULL_TREE;
1615 tree marker = NULL_TREE;
1616
1617 for (jx = ix; jx < len; jx++)
1618 {
1619 tree next = (*member_vec)[jx];
1620 if (jx != ix)
1621 {
1622 tree next_name = OVL_NAME (next);
1623 if (next_name != name)
1624 {
1625 name = next_name;
1626 break;
1627 }
1628 }
1629
1630 if (IDENTIFIER_CONV_OP_P (name))
1631 {
1632 marker = next;
1633 next = OVL_CHAIN (next);
1634 }
1635
1636 if (TREE_CODE (next) == USING_DECL)
1637 {
1638 if (IDENTIFIER_CTOR_P (name))
1639 /* Dependent inherited ctor. */
1640 continue;
1641
1642 next = strip_using_decl (next);
1643 if (TREE_CODE (next) == USING_DECL)
1644 {
1645 to_using = next;
1646 continue;
1647 }
1648
1649 if (is_overloaded_fn (next))
1650 continue;
1651 }
1652
1653 if (DECL_DECLARES_TYPE_P (next))
1654 {
1655 to_type = next;
1656 continue;
1657 }
1658
1659 if (!current)
1660 current = next;
1661 }
1662
1663 if (to_using)
1664 {
1665 if (!current)
1666 current = to_using;
1667 else
1668 current = ovl_make (to_using, current);
1669 }
1670
1671 if (to_type)
1672 {
1673 if (!current)
1674 current = to_type;
1675 else
1676 current = stat_hack (current, to_type);
1677 }
1678
1679 if (current)
1680 {
1681 if (marker)
1682 {
1683 OVL_CHAIN (marker) = current;
1684 current = marker;
1685 }
1686 (*member_vec)[store++] = current;
1687 }
1688 }
1689
1690 while (store++ < len)
1691 member_vec->pop ();
1692 }
1693
1694 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1695 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1696 know there must be at least 1 field -- the self-reference
1697 TYPE_DECL, except for anon aggregates, which will have at least
1698 one field anyway. */
1699
1700 void
1701 set_class_bindings (tree klass, unsigned extra)
1702 {
1703 unsigned n_fields = count_class_fields (klass);
1704 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1705
1706 if (member_vec || n_fields >= 8)
1707 {
1708 /* Append the new fields. */
1709 vec_safe_reserve_exact (member_vec, extra + n_fields);
1710 member_vec_append_class_fields (member_vec, klass);
1711 }
1712
1713 if (member_vec)
1714 {
1715 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1716 member_vec->qsort (member_name_cmp);
1717 member_vec_dedup (member_vec);
1718 }
1719 }
1720
1721 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1722
1723 void
1724 insert_late_enum_def_bindings (tree klass, tree enumtype)
1725 {
1726 int n_fields;
1727 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1728
1729 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1730 count them twice. */
1731 if (!member_vec)
1732 n_fields = count_class_fields (klass);
1733 else
1734 n_fields = list_length (TYPE_VALUES (enumtype));
1735
1736 if (member_vec || n_fields >= 8)
1737 {
1738 vec_safe_reserve_exact (member_vec, n_fields);
1739 if (CLASSTYPE_MEMBER_VEC (klass))
1740 member_vec_append_enum_values (member_vec, enumtype);
1741 else
1742 member_vec_append_class_fields (member_vec, klass);
1743 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1744 member_vec->qsort (member_name_cmp);
1745 member_vec_dedup (member_vec);
1746 }
1747 }
1748
1749 /* Compute the chain index of a binding_entry given the HASH value of its
1750 name and the total COUNT of chains. COUNT is assumed to be a power
1751 of 2. */
1752
1753 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1754
1755 /* A free list of "binding_entry"s awaiting for re-use. */
1756
1757 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1758
1759 /* The binding oracle; see cp-tree.h. */
1760
1761 cp_binding_oracle_function *cp_binding_oracle;
1762
1763 /* If we have a binding oracle, ask it for all namespace-scoped
1764 definitions of NAME. */
1765
1766 static inline void
1767 query_oracle (tree name)
1768 {
1769 if (!cp_binding_oracle)
1770 return;
1771
1772 /* LOOKED_UP holds the set of identifiers that we have already
1773 looked up with the oracle. */
1774 static hash_set<tree> looked_up;
1775 if (looked_up.add (name))
1776 return;
1777
1778 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1779 }
1780
1781 /* Create a binding_entry object for (NAME, TYPE). */
1782
1783 static inline binding_entry
1784 binding_entry_make (tree name, tree type)
1785 {
1786 binding_entry entry;
1787
1788 if (free_binding_entry)
1789 {
1790 entry = free_binding_entry;
1791 free_binding_entry = entry->chain;
1792 }
1793 else
1794 entry = ggc_alloc<binding_entry_s> ();
1795
1796 entry->name = name;
1797 entry->type = type;
1798 entry->chain = NULL;
1799
1800 return entry;
1801 }
1802
1803 /* Put ENTRY back on the free list. */
1804 #if 0
1805 static inline void
1806 binding_entry_free (binding_entry entry)
1807 {
1808 entry->name = NULL;
1809 entry->type = NULL;
1810 entry->chain = free_binding_entry;
1811 free_binding_entry = entry;
1812 }
1813 #endif
1814
1815 /* The datatype used to implement the mapping from names to types at
1816 a given scope. */
1817 struct GTY(()) binding_table_s {
1818 /* Array of chains of "binding_entry"s */
1819 binding_entry * GTY((length ("%h.chain_count"))) chain;
1820
1821 /* The number of chains in this table. This is the length of the
1822 member "chain" considered as an array. */
1823 size_t chain_count;
1824
1825 /* Number of "binding_entry"s in this table. */
1826 size_t entry_count;
1827 };
1828
1829 /* Construct TABLE with an initial CHAIN_COUNT. */
1830
1831 static inline void
1832 binding_table_construct (binding_table table, size_t chain_count)
1833 {
1834 table->chain_count = chain_count;
1835 table->entry_count = 0;
1836 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1837 }
1838
1839 /* Make TABLE's entries ready for reuse. */
1840 #if 0
1841 static void
1842 binding_table_free (binding_table table)
1843 {
1844 size_t i;
1845 size_t count;
1846
1847 if (table == NULL)
1848 return;
1849
1850 for (i = 0, count = table->chain_count; i < count; ++i)
1851 {
1852 binding_entry temp = table->chain[i];
1853 while (temp != NULL)
1854 {
1855 binding_entry entry = temp;
1856 temp = entry->chain;
1857 binding_entry_free (entry);
1858 }
1859 table->chain[i] = NULL;
1860 }
1861 table->entry_count = 0;
1862 }
1863 #endif
1864
1865 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1866
1867 static inline binding_table
1868 binding_table_new (size_t chain_count)
1869 {
1870 binding_table table = ggc_alloc<binding_table_s> ();
1871 table->chain = NULL;
1872 binding_table_construct (table, chain_count);
1873 return table;
1874 }
1875
1876 /* Expand TABLE to twice its current chain_count. */
1877
1878 static void
1879 binding_table_expand (binding_table table)
1880 {
1881 const size_t old_chain_count = table->chain_count;
1882 const size_t old_entry_count = table->entry_count;
1883 const size_t new_chain_count = 2 * old_chain_count;
1884 binding_entry *old_chains = table->chain;
1885 size_t i;
1886
1887 binding_table_construct (table, new_chain_count);
1888 for (i = 0; i < old_chain_count; ++i)
1889 {
1890 binding_entry entry = old_chains[i];
1891 for (; entry != NULL; entry = old_chains[i])
1892 {
1893 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1894 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1895
1896 old_chains[i] = entry->chain;
1897 entry->chain = table->chain[j];
1898 table->chain[j] = entry;
1899 }
1900 }
1901 table->entry_count = old_entry_count;
1902 }
1903
1904 /* Insert a binding for NAME to TYPE into TABLE. */
1905
1906 static void
1907 binding_table_insert (binding_table table, tree name, tree type)
1908 {
1909 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1910 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1911 binding_entry entry = binding_entry_make (name, type);
1912
1913 entry->chain = table->chain[i];
1914 table->chain[i] = entry;
1915 ++table->entry_count;
1916
1917 if (3 * table->chain_count < 5 * table->entry_count)
1918 binding_table_expand (table);
1919 }
1920
1921 /* Return the binding_entry, if any, that maps NAME. */
1922
1923 binding_entry
1924 binding_table_find (binding_table table, tree name)
1925 {
1926 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1927 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1928
1929 while (entry != NULL && entry->name != name)
1930 entry = entry->chain;
1931
1932 return entry;
1933 }
1934
1935 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1936
1937 void
1938 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1939 {
1940 size_t chain_count;
1941 size_t i;
1942
1943 if (!table)
1944 return;
1945
1946 chain_count = table->chain_count;
1947 for (i = 0; i < chain_count; ++i)
1948 {
1949 binding_entry entry = table->chain[i];
1950 for (; entry != NULL; entry = entry->chain)
1951 proc (entry, data);
1952 }
1953 }
1954 \f
1955 #ifndef ENABLE_SCOPE_CHECKING
1956 # define ENABLE_SCOPE_CHECKING 0
1957 #else
1958 # define ENABLE_SCOPE_CHECKING 1
1959 #endif
1960
1961 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1962
1963 static GTY((deletable)) cxx_binding *free_bindings;
1964
1965 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1966 field to NULL. */
1967
1968 static inline void
1969 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1970 {
1971 binding->value = value;
1972 binding->type = type;
1973 binding->previous = NULL;
1974 }
1975
1976 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1977
1978 static cxx_binding *
1979 cxx_binding_make (tree value, tree type)
1980 {
1981 cxx_binding *binding = free_bindings;
1982
1983 if (binding)
1984 free_bindings = binding->previous;
1985 else
1986 binding = ggc_alloc<cxx_binding> ();
1987
1988 /* Clear flags by default. */
1989 LOCAL_BINDING_P (binding) = false;
1990 INHERITED_VALUE_BINDING_P (binding) = false;
1991 HIDDEN_TYPE_BINDING_P (binding) = false;
1992
1993 cxx_binding_init (binding, value, type);
1994
1995 return binding;
1996 }
1997
1998 /* Put BINDING back on the free list. */
1999
2000 static inline void
2001 cxx_binding_free (cxx_binding *binding)
2002 {
2003 binding->scope = NULL;
2004 binding->previous = free_bindings;
2005 free_bindings = binding;
2006 }
2007
2008 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2009 bindings) in the class scope indicated by SCOPE. */
2010
2011 static cxx_binding *
2012 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2013 {
2014 cp_class_binding cb = {cxx_binding_make (value, type), name};
2015 cxx_binding *binding = cb.base;
2016 vec_safe_push (scope->class_shadowed, cb);
2017 binding->scope = scope;
2018 return binding;
2019 }
2020
2021 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2022 level at which this declaration is being bound. */
2023
2024 void
2025 push_binding (tree id, tree decl, cp_binding_level* level)
2026 {
2027 cxx_binding *binding;
2028
2029 if (level != class_binding_level)
2030 {
2031 binding = cxx_binding_make (decl, NULL_TREE);
2032 binding->scope = level;
2033 }
2034 else
2035 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2036
2037 /* Now, fill in the binding information. */
2038 binding->previous = IDENTIFIER_BINDING (id);
2039 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2040
2041 /* And put it on the front of the list of bindings for ID. */
2042 IDENTIFIER_BINDING (id) = binding;
2043 }
2044
2045 /* Remove the binding for DECL which should be the innermost binding
2046 for ID. */
2047
2048 void
2049 pop_local_binding (tree id, tree decl)
2050 {
2051 if (id == NULL_TREE)
2052 /* It's easiest to write the loops that call this function without
2053 checking whether or not the entities involved have names. We
2054 get here for such an entity. */
2055 return;
2056
2057 /* Get the innermost binding for ID. */
2058 cxx_binding *binding = IDENTIFIER_BINDING (id);
2059
2060 /* The name should be bound. */
2061 gcc_assert (binding != NULL);
2062
2063 /* The DECL will be either the ordinary binding or the type binding
2064 for this identifier. Remove that binding. We don't have to
2065 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2066 away. */
2067 if (binding->value == decl)
2068 binding->value = NULL_TREE;
2069 else
2070 {
2071 gcc_checking_assert (binding->type == decl);
2072 binding->type = NULL_TREE;
2073 }
2074
2075 if (!binding->value && !binding->type)
2076 {
2077 /* We're completely done with the innermost binding for this
2078 identifier. Unhook it from the list of bindings. */
2079 IDENTIFIER_BINDING (id) = binding->previous;
2080
2081 /* Add it to the free list. */
2082 cxx_binding_free (binding);
2083 }
2084 }
2085
2086 /* Remove the bindings for the decls of the current level and leave
2087 the current scope. */
2088
2089 void
2090 pop_bindings_and_leave_scope (void)
2091 {
2092 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2093 {
2094 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2095 tree name = OVL_NAME (decl);
2096
2097 pop_local_binding (name, decl);
2098 }
2099
2100 leave_scope ();
2101 }
2102
2103 /* Strip non dependent using declarations. If DECL is dependent,
2104 surreptitiously create a typename_type and return it. */
2105
2106 tree
2107 strip_using_decl (tree decl)
2108 {
2109 if (decl == NULL_TREE)
2110 return NULL_TREE;
2111
2112 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2113 decl = USING_DECL_DECLS (decl);
2114
2115 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2116 && USING_DECL_TYPENAME_P (decl))
2117 {
2118 /* We have found a type introduced by a using
2119 declaration at class scope that refers to a dependent
2120 type.
2121
2122 using typename :: [opt] nested-name-specifier unqualified-id ;
2123 */
2124 decl = make_typename_type (USING_DECL_SCOPE (decl),
2125 DECL_NAME (decl),
2126 typename_type, tf_error);
2127 if (decl != error_mark_node)
2128 decl = TYPE_NAME (decl);
2129 }
2130
2131 return decl;
2132 }
2133
2134 /* Return true if OVL is an overload for an anticipated builtin. */
2135
2136 static bool
2137 anticipated_builtin_p (tree ovl)
2138 {
2139 if (TREE_CODE (ovl) != OVERLOAD)
2140 return false;
2141
2142 if (!OVL_HIDDEN_P (ovl))
2143 return false;
2144
2145 tree fn = OVL_FUNCTION (ovl);
2146 gcc_checking_assert (DECL_ANTICIPATED (fn));
2147
2148 if (DECL_BUILTIN_P (fn))
2149 return true;
2150
2151 return false;
2152 }
2153
2154 /* BINDING records an existing declaration for a name in the current scope.
2155 But, DECL is another declaration for that same identifier in the
2156 same scope. This is the `struct stat' hack whereby a non-typedef
2157 class name or enum-name can be bound at the same level as some other
2158 kind of entity.
2159 3.3.7/1
2160
2161 A class name (9.1) or enumeration name (7.2) can be hidden by the
2162 name of an object, function, or enumerator declared in the same scope.
2163 If a class or enumeration name and an object, function, or enumerator
2164 are declared in the same scope (in any order) with the same name, the
2165 class or enumeration name is hidden wherever the object, function, or
2166 enumerator name is visible.
2167
2168 It's the responsibility of the caller to check that
2169 inserting this name is valid here. Returns nonzero if the new binding
2170 was successful. */
2171
2172 static bool
2173 supplement_binding_1 (cxx_binding *binding, tree decl)
2174 {
2175 tree bval = binding->value;
2176 bool ok = true;
2177 tree target_bval = strip_using_decl (bval);
2178 tree target_decl = strip_using_decl (decl);
2179
2180 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2181 && target_decl != target_bval
2182 && (TREE_CODE (target_bval) != TYPE_DECL
2183 /* We allow pushing an enum multiple times in a class
2184 template in order to handle late matching of underlying
2185 type on an opaque-enum-declaration followed by an
2186 enum-specifier. */
2187 || (processing_template_decl
2188 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2189 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2190 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2191 (TREE_TYPE (target_decl)))
2192 || dependent_type_p (ENUM_UNDERLYING_TYPE
2193 (TREE_TYPE (target_bval)))))))
2194 /* The new name is the type name. */
2195 binding->type = decl;
2196 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2197 an inherited type-binding out of the way to make room
2198 for a new value binding. */
2199 !target_bval
2200 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2201 has been used in a non-class scope prior declaration.
2202 In that case, we should have already issued a
2203 diagnostic; for graceful error recovery purpose, pretend
2204 this was the intended declaration for that name. */
2205 || target_bval == error_mark_node
2206 /* If TARGET_BVAL is anticipated but has not yet been
2207 declared, pretend it is not there at all. */
2208 || anticipated_builtin_p (target_bval))
2209 binding->value = decl;
2210 else if (TREE_CODE (target_bval) == TYPE_DECL
2211 && DECL_ARTIFICIAL (target_bval)
2212 && target_decl != target_bval
2213 && (TREE_CODE (target_decl) != TYPE_DECL
2214 || same_type_p (TREE_TYPE (target_decl),
2215 TREE_TYPE (target_bval))))
2216 {
2217 /* The old binding was a type name. It was placed in
2218 VALUE field because it was thought, at the point it was
2219 declared, to be the only entity with such a name. Move the
2220 type name into the type slot; it is now hidden by the new
2221 binding. */
2222 binding->type = bval;
2223 binding->value = decl;
2224 binding->value_is_inherited = false;
2225 }
2226 else if (TREE_CODE (target_bval) == TYPE_DECL
2227 && TREE_CODE (target_decl) == TYPE_DECL
2228 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2229 && binding->scope->kind != sk_class
2230 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2231 /* If either type involves template parameters, we must
2232 wait until instantiation. */
2233 || uses_template_parms (TREE_TYPE (target_decl))
2234 || uses_template_parms (TREE_TYPE (target_bval))))
2235 /* We have two typedef-names, both naming the same type to have
2236 the same name. In general, this is OK because of:
2237
2238 [dcl.typedef]
2239
2240 In a given scope, a typedef specifier can be used to redefine
2241 the name of any type declared in that scope to refer to the
2242 type to which it already refers.
2243
2244 However, in class scopes, this rule does not apply due to the
2245 stricter language in [class.mem] prohibiting redeclarations of
2246 members. */
2247 ok = false;
2248 /* There can be two block-scope declarations of the same variable,
2249 so long as they are `extern' declarations. However, there cannot
2250 be two declarations of the same static data member:
2251
2252 [class.mem]
2253
2254 A member shall not be declared twice in the
2255 member-specification. */
2256 else if (VAR_P (target_decl)
2257 && VAR_P (target_bval)
2258 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2259 && !DECL_CLASS_SCOPE_P (target_decl))
2260 {
2261 duplicate_decls (decl, binding->value);
2262 ok = false;
2263 }
2264 else if (TREE_CODE (decl) == NAMESPACE_DECL
2265 && TREE_CODE (bval) == NAMESPACE_DECL
2266 && DECL_NAMESPACE_ALIAS (decl)
2267 && DECL_NAMESPACE_ALIAS (bval)
2268 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2269 /* [namespace.alias]
2270
2271 In a declarative region, a namespace-alias-definition can be
2272 used to redefine a namespace-alias declared in that declarative
2273 region to refer only to the namespace to which it already
2274 refers. */
2275 ok = false;
2276 else
2277 {
2278 if (!error_operand_p (bval))
2279 diagnose_name_conflict (decl, bval);
2280 ok = false;
2281 }
2282
2283 return ok;
2284 }
2285
2286 /* Diagnose a name conflict between DECL and BVAL. */
2287
2288 static void
2289 diagnose_name_conflict (tree decl, tree bval)
2290 {
2291 if (TREE_CODE (decl) == TREE_CODE (bval)
2292 && TREE_CODE (decl) != NAMESPACE_DECL
2293 && !DECL_DECLARES_FUNCTION_P (decl)
2294 && (TREE_CODE (decl) != TYPE_DECL
2295 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2296 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2297 {
2298 if (concept_definition_p (decl))
2299 error ("redeclaration of %q#D with different template parameters",
2300 decl);
2301 else
2302 error ("redeclaration of %q#D", decl);
2303 }
2304 else
2305 error ("%q#D conflicts with a previous declaration", decl);
2306
2307 inform (location_of (bval), "previous declaration %q#D", bval);
2308 }
2309
2310 /* Wrapper for supplement_binding_1. */
2311
2312 static bool
2313 supplement_binding (cxx_binding *binding, tree decl)
2314 {
2315 bool ret;
2316 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2317 ret = supplement_binding_1 (binding, decl);
2318 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2319 return ret;
2320 }
2321
2322 /* Replace BINDING's current value on its scope's name list with
2323 NEWVAL. */
2324
2325 static void
2326 update_local_overload (cxx_binding *binding, tree newval)
2327 {
2328 tree *d;
2329
2330 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2331 if (*d == binding->value)
2332 {
2333 /* Stitch new list node in. */
2334 *d = tree_cons (DECL_NAME (*d), NULL_TREE, TREE_CHAIN (*d));
2335 break;
2336 }
2337 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2338 break;
2339
2340 TREE_VALUE (*d) = newval;
2341 }
2342
2343 /* Compares the parameter-type-lists of ONE and TWO and
2344 returns false if they are different. If the DECLs are template
2345 functions, the return types and the template parameter lists are
2346 compared too (DR 565). */
2347
2348 static bool
2349 matching_fn_p (tree one, tree two)
2350 {
2351 if (TREE_CODE (one) != TREE_CODE (two))
2352 return false;
2353
2354 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2355 TYPE_ARG_TYPES (TREE_TYPE (two))))
2356 return false;
2357
2358 if (TREE_CODE (one) == TEMPLATE_DECL)
2359 {
2360 /* Compare template parms. */
2361 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2362 DECL_TEMPLATE_PARMS (two)))
2363 return false;
2364
2365 /* And return type. */
2366 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2367 TREE_TYPE (TREE_TYPE (two))))
2368 return false;
2369 }
2370
2371 if (!equivalently_constrained (one, two))
2372 return false;
2373
2374 return true;
2375 }
2376
2377 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2378 binding value (possibly with anticipated builtins stripped).
2379 Diagnose conflicts and return updated decl. */
2380
2381 static tree
2382 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2383 tree old, tree decl, bool hiding = false)
2384 {
2385 tree old_type = NULL_TREE;
2386 bool hide_type = false;
2387 bool hide_value = false;
2388
2389 if (!slot)
2390 {
2391 old_type = binding->type;
2392 hide_type = HIDDEN_TYPE_BINDING_P (binding);
2393 if (!old_type)
2394 hide_value = hide_type, hide_type = false;
2395 }
2396 else if (STAT_HACK_P (*slot))
2397 {
2398 old_type = STAT_TYPE (*slot);
2399 hide_type = STAT_TYPE_HIDDEN_P (*slot);
2400 hide_value = STAT_DECL_HIDDEN_P (*slot);
2401 }
2402
2403 tree to_val = decl;
2404 tree to_type = old_type;
2405 bool local_overload = false;
2406
2407 gcc_assert (level->kind == sk_namespace ? !binding
2408 : level->kind != sk_class && !slot);
2409 if (old == error_mark_node)
2410 old = NULL_TREE;
2411
2412 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2413 {
2414 /* Pushing an artificial decl. We should not find another
2415 artificial decl here already -- lookup_elaborated_type will
2416 have already found it. */
2417 gcc_checking_assert (!to_type
2418 && !(old && DECL_IMPLICIT_TYPEDEF_P (old)));
2419
2420 if (old)
2421 {
2422 /* Put DECL into the type slot. */
2423 gcc_checking_assert (!to_type);
2424 hide_type = hiding;
2425 to_type = decl;
2426 to_val = old;
2427 }
2428 else
2429 hide_value = hiding;
2430
2431 goto done;
2432 }
2433
2434 if (old && DECL_IMPLICIT_TYPEDEF_P (old))
2435 {
2436 /* OLD is an implicit typedef. Move it to to_type. */
2437 gcc_checking_assert (!to_type);
2438
2439 to_type = old;
2440 hide_type = hide_value;
2441 old = NULL_TREE;
2442 hide_value = false;
2443 }
2444
2445 if (DECL_DECLARES_FUNCTION_P (decl))
2446 {
2447 if (!old)
2448 ;
2449 else if (OVL_P (old))
2450 {
2451 for (ovl_iterator iter (old); iter; ++iter)
2452 {
2453 tree fn = *iter;
2454
2455 if (iter.using_p () && matching_fn_p (fn, decl))
2456 {
2457 gcc_checking_assert (!iter.hidden_p ());
2458 /* If a function declaration in namespace scope or
2459 block scope has the same name and the same
2460 parameter-type- list (8.3.5) as a function
2461 introduced by a using-declaration, and the
2462 declarations do not declare the same function,
2463 the program is ill-formed. [namespace.udecl]/14 */
2464 if (tree match = duplicate_decls (decl, fn, hiding))
2465 return match;
2466 else
2467 /* FIXME: To preserve existing error behavior, we
2468 still push the decl. This might change. */
2469 diagnose_name_conflict (decl, fn);
2470 }
2471 }
2472 }
2473 else
2474 goto conflict;
2475
2476 if (to_type != old_type
2477 && warn_shadow
2478 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2479 && !(DECL_IN_SYSTEM_HEADER (decl)
2480 && DECL_IN_SYSTEM_HEADER (to_type)))
2481 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2482 decl, to_type);
2483
2484 local_overload = old && level->kind != sk_namespace;
2485 to_val = ovl_insert (decl, old, -int (hiding));
2486 }
2487 else if (old)
2488 {
2489 if (TREE_CODE (old) != TREE_CODE (decl))
2490 /* Different kinds of decls conflict. */
2491 goto conflict;
2492 else if (TREE_CODE (old) == TYPE_DECL)
2493 {
2494 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2495 {
2496 /* Two type decls to the same type. Do nothing. */
2497 gcc_checking_assert (!hiding);
2498 return old;
2499 }
2500 else
2501 goto conflict;
2502 }
2503 else if (TREE_CODE (old) == NAMESPACE_DECL)
2504 {
2505 /* Two maybe-aliased namespaces. If they're to the same target
2506 namespace, that's ok. */
2507 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2508 goto conflict;
2509
2510 /* The new one must be an alias at this point. */
2511 gcc_assert (DECL_NAMESPACE_ALIAS (decl) && !hiding);
2512 return old;
2513 }
2514 else if (TREE_CODE (old) == VAR_DECL)
2515 {
2516 /* There can be two block-scope declarations of the same
2517 variable, so long as they are `extern' declarations. */
2518 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2519 goto conflict;
2520 else if (tree match = duplicate_decls (decl, old))
2521 {
2522 gcc_checking_assert (!hide_value && !hiding);
2523 return match;
2524 }
2525 else
2526 goto conflict;
2527 }
2528 else
2529 {
2530 conflict:
2531 diagnose_name_conflict (decl, old);
2532 to_val = NULL_TREE;
2533 }
2534 }
2535 else if (hiding)
2536 hide_value = true;
2537
2538 done:
2539 if (to_val)
2540 {
2541 if (local_overload)
2542 {
2543 gcc_checking_assert (binding->value && OVL_P (binding->value));
2544 update_local_overload (binding, to_val);
2545 }
2546 else
2547 add_decl_to_level (level, decl);
2548
2549 if (slot)
2550 {
2551 if (STAT_HACK_P (*slot))
2552 {
2553 STAT_TYPE (*slot) = to_type;
2554 STAT_DECL (*slot) = to_val;
2555 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
2556 STAT_DECL_HIDDEN_P (*slot) = hide_value;
2557 }
2558 else if (to_type || hide_value)
2559 {
2560 *slot = stat_hack (to_val, to_type);
2561 STAT_TYPE_HIDDEN_P (*slot) = hide_type;
2562 STAT_DECL_HIDDEN_P (*slot) = hide_value;
2563 }
2564 else
2565 {
2566 gcc_checking_assert (!hide_type);
2567 *slot = to_val;
2568 }
2569 }
2570 else
2571 {
2572 binding->type = to_type;
2573 binding->value = to_val;
2574 HIDDEN_TYPE_BINDING_P (binding) = hide_type || hide_value;
2575 }
2576 }
2577
2578 return decl;
2579 }
2580
2581 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2582
2583 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2584
2585 /* DECL has C linkage. If we have an existing instance, make sure the
2586 new one is compatible. Make sure it has the same exception
2587 specification [7.5, 7.6]. Add DECL to the map. */
2588
2589 static void
2590 check_extern_c_conflict (tree decl)
2591 {
2592 /* Ignore artificial or system header decls. */
2593 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2594 return;
2595
2596 /* This only applies to decls at namespace scope. */
2597 if (!DECL_NAMESPACE_SCOPE_P (decl))
2598 return;
2599
2600 if (!extern_c_decls)
2601 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2602
2603 tree *slot = extern_c_decls
2604 ->find_slot_with_hash (DECL_NAME (decl),
2605 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2606 if (tree old = *slot)
2607 {
2608 if (TREE_CODE (old) == OVERLOAD)
2609 old = OVL_FUNCTION (old);
2610
2611 int mismatch = 0;
2612 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2613 ; /* If they're in the same context, we'll have already complained
2614 about a (possible) mismatch, when inserting the decl. */
2615 else if (!decls_match (decl, old))
2616 mismatch = 1;
2617 else if (TREE_CODE (decl) == FUNCTION_DECL
2618 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2619 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2620 ce_normal))
2621 mismatch = -1;
2622 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2623 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2624
2625 if (mismatch)
2626 {
2627 auto_diagnostic_group d;
2628 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2629 "conflicting C language linkage declaration %q#D", decl);
2630 inform (DECL_SOURCE_LOCATION (old),
2631 "previous declaration %q#D", old);
2632 if (mismatch < 0)
2633 inform (DECL_SOURCE_LOCATION (decl),
2634 "due to different exception specifications");
2635 }
2636 else
2637 {
2638 if (old == *slot)
2639 /* The hash table expects OVERLOADS, so construct one with
2640 OLD as both the function and the chain. This allocate
2641 an excess OVERLOAD node, but it's rare to have multiple
2642 extern "C" decls of the same name. And we save
2643 complicating the hash table logic (which is used
2644 elsewhere). */
2645 *slot = ovl_make (old, old);
2646
2647 slot = &OVL_CHAIN (*slot);
2648
2649 /* Chain it on for c_linkage_binding's use. */
2650 *slot = tree_cons (NULL_TREE, decl, *slot);
2651 }
2652 }
2653 else
2654 *slot = decl;
2655 }
2656
2657 /* Returns a list of C-linkage decls with the name NAME. Used in
2658 c-family/c-pragma.c to implement redefine_extname pragma. */
2659
2660 tree
2661 c_linkage_bindings (tree name)
2662 {
2663 if (extern_c_decls)
2664 if (tree *slot = extern_c_decls
2665 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2666 {
2667 tree result = *slot;
2668 if (TREE_CODE (result) == OVERLOAD)
2669 result = OVL_CHAIN (result);
2670 return result;
2671 }
2672
2673 return NULL_TREE;
2674 }
2675
2676 /* Subroutine of check_local_shadow. */
2677
2678 static void
2679 inform_shadowed (tree shadowed)
2680 {
2681 inform (DECL_SOURCE_LOCATION (shadowed),
2682 "shadowed declaration is here");
2683 }
2684
2685 /* DECL is being declared at a local scope. Emit suitable shadow
2686 warnings. */
2687
2688 static void
2689 check_local_shadow (tree decl)
2690 {
2691 /* Don't complain about the parms we push and then pop
2692 while tentatively parsing a function declarator. */
2693 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2694 return;
2695
2696 /* External decls are something else. */
2697 if (DECL_EXTERNAL (decl))
2698 return;
2699
2700 tree old = NULL_TREE;
2701 cp_binding_level *old_scope = NULL;
2702 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2703 {
2704 old = binding->value;
2705 old_scope = binding->scope;
2706 }
2707
2708 if (old
2709 && (TREE_CODE (old) == PARM_DECL
2710 || VAR_P (old)
2711 || (TREE_CODE (old) == TYPE_DECL
2712 && (!DECL_ARTIFICIAL (old)
2713 || TREE_CODE (decl) == TYPE_DECL)))
2714 && DECL_FUNCTION_SCOPE_P (old)
2715 && (!DECL_ARTIFICIAL (decl)
2716 || is_capture_proxy (decl)
2717 || DECL_IMPLICIT_TYPEDEF_P (decl)
2718 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2719 {
2720 /* DECL shadows a local thing possibly of interest. */
2721
2722 /* DR 2211: check that captures and parameters
2723 do not have the same name. */
2724 if (is_capture_proxy (decl))
2725 {
2726 if (current_lambda_expr ()
2727 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2728 && TREE_CODE (old) == PARM_DECL
2729 && DECL_NAME (decl) != this_identifier)
2730 {
2731 error_at (DECL_SOURCE_LOCATION (old),
2732 "lambda parameter %qD "
2733 "previously declared as a capture", old);
2734 }
2735 return;
2736 }
2737 /* Don't complain if it's from an enclosing function. */
2738 else if (DECL_CONTEXT (old) == current_function_decl
2739 && TREE_CODE (decl) != PARM_DECL
2740 && TREE_CODE (old) == PARM_DECL)
2741 {
2742 /* Go to where the parms should be and see if we find
2743 them there. */
2744 cp_binding_level *b = current_binding_level->level_chain;
2745
2746 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2747 /* Skip the ctor/dtor cleanup level. */
2748 b = b->level_chain;
2749
2750 /* [basic.scope.param] A parameter name shall not be redeclared
2751 in the outermost block of the function definition. */
2752 if (b->kind == sk_function_parms)
2753 {
2754 error_at (DECL_SOURCE_LOCATION (decl),
2755 "declaration of %q#D shadows a parameter", decl);
2756 inform (DECL_SOURCE_LOCATION (old),
2757 "%q#D previously declared here", old);
2758 return;
2759 }
2760 }
2761
2762 /* The local structure or class can't use parameters of
2763 the containing function anyway. */
2764 if (DECL_CONTEXT (old) != current_function_decl)
2765 {
2766 for (cp_binding_level *scope = current_binding_level;
2767 scope != old_scope; scope = scope->level_chain)
2768 if (scope->kind == sk_class
2769 && !LAMBDA_TYPE_P (scope->this_entity))
2770 return;
2771 }
2772 /* Error if redeclaring a local declared in a
2773 init-statement or in the condition of an if or
2774 switch statement when the new declaration is in the
2775 outermost block of the controlled statement.
2776 Redeclaring a variable from a for or while condition is
2777 detected elsewhere. */
2778 else if (VAR_P (old)
2779 && old_scope == current_binding_level->level_chain
2780 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2781 {
2782 auto_diagnostic_group d;
2783 error_at (DECL_SOURCE_LOCATION (decl),
2784 "redeclaration of %q#D", decl);
2785 inform (DECL_SOURCE_LOCATION (old),
2786 "%q#D previously declared here", old);
2787 return;
2788 }
2789 /* C++11:
2790 3.3.3/3: The name declared in an exception-declaration (...)
2791 shall not be redeclared in the outermost block of the handler.
2792 3.3.3/2: A parameter name shall not be redeclared (...) in
2793 the outermost block of any handler associated with a
2794 function-try-block.
2795 3.4.1/15: The function parameter names shall not be redeclared
2796 in the exception-declaration nor in the outermost block of a
2797 handler for the function-try-block. */
2798 else if ((TREE_CODE (old) == VAR_DECL
2799 && old_scope == current_binding_level->level_chain
2800 && old_scope->kind == sk_catch)
2801 || (TREE_CODE (old) == PARM_DECL
2802 && (current_binding_level->kind == sk_catch
2803 || current_binding_level->level_chain->kind == sk_catch)
2804 && in_function_try_handler))
2805 {
2806 auto_diagnostic_group d;
2807 if (permerror (DECL_SOURCE_LOCATION (decl),
2808 "redeclaration of %q#D", decl))
2809 inform (DECL_SOURCE_LOCATION (old),
2810 "%q#D previously declared here", old);
2811 return;
2812 }
2813
2814 /* If '-Wshadow=compatible-local' is specified without other
2815 -Wshadow= flags, we will warn only when the type of the
2816 shadowing variable (DECL) can be converted to that of the
2817 shadowed parameter (OLD_LOCAL). The reason why we only check
2818 if DECL's type can be converted to OLD_LOCAL's type (but not the
2819 other way around) is because when users accidentally shadow a
2820 parameter, more than often they would use the variable
2821 thinking (mistakenly) it's still the parameter. It would be
2822 rare that users would use the variable in the place that
2823 expects the parameter but thinking it's a new decl.
2824 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
2825 warns regardless of whether one of the types involved
2826 is a subclass of the other, since that is never okay. */
2827
2828 enum opt_code warning_code;
2829 if (warn_shadow)
2830 warning_code = OPT_Wshadow;
2831 else if ((TREE_TYPE (old)
2832 && TREE_TYPE (decl)
2833 && same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2834 || TREE_CODE (decl) == TYPE_DECL
2835 || TREE_CODE (old) == TYPE_DECL
2836 || (!dependent_type_p (TREE_TYPE (decl))
2837 && !dependent_type_p (TREE_TYPE (old))
2838 /* If the new decl uses auto, we don't yet know
2839 its type (the old type cannot be using auto
2840 at this point, without also being
2841 dependent). This is an indication we're
2842 (now) doing the shadow checking too
2843 early. */
2844 && !type_uses_auto (TREE_TYPE (decl))
2845 && can_convert_arg (TREE_TYPE (old), TREE_TYPE (decl),
2846 decl, LOOKUP_IMPLICIT, tf_none)))
2847 warning_code = OPT_Wshadow_compatible_local;
2848 else
2849 warning_code = OPT_Wshadow_local;
2850
2851 const char *msg;
2852 if (TREE_CODE (old) == PARM_DECL)
2853 msg = "declaration of %q#D shadows a parameter";
2854 else if (is_capture_proxy (old))
2855 msg = "declaration of %qD shadows a lambda capture";
2856 else
2857 msg = "declaration of %qD shadows a previous local";
2858
2859 auto_diagnostic_group d;
2860 if (warning_at (DECL_SOURCE_LOCATION (decl), warning_code, msg, decl))
2861 inform_shadowed (old);
2862 return;
2863 }
2864
2865 if (!warn_shadow)
2866 return;
2867
2868 /* Don't warn for artificial things that are not implicit typedefs. */
2869 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2870 return;
2871
2872 if (nonlambda_method_basetype ())
2873 if (tree member = lookup_member (current_nonlambda_class_type (),
2874 DECL_NAME (decl), /*protect=*/0,
2875 /*want_type=*/false, tf_warning_or_error))
2876 {
2877 member = MAYBE_BASELINK_FUNCTIONS (member);
2878
2879 /* Warn if a variable shadows a non-function, or the variable
2880 is a function or a pointer-to-function. */
2881 if (!OVL_P (member)
2882 || TREE_CODE (decl) == FUNCTION_DECL
2883 || TYPE_PTRFN_P (TREE_TYPE (decl))
2884 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2885 {
2886 auto_diagnostic_group d;
2887 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
2888 "declaration of %qD shadows a member of %qT",
2889 decl, current_nonlambda_class_type ())
2890 && DECL_P (member))
2891 inform_shadowed (member);
2892 }
2893 return;
2894 }
2895
2896 /* Now look for a namespace shadow. */
2897 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2898 if (old
2899 && (VAR_P (old)
2900 || (TREE_CODE (old) == TYPE_DECL
2901 && (!DECL_ARTIFICIAL (old)
2902 || TREE_CODE (decl) == TYPE_DECL)))
2903 && !instantiating_current_function_p ())
2904 /* XXX shadow warnings in outer-more namespaces */
2905 {
2906 auto_diagnostic_group d;
2907 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
2908 "declaration of %qD shadows a global declaration",
2909 decl))
2910 inform_shadowed (old);
2911 return;
2912 }
2913
2914 return;
2915 }
2916
2917 /* DECL is being pushed inside function CTX. Set its context, if
2918 needed. */
2919
2920 static void
2921 set_decl_context_in_fn (tree ctx, tree decl)
2922 {
2923 if (TREE_CODE (decl) == FUNCTION_DECL
2924 || (VAR_P (decl) && DECL_EXTERNAL (decl)))
2925 /* Make sure local externs are marked as such. OMP UDRs really
2926 are nested functions. */
2927 gcc_checking_assert (DECL_LOCAL_DECL_P (decl)
2928 && (DECL_NAMESPACE_SCOPE_P (decl)
2929 || (TREE_CODE (decl) == FUNCTION_DECL
2930 && DECL_OMP_DECLARE_REDUCTION_P (decl))));
2931
2932 if (!DECL_CONTEXT (decl)
2933 /* When parsing the parameter list of a function declarator,
2934 don't set DECL_CONTEXT to an enclosing function. When we
2935 push the PARM_DECLs in order to process the function body,
2936 current_binding_level->this_entity will be set. */
2937 && !(TREE_CODE (decl) == PARM_DECL
2938 && current_binding_level->kind == sk_function_parms
2939 && current_binding_level->this_entity == NULL))
2940 DECL_CONTEXT (decl) = ctx;
2941 }
2942
2943 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2944 name is already bound at the current level.
2945
2946 [basic.link] If there is a visible declaration of an entity with
2947 linkage having the same name and type, ignoring entities declared
2948 outside the innermost enclosing namespace scope, the block scope
2949 declaration declares that same entity and receives the linkage of
2950 the previous declaration.
2951
2952 Also, make sure that this decl matches any existing external decl
2953 in the enclosing namespace. */
2954
2955 static void
2956 set_local_extern_decl_linkage (tree decl, bool shadowed)
2957 {
2958 tree ns_value = decl; /* Unique marker. */
2959
2960 if (!shadowed)
2961 {
2962 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2963 if (!loc_value)
2964 {
2965 ns_value
2966 = find_namespace_value (current_namespace, DECL_NAME (decl));
2967 loc_value = ns_value;
2968 }
2969 if (loc_value == error_mark_node
2970 /* An ambiguous lookup. */
2971 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2972 loc_value = NULL_TREE;
2973
2974 for (ovl_iterator iter (loc_value); iter; ++iter)
2975 if (!iter.hidden_p ()
2976 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2977 && decls_match (*iter, decl))
2978 {
2979 /* The standard only says that the local extern inherits
2980 linkage from the previous decl; in particular, default
2981 args are not shared. Add the decl into a hash table to
2982 make sure only the previous decl in this case is seen
2983 by the middle end. */
2984 struct cxx_int_tree_map *h;
2985
2986 /* We inherit the outer decl's linkage. But we're a
2987 different decl. */
2988 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2989
2990 if (cp_function_chain->extern_decl_map == NULL)
2991 cp_function_chain->extern_decl_map
2992 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2993
2994 h = ggc_alloc<cxx_int_tree_map> ();
2995 h->uid = DECL_UID (decl);
2996 h->to = *iter;
2997 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2998 ->find_slot (h, INSERT);
2999 *loc = h;
3000 break;
3001 }
3002 }
3003
3004 if (TREE_PUBLIC (decl))
3005 {
3006 /* DECL is externally visible. Make sure it matches a matching
3007 decl in the namespace scope. We only really need to check
3008 this when inserting the decl, not when we find an existing
3009 match in the current scope. However, in practice we're
3010 going to be inserting a new decl in the majority of cases --
3011 who writes multiple extern decls for the same thing in the
3012 same local scope? Doing it here often avoids a duplicate
3013 namespace lookup. */
3014
3015 /* Avoid repeating a lookup. */
3016 if (ns_value == decl)
3017 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
3018
3019 if (ns_value == error_mark_node
3020 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
3021 ns_value = NULL_TREE;
3022
3023 for (ovl_iterator iter (ns_value); iter; ++iter)
3024 {
3025 tree other = *iter;
3026
3027 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
3028 ; /* Not externally visible. */
3029 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
3030 ; /* Both are extern "C", we'll check via that mechanism. */
3031 else if (TREE_CODE (other) != TREE_CODE (decl)
3032 || ((VAR_P (decl) || matching_fn_p (other, decl))
3033 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
3034 COMPARE_REDECLARATION)))
3035 {
3036 auto_diagnostic_group d;
3037 if (permerror (DECL_SOURCE_LOCATION (decl),
3038 "local external declaration %q#D", decl))
3039 inform (DECL_SOURCE_LOCATION (other),
3040 "does not match previous declaration %q#D", other);
3041 break;
3042 }
3043 }
3044 }
3045 }
3046
3047 /* Record DECL as belonging to the current lexical scope. Check for
3048 errors (such as an incompatible declaration for the same name
3049 already seen in the same scope). IS_FRIEND is true if DECL is
3050 declared as a friend.
3051
3052 Returns either DECL or an old decl for the same name. If an old
3053 decl is returned, it may have been smashed to agree with what DECL
3054 says. */
3055
3056 static tree
3057 do_pushdecl (tree decl, bool hiding)
3058 {
3059 if (decl == error_mark_node)
3060 return error_mark_node;
3061
3062 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl && !hiding)
3063 set_decl_context_in_fn (current_function_decl, decl);
3064
3065 /* The binding level we will be pushing into. During local class
3066 pushing, we want to push to the containing scope. */
3067 cp_binding_level *level = current_binding_level;
3068 while (level->kind == sk_class
3069 || level->kind == sk_cleanup)
3070 level = level->level_chain;
3071
3072 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3073 insert it. Other NULL-named decls, not so much. */
3074 tree name = DECL_NAME (decl);
3075 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
3076 {
3077 cxx_binding *binding = NULL; /* Local scope binding. */
3078 tree ns = NULL_TREE; /* Searched namespace. */
3079 tree *slot = NULL; /* Binding slot in namespace. */
3080 tree old = NULL_TREE;
3081
3082 if (!hiding)
3083 /* We should never unknownly push an anticipated decl. */
3084 gcc_checking_assert (!((TREE_CODE (decl) == TYPE_DECL
3085 || TREE_CODE (decl) == FUNCTION_DECL
3086 || TREE_CODE (decl) == TEMPLATE_DECL)
3087 && DECL_LANG_SPECIFIC (decl)
3088 && DECL_ANTICIPATED (decl)));
3089
3090 if (level->kind == sk_namespace)
3091 {
3092 /* We look in the decl's namespace for an existing
3093 declaration, even though we push into the current
3094 namespace. */
3095 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3096 ? CP_DECL_CONTEXT (decl) : current_namespace);
3097 /* Create the binding, if this is current namespace, because
3098 that's where we'll be pushing anyway. */
3099 slot = find_namespace_slot (ns, name, ns == current_namespace);
3100 if (slot)
3101 old = MAYBE_STAT_DECL (*slot);
3102 }
3103 else
3104 {
3105 binding = find_local_binding (level, name);
3106 if (binding)
3107 old = binding->value;
3108 }
3109
3110 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3111 && DECL_EXTERNAL (decl))
3112 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3113
3114 if (old == error_mark_node)
3115 old = NULL_TREE;
3116
3117 for (ovl_iterator iter (old); iter; ++iter)
3118 if (iter.using_p ())
3119 ; /* Ignore using decls here. */
3120 else if (tree match
3121 = duplicate_decls (decl, *iter, hiding, iter.hidden_p ()))
3122 {
3123 if (match == error_mark_node)
3124 ;
3125 else if (TREE_CODE (match) == TYPE_DECL)
3126 /* The IDENTIFIER will have the type referring to the
3127 now-smashed TYPE_DECL, because ...? Reset it. */
3128 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3129 else if (iter.hidden_p () && !hiding)
3130 {
3131 /* Unhiding a previously hidden decl. */
3132 tree head = iter.reveal_node (old);
3133 if (head != old)
3134 {
3135 gcc_checking_assert (ns);
3136 if (STAT_HACK_P (*slot))
3137 STAT_DECL (*slot) = head;
3138 else
3139 *slot = head;
3140 }
3141 if (DECL_EXTERN_C_P (match))
3142 /* We need to check and register the decl now. */
3143 check_extern_c_conflict (match);
3144 }
3145 return match;
3146 }
3147
3148 /* We are pushing a new decl. */
3149
3150 /* Skip a hidden builtin we failed to match already. There can
3151 only be one. */
3152 if (old && anticipated_builtin_p (old))
3153 old = OVL_CHAIN (old);
3154
3155 check_template_shadow (decl);
3156
3157 if (DECL_DECLARES_FUNCTION_P (decl))
3158 {
3159 check_default_args (decl);
3160
3161 if (hiding)
3162 {
3163 if (level->kind != sk_namespace)
3164 {
3165 /* In a local class, a friend function declaration must
3166 find a matching decl in the innermost non-class scope.
3167 [class.friend/11] */
3168 error_at (DECL_SOURCE_LOCATION (decl),
3169 "friend declaration %qD in local class without "
3170 "prior local declaration", decl);
3171 /* Don't attempt to push it. */
3172 return error_mark_node;
3173 }
3174 /* Hide it from ordinary lookup. */
3175 DECL_ANTICIPATED (decl) = true;
3176 }
3177 }
3178
3179 if (level->kind != sk_namespace)
3180 {
3181 check_local_shadow (decl);
3182
3183 if (TREE_CODE (decl) == NAMESPACE_DECL)
3184 /* A local namespace alias. */
3185 set_identifier_type_value_with_scope (name, NULL_TREE, level);
3186
3187 if (!binding)
3188 binding = create_local_binding (level, name);
3189 }
3190 else if (!slot)
3191 {
3192 ns = current_namespace;
3193 slot = find_namespace_slot (ns, name, true);
3194 /* Update OLD to reflect the namespace we're going to be
3195 pushing into. */
3196 old = MAYBE_STAT_DECL (*slot);
3197 }
3198
3199 old = update_binding (level, binding, slot, old, decl, hiding);
3200
3201 if (old != decl)
3202 /* An existing decl matched, use it. */
3203 decl = old;
3204 else if (TREE_CODE (decl) == TYPE_DECL)
3205 {
3206 tree type = TREE_TYPE (decl);
3207
3208 if (type != error_mark_node)
3209 {
3210 if (TYPE_NAME (type) != decl)
3211 set_underlying_type (decl);
3212
3213 set_identifier_type_value_with_scope (name, decl, level);
3214 }
3215
3216 /* If this is a locally defined typedef in a function that
3217 is not a template instantation, record it to implement
3218 -Wunused-local-typedefs. */
3219 if (!instantiating_current_function_p ())
3220 record_locally_defined_typedef (decl);
3221 }
3222 else if (VAR_P (decl))
3223 maybe_register_incomplete_var (decl);
3224
3225 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3226 && DECL_EXTERN_C_P (decl))
3227 check_extern_c_conflict (decl);
3228 }
3229 else
3230 add_decl_to_level (level, decl);
3231
3232 return decl;
3233 }
3234
3235 /* Record a decl-node X as belonging to the current lexical scope.
3236 It's a friend if IS_FRIEND is true -- which affects exactly where
3237 we push it. */
3238
3239 tree
3240 pushdecl (tree x, bool hiding)
3241 {
3242 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3243 tree ret = do_pushdecl (x, hiding);
3244 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3245 return ret;
3246 }
3247
3248 /* Enter DECL into the symbol table, if that's appropriate. Returns
3249 DECL, or a modified version thereof. */
3250
3251 tree
3252 maybe_push_decl (tree decl)
3253 {
3254 tree type = TREE_TYPE (decl);
3255
3256 /* Add this decl to the current binding level, but not if it comes
3257 from another scope, e.g. a static member variable. TEM may equal
3258 DECL or it may be a previous decl of the same name. */
3259 if (decl == error_mark_node
3260 || (TREE_CODE (decl) != PARM_DECL
3261 && DECL_CONTEXT (decl) != NULL_TREE
3262 /* Definitions of namespace members outside their namespace are
3263 possible. */
3264 && !DECL_NAMESPACE_SCOPE_P (decl))
3265 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3266 || type == unknown_type_node
3267 /* The declaration of a template specialization does not affect
3268 the functions available for overload resolution, so we do not
3269 call pushdecl. */
3270 || (TREE_CODE (decl) == FUNCTION_DECL
3271 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3272 return decl;
3273 else
3274 return pushdecl (decl);
3275 }
3276
3277 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3278 binding level. If IS_USING is true, DECL got here through a
3279 using-declaration. */
3280
3281 static void
3282 push_local_binding (tree id, tree decl, bool is_using)
3283 {
3284 /* Skip over any local classes. This makes sense if we call
3285 push_local_binding with a friend decl of a local class. */
3286 cp_binding_level *b = innermost_nonclass_level ();
3287
3288 gcc_assert (b->kind != sk_namespace);
3289 if (find_local_binding (b, id))
3290 {
3291 /* Supplement the existing binding. */
3292 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3293 /* It didn't work. Something else must be bound at this
3294 level. Do not add DECL to the list of things to pop
3295 later. */
3296 return;
3297 }
3298 else
3299 /* Create a new binding. */
3300 push_binding (id, decl, b);
3301
3302 if (TREE_CODE (decl) == OVERLOAD || is_using)
3303 /* We must put the OVERLOAD or using into a TREE_LIST since we
3304 cannot use the decl's chain itself. */
3305 decl = build_tree_list (id, decl);
3306
3307 /* And put DECL on the list of things declared by the current
3308 binding level. */
3309 add_decl_to_level (b, decl);
3310 }
3311
3312 \f
3313 /* true means unconditionally make a BLOCK for the next level pushed. */
3314
3315 static bool keep_next_level_flag;
3316
3317 static int binding_depth = 0;
3318
3319 static void
3320 indent (int depth)
3321 {
3322 int i;
3323
3324 for (i = 0; i < depth * 2; i++)
3325 putc (' ', stderr);
3326 }
3327
3328 /* Return a string describing the kind of SCOPE we have. */
3329 static const char *
3330 cp_binding_level_descriptor (cp_binding_level *scope)
3331 {
3332 /* The order of this table must match the "scope_kind"
3333 enumerators. */
3334 static const char* scope_kind_names[] = {
3335 "block-scope",
3336 "cleanup-scope",
3337 "try-scope",
3338 "catch-scope",
3339 "for-scope",
3340 "function-parameter-scope",
3341 "class-scope",
3342 "namespace-scope",
3343 "template-parameter-scope",
3344 "template-explicit-spec-scope"
3345 };
3346 const scope_kind kind = scope->explicit_spec_p
3347 ? sk_template_spec : scope->kind;
3348
3349 return scope_kind_names[kind];
3350 }
3351
3352 /* Output a debugging information about SCOPE when performing
3353 ACTION at LINE. */
3354 static void
3355 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3356 {
3357 const char *desc = cp_binding_level_descriptor (scope);
3358 if (scope->this_entity)
3359 verbatim ("%s %<%s(%E)%> %p %d", action, desc,
3360 scope->this_entity, (void *) scope, line);
3361 else
3362 verbatim ("%s %s %p %d", action, desc, (void *) scope, line);
3363 }
3364
3365 /* A chain of binding_level structures awaiting reuse. */
3366
3367 static GTY((deletable)) cp_binding_level *free_binding_level;
3368
3369 /* Insert SCOPE as the innermost binding level. */
3370
3371 void
3372 push_binding_level (cp_binding_level *scope)
3373 {
3374 /* Add it to the front of currently active scopes stack. */
3375 scope->level_chain = current_binding_level;
3376 current_binding_level = scope;
3377 keep_next_level_flag = false;
3378
3379 if (ENABLE_SCOPE_CHECKING)
3380 {
3381 scope->binding_depth = binding_depth;
3382 indent (binding_depth);
3383 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3384 "push");
3385 binding_depth++;
3386 }
3387 }
3388
3389 /* Create a new KIND scope and make it the top of the active scopes stack.
3390 ENTITY is the scope of the associated C++ entity (namespace, class,
3391 function, C++0x enumeration); it is NULL otherwise. */
3392
3393 cp_binding_level *
3394 begin_scope (scope_kind kind, tree entity)
3395 {
3396 cp_binding_level *scope;
3397
3398 /* Reuse or create a struct for this binding level. */
3399 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3400 {
3401 scope = free_binding_level;
3402 free_binding_level = scope->level_chain;
3403 memset (scope, 0, sizeof (cp_binding_level));
3404 }
3405 else
3406 scope = ggc_cleared_alloc<cp_binding_level> ();
3407
3408 scope->this_entity = entity;
3409 scope->more_cleanups_ok = true;
3410 switch (kind)
3411 {
3412 case sk_cleanup:
3413 scope->keep = true;
3414 break;
3415
3416 case sk_template_spec:
3417 scope->explicit_spec_p = true;
3418 kind = sk_template_parms;
3419 /* Fall through. */
3420 case sk_template_parms:
3421 case sk_block:
3422 case sk_try:
3423 case sk_catch:
3424 case sk_for:
3425 case sk_cond:
3426 case sk_class:
3427 case sk_scoped_enum:
3428 case sk_function_parms:
3429 case sk_transaction:
3430 case sk_omp:
3431 scope->keep = keep_next_level_flag;
3432 break;
3433
3434 case sk_namespace:
3435 NAMESPACE_LEVEL (entity) = scope;
3436 break;
3437
3438 default:
3439 /* Should not happen. */
3440 gcc_unreachable ();
3441 break;
3442 }
3443 scope->kind = kind;
3444
3445 push_binding_level (scope);
3446
3447 return scope;
3448 }
3449
3450 /* We're about to leave current scope. Pop the top of the stack of
3451 currently active scopes. Return the enclosing scope, now active. */
3452
3453 cp_binding_level *
3454 leave_scope (void)
3455 {
3456 cp_binding_level *scope = current_binding_level;
3457
3458 if (scope->kind == sk_namespace && class_binding_level)
3459 current_binding_level = class_binding_level;
3460
3461 /* We cannot leave a scope, if there are none left. */
3462 if (NAMESPACE_LEVEL (global_namespace))
3463 gcc_assert (!global_scope_p (scope));
3464
3465 if (ENABLE_SCOPE_CHECKING)
3466 {
3467 indent (--binding_depth);
3468 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3469 "leave");
3470 }
3471
3472 /* Move one nesting level up. */
3473 current_binding_level = scope->level_chain;
3474
3475 /* Namespace-scopes are left most probably temporarily, not
3476 completely; they can be reopened later, e.g. in namespace-extension
3477 or any name binding activity that requires us to resume a
3478 namespace. For classes, we cache some binding levels. For other
3479 scopes, we just make the structure available for reuse. */
3480 if (scope->kind != sk_namespace
3481 && scope != previous_class_level)
3482 {
3483 scope->level_chain = free_binding_level;
3484 gcc_assert (!ENABLE_SCOPE_CHECKING
3485 || scope->binding_depth == binding_depth);
3486 free_binding_level = scope;
3487 }
3488
3489 if (scope->kind == sk_class)
3490 {
3491 /* Reset DEFINING_CLASS_P to allow for reuse of a
3492 class-defining scope in a non-defining context. */
3493 scope->defining_class_p = 0;
3494
3495 /* Find the innermost enclosing class scope, and reset
3496 CLASS_BINDING_LEVEL appropriately. */
3497 class_binding_level = NULL;
3498 for (scope = current_binding_level; scope; scope = scope->level_chain)
3499 if (scope->kind == sk_class)
3500 {
3501 class_binding_level = scope;
3502 break;
3503 }
3504 }
3505
3506 return current_binding_level;
3507 }
3508
3509 /* When we exit a toplevel class scope, we save its binding level so
3510 that we can restore it quickly. Here, we've entered some other
3511 class, so we must invalidate our cache. */
3512
3513 void
3514 invalidate_class_lookup_cache (void)
3515 {
3516 previous_class_level->level_chain = free_binding_level;
3517 free_binding_level = previous_class_level;
3518 previous_class_level = NULL;
3519 }
3520
3521 static void
3522 resume_scope (cp_binding_level* b)
3523 {
3524 /* Resuming binding levels is meant only for namespaces,
3525 and those cannot nest into classes. */
3526 gcc_assert (!class_binding_level);
3527 /* Also, resuming a non-directly nested namespace is a no-no. */
3528 gcc_assert (b->level_chain == current_binding_level);
3529 current_binding_level = b;
3530 if (ENABLE_SCOPE_CHECKING)
3531 {
3532 b->binding_depth = binding_depth;
3533 indent (binding_depth);
3534 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3535 binding_depth++;
3536 }
3537 }
3538
3539 /* Return the innermost binding level that is not for a class scope. */
3540
3541 static cp_binding_level *
3542 innermost_nonclass_level (void)
3543 {
3544 cp_binding_level *b;
3545
3546 b = current_binding_level;
3547 while (b->kind == sk_class)
3548 b = b->level_chain;
3549
3550 return b;
3551 }
3552
3553 /* We're defining an object of type TYPE. If it needs a cleanup, but
3554 we're not allowed to add any more objects with cleanups to the current
3555 scope, create a new binding level. */
3556
3557 void
3558 maybe_push_cleanup_level (tree type)
3559 {
3560 if (type != error_mark_node
3561 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3562 && current_binding_level->more_cleanups_ok == 0)
3563 {
3564 begin_scope (sk_cleanup, NULL);
3565 current_binding_level->statement_list = push_stmt_list ();
3566 }
3567 }
3568
3569 /* Return true if we are in the global binding level. */
3570
3571 bool
3572 global_bindings_p (void)
3573 {
3574 return global_scope_p (current_binding_level);
3575 }
3576
3577 /* True if we are currently in a toplevel binding level. This
3578 means either the global binding level or a namespace in a toplevel
3579 binding level. Since there are no non-toplevel namespace levels,
3580 this really means any namespace or template parameter level. We
3581 also include a class whose context is toplevel. */
3582
3583 bool
3584 toplevel_bindings_p (void)
3585 {
3586 cp_binding_level *b = innermost_nonclass_level ();
3587
3588 return b->kind == sk_namespace || b->kind == sk_template_parms;
3589 }
3590
3591 /* True if this is a namespace scope, or if we are defining a class
3592 which is itself at namespace scope, or whose enclosing class is
3593 such a class, etc. */
3594
3595 bool
3596 namespace_bindings_p (void)
3597 {
3598 cp_binding_level *b = innermost_nonclass_level ();
3599
3600 return b->kind == sk_namespace;
3601 }
3602
3603 /* True if the innermost non-class scope is a block scope. */
3604
3605 bool
3606 local_bindings_p (void)
3607 {
3608 cp_binding_level *b = innermost_nonclass_level ();
3609 return b->kind < sk_function_parms || b->kind == sk_omp;
3610 }
3611
3612 /* True if the current level needs to have a BLOCK made. */
3613
3614 bool
3615 kept_level_p (void)
3616 {
3617 return (current_binding_level->blocks != NULL_TREE
3618 || current_binding_level->keep
3619 || current_binding_level->kind == sk_cleanup
3620 || current_binding_level->names != NULL_TREE
3621 || current_binding_level->using_directives);
3622 }
3623
3624 /* Returns the kind of the innermost scope. */
3625
3626 scope_kind
3627 innermost_scope_kind (void)
3628 {
3629 return current_binding_level->kind;
3630 }
3631
3632 /* Returns true if this scope was created to store template parameters. */
3633
3634 bool
3635 template_parm_scope_p (void)
3636 {
3637 return innermost_scope_kind () == sk_template_parms;
3638 }
3639
3640 /* If KEEP is true, make a BLOCK node for the next binding level,
3641 unconditionally. Otherwise, use the normal logic to decide whether
3642 or not to create a BLOCK. */
3643
3644 void
3645 keep_next_level (bool keep)
3646 {
3647 keep_next_level_flag = keep;
3648 }
3649
3650 /* Return the list of declarations of the current local scope. */
3651
3652 tree
3653 get_local_decls (void)
3654 {
3655 gcc_assert (current_binding_level->kind != sk_namespace
3656 && current_binding_level->kind != sk_class);
3657 return current_binding_level->names;
3658 }
3659
3660 /* Return how many function prototypes we are currently nested inside. */
3661
3662 int
3663 function_parm_depth (void)
3664 {
3665 int level = 0;
3666 cp_binding_level *b;
3667
3668 for (b = current_binding_level;
3669 b->kind == sk_function_parms;
3670 b = b->level_chain)
3671 ++level;
3672
3673 return level;
3674 }
3675
3676 /* For debugging. */
3677 static int no_print_functions = 0;
3678 static int no_print_builtins = 0;
3679
3680 static void
3681 print_binding_level (cp_binding_level* lvl)
3682 {
3683 tree t;
3684 int i = 0, len;
3685 if (lvl->this_entity)
3686 print_node_brief (stderr, "entity=", lvl->this_entity, 1);
3687 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3688 if (lvl->more_cleanups_ok)
3689 fprintf (stderr, " more-cleanups-ok");
3690 if (lvl->have_cleanups)
3691 fprintf (stderr, " have-cleanups");
3692 fprintf (stderr, "\n");
3693 if (lvl->names)
3694 {
3695 fprintf (stderr, " names:\t");
3696 /* We can probably fit 3 names to a line? */
3697 for (t = lvl->names; t; t = TREE_CHAIN (t))
3698 {
3699 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3700 continue;
3701 if (no_print_builtins
3702 && (TREE_CODE (t) == TYPE_DECL)
3703 && DECL_IS_BUILTIN (t))
3704 continue;
3705
3706 /* Function decls tend to have longer names. */
3707 if (TREE_CODE (t) == FUNCTION_DECL)
3708 len = 3;
3709 else
3710 len = 2;
3711 i += len;
3712 if (i > 6)
3713 {
3714 fprintf (stderr, "\n\t");
3715 i = len;
3716 }
3717 print_node_brief (stderr, "", t, 0);
3718 if (t == error_mark_node)
3719 break;
3720 }
3721 if (i)
3722 fprintf (stderr, "\n");
3723 }
3724 if (vec_safe_length (lvl->class_shadowed))
3725 {
3726 size_t i;
3727 cp_class_binding *b;
3728 fprintf (stderr, " class-shadowed:");
3729 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3730 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3731 fprintf (stderr, "\n");
3732 }
3733 if (lvl->type_shadowed)
3734 {
3735 fprintf (stderr, " type-shadowed:");
3736 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3737 {
3738 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3739 }
3740 fprintf (stderr, "\n");
3741 }
3742 }
3743
3744 DEBUG_FUNCTION void
3745 debug (cp_binding_level &ref)
3746 {
3747 print_binding_level (&ref);
3748 }
3749
3750 DEBUG_FUNCTION void
3751 debug (cp_binding_level *ptr)
3752 {
3753 if (ptr)
3754 debug (*ptr);
3755 else
3756 fprintf (stderr, "<nil>\n");
3757 }
3758
3759 static void
3760 print_other_binding_stack (cp_binding_level *stack)
3761 {
3762 cp_binding_level *level;
3763 for (level = stack; !global_scope_p (level); level = level->level_chain)
3764 {
3765 fprintf (stderr, "binding level %p\n", (void *) level);
3766 print_binding_level (level);
3767 }
3768 }
3769
3770 DEBUG_FUNCTION void
3771 print_binding_stack (void)
3772 {
3773 cp_binding_level *b;
3774 fprintf (stderr, "current_binding_level=%p\n"
3775 "class_binding_level=%p\n"
3776 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3777 (void *) current_binding_level, (void *) class_binding_level,
3778 (void *) NAMESPACE_LEVEL (global_namespace));
3779 if (class_binding_level)
3780 {
3781 for (b = class_binding_level; b; b = b->level_chain)
3782 if (b == current_binding_level)
3783 break;
3784 if (b)
3785 b = class_binding_level;
3786 else
3787 b = current_binding_level;
3788 }
3789 else
3790 b = current_binding_level;
3791 print_other_binding_stack (b);
3792 fprintf (stderr, "global:\n");
3793 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3794 }
3795 \f
3796 /* Return the type associated with ID. */
3797
3798 static tree
3799 identifier_type_value_1 (tree id)
3800 {
3801 /* There is no type with that name, anywhere. */
3802 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3803 return NULL_TREE;
3804 /* This is not the type marker, but the real thing. */
3805 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3806 return REAL_IDENTIFIER_TYPE_VALUE (id);
3807 /* Have to search for it. It must be on the global level, now.
3808 Ask lookup_name not to return non-types. */
3809 id = lookup_name (id, LOOK_where::BLOCK_NAMESPACE, LOOK_want::TYPE);
3810 if (id)
3811 return TREE_TYPE (id);
3812 return NULL_TREE;
3813 }
3814
3815 /* Wrapper for identifier_type_value_1. */
3816
3817 tree
3818 identifier_type_value (tree id)
3819 {
3820 tree ret;
3821 timevar_start (TV_NAME_LOOKUP);
3822 ret = identifier_type_value_1 (id);
3823 timevar_stop (TV_NAME_LOOKUP);
3824 return ret;
3825 }
3826
3827 /* Push a definition of struct, union or enum tag named ID. into
3828 binding_level B. DECL is a TYPE_DECL for the type. DECL has
3829 already been pushed into its binding level. This is bookkeeping to
3830 find it easily. */
3831
3832 static void
3833 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3834 {
3835 tree type;
3836
3837 if (b->kind != sk_namespace)
3838 {
3839 /* Shadow the marker, not the real thing, so that the marker
3840 gets restored later. */
3841 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3842 b->type_shadowed = tree_cons (id, old_type_value, b->type_shadowed);
3843 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3844 TREE_TYPE (b->type_shadowed) = type;
3845 }
3846 else
3847 {
3848 gcc_assert (decl);
3849 if (CHECKING_P)
3850 {
3851 tree *slot = find_namespace_slot (current_namespace, id);
3852 gcc_checking_assert (slot
3853 && (decl == MAYBE_STAT_TYPE (*slot)
3854 || decl == MAYBE_STAT_DECL (*slot)));
3855 }
3856
3857 /* Store marker instead of real type. */
3858 type = global_type_node;
3859 }
3860
3861 SET_IDENTIFIER_TYPE_VALUE (id, type);
3862 }
3863
3864 /* As set_identifier_type_value_with_scope, but using
3865 current_binding_level. */
3866
3867 void
3868 set_identifier_type_value (tree id, tree decl)
3869 {
3870 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3871 }
3872
3873 /* Return the name for the constructor (or destructor) for the
3874 specified class. */
3875
3876 tree
3877 constructor_name (tree type)
3878 {
3879 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3880
3881 return decl ? DECL_NAME (decl) : NULL_TREE;
3882 }
3883
3884 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3885 which must be a class type. */
3886
3887 bool
3888 constructor_name_p (tree name, tree type)
3889 {
3890 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3891
3892 /* These don't have names. */
3893 if (TREE_CODE (type) == DECLTYPE_TYPE
3894 || TREE_CODE (type) == TYPEOF_TYPE)
3895 return false;
3896
3897 if (name && name == constructor_name (type))
3898 return true;
3899
3900 return false;
3901 }
3902
3903 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3904 caller to set DECL_CONTEXT properly.
3905
3906 Note that this must only be used when X will be the new innermost
3907 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3908 without checking to see if the current IDENTIFIER_BINDING comes from a
3909 closer binding level than LEVEL. */
3910
3911 static tree
3912 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool hiding = false)
3913 {
3914 cp_binding_level *b;
3915
3916 if (level->kind == sk_class)
3917 {
3918 gcc_checking_assert (!hiding);
3919 b = class_binding_level;
3920 class_binding_level = level;
3921 pushdecl_class_level (x);
3922 class_binding_level = b;
3923 }
3924 else
3925 {
3926 tree function_decl = current_function_decl;
3927 if (level->kind == sk_namespace)
3928 current_function_decl = NULL_TREE;
3929 b = current_binding_level;
3930 current_binding_level = level;
3931 x = do_pushdecl (x, hiding);
3932 current_binding_level = b;
3933 current_function_decl = function_decl;
3934 }
3935 return x;
3936 }
3937
3938 /* Inject X into the local scope just before the function parms. */
3939
3940 tree
3941 pushdecl_outermost_localscope (tree x)
3942 {
3943 cp_binding_level *b = NULL;
3944 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3945
3946 /* Find the scope just inside the function parms. */
3947 for (cp_binding_level *n = current_binding_level;
3948 n->kind != sk_function_parms; n = b->level_chain)
3949 b = n;
3950
3951 tree ret = b ? do_pushdecl_with_scope (x, b) : error_mark_node;
3952 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3953
3954 return ret;
3955 }
3956
3957 /* Process a local-scope or namespace-scope using declaration. LOOKUP
3958 is the result of qualified lookup (both value & type are
3959 significant). FN_SCOPE_P indicates if we're at function-scope (as
3960 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
3961 bindings, which are altered to reflect the newly brought in
3962 declarations. */
3963
3964 static bool
3965 do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p,
3966 tree *value_p, tree *type_p)
3967 {
3968 tree value = *value_p;
3969 tree type = *type_p;
3970 bool failed = false;
3971
3972 /* Shift the old and new bindings around so we're comparing class and
3973 enumeration names to each other. */
3974 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
3975 {
3976 type = value;
3977 value = NULL_TREE;
3978 }
3979
3980 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
3981 {
3982 lookup.type = lookup.value;
3983 lookup.value = NULL_TREE;
3984 }
3985
3986 if (!lookup.value)
3987 /* Nothing. */;
3988 else if (OVL_P (lookup.value) && (!value || OVL_P (value)))
3989 {
3990 for (lkp_iterator usings (lookup.value); usings; ++usings)
3991 {
3992 tree new_fn = *usings;
3993
3994 /* [namespace.udecl]
3995
3996 If a function declaration in namespace scope or block
3997 scope has the same name and the same parameter types as a
3998 function introduced by a using declaration the program is
3999 ill-formed. */
4000 bool found = false;
4001 for (ovl_iterator old (value); !found && old; ++old)
4002 {
4003 tree old_fn = *old;
4004
4005 if (new_fn == old_fn)
4006 {
4007 /* The function already exists in the current
4008 namespace. */
4009 found = true;
4010 break;
4011 }
4012 else if (old.using_p ())
4013 continue; /* This is a using decl. */
4014 else if (old.hidden_p () && DECL_BUILTIN_P (old_fn))
4015 continue; /* This is an anticipated builtin. */
4016 else if (!matching_fn_p (new_fn, old_fn))
4017 continue; /* Parameters do not match. */
4018 else if (decls_match (new_fn, old_fn))
4019 {
4020 /* Extern "C" in different namespaces. */
4021 found = true;
4022 break;
4023 }
4024 else
4025 {
4026 diagnose_name_conflict (new_fn, old_fn);
4027 failed = true;
4028 found = true;
4029 break;
4030 }
4031 }
4032
4033 if (!found)
4034 /* Unlike the decl-pushing case we don't drop anticipated
4035 builtins here. They don't cause a problem, and we'd
4036 like to match them with a future declaration. */
4037 value = ovl_insert (new_fn, value, true);
4038 }
4039 }
4040 else if (value
4041 /* Ignore anticipated builtins. */
4042 && !anticipated_builtin_p (value)
4043 && (fn_scope_p || !decls_match (lookup.value, value)))
4044 {
4045 diagnose_name_conflict (lookup.value, value);
4046 failed = true;
4047 }
4048 else
4049 value = lookup.value;
4050
4051 if (lookup.type && lookup.type != type)
4052 {
4053 if (type && !decls_match (lookup.type, type))
4054 {
4055 diagnose_name_conflict (lookup.type, type);
4056 failed = true;
4057 }
4058 else
4059 type = lookup.type;
4060 }
4061
4062 /* If value is empty, shift any class or enumeration name back. */
4063 if (!value)
4064 {
4065 value = type;
4066 type = NULL_TREE;
4067 }
4068 *value_p = value;
4069 *type_p = type;
4070
4071 return failed;
4072 }
4073
4074 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4075 Both are namespaces. */
4076
4077 bool
4078 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4079 {
4080 int depth = SCOPE_DEPTH (ancestor);
4081
4082 if (!depth && !inline_only)
4083 /* The global namespace encloses everything. */
4084 return true;
4085
4086 while (SCOPE_DEPTH (descendant) > depth
4087 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4088 descendant = CP_DECL_CONTEXT (descendant);
4089
4090 return ancestor == descendant;
4091 }
4092
4093 /* Returns true if ROOT (a non-alias namespace, class, or function)
4094 encloses CHILD. CHILD may be either a class type or a namespace
4095 (maybe alias). */
4096
4097 bool
4098 is_ancestor (tree root, tree child)
4099 {
4100 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL
4101 && !DECL_NAMESPACE_ALIAS (root))
4102 || TREE_CODE (root) == FUNCTION_DECL
4103 || CLASS_TYPE_P (root));
4104 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL
4105 || CLASS_TYPE_P (child));
4106
4107 /* The global namespace encloses everything. Early-out for the
4108 common case. */
4109 if (root == global_namespace)
4110 return true;
4111
4112 /* Search CHILD until we reach namespace scope. */
4113 while (TREE_CODE (child) != NAMESPACE_DECL)
4114 {
4115 /* If we've reached the ROOT, it encloses CHILD. */
4116 if (root == child)
4117 return true;
4118
4119 /* Go out one level. */
4120 if (TYPE_P (child))
4121 child = TYPE_NAME (child);
4122 child = CP_DECL_CONTEXT (child);
4123 }
4124
4125 if (TREE_CODE (root) != NAMESPACE_DECL)
4126 /* Failed to meet the non-namespace we were looking for. */
4127 return false;
4128
4129 if (tree alias = DECL_NAMESPACE_ALIAS (child))
4130 child = alias;
4131
4132 return is_nested_namespace (root, child);
4133 }
4134
4135 /* Enter the class or namespace scope indicated by T suitable for name
4136 lookup. T can be arbitrary scope, not necessary nested inside the
4137 current scope. Returns a non-null scope to pop iff pop_scope
4138 should be called later to exit this scope. */
4139
4140 tree
4141 push_scope (tree t)
4142 {
4143 if (TREE_CODE (t) == NAMESPACE_DECL)
4144 push_decl_namespace (t);
4145 else if (CLASS_TYPE_P (t))
4146 {
4147 if (!at_class_scope_p ()
4148 || !same_type_p (current_class_type, t))
4149 push_nested_class (t);
4150 else
4151 /* T is the same as the current scope. There is therefore no
4152 need to re-enter the scope. Since we are not actually
4153 pushing a new scope, our caller should not call
4154 pop_scope. */
4155 t = NULL_TREE;
4156 }
4157
4158 return t;
4159 }
4160
4161 /* Leave scope pushed by push_scope. */
4162
4163 void
4164 pop_scope (tree t)
4165 {
4166 if (t == NULL_TREE)
4167 return;
4168 if (TREE_CODE (t) == NAMESPACE_DECL)
4169 pop_decl_namespace ();
4170 else if CLASS_TYPE_P (t)
4171 pop_nested_class ();
4172 }
4173
4174 /* Subroutine of push_inner_scope. */
4175
4176 static void
4177 push_inner_scope_r (tree outer, tree inner)
4178 {
4179 tree prev;
4180
4181 if (outer == inner
4182 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4183 return;
4184
4185 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4186 if (outer != prev)
4187 push_inner_scope_r (outer, prev);
4188 if (TREE_CODE (inner) == NAMESPACE_DECL)
4189 {
4190 cp_binding_level *save_template_parm = 0;
4191 /* Temporary take out template parameter scopes. They are saved
4192 in reversed order in save_template_parm. */
4193 while (current_binding_level->kind == sk_template_parms)
4194 {
4195 cp_binding_level *b = current_binding_level;
4196 current_binding_level = b->level_chain;
4197 b->level_chain = save_template_parm;
4198 save_template_parm = b;
4199 }
4200
4201 resume_scope (NAMESPACE_LEVEL (inner));
4202 current_namespace = inner;
4203
4204 /* Restore template parameter scopes. */
4205 while (save_template_parm)
4206 {
4207 cp_binding_level *b = save_template_parm;
4208 save_template_parm = b->level_chain;
4209 b->level_chain = current_binding_level;
4210 current_binding_level = b;
4211 }
4212 }
4213 else
4214 pushclass (inner);
4215 }
4216
4217 /* Enter the scope INNER from current scope. INNER must be a scope
4218 nested inside current scope. This works with both name lookup and
4219 pushing name into scope. In case a template parameter scope is present,
4220 namespace is pushed under the template parameter scope according to
4221 name lookup rule in 14.6.1/6.
4222
4223 Return the former current scope suitable for pop_inner_scope. */
4224
4225 tree
4226 push_inner_scope (tree inner)
4227 {
4228 tree outer = current_scope ();
4229 if (!outer)
4230 outer = current_namespace;
4231
4232 push_inner_scope_r (outer, inner);
4233 return outer;
4234 }
4235
4236 /* Exit the current scope INNER back to scope OUTER. */
4237
4238 void
4239 pop_inner_scope (tree outer, tree inner)
4240 {
4241 if (outer == inner
4242 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4243 return;
4244
4245 while (outer != inner)
4246 {
4247 if (TREE_CODE (inner) == NAMESPACE_DECL)
4248 {
4249 cp_binding_level *save_template_parm = 0;
4250 /* Temporary take out template parameter scopes. They are saved
4251 in reversed order in save_template_parm. */
4252 while (current_binding_level->kind == sk_template_parms)
4253 {
4254 cp_binding_level *b = current_binding_level;
4255 current_binding_level = b->level_chain;
4256 b->level_chain = save_template_parm;
4257 save_template_parm = b;
4258 }
4259
4260 pop_namespace ();
4261
4262 /* Restore template parameter scopes. */
4263 while (save_template_parm)
4264 {
4265 cp_binding_level *b = save_template_parm;
4266 save_template_parm = b->level_chain;
4267 b->level_chain = current_binding_level;
4268 current_binding_level = b;
4269 }
4270 }
4271 else
4272 popclass ();
4273
4274 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4275 }
4276 }
4277 \f
4278 /* Do a pushlevel for class declarations. */
4279
4280 void
4281 pushlevel_class (void)
4282 {
4283 class_binding_level = begin_scope (sk_class, current_class_type);
4284 }
4285
4286 /* ...and a poplevel for class declarations. */
4287
4288 void
4289 poplevel_class (void)
4290 {
4291 cp_binding_level *level = class_binding_level;
4292 cp_class_binding *cb;
4293 size_t i;
4294 tree shadowed;
4295
4296 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4297 gcc_assert (level != 0);
4298
4299 /* If we're leaving a toplevel class, cache its binding level. */
4300 if (current_class_depth == 1)
4301 previous_class_level = level;
4302 for (shadowed = level->type_shadowed;
4303 shadowed;
4304 shadowed = TREE_CHAIN (shadowed))
4305 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4306
4307 /* Remove the bindings for all of the class-level declarations. */
4308 if (level->class_shadowed)
4309 {
4310 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4311 {
4312 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4313 cxx_binding_free (cb->base);
4314 }
4315 ggc_free (level->class_shadowed);
4316 level->class_shadowed = NULL;
4317 }
4318
4319 /* Now, pop out of the binding level which we created up in the
4320 `pushlevel_class' routine. */
4321 gcc_assert (current_binding_level == level);
4322 leave_scope ();
4323 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4324 }
4325
4326 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4327 appropriate. DECL is the value to which a name has just been
4328 bound. CLASS_TYPE is the class in which the lookup occurred. */
4329
4330 static void
4331 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4332 tree class_type)
4333 {
4334 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4335 {
4336 tree context;
4337
4338 if (TREE_CODE (decl) == OVERLOAD)
4339 context = ovl_scope (decl);
4340 else
4341 {
4342 gcc_assert (DECL_P (decl));
4343 context = context_for_name_lookup (decl);
4344 }
4345
4346 if (is_properly_derived_from (class_type, context))
4347 INHERITED_VALUE_BINDING_P (binding) = 1;
4348 else
4349 INHERITED_VALUE_BINDING_P (binding) = 0;
4350 }
4351 else if (binding->value == decl)
4352 /* We only encounter a TREE_LIST when there is an ambiguity in the
4353 base classes. Such an ambiguity can be overridden by a
4354 definition in this class. */
4355 INHERITED_VALUE_BINDING_P (binding) = 1;
4356 else
4357 INHERITED_VALUE_BINDING_P (binding) = 0;
4358 }
4359
4360 /* Make the declaration of X appear in CLASS scope. */
4361
4362 bool
4363 pushdecl_class_level (tree x)
4364 {
4365 bool is_valid = true;
4366 bool subtime;
4367
4368 /* Do nothing if we're adding to an outer lambda closure type,
4369 outer_binding will add it later if it's needed. */
4370 if (current_class_type != class_binding_level->this_entity)
4371 return true;
4372
4373 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4374 /* Get the name of X. */
4375 tree name = OVL_NAME (x);
4376
4377 if (name)
4378 {
4379 is_valid = push_class_level_binding (name, x);
4380 if (TREE_CODE (x) == TYPE_DECL)
4381 set_identifier_type_value (name, x);
4382 }
4383 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4384 {
4385 /* If X is an anonymous aggregate, all of its members are
4386 treated as if they were members of the class containing the
4387 aggregate, for naming purposes. */
4388 location_t save_location = input_location;
4389 tree anon = TREE_TYPE (x);
4390 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4391 for (unsigned ix = member_vec->length (); ix--;)
4392 {
4393 tree binding = (*member_vec)[ix];
4394 if (STAT_HACK_P (binding))
4395 {
4396 if (!pushdecl_class_level (STAT_TYPE (binding)))
4397 is_valid = false;
4398 binding = STAT_DECL (binding);
4399 }
4400 if (!pushdecl_class_level (binding))
4401 is_valid = false;
4402 }
4403 else
4404 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4405 if (TREE_CODE (f) == FIELD_DECL)
4406 {
4407 input_location = DECL_SOURCE_LOCATION (f);
4408 if (!pushdecl_class_level (f))
4409 is_valid = false;
4410 }
4411 input_location = save_location;
4412 }
4413 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4414 return is_valid;
4415 }
4416
4417 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4418 scope. If the value returned is non-NULL, and the PREVIOUS field
4419 is not set, callers must set the PREVIOUS field explicitly. */
4420
4421 static cxx_binding *
4422 get_class_binding (tree name, cp_binding_level *scope)
4423 {
4424 tree class_type;
4425 tree type_binding;
4426 tree value_binding;
4427 cxx_binding *binding;
4428
4429 class_type = scope->this_entity;
4430
4431 /* Get the type binding. */
4432 type_binding = lookup_member (class_type, name,
4433 /*protect=*/2, /*want_type=*/true,
4434 tf_warning_or_error);
4435 /* Get the value binding. */
4436 value_binding = lookup_member (class_type, name,
4437 /*protect=*/2, /*want_type=*/false,
4438 tf_warning_or_error);
4439
4440 if (value_binding
4441 && (TREE_CODE (value_binding) == TYPE_DECL
4442 || DECL_CLASS_TEMPLATE_P (value_binding)
4443 || (TREE_CODE (value_binding) == TREE_LIST
4444 && TREE_TYPE (value_binding) == error_mark_node
4445 && (TREE_CODE (TREE_VALUE (value_binding))
4446 == TYPE_DECL))))
4447 /* We found a type binding, even when looking for a non-type
4448 binding. This means that we already processed this binding
4449 above. */
4450 ;
4451 else if (value_binding)
4452 {
4453 if (TREE_CODE (value_binding) == TREE_LIST
4454 && TREE_TYPE (value_binding) == error_mark_node)
4455 /* NAME is ambiguous. */
4456 ;
4457 else if (BASELINK_P (value_binding))
4458 /* NAME is some overloaded functions. */
4459 value_binding = BASELINK_FUNCTIONS (value_binding);
4460 }
4461
4462 /* If we found either a type binding or a value binding, create a
4463 new binding object. */
4464 if (type_binding || value_binding)
4465 {
4466 binding = new_class_binding (name,
4467 value_binding,
4468 type_binding,
4469 scope);
4470 set_inherited_value_binding_p (binding, value_binding, class_type);
4471 }
4472 else
4473 binding = NULL;
4474
4475 return binding;
4476 }
4477
4478 /* Make the declaration(s) of X appear in CLASS scope under the name
4479 NAME. Returns true if the binding is valid. */
4480
4481 static bool
4482 push_class_level_binding_1 (tree name, tree x)
4483 {
4484 cxx_binding *binding;
4485 tree decl = x;
4486 bool ok;
4487
4488 /* The class_binding_level will be NULL if x is a template
4489 parameter name in a member template. */
4490 if (!class_binding_level)
4491 return true;
4492
4493 if (name == error_mark_node)
4494 return false;
4495
4496 /* Can happen for an erroneous declaration (c++/60384). */
4497 if (!identifier_p (name))
4498 {
4499 gcc_assert (errorcount || sorrycount);
4500 return false;
4501 }
4502
4503 /* Check for invalid member names. But don't worry about a default
4504 argument-scope lambda being pushed after the class is complete. */
4505 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4506 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4507 /* Check that we're pushing into the right binding level. */
4508 gcc_assert (current_class_type == class_binding_level->this_entity);
4509
4510 /* We could have been passed a tree list if this is an ambiguous
4511 declaration. If so, pull the declaration out because
4512 check_template_shadow will not handle a TREE_LIST. */
4513 if (TREE_CODE (decl) == TREE_LIST
4514 && TREE_TYPE (decl) == error_mark_node)
4515 decl = TREE_VALUE (decl);
4516
4517 if (!check_template_shadow (decl))
4518 return false;
4519
4520 /* [class.mem]
4521
4522 If T is the name of a class, then each of the following shall
4523 have a name different from T:
4524
4525 -- every static data member of class T;
4526
4527 -- every member of class T that is itself a type;
4528
4529 -- every enumerator of every member of class T that is an
4530 enumerated type;
4531
4532 -- every member of every anonymous union that is a member of
4533 class T.
4534
4535 (Non-static data members were also forbidden to have the same
4536 name as T until TC1.) */
4537 if ((VAR_P (x)
4538 || TREE_CODE (x) == CONST_DECL
4539 || (TREE_CODE (x) == TYPE_DECL
4540 && !DECL_SELF_REFERENCE_P (x))
4541 /* A data member of an anonymous union. */
4542 || (TREE_CODE (x) == FIELD_DECL
4543 && DECL_CONTEXT (x) != current_class_type))
4544 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4545 {
4546 tree scope = context_for_name_lookup (x);
4547 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4548 {
4549 error_at (DECL_SOURCE_LOCATION (x),
4550 "%qD has the same name as the class in which it is "
4551 "declared", x);
4552 return false;
4553 }
4554 }
4555
4556 /* Get the current binding for NAME in this class, if any. */
4557 binding = IDENTIFIER_BINDING (name);
4558 if (!binding || binding->scope != class_binding_level)
4559 {
4560 binding = get_class_binding (name, class_binding_level);
4561 /* If a new binding was created, put it at the front of the
4562 IDENTIFIER_BINDING list. */
4563 if (binding)
4564 {
4565 binding->previous = IDENTIFIER_BINDING (name);
4566 IDENTIFIER_BINDING (name) = binding;
4567 }
4568 }
4569
4570 /* If there is already a binding, then we may need to update the
4571 current value. */
4572 if (binding && binding->value)
4573 {
4574 tree bval = binding->value;
4575 tree old_decl = NULL_TREE;
4576 tree target_decl = strip_using_decl (decl);
4577 tree target_bval = strip_using_decl (bval);
4578
4579 if (INHERITED_VALUE_BINDING_P (binding))
4580 {
4581 /* If the old binding was from a base class, and was for a
4582 tag name, slide it over to make room for the new binding.
4583 The old binding is still visible if explicitly qualified
4584 with a class-key. */
4585 if (TREE_CODE (target_bval) == TYPE_DECL
4586 && DECL_ARTIFICIAL (target_bval)
4587 && !(TREE_CODE (target_decl) == TYPE_DECL
4588 && DECL_ARTIFICIAL (target_decl)))
4589 {
4590 old_decl = binding->type;
4591 binding->type = bval;
4592 binding->value = NULL_TREE;
4593 INHERITED_VALUE_BINDING_P (binding) = 0;
4594 }
4595 else
4596 {
4597 old_decl = bval;
4598 /* Any inherited type declaration is hidden by the type
4599 declaration in the derived class. */
4600 if (TREE_CODE (target_decl) == TYPE_DECL
4601 && DECL_ARTIFICIAL (target_decl))
4602 binding->type = NULL_TREE;
4603 }
4604 }
4605 else if (TREE_CODE (decl) == USING_DECL
4606 && TREE_CODE (bval) == USING_DECL
4607 && same_type_p (USING_DECL_SCOPE (decl),
4608 USING_DECL_SCOPE (bval)))
4609 /* This is a using redeclaration that will be diagnosed later
4610 in supplement_binding */
4611 ;
4612 else if (TREE_CODE (decl) == USING_DECL
4613 && TREE_CODE (bval) == USING_DECL
4614 && DECL_DEPENDENT_P (decl)
4615 && DECL_DEPENDENT_P (bval))
4616 return true;
4617 else if (TREE_CODE (decl) == USING_DECL
4618 && OVL_P (target_bval))
4619 old_decl = bval;
4620 else if (TREE_CODE (bval) == USING_DECL
4621 && OVL_P (target_decl))
4622 return true;
4623 else if (OVL_P (target_decl)
4624 && OVL_P (target_bval))
4625 old_decl = bval;
4626
4627 if (old_decl && binding->scope == class_binding_level)
4628 {
4629 binding->value = x;
4630 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4631 here. This function is only used to register bindings
4632 from with the class definition itself. */
4633 INHERITED_VALUE_BINDING_P (binding) = 0;
4634 return true;
4635 }
4636 }
4637
4638 /* Note that we declared this value so that we can issue an error if
4639 this is an invalid redeclaration of a name already used for some
4640 other purpose. */
4641 note_name_declared_in_class (name, decl);
4642
4643 /* If we didn't replace an existing binding, put the binding on the
4644 stack of bindings for the identifier, and update the shadowed
4645 list. */
4646 if (binding && binding->scope == class_binding_level)
4647 /* Supplement the existing binding. */
4648 ok = supplement_binding (binding, decl);
4649 else
4650 {
4651 /* Create a new binding. */
4652 push_binding (name, decl, class_binding_level);
4653 ok = true;
4654 }
4655
4656 return ok;
4657 }
4658
4659 /* Wrapper for push_class_level_binding_1. */
4660
4661 bool
4662 push_class_level_binding (tree name, tree x)
4663 {
4664 bool ret;
4665 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4666 ret = push_class_level_binding_1 (name, x);
4667 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4668 return ret;
4669 }
4670
4671 /* Process and lookup a using decl SCOPE::lookup.name, filling in
4672 lookup.values & lookup.type. Return true if ok. */
4673
4674 static bool
4675 lookup_using_decl (tree scope, name_lookup &lookup)
4676 {
4677 tree current = current_scope ();
4678 bool dependent_p = false;
4679
4680 if (TREE_CODE (scope) == NAMESPACE_DECL)
4681 {
4682 /* Naming a namespace member. */
4683 if (TYPE_P (current))
4684 {
4685 error ("using-declaration for non-member at class scope");
4686 return false;
4687 }
4688
4689 qualified_namespace_lookup (scope, &lookup);
4690 }
4691 else if (TREE_CODE (scope) == ENUMERAL_TYPE)
4692 {
4693 error ("using-declaration may not name enumerator %<%E::%D%>",
4694 scope, lookup.name);
4695 return false;
4696 }
4697 else
4698 {
4699 /* Naming a class member. */
4700 if (!TYPE_P (current))
4701 {
4702 error ("using-declaration for member at non-class scope");
4703 return false;
4704 }
4705
4706 /* Make sure the name is not invalid */
4707 if (TREE_CODE (lookup.name) == BIT_NOT_EXPR)
4708 {
4709 error ("%<%T::%D%> names destructor", scope, lookup.name);
4710 return false;
4711 }
4712
4713 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4714 if (MAYBE_CLASS_TYPE_P (scope)
4715 && (lookup.name == TYPE_IDENTIFIER (scope)
4716 || constructor_name_p (lookup.name, scope)))
4717 {
4718 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4719 lookup.name = ctor_identifier;
4720 CLASSTYPE_NON_AGGREGATE (current) = true;
4721 }
4722
4723 /* Cannot introduce a constructor name. */
4724 if (constructor_name_p (lookup.name, current))
4725 {
4726 error ("%<%T::%D%> names constructor in %qT",
4727 scope, lookup.name, current);
4728 return false;
4729 }
4730
4731 /* Member using decls finish processing when completing the
4732 class. */
4733 /* From [namespace.udecl]:
4734
4735 A using-declaration used as a member-declaration shall refer
4736 to a member of a base class of the class being defined.
4737
4738 In general, we cannot check this constraint in a template
4739 because we do not know the entire set of base classes of the
4740 current class type. Morover, if SCOPE is dependent, it might
4741 match a non-dependent base. */
4742
4743 dependent_p = dependent_scope_p (scope);
4744 if (!dependent_p)
4745 {
4746 base_kind b_kind;
4747 tree binfo = lookup_base (current, scope, ba_any, &b_kind,
4748 tf_warning_or_error);
4749 if (b_kind < bk_proper_base)
4750 {
4751 /* If there are dependent bases, scope might resolve at
4752 instantiation time, even if it isn't exactly one of
4753 the dependent bases. */
4754 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4755 {
4756 error_not_base_type (scope, current);
4757 return false;
4758 }
4759 /* Treat as-if dependent. */
4760 dependent_p = true;
4761 }
4762 else if (lookup.name == ctor_identifier && !binfo_direct_p (binfo))
4763 {
4764 error ("cannot inherit constructors from indirect base %qT",
4765 scope);
4766 return false;
4767 }
4768 else if (IDENTIFIER_CONV_OP_P (lookup.name)
4769 && dependent_type_p (TREE_TYPE (lookup.name)))
4770 dependent_p = true;
4771 else
4772 lookup.value = lookup_member (binfo, lookup.name, 0,
4773 false, tf_warning_or_error);
4774 }
4775 }
4776
4777 if (!dependent_p)
4778 {
4779 if (!lookup.value)
4780 {
4781 error ("%qD has not been declared in %qE", lookup.name, scope);
4782 return false;
4783 }
4784
4785 if (TREE_CODE (lookup.value) == TREE_LIST
4786 /* We can (independently) have ambiguous implicit typedefs. */
4787 || (lookup.type && TREE_CODE (lookup.type) == TREE_LIST))
4788 {
4789 error ("reference to %qD is ambiguous", lookup.name);
4790 print_candidates (TREE_CODE (lookup.value) == TREE_LIST
4791 ? lookup.value : lookup.type);
4792 return false;
4793 }
4794
4795 if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
4796 {
4797 error ("using-declaration may not name namespace %qD", lookup.value);
4798 return false;
4799 }
4800 }
4801
4802 return true;
4803 }
4804
4805 /* Process "using SCOPE::NAME" in a class scope. Return the
4806 USING_DECL created. */
4807
4808 tree
4809 do_class_using_decl (tree scope, tree name)
4810 {
4811 if (name == error_mark_node
4812 || scope == error_mark_node)
4813 return NULL_TREE;
4814
4815 name_lookup lookup (name);
4816 if (!lookup_using_decl (scope, lookup))
4817 return NULL_TREE;
4818
4819 tree found = lookup.value;
4820 if (found && BASELINK_P (found))
4821 /* The binfo from which the functions came does not matter. */
4822 found = BASELINK_FUNCTIONS (found);
4823
4824 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
4825 USING_DECL_SCOPE (using_decl) = scope;
4826 USING_DECL_DECLS (using_decl) = found;
4827 DECL_DEPENDENT_P (using_decl) = !found;
4828
4829 return using_decl;
4830 }
4831
4832 \f
4833 /* Return the binding for NAME in NS. If NS is NULL, look in
4834 global_namespace. */
4835
4836 tree
4837 get_namespace_binding (tree ns, tree name)
4838 {
4839 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4840 if (!ns)
4841 ns = global_namespace;
4842 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4843 tree ret = find_namespace_value (ns, name);
4844 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4845 return ret;
4846 }
4847
4848 /* Push internal DECL into the global namespace. Does not do the
4849 full overload fn handling and does not add it to the list of things
4850 in the namespace. */
4851
4852 void
4853 set_global_binding (tree decl)
4854 {
4855 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4856
4857 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4858
4859 if (*slot)
4860 /* The user's placed something in the implementor's namespace. */
4861 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4862
4863 /* Force the binding, so compiler internals continue to work. */
4864 *slot = decl;
4865
4866 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4867 }
4868
4869 /* Set the context of a declaration to scope. Complain if we are not
4870 outside scope. */
4871
4872 void
4873 set_decl_namespace (tree decl, tree scope, bool friendp)
4874 {
4875 /* Get rid of namespace aliases. */
4876 scope = ORIGINAL_NAMESPACE (scope);
4877
4878 /* It is ok for friends to be qualified in parallel space. */
4879 if (!friendp && !is_nested_namespace (current_namespace, scope))
4880 error ("declaration of %qD not in a namespace surrounding %qD",
4881 decl, scope);
4882 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4883
4884 /* See whether this has been declared in the namespace or inline
4885 children. */
4886 tree old = NULL_TREE;
4887 {
4888 name_lookup lookup (DECL_NAME (decl),
4889 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
4890 if (!lookup.search_qualified (scope, /*usings=*/false))
4891 /* No old declaration at all. */
4892 goto not_found;
4893 old = lookup.value;
4894 }
4895
4896 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4897 if (TREE_CODE (old) == TREE_LIST)
4898 {
4899 ambiguous:
4900 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4901 error ("reference to %qD is ambiguous", decl);
4902 print_candidates (old);
4903 return;
4904 }
4905
4906 if (!DECL_DECLARES_FUNCTION_P (decl))
4907 {
4908 /* Don't compare non-function decls with decls_match here, since
4909 it can't check for the correct constness at this
4910 point. pushdecl will find those errors later. */
4911
4912 /* We might have found it in an inline namespace child of SCOPE. */
4913 if (TREE_CODE (decl) == TREE_CODE (old))
4914 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4915
4916 found:
4917 /* Writing "N::i" to declare something directly in "N" is invalid. */
4918 if (CP_DECL_CONTEXT (decl) == current_namespace
4919 && at_namespace_scope_p ())
4920 error_at (DECL_SOURCE_LOCATION (decl),
4921 "explicit qualification in declaration of %qD", decl);
4922 return;
4923 }
4924
4925 /* Since decl is a function, old should contain a function decl. */
4926 if (!OVL_P (old))
4927 {
4928 not_found:
4929 /* It didn't work, go back to the explicit scope. */
4930 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4931 error ("%qD should have been declared inside %qD", decl, scope);
4932
4933 return;
4934 }
4935
4936 /* We handle these in check_explicit_instantiation_namespace. */
4937 if (processing_explicit_instantiation)
4938 return;
4939 if (processing_template_decl || processing_specialization)
4940 /* We have not yet called push_template_decl to turn a
4941 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4942 match. But, we'll check later, when we construct the
4943 template. */
4944 return;
4945
4946 /* Instantiations or specializations of templates may be declared as
4947 friends in any namespace. */
4948 if (friendp && DECL_USE_TEMPLATE (decl))
4949 return;
4950
4951 tree found = NULL_TREE;
4952 bool hidden_p = false;
4953
4954 for (lkp_iterator iter (old); iter; ++iter)
4955 {
4956 if (iter.using_p ())
4957 continue;
4958
4959 tree ofn = *iter;
4960
4961 /* Adjust DECL_CONTEXT first so decls_match will return true
4962 if DECL will match a declaration in an inline namespace. */
4963 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4964 if (decls_match (decl, ofn))
4965 {
4966 if (found)
4967 {
4968 /* We found more than one matching declaration. This
4969 can happen if we have two inline namespace children,
4970 each containing a suitable declaration. */
4971 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4972 goto ambiguous;
4973 }
4974 found = ofn;
4975 hidden_p = iter.hidden_p ();
4976 }
4977 }
4978
4979 if (found)
4980 {
4981 if (hidden_p)
4982 {
4983 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4984 "%qD has not been declared within %qD", decl, scope);
4985 inform (DECL_SOURCE_LOCATION (found),
4986 "only here as a %<friend%>");
4987 }
4988 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4989 goto found;
4990 }
4991
4992 goto not_found;
4993 }
4994
4995 /* Return the namespace where the current declaration is declared. */
4996
4997 tree
4998 current_decl_namespace (void)
4999 {
5000 tree result;
5001 /* If we have been pushed into a different namespace, use it. */
5002 if (!vec_safe_is_empty (decl_namespace_list))
5003 return decl_namespace_list->last ();
5004
5005 if (current_class_type)
5006 result = decl_namespace_context (current_class_type);
5007 else if (current_function_decl)
5008 result = decl_namespace_context (current_function_decl);
5009 else
5010 result = current_namespace;
5011 return result;
5012 }
5013
5014 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5015 attribute visibility is seen. */
5016
5017 bool
5018 handle_namespace_attrs (tree ns, tree attributes)
5019 {
5020 tree d;
5021 bool saw_vis = false;
5022
5023 if (attributes == error_mark_node)
5024 return false;
5025
5026 for (d = attributes; d; d = TREE_CHAIN (d))
5027 {
5028 tree name = get_attribute_name (d);
5029 tree args = TREE_VALUE (d);
5030
5031 if (is_attribute_p ("visibility", name))
5032 {
5033 /* attribute visibility is a property of the syntactic block
5034 rather than the namespace as a whole, so we don't touch the
5035 NAMESPACE_DECL at all. */
5036 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5037 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5038 {
5039 warning (OPT_Wattributes,
5040 "%qD attribute requires a single NTBS argument",
5041 name);
5042 continue;
5043 }
5044
5045 if (!TREE_PUBLIC (ns))
5046 warning (OPT_Wattributes,
5047 "%qD attribute is meaningless since members of the "
5048 "anonymous namespace get local symbols", name);
5049
5050 push_visibility (TREE_STRING_POINTER (x), 1);
5051 saw_vis = true;
5052 }
5053 else if (is_attribute_p ("abi_tag", name))
5054 {
5055 if (!DECL_NAME (ns))
5056 {
5057 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5058 "namespace", name);
5059 continue;
5060 }
5061 if (!DECL_NAMESPACE_INLINE_P (ns))
5062 {
5063 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5064 "namespace", name);
5065 continue;
5066 }
5067 if (!args)
5068 {
5069 tree dn = DECL_NAME (ns);
5070 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5071 IDENTIFIER_POINTER (dn));
5072 TREE_TYPE (args) = char_array_type_node;
5073 args = fix_string_type (args);
5074 args = build_tree_list (NULL_TREE, args);
5075 }
5076 if (check_abi_tag_args (args, name))
5077 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5078 DECL_ATTRIBUTES (ns));
5079 }
5080 else if (is_attribute_p ("deprecated", name))
5081 {
5082 if (!DECL_NAME (ns))
5083 {
5084 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5085 "namespace", name);
5086 continue;
5087 }
5088 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5089 {
5090 error ("deprecated message is not a string");
5091 continue;
5092 }
5093 TREE_DEPRECATED (ns) = 1;
5094 if (args)
5095 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5096 DECL_ATTRIBUTES (ns));
5097 }
5098 else
5099 {
5100 warning (OPT_Wattributes, "%qD attribute directive ignored",
5101 name);
5102 continue;
5103 }
5104 }
5105
5106 return saw_vis;
5107 }
5108
5109 /* Temporarily set the namespace for the current declaration. */
5110
5111 void
5112 push_decl_namespace (tree decl)
5113 {
5114 if (TREE_CODE (decl) != NAMESPACE_DECL)
5115 decl = decl_namespace_context (decl);
5116 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5117 }
5118
5119 /* [namespace.memdef]/2 */
5120
5121 void
5122 pop_decl_namespace (void)
5123 {
5124 decl_namespace_list->pop ();
5125 }
5126
5127 /* Process a namespace-alias declaration. */
5128
5129 void
5130 do_namespace_alias (tree alias, tree name_space)
5131 {
5132 if (name_space == error_mark_node)
5133 return;
5134
5135 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5136
5137 name_space = ORIGINAL_NAMESPACE (name_space);
5138
5139 /* Build the alias. */
5140 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5141 DECL_NAMESPACE_ALIAS (alias) = name_space;
5142 DECL_EXTERNAL (alias) = 1;
5143 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5144 pushdecl (alias);
5145
5146 /* Emit debug info for namespace alias. */
5147 if (!building_stmt_list_p ())
5148 (*debug_hooks->early_global_decl) (alias);
5149 }
5150
5151 /* Like pushdecl, only it places X in the current namespace,
5152 if appropriate. */
5153
5154 tree
5155 pushdecl_namespace_level (tree x, bool hiding)
5156 {
5157 cp_binding_level *b = current_binding_level;
5158 tree t;
5159
5160 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5161 t = do_pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), hiding);
5162
5163 /* Now, the type_shadowed stack may screw us. Munge it so it does
5164 what we want. */
5165 if (TREE_CODE (t) == TYPE_DECL)
5166 {
5167 tree name = DECL_NAME (t);
5168 tree newval;
5169 tree *ptr = (tree *)0;
5170 for (; !global_scope_p (b); b = b->level_chain)
5171 {
5172 tree shadowed = b->type_shadowed;
5173 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5174 if (TREE_PURPOSE (shadowed) == name)
5175 {
5176 ptr = &TREE_VALUE (shadowed);
5177 /* Can't break out of the loop here because sometimes
5178 a binding level will have duplicate bindings for
5179 PT names. It's gross, but I haven't time to fix it. */
5180 }
5181 }
5182 newval = TREE_TYPE (t);
5183 if (ptr == (tree *)0)
5184 {
5185 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5186 up here if this is changed to an assertion. --KR */
5187 SET_IDENTIFIER_TYPE_VALUE (name, t);
5188 }
5189 else
5190 {
5191 *ptr = newval;
5192 }
5193 }
5194 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5195 return t;
5196 }
5197
5198 /* Process a using declaration in non-class scope. */
5199
5200 void
5201 finish_nonmember_using_decl (tree scope, tree name)
5202 {
5203 gcc_checking_assert (current_binding_level->kind != sk_class);
5204
5205 if (scope == error_mark_node || name == error_mark_node)
5206 return;
5207
5208 name_lookup lookup (name);
5209
5210 if (!lookup_using_decl (scope, lookup))
5211 return;
5212
5213 /* Emit debug info. */
5214 if (!processing_template_decl)
5215 cp_emit_debug_info_for_using (lookup.value,
5216 current_binding_level->this_entity);
5217
5218 if (current_binding_level->kind == sk_namespace)
5219 {
5220 tree *slot = find_namespace_slot (current_namespace, name, true);
5221
5222 tree value = MAYBE_STAT_DECL (*slot);
5223 tree type = MAYBE_STAT_TYPE (*slot);
5224
5225 do_nonmember_using_decl (lookup, false, &value, &type);
5226
5227 if (STAT_HACK_P (*slot))
5228 {
5229 STAT_DECL (*slot) = value;
5230 STAT_TYPE (*slot) = type;
5231 }
5232 else if (type)
5233 *slot = stat_hack (value, type);
5234 else
5235 *slot = value;
5236 }
5237 else
5238 {
5239 tree using_decl = build_lang_decl (USING_DECL, lookup.name, NULL_TREE);
5240 USING_DECL_SCOPE (using_decl) = scope;
5241 add_decl_expr (using_decl);
5242
5243 cxx_binding *binding = find_local_binding (current_binding_level, name);
5244 tree value = NULL;
5245 tree type = NULL;
5246 if (binding)
5247 {
5248 value = binding->value;
5249 type = binding->type;
5250 }
5251
5252 /* DR 36 questions why using-decls at function scope may not be
5253 duplicates. Disallow it, as C++11 claimed and PR 20420
5254 implemented. */
5255 do_nonmember_using_decl (lookup, true, &value, &type);
5256
5257 if (!value)
5258 ;
5259 else if (binding && value == binding->value)
5260 ;
5261 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5262 {
5263 update_local_overload (IDENTIFIER_BINDING (name), value);
5264 IDENTIFIER_BINDING (name)->value = value;
5265 }
5266 else
5267 /* Install the new binding. */
5268 push_local_binding (name, value, true);
5269
5270 if (!type)
5271 ;
5272 else if (binding && type == binding->type)
5273 ;
5274 else
5275 {
5276 push_local_binding (name, type, true);
5277 set_identifier_type_value (name, type);
5278 }
5279 }
5280 }
5281
5282 /* Return the declarations that are members of the namespace NS. */
5283
5284 tree
5285 cp_namespace_decls (tree ns)
5286 {
5287 return NAMESPACE_LEVEL (ns)->names;
5288 }
5289
5290 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5291 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
5292
5293 static bool
5294 qualify_lookup (tree val, LOOK_want want)
5295 {
5296 if (val == NULL_TREE)
5297 return false;
5298
5299 if (bool (want & LOOK_want::TYPE))
5300 {
5301 tree target_val = strip_using_decl (val);
5302
5303 if (TREE_CODE (STRIP_TEMPLATE (target_val)) == TYPE_DECL)
5304 return true;
5305 }
5306
5307 if (bool (want & LOOK_want::TYPE_NAMESPACE))
5308 return TREE_CODE (val) == NAMESPACE_DECL;
5309
5310 return true;
5311 }
5312
5313 /* Is there a "using namespace std;" directive within USINGS? */
5314
5315 static bool
5316 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5317 {
5318 if (!usings)
5319 return false;
5320
5321 for (unsigned ix = usings->length (); ix--;)
5322 if ((*usings)[ix] == std_node)
5323 return true;
5324
5325 return false;
5326 }
5327
5328 /* Is there a "using namespace std;" directive within the current
5329 namespace (or its ancestors)?
5330 Compare with name_lookup::search_unqualified. */
5331
5332 static bool
5333 has_using_namespace_std_directive_p ()
5334 {
5335 for (cp_binding_level *level = current_binding_level;
5336 level;
5337 level = level->level_chain)
5338 if (using_directives_contain_std_p (level->using_directives))
5339 return true;
5340
5341 return false;
5342 }
5343
5344 /* Subclass of deferred_diagnostic, for issuing a note when
5345 --param cxx-max-namespaces-for-diagnostic-help is reached.
5346
5347 The note should be issued after the error, but before any other
5348 deferred diagnostics. This is handled by decorating a wrapped
5349 deferred_diagnostic, and emitting a note before that wrapped note is
5350 deleted. */
5351
5352 class namespace_limit_reached : public deferred_diagnostic
5353 {
5354 public:
5355 namespace_limit_reached (location_t loc, unsigned limit, tree name,
5356 gnu::unique_ptr<deferred_diagnostic> wrapped)
5357 : deferred_diagnostic (loc),
5358 m_limit (limit), m_name (name),
5359 m_wrapped (move (wrapped))
5360 {
5361 }
5362
5363 ~namespace_limit_reached ()
5364 {
5365 /* Unconditionally warn that the search was truncated. */
5366 inform (get_location (),
5367 "maximum limit of %d namespaces searched for %qE",
5368 m_limit, m_name);
5369 /* m_wrapped will be implicitly deleted after this, emitting any followup
5370 diagnostic after the above note. */
5371 }
5372
5373 private:
5374 unsigned m_limit;
5375 tree m_name;
5376 gnu::unique_ptr<deferred_diagnostic> m_wrapped;
5377 };
5378
5379 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5380 Emit a note showing the location of the declaration of the suggestion. */
5381
5382 class show_candidate_location : public deferred_diagnostic
5383 {
5384 public:
5385 show_candidate_location (location_t loc, tree candidate)
5386 : deferred_diagnostic (loc),
5387 m_candidate (candidate)
5388 {
5389 }
5390
5391 ~show_candidate_location ()
5392 {
5393 inform (location_of (m_candidate), "%qE declared here", m_candidate);
5394 }
5395
5396 private:
5397 tree m_candidate;
5398 };
5399
5400 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5401 to be suggested by suggest_alternatives_for.
5402
5403 Emit a series of notes showing the various suggestions. */
5404
5405 class suggest_alternatives : public deferred_diagnostic
5406 {
5407 public:
5408 suggest_alternatives (location_t loc, vec<tree> candidates)
5409 : deferred_diagnostic (loc),
5410 m_candidates (candidates)
5411 {
5412 }
5413
5414 ~suggest_alternatives ()
5415 {
5416 if (m_candidates.length ())
5417 {
5418 inform_n (get_location (), m_candidates.length (),
5419 "suggested alternative:",
5420 "suggested alternatives:");
5421 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
5422 {
5423 tree val = m_candidates[ix];
5424
5425 inform (location_of (val), " %qE", val);
5426 }
5427 }
5428 m_candidates.release ();
5429 }
5430
5431 private:
5432 vec<tree> m_candidates;
5433 };
5434
5435 /* A class for encapsulating the result of a search across
5436 multiple namespaces (and scoped enums within them) for an
5437 unrecognized name seen at a given source location. */
5438
5439 class namespace_hints
5440 {
5441 public:
5442 namespace_hints (location_t loc, tree name);
5443
5444 name_hint convert_candidates_to_name_hint ();
5445 name_hint maybe_decorate_with_limit (name_hint);
5446
5447 private:
5448 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name);
5449
5450 location_t m_loc;
5451 tree m_name;
5452 vec<tree> m_candidates;
5453
5454 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5455 unsigned m_limit;
5456
5457 /* Was the limit reached? */
5458 bool m_limited;
5459 };
5460
5461 /* Constructor for namespace_hints. Search namespaces and scoped enums,
5462 looking for an exact match for unrecognized NAME seen at LOC. */
5463
5464 namespace_hints::namespace_hints (location_t loc, tree name)
5465 : m_loc(loc), m_name (name)
5466 {
5467 auto_vec<tree> worklist;
5468
5469 m_candidates = vNULL;
5470 m_limited = false;
5471 m_limit = param_cxx_max_namespaces_for_diagnostic_help;
5472
5473 /* Breadth-first search of namespaces. Up to limit namespaces
5474 searched (limit zero == unlimited). */
5475 worklist.safe_push (global_namespace);
5476 for (unsigned ix = 0; ix != worklist.length (); ix++)
5477 {
5478 tree ns = worklist[ix];
5479 name_lookup lookup (name);
5480
5481 if (lookup.search_qualified (ns, false))
5482 m_candidates.safe_push (lookup.value);
5483
5484 if (!m_limited)
5485 {
5486 /* Look for child namespaces. We have to do this
5487 indirectly because they are chained in reverse order,
5488 which is confusing to the user. */
5489 auto_vec<tree> children;
5490
5491 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5492 decl; decl = TREE_CHAIN (decl))
5493 {
5494 if (TREE_CODE (decl) == NAMESPACE_DECL
5495 && !DECL_NAMESPACE_ALIAS (decl)
5496 && !DECL_NAMESPACE_INLINE_P (decl))
5497 children.safe_push (decl);
5498
5499 /* Look for exact matches for NAME within scoped enums.
5500 These aren't added to the worklist, and so don't count
5501 against the search limit. */
5502 if (TREE_CODE (decl) == TYPE_DECL)
5503 {
5504 tree type = TREE_TYPE (decl);
5505 if (SCOPED_ENUM_P (type))
5506 maybe_add_candidate_for_scoped_enum (type, name);
5507 }
5508 }
5509
5510 while (!m_limited && !children.is_empty ())
5511 {
5512 if (worklist.length () == m_limit)
5513 m_limited = true;
5514 else
5515 worklist.safe_push (children.pop ());
5516 }
5517 }
5518 }
5519 }
5520
5521 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5522 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5523
5524 If m_candidates is non-empty, use it to generate a suggestion and/or
5525 a deferred diagnostic that lists the possible candidate(s).
5526 */
5527
5528 name_hint
5529 namespace_hints::convert_candidates_to_name_hint ()
5530 {
5531 /* How many candidates do we have? */
5532
5533 /* If we have just one candidate, issue a name_hint with it as a suggestion
5534 (so that consumers are able to suggest it within the error message and emit
5535 it as a fix-it hint), and with a note showing the candidate's location. */
5536 if (m_candidates.length () == 1)
5537 {
5538 tree candidate = m_candidates[0];
5539 /* Clean up CANDIDATES. */
5540 m_candidates.release ();
5541 return name_hint (expr_to_string (candidate),
5542 new show_candidate_location (m_loc, candidate));
5543 }
5544 else if (m_candidates.length () > 1)
5545 /* If we have more than one candidate, issue a name_hint without a single
5546 "suggestion", but with a deferred diagnostic that lists the
5547 various candidates. This takes ownership of m_candidates. */
5548 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
5549
5550 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5551 gcc_assert (m_candidates.length () == 0);
5552 gcc_assert (m_candidates == vNULL);
5553
5554 return name_hint ();
5555 }
5556
5557 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5558 then we want to emit a note about after the error, but before
5559 any other deferred diagnostics.
5560
5561 Handle this by figuring out what hint is needed, then optionally
5562 decorating HINT with a namespace_limit_reached wrapper. */
5563
5564 name_hint
5565 namespace_hints::maybe_decorate_with_limit (name_hint hint)
5566 {
5567 if (m_limited)
5568 return name_hint (hint.suggestion (),
5569 new namespace_limit_reached (m_loc, m_limit,
5570 m_name,
5571 hint.take_deferred ()));
5572 else
5573 return hint;
5574 }
5575
5576 /* Look inside SCOPED_ENUM for exact matches for NAME.
5577 If one is found, add its CONST_DECL to m_candidates. */
5578
5579 void
5580 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum,
5581 tree name)
5582 {
5583 gcc_assert (SCOPED_ENUM_P (scoped_enum));
5584
5585 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
5586 {
5587 tree id = TREE_PURPOSE (iter);
5588 if (id == name)
5589 {
5590 m_candidates.safe_push (TREE_VALUE (iter));
5591 return;
5592 }
5593 }
5594 }
5595
5596 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5597 name lookup failed.
5598
5599 Search through all available namespaces and any scoped enums within them
5600 and generate a suggestion and/or a deferred diagnostic that lists possible
5601 candidate(s).
5602
5603 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5604 look for near-matches and suggest the best near-match, if there is one.
5605
5606 If nothing is found, then an empty name_hint is returned. */
5607
5608 name_hint
5609 suggest_alternatives_for (location_t location, tree name,
5610 bool suggest_misspellings)
5611 {
5612 /* First, search for exact matches in other namespaces. */
5613 namespace_hints ns_hints (location, name);
5614 name_hint result = ns_hints.convert_candidates_to_name_hint ();
5615
5616 /* Otherwise, try other approaches. */
5617 if (!result)
5618 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
5619
5620 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
5621 }
5622
5623 /* The second half of suggest_alternatives_for, for when no exact matches
5624 were found in other namespaces. */
5625
5626 static name_hint
5627 suggest_alternatives_for_1 (location_t location, tree name,
5628 bool suggest_misspellings)
5629 {
5630 /* No candidates were found in the available namespaces. */
5631
5632 /* If there's a "using namespace std;" active, and this
5633 is one of the most common "std::" names, then it's probably a
5634 missing #include. */
5635 if (has_using_namespace_std_directive_p ())
5636 {
5637 name_hint hint = maybe_suggest_missing_std_header (location, name);
5638 if (hint)
5639 return hint;
5640 }
5641
5642 /* Otherwise, consider misspellings. */
5643 if (!suggest_misspellings)
5644 return name_hint ();
5645
5646 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
5647 }
5648
5649 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5650 name lookup failed.
5651
5652 Search through all available namespaces and generate a suggestion and/or
5653 a deferred diagnostic that lists possible candidate(s).
5654
5655 This is similiar to suggest_alternatives_for, but doesn't fallback to
5656 the other approaches used by that function. */
5657
5658 name_hint
5659 suggest_alternatives_in_other_namespaces (location_t location, tree name)
5660 {
5661 namespace_hints ns_hints (location, name);
5662
5663 name_hint result = ns_hints.convert_candidates_to_name_hint ();
5664
5665 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
5666 }
5667
5668 /* A well-known name within the C++ standard library, returned by
5669 get_std_name_hint. */
5670
5671 struct std_name_hint
5672 {
5673 /* A name within "std::". */
5674 const char *name;
5675
5676 /* The header name defining it within the C++ Standard Library
5677 (with '<' and '>'). */
5678 const char *header;
5679
5680 /* The dialect of C++ in which this was added. */
5681 enum cxx_dialect min_dialect;
5682 };
5683
5684 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5685 for some of the most common names within "std::".
5686 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5687
5688 static const std_name_hint *
5689 get_std_name_hint (const char *name)
5690 {
5691 static const std_name_hint hints[] = {
5692 /* <any>. */
5693 {"any", "<any>", cxx17},
5694 {"any_cast", "<any>", cxx17},
5695 {"make_any", "<any>", cxx17},
5696 /* <array>. */
5697 {"array", "<array>", cxx11},
5698 {"to_array", "<array>", cxx20},
5699 /* <atomic>. */
5700 {"atomic", "<atomic>", cxx11},
5701 {"atomic_flag", "<atomic>", cxx11},
5702 {"atomic_ref", "<atomic>", cxx20},
5703 /* <bitset>. */
5704 {"bitset", "<bitset>", cxx11},
5705 /* <compare> */
5706 {"weak_equality", "<compare>", cxx20},
5707 {"strong_equality", "<compare>", cxx20},
5708 {"partial_ordering", "<compare>", cxx20},
5709 {"weak_ordering", "<compare>", cxx20},
5710 {"strong_ordering", "<compare>", cxx20},
5711 /* <complex>. */
5712 {"complex", "<complex>", cxx98},
5713 {"complex_literals", "<complex>", cxx14},
5714 /* <condition_variable>. */
5715 {"condition_variable", "<condition_variable>", cxx11},
5716 {"condition_variable_any", "<condition_variable>", cxx11},
5717 /* <cstddef>. */
5718 {"byte", "<cstddef>", cxx17},
5719 /* <deque>. */
5720 {"deque", "<deque>", cxx98},
5721 /* <forward_list>. */
5722 {"forward_list", "<forward_list>", cxx11},
5723 /* <fstream>. */
5724 {"basic_filebuf", "<fstream>", cxx98},
5725 {"basic_ifstream", "<fstream>", cxx98},
5726 {"basic_ofstream", "<fstream>", cxx98},
5727 {"basic_fstream", "<fstream>", cxx98},
5728 {"fstream", "<fstream>", cxx98},
5729 {"ifstream", "<fstream>", cxx98},
5730 {"ofstream", "<fstream>", cxx98},
5731 /* <functional>. */
5732 {"bind", "<functional>", cxx11},
5733 {"bind_front", "<functional>", cxx20},
5734 {"function", "<functional>", cxx11},
5735 {"hash", "<functional>", cxx11},
5736 {"invoke", "<functional>", cxx17},
5737 {"mem_fn", "<functional>", cxx11},
5738 {"not_fn", "<functional>", cxx17},
5739 {"reference_wrapper", "<functional>", cxx11},
5740 {"unwrap_reference", "<functional>", cxx20},
5741 {"unwrap_reference_t", "<functional>", cxx20},
5742 {"unwrap_ref_decay", "<functional>", cxx20},
5743 {"unwrap_ref_decay_t", "<functional>", cxx20},
5744 /* <future>. */
5745 {"async", "<future>", cxx11},
5746 {"future", "<future>", cxx11},
5747 {"packaged_task", "<future>", cxx11},
5748 {"promise", "<future>", cxx11},
5749 /* <iostream>. */
5750 {"cin", "<iostream>", cxx98},
5751 {"cout", "<iostream>", cxx98},
5752 {"cerr", "<iostream>", cxx98},
5753 {"clog", "<iostream>", cxx98},
5754 {"wcin", "<iostream>", cxx98},
5755 {"wcout", "<iostream>", cxx98},
5756 {"wclog", "<iostream>", cxx98},
5757 /* <istream>. */
5758 {"istream", "<istream>", cxx98},
5759 /* <iterator>. */
5760 {"advance", "<iterator>", cxx98},
5761 {"back_inserter", "<iterator>", cxx98},
5762 {"begin", "<iterator>", cxx11},
5763 {"distance", "<iterator>", cxx98},
5764 {"end", "<iterator>", cxx11},
5765 {"front_inserter", "<iterator>", cxx98},
5766 {"inserter", "<iterator>", cxx98},
5767 {"istream_iterator", "<iterator>", cxx98},
5768 {"istreambuf_iterator", "<iterator>", cxx98},
5769 {"iterator_traits", "<iterator>", cxx98},
5770 {"move_iterator", "<iterator>", cxx11},
5771 {"next", "<iterator>", cxx11},
5772 {"ostream_iterator", "<iterator>", cxx98},
5773 {"ostreambuf_iterator", "<iterator>", cxx98},
5774 {"prev", "<iterator>", cxx11},
5775 {"reverse_iterator", "<iterator>", cxx98},
5776 /* <ostream>. */
5777 {"ostream", "<ostream>", cxx98},
5778 /* <list>. */
5779 {"list", "<list>", cxx98},
5780 /* <map>. */
5781 {"map", "<map>", cxx98},
5782 {"multimap", "<map>", cxx98},
5783 /* <memory>. */
5784 {"allocate_shared", "<memory>", cxx11},
5785 {"allocator", "<memory>", cxx98},
5786 {"allocator_traits", "<memory>", cxx11},
5787 {"make_shared", "<memory>", cxx11},
5788 {"make_unique", "<memory>", cxx14},
5789 {"shared_ptr", "<memory>", cxx11},
5790 {"unique_ptr", "<memory>", cxx11},
5791 {"weak_ptr", "<memory>", cxx11},
5792 /* <memory_resource>. */
5793 {"pmr", "<memory_resource>", cxx17},
5794 /* <mutex>. */
5795 {"mutex", "<mutex>", cxx11},
5796 {"timed_mutex", "<mutex>", cxx11},
5797 {"recursive_mutex", "<mutex>", cxx11},
5798 {"recursive_timed_mutex", "<mutex>", cxx11},
5799 {"once_flag", "<mutex>", cxx11},
5800 {"call_once,", "<mutex>", cxx11},
5801 {"lock", "<mutex>", cxx11},
5802 {"scoped_lock", "<mutex>", cxx17},
5803 {"try_lock", "<mutex>", cxx11},
5804 {"lock_guard", "<mutex>", cxx11},
5805 {"unique_lock", "<mutex>", cxx11},
5806 /* <optional>. */
5807 {"optional", "<optional>", cxx17},
5808 {"make_optional", "<optional>", cxx17},
5809 /* <ostream>. */
5810 {"ostream", "<ostream>", cxx98},
5811 {"wostream", "<ostream>", cxx98},
5812 {"ends", "<ostream>", cxx98},
5813 {"flush", "<ostream>", cxx98},
5814 {"endl", "<ostream>", cxx98},
5815 /* <queue>. */
5816 {"queue", "<queue>", cxx98},
5817 {"priority_queue", "<queue>", cxx98},
5818 /* <set>. */
5819 {"set", "<set>", cxx98},
5820 {"multiset", "<set>", cxx98},
5821 /* <shared_mutex>. */
5822 {"shared_lock", "<shared_mutex>", cxx14},
5823 {"shared_mutex", "<shared_mutex>", cxx17},
5824 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5825 /* <source_location>. */
5826 {"source_location", "<source_location>", cxx20},
5827 /* <sstream>. */
5828 {"basic_stringbuf", "<sstream>", cxx98},
5829 {"basic_istringstream", "<sstream>", cxx98},
5830 {"basic_ostringstream", "<sstream>", cxx98},
5831 {"basic_stringstream", "<sstream>", cxx98},
5832 {"istringstream", "<sstream>", cxx98},
5833 {"ostringstream", "<sstream>", cxx98},
5834 {"stringstream", "<sstream>", cxx98},
5835 /* <stack>. */
5836 {"stack", "<stack>", cxx98},
5837 /* <string>. */
5838 {"basic_string", "<string>", cxx98},
5839 {"string", "<string>", cxx98},
5840 {"wstring", "<string>", cxx98},
5841 {"u8string", "<string>", cxx20},
5842 {"u16string", "<string>", cxx11},
5843 {"u32string", "<string>", cxx11},
5844 /* <string_view>. */
5845 {"basic_string_view", "<string_view>", cxx17},
5846 {"string_view", "<string_view>", cxx17},
5847 /* <thread>. */
5848 {"thread", "<thread>", cxx11},
5849 {"this_thread", "<thread>", cxx11},
5850 /* <tuple>. */
5851 {"apply", "<tuple>", cxx17},
5852 {"forward_as_tuple", "<tuple>", cxx11},
5853 {"make_from_tuple", "<tuple>", cxx17},
5854 {"make_tuple", "<tuple>", cxx11},
5855 {"tie", "<tuple>", cxx11},
5856 {"tuple", "<tuple>", cxx11},
5857 {"tuple_cat", "<tuple>", cxx11},
5858 {"tuple_element", "<tuple>", cxx11},
5859 {"tuple_element_t", "<tuple>", cxx14},
5860 {"tuple_size", "<tuple>", cxx11},
5861 {"tuple_size_v", "<tuple>", cxx17},
5862 /* <type_traits>. */
5863 {"enable_if", "<type_traits>", cxx11},
5864 {"enable_if_t", "<type_traits>", cxx14},
5865 {"invoke_result", "<type_traits>", cxx17},
5866 {"invoke_result_t", "<type_traits>", cxx17},
5867 {"remove_cvref", "<type_traits>", cxx20},
5868 {"remove_cvref_t", "<type_traits>", cxx20},
5869 {"type_identity", "<type_traits>", cxx20},
5870 {"type_identity_t", "<type_traits>", cxx20},
5871 {"void_t", "<type_traits>", cxx17},
5872 {"conjunction", "<type_traits>", cxx17},
5873 {"conjunction_v", "<type_traits>", cxx17},
5874 {"disjunction", "<type_traits>", cxx17},
5875 {"disjunction_v", "<type_traits>", cxx17},
5876 {"negation", "<type_traits>", cxx17},
5877 {"negation_v", "<type_traits>", cxx17},
5878 /* <unordered_map>. */
5879 {"unordered_map", "<unordered_map>", cxx11},
5880 {"unordered_multimap", "<unordered_map>", cxx11},
5881 /* <unordered_set>. */
5882 {"unordered_set", "<unordered_set>", cxx11},
5883 {"unordered_multiset", "<unordered_set>", cxx11},
5884 /* <utility>. */
5885 {"declval", "<utility>", cxx11},
5886 {"forward", "<utility>", cxx11},
5887 {"make_pair", "<utility>", cxx98},
5888 {"move", "<utility>", cxx11},
5889 {"pair", "<utility>", cxx98},
5890 /* <variant>. */
5891 {"variant", "<variant>", cxx17},
5892 {"visit", "<variant>", cxx17},
5893 /* <vector>. */
5894 {"vector", "<vector>", cxx98},
5895 };
5896 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5897 for (size_t i = 0; i < num_hints; i++)
5898 {
5899 if (strcmp (name, hints[i].name) == 0)
5900 return &hints[i];
5901 }
5902 return NULL;
5903 }
5904
5905 /* Describe DIALECT. */
5906
5907 static const char *
5908 get_cxx_dialect_name (enum cxx_dialect dialect)
5909 {
5910 switch (dialect)
5911 {
5912 default:
5913 gcc_unreachable ();
5914 case cxx98:
5915 return "C++98";
5916 case cxx11:
5917 return "C++11";
5918 case cxx14:
5919 return "C++14";
5920 case cxx17:
5921 return "C++17";
5922 case cxx20:
5923 return "C++20";
5924 }
5925 }
5926
5927 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5928 that weren't recognized, but for which we know which header it ought to be
5929 in.
5930
5931 Emit a note either suggesting the header to be included, or noting that
5932 the current dialect is too early for the given name. */
5933
5934 class missing_std_header : public deferred_diagnostic
5935 {
5936 public:
5937 missing_std_header (location_t loc,
5938 const char *name_str,
5939 const std_name_hint *header_hint)
5940 : deferred_diagnostic (loc),
5941 m_name_str (name_str),
5942 m_header_hint (header_hint)
5943 {}
5944 ~missing_std_header ()
5945 {
5946 gcc_rich_location richloc (get_location ());
5947 if (cxx_dialect >= m_header_hint->min_dialect)
5948 {
5949 const char *header = m_header_hint->header;
5950 maybe_add_include_fixit (&richloc, header, true);
5951 inform (&richloc,
5952 "%<std::%s%> is defined in header %qs;"
5953 " did you forget to %<#include %s%>?",
5954 m_name_str, header, header);
5955 }
5956 else
5957 inform (&richloc,
5958 "%<std::%s%> is only available from %s onwards",
5959 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
5960 }
5961
5962 private:
5963 const char *m_name_str;
5964 const std_name_hint *m_header_hint;
5965 };
5966
5967 /* Attempt to generate a name_hint that suggests pertinent header files
5968 for NAME at LOCATION, for common names within the "std" namespace,
5969 or an empty name_hint if this isn't applicable. */
5970
5971 static name_hint
5972 maybe_suggest_missing_std_header (location_t location, tree name)
5973 {
5974 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5975
5976 const char *name_str = IDENTIFIER_POINTER (name);
5977 const std_name_hint *header_hint = get_std_name_hint (name_str);
5978 if (!header_hint)
5979 return name_hint ();
5980
5981 return name_hint (NULL, new missing_std_header (location, name_str,
5982 header_hint));
5983 }
5984
5985 /* Attempt to generate a name_hint that suggests a missing header file
5986 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5987 applicable. */
5988
5989 static name_hint
5990 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5991 {
5992 if (scope == NULL_TREE)
5993 return name_hint ();
5994 if (TREE_CODE (scope) != NAMESPACE_DECL)
5995 return name_hint ();
5996 /* We only offer suggestions for the "std" namespace. */
5997 if (scope != std_node)
5998 return name_hint ();
5999 return maybe_suggest_missing_std_header (location, name);
6000 }
6001
6002 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
6003 lookup failed within the explicitly provided SCOPE.
6004
6005 Suggest the best meaningful candidates (if any), otherwise
6006 an empty name_hint is returned. */
6007
6008 name_hint
6009 suggest_alternative_in_explicit_scope (location_t location, tree name,
6010 tree scope)
6011 {
6012 /* Something went very wrong; don't suggest anything. */
6013 if (name == error_mark_node)
6014 return name_hint ();
6015
6016 /* Resolve any namespace aliases. */
6017 scope = ORIGINAL_NAMESPACE (scope);
6018
6019 name_hint hint = maybe_suggest_missing_header (location, name, scope);
6020 if (hint)
6021 return hint;
6022
6023 cp_binding_level *level = NAMESPACE_LEVEL (scope);
6024
6025 best_match <tree, const char *> bm (name);
6026 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
6027
6028 /* See if we have a good suggesion for the user. */
6029 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
6030 if (fuzzy_name)
6031 return name_hint (fuzzy_name, NULL);
6032
6033 return name_hint ();
6034 }
6035
6036 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
6037 candidates. */
6038
6039 name_hint
6040 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum)
6041 {
6042 gcc_assert (SCOPED_ENUM_P (scoped_enum));
6043
6044 best_match <tree, const char *> bm (name);
6045 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter))
6046 {
6047 tree id = TREE_PURPOSE (iter);
6048 bm.consider (IDENTIFIER_POINTER (id));
6049 }
6050 return name_hint (bm.get_best_meaningful_candidate (), NULL);
6051 }
6052
6053 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
6054 or a class TYPE).
6055
6056 WANT as for lookup_name_1.
6057
6058 Returns a DECL (or OVERLOAD, or BASELINK) representing the
6059 declaration found. If no suitable declaration can be found,
6060 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
6061 neither a class-type nor a namespace a diagnostic is issued. */
6062
6063 tree
6064 lookup_qualified_name (tree scope, tree name, LOOK_want want, bool complain)
6065 {
6066 tree t = NULL_TREE;
6067
6068 if (TREE_CODE (scope) == NAMESPACE_DECL)
6069 {
6070 name_lookup lookup (name, want);
6071
6072 if (qualified_namespace_lookup (scope, &lookup))
6073 t = lookup.value;
6074 }
6075 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
6076 t = lookup_enumerator (scope, name);
6077 else if (is_class_type (scope, complain))
6078 t = lookup_member (scope, name, 2, bool (want & LOOK_want::TYPE),
6079 tf_warning_or_error);
6080
6081 if (!t)
6082 return error_mark_node;
6083 return t;
6084 }
6085
6086 /* Wrapper for the above that takes a string argument. The function name is
6087 not at the beginning of the line to keep this wrapper out of etags. */
6088
6089 tree lookup_qualified_name (tree t, const char *p, LOOK_want w, bool c)
6090 {
6091 return lookup_qualified_name (t, get_identifier (p), w, c);
6092 }
6093
6094 /* [namespace.qual]
6095 Accepts the NAME to lookup and its qualifying SCOPE.
6096 Returns the name/type pair found into the cxx_binding *RESULT,
6097 or false on error. */
6098
6099 static bool
6100 qualified_namespace_lookup (tree scope, name_lookup *lookup)
6101 {
6102 timevar_start (TV_NAME_LOOKUP);
6103 query_oracle (lookup->name);
6104 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
6105 timevar_stop (TV_NAME_LOOKUP);
6106 return found;
6107 }
6108
6109 /* Helper function for lookup_name_fuzzy.
6110 Traverse binding level LVL, looking for good name matches for NAME
6111 (and BM). */
6112 static void
6113 consider_binding_level (tree name, best_match <tree, const char *> &bm,
6114 cp_binding_level *lvl, bool look_within_fields,
6115 enum lookup_name_fuzzy_kind kind)
6116 {
6117 if (look_within_fields)
6118 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
6119 {
6120 tree type = lvl->this_entity;
6121 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
6122 tree best_matching_field
6123 = lookup_member_fuzzy (type, name, want_type_p);
6124 if (best_matching_field)
6125 bm.consider (IDENTIFIER_POINTER (best_matching_field));
6126 }
6127
6128 /* Only suggest names reserved for the implementation if NAME begins
6129 with an underscore. */
6130 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
6131
6132 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
6133 {
6134 tree d = t;
6135
6136 /* OVERLOADs or decls from using declaration are wrapped into
6137 TREE_LIST. */
6138 if (TREE_CODE (d) == TREE_LIST)
6139 d = OVL_FIRST (TREE_VALUE (d));
6140
6141 /* Don't use bindings from implicitly declared functions,
6142 as they were likely misspellings themselves. */
6143 if (TREE_TYPE (d) == error_mark_node)
6144 continue;
6145
6146 /* Skip anticipated decls of builtin functions. */
6147 if (TREE_CODE (d) == FUNCTION_DECL
6148 && fndecl_built_in_p (d)
6149 && DECL_ANTICIPATED (d))
6150 continue;
6151
6152 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6153 within range for). */
6154 if (TREE_CODE (d) == VAR_DECL
6155 && DECL_ARTIFICIAL (d))
6156 continue;
6157
6158 tree suggestion = DECL_NAME (d);
6159 if (!suggestion)
6160 continue;
6161
6162 /* Don't suggest names that are for anonymous aggregate types, as
6163 they are an implementation detail generated by the compiler. */
6164 if (IDENTIFIER_ANON_P (suggestion))
6165 continue;
6166
6167 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
6168
6169 /* Ignore internal names with spaces in them. */
6170 if (strchr (suggestion_str, ' '))
6171 continue;
6172
6173 /* Don't suggest names that are reserved for use by the
6174 implementation, unless NAME began with an underscore. */
6175 if (name_reserved_for_implementation_p (suggestion_str)
6176 && !consider_implementation_names)
6177 continue;
6178
6179 bm.consider (suggestion_str);
6180 }
6181 }
6182
6183 /* Subclass of deferred_diagnostic. Notify the user that the
6184 given macro was used before it was defined.
6185 This can be done in the C++ frontend since tokenization happens
6186 upfront. */
6187
6188 class macro_use_before_def : public deferred_diagnostic
6189 {
6190 public:
6191 /* Factory function. Return a new macro_use_before_def instance if
6192 appropriate, or return NULL. */
6193 static macro_use_before_def *
6194 maybe_make (location_t use_loc, cpp_hashnode *macro)
6195 {
6196 location_t def_loc = cpp_macro_definition_location (macro);
6197 if (def_loc == UNKNOWN_LOCATION)
6198 return NULL;
6199
6200 /* We only want to issue a note if the macro was used *before* it was
6201 defined.
6202 We don't want to issue a note for cases where a macro was incorrectly
6203 used, leaving it unexpanded (e.g. by using the wrong argument
6204 count). */
6205 if (!linemap_location_before_p (line_table, use_loc, def_loc))
6206 return NULL;
6207
6208 return new macro_use_before_def (use_loc, macro);
6209 }
6210
6211 private:
6212 /* Ctor. LOC is the location of the usage. MACRO is the
6213 macro that was used. */
6214 macro_use_before_def (location_t loc, cpp_hashnode *macro)
6215 : deferred_diagnostic (loc), m_macro (macro)
6216 {
6217 gcc_assert (macro);
6218 }
6219
6220 ~macro_use_before_def ()
6221 {
6222 if (is_suppressed_p ())
6223 return;
6224
6225 inform (get_location (), "the macro %qs had not yet been defined",
6226 (const char *)m_macro->ident.str);
6227 inform (cpp_macro_definition_location (m_macro),
6228 "it was later defined here");
6229 }
6230
6231 private:
6232 cpp_hashnode *m_macro;
6233 };
6234
6235 /* Determine if it can ever make sense to offer RID as a suggestion for
6236 a misspelling.
6237
6238 Subroutine of lookup_name_fuzzy. */
6239
6240 static bool
6241 suggest_rid_p (enum rid rid)
6242 {
6243 switch (rid)
6244 {
6245 /* Support suggesting function-like keywords. */
6246 case RID_STATIC_ASSERT:
6247 return true;
6248
6249 default:
6250 /* Support suggesting the various decl-specifier words, to handle
6251 e.g. "singed" vs "signed" typos. */
6252 if (cp_keyword_starts_decl_specifier_p (rid))
6253 return true;
6254
6255 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6256 and "do" for short misspellings, which are likely to lead to
6257 nonsensical results. */
6258 return false;
6259 }
6260 }
6261
6262 /* Search for near-matches for NAME within the current bindings, and within
6263 macro names, returning the best match as a const char *, or NULL if
6264 no reasonable match is found.
6265
6266 Use LOC for any deferred diagnostics. */
6267
6268 name_hint
6269 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
6270 {
6271 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
6272
6273 /* First, try some well-known names in the C++ standard library, in case
6274 the user forgot a #include. */
6275 const char *header_hint
6276 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
6277 if (header_hint)
6278 return name_hint (NULL,
6279 new suggest_missing_header (loc,
6280 IDENTIFIER_POINTER (name),
6281 header_hint));
6282
6283 best_match <tree, const char *> bm (name);
6284
6285 cp_binding_level *lvl;
6286 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
6287 consider_binding_level (name, bm, lvl, true, kind);
6288
6289 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
6290 consider_binding_level (name, bm, lvl, false, kind);
6291
6292 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6293 as:
6294 x = SOME_OTHER_MACRO (y);
6295 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6296 as a misspelled identifier.
6297
6298 Use the best distance so far so that a candidate is only set if
6299 a macro is better than anything so far. This allows early rejection
6300 (without calculating the edit distance) of macro names that must have
6301 distance >= bm.get_best_distance (), and means that we only get a
6302 non-NULL result for best_macro_match if it's better than any of
6303 the identifiers already checked. */
6304 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
6305 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
6306 /* If a macro is the closest so far to NAME, consider it. */
6307 if (best_macro)
6308 bm.consider ((const char *)best_macro->ident.str);
6309 else if (bmm.get_best_distance () == 0)
6310 {
6311 /* If we have an exact match for a macro name, then either the
6312 macro was used with the wrong argument count, or the macro
6313 has been used before it was defined. */
6314 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
6315 if (cpp_user_macro_p (macro))
6316 return name_hint (NULL,
6317 macro_use_before_def::maybe_make (loc, macro));
6318 }
6319
6320 /* Try the "starts_decl_specifier_p" keywords to detect
6321 "singed" vs "signed" typos. */
6322 for (unsigned i = 0; i < num_c_common_reswords; i++)
6323 {
6324 const c_common_resword *resword = &c_common_reswords[i];
6325
6326 if (!suggest_rid_p (resword->rid))
6327 continue;
6328
6329 tree resword_identifier = ridpointers [resword->rid];
6330 if (!resword_identifier)
6331 continue;
6332 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
6333
6334 /* Only consider reserved words that survived the
6335 filtering in init_reswords (e.g. for -std). */
6336 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
6337 continue;
6338
6339 bm.consider (IDENTIFIER_POINTER (resword_identifier));
6340 }
6341
6342 return name_hint (bm.get_best_meaningful_candidate (), NULL);
6343 }
6344
6345 /* Subroutine of outer_binding.
6346
6347 Returns TRUE if BINDING is a binding to a template parameter of
6348 SCOPE. In that case SCOPE is the scope of a primary template
6349 parameter -- in the sense of G++, i.e, a template that has its own
6350 template header.
6351
6352 Returns FALSE otherwise. */
6353
6354 static bool
6355 binding_to_template_parms_of_scope_p (cxx_binding *binding,
6356 cp_binding_level *scope)
6357 {
6358 tree binding_value, tmpl, tinfo;
6359 int level;
6360
6361 if (!binding || !scope || !scope->this_entity)
6362 return false;
6363
6364 binding_value = binding->value ? binding->value : binding->type;
6365 tinfo = get_template_info (scope->this_entity);
6366
6367 /* BINDING_VALUE must be a template parm. */
6368 if (binding_value == NULL_TREE
6369 || (!DECL_P (binding_value)
6370 || !DECL_TEMPLATE_PARM_P (binding_value)))
6371 return false;
6372
6373 /* The level of BINDING_VALUE. */
6374 level =
6375 template_type_parameter_p (binding_value)
6376 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6377 (TREE_TYPE (binding_value)))
6378 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6379
6380 /* The template of the current scope, iff said scope is a primary
6381 template. */
6382 tmpl = (tinfo
6383 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6384 ? TI_TEMPLATE (tinfo)
6385 : NULL_TREE);
6386
6387 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6388 then BINDING_VALUE is a parameter of TMPL. */
6389 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6390 }
6391
6392 /* Return the innermost non-namespace binding for NAME from a scope
6393 containing BINDING, or, if BINDING is NULL, the current scope.
6394 Please note that for a given template, the template parameters are
6395 considered to be in the scope containing the current scope.
6396 If CLASS_P is false, then class bindings are ignored. */
6397
6398 cxx_binding *
6399 outer_binding (tree name,
6400 cxx_binding *binding,
6401 bool class_p)
6402 {
6403 cxx_binding *outer;
6404 cp_binding_level *scope;
6405 cp_binding_level *outer_scope;
6406
6407 if (binding)
6408 {
6409 scope = binding->scope->level_chain;
6410 outer = binding->previous;
6411 }
6412 else
6413 {
6414 scope = current_binding_level;
6415 outer = IDENTIFIER_BINDING (name);
6416 }
6417 outer_scope = outer ? outer->scope : NULL;
6418
6419 /* Because we create class bindings lazily, we might be missing a
6420 class binding for NAME. If there are any class binding levels
6421 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6422 declared, we must lookup NAME in those class scopes. */
6423 if (class_p)
6424 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6425 {
6426 if (scope->kind == sk_class)
6427 {
6428 cxx_binding *class_binding;
6429
6430 class_binding = get_class_binding (name, scope);
6431 if (class_binding)
6432 {
6433 /* Thread this new class-scope binding onto the
6434 IDENTIFIER_BINDING list so that future lookups
6435 find it quickly. */
6436 class_binding->previous = outer;
6437 if (binding)
6438 binding->previous = class_binding;
6439 else
6440 IDENTIFIER_BINDING (name) = class_binding;
6441 return class_binding;
6442 }
6443 }
6444 /* If we are in a member template, the template parms of the member
6445 template are considered to be inside the scope of the containing
6446 class, but within G++ the class bindings are all pushed between the
6447 template parms and the function body. So if the outer binding is
6448 a template parm for the current scope, return it now rather than
6449 look for a class binding. */
6450 if (outer_scope && outer_scope->kind == sk_template_parms
6451 && binding_to_template_parms_of_scope_p (outer, scope))
6452 return outer;
6453
6454 scope = scope->level_chain;
6455 }
6456
6457 return outer;
6458 }
6459
6460 /* Return the innermost block-scope or class-scope value binding for
6461 NAME, or NULL_TREE if there is no such binding. */
6462
6463 tree
6464 innermost_non_namespace_value (tree name)
6465 {
6466 cxx_binding *binding;
6467 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6468 return binding ? binding->value : NULL_TREE;
6469 }
6470
6471 /* Look up NAME in the current binding level and its superiors in the
6472 namespace of variables, functions and typedefs. Return a ..._DECL
6473 node of some kind representing its definition if there is only one
6474 such declaration, or return a TREE_LIST with all the overloaded
6475 definitions if there are many, or return NULL_TREE if it is undefined.
6476 Hidden name, either friend declaration or built-in function, are
6477 not ignored.
6478
6479 WHERE controls which scopes are considered. It is a bit mask of
6480 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
6481 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
6482 scopes). It is an error for no bits to be set. These scopes are
6483 searched from innermost to outermost.
6484
6485 WANT controls what kind of entity we'd happy with.
6486 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
6487 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
6488 for only NAMESPACE_DECLS. These two can be bit-ored to find
6489 namespace or type.
6490
6491 WANT can also have LOOK_want::HIDDEN_FRIEND or
6492 LOOK_want::HIDDEN_LAMBDa added to it. */
6493
6494 static tree
6495 lookup_name_1 (tree name, LOOK_where where, LOOK_want want)
6496 {
6497 tree val = NULL_TREE;
6498
6499 gcc_checking_assert (unsigned (where) != 0);
6500 /* If we're looking for hidden lambda things, we shouldn't be
6501 looking in namespace scope. */
6502 gcc_checking_assert (!bool (want & LOOK_want::HIDDEN_LAMBDA)
6503 || !bool (where & LOOK_where::NAMESPACE));
6504 query_oracle (name);
6505
6506 /* Conversion operators are handled specially because ordinary
6507 unqualified name lookup will not find template conversion
6508 operators. */
6509 if (IDENTIFIER_CONV_OP_P (name))
6510 {
6511 cp_binding_level *level;
6512
6513 for (level = current_binding_level;
6514 level && level->kind != sk_namespace;
6515 level = level->level_chain)
6516 {
6517 tree class_type;
6518 tree operators;
6519
6520 /* A conversion operator can only be declared in a class
6521 scope. */
6522 if (level->kind != sk_class)
6523 continue;
6524
6525 /* Lookup the conversion operator in the class. */
6526 class_type = level->this_entity;
6527 operators = lookup_fnfields (class_type, name, /*protect=*/0,
6528 tf_warning_or_error);
6529 if (operators)
6530 return operators;
6531 }
6532
6533 return NULL_TREE;
6534 }
6535
6536 /* First, look in non-namespace scopes. */
6537
6538 if (current_class_type == NULL_TREE)
6539 /* Maybe avoid searching the binding stack at all. */
6540 where = LOOK_where (unsigned (where) & ~unsigned (LOOK_where::CLASS));
6541
6542 if (bool (where & (LOOK_where::BLOCK | LOOK_where::CLASS)))
6543 for (cxx_binding *iter = nullptr;
6544 (iter = outer_binding (name, iter, bool (where & LOOK_where::CLASS)));)
6545 {
6546 /* Skip entities we don't want. */
6547 if (!bool (where & (LOCAL_BINDING_P (iter)
6548 ? LOOK_where::BLOCK : LOOK_where::CLASS)))
6549 continue;
6550
6551 /* If this is the kind of thing we're looking for, we're done. */
6552 if (iter->value)
6553 {
6554 tree binding = NULL_TREE;
6555
6556 if (!(!iter->type && HIDDEN_TYPE_BINDING_P (iter))
6557 && (bool (want & LOOK_want::HIDDEN_LAMBDA)
6558 || !is_lambda_ignored_entity (iter->value))
6559 && qualify_lookup (iter->value, want))
6560 binding = iter->value;
6561 else if (bool (want & LOOK_want::TYPE)
6562 && !HIDDEN_TYPE_BINDING_P (iter)
6563 && iter->type)
6564 binding = iter->type;
6565
6566 if (binding)
6567 {
6568 /* The saved lookups for an operator record 'nothing
6569 found' as error_mark_node. We need to stop the search
6570 here, but not return the error mark node. */
6571 if (binding == error_mark_node)
6572 binding = NULL_TREE;
6573
6574 val = binding;
6575 goto found;
6576 }
6577 }
6578 }
6579
6580 /* Now lookup in namespace scopes. */
6581 if (bool (where & LOOK_where::NAMESPACE))
6582 {
6583 name_lookup lookup (name, want);
6584 if (lookup.search_unqualified
6585 (current_decl_namespace (), current_binding_level))
6586 val = lookup.value;
6587 }
6588
6589 found:;
6590
6591 /* If we have a single function from a using decl, pull it out. */
6592 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6593 val = OVL_FUNCTION (val);
6594
6595 return val;
6596 }
6597
6598 /* Wrapper for lookup_name_1. */
6599
6600 tree
6601 lookup_name (tree name, LOOK_where where, LOOK_want want)
6602 {
6603 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6604 tree ret = lookup_name_1 (name, where, want);
6605 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6606 return ret;
6607 }
6608
6609 tree
6610 lookup_name (tree name)
6611 {
6612 return lookup_name (name, LOOK_where::ALL, LOOK_want::NORMAL);
6613 }
6614
6615 /* Look up NAME for type used in elaborated name specifier in
6616 the scopes given by HOW.
6617
6618 Unlike lookup_name_1, we make sure that NAME is actually
6619 declared in the desired scope, not from inheritance, nor using
6620 directive. For using declaration, there is DR138 still waiting
6621 to be resolved. Hidden name coming from an earlier friend
6622 declaration is also returned, and will be made visible unless HOW
6623 is TAG_how::HIDDEN_FRIEND.
6624
6625 A TYPE_DECL best matching the NAME is returned. Catching error
6626 and issuing diagnostics are caller's responsibility. */
6627
6628 static tree
6629 lookup_elaborated_type_1 (tree name, TAG_how how)
6630 {
6631 cp_binding_level *b = current_binding_level;
6632
6633 if (b->kind != sk_namespace)
6634 /* Look in non-namespace scopes. */
6635 for (cxx_binding *iter = NULL;
6636 (iter = outer_binding (name, iter, /*class_p=*/ true)); )
6637 {
6638 /* First check we're supposed to be looking in this scope --
6639 if we're not, we're done. */
6640 for (; b != iter->scope; b = b->level_chain)
6641 if (!(b->kind == sk_cleanup
6642 || b->kind == sk_template_parms
6643 || b->kind == sk_function_parms
6644 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
6645 return NULL_TREE;
6646
6647 /* Check if this is the kind of thing we're looking for. If
6648 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
6649 come from base class. For ITER->VALUE, we can simply use
6650 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6651 our own check.
6652
6653 We check ITER->TYPE before ITER->VALUE in order to handle
6654 typedef struct C {} C;
6655 correctly. */
6656
6657 tree found = NULL_TREE;
6658 bool reveal = false;
6659 if (tree type = iter->type)
6660 {
6661 if (qualify_lookup (type, LOOK_want::TYPE)
6662 && (how != TAG_how::CURRENT_ONLY
6663 || LOCAL_BINDING_P (iter)
6664 || DECL_CONTEXT (type) == iter->scope->this_entity))
6665 {
6666 found = type;
6667 if (how != TAG_how::HIDDEN_FRIEND)
6668 reveal = HIDDEN_TYPE_BINDING_P (iter);
6669 }
6670 }
6671 else
6672 {
6673 if (qualify_lookup (iter->value, LOOK_want::TYPE)
6674 && (how != TAG_how::CURRENT_ONLY
6675 || !INHERITED_VALUE_BINDING_P (iter)))
6676 {
6677 found = iter->value;
6678 if (how != TAG_how::HIDDEN_FRIEND)
6679 reveal = !iter->type && HIDDEN_TYPE_BINDING_P (iter);
6680 }
6681 }
6682
6683 if (found)
6684 {
6685 if (reveal)
6686 {
6687 /* It is no longer a hidden binding. */
6688 HIDDEN_TYPE_BINDING_P (iter) = false;
6689
6690 /* Unanticipate the decl itself. */
6691 DECL_ANTICIPATED (found) = false;
6692 DECL_FRIEND_P (found) = false;
6693
6694 gcc_checking_assert (TREE_CODE (found) != TEMPLATE_DECL);
6695
6696 if (tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (found)))
6697 {
6698 tree tmpl = TI_TEMPLATE (ti);
6699 DECL_ANTICIPATED (tmpl) = false;
6700 DECL_FRIEND_P (tmpl) = false;
6701 }
6702 }
6703
6704 return found;
6705 }
6706 }
6707
6708 /* Now check if we can look in namespace scope. */
6709 for (; b->kind != sk_namespace; b = b->level_chain)
6710 if (!(b->kind == sk_cleanup
6711 || b->kind == sk_template_parms
6712 || b->kind == sk_function_parms
6713 || (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)))
6714 return NULL_TREE;
6715
6716 /* Look in the innermost namespace. */
6717 tree ns = b->this_entity;
6718 if (tree *slot = find_namespace_slot (ns, name))
6719 {
6720 /* If this is the kind of thing we're looking for, we're done. */
6721 tree found = NULL_TREE;
6722 bool reveal = false;
6723
6724 if (tree type = MAYBE_STAT_TYPE (*slot))
6725 {
6726 found = type;
6727 if (how != TAG_how::HIDDEN_FRIEND)
6728 {
6729 reveal = STAT_TYPE_HIDDEN_P (*slot);
6730 STAT_TYPE_HIDDEN_P (*slot) = false;
6731 }
6732 }
6733 else if (tree decl = MAYBE_STAT_DECL (*slot))
6734 {
6735 if (qualify_lookup (decl, LOOK_want::TYPE))
6736 {
6737 found = decl;
6738
6739 if (how != TAG_how::HIDDEN_FRIEND && STAT_HACK_P (*slot))
6740 {
6741 reveal = STAT_DECL_HIDDEN_P (*slot);
6742 if (reveal)
6743 {
6744 if (STAT_TYPE (*slot))
6745 STAT_DECL_HIDDEN_P (*slot) = false;
6746 else
6747 /* There is no type, just remove the stat
6748 hack. */
6749 *slot = decl;
6750 }
6751 }
6752 }
6753 }
6754
6755 if (found)
6756 {
6757 if (reveal)
6758 {
6759 /* Reveal the previously hidden thing. */
6760 DECL_ANTICIPATED (found) = false;
6761 DECL_FRIEND_P (found) = false;
6762
6763 if (TREE_CODE (found) == TEMPLATE_DECL)
6764 {
6765 DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (found)) = false;
6766 DECL_FRIEND_P (DECL_TEMPLATE_RESULT (found)) = false;
6767 }
6768 else if (tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (found)))
6769 {
6770 tree tmpl = TI_TEMPLATE (ti);
6771 DECL_ANTICIPATED (tmpl) = false;
6772 DECL_FRIEND_P (tmpl) = false;
6773 }
6774 }
6775
6776 return found;
6777 }
6778 }
6779
6780 return NULL_TREE;
6781 }
6782
6783 /* Wrapper for lookup_type_scope_1. */
6784
6785 tree
6786 lookup_elaborated_type (tree name, TAG_how how)
6787 {
6788 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6789 tree ret = lookup_elaborated_type_1 (name, how);
6790 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6791 return ret;
6792 }
6793
6794 /* Returns true iff DECL is a block-scope extern declaration of a function
6795 or variable. We will already have determined validity of the decl
6796 when pushing it. So we do not have to redo that lookup. */
6797
6798 bool
6799 is_local_extern (tree decl)
6800 {
6801 if ((TREE_CODE (decl) == FUNCTION_DECL
6802 || TREE_CODE (decl) == VAR_DECL))
6803 return DECL_LOCAL_DECL_P (decl);
6804
6805 return false;
6806 }
6807
6808 /* The type TYPE is being declared. If it is a class template, or a
6809 specialization of a class template, do any processing required and
6810 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6811 being declared a friend. B is the binding level at which this TYPE
6812 should be bound.
6813
6814 Returns the TYPE_DECL for TYPE, which may have been altered by this
6815 processing. */
6816
6817 static tree
6818 maybe_process_template_type_declaration (tree type, int is_friend,
6819 cp_binding_level *b)
6820 {
6821 tree decl = TYPE_NAME (type);
6822
6823 if (processing_template_parmlist)
6824 /* You can't declare a new template type in a template parameter
6825 list. But, you can declare a non-template type:
6826
6827 template <class A*> struct S;
6828
6829 is a forward-declaration of `A'. */
6830 ;
6831 else if (b->kind == sk_namespace
6832 && current_binding_level->kind != sk_namespace)
6833 /* If this new type is being injected into a containing scope,
6834 then it's not a template type. */
6835 ;
6836 else
6837 {
6838 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6839 || TREE_CODE (type) == ENUMERAL_TYPE);
6840
6841 if (processing_template_decl)
6842 {
6843 /* This may change after the call to push_template_decl, but
6844 we want the original value. */
6845 tree name = DECL_NAME (decl);
6846
6847 decl = push_template_decl (decl, is_friend);
6848 if (decl == error_mark_node)
6849 return error_mark_node;
6850
6851 /* If the current binding level is the binding level for the
6852 template parameters (see the comment in
6853 begin_template_parm_list) and the enclosing level is a class
6854 scope, and we're not looking at a friend, push the
6855 declaration of the member class into the class scope. In the
6856 friend case, push_template_decl will already have put the
6857 friend into global scope, if appropriate. */
6858 if (TREE_CODE (type) != ENUMERAL_TYPE
6859 && !is_friend && b->kind == sk_template_parms
6860 && b->level_chain->kind == sk_class)
6861 {
6862 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6863
6864 if (!COMPLETE_TYPE_P (current_class_type))
6865 {
6866 maybe_add_class_template_decl_list (current_class_type,
6867 type, /*friend_p=*/0);
6868 /* Put this UTD in the table of UTDs for the class. */
6869 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6870 CLASSTYPE_NESTED_UTDS (current_class_type) =
6871 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6872
6873 binding_table_insert
6874 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6875 }
6876 }
6877 }
6878 }
6879
6880 return decl;
6881 }
6882
6883 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6884 that the NAME is a class template, the tag is processed but not pushed.
6885
6886 The pushed scope depend on the SCOPE parameter:
6887 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6888 scope.
6889 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6890 non-template-parameter scope. This case is needed for forward
6891 declarations.
6892 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6893 TS_GLOBAL case except that names within template-parameter scopes
6894 are not pushed at all.
6895
6896 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6897
6898 static tree
6899 do_pushtag (tree name, tree type, TAG_how how)
6900 {
6901 tree decl;
6902
6903 cp_binding_level *b = current_binding_level;
6904 while (true)
6905 {
6906 if (/* Cleanup scopes are not scopes from the point of view of
6907 the language. */
6908 b->kind == sk_cleanup
6909 /* Neither are function parameter scopes. */
6910 || b->kind == sk_function_parms
6911 /* Neither are the scopes used to hold template parameters
6912 for an explicit specialization. For an ordinary template
6913 declaration, these scopes are not scopes from the point of
6914 view of the language. */
6915 || (b->kind == sk_template_parms
6916 && (b->explicit_spec_p || how == TAG_how::GLOBAL)))
6917 b = b->level_chain;
6918 else if (b->kind == sk_class && how != TAG_how::CURRENT_ONLY)
6919 {
6920 b = b->level_chain;
6921 if (b->kind == sk_template_parms)
6922 b = b->level_chain;
6923 }
6924 else
6925 break;
6926 }
6927
6928 gcc_assert (identifier_p (name));
6929
6930 /* Do C++ gratuitous typedefing. */
6931 if (identifier_type_value_1 (name) != type)
6932 {
6933 tree tdef;
6934 int in_class = 0;
6935 tree context = TYPE_CONTEXT (type);
6936
6937 if (! context)
6938 {
6939 cp_binding_level *cb = b;
6940 while (cb->kind != sk_namespace
6941 && cb->kind != sk_class
6942 && (cb->kind != sk_function_parms
6943 || !cb->this_entity))
6944 cb = cb->level_chain;
6945 tree cs = cb->this_entity;
6946
6947 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6948 ? cs == current_function_decl
6949 : TYPE_P (cs) ? cs == current_class_type
6950 : cs == current_namespace);
6951
6952 if (how == TAG_how::CURRENT_ONLY
6953 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6954 context = cs;
6955 else if (cs && TYPE_P (cs))
6956 /* When declaring a friend class of a local class, we want
6957 to inject the newly named class into the scope
6958 containing the local class, not the namespace
6959 scope. */
6960 context = decl_function_context (get_type_decl (cs));
6961 }
6962 if (!context)
6963 context = current_namespace;
6964
6965 if (b->kind == sk_class
6966 || (b->kind == sk_template_parms
6967 && b->level_chain->kind == sk_class))
6968 in_class = 1;
6969
6970 tdef = create_implicit_typedef (name, type);
6971 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6972 bool is_friend = how == TAG_how::HIDDEN_FRIEND;
6973 if (is_friend)
6974 {
6975 // FIXME: can go away
6976 /* This is a friend. Make this TYPE_DECL node hidden from
6977 ordinary name lookup. Its corresponding TEMPLATE_DECL
6978 will be marked in push_template_decl. */
6979 retrofit_lang_decl (tdef);
6980 DECL_ANTICIPATED (tdef) = 1;
6981 DECL_FRIEND_P (tdef) = 1;
6982 }
6983
6984 decl = maybe_process_template_type_declaration (type, is_friend, b);
6985 if (decl == error_mark_node)
6986 return decl;
6987
6988 if (b->kind == sk_class)
6989 {
6990 if (!TYPE_BEING_DEFINED (current_class_type))
6991 /* Don't push anywhere if the class is complete; a lambda in an
6992 NSDMI is not a member of the class. */
6993 ;
6994 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6995 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6996 class. But if it's a member template class, we want
6997 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6998 later. */
6999 finish_member_declaration (decl);
7000 else
7001 pushdecl_class_level (decl);
7002 }
7003 else if (b->kind != sk_template_parms)
7004 {
7005 decl = do_pushdecl_with_scope
7006 (decl, b, /*hiding=*/(how == TAG_how::HIDDEN_FRIEND));
7007 if (decl == error_mark_node)
7008 return decl;
7009
7010 if (DECL_CONTEXT (decl) == std_node
7011 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
7012 && !CLASSTYPE_TEMPLATE_INFO (type))
7013 {
7014 error ("declaration of %<std::initializer_list%> does not match "
7015 "%<#include <initializer_list>%>, isn%'t a template");
7016 return error_mark_node;
7017 }
7018 }
7019
7020 if (! in_class)
7021 set_identifier_type_value_with_scope (name, tdef, b);
7022
7023 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
7024
7025 /* If this is a local class, keep track of it. We need this
7026 information for name-mangling, and so that it is possible to
7027 find all function definitions in a translation unit in a
7028 convenient way. (It's otherwise tricky to find a member
7029 function definition it's only pointed to from within a local
7030 class.) */
7031 if (TYPE_FUNCTION_SCOPE_P (type))
7032 {
7033 if (processing_template_decl)
7034 {
7035 /* Push a DECL_EXPR so we call pushtag at the right time in
7036 template instantiation rather than in some nested context. */
7037 add_decl_expr (decl);
7038 }
7039 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
7040 else if (!LAMBDA_TYPE_P (type))
7041 determine_local_discriminator (TYPE_NAME (type));
7042 }
7043 }
7044
7045 if (b->kind == sk_class
7046 && !COMPLETE_TYPE_P (current_class_type))
7047 {
7048 maybe_add_class_template_decl_list (current_class_type,
7049 type, /*friend_p=*/0);
7050
7051 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
7052 CLASSTYPE_NESTED_UTDS (current_class_type)
7053 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
7054
7055 binding_table_insert
7056 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
7057 }
7058
7059 decl = TYPE_NAME (type);
7060 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
7061
7062 /* Set type visibility now if this is a forward declaration. */
7063 TREE_PUBLIC (decl) = 1;
7064 determine_visibility (decl);
7065
7066 return type;
7067 }
7068
7069 /* Wrapper for do_pushtag. */
7070
7071 tree
7072 pushtag (tree name, tree type, TAG_how how)
7073 {
7074 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7075 tree ret = do_pushtag (name, type, how);
7076 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7077 return ret;
7078 }
7079
7080 \f
7081 /* Subroutines for reverting temporarily to top-level for instantiation
7082 of templates and such. We actually need to clear out the class- and
7083 local-value slots of all identifiers, so that only the global values
7084 are at all visible. Simply setting current_binding_level to the global
7085 scope isn't enough, because more binding levels may be pushed. */
7086 struct saved_scope *scope_chain;
7087
7088 /* Return true if ID has not already been marked. */
7089
7090 static inline bool
7091 store_binding_p (tree id)
7092 {
7093 if (!id || !IDENTIFIER_BINDING (id))
7094 return false;
7095
7096 if (IDENTIFIER_MARKED (id))
7097 return false;
7098
7099 return true;
7100 }
7101
7102 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
7103 have enough space reserved. */
7104
7105 static void
7106 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
7107 {
7108 cxx_saved_binding saved;
7109
7110 gcc_checking_assert (store_binding_p (id));
7111
7112 IDENTIFIER_MARKED (id) = 1;
7113
7114 saved.identifier = id;
7115 saved.binding = IDENTIFIER_BINDING (id);
7116 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
7117 (*old_bindings)->quick_push (saved);
7118 IDENTIFIER_BINDING (id) = NULL;
7119 }
7120
7121 static void
7122 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
7123 {
7124 static vec<tree> bindings_need_stored;
7125 tree t, id;
7126 size_t i;
7127
7128 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7129 for (t = names; t; t = TREE_CHAIN (t))
7130 {
7131 if (TREE_CODE (t) == TREE_LIST)
7132 id = TREE_PURPOSE (t);
7133 else
7134 id = DECL_NAME (t);
7135
7136 if (store_binding_p (id))
7137 bindings_need_stored.safe_push (id);
7138 }
7139 if (!bindings_need_stored.is_empty ())
7140 {
7141 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
7142 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
7143 {
7144 /* We can apparently have duplicates in NAMES. */
7145 if (store_binding_p (id))
7146 store_binding (id, old_bindings);
7147 }
7148 bindings_need_stored.truncate (0);
7149 }
7150 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7151 }
7152
7153 /* Like store_bindings, but NAMES is a vector of cp_class_binding
7154 objects, rather than a TREE_LIST. */
7155
7156 static void
7157 store_class_bindings (vec<cp_class_binding, va_gc> *names,
7158 vec<cxx_saved_binding, va_gc> **old_bindings)
7159 {
7160 static vec<tree> bindings_need_stored;
7161 size_t i;
7162 cp_class_binding *cb;
7163
7164 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
7165 if (store_binding_p (cb->identifier))
7166 bindings_need_stored.safe_push (cb->identifier);
7167 if (!bindings_need_stored.is_empty ())
7168 {
7169 tree id;
7170 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
7171 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
7172 store_binding (id, old_bindings);
7173 bindings_need_stored.truncate (0);
7174 }
7175 }
7176
7177 /* A chain of saved_scope structures awaiting reuse. */
7178
7179 static GTY((deletable)) struct saved_scope *free_saved_scope;
7180
7181 static void
7182 do_push_to_top_level (void)
7183 {
7184 struct saved_scope *s;
7185 cp_binding_level *b;
7186 cxx_saved_binding *sb;
7187 size_t i;
7188 bool need_pop;
7189
7190 /* Reuse or create a new structure for this saved scope. */
7191 if (free_saved_scope != NULL)
7192 {
7193 s = free_saved_scope;
7194 free_saved_scope = s->prev;
7195
7196 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
7197 memset (s, 0, sizeof (*s));
7198 /* Also reuse the structure's old_bindings vector. */
7199 vec_safe_truncate (old_bindings, 0);
7200 s->old_bindings = old_bindings;
7201 }
7202 else
7203 s = ggc_cleared_alloc<saved_scope> ();
7204
7205 b = scope_chain ? current_binding_level : 0;
7206
7207 /* If we're in the middle of some function, save our state. */
7208 if (cfun)
7209 {
7210 need_pop = true;
7211 push_function_context ();
7212 }
7213 else
7214 need_pop = false;
7215
7216 if (scope_chain && previous_class_level)
7217 store_class_bindings (previous_class_level->class_shadowed,
7218 &s->old_bindings);
7219
7220 /* Have to include the global scope, because class-scope decls
7221 aren't listed anywhere useful. */
7222 for (; b; b = b->level_chain)
7223 {
7224 tree t;
7225
7226 /* Template IDs are inserted into the global level. If they were
7227 inserted into namespace level, finish_file wouldn't find them
7228 when doing pending instantiations. Therefore, don't stop at
7229 namespace level, but continue until :: . */
7230 if (global_scope_p (b))
7231 break;
7232
7233 store_bindings (b->names, &s->old_bindings);
7234 /* We also need to check class_shadowed to save class-level type
7235 bindings, since pushclass doesn't fill in b->names. */
7236 if (b->kind == sk_class)
7237 store_class_bindings (b->class_shadowed, &s->old_bindings);
7238
7239 /* Unwind type-value slots back to top level. */
7240 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
7241 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
7242 }
7243
7244 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
7245 IDENTIFIER_MARKED (sb->identifier) = 0;
7246
7247 s->prev = scope_chain;
7248 s->bindings = b;
7249 s->need_pop_function_context = need_pop;
7250 s->function_decl = current_function_decl;
7251 s->unevaluated_operand = cp_unevaluated_operand;
7252 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7253 s->suppress_location_wrappers = suppress_location_wrappers;
7254 s->x_stmt_tree.stmts_are_full_exprs_p = true;
7255
7256 scope_chain = s;
7257 current_function_decl = NULL_TREE;
7258 current_lang_base = NULL;
7259 current_lang_name = lang_name_cplusplus;
7260 current_namespace = global_namespace;
7261 push_class_stack ();
7262 cp_unevaluated_operand = 0;
7263 c_inhibit_evaluation_warnings = 0;
7264 suppress_location_wrappers = 0;
7265 }
7266
7267 static void
7268 do_pop_from_top_level (void)
7269 {
7270 struct saved_scope *s = scope_chain;
7271 cxx_saved_binding *saved;
7272 size_t i;
7273
7274 /* Clear out class-level bindings cache. */
7275 if (previous_class_level)
7276 invalidate_class_lookup_cache ();
7277 pop_class_stack ();
7278
7279 release_tree_vector (current_lang_base);
7280
7281 scope_chain = s->prev;
7282 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
7283 {
7284 tree id = saved->identifier;
7285
7286 IDENTIFIER_BINDING (id) = saved->binding;
7287 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
7288 }
7289
7290 /* If we were in the middle of compiling a function, restore our
7291 state. */
7292 if (s->need_pop_function_context)
7293 pop_function_context ();
7294 current_function_decl = s->function_decl;
7295 cp_unevaluated_operand = s->unevaluated_operand;
7296 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
7297 suppress_location_wrappers = s->suppress_location_wrappers;
7298
7299 /* Make this saved_scope structure available for reuse by
7300 push_to_top_level. */
7301 s->prev = free_saved_scope;
7302 free_saved_scope = s;
7303 }
7304
7305 /* Push into the scope of the namespace NS, even if it is deeply
7306 nested within another namespace. */
7307
7308 static void
7309 do_push_nested_namespace (tree ns)
7310 {
7311 if (ns == global_namespace)
7312 do_push_to_top_level ();
7313 else
7314 {
7315 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
7316 gcc_checking_assert
7317 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
7318 resume_scope (NAMESPACE_LEVEL (ns));
7319 current_namespace = ns;
7320 }
7321 }
7322
7323 /* Pop back from the scope of the namespace NS, which was previously
7324 entered with push_nested_namespace. */
7325
7326 static void
7327 do_pop_nested_namespace (tree ns)
7328 {
7329 while (ns != global_namespace)
7330 {
7331 ns = CP_DECL_CONTEXT (ns);
7332 current_namespace = ns;
7333 leave_scope ();
7334 }
7335
7336 do_pop_from_top_level ();
7337 }
7338
7339 /* Add TARGET to USINGS, if it does not already exist there.
7340 We used to build the complete graph of usings at this point, from
7341 the POV of the source namespaces. Now we build that as we perform
7342 the unqualified search. */
7343
7344 static void
7345 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
7346 {
7347 if (usings)
7348 for (unsigned ix = usings->length (); ix--;)
7349 if ((*usings)[ix] == target)
7350 return;
7351
7352 vec_safe_push (usings, target);
7353 }
7354
7355 /* Tell the debug system of a using directive. */
7356
7357 static void
7358 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
7359 {
7360 /* Emit debugging info. */
7361 tree context = from != global_namespace ? from : NULL_TREE;
7362 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
7363 implicit);
7364 }
7365
7366 /* Process a using directive. */
7367
7368 void
7369 finish_using_directive (tree target, tree attribs)
7370 {
7371 if (target == error_mark_node)
7372 return;
7373
7374 if (current_binding_level->kind != sk_namespace)
7375 add_stmt (build_stmt (input_location, USING_STMT, target));
7376 else
7377 emit_debug_info_using_namespace (current_binding_level->this_entity,
7378 ORIGINAL_NAMESPACE (target), false);
7379
7380 add_using_namespace (current_binding_level->using_directives,
7381 ORIGINAL_NAMESPACE (target));
7382
7383 if (attribs != error_mark_node)
7384 for (tree a = attribs; a; a = TREE_CHAIN (a))
7385 {
7386 tree name = get_attribute_name (a);
7387 if (current_binding_level->kind == sk_namespace
7388 && is_attribute_p ("strong", name))
7389 {
7390 if (warning (0, "%<strong%> using directive no longer supported")
7391 && CP_DECL_CONTEXT (target) == current_namespace)
7392 inform (DECL_SOURCE_LOCATION (target),
7393 "you can use an inline namespace instead");
7394 }
7395 else
7396 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7397 }
7398 }
7399
7400 /* Pushes X into the global namespace. */
7401
7402 tree
7403 pushdecl_top_level (tree x)
7404 {
7405 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7406 do_push_to_top_level ();
7407 x = pushdecl_namespace_level (x);
7408 do_pop_from_top_level ();
7409 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7410 return x;
7411 }
7412
7413 /* Pushes X into the global namespace and calls cp_finish_decl to
7414 register the variable, initializing it with INIT. */
7415
7416 tree
7417 pushdecl_top_level_and_finish (tree x, tree init)
7418 {
7419 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7420 do_push_to_top_level ();
7421 x = pushdecl_namespace_level (x);
7422 cp_finish_decl (x, init, false, NULL_TREE, 0);
7423 do_pop_from_top_level ();
7424 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7425 return x;
7426 }
7427
7428 /* Enter the namespaces from current_namerspace to NS. */
7429
7430 static int
7431 push_inline_namespaces (tree ns)
7432 {
7433 int count = 0;
7434 if (ns != current_namespace)
7435 {
7436 gcc_assert (ns != global_namespace);
7437 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7438 resume_scope (NAMESPACE_LEVEL (ns));
7439 current_namespace = ns;
7440 count++;
7441 }
7442 return count;
7443 }
7444
7445 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7446 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7447 we create an inline namespace (it is up to the caller to check upon
7448 redefinition). Return the number of namespaces entered. */
7449
7450 int
7451 push_namespace (tree name, bool make_inline)
7452 {
7453 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7454 int count = 0;
7455
7456 /* We should not get here if the global_namespace is not yet constructed
7457 nor if NAME designates the global namespace: The global scope is
7458 constructed elsewhere. */
7459 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7460
7461 tree ns = NULL_TREE;
7462 {
7463 name_lookup lookup (name);
7464 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7465 ;
7466 else if (TREE_CODE (lookup.value) == TREE_LIST)
7467 {
7468 /* An ambiguous lookup. If exactly one is a namespace, we
7469 want that. If more than one is a namespace, error, but
7470 pick one of them. */
7471 /* DR2061 can cause us to find multiple namespaces of the same
7472 name. We must treat that carefully and avoid thinking we
7473 need to push a new (possibly) duplicate namespace. Hey,
7474 if you want to use the same identifier within an inline
7475 nest, knock yourself out. */
7476 for (tree *chain = &lookup.value, next; (next = *chain);)
7477 {
7478 tree decl = TREE_VALUE (next);
7479 if (TREE_CODE (decl) == NAMESPACE_DECL)
7480 {
7481 if (!ns)
7482 ns = decl;
7483 else if (SCOPE_DEPTH (ns) >= SCOPE_DEPTH (decl))
7484 ns = decl;
7485
7486 /* Advance. */
7487 chain = &TREE_CHAIN (next);
7488 }
7489 else
7490 /* Stitch out. */
7491 *chain = TREE_CHAIN (next);
7492 }
7493
7494 if (TREE_CHAIN (lookup.value))
7495 {
7496 error ("%<namespace %E%> is ambiguous", name);
7497 print_candidates (lookup.value);
7498 }
7499 }
7500 else if (TREE_CODE (lookup.value) == NAMESPACE_DECL)
7501 ns = lookup.value;
7502
7503 if (ns)
7504 if (tree dna = DECL_NAMESPACE_ALIAS (ns))
7505 {
7506 /* A namespace alias is not allowed here, but if the alias
7507 is for a namespace also inside the current scope,
7508 accept it with a diagnostic. That's better than dying
7509 horribly. */
7510 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7511 {
7512 error ("namespace alias %qD not allowed here, "
7513 "assuming %qD", ns, dna);
7514 ns = dna;
7515 }
7516 else
7517 ns = NULL_TREE;
7518 }
7519 }
7520
7521 bool new_ns = false;
7522 if (ns)
7523 /* DR2061. NS might be a member of an inline namespace. We
7524 need to push into those namespaces. */
7525 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7526 else
7527 {
7528 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7529 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7530 if (!SCOPE_DEPTH (ns))
7531 /* We only allow depth 255. */
7532 sorry ("cannot nest more than %d namespaces",
7533 SCOPE_DEPTH (current_namespace));
7534 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7535 new_ns = true;
7536
7537 if (pushdecl (ns) == error_mark_node)
7538 ns = NULL_TREE;
7539 else
7540 {
7541 if (!name)
7542 {
7543 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7544
7545 if (!make_inline)
7546 add_using_namespace (current_binding_level->using_directives,
7547 ns);
7548 }
7549 else if (TREE_PUBLIC (current_namespace))
7550 TREE_PUBLIC (ns) = 1;
7551
7552 if (make_inline)
7553 {
7554 DECL_NAMESPACE_INLINE_P (ns) = true;
7555 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7556 }
7557
7558 if (!name || make_inline)
7559 emit_debug_info_using_namespace (current_namespace, ns, true);
7560 }
7561 }
7562
7563 if (ns)
7564 {
7565 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7566 {
7567 error ("inline namespace must be specified at initial definition");
7568 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7569 }
7570 if (new_ns)
7571 begin_scope (sk_namespace, ns);
7572 else
7573 resume_scope (NAMESPACE_LEVEL (ns));
7574 current_namespace = ns;
7575 count++;
7576 }
7577
7578 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7579 return count;
7580 }
7581
7582 /* Pop from the scope of the current namespace. */
7583
7584 void
7585 pop_namespace (void)
7586 {
7587 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7588
7589 gcc_assert (current_namespace != global_namespace);
7590 current_namespace = CP_DECL_CONTEXT (current_namespace);
7591 /* The binding level is not popped, as it might be re-opened later. */
7592 leave_scope ();
7593
7594 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7595 }
7596
7597 /* External entry points for do_{push_to/pop_from}_top_level. */
7598
7599 void
7600 push_to_top_level (void)
7601 {
7602 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7603 do_push_to_top_level ();
7604 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7605 }
7606
7607 void
7608 pop_from_top_level (void)
7609 {
7610 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7611 do_pop_from_top_level ();
7612 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7613 }
7614
7615 /* External entry points for do_{push,pop}_nested_namespace. */
7616
7617 void
7618 push_nested_namespace (tree ns)
7619 {
7620 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7621 do_push_nested_namespace (ns);
7622 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7623 }
7624
7625 void
7626 pop_nested_namespace (tree ns)
7627 {
7628 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7629 gcc_assert (current_namespace == ns);
7630 do_pop_nested_namespace (ns);
7631 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7632 }
7633
7634 /* Pop off extraneous binding levels left over due to syntax errors.
7635 We don't pop past namespaces, as they might be valid. */
7636
7637 void
7638 pop_everything (void)
7639 {
7640 if (ENABLE_SCOPE_CHECKING)
7641 verbatim ("XXX entering %<pop_everything ()%>");
7642 while (!namespace_bindings_p ())
7643 {
7644 if (current_binding_level->kind == sk_class)
7645 pop_nested_class ();
7646 else
7647 poplevel (0, 0, 0);
7648 }
7649 if (ENABLE_SCOPE_CHECKING)
7650 verbatim ("XXX leaving %<pop_everything ()%>");
7651 }
7652
7653 /* Emit debugging information for using declarations and directives.
7654 If input tree is overloaded fn then emit debug info for all
7655 candidates. */
7656
7657 void
7658 cp_emit_debug_info_for_using (tree t, tree context)
7659 {
7660 /* Don't try to emit any debug information if we have errors. */
7661 if (seen_error ())
7662 return;
7663
7664 /* Do not supply context to imported_module_or_decl, if
7665 it is a global namespace. */
7666 if (context == global_namespace)
7667 context = NULL_TREE;
7668
7669 t = MAYBE_BASELINK_FUNCTIONS (t);
7670
7671 for (lkp_iterator iter (t); iter; ++iter)
7672 {
7673 tree fn = *iter;
7674
7675 if (TREE_CODE (fn) == TEMPLATE_DECL)
7676 /* FIXME: Handle TEMPLATE_DECLs. */
7677 continue;
7678
7679 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7680 of a builtin function. */
7681 if (TREE_CODE (fn) == FUNCTION_DECL
7682 && DECL_EXTERNAL (fn)
7683 && fndecl_built_in_p (fn))
7684 continue;
7685
7686 if (building_stmt_list_p ())
7687 add_stmt (build_stmt (input_location, USING_STMT, fn));
7688 else
7689 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7690 false, false);
7691 }
7692 }
7693
7694 /* Return the result of unqualified lookup for the overloaded operator
7695 designated by CODE, if we are in a template and the binding we find is
7696 not. */
7697
7698 static tree
7699 op_unqualified_lookup (tree fnname)
7700 {
7701 if (cxx_binding *binding = IDENTIFIER_BINDING (fnname))
7702 {
7703 cp_binding_level *l = binding->scope;
7704 while (l && !l->this_entity)
7705 l = l->level_chain;
7706
7707 if (l && uses_template_parms (l->this_entity))
7708 /* Don't preserve decls from an uninstantiated template,
7709 wait until that template is instantiated. */
7710 return NULL_TREE;
7711 }
7712
7713 tree fns = lookup_name (fnname);
7714 if (!fns)
7715 /* Remember we found nothing! */
7716 return error_mark_node;
7717
7718 tree d = is_overloaded_fn (fns) ? get_first_fn (fns) : fns;
7719 if (DECL_CLASS_SCOPE_P (d))
7720 /* We don't need to remember class-scope functions or declarations,
7721 normal unqualified lookup will find them again. */
7722 fns = NULL_TREE;
7723
7724 return fns;
7725 }
7726
7727 /* E is an expression representing an operation with dependent type, so we
7728 don't know yet whether it will use the built-in meaning of the operator or a
7729 function. Remember declarations of that operator in scope.
7730
7731 We then inject a fake binding of that lookup into the
7732 instantiation's parameter scope. This approach fails if the user
7733 has different using declarations or directives in different local
7734 binding of the current function from whence we need to do lookups
7735 (we'll cache what we see on the first lookup). */
7736
7737 static const char *const op_bind_attrname = "operator bindings";
7738
7739 void
7740 maybe_save_operator_binding (tree e)
7741 {
7742 /* This is only useful in a generic lambda. */
7743 if (!processing_template_decl)
7744 return;
7745
7746 tree cfn = current_function_decl;
7747 if (!cfn)
7748 return;
7749
7750 /* Do this for lambdas and code that will emit a CMI. In a module's
7751 GMF we don't yet know whether there will be a CMI. */
7752 if (!current_lambda_expr ())
7753 return;
7754
7755 tree fnname = ovl_op_identifier (false, TREE_CODE (e));
7756 if (!fnname)
7757 return;
7758
7759 tree attributes = DECL_ATTRIBUTES (cfn);
7760 tree op_attr = lookup_attribute (op_bind_attrname, attributes);
7761 if (!op_attr)
7762 {
7763 op_attr = tree_cons (get_identifier (op_bind_attrname),
7764 NULL_TREE, attributes);
7765 DECL_ATTRIBUTES (cfn) = op_attr;
7766 }
7767
7768 tree op_bind = purpose_member (fnname, TREE_VALUE (op_attr));
7769 if (!op_bind)
7770 {
7771 tree fns = op_unqualified_lookup (fnname);
7772
7773 /* Always record, so we don't keep looking for this
7774 operator. */
7775 TREE_VALUE (op_attr) = tree_cons (fnname, fns, TREE_VALUE (op_attr));
7776 }
7777 }
7778
7779 /* Called from cp_free_lang_data so we don't put this into LTO. */
7780
7781 void
7782 discard_operator_bindings (tree decl)
7783 {
7784 DECL_ATTRIBUTES (decl) = remove_attribute (op_bind_attrname,
7785 DECL_ATTRIBUTES (decl));
7786 }
7787
7788 /* Subroutine of start_preparsed_function: push the bindings we saved away in
7789 maybe_save_op_lookup into the function parameter binding level. */
7790
7791 void
7792 push_operator_bindings ()
7793 {
7794 tree decl1 = current_function_decl;
7795 if (tree attr = lookup_attribute (op_bind_attrname,
7796 DECL_ATTRIBUTES (decl1)))
7797 for (tree binds = TREE_VALUE (attr); binds; binds = TREE_CHAIN (binds))
7798 if (tree val = TREE_VALUE (binds))
7799 {
7800 tree name = TREE_PURPOSE (binds);
7801 push_local_binding (name, val, /*using*/true);
7802 }
7803 }
7804
7805 #include "gt-cp-name-lookup.h"