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