1/* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
40static cxx_binding *cxx_binding_make (tree value, tree type);
41static cp_binding_level *innermost_nonclass_level (void);
42static void set_identifier_type_value_with_scope (tree id, tree decl,
43 cp_binding_level *b);
44
45/* Create an overload suitable for recording an artificial TYPE_DECL
46 and another decl. We use this machanism to implement the struct
47 stat hack within a namespace. It'd be nice to use it everywhere. */
48
49#define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
50#define STAT_TYPE(N) TREE_TYPE (N)
51#define STAT_DECL(N) OVL_FUNCTION (N)
52#define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
53#define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
54
55/* Create a STAT_HACK node with DECL as the value binding and TYPE as
56 the type binding. */
57
58static tree
59stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
60{
61 tree result = make_node (OVERLOAD);
62
63 /* Mark this as a lookup, so we can tell this is a stat hack. */
64 OVL_LOOKUP_P (result) = true;
65 STAT_DECL (result) = decl;
66 STAT_TYPE (result) = type;
67 return result;
68}
69
70/* Create a local binding level for NAME. */
71
72static cxx_binding *
73create_local_binding (cp_binding_level *level, tree name)
74{
75 cxx_binding *binding = cxx_binding_make (NULL, NULL);
76
77 INHERITED_VALUE_BINDING_P (binding) = false;
78 LOCAL_BINDING_P (binding) = true;
79 binding->scope = level;
80 binding->previous = IDENTIFIER_BINDING (name);
81
82 IDENTIFIER_BINDING (name) = binding;
83
84 return binding;
85}
86
87/* Find the binding for NAME in namespace NS. If CREATE_P is true,
88 make an empty binding if there wasn't one. */
89
90static tree *
91find_namespace_slot (tree ns, tree name, bool create_p = false)
92{
93 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
94 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
95 create_p ? INSERT : NO_INSERT);
96 return slot;
97}
98
99static tree
100find_namespace_value (tree ns, tree name)
101{
102 tree *b = find_namespace_slot (ns, name);
103
104 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
105}
106
107/* Add DECL to the list of things declared in B. */
108
109static void
110add_decl_to_level (cp_binding_level *b, tree decl)
111{
112 gcc_assert (b->kind != sk_class);
113
114 /* Make sure we don't create a circular list. xref_tag can end
115 up pushing the same artificial decl more than once. We
116 should have already detected that in update_binding. */
117 gcc_assert (b->names != decl);
118
119 /* We build up the list in reverse order, and reverse it later if
120 necessary. */
121 TREE_CHAIN (decl) = b->names;
122 b->names = decl;
123
124 /* If appropriate, add decl to separate list of statics. We
125 include extern variables because they might turn out to be
126 static later. It's OK for this list to contain a few false
127 positives. */
128 if (b->kind == sk_namespace
129 && ((VAR_P (decl)
130 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
131 || (TREE_CODE (decl) == FUNCTION_DECL
132 && (!TREE_PUBLIC (decl)
133 || decl_anon_ns_mem_p (decl)
134 || DECL_DECLARED_INLINE_P (decl)))))
135 vec_safe_push (static_decls, decl);
136}
137
138/* Find the binding for NAME in the local binding level B. */
139
140static cxx_binding *
141find_local_binding (cp_binding_level *b, tree name)
142{
143 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
144 for (;; b = b->level_chain)
145 {
146 if (binding->scope == b
147 && !(VAR_P (binding->value)
148 && DECL_DEAD_FOR_LOCAL (binding->value)))
149 return binding;
150
151 /* Cleanup contours are transparent to the language. */
152 if (b->kind != sk_cleanup)
153 break;
154 }
155 return NULL;
156}
157
158struct name_lookup
159{
160public:
161 typedef std::pair<tree, tree> using_pair;
162 typedef vec<using_pair, va_heap, vl_embed> using_queue;
163
164public:
165 tree name; /* The identifier being looked for. */
166 tree value; /* A (possibly ambiguous) set of things found. */
167 tree type; /* A type that has been found. */
168 int flags; /* Lookup flags. */
169 bool deduping; /* Full deduping is needed because using declarations
170 are in play. */
171 vec<tree, va_heap, vl_embed> *scopes;
172 name_lookup *previous; /* Previously active lookup. */
173
174protected:
175 /* Marked scope stack for outermost name lookup. */
176 static vec<tree, va_heap, vl_embed> *shared_scopes;
177 /* Currently active lookup. */
178 static name_lookup *active;
179
180public:
181 name_lookup (tree n, int f = 0)
182 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
183 deduping (false), scopes (NULL), previous (NULL)
184 {
185 preserve_state ();
186 }
187 ~name_lookup ()
188 {
189 restore_state ();
190 }
191
192private: /* Uncopyable, unmovable, unassignable. I am a rock. */
193 name_lookup (const name_lookup &);
194 name_lookup &operator= (const name_lookup &);
195
196protected:
197 static bool seen_p (tree scope)
198 {
199 return LOOKUP_SEEN_P (scope);
200 }
201 static bool found_p (tree scope)
202 {
203 return LOOKUP_FOUND_P (scope);
204 }
205
206 void mark_seen (tree scope); /* Mark and add to scope vector. */
207 static void mark_found (tree scope)
208 {
209 gcc_checking_assert (seen_p (scope));
210 LOOKUP_FOUND_P (scope) = true;
211 }
212 bool see_and_mark (tree scope)
213 {
214 bool ret = seen_p (scope);
215 if (!ret)
216 mark_seen (scope);
217 return ret;
218 }
219 bool find_and_mark (tree scope);
220
221private:
222 void preserve_state ();
223 void restore_state ();
224
225private:
226 static tree ambiguous (tree thing, tree current);
227 void add_overload (tree fns);
228 void add_value (tree new_val);
229 void add_type (tree new_type);
230 bool process_binding (tree val_bind, tree type_bind);
231
232 /* Look in only namespace. */
233 bool search_namespace_only (tree scope);
234 /* Look in namespace and its (recursive) inlines. Ignore using
235 directives. Return true if something found (inc dups). */
236 bool search_namespace (tree scope);
237 /* Look in the using directives of namespace + inlines using
238 qualified lookup rules. */
239 bool search_usings (tree scope);
240
241private:
242 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
243 using_queue *do_queue_usings (using_queue *queue, int depth,
244 vec<tree, va_gc> *usings);
245 using_queue *queue_usings (using_queue *queue, int depth,
246 vec<tree, va_gc> *usings)
247 {
248 if (usings)
249 queue = do_queue_usings (queue, depth, usings);
250 return queue;
251 }
252
253private:
254 void add_fns (tree);
255
256 void adl_expr (tree);
257 void adl_type (tree);
258 void adl_template_arg (tree);
259 void adl_class (tree);
260 void adl_bases (tree);
261 void adl_class_only (tree);
262 void adl_namespace (tree);
263 void adl_namespace_only (tree);
264
265public:
266 /* Search namespace + inlines + maybe usings as qualified lookup. */
267 bool search_qualified (tree scope, bool usings = true);
268
269 /* Search namespace + inlines + usings as unqualified lookup. */
270 bool search_unqualified (tree scope, cp_binding_level *);
271
272 /* ADL lookup of ARGS. */
273 tree search_adl (tree fns, vec<tree, va_gc> *args);
274};
275
276/* Scope stack shared by all outermost lookups. This avoids us
277 allocating and freeing on every single lookup. */
278vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
279
280/* Currently active lookup. */
281name_lookup *name_lookup::active;
282
283/* Name lookup is recursive, becase ADL can cause template
284 instatiation. This is of course a rare event, so we optimize for
285 it not happening. When we discover an active name-lookup, which
286 must be an ADL lookup, we need to unmark the marked scopes and also
287 unmark the lookup we might have been accumulating. */
288
289void
290name_lookup::preserve_state ()
291{
292 previous = active;
293 if (previous)
294 {
295 unsigned length = vec_safe_length (previous->scopes);
296 vec_safe_reserve (previous->scopes, length * 2);
297 for (unsigned ix = length; ix--;)
298 {
299 tree decl = (*previous->scopes)[ix];
300
301 gcc_checking_assert (LOOKUP_SEEN_P (decl));
302 LOOKUP_SEEN_P (decl) = false;
303
304 /* Preserve the FOUND_P state on the interrupted lookup's
305 stack. */
306 if (LOOKUP_FOUND_P (decl))
307 {
308 LOOKUP_FOUND_P (decl) = false;
309 previous->scopes->quick_push (decl);
310 }
311 }
312
313 /* Unmark the outer partial lookup. */
314 if (previous->deduping)
315 lookup_mark (previous->value, false);
316 }
317 else
318 scopes = shared_scopes;
319 active = this;
320}
321
322/* Restore the marking state of a lookup we interrupted. */
323
324void
325name_lookup::restore_state ()
326{
327 if (deduping)
328 lookup_mark (value, false);
329
330 /* Unmark and empty this lookup's scope stack. */
331 for (unsigned ix = vec_safe_length (scopes); ix--;)
332 {
333 tree decl = scopes->pop ();
334 gcc_checking_assert (LOOKUP_SEEN_P (decl));
335 LOOKUP_SEEN_P (decl) = false;
336 LOOKUP_FOUND_P (decl) = false;
337 }
338
339 active = previous;
340 if (previous)
341 {
342 free (scopes);
343
344 unsigned length = vec_safe_length (previous->scopes);
345 for (unsigned ix = 0; ix != length; ix++)
346 {
347 tree decl = (*previous->scopes)[ix];
348 if (LOOKUP_SEEN_P (decl))
349 {
350 /* The remainder of the scope stack must be recording
351 FOUND_P decls, which we want to pop off. */
352 do
353 {
354 tree decl = previous->scopes->pop ();
355 gcc_checking_assert (LOOKUP_SEEN_P (decl)
356 && !LOOKUP_FOUND_P (decl));
357 LOOKUP_FOUND_P (decl) = true;
358 }
359 while (++ix != length);
360 break;
361 }
362
363 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
364 LOOKUP_SEEN_P (decl) = true;
365 }
366
367 /* Remark the outer partial lookup. */
368 if (previous->deduping)
369 lookup_mark (previous->value, true);
370 }
371 else
372 shared_scopes = scopes;
373}
374
375void
376name_lookup::mark_seen (tree scope)
377{
378 gcc_checking_assert (!seen_p (scope));
379 LOOKUP_SEEN_P (scope) = true;
380 vec_safe_push (scopes, scope);
381}
382
383bool
384name_lookup::find_and_mark (tree scope)
385{
386 bool result = LOOKUP_FOUND_P (scope);
387 if (!result)
388 {
389 LOOKUP_FOUND_P (scope) = true;
390 if (!LOOKUP_SEEN_P (scope))
391 vec_safe_push (scopes, scope);
392 }
393
394 return result;
395}
396
397/* THING and CURRENT are ambiguous, concatenate them. */
398
399tree
400name_lookup::ambiguous (tree thing, tree current)
401{
402 if (TREE_CODE (current) != TREE_LIST)
403 {
404 current = build_tree_list (NULL_TREE, current);
405 TREE_TYPE (current) = error_mark_node;
406 }
407 current = tree_cons (NULL_TREE, thing, current);
408 TREE_TYPE (current) = error_mark_node;
409
410 return current;
411}
412
413/* FNS is a new overload set to add to the exising set. */
414
415void
416name_lookup::add_overload (tree fns)
417{
418 if (!deduping && TREE_CODE (fns) == OVERLOAD)
419 {
420 tree probe = fns;
421 if (flags & LOOKUP_HIDDEN)
422 probe = ovl_skip_hidden (probe);
423 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
424 {
425 /* We're about to add something found by a using
426 declaration, so need to engage deduping mode. */
427 lookup_mark (value, true);
428 deduping = true;
429 }
430 }
431
432 value = lookup_maybe_add (fns, value, deduping);
433}
434
435/* Add a NEW_VAL, a found value binding into the current value binding. */
436
437void
438name_lookup::add_value (tree new_val)
439{
440 if (OVL_P (new_val) && (!value || OVL_P (value)))
441 add_overload (new_val);
442 else if (!value)
443 value = new_val;
444 else if (value == new_val)
445 ;
446 else if ((TREE_CODE (value) == TYPE_DECL
447 && TREE_CODE (new_val) == TYPE_DECL
448 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
449 /* Typedefs to the same type. */;
450 else if (TREE_CODE (value) == NAMESPACE_DECL
451 && TREE_CODE (new_val) == NAMESPACE_DECL
452 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
453 /* Namespace (possibly aliased) to the same namespace. Locate
454 the namespace*/
455 value = ORIGINAL_NAMESPACE (value);
456 else
457 {
458 if (deduping)
459 {
460 /* Disengage deduping mode. */
461 lookup_mark (value, false);
462 deduping = false;
463 }
464 value = ambiguous (new_val, value);
465 }
466}
467
468/* Add a NEW_TYPE, a found type binding into the current type binding. */
469
470void
471name_lookup::add_type (tree new_type)
472{
473 if (!type)
474 type = new_type;
475 else if (TREE_CODE (type) == TREE_LIST
476 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
477 type = ambiguous (new_type, type);
478}
479
480/* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
481 true if we actually found something noteworthy. */
482
483bool
484name_lookup::process_binding (tree new_val, tree new_type)
485{
486 /* Did we really see a type? */
487 if (new_type
488 && (LOOKUP_NAMESPACES_ONLY (flags)
489 || (!(flags & LOOKUP_HIDDEN)
490 && DECL_LANG_SPECIFIC (new_type)
491 && DECL_ANTICIPATED (new_type))))
492 new_type = NULL_TREE;
493
494 if (new_val && !(flags & LOOKUP_HIDDEN))
495 new_val = ovl_skip_hidden (new_val);
496
497 /* Do we really see a value? */
498 if (new_val)
499 switch (TREE_CODE (new_val))
500 {
501 case TEMPLATE_DECL:
502 /* If we expect types or namespaces, and not templates,
503 or this is not a template class. */
504 if ((LOOKUP_QUALIFIERS_ONLY (flags)
505 && !DECL_TYPE_TEMPLATE_P (new_val)))
506 new_val = NULL_TREE;
507 break;
508 case TYPE_DECL:
509 if (LOOKUP_NAMESPACES_ONLY (flags)
510 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
511 new_val = NULL_TREE;
512 break;
513 case NAMESPACE_DECL:
514 if (LOOKUP_TYPES_ONLY (flags))
515 new_val = NULL_TREE;
516 break;
517 default:
518 if (LOOKUP_QUALIFIERS_ONLY (flags))
519 new_val = NULL_TREE;
520 }
521
522 if (!new_val)
523 {
524 new_val = new_type;
525 new_type = NULL_TREE;
526 }
527
528 /* Merge into the lookup */
529 if (new_val)
530 add_value (new_val);
531 if (new_type)
532 add_type (new_type);
533
534 return new_val != NULL_TREE;
535}
536
537/* Look in exactly namespace SCOPE. */
538
539bool
540name_lookup::search_namespace_only (tree scope)
541{
542 bool found = false;
543
544 if (tree *binding = find_namespace_slot (scope, name))
545 found |= process_binding (MAYBE_STAT_DECL (*binding),
546 MAYBE_STAT_TYPE (*binding));
547
548 return found;
549}
550
551/* Conditionally look in namespace SCOPE and inline children. */
552
553bool
554name_lookup::search_namespace (tree scope)
555{
556 if (see_and_mark (scope))
557 /* We've visited this scope before. Return what we found then. */
558 return found_p (scope);
559
560 /* Look in exactly namespace. */
561 bool found = search_namespace_only (scope);
562
563 /* Recursively look in its inline children. */
564 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
565 for (unsigned ix = inlinees->length (); ix--;)
566 found |= search_namespace ((*inlinees)[ix]);
567
568 if (found)
569 mark_found (scope);
570
571 return found;
572}
573
574/* Recursively follow using directives of SCOPE & its inline children.
575 Such following is essentially a flood-fill algorithm. */
576
577bool
578name_lookup::search_usings (tree scope)
579{
580 /* We do not check seen_p here, as that was already set during the
581 namespace_only walk. */
582 if (found_p (scope))
583 return true;
584
585 bool found = false;
586 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
587 for (unsigned ix = usings->length (); ix--;)
588 found |= search_qualified ((*usings)[ix], true);
589
590 /* Look in its inline children. */
591 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
592 for (unsigned ix = inlinees->length (); ix--;)
593 found |= search_usings ((*inlinees)[ix]);
594
595 if (found)
596 mark_found (scope);
597
598 return found;
599}
600
601/* Qualified namespace lookup in SCOPE.
602 1) Look in SCOPE (+inlines). If found, we're done.
603 2) Otherwise, if USINGS is true,
604 recurse for every using directive of SCOPE (+inlines).
605
606 Trickiness is (a) loops and (b) multiple paths to same namespace.
607 In both cases we want to not repeat any lookups, and know whether
608 to stop the caller's step #2. Do this via the FOUND_P marker. */
609
610bool
611name_lookup::search_qualified (tree scope, bool usings)
612{
613 bool found = false;
614
615 if (seen_p (scope))
616 found = found_p (scope);
617 else
618 {
619 found = search_namespace (scope);
620 if (!found && usings)
621 found = search_usings (scope);
622 }
623
624 return found;
625}
626
627/* Add SCOPE to the unqualified search queue, recursively add its
628 inlines and those via using directives. */
629
630name_lookup::using_queue *
631name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
632{
633 if (see_and_mark (scope))
634 return queue;
635
636 /* Record it. */
637 tree common = scope;
638 while (SCOPE_DEPTH (common) > depth)
639 common = CP_DECL_CONTEXT (common);
640 vec_safe_push (queue, using_pair (common, scope));
641
642 /* Queue its inline children. */
643 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
644 for (unsigned ix = inlinees->length (); ix--;)
645 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
646
647 /* Queue its using targets. */
648 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
649
650 return queue;
651}
652
653/* Add the namespaces in USINGS to the unqualified search queue. */
654
655name_lookup::using_queue *
656name_lookup::do_queue_usings (using_queue *queue, int depth,
657 vec<tree, va_gc> *usings)
658{
659 for (unsigned ix = usings->length (); ix--;)
660 queue = queue_namespace (queue, depth, (*usings)[ix]);
661
662 return queue;
663}
664
665/* Unqualified namespace lookup in SCOPE.
666 1) add scope+inlins to worklist.
667 2) recursively add target of every using directive
668 3) for each worklist item where SCOPE is common ancestor, search it
669 4) if nothing find, scope=parent, goto 1. */
670
671bool
672name_lookup::search_unqualified (tree scope, cp_binding_level *level)
673{
674 /* Make static to avoid continual reallocation. We're not
675 recursive. */
676 static using_queue *queue = NULL;
677 bool found = false;
678 int length = vec_safe_length (queue);
679
680 /* Queue local using-directives. */
681 for (; level->kind != sk_namespace; level = level->level_chain)
682 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
683
684 for (; !found; scope = CP_DECL_CONTEXT (scope))
685 {
686 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
687 int depth = SCOPE_DEPTH (scope);
688
689 /* Queue namespaces reachable from SCOPE. */
690 queue = queue_namespace (queue, depth, scope);
691
692 /* Search every queued namespace where SCOPE is the common
693 ancestor. Adjust the others. */
694 unsigned ix = length;
695 do
696 {
697 using_pair &pair = (*queue)[ix];
698 while (pair.first == scope)
699 {
700 found |= search_namespace_only (pair.second);
701 pair = queue->pop ();
702 if (ix == queue->length ())
703 goto done;
704 }
705 /* The depth is the same as SCOPE, find the parent scope. */
706 if (SCOPE_DEPTH (pair.first) == depth)
707 pair.first = CP_DECL_CONTEXT (pair.first);
708 ix++;
709 }
710 while (ix < queue->length ());
711 done:;
712 if (scope == global_namespace)
713 break;
714 }
715
716 vec_safe_truncate (queue, length);
717
718 return found;
719}
720
721/* FNS is a value binding. If it is a (set of overloaded) functions,
722 add them into the current value. */
723
724void
725name_lookup::add_fns (tree fns)
726{
727 if (!fns)
728 return;
729 else if (TREE_CODE (fns) == OVERLOAD)
730 {
731 if (TREE_TYPE (fns) != unknown_type_node)
732 fns = OVL_FUNCTION (fns);
733 }
734 else if (!DECL_DECLARES_FUNCTION_P (fns))
735 return;
736
737 add_overload (fns);
738}
739
740/* Add functions of a namespace to the lookup structure. */
741
742void
743name_lookup::adl_namespace_only (tree scope)
744{
745 mark_seen (scope);
746
747 /* Look down into inline namespaces. */
748 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
749 for (unsigned ix = inlinees->length (); ix--;)
750 adl_namespace_only ((*inlinees)[ix]);
751
752 if (tree fns = find_namespace_value (scope, name))
753 add_fns (ovl_skip_hidden (fns));
754}
755
756/* Find the containing non-inlined namespace, add it and all its
757 inlinees. */
758
759void
760name_lookup::adl_namespace (tree scope)
761{
762 if (seen_p (scope))
763 return;
764
765 /* Find the containing non-inline namespace. */
766 while (DECL_NAMESPACE_INLINE_P (scope))
767 scope = CP_DECL_CONTEXT (scope);
768
769 adl_namespace_only (scope);
770}
771
772/* Adds the class and its friends to the lookup structure. */
773
774void
775name_lookup::adl_class_only (tree type)
776{
777 /* Backend-built structures, such as __builtin_va_list, aren't
778 affected by all this. */
779 if (!CLASS_TYPE_P (type))
780 return;
781
782 type = TYPE_MAIN_VARIANT (type);
783
784 if (see_and_mark (type))
785 return;
786
787 tree context = decl_namespace_context (type);
788 adl_namespace (context);
789
790 complete_type (type);
791
792 /* Add friends. */
793 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
794 list = TREE_CHAIN (list))
795 if (name == FRIEND_NAME (list))
796 for (tree friends = FRIEND_DECLS (list); friends;
797 friends = TREE_CHAIN (friends))
798 {
799 tree fn = TREE_VALUE (friends);
800
801 /* Only interested in global functions with potentially hidden
802 (i.e. unqualified) declarations. */
803 if (CP_DECL_CONTEXT (fn) != context)
804 continue;
805
806 /* Only interested in anticipated friends. (Non-anticipated
807 ones will have been inserted during the namespace
808 adl.) */
809 if (!DECL_ANTICIPATED (fn))
810 continue;
811
812 /* Template specializations are never found by name lookup.
813 (Templates themselves can be found, but not template
814 specializations.) */
815 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
816 continue;
817
818 add_fns (fn);
819 }
820}
821
822/* Adds the class and its bases to the lookup structure.
823 Returns true on error. */
824
825void
826name_lookup::adl_bases (tree type)
827{
828 adl_class_only (type);
829
830 /* Process baseclasses. */
831 if (tree binfo = TYPE_BINFO (type))
832 {
833 tree base_binfo;
834 int i;
835
836 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
837 adl_bases (BINFO_TYPE (base_binfo));
838 }
839}
840
841/* Adds everything associated with a class argument type to the lookup
842 structure. Returns true on error.
843
844 If T is a class type (including unions), its associated classes are: the
845 class itself; the class of which it is a member, if any; and its direct
846 and indirect base classes. Its associated namespaces are the namespaces
847 of which its associated classes are members. Furthermore, if T is a
848 class template specialization, its associated namespaces and classes
849 also include: the namespaces and classes associated with the types of
850 the template arguments provided for template type parameters (excluding
851 template template parameters); the namespaces of which any template
852 template arguments are members; and the classes of which any member
853 templates used as template template arguments are members. [ Note:
854 non-type template arguments do not contribute to the set of associated
855 namespaces. --end note] */
856
857void
858name_lookup::adl_class (tree type)
859{
860 /* Backend build structures, such as __builtin_va_list, aren't
861 affected by all this. */
862 if (!CLASS_TYPE_P (type))
863 return;
864
865 type = TYPE_MAIN_VARIANT (type);
866 /* We don't set found here because we have to have set seen first,
867 which is done in the adl_bases walk. */
868 if (found_p (type))
869 return;
870
871 adl_bases (type);
872 mark_found (type);
873
874 if (TYPE_CLASS_SCOPE_P (type))
875 adl_class_only (TYPE_CONTEXT (type));
876
877 /* Process template arguments. */
878 if (CLASSTYPE_TEMPLATE_INFO (type)
879 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
880 {
881 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
882 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
883 adl_template_arg (TREE_VEC_ELT (list, i));
884 }
885}
886
887void
888name_lookup::adl_expr (tree expr)
889{
890 if (!expr)
891 return;
892
893 gcc_assert (!TYPE_P (expr));
894
895 if (TREE_TYPE (expr) != unknown_type_node)
896 {
897 adl_type (TREE_TYPE (expr));
898 return;
899 }
900
901 if (TREE_CODE (expr) == ADDR_EXPR)
902 expr = TREE_OPERAND (expr, 0);
903 if (TREE_CODE (expr) == COMPONENT_REF
904 || TREE_CODE (expr) == OFFSET_REF)
905 expr = TREE_OPERAND (expr, 1);
906 expr = MAYBE_BASELINK_FUNCTIONS (expr);
907
908 if (OVL_P (expr))
909 for (lkp_iterator iter (expr); iter; ++iter)
910 adl_type (TREE_TYPE (*iter));
911 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
912 {
913 /* The working paper doesn't currently say how to handle
914 template-id arguments. The sensible thing would seem to be
915 to handle the list of template candidates like a normal
916 overload set, and handle the template arguments like we do
917 for class template specializations. */
918
919 /* First the templates. */
920 adl_expr (TREE_OPERAND (expr, 0));
921
922 /* Now the arguments. */
923 if (tree args = TREE_OPERAND (expr, 1))
924 for (int ix = TREE_VEC_LENGTH (args); ix--;)
925 adl_template_arg (TREE_VEC_ELT (args, ix));
926 }
927}
928
929void
930name_lookup::adl_type (tree type)
931{
932 if (!type)
933 return;
934
935 if (TYPE_PTRDATAMEM_P (type))
936 {
937 /* Pointer to member: associate class type and value type. */
938 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
939 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
940 return;
941 }
942
943 switch (TREE_CODE (type))
944 {
945 case RECORD_TYPE:
946 if (TYPE_PTRMEMFUNC_P (type))
947 {
948 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
949 return;
950 }
951 /* FALLTHRU */
952 case UNION_TYPE:
953 adl_class (type);
954 return;
955
956 case METHOD_TYPE:
957 /* The basetype is referenced in the first arg type, so just
958 fall through. */
959 case FUNCTION_TYPE:
960 /* Associate the parameter types. */
961 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
962 adl_type (TREE_VALUE (args));
963 /* FALLTHROUGH */
964
965 case POINTER_TYPE:
966 case REFERENCE_TYPE:
967 case ARRAY_TYPE:
968 adl_type (TREE_TYPE (type));
969 return;
970
971 case ENUMERAL_TYPE:
972 if (TYPE_CLASS_SCOPE_P (type))
973 adl_class_only (TYPE_CONTEXT (type));
974 adl_namespace (decl_namespace_context (type));
975 return;
976
977 case LANG_TYPE:
978 gcc_assert (type == unknown_type_node
979 || type == init_list_type_node);
980 return;
981
982 case TYPE_PACK_EXPANSION:
983 adl_type (PACK_EXPANSION_PATTERN (type));
984 return;
985
986 default:
987 break;
988 }
989}
990
991/* Adds everything associated with a template argument to the lookup
992 structure. */
993
994void
995name_lookup::adl_template_arg (tree arg)
996{
997 /* [basic.lookup.koenig]
998
999 If T is a template-id, its associated namespaces and classes are
1000 ... the namespaces and classes associated with the types of the
1001 template arguments provided for template type parameters
1002 (excluding template template parameters); the namespaces in which
1003 any template template arguments are defined; and the classes in
1004 which any member templates used as template template arguments
1005 are defined. [Note: non-type template arguments do not
1006 contribute to the set of associated namespaces. ] */
1007
1008 /* Consider first template template arguments. */
1009 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1010 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1011 ;
1012 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1013 {
1014 tree ctx = CP_DECL_CONTEXT (arg);
1015
1016 /* It's not a member template. */
1017 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1018 adl_namespace (ctx);
1019 /* Otherwise, it must be member template. */
1020 else
1021 adl_class_only (ctx);
1022 }
1023 /* It's an argument pack; handle it recursively. */
1024 else if (ARGUMENT_PACK_P (arg))
1025 {
1026 tree args = ARGUMENT_PACK_ARGS (arg);
1027 int i, len = TREE_VEC_LENGTH (args);
1028 for (i = 0; i < len; ++i)
1029 adl_template_arg (TREE_VEC_ELT (args, i));
1030 }
1031 /* It's not a template template argument, but it is a type template
1032 argument. */
1033 else if (TYPE_P (arg))
1034 adl_type (arg);
1035}
1036
1037/* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1038 the call arguments. */
1039
1040tree
1041name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1042{
1043 if (fns)
1044 {
1045 deduping = true;
1046 lookup_mark (fns, true);
1047 }
1048 value = fns;
1049
1050 unsigned ix;
1051 tree arg;
1052
1053 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1054 /* OMP reduction operators put an ADL-significant type as the
1055 first arg. */
1056 if (TYPE_P (arg))
1057 adl_type (arg);
1058 else
1059 adl_expr (arg);
1060
1061 fns = value;
1062
1063 return fns;
1064}
1065
1066static bool qualified_namespace_lookup (tree, name_lookup *);
1067static void consider_binding_level (tree name,
1068 best_match <tree, const char *> &bm,
1069 cp_binding_level *lvl,
1070 bool look_within_fields,
1071 enum lookup_name_fuzzy_kind kind);
1072static void diagnose_name_conflict (tree, tree);
1073
1074/* ADL lookup of NAME. FNS is the result of regular lookup, and we
1075 don't add duplicates to it. ARGS is the vector of call
1076 arguments (which will not be empty). */
1077
1078tree
1079lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1080{
1081 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1082 name_lookup lookup (name);
1083 fns = lookup.search_adl (fns, args);
1084 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1085 return fns;
1086}
1087
1088/* FNS is an overload set of conversion functions. Return the
1089 overloads converting to TYPE. */
1090
1091static tree
1092extract_conversion_operator (tree fns, tree type)
1093{
1094 tree convs = NULL_TREE;
1095 tree tpls = NULL_TREE;
1096
1097 for (ovl_iterator iter (fns); iter; ++iter)
1098 {
1099 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1100 convs = lookup_add (*iter, convs);
1101
1102 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1103 tpls = lookup_add (*iter, tpls);
1104 }
1105
1106 if (!convs)
1107 convs = tpls;
1108
1109 return convs;
1110}
1111
1112/* Binary search of (ordered) MEMBER_VEC for NAME. */
1113
1114static tree
1115member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1116{
1117 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1118 {
1119 unsigned mid = (lo + hi) / 2;
1120 tree binding = (*member_vec)[mid];
1121 tree binding_name = OVL_NAME (binding);
1122
1123 if (binding_name > name)
1124 hi = mid;
1125 else if (binding_name < name)
1126 lo = mid + 1;
1127 else
1128 return binding;
1129 }
1130
1131 return NULL_TREE;
1132}
1133
1134/* Linear search of (unordered) MEMBER_VEC for NAME. */
1135
1136static tree
1137member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1138{
1139 for (int ix = member_vec->length (); ix--;)
1140 /* We can get a NULL binding during insertion of a new method
1141 name, because the identifier_binding machinery performs a
1142 lookup. If we find such a NULL slot, that's the thing we were
1143 looking for, so we might as well bail out immediately. */
1144 if (tree binding = (*member_vec)[ix])
1145 {
1146 if (OVL_NAME (binding) == name)
1147 return binding;
1148 }
1149 else
1150 break;
1151
1152 return NULL_TREE;
1153}
1154
1155/* Linear search of (partially ordered) fields of KLASS for NAME. */
1156
1157static tree
1158fields_linear_search (tree klass, tree name, bool want_type)
1159{
1160 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1161 {
1162 tree decl = fields;
1163
1164 if (!want_type
1165 && TREE_CODE (decl) == FIELD_DECL
1166 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1167 {
1168 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name))
1169 return temp;
1170 }
1171
1172 if (DECL_NAME (decl) != name)
1173 continue;
1174
1175 if (TREE_CODE (decl) == USING_DECL)
1176 {
1177 decl = strip_using_decl (decl);
1178 if (is_overloaded_fn (decl))
1179 continue;
1180 }
1181
1182 if (DECL_DECLARES_FUNCTION_P (decl))
1183 /* Functions are found separately. */
1184 continue;
1185
1186 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1187 return decl;
1188 }
1189
1190 return NULL_TREE;
1191}
1192
1193/* Look for NAME field inside of anonymous aggregate ANON. */
1194
1195tree
1196search_anon_aggr (tree anon, tree name)
1197{
1198 gcc_assert (COMPLETE_TYPE_P (anon));
1199 tree ret;
1200
1201 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1202 ret = member_vec_linear_search (member_vec, name);
1203 else
1204 ret = fields_linear_search (anon, name, false);
1205
1206 if (ret)
1207 {
1208 /* Anon members can only contain fields. */
1209 gcc_assert (!STAT_HACK_P (ret) && !DECL_DECLARES_TYPE_P (ret));
1210 return ret;
1211 }
1212 return NULL_TREE;
1213}
1214
1215/* Look for NAME as an immediate member of KLASS (including
1216 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1217 regular search. >0 to get a type binding (if there is one) and <0
1218 if you want (just) the member function binding.
1219
1220 Use this if you do not want lazy member creation. */
1221
1222tree
1223get_class_binding_direct (tree klass, tree name, int type_or_fns)
1224{
1225 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1226
1227 /* Conversion operators can only be found by the marker conversion
1228 operator name. */
1229 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1230 tree lookup = conv_op ? conv_op_identifier : name;
1231 tree val = NULL_TREE;
1232 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1233
1234 if (COMPLETE_TYPE_P (klass) && member_vec)
1235 {
1236 val = member_vec_binary_search (member_vec, lookup);
1237 if (!val)
1238 ;
1239 else if (type_or_fns > 0)
1240 {
1241 if (STAT_HACK_P (val))
1242 val = STAT_TYPE (val);
1243 else if (!DECL_DECLARES_TYPE_P (val))
1244 val = NULL_TREE;
1245 }
1246 else if (STAT_HACK_P (val))
1247 val = STAT_DECL (val);
1248
1249 if (val && TREE_CODE (val) == OVERLOAD
1250 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1251 {
1252 /* An overload with a dependent USING_DECL. Does the caller
1253 want the USING_DECL or the functions? */
1254 if (type_or_fns < 0)
1255 val = OVL_CHAIN (val);
1256 else
1257 val = OVL_FUNCTION (val);
1258 }
1259 }
1260 else
1261 {
1262 if (member_vec && type_or_fns <= 0)
1263 val = member_vec_linear_search (member_vec, lookup);
1264
1265 if (type_or_fns < 0)
1266 /* Don't bother looking for field. We don't want it. */;
1267 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1268 /* Dependent using declarations are a 'field', make sure we
1269 return that even if we saw an overload already. */
1270 if (tree field_val = fields_linear_search (klass, lookup,
1271 type_or_fns > 0))
1272 if (!val || TREE_CODE (field_val) == USING_DECL)
1273 val = field_val;
1274 }
1275
1276 /* Extract the conversion operators asked for, unless the general
1277 conversion operator was requested. */
1278 if (val && conv_op)
1279 {
1280 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1281 val = OVL_CHAIN (val);
1282 if (tree type = TREE_TYPE (name))
1283 val = extract_conversion_operator (val, type);
1284 }
1285
1286 return val;
1287}
1288
1289/* Look for NAME's binding in exactly KLASS. See
1290 get_class_binding_direct for argument description. Does lazy
1291 special function creation as necessary. */
1292
1293tree
1294get_class_binding (tree klass, tree name, int type_or_fns)
1295{
1296 klass = complete_type (klass);
1297
1298 if (COMPLETE_TYPE_P (klass))
1299 {
1300 /* Lazily declare functions, if we're going to search these. */
1301 if (IDENTIFIER_CTOR_P (name))
1302 {
1303 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1304 lazily_declare_fn (sfk_constructor, klass);
1305 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1306 lazily_declare_fn (sfk_copy_constructor, klass);
1307 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1308 lazily_declare_fn (sfk_move_constructor, klass);
1309 }
1310 else if (IDENTIFIER_DTOR_P (name))
1311 {
1312 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1313 lazily_declare_fn (sfk_destructor, klass);
1314 }
1315 else if (name == assign_op_identifier)
1316 {
1317 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1318 lazily_declare_fn (sfk_copy_assignment, klass);
1319 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1320 lazily_declare_fn (sfk_move_assignment, klass);
1321 }
1322 }
1323
1324 return get_class_binding_direct (klass, name, type_or_fns);
1325}
1326
1327/* Find the slot containing overloads called 'NAME'. If there is no
1328 such slot, create an empty one. KLASS might be complete at this
1329 point, in which case we need to preserve ordering. Deals with
1330 conv_op marker handling. */
1331
1332tree *
1333get_member_slot (tree klass, tree name)
1334{
1335 bool complete_p = COMPLETE_TYPE_P (klass);
1336
1337 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1338 if (!member_vec)
1339 {
1340 vec_alloc (member_vec, 8);
1341 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1342 if (complete_p)
1343 {
1344 /* If the class is complete but had no member_vec, we need
1345 to add the TYPE_FIELDS into it. We're also most likely
1346 to be adding ctors & dtors, so ask for 6 spare slots (the
1347 abstract cdtors and their clones). */
1348 set_class_bindings (klass, 6);
1349 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1350 }
1351 }
1352
1353 if (IDENTIFIER_CONV_OP_P (name))
1354 name = conv_op_identifier;
1355
1356 unsigned ix, length = member_vec->length ();
1357 for (ix = 0; ix < length; ix++)
1358 {
1359 tree *slot = &(*member_vec)[ix];
1360 tree fn_name = OVL_NAME (*slot);
1361
1362 if (fn_name == name)
1363 {
1364 /* If we found an existing slot, it must be a function set.
1365 Even with insertion after completion, because those only
1366 happen with artificial fns that have unspellable names.
1367 This means we do not have to deal with the stat hack
1368 either. */
1369 gcc_checking_assert (OVL_P (*slot));
1370 if (name == conv_op_identifier)
1371 {
1372 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1373 /* Skip the conv-op marker. */
1374 slot = &OVL_CHAIN (*slot);
1375 }
1376 return slot;
1377 }
1378
1379 if (complete_p && fn_name > name)
1380 break;
1381 }
1382
1383 /* No slot found. Create one at IX. We know in this case that our
1384 caller will succeed in adding the function. */
1385 if (complete_p)
1386 {
1387 /* Do exact allocation when complete, as we don't expect to add
1388 many. */
1389 vec_safe_reserve_exact (member_vec, 1);
1390 member_vec->quick_insert (ix, NULL_TREE);
1391 }
1392 else
1393 {
1394 gcc_checking_assert (ix == length);
1395 vec_safe_push (member_vec, NULL_TREE);
1396 }
1397 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1398
1399 tree *slot = &(*member_vec)[ix];
1400 if (name == conv_op_identifier)
1401 {
1402 /* Install the marker prefix. */
1403 *slot = ovl_make (conv_op_marker, NULL_TREE);
1404 slot = &OVL_CHAIN (*slot);
1405 }
1406
1407 return slot;
1408}
1409
1410/* Comparison function to compare two MEMBER_VEC entries by name.
1411 Because we can have duplicates during insertion of TYPE_FIELDS, we
1412 do extra checking so deduping doesn't have to deal with so many
1413 cases. */
1414
1415static int
1416member_name_cmp (const void *a_p, const void *b_p)
1417{
1418 tree a = *(const tree *)a_p;
1419 tree b = *(const tree *)b_p;
1420 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1421 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1422
1423 gcc_checking_assert (name_a && name_b);
1424 if (name_a != name_b)
1425 return name_a < name_b ? -1 : +1;
1426
1427 if (name_a == conv_op_identifier)
1428 {
1429 /* Strip the conv-op markers. */
1430 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1431 && OVL_FUNCTION (b) == conv_op_marker);
1432 a = OVL_CHAIN (a);
1433 b = OVL_CHAIN (b);
1434 }
1435
1436 if (TREE_CODE (a) == OVERLOAD)
1437 a = OVL_FUNCTION (a);
1438 if (TREE_CODE (b) == OVERLOAD)
1439 b = OVL_FUNCTION (b);
1440
1441 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1442 if (TREE_CODE (a) != TREE_CODE (b))
1443 {
1444 /* If one of them is a TYPE_DECL, it loses. */
1445 if (TREE_CODE (a) == TYPE_DECL)
1446 return +1;
1447 else if (TREE_CODE (b) == TYPE_DECL)
1448 return -1;
1449
1450 /* If one of them is a USING_DECL, it loses. */
1451 if (TREE_CODE (a) == USING_DECL)
1452 return +1;
1453 else if (TREE_CODE (b) == USING_DECL)
1454 return -1;
1455
1456 /* There are no other cases with different kinds of decls, as
1457 duplicate detection should have kicked in earlier. However,
1458 some erroneous cases get though. */
1459 gcc_assert (errorcount);
1460 }
1461
1462 /* Using source location would be the best thing here, but we can
1463 get identically-located decls in the following circumstances:
1464
1465 1) duplicate artificial type-decls for the same type.
1466
1467 2) pack expansions of using-decls.
1468
1469 We should not be doing #1, but in either case it doesn't matter
1470 how we order these. Use UID as a proxy for source ordering, so
1471 that identically-located decls still have a well-defined stable
1472 ordering. */
1473 if (DECL_UID (a) != DECL_UID (b))
1474 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1475 gcc_assert (a == b);
1476 return 0;
1477}
1478
1479static struct {
1480 gt_pointer_operator new_value;
1481 void *cookie;
1482} resort_data;
1483
1484/* This routine compares two fields like member_name_cmp but using the
1485 pointer operator in resort_field_decl_data. We don't have to deal
1486 with duplicates here. */
1487
1488static int
1489resort_member_name_cmp (const void *a_p, const void *b_p)
1490{
1491 tree a = *(const tree *)a_p;
1492 tree b = *(const tree *)b_p;
1493 tree name_a = OVL_NAME (a);
1494 tree name_b = OVL_NAME (b);
1495
1496 resort_data.new_value (&name_a, resort_data.cookie);
1497 resort_data.new_value (&name_b, resort_data.cookie);
1498
1499 gcc_checking_assert (name_a != name_b);
1500
1501 return name_a < name_b ? -1 : +1;
1502}
1503
1504/* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1505
1506void
1507resort_type_member_vec (void *obj, void */*orig_obj*/,
1508 gt_pointer_operator new_value, void* cookie)
1509{
1510 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1511 {
1512 resort_data.new_value = new_value;
1513 resort_data.cookie = cookie;
1514 qsort (member_vec->address (), member_vec->length (),
1515 sizeof (tree), resort_member_name_cmp);
1516 }
1517}
1518
1519/* Recursively count the number of fields in KLASS, including anonymous
1520 union members. */
1521
1522static unsigned
1523count_class_fields (tree klass)
1524{
1525 unsigned n_fields = 0;
1526
1527 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1528 if (DECL_DECLARES_FUNCTION_P (fields))
1529 /* Functions are dealt with separately. */;
1530 else if (TREE_CODE (fields) == FIELD_DECL
1531 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1532 n_fields += count_class_fields (TREE_TYPE (fields));
1533 else if (DECL_NAME (fields))
1534 n_fields += 1;
1535
1536 return n_fields;
1537}
1538
1539/* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1540 Recurse for anonymous members. MEMBER_VEC must have space. */
1541
1542static void
1543member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1544{
1545 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1546 if (DECL_DECLARES_FUNCTION_P (fields))
1547 /* Functions are handled separately. */;
1548 else if (TREE_CODE (fields) == FIELD_DECL
1549 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1550 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1551 else if (DECL_NAME (fields))
1552 {
1553 tree field = fields;
1554 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1555 if (TREE_CODE (field) == USING_DECL
1556 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1557 field = ovl_make (conv_op_marker, field);
1558 member_vec->quick_push (field);
1559 }
1560}
1561
1562/* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1563 MEMBER_VEC must have space. */
1564
1565static void
1566member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1567{
1568 for (tree values = TYPE_VALUES (enumtype);
1569 values; values = TREE_CHAIN (values))
1570 member_vec->quick_push (TREE_VALUE (values));
1571}
1572
1573/* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1574 DeDup adjacent DECLS of the same name. We already dealt with
1575 conflict resolution when adding the fields or methods themselves.
1576 There are three cases (which could all be combined):
1577 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1578 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1579 it wins. Otherwise the OVERLOAD does.
1580 3) two USING_DECLS. ...
1581
1582 member_name_cmp will have ordered duplicates as
1583 <fns><using><type> */
1584
1585static void
1586member_vec_dedup (vec<tree, va_gc> *member_vec)
1587{
1588 unsigned len = member_vec->length ();
1589 unsigned store = 0;
1590
1591 tree current = (*member_vec)[0], name = OVL_NAME (current);
1592 tree next = NULL_TREE, next_name = NULL_TREE;
1593 for (unsigned jx, ix = 0; ix < len;
1594 ix = jx, current = next, name = next_name)
1595 {
1596 tree to_type = NULL_TREE;
1597 tree to_using = NULL_TREE;
1598 tree marker = NULL_TREE;
1599 if (IDENTIFIER_CONV_OP_P (name))
1600 {
1601 marker = current;
1602 current = OVL_CHAIN (current);
1603 name = DECL_NAME (OVL_FUNCTION (marker));
1604 gcc_checking_assert (name == conv_op_identifier);
1605 }
1606
1607 if (TREE_CODE (current) == USING_DECL)
1608 {
1609 current = strip_using_decl (current);
1610 if (is_overloaded_fn (current))
1611 current = NULL_TREE;
1612 else if (TREE_CODE (current) == USING_DECL)
1613 {
1614 to_using = current;
1615 current = NULL_TREE;
1616 }
1617 }
1618
1619 if (current && DECL_DECLARES_TYPE_P (current))
1620 {
1621 to_type = current;
1622 current = NULL_TREE;
1623 }
1624
1625 for (jx = ix + 1; jx < len; jx++)
1626 {
1627 next = (*member_vec)[jx];
1628 next_name = OVL_NAME (next);
1629 if (next_name != name)
1630 break;
1631
1632 if (marker)
1633 {
1634 gcc_checking_assert (OVL_FUNCTION (marker)
1635 == OVL_FUNCTION (next));
1636 next = OVL_CHAIN (next);
1637 }
1638
1639 if (TREE_CODE (next) == USING_DECL)
1640 {
1641 next = strip_using_decl (next);
1642 if (is_overloaded_fn (next))
1643 next = NULL_TREE;
1644 else if (TREE_CODE (next) == USING_DECL)
1645 {
1646 to_using = next;
1647 next = NULL_TREE;
1648 }
1649 }
1650
1651 if (next && DECL_DECLARES_TYPE_P (next))
1652 to_type = next;
1653 }
1654
1655 if (to_using)
1656 {
1657 if (!current)
1658 current = to_using;
1659 else
1660 current = ovl_make (to_using, current);
1661 }
1662
1663 if (to_type)
1664 {
1665 if (!current)
1666 current = to_type;
1667 else
1668 current = stat_hack (current, to_type);
1669 }
1670
1671 gcc_assert (current);
1672 if (marker)
1673 {
1674 OVL_CHAIN (marker) = current;
1675 current = marker;
1676 }
1677 (*member_vec)[store++] = current;
1678 }
1679
1680 while (store++ < len)
1681 member_vec->pop ();
1682}
1683
1684/* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1685 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1686 know there must be at least 1 field -- the self-reference
1687 TYPE_DECL, except for anon aggregates, which will have at least
1688 one field. */
1689
1690void
1691set_class_bindings (tree klass, unsigned extra)
1692{
1693 unsigned n_fields = count_class_fields (klass);
1694 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1695
1696 if (member_vec || n_fields >= 8)
1697 {
1698 /* Append the new fields. */
1699 vec_safe_reserve_exact (member_vec, extra + n_fields);
1700 member_vec_append_class_fields (member_vec, klass);
1701 }
1702
1703 if (member_vec)
1704 {
1705 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1706 qsort (member_vec->address (), member_vec->length (),
1707 sizeof (tree), member_name_cmp);
1708 member_vec_dedup (member_vec);
1709 }
1710}
1711
1712/* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1713
1714void
1715insert_late_enum_def_bindings (tree klass, tree enumtype)
1716{
1717 int n_fields;
1718 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1719
1720 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1721 count them twice. */
1722 if (!member_vec)
1723 n_fields = count_class_fields (klass);
1724 else
1725 n_fields = list_length (TYPE_VALUES (enumtype));
1726
1727 if (member_vec || n_fields >= 8)
1728 {
1729 vec_safe_reserve_exact (member_vec, n_fields);
1730 if (CLASSTYPE_MEMBER_VEC (klass))
1731 member_vec_append_enum_values (member_vec, enumtype);
1732 else
1733 member_vec_append_class_fields (member_vec, klass);
1734 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1735 qsort (member_vec->address (), member_vec->length (),
1736 sizeof (tree), member_name_cmp);
1737 member_vec_dedup (member_vec);
1738 }
1739}
1740
1741/* Compute the chain index of a binding_entry given the HASH value of its
1742 name and the total COUNT of chains. COUNT is assumed to be a power
1743 of 2. */
1744
1745#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1746
1747/* A free list of "binding_entry"s awaiting for re-use. */
1748
1749static GTY((deletable)) binding_entry free_binding_entry = NULL;
1750
1751/* The binding oracle; see cp-tree.h. */
1752
1753cp_binding_oracle_function *cp_binding_oracle;
1754
1755/* If we have a binding oracle, ask it for all namespace-scoped
1756 definitions of NAME. */
1757
1758static inline void
1759query_oracle (tree name)
1760{
1761 if (!cp_binding_oracle)
1762 return;
1763
1764 /* LOOKED_UP holds the set of identifiers that we have already
1765 looked up with the oracle. */
1766 static hash_set<tree> looked_up;
1767 if (looked_up.add (name))
1768 return;
1769
1770 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1771}
1772
1773/* Create a binding_entry object for (NAME, TYPE). */
1774
1775static inline binding_entry
1776binding_entry_make (tree name, tree type)
1777{
1778 binding_entry entry;
1779
1780 if (free_binding_entry)
1781 {
1782 entry = free_binding_entry;
1783 free_binding_entry = entry->chain;
1784 }
1785 else
1786 entry = ggc_alloc<binding_entry_s> ();
1787
1788 entry->name = name;
1789 entry->type = type;
1790 entry->chain = NULL;
1791
1792 return entry;
1793}
1794
1795/* Put ENTRY back on the free list. */
1796#if 0
1797static inline void
1798binding_entry_free (binding_entry entry)
1799{
1800 entry->name = NULL;
1801 entry->type = NULL;
1802 entry->chain = free_binding_entry;
1803 free_binding_entry = entry;
1804}
1805#endif
1806
1807/* The datatype used to implement the mapping from names to types at
1808 a given scope. */
1809struct GTY(()) binding_table_s {
1810 /* Array of chains of "binding_entry"s */
1811 binding_entry * GTY((length ("%h.chain_count"))) chain;
1812
1813 /* The number of chains in this table. This is the length of the
1814 member "chain" considered as an array. */
1815 size_t chain_count;
1816
1817 /* Number of "binding_entry"s in this table. */
1818 size_t entry_count;
1819};
1820
1821/* Construct TABLE with an initial CHAIN_COUNT. */
1822
1823static inline void
1824binding_table_construct (binding_table table, size_t chain_count)
1825{
1826 table->chain_count = chain_count;
1827 table->entry_count = 0;
1828 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1829}
1830
1831/* Make TABLE's entries ready for reuse. */
1832#if 0
1833static void
1834binding_table_free (binding_table table)
1835{
1836 size_t i;
1837 size_t count;
1838
1839 if (table == NULL)
1840 return;
1841
1842 for (i = 0, count = table->chain_count; i < count; ++i)
1843 {
1844 binding_entry temp = table->chain[i];
1845 while (temp != NULL)
1846 {
1847 binding_entry entry = temp;
1848 temp = entry->chain;
1849 binding_entry_free (entry);
1850 }
1851 table->chain[i] = NULL;
1852 }
1853 table->entry_count = 0;
1854}
1855#endif
1856
1857/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1858
1859static inline binding_table
1860binding_table_new (size_t chain_count)
1861{
1862 binding_table table = ggc_alloc<binding_table_s> ();
1863 table->chain = NULL;
1864 binding_table_construct (table, chain_count);
1865 return table;
1866}
1867
1868/* Expand TABLE to twice its current chain_count. */
1869
1870static void
1871binding_table_expand (binding_table table)
1872{
1873 const size_t old_chain_count = table->chain_count;
1874 const size_t old_entry_count = table->entry_count;
1875 const size_t new_chain_count = 2 * old_chain_count;
1876 binding_entry *old_chains = table->chain;
1877 size_t i;
1878
1879 binding_table_construct (table, new_chain_count);
1880 for (i = 0; i < old_chain_count; ++i)
1881 {
1882 binding_entry entry = old_chains[i];
1883 for (; entry != NULL; entry = old_chains[i])
1884 {
1885 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1886 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1887
1888 old_chains[i] = entry->chain;
1889 entry->chain = table->chain[j];
1890 table->chain[j] = entry;
1891 }
1892 }
1893 table->entry_count = old_entry_count;
1894}
1895
1896/* Insert a binding for NAME to TYPE into TABLE. */
1897
1898static void
1899binding_table_insert (binding_table table, tree name, tree type)
1900{
1901 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1902 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1903 binding_entry entry = binding_entry_make (name, type);
1904
1905 entry->chain = table->chain[i];
1906 table->chain[i] = entry;
1907 ++table->entry_count;
1908
1909 if (3 * table->chain_count < 5 * table->entry_count)
1910 binding_table_expand (table);
1911}
1912
1913/* Return the binding_entry, if any, that maps NAME. */
1914
1915binding_entry
1916binding_table_find (binding_table table, tree name)
1917{
1918 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1919 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1920
1921 while (entry != NULL && entry->name != name)
1922 entry = entry->chain;
1923
1924 return entry;
1925}
1926
1927/* Apply PROC -- with DATA -- to all entries in TABLE. */
1928
1929void
1930binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1931{
1932 size_t chain_count;
1933 size_t i;
1934
1935 if (!table)
1936 return;
1937
1938 chain_count = table->chain_count;
1939 for (i = 0; i < chain_count; ++i)
1940 {
1941 binding_entry entry = table->chain[i];
1942 for (; entry != NULL; entry = entry->chain)
1943 proc (entry, data);
1944 }
1945}
1946
1947#ifndef ENABLE_SCOPE_CHECKING
1948# define ENABLE_SCOPE_CHECKING 0
1949#else
1950# define ENABLE_SCOPE_CHECKING 1
1951#endif
1952
1953/* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1954
1955static GTY((deletable)) cxx_binding *free_bindings;
1956
1957/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1958 field to NULL. */
1959
1960static inline void
1961cxx_binding_init (cxx_binding *binding, tree value, tree type)
1962{
1963 binding->value = value;
1964 binding->type = type;
1965 binding->previous = NULL;
1966}
1967
1968/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1969
1970static cxx_binding *
1971cxx_binding_make (tree value, tree type)
1972{
1973 cxx_binding *binding;
1974 if (free_bindings)
1975 {
1976 binding = free_bindings;
1977 free_bindings = binding->previous;
1978 }
1979 else
1980 binding = ggc_alloc<cxx_binding> ();
1981
1982 cxx_binding_init (binding, value, type);
1983
1984 return binding;
1985}
1986
1987/* Put BINDING back on the free list. */
1988
1989static inline void
1990cxx_binding_free (cxx_binding *binding)
1991{
1992 binding->scope = NULL;
1993 binding->previous = free_bindings;
1994 free_bindings = binding;
1995}
1996
1997/* Create a new binding for NAME (with the indicated VALUE and TYPE
1998 bindings) in the class scope indicated by SCOPE. */
1999
2000static cxx_binding *
2001new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2002{
2003 cp_class_binding cb = {cxx_binding_make (value, type), name};
2004 cxx_binding *binding = cb.base;
2005 vec_safe_push (scope->class_shadowed, cb);
2006 binding->scope = scope;
2007 return binding;
2008}
2009
2010/* Make DECL the innermost binding for ID. The LEVEL is the binding
2011 level at which this declaration is being bound. */
2012
2013void
2014push_binding (tree id, tree decl, cp_binding_level* level)
2015{
2016 cxx_binding *binding;
2017
2018 if (level != class_binding_level)
2019 {
2020 binding = cxx_binding_make (decl, NULL_TREE);
2021 binding->scope = level;
2022 }
2023 else
2024 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2025
2026 /* Now, fill in the binding information. */
2027 binding->previous = IDENTIFIER_BINDING (id);
2028 INHERITED_VALUE_BINDING_P (binding) = 0;
2029 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2030
2031 /* And put it on the front of the list of bindings for ID. */
2032 IDENTIFIER_BINDING (id) = binding;
2033}
2034
2035/* Remove the binding for DECL which should be the innermost binding
2036 for ID. */
2037
2038void
2039pop_local_binding (tree id, tree decl)
2040{
2041 cxx_binding *binding;
2042
2043 if (id == NULL_TREE)
2044 /* It's easiest to write the loops that call this function without
2045 checking whether or not the entities involved have names. We
2046 get here for such an entity. */
2047 return;
2048
2049 /* Get the innermost binding for ID. */
2050 binding = IDENTIFIER_BINDING (id);
2051
2052 /* The name should be bound. */
2053 gcc_assert (binding != NULL);
2054
2055 /* The DECL will be either the ordinary binding or the type
2056 binding for this identifier. Remove that binding. */
2057 if (binding->value == decl)
2058 binding->value = NULL_TREE;
2059 else
2060 {
2061 gcc_assert (binding->type == decl);
2062 binding->type = NULL_TREE;
2063 }
2064
2065 if (!binding->value && !binding->type)
2066 {
2067 /* We're completely done with the innermost binding for this
2068 identifier. Unhook it from the list of bindings. */
2069 IDENTIFIER_BINDING (id) = binding->previous;
2070
2071 /* Add it to the free list. */
2072 cxx_binding_free (binding);
2073 }
2074}
2075
2076/* Remove the bindings for the decls of the current level and leave
2077 the current scope. */
2078
2079void
2080pop_bindings_and_leave_scope (void)
2081{
2082 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2083 {
2084 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2085 tree name = OVL_NAME (decl);
2086
2087 pop_local_binding (name, decl);
2088 }
2089
2090 leave_scope ();
2091}
2092
2093/* Strip non dependent using declarations. If DECL is dependent,
2094 surreptitiously create a typename_type and return it. */
2095
2096tree
2097strip_using_decl (tree decl)
2098{
2099 if (decl == NULL_TREE)
2100 return NULL_TREE;
2101
2102 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2103 decl = USING_DECL_DECLS (decl);
2104
2105 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2106 && USING_DECL_TYPENAME_P (decl))
2107 {
2108 /* We have found a type introduced by a using
2109 declaration at class scope that refers to a dependent
2110 type.
2111
2112 using typename :: [opt] nested-name-specifier unqualified-id ;
2113 */
2114 decl = make_typename_type (TREE_TYPE (decl),
2115 DECL_NAME (decl),
2116 typename_type, tf_error);
2117 if (decl != error_mark_node)
2118 decl = TYPE_NAME (decl);
2119 }
2120
2121 return decl;
2122}
2123
2124/* Return true if OVL is an overload for an anticipated builtin. */
2125
2126static bool
2127anticipated_builtin_p (tree ovl)
2128{
2129 if (TREE_CODE (ovl) != OVERLOAD)
2130 return false;
2131
2132 if (!OVL_HIDDEN_P (ovl))
2133 return false;
2134
2135 tree fn = OVL_FUNCTION (ovl);
2136 gcc_checking_assert (DECL_ANTICIPATED (fn));
2137
2138 if (DECL_HIDDEN_FRIEND_P (fn))
2139 return false;
2140
2141 return true;
2142}
2143
2144/* BINDING records an existing declaration for a name in the current scope.
2145 But, DECL is another declaration for that same identifier in the
2146 same scope. This is the `struct stat' hack whereby a non-typedef
2147 class name or enum-name can be bound at the same level as some other
2148 kind of entity.
2149 3.3.7/1
2150
2151 A class name (9.1) or enumeration name (7.2) can be hidden by the
2152 name of an object, function, or enumerator declared in the same scope.
2153 If a class or enumeration name and an object, function, or enumerator
2154 are declared in the same scope (in any order) with the same name, the
2155 class or enumeration name is hidden wherever the object, function, or
2156 enumerator name is visible.
2157
2158 It's the responsibility of the caller to check that
2159 inserting this name is valid here. Returns nonzero if the new binding
2160 was successful. */
2161
2162static bool
2163supplement_binding_1 (cxx_binding *binding, tree decl)
2164{
2165 tree bval = binding->value;
2166 bool ok = true;
2167 tree target_bval = strip_using_decl (bval);
2168 tree target_decl = strip_using_decl (decl);
2169
2170 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2171 && target_decl != target_bval
2172 && (TREE_CODE (target_bval) != TYPE_DECL
2173 /* We allow pushing an enum multiple times in a class
2174 template in order to handle late matching of underlying
2175 type on an opaque-enum-declaration followed by an
2176 enum-specifier. */
2177 || (processing_template_decl
2178 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2179 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2180 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2181 (TREE_TYPE (target_decl)))
2182 || dependent_type_p (ENUM_UNDERLYING_TYPE
2183 (TREE_TYPE (target_bval)))))))
2184 /* The new name is the type name. */
2185 binding->type = decl;
2186 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2187 an inherited type-binding out of the way to make room
2188 for a new value binding. */
2189 !target_bval
2190 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2191 has been used in a non-class scope prior declaration.
2192 In that case, we should have already issued a
2193 diagnostic; for graceful error recovery purpose, pretend
2194 this was the intended declaration for that name. */
2195 || target_bval == error_mark_node
2196 /* If TARGET_BVAL is anticipated but has not yet been
2197 declared, pretend it is not there at all. */
2198 || anticipated_builtin_p (target_bval))
2199 binding->value = decl;
2200 else if (TREE_CODE (target_bval) == TYPE_DECL
2201 && DECL_ARTIFICIAL (target_bval)
2202 && target_decl != target_bval
2203 && (TREE_CODE (target_decl) != TYPE_DECL
2204 || same_type_p (TREE_TYPE (target_decl),
2205 TREE_TYPE (target_bval))))
2206 {
2207 /* The old binding was a type name. It was placed in
2208 VALUE field because it was thought, at the point it was
2209 declared, to be the only entity with such a name. Move the
2210 type name into the type slot; it is now hidden by the new
2211 binding. */
2212 binding->type = bval;
2213 binding->value = decl;
2214 binding->value_is_inherited = false;
2215 }
2216 else if (TREE_CODE (target_bval) == TYPE_DECL
2217 && TREE_CODE (target_decl) == TYPE_DECL
2218 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2219 && binding->scope->kind != sk_class
2220 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2221 /* If either type involves template parameters, we must
2222 wait until instantiation. */
2223 || uses_template_parms (TREE_TYPE (target_decl))
2224 || uses_template_parms (TREE_TYPE (target_bval))))
2225 /* We have two typedef-names, both naming the same type to have
2226 the same name. In general, this is OK because of:
2227
2228 [dcl.typedef]
2229
2230 In a given scope, a typedef specifier can be used to redefine
2231 the name of any type declared in that scope to refer to the
2232 type to which it already refers.
2233
2234 However, in class scopes, this rule does not apply due to the
2235 stricter language in [class.mem] prohibiting redeclarations of
2236 members. */
2237 ok = false;
2238 /* There can be two block-scope declarations of the same variable,
2239 so long as they are `extern' declarations. However, there cannot
2240 be two declarations of the same static data member:
2241
2242 [class.mem]
2243
2244 A member shall not be declared twice in the
2245 member-specification. */
2246 else if (VAR_P (target_decl)
2247 && VAR_P (target_bval)
2248 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2249 && !DECL_CLASS_SCOPE_P (target_decl))
2250 {
2251 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2252 ok = false;
2253 }
2254 else if (TREE_CODE (decl) == NAMESPACE_DECL
2255 && TREE_CODE (bval) == NAMESPACE_DECL
2256 && DECL_NAMESPACE_ALIAS (decl)
2257 && DECL_NAMESPACE_ALIAS (bval)
2258 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2259 /* [namespace.alias]
2260
2261 In a declarative region, a namespace-alias-definition can be
2262 used to redefine a namespace-alias declared in that declarative
2263 region to refer only to the namespace to which it already
2264 refers. */
2265 ok = false;
2266 else
2267 {
2268 if (!error_operand_p (bval))
2269 diagnose_name_conflict (decl, bval);
2270 ok = false;
2271 }
2272
2273 return ok;
2274}
2275
2276/* Diagnose a name conflict between DECL and BVAL. */
2277
2278static void
2279diagnose_name_conflict (tree decl, tree bval)
2280{
2281 if (TREE_CODE (decl) == TREE_CODE (bval)
2282 && TREE_CODE (decl) != NAMESPACE_DECL
2283 && !DECL_DECLARES_FUNCTION_P (decl)
2284 && (TREE_CODE (decl) != TYPE_DECL
2285 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2286 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2287 error ("redeclaration of %q#D", decl);
2288 else
2289 error ("%q#D conflicts with a previous declaration", decl);
2290
2291 inform (location_of (bval), "previous declaration %q#D", bval);
2292}
2293
2294/* Wrapper for supplement_binding_1. */
2295
2296static bool
2297supplement_binding (cxx_binding *binding, tree decl)
2298{
2299 bool ret;
2300 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2301 ret = supplement_binding_1 (binding, decl);
2302 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2303 return ret;
2304}
2305
2306/* Replace BINDING's current value on its scope's name list with
2307 NEWVAL. */
2308
2309static void
2310update_local_overload (cxx_binding *binding, tree newval)
2311{
2312 tree *d;
2313
2314 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2315 if (*d == binding->value)
2316 {
2317 /* Stitch new list node in. */
2318 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2319 break;
2320 }
2321 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2322 break;
2323
2324 TREE_VALUE (*d) = newval;
2325}
2326
2327/* Compares the parameter-type-lists of ONE and TWO and
2328 returns false if they are different. If the DECLs are template
2329 functions, the return types and the template parameter lists are
2330 compared too (DR 565). */
2331
2332static bool
2333matching_fn_p (tree one, tree two)
2334{
2335 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2336 TYPE_ARG_TYPES (TREE_TYPE (two))))
2337 return false;
2338
2339 if (TREE_CODE (one) == TEMPLATE_DECL
2340 && TREE_CODE (two) == TEMPLATE_DECL)
2341 {
2342 /* Compare template parms. */
2343 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2344 DECL_TEMPLATE_PARMS (two)))
2345 return false;
2346
2347 /* And return type. */
2348 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2349 TREE_TYPE (TREE_TYPE (two))))
2350 return false;
2351 }
2352
2353 return true;
2354}
2355
2356/* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2357 binding value (possibly with anticipated builtins stripped).
2358 Diagnose conflicts and return updated decl. */
2359
2360static tree
2361update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2362 tree old, tree decl, bool is_friend)
2363{
2364 tree to_val = decl;
2365 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2366 tree to_type = old_type;
2367
2368 gcc_assert (level->kind == sk_namespace ? !binding
2369 : level->kind != sk_class && !slot);
2370 if (old == error_mark_node)
2371 old = NULL_TREE;
2372
2373 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2374 {
2375 tree other = to_type;
2376
2377 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2378 other = old;
2379
2380 /* Pushing an artificial typedef. See if this matches either
2381 the type slot or the old value slot. */
2382 if (!other)
2383 ;
2384 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2385 /* Two artificial decls to same type. Do nothing. */
2386 return other;
2387 else
2388 goto conflict;
2389
2390 if (old)
2391 {
2392 /* Slide decl into the type slot, keep old unaltered */
2393 to_type = decl;
2394 to_val = old;
2395 goto done;
2396 }
2397 }
2398
2399 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2400 {
2401 /* Slide old into the type slot. */
2402 to_type = old;
2403 old = NULL_TREE;
2404 }
2405
2406 if (DECL_DECLARES_FUNCTION_P (decl))
2407 {
2408 if (!old)
2409 ;
2410 else if (OVL_P (old))
2411 {
2412 for (ovl_iterator iter (old); iter; ++iter)
2413 {
2414 tree fn = *iter;
2415
2416 if (iter.using_p () && matching_fn_p (fn, decl))
2417 {
2418 /* If a function declaration in namespace scope or
2419 block scope has the same name and the same
2420 parameter-type- list (8.3.5) as a function
2421 introduced by a using-declaration, and the
2422 declarations do not declare the same function,
2423 the program is ill-formed. [namespace.udecl]/14 */
2424 if (tree match = duplicate_decls (decl, fn, is_friend))
2425 return match;
2426 else
2427 /* FIXME: To preserve existing error behavior, we
2428 still push the decl. This might change. */
2429 diagnose_name_conflict (decl, fn);
2430 }
2431 }
2432 }
2433 else
2434 goto conflict;
2435
2436 if (to_type != old_type
2437 && warn_shadow
2438 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2439 && !(DECL_IN_SYSTEM_HEADER (decl)
2440 && DECL_IN_SYSTEM_HEADER (to_type)))
2441 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2442 decl, to_type);
2443
2444 to_val = ovl_insert (decl, old);
2445 }
2446 else if (!old)
2447 ;
2448 else if (TREE_CODE (old) != TREE_CODE (decl))
2449 /* Different kinds of decls conflict. */
2450 goto conflict;
2451 else if (TREE_CODE (old) == TYPE_DECL)
2452 {
2453 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2454 /* Two type decls to the same type. Do nothing. */
2455 return old;
2456 else
2457 goto conflict;
2458 }
2459 else if (TREE_CODE (old) == NAMESPACE_DECL)
2460 {
2461 /* Two maybe-aliased namespaces. If they're to the same target
2462 namespace, that's ok. */
2463 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2464 goto conflict;
2465
2466 /* The new one must be an alias at this point. */
2467 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2468 return old;
2469 }
2470 else if (TREE_CODE (old) == VAR_DECL)
2471 {
2472 /* There can be two block-scope declarations of the same
2473 variable, so long as they are `extern' declarations. */
2474 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2475 goto conflict;
2476 else if (tree match = duplicate_decls (decl, old, false))
2477 return match;
2478 else
2479 goto conflict;
2480 }
2481 else
2482 {
2483 conflict:
2484 diagnose_name_conflict (decl, old);
2485 to_val = NULL_TREE;
2486 }
2487
2488 done:
2489 if (to_val)
2490 {
2491 if (level->kind != sk_namespace
2492 && !to_type && binding->value && OVL_P (to_val))
2493 update_local_overload (binding, to_val);
2494 else
2495 {
2496 tree to_add = to_val;
2497
2498 if (level->kind == sk_namespace)
2499 to_add = decl;
2500 else if (to_type == decl)
2501 to_add = decl;
2502 else if (TREE_CODE (to_add) == OVERLOAD)
2503 to_add = build_tree_list (NULL_TREE, to_add);
2504
2505 add_decl_to_level (level, to_add);
2506 }
2507
2508 if (slot)
2509 {
2510 if (STAT_HACK_P (*slot))
2511 {
2512 STAT_TYPE (*slot) = to_type;
2513 STAT_DECL (*slot) = to_val;
2514 }
2515 else if (to_type)
2516 *slot = stat_hack (to_val, to_type);
2517 else
2518 *slot = to_val;
2519 }
2520 else
2521 {
2522 binding->type = to_type;
2523 binding->value = to_val;
2524 }
2525 }
2526
2527 return decl;
2528}
2529
2530/* Table of identifiers to extern C declarations (or LISTS thereof). */
2531
2532static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2533
2534/* DECL has C linkage. If we have an existing instance, make sure the
2535 new one is compatible. Make sure it has the same exception
2536 specification [7.5, 7.6]. Add DECL to the map. */
2537
2538static void
2539check_extern_c_conflict (tree decl)
2540{
2541 /* Ignore artificial or system header decls. */
2542 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2543 return;
2544
2545 if (!extern_c_decls)
2546 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2547
2548 tree *slot = extern_c_decls
2549 ->find_slot_with_hash (DECL_NAME (decl),
2550 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2551 if (tree old = *slot)
2552 {
2553 if (TREE_CODE (old) == OVERLOAD)
2554 old = OVL_FUNCTION (old);
2555
2556 int mismatch = 0;
2557 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2558 ; /* If they're in the same context, we'll have already complained
2559 about a (possible) mismatch, when inserting the decl. */
2560 else if (!decls_match (decl, old))
2561 mismatch = 1;
2562 else if (TREE_CODE (decl) == FUNCTION_DECL
2563 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2564 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2565 ce_normal))
2566 mismatch = -1;
2567 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2568 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2569
2570 if (mismatch)
2571 {
2572 pedwarn (input_location, 0,
2573 "conflicting C language linkage declaration %q#D", decl);
2574 inform (DECL_SOURCE_LOCATION (old),
2575 "previous declaration %q#D", old);
2576 if (mismatch < 0)
2577 inform (input_location,
2578 "due to different exception specifications");
2579 }
2580 else
2581 {
2582 if (old == *slot)
2583 /* The hash table expects OVERLOADS, so construct one with
2584 OLD as both the function and the chain. This allocate
2585 an excess OVERLOAD node, but it's rare to have multiple
2586 extern "C" decls of the same name. And we save
2587 complicating the hash table logic (which is used
2588 elsewhere). */
2589 *slot = ovl_make (old, old);
2590
2591 slot = &OVL_CHAIN (*slot);
2592
2593 /* Chain it on for c_linkage_binding's use. */
2594 *slot = tree_cons (NULL_TREE, decl, *slot);
2595 }
2596 }
2597 else
2598 *slot = decl;
2599}
2600
2601/* Returns a list of C-linkage decls with the name NAME. Used in
2602 c-family/c-pragma.c to implement redefine_extname pragma. */
2603
2604tree
2605c_linkage_bindings (tree name)
2606{
2607 if (extern_c_decls)
2608 if (tree *slot = extern_c_decls
2609 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2610 {
2611 tree result = *slot;
2612 if (TREE_CODE (result) == OVERLOAD)
2613 result = OVL_CHAIN (result);
2614 return result;
2615 }
2616
2617 return NULL_TREE;
2618}
2619
2620/* DECL is being declared at a local scope. Emit suitable shadow
2621 warnings. */
2622
2623static void
2624check_local_shadow (tree decl)
2625{
2626 /* Don't complain about the parms we push and then pop
2627 while tentatively parsing a function declarator. */
2628 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2629 return;
2630
2631 /* Inline decls shadow nothing. */
2632 if (DECL_FROM_INLINE (decl))
2633 return;
2634
2635 /* External decls are something else. */
2636 if (DECL_EXTERNAL (decl))
2637 return;
2638
2639 tree old = NULL_TREE;
2640 cp_binding_level *old_scope = NULL;
2641 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2642 {
2643 old = binding->value;
2644 old_scope = binding->scope;
2645 }
2646 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2647 old = DECL_SHADOWED_FOR_VAR (old);
2648
2649 tree shadowed = NULL_TREE;
2650 if (old
2651 && (TREE_CODE (old) == PARM_DECL
2652 || VAR_P (old)
2653 || (TREE_CODE (old) == TYPE_DECL
2654 && (!DECL_ARTIFICIAL (old)
2655 || TREE_CODE (decl) == TYPE_DECL)))
2656 && (!DECL_ARTIFICIAL (decl)
2657 || DECL_IMPLICIT_TYPEDEF_P (decl)
2658 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2659 {
2660 /* DECL shadows a local thing possibly of interest. */
2661
2662 /* Don't complain if it's from an enclosing function. */
2663 if (DECL_CONTEXT (old) == current_function_decl
2664 && TREE_CODE (decl) != PARM_DECL
2665 && TREE_CODE (old) == PARM_DECL)
2666 {
2667 /* Go to where the parms should be and see if we find
2668 them there. */
2669 cp_binding_level *b = current_binding_level->level_chain;
2670
2671 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2672 /* Skip the ctor/dtor cleanup level. */
2673 b = b->level_chain;
2674
2675 /* ARM $8.3 */
2676 if (b->kind == sk_function_parms)
2677 {
2678 error ("declaration of %q#D shadows a parameter", decl);
2679 return;
2680 }
2681 }
2682
2683 /* The local structure or class can't use parameters of
2684 the containing function anyway. */
2685 if (DECL_CONTEXT (old) != current_function_decl)
2686 {
2687 for (cp_binding_level *scope = current_binding_level;
2688 scope != old_scope; scope = scope->level_chain)
2689 if (scope->kind == sk_class
2690 && !LAMBDA_TYPE_P (scope->this_entity))
2691 return;
2692 }
2693 /* Error if redeclaring a local declared in a
2694 init-statement or in the condition of an if or
2695 switch statement when the new declaration is in the
2696 outermost block of the controlled statement.
2697 Redeclaring a variable from a for or while condition is
2698 detected elsewhere. */
2699 else if (VAR_P (old)
2700 && old_scope == current_binding_level->level_chain
2701 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2702 {
2703 error ("redeclaration of %q#D", decl);
2704 inform (DECL_SOURCE_LOCATION (old),
2705 "%q#D previously declared here", old);
2706 return;
2707 }
2708 /* C++11:
2709 3.3.3/3: The name declared in an exception-declaration (...)
2710 shall not be redeclared in the outermost block of the handler.
2711 3.3.3/2: A parameter name shall not be redeclared (...) in
2712 the outermost block of any handler associated with a
2713 function-try-block.
2714 3.4.1/15: The function parameter names shall not be redeclared
2715 in the exception-declaration nor in the outermost block of a
2716 handler for the function-try-block. */
2717 else if ((TREE_CODE (old) == VAR_DECL
2718 && old_scope == current_binding_level->level_chain
2719 && old_scope->kind == sk_catch)
2720 || (TREE_CODE (old) == PARM_DECL
2721 && (current_binding_level->kind == sk_catch
2722 || current_binding_level->level_chain->kind == sk_catch)
2723 && in_function_try_handler))
2724 {
2725 if (permerror (input_location, "redeclaration of %q#D", decl))
2726 inform (DECL_SOURCE_LOCATION (old),
2727 "%q#D previously declared here", old);
2728 return;
2729 }
2730
2731 /* If '-Wshadow=compatible-local' is specified without other
2732 -Wshadow= flags, we will warn only when the type of the
2733 shadowing variable (DECL) can be converted to that of the
2734 shadowed parameter (OLD_LOCAL). The reason why we only check
2735 if DECL's type can be converted to OLD_LOCAL's type (but not the
2736 other way around) is because when users accidentally shadow a
2737 parameter, more than often they would use the variable
2738 thinking (mistakenly) it's still the parameter. It would be
2739 rare that users would use the variable in the place that
2740 expects the parameter but thinking it's a new decl. */
2741
2742 enum opt_code warning_code;
2743 if (warn_shadow)
2744 warning_code = OPT_Wshadow;
2745 else if (warn_shadow_local)
2746 warning_code = OPT_Wshadow_local;
2747 else if (warn_shadow_compatible_local
2748 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2749 || (!dependent_type_p (TREE_TYPE (decl))
2750 && !dependent_type_p (TREE_TYPE (old))
2751 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2752 tf_none))))
2753 warning_code = OPT_Wshadow_compatible_local;
2754 else
2755 return;
2756
2757 const char *msg;
2758 if (TREE_CODE (old) == PARM_DECL)
2759 msg = "declaration of %q#D shadows a parameter";
2760 else if (is_capture_proxy (old))
2761 msg = "declaration of %qD shadows a lambda capture";
2762 else
2763 msg = "declaration of %qD shadows a previous local";
2764
2765 if (warning_at (input_location, warning_code, msg, decl))
2766 {
2767 shadowed = old;
2768 goto inform_shadowed;
2769 }
2770 return;
2771 }
2772
2773 if (!warn_shadow)
2774 return;
2775
2776 /* Don't warn for artificial things that are not implicit typedefs. */
2777 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2778 return;
2779
2780 if (nonlambda_method_basetype ())
2781 if (tree member = lookup_member (current_nonlambda_class_type (),
2782 DECL_NAME (decl), /*protect=*/0,
2783 /*want_type=*/false, tf_warning_or_error))
2784 {
2785 member = MAYBE_BASELINK_FUNCTIONS (member);
2786
2787 /* Warn if a variable shadows a non-function, or the variable
2788 is a function or a pointer-to-function. */
2789 if (!OVL_P (member)
2790 || TREE_CODE (decl) == FUNCTION_DECL
2791 || TYPE_PTRFN_P (TREE_TYPE (decl))
2792 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2793 {
2794 if (warning_at (input_location, OPT_Wshadow,
2795 "declaration of %qD shadows a member of %qT",
2796 decl, current_nonlambda_class_type ())
2797 && DECL_P (member))
2798 {
2799 shadowed = member;
2800 goto inform_shadowed;
2801 }
2802 }
2803 return;
2804 }
2805
2806 /* Now look for a namespace shadow. */
2807 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2808 if (old
2809 && (VAR_P (old)
2810 || (TREE_CODE (old) == TYPE_DECL
2811 && (!DECL_ARTIFICIAL (old)
2812 || TREE_CODE (decl) == TYPE_DECL)))
2813 && !instantiating_current_function_p ())
2814 /* XXX shadow warnings in outer-more namespaces */
2815 {
2816 if (warning_at (input_location, OPT_Wshadow,
2817 "declaration of %qD shadows a global declaration",
2818 decl))
2819 {
2820 shadowed = old;
2821 goto inform_shadowed;
2822 }
2823 return;
2824 }
2825
2826 return;
2827
2828 inform_shadowed:
2829 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2830}
2831
2832/* DECL is being pushed inside function CTX. Set its context, if
2833 needed. */
2834
2835static void
2836set_decl_context_in_fn (tree ctx, tree decl)
2837{
2838 if (!DECL_CONTEXT (decl)
2839 /* A local declaration for a function doesn't constitute
2840 nesting. */
2841 && TREE_CODE (decl) != FUNCTION_DECL
2842 /* A local declaration for an `extern' variable is in the
2843 scope of the current namespace, not the current
2844 function. */
2845 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2846 /* When parsing the parameter list of a function declarator,
2847 don't set DECL_CONTEXT to an enclosing function. When we
2848 push the PARM_DECLs in order to process the function body,
2849 current_binding_level->this_entity will be set. */
2850 && !(TREE_CODE (decl) == PARM_DECL
2851 && current_binding_level->kind == sk_function_parms
2852 && current_binding_level->this_entity == NULL))
2853 DECL_CONTEXT (decl) = ctx;
2854
2855 /* If this is the declaration for a namespace-scope function,
2856 but the declaration itself is in a local scope, mark the
2857 declaration. */
2858 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2859 DECL_LOCAL_FUNCTION_P (decl) = 1;
2860}
2861
2862/* DECL is a local-scope decl with linkage. SHADOWED is true if the
2863 name is already bound at the current level.
2864
2865 [basic.link] If there is a visible declaration of an entity with
2866 linkage having the same name and type, ignoring entities declared
2867 outside the innermost enclosing namespace scope, the block scope
2868 declaration declares that same entity and receives the linkage of
2869 the previous declaration.
2870
2871 Also, make sure that this decl matches any existing external decl
2872 in the enclosing namespace. */
2873
2874static void
2875set_local_extern_decl_linkage (tree decl, bool shadowed)
2876{
2877 tree ns_value = decl; /* Unique marker. */
2878
2879 if (!shadowed)
2880 {
2881 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2882 if (!loc_value)
2883 {
2884 ns_value
2885 = find_namespace_value (current_namespace, DECL_NAME (decl));
2886 loc_value = ns_value;
2887 }
2888 if (loc_value == error_mark_node)
2889 loc_value = NULL_TREE;
2890
2891 for (ovl_iterator iter (loc_value); iter; ++iter)
2892 if (!iter.hidden_p ()
2893 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2894 && decls_match (*iter, decl))
2895 {
2896 /* The standard only says that the local extern inherits
2897 linkage from the previous decl; in particular, default
2898 args are not shared. Add the decl into a hash table to
2899 make sure only the previous decl in this case is seen
2900 by the middle end. */
2901 struct cxx_int_tree_map *h;
2902
2903 /* We inherit the outer decl's linkage. But we're a
2904 different decl. */
2905 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2906
2907 if (cp_function_chain->extern_decl_map == NULL)
2908 cp_function_chain->extern_decl_map
2909 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2910
2911 h = ggc_alloc<cxx_int_tree_map> ();
2912 h->uid = DECL_UID (decl);
2913 h->to = *iter;
2914 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2915 ->find_slot (h, INSERT);
2916 *loc = h;
2917 break;
2918 }
2919 }
2920
2921 if (TREE_PUBLIC (decl))
2922 {
2923 /* DECL is externally visible. Make sure it matches a matching
2924 decl in the namespace scope. We only really need to check
2925 this when inserting the decl, not when we find an existing
2926 match in the current scope. However, in practice we're
2927 going to be inserting a new decl in the majority of cases --
2928 who writes multiple extern decls for the same thing in the
2929 same local scope? Doing it here often avoids a duplicate
2930 namespace lookup. */
2931
2932 /* Avoid repeating a lookup. */
2933 if (ns_value == decl)
2934 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2935
2936 if (ns_value == error_mark_node)
2937 ns_value = NULL_TREE;
2938
2939 for (ovl_iterator iter (ns_value); iter; ++iter)
2940 {
2941 tree other = *iter;
2942
2943 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2944 ; /* Not externally visible. */
2945 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2946 ; /* Both are extern "C", we'll check via that mechanism. */
2947 else if (TREE_CODE (other) != TREE_CODE (decl)
2948 || ((VAR_P (decl) || matching_fn_p (other, decl))
2949 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2950 COMPARE_REDECLARATION)))
2951 {
2952 if (permerror (DECL_SOURCE_LOCATION (decl),
2953 "local external declaration %q#D", decl))
2954 inform (DECL_SOURCE_LOCATION (other),
2955 "does not match previous declaration %q#D", other);
2956 break;
2957 }
2958 }
2959 }
2960}
2961
2962/* Record DECL as belonging to the current lexical scope. Check for
2963 errors (such as an incompatible declaration for the same name
2964 already seen in the same scope). IS_FRIEND is true if DECL is
2965 declared as a friend.
2966
2967 Returns either DECL or an old decl for the same name. If an old
2968 decl is returned, it may have been smashed to agree with what DECL
2969 says. */
2970
2971static tree
2972do_pushdecl (tree decl, bool is_friend)
2973{
2974 if (decl == error_mark_node)
2975 return error_mark_node;
2976
2977 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2978 set_decl_context_in_fn (current_function_decl, decl);
2979
2980 /* The binding level we will be pushing into. During local class
2981 pushing, we want to push to the containing scope. */
2982 cp_binding_level *level = current_binding_level;
2983 while (level->kind == sk_class)
2984 level = level->level_chain;
2985
2986 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2987 insert it. Other NULL-named decls, not so much. */
2988 tree name = DECL_NAME (decl);
2989 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2990 {
2991 cxx_binding *binding = NULL; /* Local scope binding. */
2992 tree ns = NULL_TREE; /* Searched namespace. */
2993 tree *slot = NULL; /* Binding slot in namespace. */
2994 tree old = NULL_TREE;
2995
2996 if (level->kind == sk_namespace)
2997 {
2998 /* We look in the decl's namespace for an existing
2999 declaration, even though we push into the current
3000 namespace. */
3001 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3002 ? CP_DECL_CONTEXT (decl) : current_namespace);
3003 /* Create the binding, if this is current namespace, because
3004 that's where we'll be pushing anyway. */
3005 slot = find_namespace_slot (ns, name, ns == current_namespace);
3006 if (slot)
3007 old = MAYBE_STAT_DECL (*slot);
3008 }
3009 else
3010 {
3011 binding = find_local_binding (level, name);
3012 if (binding)
3013 old = binding->value;
3014 }
3015
3016 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3017 && DECL_EXTERNAL (decl))
3018 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3019
3020 if (old == error_mark_node)
3021 old = NULL_TREE;
3022
3023 for (ovl_iterator iter (old); iter; ++iter)
3024 if (iter.using_p ())
3025 ; /* Ignore using decls here. */
3026 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3027 {
3028 if (match == error_mark_node)
3029 ;
3030 else if (TREE_CODE (match) == TYPE_DECL)
3031 /* The IDENTIFIER will have the type referring to the
3032 now-smashed TYPE_DECL, because ...? Reset it. */
3033 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3034 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3035 {
3036 /* Unhiding a previously hidden decl. */
3037 tree head = iter.reveal_node (old);
3038 if (head != old)
3039 {
3040 if (!ns)
3041 {
3042 update_local_overload (binding, head);
3043 binding->value = head;
3044 }
3045 else if (STAT_HACK_P (*slot))
3046 STAT_DECL (*slot) = head;
3047 else
3048 *slot = head;
3049 }
3050 if (DECL_EXTERN_C_P (match))
3051 /* We need to check and register the decl now. */
3052 check_extern_c_conflict (match);
3053 }
3054 return match;
3055 }
3056
3057 /* We are pushing a new decl. */
3058
3059 /* Skip a hidden builtin we failed to match already. There can
3060 only be one. */
3061 if (old && anticipated_builtin_p (old))
3062 old = OVL_CHAIN (old);
3063
3064 check_template_shadow (decl);
3065
3066 if (DECL_DECLARES_FUNCTION_P (decl))
3067 {
3068 check_default_args (decl);
3069
3070 if (is_friend)
3071 {
3072 if (level->kind != sk_namespace)
3073 /* In a local class, a friend function declaration must
3074 find a matching decl in the innermost non-class scope.
3075 [class.friend/11] */
3076 error ("friend declaration %qD in local class without "
3077 "prior local declaration", decl);
3078 else if (!flag_friend_injection)
3079 /* Hide it from ordinary lookup. */
3080 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3081 }
3082 }
3083
3084 if (level->kind != sk_namespace)
3085 {
3086 check_local_shadow (decl);
3087
3088 if (TREE_CODE (decl) == NAMESPACE_DECL)
3089 /* A local namespace alias. */
3090 set_identifier_type_value (name, NULL_TREE);
3091
3092 if (!binding)
3093 binding = create_local_binding (level, name);
3094 }
3095 else if (!slot)
3096 {
3097 ns = current_namespace;
3098 slot = find_namespace_slot (ns, name, true);
3099 /* Update OLD to reflect the namespace we're going to be
3100 pushing into. */
3101 old = MAYBE_STAT_DECL (*slot);
3102 }
3103
3104 old = update_binding (level, binding, slot, old, decl, is_friend);
3105
3106 if (old != decl)
3107 /* An existing decl matched, use it. */
3108 decl = old;
3109 else if (TREE_CODE (decl) == TYPE_DECL)
3110 {
3111 tree type = TREE_TYPE (decl);
3112
3113 if (type != error_mark_node)
3114 {
3115 if (TYPE_NAME (type) != decl)
3116 set_underlying_type (decl);
3117
3118 if (!ns)
3119 set_identifier_type_value_with_scope (name, decl, level);
3120 else
3121 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3122 }
3123
3124 /* If this is a locally defined typedef in a function that
3125 is not a template instantation, record it to implement
3126 -Wunused-local-typedefs. */
3127 if (!instantiating_current_function_p ())
3128 record_locally_defined_typedef (decl);
3129 }
3130 else if (VAR_P (decl))
3131 maybe_register_incomplete_var (decl);
3132
3133 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3134 && DECL_EXTERN_C_P (decl))
3135 check_extern_c_conflict (decl);
3136 }
3137 else
3138 add_decl_to_level (level, decl);
3139
3140 return decl;
3141}
3142
3143/* Record a decl-node X as belonging to the current lexical scope.
3144 It's a friend if IS_FRIEND is true -- which affects exactly where
3145 we push it. */
3146
3147tree
3148pushdecl (tree x, bool is_friend)
3149{
3150 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3151 tree ret = do_pushdecl (x, is_friend);
3152 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3153 return ret;
3154}
3155
3156/* Enter DECL into the symbol table, if that's appropriate. Returns
3157 DECL, or a modified version thereof. */
3158
3159tree
3160maybe_push_decl (tree decl)
3161{
3162 tree type = TREE_TYPE (decl);
3163
3164 /* Add this decl to the current binding level, but not if it comes
3165 from another scope, e.g. a static member variable. TEM may equal
3166 DECL or it may be a previous decl of the same name. */
3167 if (decl == error_mark_node
3168 || (TREE_CODE (decl) != PARM_DECL
3169 && DECL_CONTEXT (decl) != NULL_TREE
3170 /* Definitions of namespace members outside their namespace are
3171 possible. */
3172 && !DECL_NAMESPACE_SCOPE_P (decl))
3173 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3174 || type == unknown_type_node
3175 /* The declaration of a template specialization does not affect
3176 the functions available for overload resolution, so we do not
3177 call pushdecl. */
3178 || (TREE_CODE (decl) == FUNCTION_DECL
3179 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3180 return decl;
3181 else
3182 return pushdecl (decl);
3183}
3184
3185/* Bind DECL to ID in the current_binding_level, assumed to be a local
3186 binding level. If IS_USING is true, DECL got here through a
3187 using-declaration. */
3188
3189static void
3190push_local_binding (tree id, tree decl, bool is_using)
3191{
3192 /* Skip over any local classes. This makes sense if we call
3193 push_local_binding with a friend decl of a local class. */
3194 cp_binding_level *b = innermost_nonclass_level ();
3195
3196 gcc_assert (b->kind != sk_namespace);
3197 if (find_local_binding (b, id))
3198 {
3199 /* Supplement the existing binding. */
3200 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3201 /* It didn't work. Something else must be bound at this
3202 level. Do not add DECL to the list of things to pop
3203 later. */
3204 return;
3205 }
3206 else
3207 /* Create a new binding. */
3208 push_binding (id, decl, b);
3209
3210 if (TREE_CODE (decl) == OVERLOAD || is_using)
3211 /* We must put the OVERLOAD or using into a TREE_LIST since we
3212 cannot use the decl's chain itself. */
3213 decl = build_tree_list (NULL_TREE, decl);
3214
3215 /* And put DECL on the list of things declared by the current
3216 binding level. */
3217 add_decl_to_level (b, decl);
3218}
3219
3220/* Check to see whether or not DECL is a variable that would have been
3221 in scope under the ARM, but is not in scope under the ANSI/ISO
3222 standard. If so, issue an error message. If name lookup would
3223 work in both cases, but return a different result, this function
3224 returns the result of ANSI/ISO lookup. Otherwise, it returns
3225 DECL. */
3226
3227tree
3228check_for_out_of_scope_variable (tree decl)
3229{
3230 tree shadowed;
3231
3232 /* We only care about out of scope variables. */
3233 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3234 return decl;
3235
3236 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3237 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3238 while (shadowed != NULL_TREE && VAR_P (shadowed)
3239 && DECL_DEAD_FOR_LOCAL (shadowed))
3240 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3241 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3242 if (!shadowed)
3243 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3244 if (shadowed)
3245 {
3246 if (!DECL_ERROR_REPORTED (decl))
3247 {
3248 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3249 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3250 " matches this %qD under ISO standard rules",
3251 shadowed);
3252 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3253 " matches this %qD under old rules", decl);
3254 DECL_ERROR_REPORTED (decl) = 1;
3255 }
3256 return shadowed;
3257 }
3258
3259 /* If we have already complained about this declaration, there's no
3260 need to do it again. */
3261 if (DECL_ERROR_REPORTED (decl))
3262 return decl;
3263
3264 DECL_ERROR_REPORTED (decl) = 1;
3265
3266 if (TREE_TYPE (decl) == error_mark_node)
3267 return decl;
3268
3269 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3270 {
3271 error ("name lookup of %qD changed for ISO %<for%> scoping",
3272 DECL_NAME (decl));
3273 error (" cannot use obsolete binding at %q+D because "
3274 "it has a destructor", decl);
3275 return error_mark_node;
3276 }
3277 else
3278 {
3279 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3280 DECL_NAME (decl));
3281 if (flag_permissive)
3282 permerror (DECL_SOURCE_LOCATION (decl),
3283 " using obsolete binding at %qD", decl);
3284 else
3285 {
3286 static bool hint;
3287 if (!hint)
3288 {
3289 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3290 hint = true;
3291 }
3292 }
3293 }
3294
3295 return decl;
3296}
3297
3298/* true means unconditionally make a BLOCK for the next level pushed. */
3299
3300static bool keep_next_level_flag;
3301
3302static int binding_depth = 0;
3303
3304static void
3305indent (int depth)
3306{
3307 int i;
3308
3309 for (i = 0; i < depth * 2; i++)
3310 putc (' ', stderr);
3311}
3312
3313/* Return a string describing the kind of SCOPE we have. */
3314static const char *
3315cp_binding_level_descriptor (cp_binding_level *scope)
3316{
3317 /* The order of this table must match the "scope_kind"
3318 enumerators. */
3319 static const char* scope_kind_names[] = {
3320 "block-scope",
3321 "cleanup-scope",
3322 "try-scope",
3323 "catch-scope",
3324 "for-scope",
3325 "function-parameter-scope",
3326 "class-scope",
3327 "namespace-scope",
3328 "template-parameter-scope",
3329 "template-explicit-spec-scope"
3330 };
3331 const scope_kind kind = scope->explicit_spec_p
3332 ? sk_template_spec : scope->kind;
3333
3334 return scope_kind_names[kind];
3335}
3336
3337/* Output a debugging information about SCOPE when performing
3338 ACTION at LINE. */
3339static void
3340cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3341{
3342 const char *desc = cp_binding_level_descriptor (scope);
3343 if (scope->this_entity)
3344 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3345 scope->this_entity, (void *) scope, line);
3346 else
3347 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3348}
3349
3350/* Return the estimated initial size of the hashtable of a NAMESPACE
3351 scope. */
3352
3353static inline size_t
3354namespace_scope_ht_size (tree ns)
3355{
3356 tree name = DECL_NAME (ns);
3357
3358 return name == std_identifier
3359 ? NAMESPACE_STD_HT_SIZE
3360 : (name == global_identifier
3361 ? GLOBAL_SCOPE_HT_SIZE
3362 : NAMESPACE_ORDINARY_HT_SIZE);
3363}
3364
3365/* A chain of binding_level structures awaiting reuse. */
3366
3367static GTY((deletable)) cp_binding_level *free_binding_level;
3368
3369/* Insert SCOPE as the innermost binding level. */
3370
3371void
3372push_binding_level (cp_binding_level *scope)
3373{
3374 /* Add it to the front of currently active scopes stack. */
3375 scope->level_chain = current_binding_level;
3376 current_binding_level = scope;
3377 keep_next_level_flag = false;
3378
3379 if (ENABLE_SCOPE_CHECKING)
3380 {
3381 scope->binding_depth = binding_depth;
3382 indent (binding_depth);
3383 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3384 "push");
3385 binding_depth++;
3386 }
3387}
3388
3389/* Create a new KIND scope and make it the top of the active scopes stack.
3390 ENTITY is the scope of the associated C++ entity (namespace, class,
3391 function, C++0x enumeration); it is NULL otherwise. */
3392
3393cp_binding_level *
3394begin_scope (scope_kind kind, tree entity)
3395{
3396 cp_binding_level *scope;
3397
3398 /* Reuse or create a struct for this binding level. */
3399 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3400 {
3401 scope = free_binding_level;
3402 free_binding_level = scope->level_chain;
3403 memset (scope, 0, sizeof (cp_binding_level));
3404 }
3405 else
3406 scope = ggc_cleared_alloc<cp_binding_level> ();
3407
3408 scope->this_entity = entity;
3409 scope->more_cleanups_ok = true;
3410 switch (kind)
3411 {
3412 case sk_cleanup:
3413 scope->keep = true;
3414 break;
3415
3416 case sk_template_spec:
3417 scope->explicit_spec_p = true;
3418 kind = sk_template_parms;
3419 /* Fall through. */
3420 case sk_template_parms:
3421 case sk_block:
3422 case sk_try:
3423 case sk_catch:
3424 case sk_for:
3425 case sk_cond:
3426 case sk_class:
3427 case sk_scoped_enum:
3428 case sk_function_parms:
3429 case sk_transaction:
3430 case sk_omp:
3431 scope->keep = keep_next_level_flag;
3432 break;
3433
3434 case sk_namespace:
3435 NAMESPACE_LEVEL (entity) = scope;
3436 break;
3437
3438 default:
3439 /* Should not happen. */
3440 gcc_unreachable ();
3441 break;
3442 }
3443 scope->kind = kind;
3444
3445 push_binding_level (scope);
3446
3447 return scope;
3448}
3449
3450/* We're about to leave current scope. Pop the top of the stack of
3451 currently active scopes. Return the enclosing scope, now active. */
3452
3453cp_binding_level *
3454leave_scope (void)
3455{
3456 cp_binding_level *scope = current_binding_level;
3457
3458 if (scope->kind == sk_namespace && class_binding_level)
3459 current_binding_level = class_binding_level;
3460
3461 /* We cannot leave a scope, if there are none left. */
3462 if (NAMESPACE_LEVEL (global_namespace))
3463 gcc_assert (!global_scope_p (scope));
3464
3465 if (ENABLE_SCOPE_CHECKING)
3466 {
3467 indent (--binding_depth);
3468 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3469 "leave");
3470 }
3471
3472 /* Move one nesting level up. */
3473 current_binding_level = scope->level_chain;
3474
3475 /* Namespace-scopes are left most probably temporarily, not
3476 completely; they can be reopened later, e.g. in namespace-extension
3477 or any name binding activity that requires us to resume a
3478 namespace. For classes, we cache some binding levels. For other
3479 scopes, we just make the structure available for reuse. */
3480 if (scope->kind != sk_namespace
3481 && scope->kind != sk_class)
3482 {
3483 scope->level_chain = free_binding_level;
3484 gcc_assert (!ENABLE_SCOPE_CHECKING
3485 || scope->binding_depth == binding_depth);
3486 free_binding_level = scope;
3487 }
3488
3489 if (scope->kind == sk_class)
3490 {
3491 /* Reset DEFINING_CLASS_P to allow for reuse of a
3492 class-defining scope in a non-defining context. */
3493 scope->defining_class_p = 0;
3494
3495 /* Find the innermost enclosing class scope, and reset
3496 CLASS_BINDING_LEVEL appropriately. */
3497 class_binding_level = NULL;
3498 for (scope = current_binding_level; scope; scope = scope->level_chain)
3499 if (scope->kind == sk_class)
3500 {
3501 class_binding_level = scope;
3502 break;
3503 }
3504 }
3505
3506 return current_binding_level;
3507}
3508
3509static void
3510resume_scope (cp_binding_level* b)
3511{
3512 /* Resuming binding levels is meant only for namespaces,
3513 and those cannot nest into classes. */
3514 gcc_assert (!class_binding_level);
3515 /* Also, resuming a non-directly nested namespace is a no-no. */
3516 gcc_assert (b->level_chain == current_binding_level);
3517 current_binding_level = b;
3518 if (ENABLE_SCOPE_CHECKING)
3519 {
3520 b->binding_depth = binding_depth;
3521 indent (binding_depth);
3522 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3523 binding_depth++;
3524 }
3525}
3526
3527/* Return the innermost binding level that is not for a class scope. */
3528
3529static cp_binding_level *
3530innermost_nonclass_level (void)
3531{
3532 cp_binding_level *b;
3533
3534 b = current_binding_level;
3535 while (b->kind == sk_class)
3536 b = b->level_chain;
3537
3538 return b;
3539}
3540
3541/* We're defining an object of type TYPE. If it needs a cleanup, but
3542 we're not allowed to add any more objects with cleanups to the current
3543 scope, create a new binding level. */
3544
3545void
3546maybe_push_cleanup_level (tree type)
3547{
3548 if (type != error_mark_node
3549 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3550 && current_binding_level->more_cleanups_ok == 0)
3551 {
3552 begin_scope (sk_cleanup, NULL);
3553 current_binding_level->statement_list = push_stmt_list ();
3554 }
3555}
3556
3557/* Return true if we are in the global binding level. */
3558
3559bool
3560global_bindings_p (void)
3561{
3562 return global_scope_p (current_binding_level);
3563}
3564
3565/* True if we are currently in a toplevel binding level. This
3566 means either the global binding level or a namespace in a toplevel
3567 binding level. Since there are no non-toplevel namespace levels,
3568 this really means any namespace or template parameter level. We
3569 also include a class whose context is toplevel. */
3570
3571bool
3572toplevel_bindings_p (void)
3573{
3574 cp_binding_level *b = innermost_nonclass_level ();
3575
3576 return b->kind == sk_namespace || b->kind == sk_template_parms;
3577}
3578
3579/* True if this is a namespace scope, or if we are defining a class
3580 which is itself at namespace scope, or whose enclosing class is
3581 such a class, etc. */
3582
3583bool
3584namespace_bindings_p (void)
3585{
3586 cp_binding_level *b = innermost_nonclass_level ();
3587
3588 return b->kind == sk_namespace;
3589}
3590
3591/* True if the innermost non-class scope is a block scope. */
3592
3593bool
3594local_bindings_p (void)
3595{
3596 cp_binding_level *b = innermost_nonclass_level ();
3597 return b->kind < sk_function_parms || b->kind == sk_omp;
3598}
3599
3600/* True if the current level needs to have a BLOCK made. */
3601
3602bool
3603kept_level_p (void)
3604{
3605 return (current_binding_level->blocks != NULL_TREE
3606 || current_binding_level->keep
3607 || current_binding_level->kind == sk_cleanup
3608 || current_binding_level->names != NULL_TREE
3609 || current_binding_level->using_directives);
3610}
3611
3612/* Returns the kind of the innermost scope. */
3613
3614scope_kind
3615innermost_scope_kind (void)
3616{
3617 return current_binding_level->kind;
3618}
3619
3620/* Returns true if this scope was created to store template parameters. */
3621
3622bool
3623template_parm_scope_p (void)
3624{
3625 return innermost_scope_kind () == sk_template_parms;
3626}
3627
3628/* If KEEP is true, make a BLOCK node for the next binding level,
3629 unconditionally. Otherwise, use the normal logic to decide whether
3630 or not to create a BLOCK. */
3631
3632void
3633keep_next_level (bool keep)
3634{
3635 keep_next_level_flag = keep;
3636}
3637
3638/* Return the list of declarations of the current local scope. */
3639
3640tree
3641get_local_decls (void)
3642{
3643 gcc_assert (current_binding_level->kind != sk_namespace
3644 && current_binding_level->kind != sk_class);
3645 return current_binding_level->names;
3646}
3647
3648/* Return how many function prototypes we are currently nested inside. */
3649
3650int
3651function_parm_depth (void)
3652{
3653 int level = 0;
3654 cp_binding_level *b;
3655
3656 for (b = current_binding_level;
3657 b->kind == sk_function_parms;
3658 b = b->level_chain)
3659 ++level;
3660
3661 return level;
3662}
3663
3664/* For debugging. */
3665static int no_print_functions = 0;
3666static int no_print_builtins = 0;
3667
3668static void
3669print_binding_level (cp_binding_level* lvl)
3670{
3671 tree t;
3672 int i = 0, len;
3673 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3674 if (lvl->more_cleanups_ok)
3675 fprintf (stderr, " more-cleanups-ok");
3676 if (lvl->have_cleanups)
3677 fprintf (stderr, " have-cleanups");
3678 fprintf (stderr, "\n");
3679 if (lvl->names)
3680 {
3681 fprintf (stderr, " names:\t");
3682 /* We can probably fit 3 names to a line? */
3683 for (t = lvl->names; t; t = TREE_CHAIN (t))
3684 {
3685 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3686 continue;
3687 if (no_print_builtins
3688 && (TREE_CODE (t) == TYPE_DECL)
3689 && DECL_IS_BUILTIN (t))
3690 continue;
3691
3692 /* Function decls tend to have longer names. */
3693 if (TREE_CODE (t) == FUNCTION_DECL)
3694 len = 3;
3695 else
3696 len = 2;
3697 i += len;
3698 if (i > 6)
3699 {
3700 fprintf (stderr, "\n\t");
3701 i = len;
3702 }
3703 print_node_brief (stderr, "", t, 0);
3704 if (t == error_mark_node)
3705 break;
3706 }
3707 if (i)
3708 fprintf (stderr, "\n");
3709 }
3710 if (vec_safe_length (lvl->class_shadowed))
3711 {
3712 size_t i;
3713 cp_class_binding *b;
3714 fprintf (stderr, " class-shadowed:");
3715 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3716 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3717 fprintf (stderr, "\n");
3718 }
3719 if (lvl->type_shadowed)
3720 {
3721 fprintf (stderr, " type-shadowed:");
3722 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3723 {
3724 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3725 }
3726 fprintf (stderr, "\n");
3727 }
3728}
3729
3730DEBUG_FUNCTION void
3731debug (cp_binding_level &ref)
3732{
3733 print_binding_level (&ref);
3734}
3735
3736DEBUG_FUNCTION void
3737debug (cp_binding_level *ptr)
3738{
3739 if (ptr)
3740 debug (*ptr);
3741 else
3742 fprintf (stderr, "<nil>\n");
3743}
3744
3745
3746void
3747print_other_binding_stack (cp_binding_level *stack)
3748{
3749 cp_binding_level *level;
3750 for (level = stack; !global_scope_p (level); level = level->level_chain)
3751 {
3752 fprintf (stderr, "binding level %p\n", (void *) level);
3753 print_binding_level (level);
3754 }
3755}
3756
3757void
3758print_binding_stack (void)
3759{
3760 cp_binding_level *b;
3761 fprintf (stderr, "current_binding_level=%p\n"
3762 "class_binding_level=%p\n"
3763 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3764 (void *) current_binding_level, (void *) class_binding_level,
3765 (void *) NAMESPACE_LEVEL (global_namespace));
3766 if (class_binding_level)
3767 {
3768 for (b = class_binding_level; b; b = b->level_chain)
3769 if (b == current_binding_level)
3770 break;
3771 if (b)
3772 b = class_binding_level;
3773 else
3774 b = current_binding_level;
3775 }
3776 else
3777 b = current_binding_level;
3778 print_other_binding_stack (b);
3779 fprintf (stderr, "global:\n");
3780 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3781}
3782
3783/* Return the type associated with ID. */
3784
3785static tree
3786identifier_type_value_1 (tree id)
3787{
3788 /* There is no type with that name, anywhere. */
3789 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3790 return NULL_TREE;
3791 /* This is not the type marker, but the real thing. */
3792 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3793 return REAL_IDENTIFIER_TYPE_VALUE (id);
3794 /* Have to search for it. It must be on the global level, now.
3795 Ask lookup_name not to return non-types. */
3796 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3797 if (id)
3798 return TREE_TYPE (id);
3799 return NULL_TREE;
3800}
3801
3802/* Wrapper for identifier_type_value_1. */
3803
3804tree
3805identifier_type_value (tree id)
3806{
3807 tree ret;
3808 timevar_start (TV_NAME_LOOKUP);
3809 ret = identifier_type_value_1 (id);
3810 timevar_stop (TV_NAME_LOOKUP);
3811 return ret;
3812}
3813
3814/* Push a definition of struct, union or enum tag named ID. into
3815 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3816 the tag ID is not already defined. */
3817
3818static void
3819set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3820{
3821 tree type;
3822
3823 if (b->kind != sk_namespace)
3824 {
3825 /* Shadow the marker, not the real thing, so that the marker
3826 gets restored later. */
3827 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3828 b->type_shadowed
3829 = tree_cons (id, old_type_value, b->type_shadowed);
3830 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3831 TREE_TYPE (b->type_shadowed) = type;
3832 }
3833 else
3834 {
3835 tree *slot = find_namespace_slot (current_namespace, id, true);
3836 gcc_assert (decl);
3837 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3838
3839 /* Store marker instead of real type. */
3840 type = global_type_node;
3841 }
3842 SET_IDENTIFIER_TYPE_VALUE (id, type);
3843}
3844
3845/* As set_identifier_type_value_with_scope, but using
3846 current_binding_level. */
3847
3848void
3849set_identifier_type_value (tree id, tree decl)
3850{
3851 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3852}
3853
3854/* Return the name for the constructor (or destructor) for the
3855 specified class. */
3856
3857tree
3858constructor_name (tree type)
3859{
3860 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3861
3862 return decl ? DECL_NAME (decl) : NULL_TREE;
3863}
3864
3865/* Returns TRUE if NAME is the name for the constructor for TYPE,
3866 which must be a class type. */
3867
3868bool
3869constructor_name_p (tree name, tree type)
3870{
3871 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3872
3873 /* These don't have names. */
3874 if (TREE_CODE (type) == DECLTYPE_TYPE
3875 || TREE_CODE (type) == TYPEOF_TYPE)
3876 return false;
3877
3878 if (name && name == constructor_name (type))
3879 return true;
3880
3881 return false;
3882}
3883
3884/* Counter used to create anonymous type names. */
3885
3886static GTY(()) int anon_cnt;
3887
3888/* Return an IDENTIFIER which can be used as a name for
3889 unnamed structs and unions. */
3890
3891tree
3892make_anon_name (void)
3893{
3894 char buf[32];
3895
3896 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3897 return get_identifier (buf);
3898}
3899
3900/* This code is practically identical to that for creating
3901 anonymous names, but is just used for lambdas instead. This isn't really
3902 necessary, but it's convenient to avoid treating lambdas like other
3903 unnamed types. */
3904
3905static GTY(()) int lambda_cnt = 0;
3906
3907tree
3908make_lambda_name (void)
3909{
3910 char buf[32];
3911
3912 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3913 return get_identifier (buf);
3914}
3915
3916/* Insert another USING_DECL into the current binding level, returning
3917 this declaration. If this is a redeclaration, do nothing, and
3918 return NULL_TREE if this not in namespace scope (in namespace
3919 scope, a using decl might extend any previous bindings). */
3920
3921static tree
3922push_using_decl_1 (tree scope, tree name)
3923{
3924 tree decl;
3925
3926 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3927 gcc_assert (identifier_p (name));
3928 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3929 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3930 break;
3931 if (decl)
3932 return namespace_bindings_p () ? decl : NULL_TREE;
3933 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3934 USING_DECL_SCOPE (decl) = scope;
3935 DECL_CHAIN (decl) = current_binding_level->usings;
3936 current_binding_level->usings = decl;
3937 return decl;
3938}
3939
3940/* Wrapper for push_using_decl_1. */
3941
3942static tree
3943push_using_decl (tree scope, tree name)
3944{
3945 tree ret;
3946 timevar_start (TV_NAME_LOOKUP);
3947 ret = push_using_decl_1 (scope, name);
3948 timevar_stop (TV_NAME_LOOKUP);
3949 return ret;
3950}
3951
3952/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3953 caller to set DECL_CONTEXT properly.
3954
3955 Note that this must only be used when X will be the new innermost
3956 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3957 without checking to see if the current IDENTIFIER_BINDING comes from a
3958 closer binding level than LEVEL. */
3959
3960static tree
3961do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3962{
3963 cp_binding_level *b;
3964 tree function_decl = current_function_decl;
3965
3966 current_function_decl = NULL_TREE;
3967 if (level->kind == sk_class)
3968 {
3969 b = class_binding_level;
3970 class_binding_level = level;
3971 pushdecl_class_level (x);
3972 class_binding_level = b;
3973 }
3974 else
3975 {
3976 b = current_binding_level;
3977 current_binding_level = level;
3978 x = pushdecl (x, is_friend);
3979 current_binding_level = b;
3980 }
3981 current_function_decl = function_decl;
3982 return x;
3983}
3984
3985/* Inject X into the local scope just before the function parms. */
3986
3987tree
3988pushdecl_outermost_localscope (tree x)
3989{
3990 cp_binding_level *b = NULL;
3991 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3992
3993 /* Find the scope just inside the function parms. */
3994 for (cp_binding_level *n = current_binding_level;
3995 n->kind != sk_function_parms; n = b->level_chain)
3996 b = n;
3997
3998 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3999 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4000
4001 return ret;
4002}
4003
4004/* Check a non-member using-declaration. Return the name and scope
4005 being used, and the USING_DECL, or NULL_TREE on failure. */
4006
4007static tree
4008validate_nonmember_using_decl (tree decl, tree scope, tree name)
4009{
4010 /* [namespace.udecl]
4011 A using-declaration for a class member shall be a
4012 member-declaration. */
4013 if (TYPE_P (scope))
4014 {
4015 error ("%qT is not a namespace or unscoped enum", scope);
4016 return NULL_TREE;
4017 }
4018 else if (scope == error_mark_node)
4019 return NULL_TREE;
4020
4021 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4022 {
4023 /* 7.3.3/5
4024 A using-declaration shall not name a template-id. */
4025 error ("a using-declaration cannot specify a template-id. "
4026 "Try %<using %D%>", name);
4027 return NULL_TREE;
4028 }
4029
4030 if (TREE_CODE (decl) == NAMESPACE_DECL)
4031 {
4032 error ("namespace %qD not allowed in using-declaration", decl);
4033 return NULL_TREE;
4034 }
4035
4036 if (TREE_CODE (decl) == SCOPE_REF)
4037 {
4038 /* It's a nested name with template parameter dependent scope.
4039 This can only be using-declaration for class member. */
4040 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4041 return NULL_TREE;
4042 }
4043
4044 decl = OVL_FIRST (decl);
4045
4046 /* Make a USING_DECL. */
4047 tree using_decl = push_using_decl (scope, name);
4048
4049 if (using_decl == NULL_TREE
4050 && at_function_scope_p ()
4051 && VAR_P (decl))
4052 /* C++11 7.3.3/10. */
4053 error ("%qD is already declared in this scope", name);
4054
4055 return using_decl;
4056}
4057
4058/* Process a local-scope or namespace-scope using declaration. SCOPE
4059 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4060 point to the binding for NAME in the current scope and are
4061 updated. */
4062
4063static void
4064do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4065{
4066 name_lookup lookup (name, 0);
4067
4068 if (!qualified_namespace_lookup (scope, &lookup))
4069 {
4070 error ("%qD not declared", name);
4071 return;
4072 }
4073 else if (TREE_CODE (lookup.value) == TREE_LIST)
4074 {
4075 error ("reference to %qD is ambiguous", name);
4076 print_candidates (lookup.value);
4077 lookup.value = NULL_TREE;
4078 }
4079
4080 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4081 {
4082 error ("reference to %qD is ambiguous", name);
4083 print_candidates (lookup.type);
4084 lookup.type = NULL_TREE;
4085 }
4086
4087 tree value = *value_p;
4088 tree type = *type_p;
4089
4090 /* Shift the old and new bindings around so we're comparing class and
4091 enumeration names to each other. */
4092 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4093 {
4094 type = value;
4095 value = NULL_TREE;
4096 }
4097
4098 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4099 {
4100 lookup.type = lookup.value;
4101 lookup.value = NULL_TREE;
4102 }
4103
4104 if (lookup.value && lookup.value != value)
4105 {
4106 /* Check for using functions. */
4107 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4108 {
4109 for (lkp_iterator usings (lookup.value); usings; ++usings)
4110 {
4111 tree new_fn = *usings;
4112
4113 /* [namespace.udecl]
4114
4115 If a function declaration in namespace scope or block
4116 scope has the same name and the same parameter types as a
4117 function introduced by a using declaration the program is
4118 ill-formed. */
4119 bool found = false;
4120 for (ovl_iterator old (value); !found && old; ++old)
4121 {
4122 tree old_fn = *old;
4123
4124 if (new_fn == old_fn)
4125 /* The function already exists in the current
4126 namespace. */
4127 found = true;
4128 else if (old.using_p ())
4129 continue; /* This is a using decl. */
4130 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4131 continue; /* This is an anticipated builtin. */
4132 else if (!matching_fn_p (new_fn, old_fn))
4133 continue; /* Parameters do not match. */
4134 else if (decls_match (new_fn, old_fn))
4135 found = true;
4136 else
4137 {
4138 diagnose_name_conflict (new_fn, old_fn);
4139 found = true;
4140 }
4141 }
4142
4143 if (!found)
4144 /* Unlike the overload case we don't drop anticipated
4145 builtins here. They don't cause a problem, and
4146 we'd like to match them with a future
4147 declaration. */
4148 value = ovl_insert (new_fn, value, true);
4149 }
4150 }
4151 else if (value
4152 /* Ignore anticipated builtins. */
4153 && !anticipated_builtin_p (value)
4154 && !decls_match (lookup.value, value))
4155 diagnose_name_conflict (lookup.value, value);
4156 else
4157 value = lookup.value;
4158 }
4159
4160 if (lookup.type && lookup.type != type)
4161 {
4162 if (type && !decls_match (lookup.type, type))
4163 diagnose_name_conflict (lookup.type, type);
4164 else
4165 type = lookup.type;
4166 }
4167
4168 /* If bind->value is empty, shift any class or enumeration name back. */
4169 if (!value)
4170 {
4171 value = type;
4172 type = NULL_TREE;
4173 }
4174
4175 *value_p = value;
4176 *type_p = type;
4177}
4178
4179/* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4180 Both are namespaces. */
4181
4182bool
4183is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4184{
4185 int depth = SCOPE_DEPTH (ancestor);
4186
4187 if (!depth && !inline_only)
4188 /* The global namespace encloses everything. */
4189 return true;
4190
4191 while (SCOPE_DEPTH (descendant) > depth
4192 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4193 descendant = CP_DECL_CONTEXT (descendant);
4194
4195 return ancestor == descendant;
4196}
4197
4198/* Returns true if ROOT (a namespace, class, or function) encloses
4199 CHILD. CHILD may be either a class type or a namespace. */
4200
4201bool
4202is_ancestor (tree root, tree child)
4203{
4204 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4205 || TREE_CODE (root) == FUNCTION_DECL
4206 || CLASS_TYPE_P (root)));
4207 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4208 || CLASS_TYPE_P (child)));
4209
4210 /* The global namespace encloses everything. */
4211 if (root == global_namespace)
4212 return true;
4213
4214 /* Search until we reach namespace scope. */
4215 while (TREE_CODE (child) != NAMESPACE_DECL)
4216 {
4217 /* If we've reached the ROOT, it encloses CHILD. */
4218 if (root == child)
4219 return true;
4220 /* Go out one level. */
4221 if (TYPE_P (child))
4222 child = TYPE_NAME (child);
4223 child = CP_DECL_CONTEXT (child);
4224 }
4225
4226 if (TREE_CODE (root) == NAMESPACE_DECL)
4227 return is_nested_namespace (root, child);
4228
4229 return false;
4230}
4231
4232/* Enter the class or namespace scope indicated by T suitable for name
4233