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