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