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