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