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