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