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