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