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