]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/name-lookup.c
Convert pdp11 back end to CCmode.
[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 {
2559 pedwarn (input_location, 0,
7cd6ea64
NS
2560 "conflicting C language linkage declaration %q#D", decl);
2561 inform (DECL_SOURCE_LOCATION (old),
2562 "previous declaration %q#D", old);
539f481a 2563 if (mismatch < 0)
7cd6ea64
NS
2564 inform (input_location,
2565 "due to different exception specifications");
539f481a
NS
2566 }
2567 else
9bc3f420
NS
2568 {
2569 if (old == *slot)
2570 /* The hash table expects OVERLOADS, so construct one with
2571 OLD as both the function and the chain. This allocate
2572 an excess OVERLOAD node, but it's rare to have multiple
2573 extern "C" decls of the same name. And we save
2574 complicating the hash table logic (which is used
2575 elsewhere). */
2576 *slot = ovl_make (old, old);
2577
2578 slot = &OVL_CHAIN (*slot);
2579
2580 /* Chain it on for c_linkage_binding's use. */
2581 *slot = tree_cons (NULL_TREE, decl, *slot);
2582 }
539f481a 2583 }
9bc3f420
NS
2584 else
2585 *slot = decl;
539f481a
NS
2586}
2587
2588/* Returns a list of C-linkage decls with the name NAME. Used in
2589 c-family/c-pragma.c to implement redefine_extname pragma. */
2590
2591tree
2592c_linkage_bindings (tree name)
2593{
7cd6ea64
NS
2594 if (extern_c_decls)
2595 if (tree *slot = extern_c_decls
9bc3f420
NS
2596 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2597 {
2598 tree result = *slot;
2599 if (TREE_CODE (result) == OVERLOAD)
2600 result = OVL_CHAIN (result);
2601 return result;
2602 }
2603
539f481a
NS
2604 return NULL_TREE;
2605}
2606
c0c24822
NS
2607/* DECL is being declared at a local scope. Emit suitable shadow
2608 warnings. */
2609
2610static void
2611check_local_shadow (tree decl)
2612{
2613 /* Don't complain about the parms we push and then pop
2614 while tentatively parsing a function declarator. */
2615 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2616 return;
2617
2618 /* Inline decls shadow nothing. */
2619 if (DECL_FROM_INLINE (decl))
2620 return;
2621
2622 /* External decls are something else. */
2623 if (DECL_EXTERNAL (decl))
2624 return;
2625
2626 tree old = NULL_TREE;
2627 cp_binding_level *old_scope = NULL;
2628 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2629 {
2630 old = binding->value;
2631 old_scope = binding->scope;
2632 }
c0c24822
NS
2633
2634 tree shadowed = NULL_TREE;
2635 if (old
2636 && (TREE_CODE (old) == PARM_DECL
2637 || VAR_P (old)
2638 || (TREE_CODE (old) == TYPE_DECL
2639 && (!DECL_ARTIFICIAL (old)
2640 || TREE_CODE (decl) == TYPE_DECL)))
006fbbe9 2641 && DECL_FUNCTION_SCOPE_P (old)
c0c24822
NS
2642 && (!DECL_ARTIFICIAL (decl)
2643 || DECL_IMPLICIT_TYPEDEF_P (decl)
2644 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2645 {
2646 /* DECL shadows a local thing possibly of interest. */
2647
2648 /* Don't complain if it's from an enclosing function. */
2649 if (DECL_CONTEXT (old) == current_function_decl
2650 && TREE_CODE (decl) != PARM_DECL
2651 && TREE_CODE (old) == PARM_DECL)
2652 {
2653 /* Go to where the parms should be and see if we find
2654 them there. */
2655 cp_binding_level *b = current_binding_level->level_chain;
2656
2657 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2658 /* Skip the ctor/dtor cleanup level. */
2659 b = b->level_chain;
2660
2661 /* ARM $8.3 */
2662 if (b->kind == sk_function_parms)
2663 {
2664 error ("declaration of %q#D shadows a parameter", decl);
2665 return;
2666 }
2667 }
2668
2669 /* The local structure or class can't use parameters of
2670 the containing function anyway. */
2671 if (DECL_CONTEXT (old) != current_function_decl)
2672 {
2673 for (cp_binding_level *scope = current_binding_level;
2674 scope != old_scope; scope = scope->level_chain)
2675 if (scope->kind == sk_class
2676 && !LAMBDA_TYPE_P (scope->this_entity))
2677 return;
2678 }
2679 /* Error if redeclaring a local declared in a
2680 init-statement or in the condition of an if or
2681 switch statement when the new declaration is in the
2682 outermost block of the controlled statement.
2683 Redeclaring a variable from a for or while condition is
2684 detected elsewhere. */
2685 else if (VAR_P (old)
2686 && old_scope == current_binding_level->level_chain
2687 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2688 {
2689 error ("redeclaration of %q#D", decl);
2690 inform (DECL_SOURCE_LOCATION (old),
2691 "%q#D previously declared here", old);
2692 return;
2693 }
2694 /* C++11:
2695 3.3.3/3: The name declared in an exception-declaration (...)
2696 shall not be redeclared in the outermost block of the handler.
2697 3.3.3/2: A parameter name shall not be redeclared (...) in
2698 the outermost block of any handler associated with a
2699 function-try-block.
2700 3.4.1/15: The function parameter names shall not be redeclared
2701 in the exception-declaration nor in the outermost block of a
2702 handler for the function-try-block. */
2703 else if ((TREE_CODE (old) == VAR_DECL
2704 && old_scope == current_binding_level->level_chain
2705 && old_scope->kind == sk_catch)
2706 || (TREE_CODE (old) == PARM_DECL
2707 && (current_binding_level->kind == sk_catch
2708 || current_binding_level->level_chain->kind == sk_catch)
2709 && in_function_try_handler))
2710 {
2711 if (permerror (input_location, "redeclaration of %q#D", decl))
2712 inform (DECL_SOURCE_LOCATION (old),
2713 "%q#D previously declared here", old);
2714 return;
2715 }
2716
2717 /* If '-Wshadow=compatible-local' is specified without other
2718 -Wshadow= flags, we will warn only when the type of the
2719 shadowing variable (DECL) can be converted to that of the
2720 shadowed parameter (OLD_LOCAL). The reason why we only check
2721 if DECL's type can be converted to OLD_LOCAL's type (but not the
2722 other way around) is because when users accidentally shadow a
2723 parameter, more than often they would use the variable
2724 thinking (mistakenly) it's still the parameter. It would be
2725 rare that users would use the variable in the place that
2726 expects the parameter but thinking it's a new decl. */
2727
2728 enum opt_code warning_code;
2729 if (warn_shadow)
2730 warning_code = OPT_Wshadow;
2731 else if (warn_shadow_local)
2732 warning_code = OPT_Wshadow_local;
2733 else if (warn_shadow_compatible_local
9db84ece
NS
2734 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2735 || (!dependent_type_p (TREE_TYPE (decl))
2736 && !dependent_type_p (TREE_TYPE (old))
2737 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2738 tf_none))))
c0c24822
NS
2739 warning_code = OPT_Wshadow_compatible_local;
2740 else
2741 return;
2742
2743 const char *msg;
2744 if (TREE_CODE (old) == PARM_DECL)
2745 msg = "declaration of %q#D shadows a parameter";
2746 else if (is_capture_proxy (old))
2747 msg = "declaration of %qD shadows a lambda capture";
2748 else
2749 msg = "declaration of %qD shadows a previous local";
2750
2751 if (warning_at (input_location, warning_code, msg, decl))
2752 {
2753 shadowed = old;
2754 goto inform_shadowed;
2755 }
2756 return;
2757 }
2758
2759 if (!warn_shadow)
2760 return;
2761
2762 /* Don't warn for artificial things that are not implicit typedefs. */
2763 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2764 return;
2765
2766 if (nonlambda_method_basetype ())
2767 if (tree member = lookup_member (current_nonlambda_class_type (),
2768 DECL_NAME (decl), /*protect=*/0,
2769 /*want_type=*/false, tf_warning_or_error))
2770 {
2771 member = MAYBE_BASELINK_FUNCTIONS (member);
2772
2773 /* Warn if a variable shadows a non-function, or the variable
2774 is a function or a pointer-to-function. */
5256a7f5 2775 if (!OVL_P (member)
c0c24822
NS
2776 || TREE_CODE (decl) == FUNCTION_DECL
2777 || TYPE_PTRFN_P (TREE_TYPE (decl))
2778 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2779 {
2780 if (warning_at (input_location, OPT_Wshadow,
2781 "declaration of %qD shadows a member of %qT",
2782 decl, current_nonlambda_class_type ())
2783 && DECL_P (member))
2784 {
2785 shadowed = member;
2786 goto inform_shadowed;
2787 }
2788 }
2789 return;
2790 }
2791
2792 /* Now look for a namespace shadow. */
3c9cca88 2793 old = find_namespace_value (current_namespace, DECL_NAME (decl));
c0c24822
NS
2794 if (old
2795 && (VAR_P (old)
2796 || (TREE_CODE (old) == TYPE_DECL
2797 && (!DECL_ARTIFICIAL (old)
2798 || TREE_CODE (decl) == TYPE_DECL)))
2799 && !instantiating_current_function_p ())
2800 /* XXX shadow warnings in outer-more namespaces */
2801 {
2802 if (warning_at (input_location, OPT_Wshadow,
2803 "declaration of %qD shadows a global declaration",
2804 decl))
2805 {
2806 shadowed = old;
2807 goto inform_shadowed;
2808 }
2809 return;
2810 }
2811
2812 return;
2813
2814 inform_shadowed:
2815 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2816}
2817
e4ea7a4c
NS
2818/* DECL is being pushed inside function CTX. Set its context, if
2819 needed. */
2820
2821static void
2822set_decl_context_in_fn (tree ctx, tree decl)
2823{
2824 if (!DECL_CONTEXT (decl)
2825 /* A local declaration for a function doesn't constitute
2826 nesting. */
2827 && TREE_CODE (decl) != FUNCTION_DECL
2828 /* A local declaration for an `extern' variable is in the
2829 scope of the current namespace, not the current
2830 function. */
2831 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2832 /* When parsing the parameter list of a function declarator,
2833 don't set DECL_CONTEXT to an enclosing function. When we
2834 push the PARM_DECLs in order to process the function body,
2835 current_binding_level->this_entity will be set. */
2836 && !(TREE_CODE (decl) == PARM_DECL
2837 && current_binding_level->kind == sk_function_parms
2838 && current_binding_level->this_entity == NULL))
2839 DECL_CONTEXT (decl) = ctx;
2840
2841 /* If this is the declaration for a namespace-scope function,
2842 but the declaration itself is in a local scope, mark the
2843 declaration. */
2844 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2845 DECL_LOCAL_FUNCTION_P (decl) = 1;
2846}
2847
2848/* DECL is a local-scope decl with linkage. SHADOWED is true if the
2849 name is already bound at the current level.
2850
2851 [basic.link] If there is a visible declaration of an entity with
2852 linkage having the same name and type, ignoring entities declared
2853 outside the innermost enclosing namespace scope, the block scope
2854 declaration declares that same entity and receives the linkage of
2855 the previous declaration.
2856
2857 Also, make sure that this decl matches any existing external decl
2858 in the enclosing namespace. */
2859
2860static void
2861set_local_extern_decl_linkage (tree decl, bool shadowed)
2862{
2863 tree ns_value = decl; /* Unique marker. */
2864
2865 if (!shadowed)
2866 {
2867 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2868 if (!loc_value)
2869 {
2870 ns_value
3c9cca88 2871 = find_namespace_value (current_namespace, DECL_NAME (decl));
e4ea7a4c
NS
2872 loc_value = ns_value;
2873 }
8f3284a4
NS
2874 if (loc_value == error_mark_node
2875 /* An ambiguous lookup. */
2876 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
e4ea7a4c
NS
2877 loc_value = NULL_TREE;
2878
2879 for (ovl_iterator iter (loc_value); iter; ++iter)
ef4c5e78 2880 if (!iter.hidden_p ()
e4ea7a4c
NS
2881 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2882 && decls_match (*iter, decl))
2883 {
2884 /* The standard only says that the local extern inherits
2885 linkage from the previous decl; in particular, default
2886 args are not shared. Add the decl into a hash table to
2887 make sure only the previous decl in this case is seen
2888 by the middle end. */
2889 struct cxx_int_tree_map *h;
2890
2891 /* We inherit the outer decl's linkage. But we're a
2892 different decl. */
2893 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2894
2895 if (cp_function_chain->extern_decl_map == NULL)
2896 cp_function_chain->extern_decl_map
2897 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2898
2899 h = ggc_alloc<cxx_int_tree_map> ();
2900 h->uid = DECL_UID (decl);
2901 h->to = *iter;
2902 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2903 ->find_slot (h, INSERT);
2904 *loc = h;
2905 break;
2906 }
2907 }
2908
2909 if (TREE_PUBLIC (decl))
2910 {
2911 /* DECL is externally visible. Make sure it matches a matching
3c9cca88 2912 decl in the namespace scope. We only really need to check
e4ea7a4c
NS
2913 this when inserting the decl, not when we find an existing
2914 match in the current scope. However, in practice we're
2915 going to be inserting a new decl in the majority of cases --
2916 who writes multiple extern decls for the same thing in the
2917 same local scope? Doing it here often avoids a duplicate
2918 namespace lookup. */
2919
2920 /* Avoid repeating a lookup. */
2921 if (ns_value == decl)
3c9cca88 2922 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
e4ea7a4c 2923
8f3284a4
NS
2924 if (ns_value == error_mark_node
2925 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
e4ea7a4c
NS
2926 ns_value = NULL_TREE;
2927
2928 for (ovl_iterator iter (ns_value); iter; ++iter)
2929 {
2930 tree other = *iter;
2931
2932 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2933 ; /* Not externally visible. */
2934 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2935 ; /* Both are extern "C", we'll check via that mechanism. */
2936 else if (TREE_CODE (other) != TREE_CODE (decl)
2937 || ((VAR_P (decl) || matching_fn_p (other, decl))
2938 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2939 COMPARE_REDECLARATION)))
2940 {
2941 if (permerror (DECL_SOURCE_LOCATION (decl),
2942 "local external declaration %q#D", decl))
2943 inform (DECL_SOURCE_LOCATION (other),
2944 "does not match previous declaration %q#D", other);
2945 break;
2946 }
2947 }
2948 }
2949}
2950
3a9cc685
NS
2951/* Record DECL as belonging to the current lexical scope. Check for
2952 errors (such as an incompatible declaration for the same name
2953 already seen in the same scope). IS_FRIEND is true if DECL is
b655c310 2954 declared as a friend.
00e8de68 2955
3a9cc685
NS
2956 Returns either DECL or an old decl for the same name. If an old
2957 decl is returned, it may have been smashed to agree with what DECL
2958 says. */
89b578be 2959
b655c310 2960static tree
3a9cc685 2961do_pushdecl (tree decl, bool is_friend)
89b578be 2962{
3a9cc685 2963 if (decl == error_mark_node)
b655c310 2964 return error_mark_node;
00e8de68 2965
3a9cc685
NS
2966 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2967 set_decl_context_in_fn (current_function_decl, decl);
c8094d83 2968
3a9cc685
NS
2969 /* The binding level we will be pushing into. During local class
2970 pushing, we want to push to the containing scope. */
2971 cp_binding_level *level = current_binding_level;
2972 while (level->kind == sk_class)
2973 level = level->level_chain;
00e8de68 2974
e833f686
NS
2975 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2976 insert it. Other NULL-named decls, not so much. */
2977 tree name = DECL_NAME (decl);
2978 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
00e8de68 2979 {
3c9cca88 2980 cxx_binding *binding = NULL; /* Local scope binding. */
3a9cc685 2981 tree ns = NULL_TREE; /* Searched namespace. */
3c9cca88 2982 tree *slot = NULL; /* Binding slot in namespace. */
3a9cc685 2983 tree old = NULL_TREE;
00e8de68 2984
3a9cc685 2985 if (level->kind == sk_namespace)
b655c310 2986 {
3a9cc685
NS
2987 /* We look in the decl's namespace for an existing
2988 declaration, even though we push into the current
2989 namespace. */
2990 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2991 ? CP_DECL_CONTEXT (decl) : current_namespace);
2992 /* Create the binding, if this is current namespace, because
2993 that's where we'll be pushing anyway. */
3c9cca88
NS
2994 slot = find_namespace_slot (ns, name, ns == current_namespace);
2995 if (slot)
2996 old = MAYBE_STAT_DECL (*slot);
b655c310 2997 }
3a9cc685 2998 else
3c9cca88
NS
2999 {
3000 binding = find_local_binding (level, name);
3001 if (binding)
3002 old = binding->value;
3003 }
00e8de68 3004
3a9cc685
NS
3005 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3006 && DECL_EXTERNAL (decl))
3007 set_local_extern_decl_linkage (decl, old != NULL_TREE);
00e8de68 3008
3a9cc685
NS
3009 if (old == error_mark_node)
3010 old = NULL_TREE;
00e8de68 3011
3a9cc685
NS
3012 for (ovl_iterator iter (old); iter; ++iter)
3013 if (iter.using_p ())
3014 ; /* Ignore using decls here. */
3015 else if (tree match = duplicate_decls (decl, *iter, is_friend))
ef4c5e78 3016 {
274c1516
NS
3017 if (match == error_mark_node)
3018 ;
3019 else if (TREE_CODE (match) == TYPE_DECL)
3020 /* The IDENTIFIER will have the type referring to the
3021 now-smashed TYPE_DECL, because ...? Reset it. */
3022 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3023 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
ef4c5e78
NS
3024 {
3025 /* Unhiding a previously hidden decl. */
3026 tree head = iter.reveal_node (old);
3027 if (head != old)
3028 {
3029 if (!ns)
3c9cca88
NS
3030 {
3031 update_local_overload (binding, head);
3032 binding->value = head;
3033 }
3034 else if (STAT_HACK_P (*slot))
3035 STAT_DECL (*slot) = head;
3036 else
3037 *slot = head;
ef4c5e78 3038 }
7cd6ea64
NS
3039 if (DECL_EXTERN_C_P (match))
3040 /* We need to check and register the decl now. */
ef4c5e78
NS
3041 check_extern_c_conflict (match);
3042 }
3043 return match;
3044 }
c8094d83 3045
3a9cc685 3046 /* We are pushing a new decl. */
00e8de68 3047
ef4c5e78
NS
3048 /* Skip a hidden builtin we failed to match already. There can
3049 only be one. */
3050 if (old && anticipated_builtin_p (old))
3051 old = OVL_CHAIN (old);
5294e4c3 3052
3a9cc685 3053 check_template_shadow (decl);
00e8de68 3054
3a9cc685 3055 if (DECL_DECLARES_FUNCTION_P (decl))
b655c310 3056 {
3a9cc685 3057 check_default_args (decl);
00e8de68 3058
3a9cc685 3059 if (is_friend)
b655c310 3060 {
3a9cc685 3061 if (level->kind != sk_namespace)
b1a7e33d
NS
3062 {
3063 /* In a local class, a friend function declaration must
3064 find a matching decl in the innermost non-class scope.
3065 [class.friend/11] */
3066 error ("friend declaration %qD in local class without "
3067 "prior local declaration", decl);
3068 /* Don't attempt to push it. */
3069 return error_mark_node;
3070 }
6c072e21
NS
3071 /* Hide it from ordinary lookup. */
3072 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
b655c310
NS
3073 }
3074 }
00e8de68 3075
3a9cc685 3076 if (level->kind != sk_namespace)
b655c310 3077 {
3a9cc685 3078 check_local_shadow (decl);
00e8de68 3079
3a9cc685
NS
3080 if (TREE_CODE (decl) == NAMESPACE_DECL)
3081 /* A local namespace alias. */
3082 set_identifier_type_value (name, NULL_TREE);
00e8de68 3083
3a9cc685
NS
3084 if (!binding)
3085 binding = create_local_binding (level, name);
b655c310 3086 }
3c9cca88 3087 else if (!slot)
b655c310 3088 {
3a9cc685 3089 ns = current_namespace;
3c9cca88 3090 slot = find_namespace_slot (ns, name, true);
be3b7dcf
NS
3091 /* Update OLD to reflect the namespace we're going to be
3092 pushing into. */
3093 old = MAYBE_STAT_DECL (*slot);
b655c310 3094 }
fdf03377 3095
3c9cca88 3096 old = update_binding (level, binding, slot, old, decl, is_friend);
fdf03377 3097
3a9cc685
NS
3098 if (old != decl)
3099 /* An existing decl matched, use it. */
3100 decl = old;
3101 else if (TREE_CODE (decl) == TYPE_DECL)
3102 {
3103 tree type = TREE_TYPE (decl);
00e8de68 3104
3a9cc685 3105 if (type != error_mark_node)
b655c310 3106 {
3a9cc685
NS
3107 if (TYPE_NAME (type) != decl)
3108 set_underlying_type (decl);
00e8de68 3109
3a9cc685
NS
3110 if (!ns)
3111 set_identifier_type_value_with_scope (name, decl, level);
b655c310 3112 else
3a9cc685 3113 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
b655c310 3114 }
7b3b6ae4 3115
3a9cc685
NS
3116 /* If this is a locally defined typedef in a function that
3117 is not a template instantation, record it to implement
3118 -Wunused-local-typedefs. */
3119 if (!instantiating_current_function_p ())
3120 record_locally_defined_typedef (decl);
3121 }
3122 else if (VAR_P (decl))
3123 maybe_register_incomplete_var (decl);
7cd6ea64
NS
3124
3125 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3126 && DECL_EXTERN_C_P (decl))
3a9cc685 3127 check_extern_c_conflict (decl);
00e8de68 3128 }
3a9cc685
NS
3129 else
3130 add_decl_to_level (level, decl);
00e8de68 3131
3a9cc685 3132 return decl;
00e8de68
GDR
3133}
3134
4f15a5da 3135/* Record a decl-node X as belonging to the current lexical scope.
32196b87
NS
3136 It's a friend if IS_FRIEND is true -- which affects exactly where
3137 we push it. */
575bfb00
LC
3138
3139tree
4f15a5da 3140pushdecl (tree x, bool is_friend)
575bfb00 3141{
b655c310 3142 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
32196b87 3143 tree ret = do_pushdecl (x, is_friend);
b655c310 3144 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
575bfb00
LC
3145 return ret;
3146}
3147
b655c310
NS
3148/* Enter DECL into the symbol table, if that's appropriate. Returns
3149 DECL, or a modified version thereof. */
00e8de68 3150
b655c310
NS
3151tree
3152maybe_push_decl (tree decl)
00e8de68 3153{
b655c310 3154 tree type = TREE_TYPE (decl);
00e8de68 3155
b655c310
NS
3156 /* Add this decl to the current binding level, but not if it comes
3157 from another scope, e.g. a static member variable. TEM may equal
3158 DECL or it may be a previous decl of the same name. */
3159 if (decl == error_mark_node
3160 || (TREE_CODE (decl) != PARM_DECL
3161 && DECL_CONTEXT (decl) != NULL_TREE
3162 /* Definitions of namespace members outside their namespace are
3163 possible. */
3164 && !DECL_NAMESPACE_SCOPE_P (decl))
3165 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3166 || type == unknown_type_node
3167 /* The declaration of a template specialization does not affect
3168 the functions available for overload resolution, so we do not
3169 call pushdecl. */
3170 || (TREE_CODE (decl) == FUNCTION_DECL
3171 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3172 return decl;
00e8de68 3173 else
b655c310 3174 return pushdecl (decl);
00e8de68
GDR
3175}
3176
b655c310 3177/* Bind DECL to ID in the current_binding_level, assumed to be a local
9d029ddf
NS
3178 binding level. If IS_USING is true, DECL got here through a
3179 using-declaration. */
00e8de68 3180
9d029ddf
NS
3181static void
3182push_local_binding (tree id, tree decl, bool is_using)
00e8de68 3183{
b655c310
NS
3184 /* Skip over any local classes. This makes sense if we call
3185 push_local_binding with a friend decl of a local class. */
3c9cca88 3186 cp_binding_level *b = innermost_nonclass_level ();
5ad4f1c8 3187
3c9cca88
NS
3188 gcc_assert (b->kind != sk_namespace);
3189 if (find_local_binding (b, id))
b655c310
NS
3190 {
3191 /* Supplement the existing binding. */
3192 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3193 /* It didn't work. Something else must be bound at this
3194 level. Do not add DECL to the list of things to pop
3195 later. */
3196 return;
3197 }
3198 else
3199 /* Create a new binding. */
3200 push_binding (id, decl, b);
5a167978 3201
9d029ddf
NS
3202 if (TREE_CODE (decl) == OVERLOAD || is_using)
3203 /* We must put the OVERLOAD or using into a TREE_LIST since we
3204 cannot use the decl's chain itself. */
b655c310 3205 decl = build_tree_list (NULL_TREE, decl);
5a167978 3206
b655c310
NS
3207 /* And put DECL on the list of things declared by the current
3208 binding level. */
9d029ddf 3209 add_decl_to_level (b, decl);
5a167978
GDR
3210}
3211
b655c310
NS
3212\f
3213/* true means unconditionally make a BLOCK for the next level pushed. */
a5e6b29b 3214
b655c310 3215static bool keep_next_level_flag;
d5f4eddd 3216
b655c310 3217static int binding_depth = 0;
d5f4eddd 3218
b655c310
NS
3219static void
3220indent (int depth)
d5f4eddd 3221{
b655c310 3222 int i;
d5f4eddd 3223
b655c310
NS
3224 for (i = 0; i < depth * 2; i++)
3225 putc (' ', stderr);
d5f4eddd
JM
3226}
3227
b655c310
NS
3228/* Return a string describing the kind of SCOPE we have. */
3229static const char *
3230cp_binding_level_descriptor (cp_binding_level *scope)
ed3cf953 3231{
b655c310
NS
3232 /* The order of this table must match the "scope_kind"
3233 enumerators. */
3234 static const char* scope_kind_names[] = {
3235 "block-scope",
3236 "cleanup-scope",
3237 "try-scope",
3238 "catch-scope",
3239 "for-scope",
3240 "function-parameter-scope",
3241 "class-scope",
3242 "namespace-scope",
3243 "template-parameter-scope",
3244 "template-explicit-spec-scope"
3245 };
3246 const scope_kind kind = scope->explicit_spec_p
3247 ? sk_template_spec : scope->kind;
ed3cf953 3248
b655c310 3249 return scope_kind_names[kind];
ed3cf953
GDR
3250}
3251
b655c310
NS
3252/* Output a debugging information about SCOPE when performing
3253 ACTION at LINE. */
3254static void
3255cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
ed3cf953 3256{
b655c310
NS
3257 const char *desc = cp_binding_level_descriptor (scope);
3258 if (scope->this_entity)
0f2c4a8f 3259 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
b655c310
NS
3260 scope->this_entity, (void *) scope, line);
3261 else
3262 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
ed3cf953
GDR
3263}
3264
b655c310
NS
3265/* Return the estimated initial size of the hashtable of a NAMESPACE
3266 scope. */
ed3cf953 3267
b655c310
NS
3268static inline size_t
3269namespace_scope_ht_size (tree ns)
ed3cf953 3270{
b655c310 3271 tree name = DECL_NAME (ns);
ed3cf953 3272
b655c310
NS
3273 return name == std_identifier
3274 ? NAMESPACE_STD_HT_SIZE
ad9870f2 3275 : (name == global_identifier
b655c310
NS
3276 ? GLOBAL_SCOPE_HT_SIZE
3277 : NAMESPACE_ORDINARY_HT_SIZE);
ed3cf953 3278}
ed3cf953 3279
b655c310 3280/* A chain of binding_level structures awaiting reuse. */
ecba6c56 3281
b655c310 3282static GTY((deletable)) cp_binding_level *free_binding_level;
ecba6c56 3283
b655c310 3284/* Insert SCOPE as the innermost binding level. */
ecba6c56 3285
b655c310
NS
3286void
3287push_binding_level (cp_binding_level *scope)
3288{
3289 /* Add it to the front of currently active scopes stack. */
3290 scope->level_chain = current_binding_level;
3291 current_binding_level = scope;
3292 keep_next_level_flag = false;
3293
3294 if (ENABLE_SCOPE_CHECKING)
ecba6c56 3295 {
b655c310
NS
3296 scope->binding_depth = binding_depth;
3297 indent (binding_depth);
3298 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3299 "push");
3300 binding_depth++;
ecba6c56 3301 }
ecba6c56
DS
3302}
3303
b655c310
NS
3304/* Create a new KIND scope and make it the top of the active scopes stack.
3305 ENTITY is the scope of the associated C++ entity (namespace, class,
3306 function, C++0x enumeration); it is NULL otherwise. */
3a636414 3307
b655c310
NS
3308cp_binding_level *
3309begin_scope (scope_kind kind, tree entity)
3a636414 3310{
b655c310 3311 cp_binding_level *scope;
3a636414 3312
b655c310
NS
3313 /* Reuse or create a struct for this binding level. */
3314 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3a636414 3315 {
b655c310
NS
3316 scope = free_binding_level;
3317 free_binding_level = scope->level_chain;
3318 memset (scope, 0, sizeof (cp_binding_level));
3a636414 3319 }
b655c310
NS
3320 else
3321 scope = ggc_cleared_alloc<cp_binding_level> ();
3a636414 3322
b655c310
NS
3323 scope->this_entity = entity;
3324 scope->more_cleanups_ok = true;
3325 switch (kind)
3326 {
3327 case sk_cleanup:
3328 scope->keep = true;
3329 break;
a5e6b29b 3330
b655c310
NS
3331 case sk_template_spec:
3332 scope->explicit_spec_p = true;
3333 kind = sk_template_parms;
3334 /* Fall through. */
3335 case sk_template_parms:
3336 case sk_block:
3337 case sk_try:
3338 case sk_catch:
3339 case sk_for:
3340 case sk_cond:
3341 case sk_class:
3342 case sk_scoped_enum:
3343 case sk_function_parms:
3344 case sk_transaction:
3345 case sk_omp:
3346 scope->keep = keep_next_level_flag;
3347 break;
a5e6b29b 3348
b655c310
NS
3349 case sk_namespace:
3350 NAMESPACE_LEVEL (entity) = scope;
a5e6b29b 3351 break;
b655c310
NS
3352
3353 default:
3354 /* Should not happen. */
3355 gcc_unreachable ();
3356 break;
3357 }
3358 scope->kind = kind;
3359
3360 push_binding_level (scope);
3361
3362 return scope;
575bfb00
LC
3363}
3364
b655c310
NS
3365/* We're about to leave current scope. Pop the top of the stack of
3366 currently active scopes. Return the enclosing scope, now active. */
575bfb00 3367
b655c310
NS
3368cp_binding_level *
3369leave_scope (void)
575bfb00 3370{
b655c310 3371 cp_binding_level *scope = current_binding_level;
a5e6b29b 3372
b655c310
NS
3373 if (scope->kind == sk_namespace && class_binding_level)
3374 current_binding_level = class_binding_level;
2b8dfc07 3375
b655c310
NS
3376 /* We cannot leave a scope, if there are none left. */
3377 if (NAMESPACE_LEVEL (global_namespace))
3378 gcc_assert (!global_scope_p (scope));
ed3cf953 3379
b655c310
NS
3380 if (ENABLE_SCOPE_CHECKING)
3381 {
3382 indent (--binding_depth);
3383 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3384 "leave");
3385 }
ed3cf953 3386
b655c310
NS
3387 /* Move one nesting level up. */
3388 current_binding_level = scope->level_chain;
3389
3390 /* Namespace-scopes are left most probably temporarily, not
3391 completely; they can be reopened later, e.g. in namespace-extension
3392 or any name binding activity that requires us to resume a
3393 namespace. For classes, we cache some binding levels. For other
3394 scopes, we just make the structure available for reuse. */
3395 if (scope->kind != sk_namespace
3396 && scope->kind != sk_class)
00e8de68 3397 {
b655c310
NS
3398 scope->level_chain = free_binding_level;
3399 gcc_assert (!ENABLE_SCOPE_CHECKING
3400 || scope->binding_depth == binding_depth);
3401 free_binding_level = scope;
00e8de68 3402 }
b655c310
NS
3403
3404 if (scope->kind == sk_class)
00e8de68 3405 {
b655c310
NS
3406 /* Reset DEFINING_CLASS_P to allow for reuse of a
3407 class-defining scope in a non-defining context. */
3408 scope->defining_class_p = 0;
3409
3410 /* Find the innermost enclosing class scope, and reset
3411 CLASS_BINDING_LEVEL appropriately. */
3412 class_binding_level = NULL;
3413 for (scope = current_binding_level; scope; scope = scope->level_chain)
3414 if (scope->kind == sk_class)
3415 {
3416 class_binding_level = scope;
3417 break;
3418 }
00e8de68 3419 }
b655c310
NS
3420
3421 return current_binding_level;
00e8de68 3422}
575bfb00 3423
b655c310
NS
3424static void
3425resume_scope (cp_binding_level* b)
575bfb00 3426{
b655c310
NS
3427 /* Resuming binding levels is meant only for namespaces,
3428 and those cannot nest into classes. */
3429 gcc_assert (!class_binding_level);
3430 /* Also, resuming a non-directly nested namespace is a no-no. */
3431 gcc_assert (b->level_chain == current_binding_level);
3432 current_binding_level = b;
3433 if (ENABLE_SCOPE_CHECKING)
3434 {
3435 b->binding_depth = binding_depth;
3436 indent (binding_depth);
3437 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3438 binding_depth++;
3439 }
575bfb00
LC
3440}
3441
b655c310 3442/* Return the innermost binding level that is not for a class scope. */
fde6f97e 3443
b655c310
NS
3444static cp_binding_level *
3445innermost_nonclass_level (void)
fde6f97e 3446{
b655c310 3447 cp_binding_level *b;
fde6f97e 3448
b655c310
NS
3449 b = current_binding_level;
3450 while (b->kind == sk_class)
3451 b = b->level_chain;
fde6f97e 3452
b655c310 3453 return b;
fde6f97e 3454}
00e8de68 3455
b655c310
NS
3456/* We're defining an object of type TYPE. If it needs a cleanup, but
3457 we're not allowed to add any more objects with cleanups to the current
3458 scope, create a new binding level. */
a5e6b29b 3459
b655c310
NS
3460void
3461maybe_push_cleanup_level (tree type)
a5e6b29b 3462{
b655c310
NS
3463 if (type != error_mark_node
3464 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3465 && current_binding_level->more_cleanups_ok == 0)
a5e6b29b 3466 {
b655c310
NS
3467 begin_scope (sk_cleanup, NULL);
3468 current_binding_level->statement_list = push_stmt_list ();
3469 }
3470}
a5e6b29b 3471
b655c310 3472/* Return true if we are in the global binding level. */
a5e6b29b 3473
b655c310
NS
3474bool
3475global_bindings_p (void)
3476{
3477 return global_scope_p (current_binding_level);
3478}
a5e6b29b 3479
b655c310
NS
3480/* True if we are currently in a toplevel binding level. This
3481 means either the global binding level or a namespace in a toplevel
3482 binding level. Since there are no non-toplevel namespace levels,
3483 this really means any namespace or template parameter level. We
3484 also include a class whose context is toplevel. */
1a32490a 3485
b655c310
NS
3486bool
3487toplevel_bindings_p (void)
3488{
3489 cp_binding_level *b = innermost_nonclass_level ();
a5e6b29b 3490
b655c310
NS
3491 return b->kind == sk_namespace || b->kind == sk_template_parms;
3492}
a5e6b29b 3493
b655c310
NS
3494/* True if this is a namespace scope, or if we are defining a class
3495 which is itself at namespace scope, or whose enclosing class is
3496 such a class, etc. */
a5e6b29b 3497
b655c310
NS
3498bool
3499namespace_bindings_p (void)
3500{
3501 cp_binding_level *b = innermost_nonclass_level ();
a5e6b29b 3502
b655c310
NS
3503 return b->kind == sk_namespace;
3504}
a5e6b29b 3505
b655c310 3506/* True if the innermost non-class scope is a block scope. */
a5e6b29b 3507
b655c310
NS
3508bool
3509local_bindings_p (void)
3510{
3511 cp_binding_level *b = innermost_nonclass_level ();
3512 return b->kind < sk_function_parms || b->kind == sk_omp;
3513}
a5e6b29b 3514
b655c310 3515/* True if the current level needs to have a BLOCK made. */
a5e6b29b 3516
b655c310
NS
3517bool
3518kept_level_p (void)
3519{
3520 return (current_binding_level->blocks != NULL_TREE
3521 || current_binding_level->keep
3522 || current_binding_level->kind == sk_cleanup
3523 || current_binding_level->names != NULL_TREE
3524 || current_binding_level->using_directives);
575bfb00
LC
3525}
3526
b655c310 3527/* Returns the kind of the innermost scope. */
575bfb00 3528
b655c310
NS
3529scope_kind
3530innermost_scope_kind (void)
575bfb00 3531{
b655c310 3532 return current_binding_level->kind;
a5e6b29b
GDR
3533}
3534
b655c310 3535/* Returns true if this scope was created to store template parameters. */
5a167978 3536
b655c310
NS
3537bool
3538template_parm_scope_p (void)
5a167978 3539{
b655c310
NS
3540 return innermost_scope_kind () == sk_template_parms;
3541}
7756db03 3542
b655c310
NS
3543/* If KEEP is true, make a BLOCK node for the next binding level,
3544 unconditionally. Otherwise, use the normal logic to decide whether
3545 or not to create a BLOCK. */
5a167978 3546
b655c310
NS
3547void
3548keep_next_level (bool keep)
3549{
3550 keep_next_level_flag = keep;
3551}
5a167978 3552
9c82d7b6 3553/* Return the list of declarations of the current local scope. */
5a167978 3554
b655c310 3555tree
9c82d7b6 3556get_local_decls (void)
b655c310 3557{
9c82d7b6
NS
3558 gcc_assert (current_binding_level->kind != sk_namespace
3559 && current_binding_level->kind != sk_class);
b655c310
NS
3560 return current_binding_level->names;
3561}
5a167978 3562
b655c310 3563/* Return how many function prototypes we are currently nested inside. */
5a167978 3564
b655c310
NS
3565int
3566function_parm_depth (void)
3567{
3568 int level = 0;
3569 cp_binding_level *b;
8597cab1 3570
b655c310
NS
3571 for (b = current_binding_level;
3572 b->kind == sk_function_parms;
3573 b = b->level_chain)
3574 ++level;
3575
3576 return level;
5a167978
GDR
3577}
3578
b655c310
NS
3579/* For debugging. */
3580static int no_print_functions = 0;
3581static int no_print_builtins = 0;
5a167978
GDR
3582
3583static void
b655c310 3584print_binding_level (cp_binding_level* lvl)
5a167978 3585{
b655c310
NS
3586 tree t;
3587 int i = 0, len;
3588 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3589 if (lvl->more_cleanups_ok)
3590 fprintf (stderr, " more-cleanups-ok");
3591 if (lvl->have_cleanups)
3592 fprintf (stderr, " have-cleanups");
3593 fprintf (stderr, "\n");
3594 if (lvl->names)
3595 {
3596 fprintf (stderr, " names:\t");
3597 /* We can probably fit 3 names to a line? */
3598 for (t = lvl->names; t; t = TREE_CHAIN (t))
3599 {
3600 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3601 continue;
3602 if (no_print_builtins
3603 && (TREE_CODE (t) == TYPE_DECL)
3604 && DECL_IS_BUILTIN (t))
3605 continue;
5a167978 3606
b655c310
NS
3607 /* Function decls tend to have longer names. */
3608 if (TREE_CODE (t) == FUNCTION_DECL)
3609 len = 3;
3610 else
3611 len = 2;
3612 i += len;
3613 if (i > 6)
3614 {
3615 fprintf (stderr, "\n\t");
3616 i = len;
3617 }
3618 print_node_brief (stderr, "", t, 0);
3619 if (t == error_mark_node)
3620 break;
3621 }
3622 if (i)
3623 fprintf (stderr, "\n");
3624 }
3625 if (vec_safe_length (lvl->class_shadowed))
5a167978 3626 {
b655c310
NS
3627 size_t i;
3628 cp_class_binding *b;
3629 fprintf (stderr, " class-shadowed:");
3630 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3631 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3632 fprintf (stderr, "\n");
5a167978 3633 }
b655c310 3634 if (lvl->type_shadowed)
44fd0e80 3635 {
b655c310
NS
3636 fprintf (stderr, " type-shadowed:");
3637 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3638 {
3639 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3640 }
3641 fprintf (stderr, "\n");
44fd0e80 3642 }
b655c310 3643}
44fd0e80 3644
b655c310
NS
3645DEBUG_FUNCTION void
3646debug (cp_binding_level &ref)
3647{
3648 print_binding_level (&ref);
3649}
3650
3651DEBUG_FUNCTION void
3652debug (cp_binding_level *ptr)
3653{
3654 if (ptr)
3655 debug (*ptr);
3656 else
3657 fprintf (stderr, "<nil>\n");
3658}
3659
3660
d4e15523 3661static void
b655c310
NS
3662print_other_binding_stack (cp_binding_level *stack)
3663{
3664 cp_binding_level *level;
3665 for (level = stack; !global_scope_p (level); level = level->level_chain)
44fd0e80 3666 {
b655c310
NS
3667 fprintf (stderr, "binding level %p\n", (void *) level);
3668 print_binding_level (level);
44fd0e80 3669 }
b655c310 3670}
44fd0e80 3671
b655c310
NS
3672void
3673print_binding_stack (void)
3674{
3675 cp_binding_level *b;
3676 fprintf (stderr, "current_binding_level=%p\n"
3677 "class_binding_level=%p\n"
3678 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3679 (void *) current_binding_level, (void *) class_binding_level,
3680 (void *) NAMESPACE_LEVEL (global_namespace));
3681 if (class_binding_level)
5a167978 3682 {
b655c310
NS
3683 for (b = class_binding_level; b; b = b->level_chain)
3684 if (b == current_binding_level)
3685 break;
3686 if (b)
3687 b = class_binding_level;
3688 else
3689 b = current_binding_level;
3690 }
3691 else
3692 b = current_binding_level;
3693 print_other_binding_stack (b);
3694 fprintf (stderr, "global:\n");
3695 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3696}
3697\f
3698/* Return the type associated with ID. */
5a167978 3699
b655c310
NS
3700static tree
3701identifier_type_value_1 (tree id)
3702{
3703 /* There is no type with that name, anywhere. */
3704 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3705 return NULL_TREE;
3706 /* This is not the type marker, but the real thing. */
3707 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3708 return REAL_IDENTIFIER_TYPE_VALUE (id);
3709 /* Have to search for it. It must be on the global level, now.
3710 Ask lookup_name not to return non-types. */
3711 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3712 if (id)
3713 return TREE_TYPE (id);
3714 return NULL_TREE;
3715}
44fd0e80 3716
b655c310 3717/* Wrapper for identifier_type_value_1. */
1374a761 3718
b655c310
NS
3719tree
3720identifier_type_value (tree id)
3721{
3722 tree ret;
3723 timevar_start (TV_NAME_LOOKUP);
3724 ret = identifier_type_value_1 (id);
3725 timevar_stop (TV_NAME_LOOKUP);
3726 return ret;
3727}
44fd0e80 3728
b655c310
NS
3729/* Push a definition of struct, union or enum tag named ID. into
3730 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3731 the tag ID is not already defined. */
1374a761 3732
b655c310
NS
3733static void
3734set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3735{
3736 tree type;
5a167978 3737
b655c310 3738 if (b->kind != sk_namespace)
5a167978 3739 {
b655c310
NS
3740 /* Shadow the marker, not the real thing, so that the marker
3741 gets restored later. */
3742 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3743 b->type_shadowed
3744 = tree_cons (id, old_type_value, b->type_shadowed);
3745 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3746 TREE_TYPE (b->type_shadowed) = type;
19831e2b
OW
3747 }
3748 else
3749 {
3c9cca88
NS
3750 tree *slot = find_namespace_slot (current_namespace, id, true);
3751 gcc_assert (decl);
3752 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
44fd0e80 3753
b655c310
NS
3754 /* Store marker instead of real type. */
3755 type = global_type_node;
3756 }
3757 SET_IDENTIFIER_TYPE_VALUE (id, type);
5a167978
GDR
3758}
3759
b655c310
NS
3760/* As set_identifier_type_value_with_scope, but using
3761 current_binding_level. */
5a167978
GDR
3762
3763void
b655c310 3764set_identifier_type_value (tree id, tree decl)
5a167978 3765{
b655c310
NS
3766 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3767}
5a167978 3768
b655c310 3769/* Return the name for the constructor (or destructor) for the
5fee5eca 3770 specified class. */
5a167978 3771
b655c310
NS
3772tree
3773constructor_name (tree type)
3774{
56b2a94b
NS
3775 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3776
3777 return decl ? DECL_NAME (decl) : NULL_TREE;
b655c310 3778}
5a167978 3779
b655c310
NS
3780/* Returns TRUE if NAME is the name for the constructor for TYPE,
3781 which must be a class type. */
5a167978 3782
b655c310
NS
3783bool
3784constructor_name_p (tree name, tree type)
3785{
b655c310
NS
3786 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3787
b655c310
NS
3788 /* These don't have names. */
3789 if (TREE_CODE (type) == DECLTYPE_TYPE
3790 || TREE_CODE (type) == TYPEOF_TYPE)
3791 return false;
3792
56b2a94b 3793 if (name && name == constructor_name (type))
b655c310 3794 return true;
5fee5eca 3795
b655c310 3796 return false;
5a167978
GDR
3797}
3798
b655c310 3799/* Counter used to create anonymous type names. */
5a167978 3800
b655c310
NS
3801static GTY(()) int anon_cnt;
3802
3803/* Return an IDENTIFIER which can be used as a name for
3804 unnamed structs and unions. */
3805
3806tree
3807make_anon_name (void)
5a167978 3808{
b655c310
NS
3809 char buf[32];
3810
3811 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3812 return get_identifier (buf);
3813}
3814
3815/* This code is practically identical to that for creating
3816 anonymous names, but is just used for lambdas instead. This isn't really
3817 necessary, but it's convenient to avoid treating lambdas like other
3818 unnamed types. */
3819
3820static GTY(()) int lambda_cnt = 0;
3821
3822tree
3823make_lambda_name (void)
3824{
3825 char buf[32];
3826
3827 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3828 return get_identifier (buf);
3829}
c8094d83 3830
b655c310
NS
3831/* Insert another USING_DECL into the current binding level, returning
3832 this declaration. If this is a redeclaration, do nothing, and
3833 return NULL_TREE if this not in namespace scope (in namespace
3834 scope, a using decl might extend any previous bindings). */
87c465f5 3835
b655c310
NS
3836static tree
3837push_using_decl_1 (tree scope, tree name)
87c465f5 3838{
b655c310 3839 tree decl;
87c465f5 3840
b655c310
NS
3841 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3842 gcc_assert (identifier_p (name));
3843 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3844 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3845 break;
3846 if (decl)
3847 return namespace_bindings_p () ? decl : NULL_TREE;
3848 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3849 USING_DECL_SCOPE (decl) = scope;
3850 DECL_CHAIN (decl) = current_binding_level->usings;
3851 current_binding_level->usings = decl;
3852 return decl;
87c465f5
KL
3853}
3854
b655c310 3855/* Wrapper for push_using_decl_1. */
87c465f5 3856
b655c310
NS
3857static tree
3858push_using_decl (tree scope, tree name)
87c465f5 3859{
b655c310
NS
3860 tree ret;
3861 timevar_start (TV_NAME_LOOKUP);
3862 ret = push_using_decl_1 (scope, name);
3863 timevar_stop (TV_NAME_LOOKUP);
3864 return ret;
3865}
87c465f5 3866
b655c310
NS
3867/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3868 caller to set DECL_CONTEXT properly.
87c465f5 3869
b655c310
NS
3870 Note that this must only be used when X will be the new innermost
3871 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3872 without checking to see if the current IDENTIFIER_BINDING comes from a
3873 closer binding level than LEVEL. */
87c465f5 3874
b655c310 3875static tree
5256a7f5 3876do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
b655c310
NS
3877{
3878 cp_binding_level *b;
87c465f5 3879
b655c310
NS
3880 if (level->kind == sk_class)
3881 {
3882 b = class_binding_level;
3883 class_binding_level = level;
3884 pushdecl_class_level (x);
3885 class_binding_level = b;
3886 }
3887 else
3888 {
d04e6ed5
NS
3889 tree function_decl = current_function_decl;
3890 if (level->kind == sk_namespace)
3891 current_function_decl = NULL_TREE;
b655c310
NS
3892 b = current_binding_level;
3893 current_binding_level = level;
4f15a5da 3894 x = pushdecl (x, is_friend);
b655c310 3895 current_binding_level = b;
d04e6ed5 3896 current_function_decl = function_decl;
87c465f5 3897 }
b655c310 3898 return x;
87c465f5 3899}
9e931c2a 3900
d16d5eac 3901/* Inject X into the local scope just before the function parms. */
00e8de68 3902
b655c310 3903tree
d16d5eac 3904pushdecl_outermost_localscope (tree x)
00e8de68 3905{
c8634a1a 3906 cp_binding_level *b = NULL;
b655c310 3907 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
d16d5eac 3908
c8634a1a
NS
3909 /* Find the scope just inside the function parms. */
3910 for (cp_binding_level *n = current_binding_level;
3911 n->kind != sk_function_parms; n = b->level_chain)
3912 b = n;
3913
5256a7f5 3914 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
b655c310 3915 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
c8634a1a 3916
b655c310 3917 return ret;
00e8de68
GDR
3918}
3919
b655c310
NS
3920/* Check a non-member using-declaration. Return the name and scope
3921 being used, and the USING_DECL, or NULL_TREE on failure. */
d2f2c87b 3922
b655c310
NS
3923static tree
3924validate_nonmember_using_decl (tree decl, tree scope, tree name)
3925{
3926 /* [namespace.udecl]
3927 A using-declaration for a class member shall be a
3928 member-declaration. */
3929 if (TYPE_P (scope))
90ea9897 3930 {
b655c310
NS
3931 error ("%qT is not a namespace or unscoped enum", scope);
3932 return NULL_TREE;
d2f2c87b 3933 }
b655c310
NS
3934 else if (scope == error_mark_node)
3935 return NULL_TREE;
d2f2c87b 3936
b655c310 3937 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
90ea9897 3938 {
b655c310
NS
3939 /* 7.3.3/5
3940 A using-declaration shall not name a template-id. */
3941 error ("a using-declaration cannot specify a template-id. "
3942 "Try %<using %D%>", name);
3943 return NULL_TREE;
90ea9897
MM
3944 }
3945
b655c310 3946 if (TREE_CODE (decl) == NAMESPACE_DECL)
00e8de68 3947 {
b655c310
NS
3948 error ("namespace %qD not allowed in using-declaration", decl);
3949 return NULL_TREE;
00e8de68
GDR
3950 }
3951
b655c310 3952 if (TREE_CODE (decl) == SCOPE_REF)
90ea9897 3953 {
b655c310
NS
3954 /* It's a nested name with template parameter dependent scope.
3955 This can only be using-declaration for class member. */
3956 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3957 return NULL_TREE;
90ea9897 3958 }
00e8de68 3959
9d029ddf 3960 decl = OVL_FIRST (decl);
575bfb00 3961
b655c310
NS
3962 /* Make a USING_DECL. */
3963 tree using_decl = push_using_decl (scope, name);
575bfb00 3964
b655c310
NS
3965 if (using_decl == NULL_TREE
3966 && at_function_scope_p ()
3967 && VAR_P (decl))
3968 /* C++11 7.3.3/10. */
3969 error ("%qD is already declared in this scope", name);
3970
3971 return using_decl;
00e8de68
GDR
3972}
3973
9d029ddf
NS
3974/* Process a local-scope or namespace-scope using declaration. SCOPE
3975 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3976 point to the binding for NAME in the current scope and are
3977 updated. */
1d786913 3978
b655c310 3979static void
9d029ddf 3980do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
5a167978 3981{
9dda0ace 3982 name_lookup lookup (name, 0);
c8094d83 3983
9dda0ace 3984 if (!qualified_namespace_lookup (scope, &lookup))
5a167978 3985 {
b655c310
NS
3986 error ("%qD not declared", name);
3987 return;
5a167978 3988 }
9d029ddf
NS
3989 else if (TREE_CODE (lookup.value) == TREE_LIST)
3990 {
3991 error ("reference to %qD is ambiguous", name);
3992 print_candidates (lookup.value);
3993 lookup.value = NULL_TREE;
3994 }
3995
3996 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
3997 {
3998 error ("reference to %qD is ambiguous", name);
3999 print_candidates (lookup.type);
4000 lookup.type = NULL_TREE;
4001 }
4002
4003 tree value = *value_p;
4004 tree type = *type_p;
98ed9dae 4005
b655c310
NS
4006 /* Shift the old and new bindings around so we're comparing class and
4007 enumeration names to each other. */
9d029ddf 4008 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
5a167978 4009 {
9d029ddf
NS
4010 type = value;
4011 value = NULL_TREE;
98ed9dae 4012 }
b655c310 4013
9d029ddf 4014 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
140bec21 4015 {
9d029ddf
NS
4016 lookup.type = lookup.value;
4017 lookup.value = NULL_TREE;
140bec21 4018 }
b655c310 4019
9d029ddf 4020 if (lookup.value && lookup.value != value)
98ed9dae 4021 {
b655c310 4022 /* Check for using functions. */
9d029ddf 4023 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
b655c310 4024 {
9d029ddf 4025 for (lkp_iterator usings (lookup.value); usings; ++usings)
b655c310 4026 {
9d029ddf 4027 tree new_fn = *usings;
577b02d8 4028
b655c310 4029 /* [namespace.udecl]
c8094d83 4030
b655c310
NS
4031 If a function declaration in namespace scope or block
4032 scope has the same name and the same parameter types as a
4033 function introduced by a using declaration the program is
4034 ill-formed. */
9d029ddf
NS
4035 bool found = false;
4036 for (ovl_iterator old (value); !found && old; ++old)
b655c310 4037 {
9d029ddf 4038 tree old_fn = *old;
3db45ab5 4039
b655c310 4040 if (new_fn == old_fn)
9d029ddf
NS
4041 /* The function already exists in the current
4042 namespace. */
4043 found = true;
4044 else if (old.using_p ())
4045 continue; /* This is a using decl. */
ef4c5e78 4046 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
9d029ddf
NS
4047 continue; /* This is an anticipated builtin. */
4048 else if (!matching_fn_p (new_fn, old_fn))
4049 continue; /* Parameters do not match. */
4050 else if (decls_match (new_fn, old_fn))
4051 found = true;
4052 else
b655c310 4053 {
9d029ddf
NS
4054 diagnose_name_conflict (new_fn, old_fn);
4055 found = true;
b655c310
NS
4056 }
4057 }
1e9f5818 4058
9d029ddf
NS
4059 if (!found)
4060 /* Unlike the overload case we don't drop anticipated
4061 builtins here. They don't cause a problem, and
4062 we'd like to match them with a future
4063 declaration. */
4064 value = ovl_insert (new_fn, value, true);
b01e6d2b 4065 }
98ed9dae 4066 }
9d029ddf
NS
4067 else if (value
4068 /* Ignore anticipated builtins. */
ef4c5e78 4069 && !anticipated_builtin_p (value)
9d029ddf
NS
4070 && !decls_match (lookup.value, value))
4071 diagnose_name_conflict (lookup.value, value);
b655c310 4072 else
9d029ddf 4073 value = lookup.value;
b655c310
NS
4074 }
4075
9d029ddf 4076 if (lookup.type && lookup.type != type)
b655c310 4077 {
9d029ddf
NS
4078 if (type && !decls_match (lookup.type, type))
4079 diagnose_name_conflict (lookup.type, type);
b655c310 4080 else
9d029ddf 4081 type = lookup.type;
b655c310 4082 }
9d029ddf
NS
4083
4084 /* If bind->value is empty, shift any class or enumeration name back. */
4085 if (!value)
b655c310 4086 {
9d029ddf
NS
4087 value = type;
4088 type = NULL_TREE;
1e9f5818 4089 }
98ed9dae 4090
9d029ddf
NS
4091 *value_p = value;
4092 *type_p = type;
5a167978
GDR
4093}
4094
322763f5
NS
4095/* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4096 Both are namespaces. */
4097
4098bool
4099is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4100{
4101 int depth = SCOPE_DEPTH (ancestor);
4102
4103 if (!depth && !inline_only)
4104 /* The global namespace encloses everything. */
4105 return true;
4106
4107 while (SCOPE_DEPTH (descendant) > depth
4108 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4109 descendant = CP_DECL_CONTEXT (descendant);
4110
4111 return ancestor == descendant;
4112}
4113
b655c310
NS
4114/* Returns true if ROOT (a namespace, class, or function) encloses
4115 CHILD. CHILD may be either a class type or a namespace. */
575bfb00 4116
b655c310
NS
4117bool
4118is_ancestor (tree root, tree child)
00e8de68 4119{
b655c310
NS
4120 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4121 || TREE_CODE (root) == FUNCTION_DECL
4122 || CLASS_TYPE_P (root)));
4123 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4124 || CLASS_TYPE_P (child)));
00e8de68 4125
b655c310
NS
4126 /* The global namespace encloses everything. */
4127 if (root == global_namespace)
4128 return true;
00e8de68 4129
322763f5
NS
4130 /* Search until we reach namespace scope. */
4131 while (TREE_CODE (child) != NAMESPACE_DECL)
b655c310 4132 {
b655c310
NS
4133 /* If we've reached the ROOT, it encloses CHILD. */
4134 if (root == child)
4135 return true;
4136 /* Go out one level. */
4137 if (TYPE_P (child))
4138 child = TYPE_NAME (child);
322763f5 4139 child = CP_DECL_CONTEXT (child);
b655c310 4140 }
322763f5
NS
4141
4142 if (TREE_CODE (root) == NAMESPACE_DECL)
4143 return is_nested_namespace (root, child);
4144
4145 return false;
575bfb00
LC
4146}
4147
b655c310
NS
4148/* Enter the class or namespace scope indicated by T suitable for name
4149 lookup. T can be arbitrary scope, not necessary nested inside the
4150 current scope. Returns a non-null scope to pop iff pop_scope
4151 should be called later to exit this scope. */
ed3cf953 4152
b655c310
NS
4153tree
4154push_scope (tree t)
ed3cf953 4155{
b655c310
NS
4156 if (TREE_CODE (t) == NAMESPACE_DECL)
4157 push_decl_namespace (t);
4158 else if (CLASS_TYPE_P (t))
4159 {
4160 if (!at_class_scope_p ()
4161 || !same_type_p (current_class_type, t))
4162 push_nested_class (t);
4163 else
4164 /* T is the same as the current scope. There is therefore no
4165 need to re-enter the scope. Since we are not actually
4166 pushing a new scope, our caller should not call
4167 pop_scope. */
4168 t = NULL_TREE;
4169 }
ed3cf953 4170
b655c310 4171 return t;
575bfb00
LC
4172}
4173
b655c310 4174/* Leave scope pushed by push_scope. */
575bfb00
LC
4175
4176void
b655c310 4177pop_scope (tree t)
575bfb00 4178{
b655c310
NS
4179 if (t == NULL_TREE)
4180 return;
4181 if (TREE_CODE (t) == NAMESPACE_DECL)
4182 pop_decl_namespace ();
4183 else if CLASS_TYPE_P (t)
4184 pop_nested_class ();
ed3cf953
GDR
4185}
4186
b655c310 4187/* Subroutine of push_inner_scope. */
5a167978 4188
b655c310
NS
4189static void
4190push_inner_scope_r (tree outer, tree inner)
5a167978 4191{
b655c310 4192 tree prev;
5ae9ba3e 4193
b655c310
NS
4194 if (outer == inner
4195 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
abc088aa 4196 return;
b655c310
NS
4197
4198 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4199 if (outer != prev)
4200 push_inner_scope_r (outer, prev);
4201 if (TREE_CODE (inner) == NAMESPACE_DECL)
5ae9ba3e 4202 {
b655c310
NS
4203 cp_binding_level *save_template_parm = 0;
4204 /* Temporary take out template parameter scopes. They are saved
4205 in reversed order in save_template_parm. */
4206 while (current_binding_level->kind == sk_template_parms)
160594b0 4207 {
b655c310
NS
4208 cp_binding_level *b = current_binding_level;
4209 current_binding_level = b->level_chain;
4210 b->level_chain = save_template_parm;
4211 save_template_parm = b;
160594b0 4212 }
b655c310
NS
4213
4214 resume_scope (NAMESPACE_LEVEL (inner));
4215 current_namespace = inner;
4216
4217 /* Restore template parameter scopes. */
4218 while (save_template_parm)
160594b0 4219 {
b655c310
NS
4220 cp_binding_level *b = save_template_parm;
4221 save_template_parm = b->level_chain;
4222 b->level_chain = current_binding_level;
4223 current_binding_level = b;
160594b0 4224 }
5ae9ba3e 4225 }
b655c310
NS
4226 else
4227 pushclass (inner);
c8094d83 4228}
5a167978 4229
b655c310
NS
4230/* Enter the scope INNER from current scope. INNER must be a scope
4231 nested inside current scope. This works with both name lookup and
4232 pushing name into scope. In case a template parameter scope is present,
4233 namespace is pushed under the template parameter scope according to
4234 name lookup rule in 14.6.1/6.
4235
4236 Return the former current scope suitable for pop_inner_scope. */
5a167978 4237
ae099258 4238tree
b655c310 4239push_inner_scope (tree inner)
5a167978 4240{
b655c310
NS
4241 tree outer = current_scope ();
4242 if (!outer)
4243 outer = current_namespace;
5a167978 4244
b655c310
NS
4245 push_inner_scope_r (outer, inner);
4246 return outer;
5a167978
GDR
4247}
4248
b655c310 4249/* Exit the current scope INNER back to scope OUTER. */
00e8de68 4250
b655c310
NS
4251void
4252pop_inner_scope (tree outer, tree inner)
0ed5edac 4253{
b655c310
NS
4254 if (outer == inner
4255 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4256 return;
0ed5edac 4257
b655c310 4258 while (outer != inner)
65567efa 4259 {
b655c310 4260 if (TREE_CODE (inner) == NAMESPACE_DECL)
65567efa 4261 {
b655c310
NS
4262 cp_binding_level *save_template_parm = 0;
4263 /* Temporary take out template parameter scopes. They are saved
4264 in reversed order in save_template_parm. */
4265 while (current_binding_level->kind == sk_template_parms)
65567efa 4266 {
b655c310
NS
4267 cp_binding_level *b = current_binding_level;
4268 current_binding_level = b->level_chain;
4269 b->level_chain = save_template_parm;
4270 save_template_parm = b;
65567efa
JM
4271 }
4272
b655c310 4273 pop_namespace ();
65567efa 4274
b655c310
NS
4275 /* Restore template parameter scopes. */
4276 while (save_template_parm)
7cb73573 4277 {
b655c310
NS
4278 cp_binding_level *b = save_template_parm;
4279 save_template_parm = b->level_chain;
4280 b->level_chain = current_binding_level;
4281 current_binding_level = b;
7cb73573 4282 }
e3501bab 4283 }
65567efa 4284 else
b655c310
NS
4285 popclass ();
4286
4287 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
65567efa 4288 }
b655c310
NS
4289}
4290\f
4291/* Do a pushlevel for class declarations. */
65567efa 4292
b655c310
NS
4293void
4294pushlevel_class (void)
4295{
4296 class_binding_level = begin_scope (sk_class, current_class_type);
65567efa 4297}
0ed5edac 4298
b655c310
NS
4299/* ...and a poplevel for class declarations. */
4300
4301void
4302poplevel_class (void)
00e8de68 4303{
b655c310
NS
4304 cp_binding_level *level = class_binding_level;
4305 cp_class_binding *cb;
4306 size_t i;
4307 tree shadowed;
00e8de68 4308
575bfb00 4309 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
b655c310 4310 gcc_assert (level != 0);
c8094d83 4311
b655c310
NS
4312 /* If we're leaving a toplevel class, cache its binding level. */
4313 if (current_class_depth == 1)
4314 previous_class_level = level;
4315 for (shadowed = level->type_shadowed;
4316 shadowed;
4317 shadowed = TREE_CHAIN (shadowed))
4318 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
00e8de68 4319
b655c310
NS
4320 /* Remove the bindings for all of the class-level declarations. */
4321 if (level->class_shadowed)
00e8de68 4322 {
b655c310 4323 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
0cbd7506 4324 {
b655c310
NS
4325 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4326 cxx_binding_free (cb->base);
0cbd7506 4327 }
b655c310
NS
4328 ggc_free (level->class_shadowed);
4329 level->class_shadowed = NULL;
00e8de68
GDR
4330 }
4331
b655c310
NS
4332 /* Now, pop out of the binding level which we created up in the
4333 `pushlevel_class' routine. */
4334 gcc_assert (current_binding_level == level);
4335 leave_scope ();
4336 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4337}
4338
4339/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4340 appropriate. DECL is the value to which a name has just been
4341 bound. CLASS_TYPE is the class in which the lookup occurred. */
4342
4343static void
4344set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4345 tree class_type)
4346{
4347 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
00e8de68 4348 {
b655c310
NS
4349 tree context;
4350
4351 if (TREE_CODE (decl) == OVERLOAD)
4352 context = ovl_scope (decl);
b9e75696 4353 else
ed36980c 4354 {
b655c310
NS
4355 gcc_assert (DECL_P (decl));
4356 context = context_for_name_lookup (decl);
ed36980c 4357 }
00e8de68 4358
b655c310
NS
4359 if (is_properly_derived_from (class_type, context))
4360 INHERITED_VALUE_BINDING_P (binding) = 1;
4361 else
4362 INHERITED_VALUE_BINDING_P (binding) = 0;
4363 }
4364 else if (binding->value == decl)
4365 /* We only encounter a TREE_LIST when there is an ambiguity in the
4366 base classes. Such an ambiguity can be overridden by a
4367 definition in this class. */
4368 INHERITED_VALUE_BINDING_P (binding) = 1;
4369 else
4370 INHERITED_VALUE_BINDING_P (binding) = 0;
00e8de68
GDR
4371}
4372
b655c310 4373/* Make the declaration of X appear in CLASS scope. */
00e8de68 4374
b655c310
NS
4375bool
4376pushdecl_class_level (tree x)
00e8de68 4377{
b655c310
NS
4378 bool is_valid = true;
4379 bool subtime;
00e8de68 4380
b655c310
NS
4381 /* Do nothing if we're adding to an outer lambda closure type,
4382 outer_binding will add it later if it's needed. */
4383 if (current_class_type != class_binding_level->this_entity)
4384 return true;
00e8de68 4385
b655c310
NS
4386 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4387 /* Get the name of X. */
848bf88d 4388 tree name = OVL_NAME (x);
00e8de68 4389
b655c310 4390 if (name)
00e8de68 4391 {
b655c310
NS
4392 is_valid = push_class_level_binding (name, x);
4393 if (TREE_CODE (x) == TYPE_DECL)
4394 set_identifier_type_value (name, x);
00e8de68 4395 }
b655c310
NS
4396 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4397 {
4398 /* If X is an anonymous aggregate, all of its members are
4399 treated as if they were members of the class containing the
4400 aggregate, for naming purposes. */
6f87580f
NS
4401 location_t save_location = input_location;
4402 tree anon = TREE_TYPE (x);
4403 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4404 for (unsigned ix = member_vec->length (); ix--;)
4405 {
4406 tree binding = (*member_vec)[ix];
4407 if (STAT_HACK_P (binding))
4408 {
4409 if (!pushdecl_class_level (STAT_TYPE (binding)))
4410 is_valid = false;
4411 binding = STAT_DECL (binding);
4412 }
4413 if (!pushdecl_class_level (binding))
4414 is_valid = false;
b655c310 4415 }
6f87580f
NS
4416 else
4417 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4418 if (TREE_CODE (f) == FIELD_DECL)
4419 {
4420 input_location = DECL_SOURCE_LOCATION (f);
4421 if (!pushdecl_class_level (f))
4422 is_valid = false;
4423 }
4424 input_location = save_location;
b655c310 4425 }
575bfb00 4426 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310 4427 return is_valid;
00e8de68
GDR
4428}
4429
b655c310
NS
4430/* Return the BINDING (if any) for NAME in SCOPE, which is a class
4431 scope. If the value returned is non-NULL, and the PREVIOUS field
4432 is not set, callers must set the PREVIOUS field explicitly. */
5a167978 4433
b655c310
NS
4434static cxx_binding *
4435get_class_binding (tree name, cp_binding_level *scope)
5a167978 4436{
b655c310
NS
4437 tree class_type;
4438 tree type_binding;
4439 tree value_binding;
4440 cxx_binding *binding;
5a167978 4441
b655c310 4442 class_type = scope->this_entity;
5a167978 4443
b655c310
NS
4444 /* Get the type binding. */
4445 type_binding = lookup_member (class_type, name,
4446 /*protect=*/2, /*want_type=*/true,
4447 tf_warning_or_error);
4448 /* Get the value binding. */
4449 value_binding = lookup_member (class_type, name,
4450 /*protect=*/2, /*want_type=*/false,
4451 tf_warning_or_error);
5a167978 4452
b655c310
NS
4453 if (value_binding
4454 && (TREE_CODE (value_binding) == TYPE_DECL
4455 || DECL_CLASS_TEMPLATE_P (value_binding)
4456 || (TREE_CODE (value_binding) == TREE_LIST
4457 && TREE_TYPE (value_binding) == error_mark_node
4458 && (TREE_CODE (TREE_VALUE (value_binding))
4459 == TYPE_DECL))))
4460 /* We found a type binding, even when looking for a non-type
4461 binding. This means that we already processed this binding
4462 above. */
4463 ;
4464 else if (value_binding)
4465 {
4466 if (TREE_CODE (value_binding) == TREE_LIST
4467 && TREE_TYPE (value_binding) == error_mark_node)
4468 /* NAME is ambiguous. */
4469 ;
4470 else if (BASELINK_P (value_binding))
4471 /* NAME is some overloaded functions. */
4472 value_binding = BASELINK_FUNCTIONS (value_binding);
4473 }
5a167978 4474
b655c310
NS
4475 /* If we found either a type binding or a value binding, create a
4476 new binding object. */
4477 if (type_binding || value_binding)
4478 {
4479 binding = new_class_binding (name,
4480 value_binding,
4481 type_binding,
4482 scope);
4483 /* This is a class-scope binding, not a block-scope binding. */
4484 LOCAL_BINDING_P (binding) = 0;
4485 set_inherited_value_binding_p (binding, value_binding, class_type);
4486 }
575bfb00 4487 else
b655c310
NS
4488 binding = NULL;
4489
4490 return binding;
575bfb00
LC
4491}
4492
b655c310
NS
4493/* Make the declaration(s) of X appear in CLASS scope under the name
4494 NAME. Returns true if the binding is valid. */
575bfb00 4495
b655c310
NS
4496static bool
4497push_class_level_binding_1 (tree name, tree x)
575bfb00 4498{
b655c310
NS
4499 cxx_binding *binding;
4500 tree decl = x;
4501 bool ok;
5a167978 4502
b655c310
NS
4503 /* The class_binding_level will be NULL if x is a template
4504 parameter name in a member template. */
4505 if (!class_binding_level)
4506 return true;
5a167978 4507
b655c310
NS
4508 if (name == error_mark_node)
4509 return false;
166206ce 4510
b655c310
NS
4511 /* Can happen for an erroneous declaration (c++/60384). */
4512 if (!identifier_p (name))
4513 {
4514 gcc_assert (errorcount || sorrycount);
4515 return false;
4516 }
5a167978 4517
b655c310
NS
4518 /* Check for invalid member names. But don't worry about a default
4519 argument-scope lambda being pushed after the class is complete. */
4520 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4521 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4522 /* Check that we're pushing into the right binding level. */
4523 gcc_assert (current_class_type == class_binding_level->this_entity);
5a167978 4524
b655c310
NS
4525 /* We could have been passed a tree list if this is an ambiguous
4526 declaration. If so, pull the declaration out because
4527 check_template_shadow will not handle a TREE_LIST. */
4528 if (TREE_CODE (decl) == TREE_LIST
4529 && TREE_TYPE (decl) == error_mark_node)
4530 decl = TREE_VALUE (decl);
6097b0c3 4531
b655c310
NS
4532 if (!check_template_shadow (decl))
4533 return false;
5a167978 4534
b655c310 4535 /* [class.mem]
00e8de68 4536
b655c310
NS
4537 If T is the name of a class, then each of the following shall
4538 have a name different from T:
00e8de68 4539
b655c310 4540 -- every static data member of class T;
00e8de68 4541
b655c310
NS
4542 -- every member of class T that is itself a type;
4543
4544 -- every enumerator of every member of class T that is an
4545 enumerated type;
4546
4547 -- every member of every anonymous union that is a member of
4548 class T.
4549
4550 (Non-static data members were also forbidden to have the same
4551 name as T until TC1.) */
4552 if ((VAR_P (x)
4553 || TREE_CODE (x) == CONST_DECL
4554 || (TREE_CODE (x) == TYPE_DECL
4555 && !DECL_SELF_REFERENCE_P (x))
4556 /* A data member of an anonymous union. */
4557 || (TREE_CODE (x) == FIELD_DECL
4558 && DECL_CONTEXT (x) != current_class_type))
56b2a94b 4559 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
b655c310
NS
4560 {
4561 tree scope = context_for_name_lookup (x);
4562 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4563 {
4564 error ("%qD has the same name as the class in which it is "
4565 "declared",
4566 x);
4567 return false;
4568 }
4569 }
4570
4571 /* Get the current binding for NAME in this class, if any. */
4572 binding = IDENTIFIER_BINDING (name);
4573 if (!binding || binding->scope != class_binding_level)
00e8de68 4574 {
b655c310
NS
4575 binding = get_class_binding (name, class_binding_level);
4576 /* If a new binding was created, put it at the front of the
4577 IDENTIFIER_BINDING list. */
4578 if (binding)
0cbd7506 4579 {
b655c310
NS
4580 binding->previous = IDENTIFIER_BINDING (name);
4581 IDENTIFIER_BINDING (name) = binding;
0cbd7506 4582 }
b655c310
NS
4583 }
4584
4585 /* If there is already a binding, then we may need to update the
4586 current value. */
4587 if (binding && binding->value)
4588 {
4589 tree bval = binding->value;
4590 tree old_decl = NULL_TREE;
4591 tree target_decl = strip_using_decl (decl);
4592 tree target_bval = strip_using_decl (bval);
4593
4594 if (INHERITED_VALUE_BINDING_P (binding))
0cbd7506 4595 {
b655c310
NS
4596 /* If the old binding was from a base class, and was for a
4597 tag name, slide it over to make room for the new binding.
4598 The old binding is still visible if explicitly qualified
4599 with a class-key. */
4600 if (TREE_CODE (target_bval) == TYPE_DECL
4601 && DECL_ARTIFICIAL (target_bval)
4602 && !(TREE_CODE (target_decl) == TYPE_DECL
4603 && DECL_ARTIFICIAL (target_decl)))
4604 {
4605 old_decl = binding->type;
4606 binding->type = bval;
4607 binding->value = NULL_TREE;
4608 INHERITED_VALUE_BINDING_P (binding) = 0;
4609 }
4610 else
4611 {
4612 old_decl = bval;
4613 /* Any inherited type declaration is hidden by the type
4614 declaration in the derived class. */
4615 if (TREE_CODE (target_decl) == TYPE_DECL
4616 && DECL_ARTIFICIAL (target_decl))
4617 binding->type = NULL_TREE;
4618 }
00e8de68 4619 }
b655c310 4620 else if (TREE_CODE (target_decl) == OVERLOAD
5256a7f5 4621 && OVL_P (target_bval))
b655c310
NS
4622 old_decl = bval;
4623 else if (TREE_CODE (decl) == USING_DECL
4624 && TREE_CODE (bval) == USING_DECL
4625 && same_type_p (USING_DECL_SCOPE (decl),
4626 USING_DECL_SCOPE (bval)))
4627 /* This is a using redeclaration that will be diagnosed later
4628 in supplement_binding */
4629 ;
4630 else if (TREE_CODE (decl) == USING_DECL
4631 && TREE_CODE (bval) == USING_DECL
4632 && DECL_DEPENDENT_P (decl)
4633 && DECL_DEPENDENT_P (bval))
4634 return true;
4635 else if (TREE_CODE (decl) == USING_DECL
5256a7f5 4636 && OVL_P (target_bval))
b655c310
NS
4637 old_decl = bval;
4638 else if (TREE_CODE (bval) == USING_DECL
5256a7f5 4639 && OVL_P (target_decl))
b655c310
NS
4640 return true;
4641
4642 if (old_decl && binding->scope == class_binding_level)
0cbd7506 4643 {
b655c310
NS
4644 binding->value = x;
4645 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4646 here. This function is only used to register bindings
4647 from with the class definition itself. */
4648 INHERITED_VALUE_BINDING_P (binding) = 0;
4649 return true;
0cbd7506 4650 }
00e8de68 4651 }
00e8de68 4652
b655c310
NS
4653 /* Note that we declared this value so that we can issue an error if
4654 this is an invalid redeclaration of a name already used for some
4655 other purpose. */
4656 note_name_declared_in_class (name, decl);
575bfb00 4657
b655c310
NS
4658 /* If we didn't replace an existing binding, put the binding on the
4659 stack of bindings for the identifier, and update the shadowed
4660 list. */
4661 if (binding && binding->scope == class_binding_level)
4662 /* Supplement the existing binding. */
4663 ok = supplement_binding (binding, decl);
4664 else
5a167978 4665 {
b655c310
NS
4666 /* Create a new binding. */
4667 push_binding (name, decl, class_binding_level);
4668 ok = true;
5a167978
GDR
4669 }
4670
b655c310 4671 return ok;
575bfb00
LC
4672}
4673
b655c310 4674/* Wrapper for push_class_level_binding_1. */
575bfb00 4675
b655c310
NS
4676bool
4677push_class_level_binding (tree name, tree x)
575bfb00 4678{
b655c310 4679 bool ret;
575bfb00 4680 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
b655c310 4681 ret = push_class_level_binding_1 (name, x);
575bfb00 4682 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310 4683 return ret;
5a167978
GDR
4684}
4685
b655c310
NS
4686/* Process "using SCOPE::NAME" in a class scope. Return the
4687 USING_DECL created. */
5a167978 4688
b655c310
NS
4689tree
4690do_class_using_decl (tree scope, tree name)
5a167978 4691{
b655c310
NS
4692 if (name == error_mark_node)
4693 return NULL_TREE;
166206ce 4694
b655c310
NS
4695 if (!scope || !TYPE_P (scope))
4696 {
4697 error ("using-declaration for non-member at class scope");
4698 return NULL_TREE;
4699 }
166206ce 4700
b655c310
NS
4701 /* Make sure the name is not invalid */
4702 if (TREE_CODE (name) == BIT_NOT_EXPR)
6097b0c3 4703 {
b655c310
NS
4704 error ("%<%T::%D%> names destructor", scope, name);
4705 return NULL_TREE;
6097b0c3 4706 }
0c29f2a2 4707
b655c310
NS
4708 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4709 if (MAYBE_CLASS_TYPE_P (scope)
4710 && (name == TYPE_IDENTIFIER (scope)
4711 || constructor_name_p (name, scope)))
6097b0c3 4712 {
b655c310
NS
4713 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4714 name = ctor_identifier;
4715 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
d19c0f4b 4716 }
0c29f2a2
NS
4717
4718 /* Cannot introduce a constructor name. */
b655c310
NS
4719 if (constructor_name_p (name, current_class_type))
4720 {
4721 error ("%<%T::%D%> names constructor in %qT",
4722 scope, name, current_class_type);
4723 return NULL_TREE;
4724 }
4725
b655c310 4726 /* From [namespace.udecl]:
1b255e8f 4727
b655c310
NS
4728 A using-declaration used as a member-declaration shall refer to a
4729 member of a base class of the class being defined.
4730
4731 In general, we cannot check this constraint in a template because
4732 we do not know the entire set of base classes of the current
4733 class type. Morover, if SCOPE is dependent, it might match a
4734 non-dependent base. */
4735
0c29f2a2
NS
4736 tree decl = NULL_TREE;
4737 if (!dependent_scope_p (scope))
86098eb8 4738 {
b655c310 4739 base_kind b_kind;
0c29f2a2
NS
4740 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4741 tf_warning_or_error);
b655c310 4742 if (b_kind < bk_proper_base)
86098eb8 4743 {
0c29f2a2
NS
4744 /* If there are dependent bases, scope might resolve at
4745 instantiation time, even if it isn't exactly one of the
4746 dependent bases. */
4747 if (b_kind == bk_same_type || !any_dependent_bases_p ())
9deb204a 4748 {
b655c310
NS
4749 error_not_base_type (scope, current_class_type);
4750 return NULL_TREE;
9deb204a 4751 }
86098eb8 4752 }
84eb0f1a 4753 else if (name == ctor_identifier && !binfo_direct_p (binfo))
b655c310
NS
4754 {
4755 error ("cannot inherit constructors from indirect base %qT", scope);
4756 return NULL_TREE;
4757 }
0c29f2a2
NS
4758 else if (!IDENTIFIER_CONV_OP_P (name)
4759 || !dependent_type_p (TREE_TYPE (name)))
b655c310
NS
4760 {
4761 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4762 if (!decl)
4763 {
4764 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4765 scope);
4766 return NULL_TREE;
4767 }
0c29f2a2 4768
b655c310
NS
4769 /* The binfo from which the functions came does not matter. */
4770 if (BASELINK_P (decl))
4771 decl = BASELINK_FUNCTIONS (decl);
4772 }
86098eb8 4773 }
86098eb8 4774
0c29f2a2 4775 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
b655c310
NS
4776 USING_DECL_DECLS (value) = decl;
4777 USING_DECL_SCOPE (value) = scope;
4778 DECL_DEPENDENT_P (value) = !decl;
a5e6b29b 4779
b655c310 4780 return value;
a5e6b29b
GDR
4781}
4782
b655c310 4783\f
4b4b2e58
NS
4784/* Return the binding for NAME in NS. If NS is NULL, look in
4785 global_namespace. */
4786
a5e6b29b 4787tree
06aa5490 4788get_namespace_binding (tree ns, tree name)
a5e6b29b 4789{
b655c310 4790 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4b4b2e58
NS
4791 if (!ns)
4792 ns = global_namespace;
3c9cca88
NS
4793 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4794 tree ret = find_namespace_value (ns, name);
b655c310 4795 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3c9cca88 4796 return ret;
5a167978
GDR
4797}
4798
87e3d7cf
NS
4799/* Push internal DECL into the global namespace. Does not do the
4800 full overload fn handling and does not add it to the list of things
4801 in the namespace. */
1e003829 4802
b655c310 4803void
87e3d7cf 4804set_global_binding (tree decl)
1e003829 4805{
b655c310 4806 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4b4b2e58 4807
87e3d7cf 4808 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
3c9cca88 4809
a6a5091a
NS
4810 if (*slot)
4811 /* The user's placed something in the implementor's namespace. */
4812 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4813
4814 /* Force the binding, so compiler internals continue to work. */
4815 *slot = decl;
4b4b2e58 4816
b655c310 4817 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1e003829
JM
4818}
4819
b655c310
NS
4820/* Set the context of a declaration to scope. Complain if we are not
4821 outside scope. */
5a167978 4822
b655c310
NS
4823void
4824set_decl_namespace (tree decl, tree scope, bool friendp)
5a167978 4825{
b655c310
NS
4826 /* Get rid of namespace aliases. */
4827 scope = ORIGINAL_NAMESPACE (scope);
af92ab36 4828
b655c310 4829 /* It is ok for friends to be qualified in parallel space. */
322763f5 4830 if (!friendp && !is_nested_namespace (current_namespace, scope))
b655c310
NS
4831 error ("declaration of %qD not in a namespace surrounding %qD",
4832 decl, scope);
4833 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
af92ab36 4834
5ec046c0
NS
4835 /* See whether this has been declared in the namespace or inline
4836 children. */
4837 tree old = NULL_TREE;
4838 {
4839 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4840 if (!lookup.search_qualified (scope, /*usings=*/false))
4841 /* No old declaration at all. */
4842 goto not_found;
4843 old = lookup.value;
4844 }
c8094d83 4845
b655c310
NS
4846 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4847 if (TREE_CODE (old) == TREE_LIST)
5a167978 4848 {
5ec046c0
NS
4849 ambiguous:
4850 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
b655c310
NS
4851 error ("reference to %qD is ambiguous", decl);
4852 print_candidates (old);
4853 return;
4854 }
5ec046c0
NS
4855
4856 if (!DECL_DECLARES_FUNCTION_P (decl))
b655c310 4857 {
b655c310
NS
4858 /* Don't compare non-function decls with decls_match here, since
4859 it can't check for the correct constness at this
5ec046c0
NS
4860 point. pushdecl will find those errors later. */
4861
4862 /* We might have found it in an inline namespace child of SCOPE. */
4863 if (TREE_CODE (decl) == TREE_CODE (old))
4864 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4865
4866 found:
4867 /* Writing "N::i" to declare something directly in "N" is invalid. */
4868 if (CP_DECL_CONTEXT (decl) == current_namespace
4869 && at_namespace_scope_p ())
4870 error ("explicit qualification in declaration of %qD", decl);
b655c310
NS
4871 return;
4872 }
5ec046c0 4873
b655c310 4874 /* Since decl is a function, old should contain a function decl. */
e1cad930 4875 if (!OVL_P (old))
5ec046c0
NS
4876 goto not_found;
4877
b655c310
NS
4878 /* We handle these in check_explicit_instantiation_namespace. */
4879 if (processing_explicit_instantiation)
4880 return;
4881 if (processing_template_decl || processing_specialization)
4882 /* We have not yet called push_template_decl to turn a
4883 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4884 match. But, we'll check later, when we construct the
4885 template. */
4886 return;
4887 /* Instantiations or specializations of templates may be declared as
4888 friends in any namespace. */
4889 if (friendp && DECL_USE_TEMPLATE (decl))
4890 return;
5ec046c0
NS
4891
4892 tree found;
4893 found = NULL_TREE;
4894
4895 for (lkp_iterator iter (old); iter; ++iter)
b655c310 4896 {
5ec046c0
NS
4897 if (iter.using_p ())
4898 continue;
87c976ae 4899
5ec046c0
NS
4900 tree ofn = *iter;
4901
4902 /* Adjust DECL_CONTEXT first so decls_match will return true
4903 if DECL will match a declaration in an inline namespace. */
4904 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4905 if (decls_match (decl, ofn))
b655c310 4906 {
5ec046c0 4907 if (found)
b655c310 4908 {
5ec046c0
NS
4909 /* We found more than one matching declaration. */
4910 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4911 goto ambiguous;
b655c310 4912 }
5ec046c0 4913 found = ofn;
5a167978
GDR
4914 }
4915 }
5ec046c0
NS
4916
4917 if (found)
5a167978 4918 {
5ec046c0
NS
4919 if (DECL_HIDDEN_FRIEND_P (found))
4920 {
4921 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4922 "%qD has not been declared within %qD", decl, scope);
4923 inform (DECL_SOURCE_LOCATION (found),
4924 "only here as a %<friend%>");
4925 }
4926 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4927 goto found;
5a167978 4928 }
b655c310 4929
5ec046c0 4930 not_found:
b655c310
NS
4931 /* It didn't work, go back to the explicit scope. */
4932 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
b655c310 4933 error ("%qD should have been declared inside %qD", decl, scope);
5a167978
GDR
4934}
4935
b655c310 4936/* Return the namespace where the current declaration is declared. */
00e8de68
GDR
4937
4938tree
b655c310 4939current_decl_namespace (void)
00e8de68 4940{
b655c310
NS
4941 tree result;
4942 /* If we have been pushed into a different namespace, use it. */
4943 if (!vec_safe_is_empty (decl_namespace_list))
4944 return decl_namespace_list->last ();
00e8de68 4945
b655c310
NS
4946 if (current_class_type)
4947 result = decl_namespace_context (current_class_type);
4948 else if (current_function_decl)
4949 result = decl_namespace_context (current_function_decl);
4950 else
4951 result = current_namespace;
4952 return result;
00e8de68
GDR
4953}
4954
b655c310
NS
4955/* Process any ATTRIBUTES on a namespace definition. Returns true if
4956 attribute visibility is seen. */
00e8de68 4957
b655c310
NS
4958bool
4959handle_namespace_attrs (tree ns, tree attributes)
00e8de68 4960{
b655c310
NS
4961 tree d;
4962 bool saw_vis = false;
4963
93e544c1
JJ
4964 if (attributes == error_mark_node)
4965 return false;
4966
b655c310 4967 for (d = attributes; d; d = TREE_CHAIN (d))
af79925b 4968 {
b655c310
NS
4969 tree name = get_attribute_name (d);
4970 tree args = TREE_VALUE (d);
4971
4972 if (is_attribute_p ("visibility", name))
4973 {
4974 /* attribute visibility is a property of the syntactic block
4975 rather than the namespace as a whole, so we don't touch the
4976 NAMESPACE_DECL at all. */
4977 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4978 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4979 {
4980 warning (OPT_Wattributes,
4981 "%qD attribute requires a single NTBS argument",
4982 name);
4983 continue;
4984 }
4985
4986 if (!TREE_PUBLIC (ns))
4987 warning (OPT_Wattributes,
4988 "%qD attribute is meaningless since members of the "
4989 "anonymous namespace get local symbols", name);
4990
4991 push_visibility (TREE_STRING_POINTER (x), 1);
4992 saw_vis = true;
4993 }
4994 else if (is_attribute_p ("abi_tag", name))
4995 {
44e00a7a 4996 if (!DECL_NAME (ns))
b655c310 4997 {
44e00a7a 4998 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
b655c310
NS
4999 "namespace", name);
5000 continue;
5001 }
44e00a7a 5002 if (!DECL_NAMESPACE_INLINE_P (ns))
b655c310 5003 {
44e00a7a 5004 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
b655c310
NS
5005 "namespace", name);
5006 continue;
5007 }
5008 if (!args)
5009 {
5010 tree dn = DECL_NAME (ns);
5011 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5012 IDENTIFIER_POINTER (dn));
5013 TREE_TYPE (args) = char_array_type_node;
5014 args = fix_string_type (args);
5015 args = build_tree_list (NULL_TREE, args);
5016 }
5017 if (check_abi_tag_args (args, name))
5018 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5019 DECL_ATTRIBUTES (ns));
5020 }
5021 else
5022 {
5023 warning (OPT_Wattributes, "%qD attribute directive ignored",
5024 name);
5025 continue;
5026 }
af79925b 5027 }
bd3d082e 5028
b655c310
NS
5029 return saw_vis;
5030}
e1cad930 5031
b655c310 5032/* Temporarily set the namespace for the current declaration. */
bd3d082e 5033
b655c310
NS
5034void
5035push_decl_namespace (tree decl)
bd3d082e 5036{
b655c310
NS
5037 if (TREE_CODE (decl) != NAMESPACE_DECL)
5038 decl = decl_namespace_context (decl);
5039 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
00e8de68
GDR
5040}
5041
b655c310 5042/* [namespace.memdef]/2 */
d63d5d0c 5043
b655c310
NS
5044void
5045pop_decl_namespace (void)
d63d5d0c 5046{
b655c310
NS
5047 decl_namespace_list->pop ();
5048}
d63d5d0c 5049
b655c310 5050/* Process a namespace-alias declaration. */
501c95ff
NF
5051
5052void
b655c310 5053do_namespace_alias (tree alias, tree name_space)
501c95ff 5054{
b655c310
NS
5055 if (name_space == error_mark_node)
5056 return;
501c95ff 5057
b655c310 5058 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
501c95ff 5059
b655c310 5060 name_space = ORIGINAL_NAMESPACE (name_space);
501c95ff 5061
b655c310
NS
5062 /* Build the alias. */
5063 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5064 DECL_NAMESPACE_ALIAS (alias) = name_space;
5065 DECL_EXTERNAL (alias) = 1;
5066 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5067 pushdecl (alias);
501c95ff 5068
b655c310
NS
5069 /* Emit debug info for namespace alias. */
5070 if (!building_stmt_list_p ())
5071 (*debug_hooks->early_global_decl) (alias);
5072}
501c95ff 5073
b655c310
NS
5074/* Like pushdecl, only it places X in the current namespace,
5075 if appropriate. */
501c95ff 5076
b655c310
NS
5077tree
5078pushdecl_namespace_level (tree x, bool is_friend)
5079{
5080 cp_binding_level *b = current_binding_level;
5081 tree t;
501c95ff 5082
b655c310 5083 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5256a7f5 5084 t = do_pushdecl_with_scope
d16d5eac 5085 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
501c95ff 5086
b655c310
NS
5087 /* Now, the type_shadowed stack may screw us. Munge it so it does
5088 what we want. */
5089 if (TREE_CODE (t) == TYPE_DECL)
52ed68f7 5090 {
b655c310
NS
5091 tree name = DECL_NAME (t);
5092 tree newval;
5093 tree *ptr = (tree *)0;
5094 for (; !global_scope_p (b); b = b->level_chain)
52ed68f7 5095 {
b655c310
NS
5096 tree shadowed = b->type_shadowed;
5097 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5098 if (TREE_PURPOSE (shadowed) == name)
5099 {
5100 ptr = &TREE_VALUE (shadowed);
5101 /* Can't break out of the loop here because sometimes
5102 a binding level will have duplicate bindings for
5103 PT names. It's gross, but I haven't time to fix it. */
5104 }
5105 }
5106 newval = TREE_TYPE (t);
5107 if (ptr == (tree *)0)
5108 {
5109 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5110 up here if this is changed to an assertion. --KR */
5111 SET_IDENTIFIER_TYPE_VALUE (name, t);
5112 }
5113 else
5114 {
5115 *ptr = newval;
52ed68f7 5116 }
52ed68f7 5117 }
b655c310
NS
5118 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5119 return t;
501c95ff
NF
5120}
5121
9d029ddf 5122/* Process a using-declaration appearing in namespace scope. */
ebed7175 5123
b655c310 5124void
9d029ddf 5125finish_namespace_using_decl (tree decl, tree scope, tree name)
ebed7175 5126{
b655c310 5127 tree orig_decl = decl;
fcb2cdfc 5128
3c9cca88
NS
5129 gcc_checking_assert (current_binding_level->kind == sk_namespace
5130 && !processing_template_decl);
b655c310
NS
5131 decl = validate_nonmember_using_decl (decl, scope, name);
5132 if (decl == NULL_TREE)
5133 return;
ebed7175 5134
3c9cca88
NS
5135 tree *slot = find_namespace_slot (current_namespace, name, true);
5136 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5137 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5138 do_nonmember_using_decl (scope, name, &val, &type);
5139 if (STAT_HACK_P (*slot))
5140 {
5141 STAT_DECL (*slot) = val;
5142 STAT_TYPE (*slot) = type;
5143 }
5144 else if (type)
5145 *slot = stat_hack (val, type);
5146 else
5147 *slot = val;
b655c310
NS
5148
5149 /* Emit debug info. */
3c9cca88 5150 cp_emit_debug_info_for_using (orig_decl, current_namespace);
9d029ddf 5151}
b655c310 5152
3c9cca88 5153/* Process a using-declaration at function scope. */
9d029ddf
NS
5154
5155void
5156finish_local_using_decl (tree decl, tree scope, tree name)
5157{
5158 tree orig_decl = decl;
5159
5160 gcc_checking_assert (current_binding_level->kind != sk_class
5161 && current_binding_level->kind != sk_namespace);
5162 decl = validate_nonmember_using_decl (decl, scope, name);
5163 if (decl == NULL_TREE)
5164 return;
5165
e1cad930 5166 add_decl_expr (decl);
9d029ddf
NS
5167
5168 cxx_binding *binding = find_local_binding (current_binding_level, name);
5169 tree value = binding ? binding->value : NULL_TREE;
5170 tree type = binding ? binding->type : NULL_TREE;
5171
5172 do_nonmember_using_decl (scope, name, &value, &type);
5173
5174 if (!value)
5175 ;
5176 else if (binding && value == binding->value)
5177 ;
5178 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5179 {
5180 update_local_overload (IDENTIFIER_BINDING (name), value);
5181 IDENTIFIER_BINDING (name)->value = value;
5182 }
5183 else
5184 /* Install the new binding. */
5185 push_local_binding (name, value, true);
5186
5187 if (!type)
5188 ;
5189 else if (binding && type == binding->type)
5190 ;
5191 else
5192 {
5193 push_local_binding (name, type, true);
5194 set_identifier_type_value (name, type);
5195 }
5196
5197 /* Emit debug info. */
5198 if (!processing_template_decl)
5199 cp_emit_debug_info_for_using (orig_decl, current_scope ());
ebed7175
DM
5200}
5201
b655c310
NS
5202/* Return the declarations that are members of the namespace NS. */
5203
5204tree
5205cp_namespace_decls (tree ns)
5206{
5207 return NAMESPACE_LEVEL (ns)->names;
52ed68f7
DM
5208}
5209
b655c310 5210/* Combine prefer_type and namespaces_only into flags. */
9ca6556e 5211
b655c310
NS
5212static int
5213lookup_flags (int prefer_type, int namespaces_only)
5214{
5215 if (namespaces_only)
5216 return LOOKUP_PREFER_NAMESPACES;
5217 if (prefer_type > 1)
5218 return LOOKUP_PREFER_TYPES;
5219 if (prefer_type > 0)
5220 return LOOKUP_PREFER_BOTH;
5221 return 0;
5222}
9ca6556e 5223
b655c310
NS
5224/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5225 ignore it or not. Subroutine of lookup_name_real and
5226 lookup_type_scope. */
172a4594
DS
5227
5228static bool
b655c310 5229qualify_lookup (tree val, int flags)
172a4594 5230{
b655c310 5231 if (val == NULL_TREE)
172a4594 5232 return false;
b655c310
NS
5233 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5234 return true;
5235 if (flags & LOOKUP_PREFER_TYPES)
5236 {
5237 tree target_val = strip_using_decl (val);
5238 if (TREE_CODE (target_val) == TYPE_DECL
5239 || TREE_CODE (target_val) == TEMPLATE_DECL)
5240 return true;
5241 }
5242 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
74788b80 5243 return false;
b655c310 5244 /* Look through lambda things that we shouldn't be able to see. */
5c263e84 5245 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
b655c310
NS
5246 return false;
5247 return true;
5248}
74788b80 5249
cb731872
DM
5250/* Is there a "using namespace std;" directive within USINGS? */
5251
5252static bool
5253using_directives_contain_std_p (vec<tree, va_gc> *usings)
5254{
5255 if (!usings)
5256 return false;
5257
5258 for (unsigned ix = usings->length (); ix--;)
5259 if ((*usings)[ix] == std_node)
5260 return true;
5261
5262 return false;
5263}
5264
5265/* Is there a "using namespace std;" directive within the current
5266 namespace (or its ancestors)?
5267 Compare with name_lookup::search_unqualified. */
5268
5269static bool
5270has_using_namespace_std_directive_p ()
5271{
5272 /* Look at local using-directives. */
5273 for (cp_binding_level *level = current_binding_level;
5274 level->kind != sk_namespace;
5275 level = level->level_chain)
5276 if (using_directives_contain_std_p (level->using_directives))
5277 return true;
5278
5279 /* Look at this namespace and its ancestors. */
5280 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5281 {
5282 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5283 return true;
5284
5285 if (scope == global_namespace)
5286 break;
5287 }
5288
5289 return false;
5290}
5291
b655c310
NS
5292/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5293 lookup failed. Search through all available namespaces and print out
5294 possible candidates. If no exact matches are found, and
5295 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5296 suggest the best near-match, if there is one. */
90ea9897 5297
b655c310
NS
5298void
5299suggest_alternatives_for (location_t location, tree name,
5300 bool suggest_misspellings)
90ea9897 5301{
b655c310 5302 vec<tree> candidates = vNULL;
c957e9c0
NS
5303 vec<tree> worklist = vNULL;
5304 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5305 bool limited = false;
5306
5307 /* Breadth-first search of namespaces. Up to limit namespaces
5308 searched (limit zero == unlimited). */
5309 worklist.safe_push (global_namespace);
5310 for (unsigned ix = 0; ix != worklist.length (); ix++)
b655c310 5311 {
c957e9c0 5312 tree ns = worklist[ix];
25396db9 5313 name_lookup lookup (name);
90ea9897 5314
25396db9
NS
5315 if (lookup.search_qualified (ns, false))
5316 candidates.safe_push (lookup.value);
00e8de68 5317
c957e9c0 5318 if (!limited)
00e8de68 5319 {
c957e9c0
NS
5320 /* Look for child namespaces. We have to do this
5321 indirectly because they are chained in reverse order,
5322 which is confusing to the user. */
5323 vec<tree> children = vNULL;
5324
5325 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5326 decl; decl = TREE_CHAIN (decl))
5327 if (TREE_CODE (decl) == NAMESPACE_DECL
25396db9
NS
5328 && !DECL_NAMESPACE_ALIAS (decl)
5329 && !DECL_NAMESPACE_INLINE_P (decl))
c957e9c0
NS
5330 children.safe_push (decl);
5331
5332 while (!limited && !children.is_empty ())
b655c310 5333 {
c957e9c0
NS
5334 if (worklist.length () == limit)
5335 {
5336 /* Unconditionally warn that the search was truncated. */
5337 inform (location,
5338 "maximum limit of %d namespaces searched for %qE",
5339 limit, name);
5340 limited = true;
5341 }
5342 else
5343 worklist.safe_push (children.pop ());
b655c310 5344 }
c957e9c0 5345 children.release ();
b655c310 5346 }
b655c310 5347 }
c957e9c0 5348 worklist.release ();
c8094d83 5349
c957e9c0
NS
5350 if (candidates.length ())
5351 {
5352 inform_n (location, candidates.length (),
5353 "suggested alternative:",
5354 "suggested alternatives:");
5355 for (unsigned ix = 0; ix != candidates.length (); ix++)
5356 {
5357 tree val = candidates[ix];
c8094d83 5358
c957e9c0
NS
5359 inform (location_of (val), " %qE", val);
5360 }
5361 candidates.release ();
cb731872 5362 return;
c957e9c0 5363 }
cb731872
DM
5364
5365 /* No candidates were found in the available namespaces. */
5366
5367 /* If there's a "using namespace std;" active, and this
5368 is one of the most common "std::" names, then it's probably a
5369 missing #include. */
5370 if (has_using_namespace_std_directive_p ())
5371 if (maybe_suggest_missing_std_header (location, name))
5372 return;
5373
5374 /* Otherwise, consider misspellings. */
5375 if (!suggest_misspellings)
5376 return;
5377 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5378 location))
c957e9c0
NS
5379 {
5380 /* Show a spelling correction. */
5381 gcc_rich_location richloc (location);
00e8de68 5382
6c7a259b
DM
5383 richloc.add_fixit_replace (hint.suggestion ());
5384 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
c957e9c0 5385 }
b655c310 5386}
00e8de68 5387
e1c7971b
DM
5388/* A well-known name within the C++ standard library, returned by
5389 get_std_name_hint. */
5390
5391struct std_name_hint
5392{
5393 /* A name within "std::". */
5394 const char *name;
5395
5396 /* The header name defining it within the C++ Standard Library
5397 (with '<' and '>'). */
5398 const char *header;
5399
5400 /* The dialect of C++ in which this was added. */
5401 enum cxx_dialect min_dialect;
5402};
5403
b655c310
NS
5404/* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5405 for some of the most common names within "std::".
e1c7971b 5406 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
00e8de68 5407
e1c7971b 5408static const std_name_hint *
b655c310
NS
5409get_std_name_hint (const char *name)
5410{
b655c310 5411 static const std_name_hint hints[] = {
e1c7971b
DM
5412 /* <any>. */
5413 {"any", "<any>", cxx17},
5414 {"any_cast", "<any>", cxx17},
5415 {"make_any", "<any>", cxx17},
b655c310 5416 /* <array>. */
e1c7971b
DM
5417 {"array", "<array>", cxx11},
5418 /* <atomic>. */
5419 {"atomic", "<atomic>", cxx11},
5420 {"atomic_flag", "<atomic>", cxx11},
5421 /* <bitset>. */
5422 {"bitset", "<bitset>", cxx11},
b3e862e0 5423 /* <complex>. */
e1c7971b
DM
5424 {"complex", "<complex>", cxx98},
5425 {"complex_literals", "<complex>", cxx98},
5426 /* <condition_variable>. */
5427 {"condition_variable", "<condition_variable>", cxx11},
5428 {"condition_variable_any", "<condition_variable>", cxx11},
b655c310 5429 /* <deque>. */
e1c7971b 5430 {"deque", "<deque>", cxx98},
b655c310 5431 /* <forward_list>. */
e1c7971b 5432 {"forward_list", "<forward_list>", cxx11},
b655c310 5433 /* <fstream>. */
e1c7971b
DM
5434 {"basic_filebuf", "<fstream>", cxx98},
5435 {"basic_ifstream", "<fstream>", cxx98},
5436 {"basic_ofstream", "<fstream>", cxx98},
5437 {"basic_fstream", "<fstream>", cxx98},
5438 {"fstream", "<fstream>", cxx98},
5439 {"ifstream", "<fstream>", cxx98},
5440 {"ofstream", "<fstream>", cxx98},
5441 /* <functional>. */
5442 {"bind", "<functional>", cxx11},
5443 {"function", "<functional>", cxx11},
5444 {"hash", "<functional>", cxx11},
5445 {"mem_fn", "<functional>", cxx11},
5446 /* <future>. */
5447 {"async", "<future>", cxx11},
5448 {"future", "<future>", cxx11},
5449 {"packaged_task", "<future>", cxx11},
5450 {"promise", "<future>", cxx11},
b655c310 5451 /* <iostream>. */
e1c7971b
DM
5452 {"cin", "<iostream>", cxx98},
5453 {"cout", "<iostream>", cxx98},
5454 {"cerr", "<iostream>", cxx98},
5455 {"clog", "<iostream>", cxx98},
5456 {"wcin", "<iostream>", cxx98},
5457 {"wcout", "<iostream>", cxx98},
5458 {"wclog", "<iostream>", cxx98},
5459 /* <istream>. */
5460 {"istream", "<istream>", cxx98},
5461 /* <iterator>. */
5462 {"advance", "<iterator>", cxx98},
5463 {"back_inserter", "<iterator>", cxx98},
5464 {"begin", "<iterator>", cxx11},
5465 {"distance", "<iterator>", cxx98},
5466 {"end", "<iterator>", cxx11},
5467 {"front_inserter", "<iterator>", cxx98},
5468 {"inserter", "<iterator>", cxx98},
5469 {"istream_iterator", "<iterator>", cxx98},
5470 {"istreambuf_iterator", "<iterator>", cxx98},
5471 {"iterator_traits", "<iterator>", cxx98},
5472 {"move_iterator", "<iterator>", cxx11},
5473 {"next", "<iterator>", cxx11},
5474 {"ostream_iterator", "<iterator>", cxx98},
5475 {"ostreambuf_iterator", "<iterator>", cxx98},
5476 {"prev", "<iterator>", cxx11},
5477 {"reverse_iterator", "<iterator>", cxx98},
5478 /* <ostream>. */
5479 {"ostream", "<ostream>", cxx98},
b655c310 5480 /* <list>. */
e1c7971b 5481 {"list", "<list>", cxx98},
b655c310 5482 /* <map>. */
e1c7971b
DM
5483 {"map", "<map>", cxx98},
5484 {"multimap", "<map>", cxx98},
62e98ef1 5485 /* <memory>. */
e1c7971b
DM
5486 {"make_shared", "<memory>", cxx11},
5487 {"make_unique", "<memory>", cxx11},
5488 {"shared_ptr", "<memory>", cxx11},
5489 {"unique_ptr", "<memory>", cxx11},
5490 {"weak_ptr", "<memory>", cxx11},
5491 /* <mutex>. */
5492 {"mutex", "<mutex>", cxx11},
5493 {"timed_mutex", "<mutex>", cxx11},
5494 {"recursive_mutex", "<mutex>", cxx11},
5495 {"recursive_timed_mutex", "<mutex>", cxx11},
5496 {"once_flag", "<mutex>", cxx11},
5497 {"call_once,", "<mutex>", cxx11},
5498 {"lock", "<mutex>", cxx11},
5499 {"scoped_lock", "<mutex>", cxx17},
5500 {"try_lock", "<mutex>", cxx11},
5501 {"lock_guard", "<mutex>", cxx11},
5502 {"unique_lock", "<mutex>", cxx11},
5503 /* <optional>. */
5504 {"optional", "<optional>", cxx17},
5505 {"make_optional", "<optional>", cxx17},
b655c310 5506 /* <ostream>. */
e1c7971b
DM
5507 {"ostream", "<ostream>", cxx98},
5508 {"wostream", "<ostream>", cxx98},
5509 {"ends", "<ostream>", cxx98},
5510 {"flush", "<ostream>", cxx98},
5511 {"endl", "<ostream>", cxx98},
5512 /* <queue>. */
5513 {"queue", "<queue>", cxx98},
5514 {"priority_queue", "<queue>", cxx98},
b655c310 5515 /* <set>. */
e1c7971b
DM
5516 {"set", "<set>", cxx98},
5517 {"multiset", "<set>", cxx98},
5518 /* <shared_mutex>. */
5519 {"shared_lock", "<shared_mutex>", cxx14},
5520 {"shared_mutex", "<shared_mutex>", cxx17},
5521 {"shared_timed_mutex", "<shared_mutex>", cxx14},
b655c310 5522 /* <sstream>. */
e1c7971b
DM
5523 {"basic_stringbuf", "<sstream>", cxx98},
5524 {"basic_istringstream", "<sstream>", cxx98},
5525 {"basic_ostringstream", "<sstream>", cxx98},
5526 {"basic_stringstream", "<sstream>", cxx98},
5527 {"istringstream", "<sstream>", cxx98},
5528 {"ostringstream", "<sstream>", cxx98},
5529 {"stringstream", "<sstream>", cxx98},
b655c310 5530 /* <stack>. */
e1c7971b 5531 {"stack", "<stack>", cxx98},
b655c310 5532 /* <string>. */
e1c7971b
DM
5533 {"basic_string", "<string>", cxx98},
5534 {"string", "<string>", cxx98},
5535 {"wstring", "<string>", cxx98},
5536 {"u16string", "<string>", cxx11},
5537 {"u32string", "<string>", cxx11},
5538 /* <string_view>. */
5539 {"string_view", "<string_view>", cxx17},
5540 /* <thread>. */
5541 {"thread", "<thread>", cxx11},
5542 /* <tuple>. */
5543 {"make_tuple", "<tuple>", cxx11},
5544 {"tuple", "<tuple>", cxx11},
5545 {"tuple_element", "<tuple>", cxx11},
5546 {"tuple_size", "<tuple>", cxx11},
b655c310 5547 /* <unordered_map>. */
e1c7971b
DM
5548 {"unordered_map", "<unordered_map>", cxx11},
5549 {"unordered_multimap", "<unordered_map>", cxx11},
b655c310 5550 /* <unordered_set>. */
e1c7971b
DM
5551 {"unordered_set", "<unordered_set>", cxx11},
5552 {"unordered_multiset", "<unordered_set>", cxx11},
62e98ef1 5553 /* <utility>. */
e1c7971b
DM
5554 {"declval", "<utility>", cxx11},
5555 {"forward", "<utility>", cxx11},
5556 {"make_pair", "<utility>", cxx98},
5557 {"move", "<utility>", cxx11},
5558 {"pair", "<utility>", cxx98},
5559 /* <variant>. */
5560 {"variant", "<variant>", cxx17},
5561 {"visit", "<variant>", cxx17},
b655c310 5562 /* <vector>. */
e1c7971b 5563 {"vector", "<vector>", cxx98},
b655c310
NS
5564 };
5565 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5566 for (size_t i = 0; i < num_hints; i++)
5567 {
01512446 5568 if (strcmp (name, hints[i].name) == 0)
e1c7971b 5569 return &hints[i];
b655c310
NS
5570 }
5571 return NULL;
5572}
00e8de68 5573
e1c7971b
DM
5574/* Describe DIALECT. */
5575
5576static const char *
5577get_cxx_dialect_name (enum cxx_dialect dialect)
5578{
5579 switch (dialect)
5580 {
5581 default:
5582 gcc_unreachable ();
5583 case cxx98:
5584 return "C++98";
5585 case cxx11:
5586 return "C++11";
5587 case cxx14:
5588 return "C++14";
5589 case cxx17:
5590 return "C++17";
5591 case cxx2a:
5592 return "C++2a";
5593 }
5594}
5595
cb731872
DM
5596/* Suggest pertinent header files for NAME at LOCATION, for common
5597 names within the "std" namespace.
f661e57e 5598 Return true iff a suggestion was offered. */
00e8de68 5599
f661e57e 5600static bool
cb731872 5601maybe_suggest_missing_std_header (location_t location, tree name)
b655c310 5602{
b655c310 5603 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
c8094d83 5604
b655c310 5605 const char *name_str = IDENTIFIER_POINTER (name);
e1c7971b 5606 const std_name_hint *header_hint = get_std_name_hint (name_str);
f661e57e
DM
5607 if (!header_hint)
5608 return false;
5609
5610 gcc_rich_location richloc (location);
e1c7971b
DM
5611 if (cxx_dialect >= header_hint->min_dialect)
5612 {
5613 const char *header = header_hint->header;
5614 maybe_add_include_fixit (&richloc, header);
5615 inform (&richloc,
5616 "%<std::%s%> is defined in header %qs;"
5617 " did you forget to %<#include %s%>?",
5618 name_str, header, header);
5619 }
5620 else
5621 {
5622 inform (&richloc,
5623 "%<std::%s%> is only available from %s onwards",
5624 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5625 }
f661e57e 5626 return true;
b655c310 5627}
c8094d83 5628
cb731872
DM
5629/* If SCOPE is the "std" namespace, then suggest pertinent header
5630 files for NAME at LOCATION.
5631 Return true iff a suggestion was offered. */
5632
5633static bool
5634maybe_suggest_missing_header (location_t location, tree name, tree scope)
5635{
5636 if (scope == NULL_TREE)
5637 return false;
5638 if (TREE_CODE (scope) != NAMESPACE_DECL)
5639 return false;
5640 /* We only offer suggestions for the "std" namespace. */
5641 if (scope != std_node)
5642 return false;
5643 return maybe_suggest_missing_std_header (location, name);
5644}
5645
b655c310
NS
5646/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5647 lookup failed within the explicitly provided SCOPE. Suggest the
5648 the best meaningful candidates (if any) as a fix-it hint.
5649 Return true iff a suggestion was provided. */
c8094d83 5650
b655c310
NS
5651bool
5652suggest_alternative_in_explicit_scope (location_t location, tree name,
5653 tree scope)
5654{
7518398d
MP
5655 /* Something went very wrong; don't suggest anything. */
5656 if (name == error_mark_node)
5657 return false;
5658
b655c310
NS
5659 /* Resolve any namespace aliases. */
5660 scope = ORIGINAL_NAMESPACE (scope);
b9f673eb 5661
f661e57e
DM
5662 if (maybe_suggest_missing_header (location, name, scope))
5663 return true;
5664
b655c310 5665 cp_binding_level *level = NAMESPACE_LEVEL (scope);
d4d8c232 5666
b655c310
NS
5667 best_match <tree, const char *> bm (name);
5668 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
d4d8c232 5669
b655c310
NS
5670 /* See if we have a good suggesion for the user. */
5671 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5672 if (fuzzy_name)
5673 {
5674 gcc_rich_location richloc (location);
5675 richloc.add_fixit_replace (fuzzy_name);
64a5912c
DM
5676 inform (&richloc, "suggested alternative: %qs",
5677 fuzzy_name);
b655c310
NS
5678 return true;
5679 }
d4d8c232 5680
b655c310
NS
5681 return false;
5682}
d4d8c232 5683
b655c310
NS
5684/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5685 or a class TYPE).
00e8de68 5686
b655c310
NS
5687 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5688 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
00e8de68 5689
b655c310
NS
5690 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5691 declaration found. If no suitable declaration can be found,
5692 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5693 neither a class-type nor a namespace a diagnostic is issued. */
00e8de68 5694
98803730 5695tree
b655c310
NS
5696lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5697 bool find_hidden)
98803730 5698{
b655c310
NS
5699 tree t = NULL_TREE;
5700
5701 if (TREE_CODE (scope) == NAMESPACE_DECL)
5702 {
b655c310
NS
5703 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5704 if (find_hidden)
5705 flags |= LOOKUP_HIDDEN;
9dda0ace
NS
5706 name_lookup lookup (name, flags);
5707
5708 if (qualified_namespace_lookup (scope, &lookup))
5709 t = lookup.value;
b655c310
NS
5710 }
5711 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5712 t = lookup_enumerator (scope, name);
5713 else if (is_class_type (scope, complain))
5714 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5715
5716 if (!t)
5717 return error_mark_node;
5718 return t;
98803730
MS
5719}
5720
b655c310
NS
5721/* [namespace.qual]
5722 Accepts the NAME to lookup and its qualifying SCOPE.
5723 Returns the name/type pair found into the cxx_binding *RESULT,
5724 or false on error. */
461c6fce 5725
b655c310 5726static bool
9dda0ace
NS
5727qualified_namespace_lookup (tree scope, name_lookup *lookup)
5728{
b655c310 5729 timevar_start (TV_NAME_LOOKUP);
9dda0ace
NS
5730 query_oracle (lookup->name);
5731 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
b655c310 5732 timevar_stop (TV_NAME_LOOKUP);
9dda0ace 5733 return found;
575bfb00
LC
5734}
5735
b655c310
NS
5736/* Helper function for lookup_name_fuzzy.
5737 Traverse binding level LVL, looking for good name matches for NAME
5738 (and BM). */
5739static void
5740consider_binding_level (tree name, best_match <tree, const char *> &bm,
5741 cp_binding_level *lvl, bool look_within_fields,
5742 enum lookup_name_fuzzy_kind kind)
00e8de68 5743{
b655c310
NS
5744 if (look_within_fields)
5745 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5746 {
5747 tree type = lvl->this_entity;
5748 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5749 tree best_matching_field
5750 = lookup_member_fuzzy (type, name, want_type_p);
5751 if (best_matching_field)
5752 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5753 }
00e8de68 5754
c79144f8
DM
5755 /* Only suggest names reserved for the implementation if NAME begins
5756 with an underscore. */
5757 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5758
b655c310 5759 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
00e8de68 5760 {
b655c310 5761 tree d = t;
00e8de68 5762
b655c310
NS
5763 /* OVERLOADs or decls from using declaration are wrapped into
5764 TREE_LIST. */
5765 if (TREE_CODE (d) == TREE_LIST)
87c976ae 5766 d = OVL_FIRST (TREE_VALUE (d));
00e8de68 5767
b655c310
NS
5768 /* Don't use bindings from implicitly declared functions,
5769 as they were likely misspellings themselves. */
5770 if (TREE_TYPE (d) == error_mark_node)
5771 continue;
00e8de68 5772
b655c310
NS
5773 /* Skip anticipated decls of builtin functions. */
5774 if (TREE_CODE (d) == FUNCTION_DECL
5775 && DECL_BUILT_IN (d)
5776 && DECL_ANTICIPATED (d))
5777 continue;
575bfb00 5778
66bd3086
DM
5779 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5780 within range for). */
5781 if (TREE_CODE (d) == VAR_DECL
5782 && DECL_ARTIFICIAL (d))
5783 continue;
5784
c79144f8
DM
5785 tree suggestion = DECL_NAME (d);
5786 if (!suggestion)
5787 continue;
5788
5789 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5790
5791 /* Ignore internal names with spaces in them. */
5792 if (strchr (suggestion_str, ' '))
5793 continue;
5794
5795 /* Don't suggest names that are reserved for use by the
5796 implementation, unless NAME began with an underscore. */
5797 if (name_reserved_for_implementation_p (suggestion_str)
5798 && !consider_implementation_names)
5799 continue;
5800
5801 bm.consider (suggestion_str);
b655c310 5802 }
575bfb00
LC
5803}
5804
01ada121
DM
5805/* Subclass of deferred_diagnostic. Notify the user that the
5806 given macro was used before it was defined.
5807 This can be done in the C++ frontend since tokenization happens
5808 upfront. */
5809
5810class macro_use_before_def : public deferred_diagnostic
5811{
5812 public:
b0cc1e53
DM
5813 /* Factory function. Return a new macro_use_before_def instance if
5814 appropriate, or return NULL. */
5815 static macro_use_before_def *
5816 maybe_make (location_t use_loc, cpp_hashnode *macro)
5817 {
5818 source_location def_loc = cpp_macro_definition_location (macro);
5819 if (def_loc == UNKNOWN_LOCATION)
5820 return NULL;
5821
5822 /* We only want to issue a note if the macro was used *before* it was
5823 defined.
5824 We don't want to issue a note for cases where a macro was incorrectly
5825 used, leaving it unexpanded (e.g. by using the wrong argument
5826 count). */
5827 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5828 return NULL;
5829
5830 return new macro_use_before_def (use_loc, macro);
5831 }
5832
5833 private:
01ada121
DM
5834 /* Ctor. LOC is the location of the usage. MACRO is the
5835 macro that was used. */
5836 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5837 : deferred_diagnostic (loc), m_macro (macro)
5838 {
5839 gcc_assert (macro);
5840 }
5841
5842 ~macro_use_before_def ()
5843 {
5844 if (is_suppressed_p ())
5845 return;
5846
b0cc1e53
DM
5847 inform (get_location (), "the macro %qs had not yet been defined",
5848 (const char *)m_macro->ident.str);
5849 inform (cpp_macro_definition_location (m_macro),
5850 "it was later defined here");
01ada121
DM
5851 }
5852
5853 private:
5854 cpp_hashnode *m_macro;
5855};
5856
0d7d8e66
DM
5857/* Determine if it can ever make sense to offer RID as a suggestion for
5858 a misspelling.
5859
5860 Subroutine of lookup_name_fuzzy. */
5861
5862static bool
5863suggest_rid_p (enum rid rid)
5864{
5865 switch (rid)
5866 {
5867 /* Support suggesting function-like keywords. */
5868 case RID_STATIC_ASSERT:
5869 return true;
5870
5871 default:
5872 /* Support suggesting the various decl-specifier words, to handle
5873 e.g. "singed" vs "signed" typos. */
5874 if (cp_keyword_starts_decl_specifier_p (rid))
5875 return true;
5876
5877 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5878 and "do" for short misspellings, which are likely to lead to
5879 nonsensical results. */
5880 return false;
5881 }
5882}
01ada121 5883
b655c310
NS
5884/* Search for near-matches for NAME within the current bindings, and within
5885 macro names, returning the best match as a const char *, or NULL if
01ada121
DM
5886 no reasonable match is found.
5887
5888 Use LOC for any deferred diagnostics. */
575bfb00 5889
6c7a259b 5890name_hint
01ada121 5891lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
e8f43da6 5892{
b655c310 5893 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
e8f43da6 5894
26edace6
DM
5895 /* First, try some well-known names in the C++ standard library, in case
5896 the user forgot a #include. */
5897 const char *header_hint
5898 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5899 if (header_hint)
5900 return name_hint (NULL,
5901 new suggest_missing_header (loc,
5902 IDENTIFIER_POINTER (name),
5903 header_hint));
5904
b655c310 5905 best_match <tree, const char *> bm (name);
e8f43da6 5906
b655c310
NS
5907 cp_binding_level *lvl;
5908 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5909 consider_binding_level (name, bm, lvl, true, kind);
e8f43da6 5910
b655c310
NS
5911 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5912 consider_binding_level (name, bm, lvl, false, kind);
e8f43da6 5913
b655c310
NS
5914 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5915 as:
5916 x = SOME_OTHER_MACRO (y);
5917 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5918 as a misspelled identifier.
e8f43da6 5919
b655c310
NS
5920 Use the best distance so far so that a candidate is only set if
5921 a macro is better than anything so far. This allows early rejection
5922 (without calculating the edit distance) of macro names that must have
5923 distance >= bm.get_best_distance (), and means that we only get a
5924 non-NULL result for best_macro_match if it's better than any of
5925 the identifiers already checked. */
5926 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5927 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5928 /* If a macro is the closest so far to NAME, consider it. */
5929 if (best_macro)
5930 bm.consider ((const char *)best_macro->ident.str);
01ada121
DM
5931 else if (bmm.get_best_distance () == 0)
5932 {
b0cc1e53
DM
5933 /* If we have an exact match for a macro name, then either the
5934 macro was used with the wrong argument count, or the macro
5935 has been used before it was defined. */
01ada121 5936 cpp_hashnode *macro = bmm.blithely_get_best_candidate ();
e30947eb 5937 if (macro && (macro->flags & NODE_BUILTIN) == 0)
01ada121 5938 return name_hint (NULL,
b0cc1e53 5939 macro_use_before_def::maybe_make (loc, macro));
01ada121 5940 }
00e8de68 5941
b655c310
NS
5942 /* Try the "starts_decl_specifier_p" keywords to detect
5943 "singed" vs "signed" typos. */
5944 for (unsigned i = 0; i < num_c_common_reswords; i++)
5945 {
5946 const c_common_resword *resword = &c_common_reswords[i];
00e8de68 5947
0d7d8e66
DM
5948 if (!suggest_rid_p (resword->rid))
5949 continue;
00e8de68 5950
b655c310
NS
5951 tree resword_identifier = ridpointers [resword->rid];
5952 if (!resword_identifier)
5953 continue;
5954 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
00e8de68 5955
b655c310
NS
5956 /* Only consider reserved words that survived the
5957 filtering in init_reswords (e.g. for -std). */
84c0088f 5958 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
b655c310 5959 continue;
00e8de68 5960
b655c310
NS
5961 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5962 }
5963
6c7a259b 5964 return name_hint (bm.get_best_meaningful_candidate (), NULL);
b655c310 5965}
5a167978 5966
b655c310 5967/* Subroutine of outer_binding.
5a167978 5968
b655c310
NS
5969 Returns TRUE if BINDING is a binding to a template parameter of
5970 SCOPE. In that case SCOPE is the scope of a primary template
5971 parameter -- in the sense of G++, i.e, a template that has its own
5972 template header.
5a167978 5973
b655c310 5974 Returns FALSE otherwise. */
5a167978
GDR
5975
5976static bool
b655c310
NS
5977binding_to_template_parms_of_scope_p (cxx_binding *binding,
5978 cp_binding_level *scope)
5a167978 5979{
b655c310
NS
5980 tree binding_value, tmpl, tinfo;
5981 int level;
5a167978 5982
b655c310
NS
5983 if (!binding || !scope || !scope->this_entity)
5984 return false;
5985
5986 binding_value = binding->value ? binding->value : binding->type;
5987 tinfo = get_template_info (scope->this_entity);
5988
5989 /* BINDING_VALUE must be a template parm. */
5990 if (binding_value == NULL_TREE
5991 || (!DECL_P (binding_value)
5992 || !DECL_TEMPLATE_PARM_P (binding_value)))
5993 return false;
5994
5995 /* The level of BINDING_VALUE. */
5996 level =
5997 template_type_parameter_p (binding_value)
5998 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5999 (TREE_TYPE (binding_value)))
6000 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6001
6002 /* The template of the current scope, iff said scope is a primary
6003 template. */
6004 tmpl = (tinfo
6005 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6006 ? TI_TEMPLATE (tinfo)
6007 : NULL_TREE);
6008
6009 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6010 then BINDING_VALUE is a parameter of TMPL. */
6011 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5a167978
GDR
6012}
6013
b655c310
NS
6014/* Return the innermost non-namespace binding for NAME from a scope
6015 containing BINDING, or, if BINDING is NULL, the current scope.
6016 Please note that for a given template, the template parameters are
6017 considered to be in the scope containing the current scope.
6018 If CLASS_P is false, then class bindings are ignored. */
86098eb8 6019
b655c310
NS
6020cxx_binding *
6021outer_binding (tree name,
6022 cxx_binding *binding,
6023 bool class_p)
86098eb8 6024{
b655c310
NS
6025 cxx_binding *outer;
6026 cp_binding_level *scope;
6027 cp_binding_level *outer_scope;
d4ccba66 6028
b655c310 6029 if (binding)
86098eb8 6030 {
b655c310
NS
6031 scope = binding->scope->level_chain;
6032 outer = binding->previous;
6033 }
6034 else
6035 {
6036 scope = current_binding_level;
6037 outer = IDENTIFIER_BINDING (name);
86098eb8 6038 }
b655c310 6039 outer_scope = outer ? outer->scope : NULL;
d4ccba66 6040
b655c310
NS
6041 /* Because we create class bindings lazily, we might be missing a
6042 class binding for NAME. If there are any class binding levels
6043 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6044 declared, we must lookup NAME in those class scopes. */
6045 if (class_p)
6046 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6047 {
6048 if (scope->kind == sk_class)
6049 {
6050 cxx_binding *class_binding;
d4ccba66 6051
b655c310
NS
6052 class_binding = get_class_binding (name, scope);
6053 if (class_binding)
6054 {
6055 /* Thread this new class-scope binding onto the
6056 IDENTIFIER_BINDING list so that future lookups
6057 find it quickly. */
6058 class_binding->previous = outer;
6059 if (binding)
6060 binding->previous = class_binding;
6061 else
6062 IDENTIFIER_BINDING (name) = class_binding;
6063 return class_binding;
6064 }
6065 }
6066 /* If we are in a member template, the template parms of the member
6067 template are considered to be inside the scope of the containing
6068 class, but within G++ the class bindings are all pushed between the
6069 template parms and the function body. So if the outer binding is
6070 a template parm for the current scope, return it now rather than
6071 look for a class binding. */
6072 if (outer_scope && outer_scope->kind == sk_template_parms
6073 && binding_to_template_parms_of_scope_p (outer, scope))
6074 return outer;
6075
6076 scope = scope->level_chain;
6077 }
6078
6079 return outer;
86098eb8
JM
6080}
6081
b655c310
NS
6082/* Return the innermost block-scope or class-scope value binding for
6083 NAME, or NULL_TREE if there is no such binding. */
5a167978 6084
b655c310
NS
6085tree
6086innermost_non_namespace_value (tree name)
5a167978 6087{
b655c310
NS
6088 cxx_binding *binding;
6089 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6090 return binding ? binding->value : NULL_TREE;
6091}
5a167978 6092
b655c310
NS
6093/* Look up NAME in the current binding level and its superiors in the
6094 namespace of variables, functions and typedefs. Return a ..._DECL
6095 node of some kind representing its definition if there is only one
6096 such declaration, or return a TREE_LIST with all the overloaded
6097 definitions if there are many, or return 0 if it is undefined.
6098 Hidden name, either friend declaration or built-in function, are
6099 not ignored.
86098eb8 6100
b655c310
NS
6101 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6102 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6103 Otherwise we prefer non-TYPE_DECLs.
c8094d83 6104
b655c310
NS
6105 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6106 BLOCK_P is false, bindings in block scopes are ignored. */
4cfaec1c 6107
b655c310
NS
6108static tree
6109lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6110 int namespaces_only, int flags)
6111{
6112 cxx_binding *iter;
6113 tree val = NULL_TREE;
5a167978 6114
b655c310
NS
6115 query_oracle (name);
6116
6117 /* Conversion operators are handled specially because ordinary
6118 unqualified name lookup will not find template conversion
6119 operators. */
84c0088f 6120 if (IDENTIFIER_CONV_OP_P (name))
d63d5d0c 6121 {
b655c310 6122 cp_binding_level *level;
d63d5d0c 6123
b655c310
NS
6124 for (level = current_binding_level;
6125 level && level->kind != sk_namespace;
6126 level = level->level_chain)
6127 {
6128 tree class_type;
6129 tree operators;
6130
6131 /* A conversion operator can only be declared in a class
6132 scope. */
6133 if (level->kind != sk_class)
6134 continue;
6135
6136 /* Lookup the conversion operator in the class. */
6137 class_type = level->this_entity;
6138 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6139 if (operators)
6140 return operators;
6141 }
6142
6143 return NULL_TREE;
d63d5d0c 6144 }
c8094d83 6145
b655c310
NS
6146 flags |= lookup_flags (prefer_type, namespaces_only);
6147
6148 /* First, look in non-namespace scopes. */
6149
6150 if (current_class_type == NULL_TREE)
6151 nonclass = 1;
5a167978 6152
b655c310
NS
6153 if (block_p || !nonclass)
6154 for (iter = outer_binding (name, NULL, !nonclass);
6155 iter;
6156 iter = outer_binding (name, iter, !nonclass))
6157 {
6158 tree binding;
5a167978 6159
b655c310
NS
6160 /* Skip entities we don't want. */
6161 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6162 continue;
5a167978 6163
b655c310
NS
6164 /* If this is the kind of thing we're looking for, we're done. */
6165 if (qualify_lookup (iter->value, flags))
6166 binding = iter->value;
6167 else if ((flags & LOOKUP_PREFER_TYPES)
6168 && qualify_lookup (iter->type, flags))
6169 binding = iter->type;
6170 else
6171 binding = NULL_TREE;
5a167978 6172
b655c310
NS
6173 if (binding)
6174 {
ef4c5e78 6175 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
b655c310
NS
6176 {
6177 /* A non namespace-scope binding can only be hidden in the
6178 presence of a local class, due to friend declarations.
5a167978 6179
b655c310 6180 In particular, consider:
ba796308 6181
b655c310
NS
6182 struct C;
6183 void f() {
6184 struct A {
6185 friend struct B;
6186 friend struct C;
6187 void g() {
6188 B* b; // error: B is hidden
6189 C* c; // OK, finds ::C
6190 }
6191 };
6192 B *b; // error: B is hidden
6193 C *c; // OK, finds ::C
6194 struct B {};
6195 B *bb; // OK
6196 }
5a167978 6197
b655c310
NS
6198 The standard says that "B" is a local class in "f"
6199 (but not nested within "A") -- but that name lookup
6200 for "B" does not find this declaration until it is
6201 declared directly with "f".
5a167978 6202
b655c310 6203 In particular:
c8094d83 6204
b655c310 6205 [class.friend]
5a167978 6206
b655c310
NS
6207 If a friend declaration appears in a local class and
6208 the name specified is an unqualified name, a prior
6209 declaration is looked up without considering scopes
6210 that are outside the innermost enclosing non-class
6211 scope. For a friend function declaration, if there is
6212 no prior declaration, the program is ill-formed. For a
6213 friend class declaration, if there is no prior
6214 declaration, the class that is specified belongs to the
6215 innermost enclosing non-class scope, but if it is
6216 subsequently referenced, its name is not found by name
6217 lookup until a matching declaration is provided in the
6218 innermost enclosing nonclass scope.
ccb14335 6219
b655c310
NS
6220 So just keep looking for a non-hidden binding.
6221 */
6222 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6223 continue;
6224 }
6225 val = binding;
6226 break;
6227 }
6228 }
2395cd2e 6229
b655c310
NS
6230 /* Now lookup in namespace scopes. */
6231 if (!val)
932f48ac
NS
6232 {
6233 name_lookup lookup (name, flags);
6234 if (lookup.search_unqualified
6235 (current_decl_namespace (), current_binding_level))
6236 val = lookup.value;
6237 }
c8b2e872 6238
b655c310
NS
6239 /* If we have a single function from a using decl, pull it out. */
6240 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6241 val = OVL_FUNCTION (val);
6242
6243 return val;
db10df3d
JM
6244}
6245
b655c310 6246/* Wrapper for lookup_name_real_1. */
db10df3d 6247
b655c310
NS
6248tree
6249lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6250 int namespaces_only, int flags)
db10df3d 6251{
b655c310
NS
6252 tree ret;
6253 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6254 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6255 namespaces_only, flags);
6256 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6257 return ret;
6258}
db10df3d 6259
b655c310
NS
6260tree
6261lookup_name_nonclass (tree name)
6262{
6263 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
db10df3d
JM
6264}
6265
b655c310
NS
6266tree
6267lookup_name (tree name)
6268{
6269 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6270}
db10df3d 6271
b655c310
NS
6272tree
6273lookup_name_prefer_type (tree name, int prefer_type)
db10df3d 6274{
b655c310
NS
6275 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6276}
db10df3d 6277
b655c310
NS
6278/* Look up NAME for type used in elaborated name specifier in
6279 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6280 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6281 name, more scopes are checked if cleanup or template parameter
6282 scope is encountered.
db10df3d 6283
b655c310
NS
6284 Unlike lookup_name_real, we make sure that NAME is actually
6285 declared in the desired scope, not from inheritance, nor using
6286 directive. For using declaration, there is DR138 still waiting
6287 to be resolved. Hidden name coming from an earlier friend
6288 declaration is also returned.
db10df3d 6289
b655c310
NS
6290 A TYPE_DECL best matching the NAME is returned. Catching error
6291 and issuing diagnostics are caller's responsibility. */
db10df3d 6292
b655c310
NS
6293static tree
6294lookup_type_scope_1 (tree name, tag_scope scope)
6295{
6296 cxx_binding *iter = NULL;
6297 tree val = NULL_TREE;
3c9cca88 6298 cp_binding_level *level = NULL;
db10df3d 6299
b655c310
NS
6300 /* Look in non-namespace scope first. */
6301 if (current_binding_level->kind != sk_namespace)
6302 iter = outer_binding (name, NULL, /*class_p=*/ true);
6303 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5a167978 6304 {
b655c310
NS
6305 /* Check if this is the kind of thing we're looking for.
6306 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6307 base class. For ITER->VALUE, we can simply use
6308 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6309 our own check.
5a167978 6310
b655c310
NS
6311 We check ITER->TYPE before ITER->VALUE in order to handle
6312 typedef struct C {} C;
6313 correctly. */
5a167978 6314
b655c310
NS
6315 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6316 && (scope != ts_current
6317 || LOCAL_BINDING_P (iter)
6318 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6319 val = iter->type;
6320 else if ((scope != ts_current
6321 || !INHERITED_VALUE_BINDING_P (iter))
6322 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6323 val = iter->value;
5a167978 6324
b655c310
NS
6325 if (val)
6326 break;
6327 }
5a167978 6328
b655c310 6329 /* Look in namespace scope. */
3c9cca88
NS
6330 if (val)
6331 level = iter->scope;
6332 else
5a167978 6333 {
3c9cca88 6334 tree ns = current_decl_namespace ();
b655c310 6335
3c9cca88 6336 if (tree *slot = find_namespace_slot (ns, name))
b655c310
NS
6337 {
6338 /* If this is the kind of thing we're looking for, we're done. */
3c9cca88
NS
6339 if (tree type = MAYBE_STAT_TYPE (*slot))
6340 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6341 val = type;
6342 if (!val)
6343 {
6344 if (tree decl = MAYBE_STAT_DECL (*slot))
6345 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6346 val = decl;
6347 }
6348 level = NAMESPACE_LEVEL (ns);
b655c310 6349 }
5a167978 6350 }
b655c310
NS
6351
6352 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6353 and template parameter scopes. */
6354 if (val)
5a167978 6355 {
b655c310
NS
6356 cp_binding_level *b = current_binding_level;
6357 while (b)
6358 {
3c9cca88 6359 if (level == b)
b655c310 6360 return val;
5d80a306 6361
b655c310
NS
6362 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6363 || b->kind == sk_function_parms)
6364 b = b->level_chain;
6365 else if (b->kind == sk_class
6366 && scope == ts_within_enclosing_non_class)
6367 b = b->level_chain;
6368 else
6369 break;
6370 }
5a167978 6371 }
5a167978 6372
b655c310 6373 return NULL_TREE;
5a167978 6374}
b655c310
NS
6375
6376/* Wrapper for lookup_type_scope_1. */
5a167978 6377
b655c310
NS
6378tree
6379lookup_type_scope (tree name, tag_scope scope)
c166b898 6380{
b655c310
NS
6381 tree ret;
6382 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6383 ret = lookup_type_scope_1 (name, scope);
6384 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6385 return ret;
c166b898
ILT
6386}
6387
b655c310
NS
6388/* Returns true iff DECL is a block-scope extern declaration of a function
6389 or variable. */
6390
6391bool
6392is_local_extern (tree decl)
5a167978 6393{
b655c310 6394 cxx_binding *binding;
5a167978 6395
b655c310
NS
6396 /* For functions, this is easy. */
6397 if (TREE_CODE (decl) == FUNCTION_DECL)
6398 return DECL_LOCAL_FUNCTION_P (decl);
d63d5d0c 6399
b655c310
NS
6400 if (!VAR_P (decl))
6401 return false;
6402 if (!current_function_decl)
6403 return false;
5a167978 6404
b655c310
NS
6405 /* For variables, this is not easy. We need to look at the binding stack
6406 for the identifier to see whether the decl we have is a local. */
6407 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6408 binding && binding->scope->kind != sk_namespace;
6409 binding = binding->previous)
6410 if (binding->value == decl)
6411 return LOCAL_BINDING_P (binding);
5a167978 6412
b655c310
NS
6413 return false;
6414}
ca1085f0 6415
00e8de68
GDR
6416/* The type TYPE is being declared. If it is a class template, or a
6417 specialization of a class template, do any processing required and
6418 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6419 being declared a friend. B is the binding level at which this TYPE
6420 should be bound.
6421
6422 Returns the TYPE_DECL for TYPE, which may have been altered by this
6423 processing. */
6424
6425static tree
bd3d082e 6426maybe_process_template_type_declaration (tree type, int is_friend,
2c140474 6427 cp_binding_level *b)
00e8de68
GDR
6428{
6429 tree decl = TYPE_NAME (type);
6430
6431 if (processing_template_parmlist)
6432 /* You can't declare a new template type in a template parameter
6433 list. But, you can declare a non-template type:
6434
0cbd7506 6435 template <class A*> struct S;
00e8de68
GDR
6436
6437 is a forward-declaration of `A'. */
6438 ;
c43e95f8
MM
6439 else if (b->kind == sk_namespace
6440 && current_binding_level->kind != sk_namespace)
6441 /* If this new type is being injected into a containing scope,
6442 then it's not a template type. */
6443 ;
00e8de68
GDR
6444 else
6445 {
9e1e64ec
PC
6446 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6447 || TREE_CODE (type) == ENUMERAL_TYPE);
00e8de68
GDR
6448
6449 if (processing_template_decl)
6450 {
6451 /* This may change after the call to
6452 push_template_decl_real, but we want the original value. */
6453 tree name = DECL_NAME (decl);
6454
bd3d082e 6455 decl = push_template_decl_real (decl, is_friend);
79faac54
PC
6456 if (decl == error_mark_node)
6457 return error_mark_node;
6458
00e8de68
GDR
6459 /* If the current binding level is the binding level for the
6460 template parameters (see the comment in
6461 begin_template_parm_list) and the enclosing level is a class
6462 scope, and we're not looking at a friend, push the
6463 declaration of the member class into the class scope. In the
6464 friend case, push_template_decl will already have put the
6465 friend into global scope, if appropriate. */
6466 if (TREE_CODE (type) != ENUMERAL_TYPE
bd3d082e 6467 && !is_friend && b->kind == sk_template_parms
00e8de68
GDR
6468 && b->level_chain->kind == sk_class)
6469 {
6470 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
e57df6fe 6471
00e8de68
GDR
6472 if (!COMPLETE_TYPE_P (current_class_type))
6473 {
6474 maybe_add_class_template_decl_list (current_class_type,
6475 type, /*friend_p=*/0);
c72a1a86 6476 /* Put this UTD in the table of UTDs for the class. */
e57df6fe
KL
6477 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6478 CLASSTYPE_NESTED_UTDS (current_class_type) =
6479 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6480
6481 binding_table_insert
6482 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
00e8de68
GDR
6483 }
6484 }
6485 }
6486 }
6487
6488 return decl;
6489}
6490
5a24482e
KL
6491/* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6492 that the NAME is a class template, the tag is processed but not pushed.
6493
6494 The pushed scope depend on the SCOPE parameter:
6495 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6496 scope.
6497 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6498 non-template-parameter scope. This case is needed for forward
6499 declarations.
6500 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6501 TS_GLOBAL case except that names within template-parameter scopes
6502 are not pushed at all.
6503
c6f9f83b 6504 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
00e8de68 6505
575bfb00 6506static tree
5256a7f5 6507do_pushtag (tree name, tree type, tag_scope scope)
00e8de68 6508{
6a000704 6509 tree decl;
00e8de68 6510
5256a7f5 6511 cp_binding_level *b = current_binding_level;
8945521a
JM
6512 while (true)
6513 {
6514 if (/* Cleanup scopes are not scopes from the point of view of
6515 the language. */
6516 b->kind == sk_cleanup
6517 /* Neither are function parameter scopes. */
6518 || b->kind == sk_function_parms
6519 /* Neither are the scopes used to hold template parameters
6520 for an explicit specialization. For an ordinary template
6521 declaration, these scopes are not scopes from the point of
6522 view of the language. */
6523 || (b->kind == sk_template_parms
6524 && (b->explicit_spec_p || scope == ts_global)))
6525 b = b->level_chain;
6526 else if (b->kind == sk_class
6527 && scope != ts_current)
6528 {
6529 b = b->level_chain;
6530 if (b->kind == sk_template_parms)
6531 b = b->level_chain;
6532 }
6533 else
6534 break;
6535 }
00e8de68 6536
9dc6f476 6537 gcc_assert (identifier_p (name));
3db45ab5 6538
6a000704 6539 /* Do C++ gratuitous typedefing. */
575bfb00 6540 if (identifier_type_value_1 (name) != type)
00e8de68 6541 {
6a000704
NS
6542 tree tdef;
6543 int in_class = 0;
6544 tree context = TYPE_CONTEXT (type);
00e8de68 6545
6a000704
NS
6546 if (! context)
6547 {
fe7a23a6
JM
6548 cp_binding_level *cb = b;
6549 while (cb->kind != sk_namespace
6550 && cb->kind != sk_class
6551 && (cb->kind != sk_function_parms
6552 || !cb->this_entity))
6553 cb = cb->level_chain;
6554 tree cs = cb->this_entity;
6555
6556 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6557 ? cs == current_function_decl
6558 : TYPE_P (cs) ? cs == current_class_type
6559 : cs == current_namespace);
582f844c 6560
d5f4eddd
JM
6561 if (scope == ts_current
6562 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6a000704 6563 context = cs;
c443f3d5 6564 else if (cs && TYPE_P (cs))
6a000704
NS
6565 /* When declaring a friend class of a local class, we want
6566 to inject the newly named class into the scope
6567 containing the local class, not the namespace
6568 scope. */
6569 context = decl_function_context (get_type_decl (cs));
6570 }
6571 if (!context)
6572 context = current_namespace;
bd3d082e 6573
6a000704
NS
6574 if (b->kind == sk_class
6575 || (b->kind == sk_template_parms
6576 && b->level_chain->kind == sk_class))
6577 in_class = 1;
00e8de68 6578
6a000704
NS
6579 tdef = create_implicit_typedef (name, type);
6580 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6581 if (scope == ts_within_enclosing_non_class)
00e8de68 6582 {
6a000704
NS
6583 /* This is a friend. Make this TYPE_DECL node hidden from
6584 ordinary name lookup. Its corresponding TEMPLATE_DECL
6585 will be marked in push_template_decl_real. */
6586 retrofit_lang_decl (tdef);
6587 DECL_ANTICIPATED (tdef) = 1;
6588 DECL_FRIEND_P (tdef) = 1;
6589 }
e57df6fe 6590
6a000704
NS
6591 decl = maybe_process_template_type_declaration
6592 (type, scope == ts_within_enclosing_non_class, b);
6593 if (decl == error_mark_node)
575bfb00 6594 return decl;
3db45ab5 6595
6a000704
NS
6596 if (b->kind == sk_class)
6597 {
fe7a23a6
JM
6598 if (!TYPE_BEING_DEFINED (current_class_type))
6599 /* Don't push anywhere if the class is complete; a lambda in an
6600 NSDMI is not a member of the class. */
6601 ;
6602 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6a000704
NS
6603 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6604 class. But if it's a member template class, we want
6605 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6606 later. */
6607 finish_member_declaration (decl);
6608 else
6609 pushdecl_class_level (decl);
00e8de68 6610 }
6a000704 6611 else if (b->kind != sk_template_parms)
c5f8391c 6612 {
5256a7f5 6613 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
c5f8391c 6614 if (decl == error_mark_node)
575bfb00 6615 return decl;
d3e19c2c
PC
6616
6617 if (DECL_CONTEXT (decl) == std_node
ad9870f2 6618 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
d3e19c2c
PC
6619 && !CLASSTYPE_TEMPLATE_INFO (type))
6620 {
61f84e25
PC
6621 error ("declaration of %<std::initializer_list%> does not match "
6622 "%<#include <initializer_list>%>, isn't a template");
d3e19c2c
PC
6623 return error_mark_node;
6624 }
c5f8391c 6625 }
6a000704 6626
dc3ca06f
SM
6627 if (! in_class)
6628 set_identifier_type_value_with_scope (name, tdef, b);
6629
6a000704
NS
6630 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6631
6632 /* If this is a local class, keep track of it. We need this
6633 information for name-mangling, and so that it is possible to
6634 find all function definitions in a translation unit in a
6635 convenient way. (It's otherwise tricky to find a member
6636 function definition it's only pointed to from within a local
6637 class.) */
9ededfc5 6638 if (TYPE_FUNCTION_SCOPE_P (type))
fdaf2f48
JM
6639 {
6640 if (processing_template_decl)
6641 {
6642 /* Push a DECL_EXPR so we call pushtag at the right time in
6643 template instantiation rather than in some nested context. */
6644 add_decl_expr (decl);
6645 }
373d1f5f
JM
6646 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6647 else if (!LAMBDA_TYPE_P (type))
9771b263 6648 vec_safe_push (local_classes, type);
fdaf2f48 6649 }
00e8de68 6650 }
c443f3d5 6651
6a000704
NS
6652 if (b->kind == sk_class
6653 && !COMPLETE_TYPE_P (current_class_type))
00e8de68 6654 {
6a000704
NS
6655 maybe_add_class_template_decl_list (current_class_type,
6656 type, /*friend_p=*/0);
3db45ab5 6657
6a000704
NS
6658 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6659 CLASSTYPE_NESTED_UTDS (current_class_type)
6660 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
3db45ab5 6661
6a000704
NS
6662 binding_table_insert
6663 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
00e8de68 6664 }
6a000704
NS
6665
6666 decl = TYPE_NAME (type);
6667 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6a000704 6668
b9e75696
JM
6669 /* Set type visibility now if this is a forward declaration. */
6670 TREE_PUBLIC (decl) = 1;
6671 determine_visibility (decl);
6672
575bfb00
LC
6673 return type;
6674}
6675
5256a7f5 6676/* Wrapper for do_pushtag. */
575bfb00
LC
6677
6678tree
6679pushtag (tree name, tree type, tag_scope scope)
6680{
6681 tree ret;
6682 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5256a7f5 6683 ret = do_pushtag (name, type, scope);
575bfb00
LC
6684 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6685 return ret;
00e8de68 6686}
8db29d88 6687
00e8de68 6688\f
00e8de68
GDR
6689/* Subroutines for reverting temporarily to top-level for instantiation
6690 of templates and such. We actually need to clear out the class- and
6691 local-value slots of all identifiers, so that only the global values
6692 are at all visible. Simply setting current_binding_level to the global
6693 scope isn't enough, because more binding levels may be pushed. */
6694struct saved_scope *scope_chain;
6695
71f15f31
RG
6696/* Return true if ID has not already been marked. */
6697
6698static inline bool
6699store_binding_p (tree id)
6700{
6701 if (!id || !IDENTIFIER_BINDING (id))
6702 return false;
6703
6704 if (IDENTIFIER_MARKED (id))
6705 return false;
6706
6707 return true;
6708}
6709
6710/* Add an appropriate binding to *OLD_BINDINGS which needs to already
6711 have enough space reserved. */
89b578be 6712
f44b0c8e 6713static void
9771b263 6714store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
89b578be 6715{
f32682ca 6716 cxx_saved_binding saved;
89b578be 6717
71f15f31 6718 gcc_checking_assert (store_binding_p (id));
c8094d83 6719
f44b0c8e 6720 IDENTIFIER_MARKED (id) = 1;
89b578be 6721
f32682ca
DN
6722 saved.identifier = id;
6723 saved.binding = IDENTIFIER_BINDING (id);
6724 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
9771b263 6725 (*old_bindings)->quick_push (saved);
89b578be 6726 IDENTIFIER_BINDING (id) = NULL;
89b578be
MM
6727}
6728
f44b0c8e 6729static void
9771b263 6730store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
00e8de68 6731{
199d1d48 6732 static vec<tree> bindings_need_stored;
71f15f31
RG
6733 tree t, id;
6734 size_t i;
00e8de68 6735
575bfb00 6736 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
00e8de68
GDR
6737 for (t = names; t; t = TREE_CHAIN (t))
6738 {
00e8de68
GDR
6739 if (TREE_CODE (t) == TREE_LIST)
6740 id = TREE_PURPOSE (t);
6741 else
6742 id = DECL_NAME (t);
6743
71f15f31 6744 if (store_binding_p (id))
9771b263 6745 bindings_need_stored.safe_push (id);
71f15f31 6746 }
9771b263 6747 if (!bindings_need_stored.is_empty ())
71f15f31 6748 {
9771b263
DN
6749 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6750 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
71f15f31 6751 {
5764ee3c 6752 /* We can apparently have duplicates in NAMES. */
71f15f31
RG
6753 if (store_binding_p (id))
6754 store_binding (id, old_bindings);
6755 }
9771b263 6756 bindings_need_stored.truncate (0);
00e8de68 6757 }
575bfb00 6758 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
00e8de68
GDR
6759}
6760
89b578be
MM
6761/* Like store_bindings, but NAMES is a vector of cp_class_binding
6762 objects, rather than a TREE_LIST. */
6763
f44b0c8e 6764static void
9771b263
DN
6765store_class_bindings (vec<cp_class_binding, va_gc> *names,
6766 vec<cxx_saved_binding, va_gc> **old_bindings)
89b578be 6767{
199d1d48 6768 static vec<tree> bindings_need_stored;
89b578be
MM
6769 size_t i;
6770 cp_class_binding *cb;
89b578be 6771
9771b263 6772 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
71f15f31 6773 if (store_binding_p (cb->identifier))
9771b263
DN
6774 bindings_need_stored.safe_push (cb->identifier);
6775 if (!bindings_need_stored.is_empty ())
71f15f31
RG
6776 {
6777 tree id;
9771b263
DN
6778 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6779 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
71f15f31 6780 store_binding (id, old_bindings);
9771b263 6781 bindings_need_stored.truncate (0);
71f15f31 6782 }
89b578be
MM
6783}
6784
5c712250
PP
6785/* A chain of saved_scope structures awaiting reuse. */
6786
6787static GTY((deletable)) struct saved_scope *free_saved_scope;
6788
c405923d
NS
6789static void
6790do_push_to_top_level (void)
00e8de68
GDR
6791{
6792 struct saved_scope *s;
2c140474 6793 cp_binding_level *b;
f44b0c8e
MM
6794 cxx_saved_binding *sb;
6795 size_t i;
30bcc028 6796 bool need_pop;
00e8de68 6797
5c712250
PP
6798 /* Reuse or create a new structure for this saved scope. */
6799 if (free_saved_scope != NULL)
6800 {
6801 s = free_saved_scope;
6802 free_saved_scope = s->prev;
6803
6804 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6805 memset (s, 0, sizeof (*s));
6806 /* Also reuse the structure's old_bindings vector. */
6807 vec_safe_truncate (old_bindings, 0);
6808 s->old_bindings = old_bindings;
6809 }
6810 else
6811 s = ggc_cleared_alloc<saved_scope> ();
00e8de68
GDR
6812
6813 b = scope_chain ? current_binding_level : 0;
6814
6815 /* If we're in the middle of some function, save our state. */
6816 if (cfun)
6817 {
30bcc028 6818 need_pop = true;
d2784db4 6819 push_function_context ();
00e8de68
GDR
6820 }
6821 else
30bcc028 6822 need_pop = false;
00e8de68 6823
89b578be 6824 if (scope_chain && previous_class_level)
f44b0c8e
MM
6825 store_class_bindings (previous_class_level->class_shadowed,
6826 &s->old_bindings);
00e8de68
GDR
6827
6828 /* Have to include the global scope, because class-scope decls
6829 aren't listed anywhere useful. */
6830 for (; b; b = b->level_chain)
6831 {
6832 tree t;
6833
6834 /* Template IDs are inserted into the global level. If they were
6835 inserted into namespace level, finish_file wouldn't find them
6836 when doing pending instantiations. Therefore, don't stop at
6837 namespace level, but continue until :: . */
c353b8e3 6838 if (global_scope_p (b))
00e8de68
GDR
6839 break;
6840
f44b0c8e 6841 store_bindings (b->names, &s->old_bindings);
00e8de68
GDR
6842 /* We also need to check class_shadowed to save class-level type
6843 bindings, since pushclass doesn't fill in b->names. */
6844 if (b->kind == sk_class)
f44b0c8e 6845 store_class_bindings (b->class_shadowed, &s->old_bindings);
00e8de68
GDR
6846
6847 /* Unwind type-value slots back to top level. */
6848 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6849 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6850 }
f44b0c8e 6851
9771b263 6852 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
f44b0c8e
MM
6853 IDENTIFIER_MARKED (sb->identifier) = 0;
6854
00e8de68 6855 s->prev = scope_chain;
00e8de68
GDR
6856 s->bindings = b;
6857 s->need_pop_function_context = need_pop;
6858 s->function_decl = current_function_decl;
7d882b83
ILT
6859 s->unevaluated_operand = cp_unevaluated_operand;
6860 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
04f7a48e 6861 s->x_stmt_tree.stmts_are_full_exprs_p = true;
00e8de68
GDR
6862
6863 scope_chain = s;
6864 current_function_decl = NULL_TREE;
50200c65 6865 current_lang_base = NULL;
00e8de68
GDR
6866 current_lang_name = lang_name_cplusplus;
6867 current_namespace = global_namespace;
c888c93b 6868 push_class_stack ();
7d882b83
ILT
6869 cp_unevaluated_operand = 0;
6870 c_inhibit_evaluation_warnings = 0;
00e8de68
GDR
6871}
6872
575bfb00 6873static void
c405923d 6874do_pop_from_top_level (void)
00e8de68
GDR
6875{
6876 struct saved_scope *s = scope_chain;
6877 cxx_saved_binding *saved;
f44b0c8e 6878 size_t i;
00e8de68 6879
00e8de68 6880 /* Clear out class-level bindings cache. */
89b578be 6881 if (previous_class_level)
00e8de68 6882 invalidate_class_lookup_cache ();
c888c93b 6883 pop_class_stack ();
00e8de68 6884
50200c65 6885 release_tree_vector (current_lang_base);
00e8de68
GDR
6886
6887 scope_chain = s->prev;
9771b263 6888 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
00e8de68
GDR
6889 {
6890 tree id = saved->identifier;
6891
6892 IDENTIFIER_BINDING (id) = saved->binding;
00e8de68
GDR
6893 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6894 }
6895
6896 /* If we were in the middle of compiling a function, restore our
6897 state. */
6898 if (s->need_pop_function_context)
d2784db4 6899 pop_function_context ();
00e8de68 6900 current_function_decl = s->function_decl;
7d882b83
ILT
6901 cp_unevaluated_operand = s->unevaluated_operand;
6902 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5c712250
PP
6903
6904 /* Make this saved_scope structure available for reuse by
6905 push_to_top_level. */
6906 s->prev = free_saved_scope;
6907 free_saved_scope = s;
00e8de68
GDR
6908}
6909
c405923d
NS
6910/* Push into the scope of the namespace NS, even if it is deeply
6911 nested within another namespace. */
575bfb00 6912
c405923d
NS
6913static void
6914do_push_nested_namespace (tree ns)
6915{
6916 if (ns == global_namespace)
6917 do_push_to_top_level ();
6918 else
6919 {
6920 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6921 gcc_checking_assert
e833f686 6922 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
c405923d
NS
6923 resume_scope (NAMESPACE_LEVEL (ns));
6924 current_namespace = ns;
6925 }
6926}
6927
6928/* Pop back from the scope of the namespace NS, which was previously
6929 entered with push_nested_namespace. */
6930
6931static void
6932do_pop_nested_namespace (tree ns)
6933{
6934 while (ns != global_namespace)
6935 {
6936 ns = CP_DECL_CONTEXT (ns);
6937 current_namespace = ns;
6938 leave_scope ();
6939 }
6940
6941 do_pop_from_top_level ();
6942}
6943
44e00a7a
NS
6944/* Add TARGET to USINGS, if it does not already exist there.
6945 We used to build the complete graph of usings at this point, from
6946 the POV of the source namespaces. Now we build that as we perform
6947 the unqualified search. */
65cc1407
NS
6948
6949static void
3c9feefc 6950add_using_namespace (vec<tree, va_gc> *&usings, tree target)
65cc1407 6951{
3c9feefc
NS
6952 if (usings)
6953 for (unsigned ix = usings->length (); ix--;)
6954 if ((*usings)[ix] == target)
6955 return;
65cc1407 6956
3c9feefc 6957 vec_safe_push (usings, target);
65cc1407
NS
6958}
6959
44e00a7a 6960/* Tell the debug system of a using directive. */
65cc1407
NS
6961
6962static void
e071b767 6963emit_debug_info_using_namespace (tree from, tree target, bool implicit)
65cc1407 6964{
44e00a7a
NS
6965 /* Emit debugging info. */
6966 tree context = from != global_namespace ? from : NULL_TREE;
e071b767
JJ
6967 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6968 implicit);
65cc1407
NS
6969}
6970
6971/* Process a namespace-scope using directive. */
6972
6973void
6974finish_namespace_using_directive (tree target, tree attribs)
6975{
6976 gcc_checking_assert (namespace_bindings_p ());
6977 if (target == error_mark_node)
6978 return;
6979
44e00a7a 6980 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
65cc1407 6981 ORIGINAL_NAMESPACE (target));
44e00a7a 6982 emit_debug_info_using_namespace (current_namespace,
e071b767 6983 ORIGINAL_NAMESPACE (target), false);
65cc1407
NS
6984
6985 if (attribs == error_mark_node)
6986 return;
6987
6988 for (tree a = attribs; a; a = TREE_CHAIN (a))
6989 {
6990 tree name = get_attribute_name (a);
6991 if (is_attribute_p ("strong", name))
6992 {
6993 warning (0, "strong using directive no longer supported");
6994 if (CP_DECL_CONTEXT (target) == current_namespace)
6995 inform (DECL_SOURCE_LOCATION (target),
6996 "you may use an inline namespace instead");
6997 }
6998 else
6999 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7000 }
7001}
7002
7003/* Process a function-scope using-directive. */
7004
7005void
7006finish_local_using_directive (tree target, tree attribs)
7007{
7008 gcc_checking_assert (local_bindings_p ());
7009 if (target == error_mark_node)
7010 return;
7011
7012 if (attribs)
7013 warning (OPT_Wattributes, "attributes ignored on local using directive");
7014
7015 add_stmt (build_stmt (input_location, USING_STMT, target));
7016
44e00a7a
NS
7017 add_using_namespace (current_binding_level->using_directives,
7018 ORIGINAL_NAMESPACE (target));
65cc1407
NS
7019}
7020
c405923d
NS
7021/* Pushes X into the global namespace. */
7022
7023tree
7024pushdecl_top_level (tree x, bool is_friend)
575bfb00
LC
7025{
7026 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
c405923d
NS
7027 do_push_to_top_level ();
7028 x = pushdecl_namespace_level (x, is_friend);
7029 do_pop_from_top_level ();
575bfb00 7030 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
c405923d
NS
7031 return x;
7032}
7033
7034/* Pushes X into the global namespace and calls cp_finish_decl to
7035 register the variable, initializing it with INIT. */
7036
7037tree
7038pushdecl_top_level_and_finish (tree x, tree init)
7039{
7040 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7041 do_push_to_top_level ();
7042 x = pushdecl_namespace_level (x, false);
7043 cp_finish_decl (x, init, false, NULL_TREE, 0);
7044 do_pop_from_top_level ();
7045 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7046 return x;
575bfb00
LC
7047}
7048
945bf9e1
NS
7049/* Enter the namespaces from current_namerspace to NS. */
7050
7051static int
7052push_inline_namespaces (tree ns)
7053{
7054 int count = 0;
7055 if (ns != current_namespace)
7056 {
7057 gcc_assert (ns != global_namespace);
7058 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7059 resume_scope (NAMESPACE_LEVEL (ns));
7060 current_namespace = ns;
7061 count++;
7062 }
7063 return count;
7064}
7065
3a77e7cc
NS
7066/* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7067 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7068 we create an inline namespace (it is up to the caller to check upon
7069 redefinition). Return the number of namespaces entered. */
b655c310 7070
3a77e7cc
NS
7071int
7072push_namespace (tree name, bool make_inline)
b655c310 7073{
b655c310 7074 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3a77e7cc 7075 int count = 0;
b655c310
NS
7076
7077 /* We should not get here if the global_namespace is not yet constructed
7078 nor if NAME designates the global namespace: The global scope is
7079 constructed elsewhere. */
e833f686 7080 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
3a77e7cc 7081
945bf9e1
NS
7082 tree ns = NULL_TREE;
7083 {
7084 name_lookup lookup (name, 0);
7085 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7086 ;
7087 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7088 ;
7089 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7090 {
7091 /* A namespace alias is not allowed here, but if the alias
7092 is for a namespace also inside the current scope,
7093 accept it with a diagnostic. That's better than dying
7094 horribly. */
7095 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7096 {
7097 error ("namespace alias %qD not allowed here, "
7098 "assuming %qD", lookup.value, dna);
7099 ns = dna;
7100 }
7101 }
7102 else
7103 ns = lookup.value;
7104 }
b655c310 7105
3a77e7cc 7106 bool new_ns = false;
945bf9e1
NS
7107 if (ns)
7108 /* DR2061. NS might be a member of an inline namespace. We
7109 need to push into those namespaces. */
7110 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7111 else
b655c310 7112 {
3a77e7cc 7113 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
322763f5
NS
7114 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7115 if (!SCOPE_DEPTH (ns))
7116 /* We only allow depth 255. */
7117 sorry ("cannot nest more than %d namespaces",
7118 SCOPE_DEPTH (current_namespace));
3a77e7cc
NS
7119 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7120 new_ns = true;
7121
7122 if (pushdecl (ns) == error_mark_node)
7123 ns = NULL_TREE;
b655c310 7124 else
b655c310 7125 {
e833f686 7126 if (!name)
3a77e7cc 7127 {
e833f686 7128 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
3a77e7cc
NS
7129
7130 if (!make_inline)
44e00a7a
NS
7131 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7132 ns);
3a77e7cc
NS
7133 }
7134 else if (TREE_PUBLIC (current_namespace))
7135 TREE_PUBLIC (ns) = 1;
7136
7137 if (make_inline)
3c9feefc
NS
7138 {
7139 DECL_NAMESPACE_INLINE_P (ns) = true;
7140 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7141 }
e071b767 7142
e833f686 7143 if (!name || make_inline)
e071b767 7144 emit_debug_info_using_namespace (current_namespace, ns, true);
b655c310 7145 }
3a77e7cc
NS
7146 }
7147
7148 if (ns)
7149 {
7150 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
b655c310 7151 {
3a77e7cc
NS
7152 error ("inline namespace must be specified at initial definition");
7153 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
b655c310 7154 }
3a77e7cc
NS
7155 if (new_ns)
7156 begin_scope (sk_namespace, ns);
7157 else
7158 resume_scope (NAMESPACE_LEVEL (ns));
7159 current_namespace = ns;
7160 count++;
b655c310 7161 }
b655c310
NS
7162
7163 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3a77e7cc 7164 return count;
b655c310
NS
7165}
7166
7167/* Pop from the scope of the current namespace. */
7168
7169void
7170pop_namespace (void)
7171{
3c9feefc
NS
7172 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7173
b655c310
NS
7174 gcc_assert (current_namespace != global_namespace);
7175 current_namespace = CP_DECL_CONTEXT (current_namespace);
7176 /* The binding level is not popped, as it might be re-opened later. */
7177 leave_scope ();
3c9feefc
NS
7178
7179 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310
NS
7180}
7181
c405923d 7182/* External entry points for do_{push_to/pop_from}_top_level. */
b655c310
NS
7183
7184void
c405923d 7185push_to_top_level (void)
b655c310 7186{
c405923d
NS
7187 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7188 do_push_to_top_level ();
7189 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310
NS
7190}
7191
c405923d
NS
7192void
7193pop_from_top_level (void)
7194{
7195 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7196 do_pop_from_top_level ();
7197 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7198}
7199
7200/* External entry points for do_{push,pop}_nested_namespace. */
7201
7202void
7203push_nested_namespace (tree ns)
7204{
7205 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7206 do_push_nested_namespace (ns);
7207 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7208}
b655c310
NS
7209
7210void
7211pop_nested_namespace (tree ns)
7212{
7213 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7214 gcc_assert (current_namespace == ns);
c405923d 7215 do_pop_nested_namespace (ns);
b655c310
NS
7216 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7217}
575bfb00 7218
00e8de68 7219/* Pop off extraneous binding levels left over due to syntax errors.
00e8de68
GDR
7220 We don't pop past namespaces, as they might be valid. */
7221
7222void
7223pop_everything (void)
7224{
7225 if (ENABLE_SCOPE_CHECKING)
7226 verbatim ("XXX entering pop_everything ()\n");
056a17ee 7227 while (!namespace_bindings_p ())
00e8de68
GDR
7228 {
7229 if (current_binding_level->kind == sk_class)
7230 pop_nested_class ();
7231 else
7232 poplevel (0, 0, 0);
7233 }
7234 if (ENABLE_SCOPE_CHECKING)
7235 verbatim ("XXX leaving pop_everything ()\n");
7236}
7237
6097b0c3 7238/* Emit debugging information for using declarations and directives.
c8094d83 7239 If input tree is overloaded fn then emit debug info for all
6097b0c3
DP
7240 candidates. */
7241
98ed9dae 7242void
6097b0c3
DP
7243cp_emit_debug_info_for_using (tree t, tree context)
7244{
099f36ab 7245 /* Don't try to emit any debug information if we have errors. */
1da2ed5f 7246 if (seen_error ())
099f36ab
JM
7247 return;
7248
c8094d83 7249 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6097b0c3 7250 of a builtin function. */
c8094d83 7251 if (TREE_CODE (t) == FUNCTION_DECL
6097b0c3
DP
7252 && DECL_EXTERNAL (t)
7253 && DECL_BUILT_IN (t))
7254 return;
7255
7256 /* Do not supply context to imported_module_or_decl, if
7257 it is a global namespace. */
7258 if (context == global_namespace)
7259 context = NULL_TREE;
c8094d83 7260
e1cad930 7261 t = MAYBE_BASELINK_FUNCTIONS (t);
c8094d83 7262
6097b0c3 7263 /* FIXME: Handle TEMPLATE_DECLs. */
87c976ae
NS
7264 for (lkp_iterator iter (t); iter; ++iter)
7265 {
7266 tree fn = *iter;
7267 if (TREE_CODE (fn) != TEMPLATE_DECL)
7268 {
7269 if (building_stmt_list_p ())
7270 add_stmt (build_stmt (input_location, USING_STMT, fn));
7271 else
e071b767
JJ
7272 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7273 false, false);
87c976ae
NS
7274 }
7275 }
98ed9dae 7276}
6097b0c3 7277
28ea4c88 7278#include "gt-cp-name-lookup.h"